Cater for signature over hash (with version checks) (#655)

* Cater for signature over hash (with version checks)

* Allow Submittable signAndSend

* isUndefined for nonce check

* .sign with backwards compat

* e2e tests started

* Work around optional callback for Promise

* Commit actual e2e test

* Promise allows for submitExtrinsic

* e2e tests are passing

* Bump version, update CHANGELOG
This commit is contained in:
Jaco Greeff
2019-01-31 18:01:14 +01:00
committed by GitHub
parent 8da2b1c451
commit c1a6f0230c
23 changed files with 153 additions and 66 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-derive",
"version": "0.41.14",
"version": "0.42.0",
"description": "Common functions used across Polkadot, derived from RPC calls and storage queries.",
"main": "index.js",
"keywords": [
@@ -31,8 +31,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/api": "^0.41.14",
"@polkadot/types": "^0.41.14",
"@polkadot/api": "^0.42.0",
"@polkadot/types": "^0.42.0",
"@types/memoizee": "^0.4.2",
"@types/rx": "^4.1.1",
"memoizee": "^0.4.14",
+7 -7
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "0.41.14",
"version": "0.42.0",
"description": "An RxJS wrapper around the Polkadot JS RPC",
"main": "index.js",
"keywords": [
@@ -30,12 +30,12 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/api-derive": "^0.41.14",
"@polkadot/extrinsics": "^0.41.14",
"@polkadot/rpc-provider": "^0.41.14",
"@polkadot/rpc-rx": "^0.41.14",
"@polkadot/storage": "^0.41.14",
"@polkadot/types": "^0.41.14",
"@polkadot/api-derive": "^0.42.0",
"@polkadot/extrinsics": "^0.42.0",
"@polkadot/rpc-provider": "^0.42.0",
"@polkadot/rpc-rx": "^0.42.0",
"@polkadot/storage": "^0.42.0",
"@polkadot/types": "^0.42.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.3"
}
+1
View File
@@ -320,6 +320,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
this._derive = this.decorateDerive(this._rx as ApiInterface$Rx, this.onCall);
this._rx.genesisHash = this._genesisHash;
this._rx.runtimeVersion = this._runtimeVersion;
this._rx.tx = this.decorateExtrinsics(extrinsics, rxOnCall);
this._rx.query = this.decorateStorage(storage, rxOnCall);
this._rx.derive = this.decorateDerive(this._rx as ApiInterface$Rx, rxOnCall);
+44 -11
View File
@@ -3,16 +3,26 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair } from '@polkadot/keyring/types';
import { Extrinsic, ExtrinsicStatus, EventRecord, Hash, Index, Method, SignedBlock, Text } from '@polkadot/types/index';
import { Struct, Vector } from '@polkadot/types/codec';
import { Extrinsic, ExtrinsicStatus, EventRecord, Hash, Index, Method, RuntimeVersion, SignedBlock, Text } from '@polkadot/types/index';
import { AnyNumber, AnyU8a, CodecCallback } from '@polkadot/types/types';
import { ApiInterface$Rx, ApiType, OnCallDefinition } from './types';
import { Observable, of, combineLatest } from 'rxjs';
import { first, map, switchMap } from 'rxjs/operators';
import { Struct, Vector } from '@polkadot/types/codec';
import { isBn, isFunction, isNumber, isUndefined } from '@polkadot/util';
import filterEvents from './util/filterEvents';
type SignatureOptionsPartial = {
blockHash?: AnyU8a,
era?: Uint8Array,
nonce: AnyNumber,
version?: RuntimeVersion
};
type StatusCb = (result: SubmittableResult) => any;
type SumbitableResultResult<CodecResult, SubscriptionResult> =
CodecResult extends Observable<any>
? Observable<SubmittableResult>
@@ -122,24 +132,47 @@ export default class SubmittableExtrinsic<CodecResult, SubscriptionResult> exten
) as unknown as SumbitableResultSubscription<CodecResult, SubscriptionResult>;
}
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash?: AnyU8a): SubmittableExtrinsic<CodecResult, SubscriptionResult> {
super.sign(signerPair, nonce, blockHash || this._api.genesisHash);
// NOTE with the nonce hack, we don't add a signature overload for 2 reasons -
// - it makes it incompatible with the base Extrinsic
// - for those using TS, we would like to move them towards the new version
sign (account: KeyringPair, _options: SignatureOptionsPartial): SubmittableExtrinsic<CodecResult, SubscriptionResult> {
// HACK here we actually override nonce if it was specified (backwards compat for
// the previous signature - don't let userspace break, but allow then time to upgrade)
const options: SignatureOptionsPartial = isBn(_options) || isNumber(_options)
? { nonce: _options as any as number }
: _options;
super.sign(account, {
blockHash: this._api.genesisHash,
version: this._api.runtimeVersion,
...options
});
return this;
}
signAndSend (signerPair: KeyringPair): SumbitableResultResult<CodecResult, SubscriptionResult>;
signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableResult) => any): SumbitableResultSubscription<CodecResult, SubscriptionResult>;
signAndSend (signerPair: KeyringPair, statusCb?: (result: SubmittableResult) => any): SumbitableResultResult<CodecResult, SubscriptionResult> | SumbitableResultSubscription<CodecResult, SubscriptionResult> {
signAndSend (account: KeyringPair, options?: Partial<SignatureOptionsPartial>): SumbitableResultResult<CodecResult, SubscriptionResult>;
signAndSend (account: KeyringPair, statusCb: StatusCb): SumbitableResultSubscription<CodecResult, SubscriptionResult>;
signAndSend (account: KeyringPair, _options?: Partial<SignatureOptionsPartial> | StatusCb, statusCb?: StatusCb): SumbitableResultResult<CodecResult, SubscriptionResult> | SumbitableResultSubscription<CodecResult, SubscriptionResult> {
let options: Partial<SignatureOptionsPartial> = {};
if (isFunction(_options)) {
statusCb = _options;
} else {
options = _options || {};
}
const isSubscription = this._noStatusCb || !!statusCb;
return this._onCall(
() => (this._api.query.system
.accountNonce(signerPair.address()) as Observable<Index>)
.pipe(
() => (
isUndefined(options.nonce)
? this._api.query.system.accountNonce(account.address()) as Observable<Index>
: of(new Index(options.nonce))
).pipe(
first(),
switchMap((nonce) => {
this.sign(signerPair, nonce);
this.sign(account, { ...options, nonce });
return isSubscription
? this.subscribeObservable()
+1 -1
View File
@@ -89,7 +89,7 @@ import Combinator, { CombinatorCallback, CombinatorFunction } from './Combinator
* // create transfer
* transfer(keyring.bob.address(), 12345)
* // sign the transcation
* .sign(keyring.alice, nonce)
* .sign(keyring.alice, { nonce })
* // send the transaction (optional status callback)
* .send((status) => {
* console.log(`current status ${status.type}`);
+1 -1
View File
@@ -99,7 +99,7 @@ import ApiBase from '../Base';
* // create transfer
* .transfer(keyring.bob.address(), 12345)
* // sign the transcation
* .sign(keyring.alice, nonce)
* .sign(keyring.alice, { nonce })
* // send the transaction
* .send()
* )
+29 -6
View File
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import testingPairs from '@polkadot/keyring/testingPairs';
import { randomAsHex } from '@polkadot/util-crypto';
import Api from '../../src/promise';
@@ -27,6 +28,30 @@ describe.skip('e2e transactions', () => {
it('makes a transfer (sign, then send)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address());
return api.tx.balances
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, { nonce })
.send(({ events, status, type }) => {
console.log('Transaction status:', type);
if (type === 'Finalised') {
console.log('Completed at block hash', status.value.toHex());
console.log('Events:');
events.forEach(({ phase, event: { data, method, section } }) => {
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
});
if (events.length) {
done();
}
}
});
});
it('makes a transfer (sign, then send - compat version)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address());
return api.tx.balances
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, nonce)
@@ -78,13 +103,11 @@ describe.skip('e2e transactions', () => {
});
it('makes a proposal', async () => {
const nonce = await api.query.system.accountNonce(keyring.alice.address());
// don't wait for status, just get hash
// don't wait for status, just get hash. Here we generate a large-ish payload
// to ensure that we can sign with the hashed version as well (and have it accepted)
const hash = await api.tx.democracy
.propose(api.tx.consensus.setCode('0xdeadbeef'), 10000)
.sign(keyring.alice, nonce)
.send();
.propose(api.tx.consensus.setCode(randomAsHex(4096)), 10000)
.signAndSend(keyring.bob);
expect(hash.toHex()).toHaveLength(66);
});
+2 -2
View File
@@ -30,7 +30,7 @@ describe.skip('e2e transactions', () => {
switchMap((nonce) =>
api.tx.balances
.transfer(keyring.bob.address(), 12345)
.sign(keyring.alice, nonce)
.sign(keyring.alice, { nonce })
.send()
)
)
@@ -49,7 +49,7 @@ describe.skip('e2e transactions', () => {
switchMap((nonce) =>
api.tx.democracy
.propose(api.tx.consensus.setCode('0xdeadbeef'), 10000)
.sign(keyring.alice, nonce)
.sign(keyring.alice, { nonce })
.send()
)
)
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-core",
"version": "0.41.14",
"version": "0.42.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
@@ -30,9 +30,9 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-core#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/jsonrpc": "^0.41.14",
"@polkadot/rpc-provider": "^0.41.14",
"@polkadot/types": "^0.41.14",
"@polkadot/jsonrpc": "^0.42.0",
"@polkadot/rpc-provider": "^0.42.0",
"@polkadot/types": "^0.42.0",
"@polkadot/util": "^0.33.36"
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-provider",
"version": "0.41.14",
"version": "0.42.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
@@ -31,7 +31,7 @@
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/keyring": "^0.33.36",
"@polkadot/storage": "^0.41.14",
"@polkadot/storage": "^0.42.0",
"@polkadot/util": "^0.33.36",
"@polkadot/util-crypto": "^0.33.36",
"@types/nock": "^9.3.0",
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/rpc-rx",
"version": "0.41.14",
"version": "0.42.0",
"description": "An RxJs wrapper around the Polkadot JS API",
"main": "index.js",
"keywords": [
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-rx#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/rpc-core": "^0.41.14",
"@polkadot/rpc-provider": "^0.41.14",
"@polkadot/rpc-core": "^0.42.0",
"@polkadot/rpc-provider": "^0.42.0",
"@types/memoizee": "^0.4.2",
"@types/rx": "^4.1.1",
"memoizee": "^0.4.14",
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/extrinsics",
"version": "0.41.14",
"version": "0.42.0",
"main": "index.js",
"repository": "https://github.com/polkadot-js/api.git",
"author": "Jaco Greeff <jacogr@gmail.com>",
@@ -11,7 +11,7 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/types": "^0.41.14",
"@polkadot/types": "^0.42.0",
"@polkadot/util": "^0.33.36"
}
}
+8 -3
View File
@@ -16,8 +16,10 @@ describe('extrinsics', () => {
extrinsics.timestamp.set(10101)
).sign(
keyring.nobody,
1234,
new Uint8Array()
{
blockHash: new Uint8Array(),
nonce: 1234
}
).toU8a(true)
).toEqual(new Uint8Array([
129, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73, 19, 0, 0, 0, 213, 157
@@ -28,7 +30,10 @@ describe('extrinsics', () => {
expect(
new Extrinsic(
extrinsics.balances.transfer(keyring.bob.publicKey(), 6969)
).sign(keyring.alice, 0, '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f').toU8a()
).sign(keyring.alice, {
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
nonce: 0
}).toU8a()
).toEqual(new Uint8Array([
37, 2, 129, 255, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79, 245, 228, 136, 9, 232, 31, 206, 95, 236, 20, 206, 189, 109, 251, 83, 198, 37, 79, 144, 90, 72, 33, 101, 250, 52, 0, 66, 80, 222, 78, 34, 124, 6, 101, 244, 44, 168, 225, 239, 153, 185, 58, 113, 221, 221, 136, 43, 237, 9, 6, 201, 17, 56, 110, 105, 78, 173, 109, 176, 100, 24, 78, 164, 15, 0, 0, 3, 0, 255, 215, 86, 142, 95, 10, 126, 218, 103, 168, 38, 145, 255, 55, 154, 196, 187, 164, 249, 201, 184, 89, 254, 119, 155, 93, 70, 54, 59, 97, 173, 45, 185, 229, 108
]));
@@ -30,7 +30,7 @@ describe.skip('e2e transfer', () => {
.then((genesisHash) => {
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969);
extrinsic.sign(keyring.alice, 0, genesisHash);
extrinsic.sign(keyring.alice, { blockHash: genesisHash, nonce: 0 });
return api.author.submitExtrinsic(extrinsic.toU8a());
})
@@ -43,7 +43,7 @@ describe.skip('e2e transfer', () => {
.then((genesisHash) => {
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969);
extrinsic.sign(keyring.alice, 0, genesisHash);
extrinsic.sign(keyring.alice, { blockHash: genesisHash, nonce: 0 });
return api.author.submitAndWatchExtrinsic(extrinsic, (status) => {
console.log(status);
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/jsonrpc",
"version": "0.41.14",
"version": "0.42.0",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/storage",
"version": "0.41.14",
"version": "0.42.0",
"description": "Generic wrapper wrapper around state storage",
"main": "index.js",
"engines": {
@@ -31,7 +31,7 @@
"dependencies": {
"@babel/runtime": "^7.2.0",
"@polkadot/keyring": "^0.33.36",
"@polkadot/types": "^0.41.14",
"@polkadot/types": "^0.42.0",
"@polkadot/util": "^0.33.36",
"@polkadot/util-crypto": "^0.33.36"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/types",
"version": "0.41.14",
"version": "0.42.0",
"description": "Implementation of the Parity codec",
"main": "index.js",
"keywords": [
+3 -3
View File
@@ -11,7 +11,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from './codec/Compact';
import Struct from './codec/Struct';
import Address from './Address';
import ExtrinsicSignature from './ExtrinsicSignature';
import ExtrinsicSignature, { SignatureOptions } from './ExtrinsicSignature';
import Hash from './Hash';
import { FunctionMetadata } from './Metadata/v0/Modules';
import Method from './Method';
@@ -156,8 +156,8 @@ export default class Extrinsic extends Struct {
/**
* @description Sign the extrinsic with a specific keypair
*/
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a, era?: Uint8Array): Extrinsic {
this.signature.sign(this.method, signerPair, nonce, blockHash, era);
sign (account: KeyringPair, options: SignatureOptions): Extrinsic {
this.signature.sign(this.method, account, options);
return this;
}
+12 -4
View File
@@ -10,10 +10,18 @@ import Address from './Address';
import ExtrinsicEra from './ExtrinsicEra';
import Method from './Method';
import Nonce from './NonceCompact';
import RuntimeVersion from './RuntimeVersion';
import Signature from './Signature';
import SignaturePayload from './SignaturePayload';
import U8 from './U8';
export type SignatureOptions = {
blockHash: AnyU8a,
era?: Uint8Array,
nonce: AnyNumber,
version?: RuntimeVersion
};
export const IMMORTAL_ERA = new Uint8Array([0]);
const BIT_SIGNED = 0b10000000;
@@ -139,15 +147,15 @@ export default class ExtrinsicSignature extends Struct {
/**
* @description Generate a payload and pplies the signature from a keypair
*/
sign (method: Method, signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a, era: Uint8Array = IMMORTAL_ERA): ExtrinsicSignature {
const signer = new Address(signerPair.publicKey());
sign (method: Method, account: KeyringPair, { blockHash, era, nonce, version }: SignatureOptions): ExtrinsicSignature {
const signer = new Address(account.publicKey());
const signingPayload = new SignaturePayload({
nonce,
method,
era,
era: era || IMMORTAL_ERA,
blockHash
});
const signature = new Signature(signingPayload.sign(signerPair));
const signature = new Signature(signingPayload.sign(account, version));
return this.injectSignature(signature, signer, signingPayload.nonce, signingPayload.era);
}
+17 -5
View File
@@ -5,11 +5,14 @@
import { KeyringPair } from '@polkadot/keyring/types';
import { AnyNumber, AnyU8a } from './types';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Struct from './codec/Struct';
import Method from './Method';
import Hash from './Hash';
import Nonce from './NonceCompact';
import ExtrinsicEra from './ExtrinsicEra';
import Hash from './Hash';
import Method from './Method';
import Nonce from './NonceCompact';
import RuntimeVersion from './RuntimeVersion';
type SignaturePayloadValue = {
nonce?: AnyNumber,
@@ -90,8 +93,17 @@ export default class SignaturePayload extends Struct {
/**
* @description Sign the payload with the keypair
*/
sign (signerPair: KeyringPair): Uint8Array {
this._signature = signerPair.sign(this.toU8a());
sign (signerPair: KeyringPair, version?: RuntimeVersion): Uint8Array {
// for newer api versions, hash when length > 256
const isLegacy = !version ||
(version.specName.eq('node') && version.specVersion.ltn(18)) ||
(version.specName.eq('polkadot') && version.specVersion.ltn(107));
const u8a = this.toU8a();
const encoded = !isLegacy && (u8a.length > 256)
? blake2AsU8a(u8a)
: u8a;
this._signature = signerPair.sign(encoded);
return this._signature;
}