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