From c1a6f0230c79787c9bf9e0d985878d7ec4b306ae Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 31 Jan 2019 18:01:14 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 5 ++ .../promise/09_transfer_events/index.js | 4 +- lerna.json | 2 +- packages/api-derive/package.json | 6 +- packages/api/package.json | 14 ++--- packages/api/src/Base.ts | 1 + packages/api/src/SubmittableExtrinsic.ts | 55 +++++++++++++++---- packages/api/src/promise/index.ts | 2 +- packages/api/src/rx/index.ts | 2 +- packages/api/test/e2e/promise-tx.spec.js | 35 ++++++++++-- packages/api/test/e2e/rx-tx.spec.js | 4 +- packages/rpc-core/package.json | 8 +-- packages/rpc-provider/package.json | 4 +- packages/rpc-rx/package.json | 6 +- packages/type-extrinsics/package.json | 4 +- packages/type-extrinsics/src/index.spec.ts | 11 +++- .../type-extrinsics/test/e2e/transfer.spec.js | 4 +- packages/type-jsonrpc/package.json | 2 +- packages/type-storage/package.json | 4 +- packages/types/package.json | 2 +- packages/types/src/Extrinsic.ts | 6 +- packages/types/src/ExtrinsicSignature.ts | 16 ++++-- packages/types/src/SignaturePayload.ts | 22 ++++++-- 23 files changed, 153 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d53eb4bf17..319275f680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 0.42.1 + +- Support for substrate hash signing with implVersion >= 18 +- Changed signatures for Extrinsic signing, this should not have (much) of an impact since it is generally not used directly. SubmittableExtrinsic (as exposed by the API), supports the old-style use. + # 0.41.1 - Support the V1 metadata specification from Substrate in addition to the currently testnet-active V0 version diff --git a/docs/examples/promise/09_transfer_events/index.js b/docs/examples/promise/09_transfer_events/index.js index 6c768c916b..dddf20f882 100755 --- a/docs/examples/promise/09_transfer_events/index.js +++ b/docs/examples/promise/09_transfer_events/index.js @@ -21,7 +21,7 @@ async function main () { const api = await ApiPromise.create(); // get the nonce for the admin key - const aliceNonce = await api.query.system.accountNonce(ALICE); + const nonce = await api.query.system.accountNonce(ALICE); // find the actual keypair in the keyring const alicePair = keyring.getPair(ALICE); @@ -34,7 +34,7 @@ async function main () { // Do the transfer and track the actual status api.tx.balances .transfer(recipient, AMOUNT) - .sign(alicePair, aliceNonce) + .sign(alicePair, { nonce }) .send(({ events = [], status, type }) => { console.log('Transaction status:', type); diff --git a/lerna.json b/lerna.json index 7334cd85d9..d34edf2f46 100644 --- a/lerna.json +++ b/lerna.json @@ -9,5 +9,5 @@ "packages": [ "packages/*" ], - "version": "0.41.14" + "version": "0.42.0" } diff --git a/packages/api-derive/package.json b/packages/api-derive/package.json index 11388dbde8..0bea390b06 100644 --- a/packages/api-derive/package.json +++ b/packages/api-derive/package.json @@ -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", diff --git a/packages/api/package.json b/packages/api/package.json index 73acedfda2..33fef4457a 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -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" } diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index d55859f2e5..506938ffa5 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -320,6 +320,7 @@ export default abstract class ApiBase 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); diff --git a/packages/api/src/SubmittableExtrinsic.ts b/packages/api/src/SubmittableExtrinsic.ts index c93cb3de71..3b192c866c 100644 --- a/packages/api/src/SubmittableExtrinsic.ts +++ b/packages/api/src/SubmittableExtrinsic.ts @@ -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 extends Observable ? Observable @@ -122,24 +132,47 @@ export default class SubmittableExtrinsic exten ) as unknown as SumbitableResultSubscription; } - sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash?: AnyU8a): SubmittableExtrinsic { - 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 { + // 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; - signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableResult) => any): SumbitableResultSubscription; - signAndSend (signerPair: KeyringPair, statusCb?: (result: SubmittableResult) => any): SumbitableResultResult | SumbitableResultSubscription { + signAndSend (account: KeyringPair, options?: Partial): SumbitableResultResult; + signAndSend (account: KeyringPair, statusCb: StatusCb): SumbitableResultSubscription; + signAndSend (account: KeyringPair, _options?: Partial | StatusCb, statusCb?: StatusCb): SumbitableResultResult | SumbitableResultSubscription { + let options: Partial = {}; + + 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) - .pipe( + () => ( + isUndefined(options.nonce) + ? this._api.query.system.accountNonce(account.address()) as Observable + : of(new Index(options.nonce)) + ).pipe( first(), switchMap((nonce) => { - this.sign(signerPair, nonce); + this.sign(account, { ...options, nonce }); return isSubscription ? this.subscribeObservable() diff --git a/packages/api/src/promise/index.ts b/packages/api/src/promise/index.ts index 014b80efdd..1b220f524f 100644 --- a/packages/api/src/promise/index.ts +++ b/packages/api/src/promise/index.ts @@ -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}`); diff --git a/packages/api/src/rx/index.ts b/packages/api/src/rx/index.ts index 357c939828..f6dc3876fb 100644 --- a/packages/api/src/rx/index.ts +++ b/packages/api/src/rx/index.ts @@ -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() * ) diff --git a/packages/api/test/e2e/promise-tx.spec.js b/packages/api/test/e2e/promise-tx.spec.js index aa4547fb6c..b7a65586c0 100644 --- a/packages/api/test/e2e/promise-tx.spec.js +++ b/packages/api/test/e2e/promise-tx.spec.js @@ -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); }); diff --git a/packages/api/test/e2e/rx-tx.spec.js b/packages/api/test/e2e/rx-tx.spec.js index def6f0894b..a2ab0c1f82 100644 --- a/packages/api/test/e2e/rx-tx.spec.js +++ b/packages/api/test/e2e/rx-tx.spec.js @@ -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() ) ) diff --git a/packages/rpc-core/package.json b/packages/rpc-core/package.json index 34cb7c9cc5..292b1d834a 100644 --- a/packages/rpc-core/package.json +++ b/packages/rpc-core/package.json @@ -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" } } diff --git a/packages/rpc-provider/package.json b/packages/rpc-provider/package.json index fffc13dbb6..4bb3c4a56f 100644 --- a/packages/rpc-provider/package.json +++ b/packages/rpc-provider/package.json @@ -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", diff --git a/packages/rpc-rx/package.json b/packages/rpc-rx/package.json index d1eafacd54..c6017a5e5d 100644 --- a/packages/rpc-rx/package.json +++ b/packages/rpc-rx/package.json @@ -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", diff --git a/packages/type-extrinsics/package.json b/packages/type-extrinsics/package.json index 08fd6a0edd..75f146ca77 100644 --- a/packages/type-extrinsics/package.json +++ b/packages/type-extrinsics/package.json @@ -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 ", @@ -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" } } diff --git a/packages/type-extrinsics/src/index.spec.ts b/packages/type-extrinsics/src/index.spec.ts index 6da72c154a..7bf17691fa 100644 --- a/packages/type-extrinsics/src/index.spec.ts +++ b/packages/type-extrinsics/src/index.spec.ts @@ -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 ])); diff --git a/packages/type-extrinsics/test/e2e/transfer.spec.js b/packages/type-extrinsics/test/e2e/transfer.spec.js index a5b17f02a5..6affaf7dd7 100644 --- a/packages/type-extrinsics/test/e2e/transfer.spec.js +++ b/packages/type-extrinsics/test/e2e/transfer.spec.js @@ -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); diff --git a/packages/type-jsonrpc/package.json b/packages/type-jsonrpc/package.json index aede88eb4a..a4dea6ce92 100644 --- a/packages/type-jsonrpc/package.json +++ b/packages/type-jsonrpc/package.json @@ -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": { diff --git a/packages/type-storage/package.json b/packages/type-storage/package.json index 8a882cc1d0..5801e55b1a 100644 --- a/packages/type-storage/package.json +++ b/packages/type-storage/package.json @@ -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" } diff --git a/packages/types/package.json b/packages/types/package.json index 254221659e..ad684aeaa6 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -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": [ diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index 709733df85..a5e310b3d4 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -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; } diff --git a/packages/types/src/ExtrinsicSignature.ts b/packages/types/src/ExtrinsicSignature.ts index d7bd3bd1e9..3d7bbfde5b 100644 --- a/packages/types/src/ExtrinsicSignature.ts +++ b/packages/types/src/ExtrinsicSignature.ts @@ -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); } diff --git a/packages/types/src/SignaturePayload.ts b/packages/types/src/SignaturePayload.ts index 5d83b651ef..9304f02aa7 100644 --- a/packages/types/src/SignaturePayload.ts +++ b/packages/types/src/SignaturePayload.ts @@ -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; }