Nonce displayed & updated

This commit is contained in:
Jaco Greeff 2018-04-18 16:44:20 +02:00
parent 9263058345
commit 2e7c858ce9
25 changed files with 293 additions and 42 deletions

View File

@ -13,7 +13,7 @@
"test": "react-scripts test --env=jsdom --coverage"
},
"devDependencies": {
"@polkadot/rx-react": "^0.2.4",
"@polkadot/rx-react": "^0.2.7",
"@polkadot/util": "^0.18.6",
"react": "^16.3.1",
"react-dom": "^16.3.1",

View File

@ -13,7 +13,7 @@
"test": "react-scripts test --env=jsdom --coverage"
},
"devDependencies": {
"@polkadot/rx-react": "^0.2.4",
"@polkadot/rx-react": "^0.2.7",
"@polkadot/ui-react": "^0.9.10",
"@polkadot/util": "^0.18.6",
"@polkadot/util-keyring": "^0.18.6",

View File

@ -20,7 +20,7 @@ type Props = BaseProps & {
};
export default function Account ({ className, label, subject, style }: Props) {
const AccountBalance = withObservableParams(Balance, subject, ({ publicKey }: KeyringPair): Uint8Array => publicKey());
const AccountBalance = withObservableParams(Balance, subject);
return (
<div

View File

@ -12,20 +12,19 @@ import Dropdown from 'semantic-ui-react/dist/es/modules/Dropdown';
import Label from 'semantic-ui-react/dist/es/elements/Label';
import extrinsicName from '../subject/extrinsicName';
import calls from '../calls';
type Props = BaseProps & {};
const callOptions = [
{
text: 'Staking Transfer',
value: 'staking_transfer'
}
];
const onChange = (_, { value }) => {
const onChange = (event, { value }) => {
extrinsicName.next(value);
};
const options = Object.keys(calls).map((value) => ({
text: calls[value].description,
value
}));
export default function CallSelect ({ className, style }: Props) {
return (
<div
@ -37,7 +36,7 @@ export default function CallSelect ({ className, style }: Props) {
<Dropdown
selection
onChange={onChange}
options={callOptions}
options={options}
/>
</div>
</div>

View File

@ -29,7 +29,7 @@ const testOptions = Object.keys(keyring).map((name) => ({
}));
export default function InputAddress (props: Props): React$Node {
const onChange = (_, { value }) => {
const onChange = (event, { value }) => {
if (props.subject) {
props.subject.next(keyring[value]);
}

View File

@ -8,19 +8,47 @@ import type { BaseProps } from '@polkadot/portal/types';
import './Sender.css';
import React from 'react';
import Label from 'semantic-ui-react/dist/es/elements/Label';
import Nonce from '@polkadot/rx-react/Nonce';
import withObservableParams from '@polkadot/rx-react/with/observableParams';
import u8aToBn from '@polkadot/util/u8a/toBn';
import Account from '../Account';
import addrSender from '../subject/addrSender';
import nonceSender from '../subject/nonceSender';
type Props = BaseProps & {};
const onNonceChange = (value: Uint8Array) => {
if (!value || value.length !== 8) {
return;
}
const bn = u8aToBn(value, 64, true);
console.log('next nonce', bn.toString());
nonceSender.next(bn);
};
export default function Sender (props: Props) {
return (
const SenderNonce = withObservableParams(Nonce, addrSender);
return [
<Account
{...props}
className={['testing--Sender', props.className].join(' ')}
label='using the selected account'
subject={addrSender}
/>
);
/>,
<div className='testing--split'>
<div className='small'>
<Label>with an future index</Label>
<SenderNonce
className='ui disabled dropdown selection'
onChange={onNonceChange}
/>
</div>
</div>
];
}

View File

@ -0,0 +1,9 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
}: Calls);

View File

@ -0,0 +1,9 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
}: Calls);

View File

@ -0,0 +1,9 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
}: Calls);

View File

@ -0,0 +1,9 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
}: Calls);

View File

@ -0,0 +1,48 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
const bnToU8a = require('@polkadot/util/bn/toU8a');
const u8aConcat = require('@polkadot/util/u8a/concat');
const consensus = require('./consensus');
const council = require('./council');
const councilVoting = require('./councilVoting');
const democracy = require('./democracy');
const session = require('./session');
const staking = require('./staking');
const calls = {
consensus, // 0
session, // 1
staking, // 2
democracy, // 3
council, // 4
councilVoting // 5
};
const flattenned: Calls = Object
.keys(calls)
.reduce((flat, sectionName, index) => {
const section = calls[sectionName];
const sectionIndex = bnToU8a(index, 8);
Object
.keys(section)
.reduce((flat, method, index) => {
const name = `${sectionName}_${method}`;
const methodIndex = bnToU8a(index, 8);
flat[name] = section[method];
flat[name].index = u8aConcat(sectionIndex, methodIndex);
return flat;
}, flat);
return flat;
}, ({}: $Shape<FlatCalls>));
module.exports = flattenned;

View File

@ -0,0 +1,9 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
}: Calls);

View File

@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Calls } from './types';
module.exports = ({
transfer: {
description: 'Staking Transfer',
params: ['AccountId', 'Balance']
},
stake: {
description: 'Stake',
params: []
},
unstake: {
description: 'Unstake',
params: []
}
}: Calls);

View File

