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:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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()
|
||||
* )
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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()
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user