@ -0,0 +1,18 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
export type Call$Type = 'AccountId' | 'Balance' | 'BlockNumber';
export type Call$Params = Array<Call$Type>;
export type Call = {
description: string,
index: Uint8Array,
params: Call$Params
};
export type Calls = {
[string]: $Shape<Call>
}

View File

@ -0,0 +1,33 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type BN from 'bn.js';
import type { Call } from '../calls/types';
const bnToU8a = require('@polkadot/util/bn/toU8a');
const u8aConcat = require('@polkadot/util/u8a/concat');
const u8aToHex = require('@polkadot/util/u8a/toHex');
const encodeParams = require('./params');
// flowlint-next-line unclear-type:off
module.exports = function encode (sender: KeyringPair, nonce: number | BN, { index, params }: Call, values: Array<mixed>) {
const encodedParams = encodeParams(values, params);
const message = u8aConcat(
sender.publicKey(),
bnToU8a(nonce, 64, true),
index,
encodedParams
);
const signature = sender.sign(message);
console.log('message', u8aToHex(message));
console.log('signature', u8aToHex(signature));
return {
message,
signature
};
};

View File

@ -0,0 +1,23 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Call$Type } from '../../calls/types';
const bnToU8a = require('@polkadot/util/bn/toU8a');
// flowlint-next-line unclear-type:off
module.exports = function encodeValue (value: any, type: Call$Type): Uint8Array {
switch (type) {
case 'AccountId':
return value;
case 'Balance':
case 'BlockNumber':
return bnToU8a(value, 64, true);
default:
throw new Error(`Unable to encode value with type '${type}'`);
}
};

View File

@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { Call$Params } from '../../calls/types';
const u8aConcat = require('@polkadot/util/u8a/concat');
const encodeParam = require('./param');
// flowlint-next-line unclear-type:off
module.exports = function encodeParams (values: Array<mixed>, params: Call$Params): Uint8Array {
return u8aConcat.apply(null, values.map((value, index) => {
return encodeParam(value, params[index]);
}));
};

View File

@ -21,26 +21,21 @@
}
.testing--split > div.full {
flex: 1 100%;
flex: 0 100%;
min-width: 0;
}
.testing--split > div.large {
flex: 1 75%;
flex: 0 75%;
min-width: 0;
}
.testing--split > div.medium {
flex: 1 50%;
flex: 0 50%;
min-width: 0;
}
.testing--split > div.small {
flex: 1 25%;
min-width: 0;
}
.testing--split > div:only-child {
flex-grow: 0;
flex: 0 25%;
min-width: 0;
}

View File

@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { KeyringPair } from '@polkadot/uti-keyring/types';
import { map } from 'rxjs/operators/map';
import { ReplaySubject } from 'rxjs/ReplaySubject';
export default function addr (): $rxjs$ReplaySubject {
return new ReplaySubject(1).pipe(
map((pair: KeyringPair) => {
if (!pair) {
return new Uint8Array([]);
}
return pair.publicKey();
})
);
}

View File

@ -3,8 +3,8 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import base from './base';
import addr from './addr';
const subject = base();
const subject = addr();
export default subject;

View File

@ -3,8 +3,8 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import base from './base';
import addr from './addr';
const subject = base();
const subject = addr();
export default subject;

View File

@ -3,8 +3,8 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import base from './base';
import { ReplaySubject } from 'rxjs/ReplaySubject';
const subject = base();
const subject = new ReplaySubject(1);
export default subject;

View File

@ -3,8 +3,11 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import BN from 'bn.js';
import { ReplaySubject } from 'rxjs/ReplaySubject';
export default function base () {
return new ReplaySubject(1);
}
const subject = new ReplaySubject(1);
subject.next(new BN(0));
export default subject;

View File

@ -16,7 +16,7 @@
"devDependencies": {
"@polkadot/app-home": "^0.1.0",
"@polkadot/app-testing": "^0.1.0",
"@polkadot/rx-react": "^0.2.4",
"@polkadot/rx-react": "^0.2.7",
"@polkadot/ui-react": "^0.9.10",
"i18next": "^11.1.1",
"i18next-browser-languagedetector": "^2.2.0",

View File

@ -1299,19 +1299,19 @@
"@polkadot/util" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/rx-api@^0.2.4":
version "0.2.4"
resolved "https://registry.yarnpkg.com/@polkadot/rx-api/-/rx-api-0.2.4.tgz#e49c88d2d68a4e53d9d2ab0e307c564aa2ac5c20"
"@polkadot/rx-api@^0.2.7":
version "0.2.7"
resolved "https://registry.yarnpkg.com/@polkadot/rx-api/-/rx-api-0.2.7.tgz#aef716e7e8d3740746307331d242043c7694a8fd"
dependencies:
"@polkadot/api" "^0.8.14"
"@polkadot/api-provider" "^0.8.14"
rxjs "^5.5.10"
"@polkadot/rx-react@^0.2.4":
version "0.2.4"
resolved "https://registry.yarnpkg.com/@polkadot/rx-react/-/rx-react-0.2.4.tgz#fe142b564db5918c5207d8ae1cde86a3091caa84"
"@polkadot/rx-react@^0.2.7":
version "0.2.7"
resolved "https://registry.yarnpkg.com/@polkadot/rx-react/-/rx-react-0.2.7.tgz#5ec876ddf866e1e8ea2a70ddb484c1872102e2ac"
dependencies:
"@polkadot/rx-api" "^0.2.4"
"@polkadot/rx-api" "^0.2.7"
"@polkadot/util" "^0.18.6"
"@polkadot/ui-identicon@^0.9.10":