Support new v3 extrinsic (payload with genesisHash) (#1248)
* Make a start, copy stuff * Payload adjusted * Linting * Small cleanups after testing * Some duplication cleanups (yes... this needs work) * Slightly less duplication (still not great) * Typo in getter
This commit is contained in:
@@ -18,6 +18,7 @@ describe('extrinsics', (): void => {
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
|
||||
).sign(keyring.alice, {
|
||||
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
nonce: 0
|
||||
}).toHex()
|
||||
).toEqual(
|
||||
|
||||
@@ -178,7 +178,8 @@ export default function createSubmittableExtrinsic<ApiType> (
|
||||
blockHash: api.genesisHash,
|
||||
version: api.runtimeVersion,
|
||||
...options,
|
||||
...extras
|
||||
...extras,
|
||||
genesisHash: api.genesisHash
|
||||
} as unknown as SignatureOptions;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,11 @@ import Address from '../Generic/Address';
|
||||
import Call from '../Generic/Call';
|
||||
import ExtrinsicV1, { ExtrinsicValueV1 } from './v1/Extrinsic';
|
||||
import ExtrinsicV2, { ExtrinsicValueV2 } from './v2/Extrinsic';
|
||||
import ExtrinsicV3, { ExtrinsicValueV3 } from './v3/Extrinsic';
|
||||
import ExtrinsicEra from './ExtrinsicEra';
|
||||
import { BIT_SIGNED, BIT_UNSIGNED, DEFAULT_VERSION, UNMASK_VERSION } from './constants';
|
||||
|
||||
type ExtrinsicValue = ExtrinsicValueV1 | ExtrinsicValueV2;
|
||||
type ExtrinsicValue = ExtrinsicValueV1 | ExtrinsicValueV2 | ExtrinsicValueV3;
|
||||
|
||||
interface ExtrinsicOptions {
|
||||
version?: number;
|
||||
@@ -35,12 +36,12 @@ interface ExtrinsicOptions {
|
||||
* - signed, to create a transaction
|
||||
* - left as is, to create an inherent
|
||||
*/
|
||||
export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implements IExtrinsic {
|
||||
export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2 | ExtrinsicV3> implements IExtrinsic {
|
||||
public constructor (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: ExtrinsicOptions = {}) {
|
||||
super(Extrinsic.decodeExtrinsic(value, version));
|
||||
}
|
||||
|
||||
private static newFromValue (value: any, version: number): ExtrinsicV1 | ExtrinsicV2 {
|
||||
private static newFromValue (value: any, version: number): ExtrinsicV1 | ExtrinsicV2 | ExtrinsicV3 {
|
||||
if (value instanceof Extrinsic) {
|
||||
return value.raw;
|
||||
}
|
||||
@@ -51,11 +52,12 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
|
||||
switch (type) {
|
||||
case 1: return new ExtrinsicV1(value, { isSigned });
|
||||
case 2: return new ExtrinsicV2(value, { isSigned });
|
||||
case 3: return new ExtrinsicV3(value, { isSigned });
|
||||
default: throw new Error(`Unsupported extrinsic version ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicV1 | ExtrinsicV2 {
|
||||
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicV1 | ExtrinsicV2 | ExtrinsicV3 {
|
||||
if (Array.isArray(value) || isHex(value)) {
|
||||
// Instead of the block below, it should simply be:
|
||||
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
|
||||
@@ -90,7 +92,7 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
|
||||
return Extrinsic.newFromValue(value, version);
|
||||
}
|
||||
|
||||
private static decodeU8a (value: Uint8Array): ExtrinsicV1 | ExtrinsicV2 {
|
||||
private static decodeU8a (value: Uint8Array): ExtrinsicV1 | ExtrinsicV2 | ExtrinsicV3 {
|
||||
return Extrinsic.newFromValue(value.subarray(1), value[0]);
|
||||
}
|
||||
|
||||
@@ -226,8 +228,10 @@ export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implement
|
||||
// @ts-ignore
|
||||
if (args.length === 2) {
|
||||
payload = {
|
||||
blockHash: new Uint8Array(),
|
||||
// @ts-ignore
|
||||
era: args[1] as string,
|
||||
genesisHash: new Uint8Array(),
|
||||
method: this.method.toHex(),
|
||||
nonce: args[0] as string,
|
||||
tip: 0
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Hash, Index } from '../../interfaces/runtime';
|
||||
import { IKeyringPair } from '../../types';
|
||||
import { ExtrinsicPayloadValue, IKeyringPair } from '../../types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -11,8 +11,9 @@ import createType from '../../codec/createType';
|
||||
import Base from '../../codec/Base';
|
||||
import Compact from '../../codec/Compact';
|
||||
import U8a from '../../codec/U8a';
|
||||
import ExtrinsicPayloadV1, { ExtrinsicPayloadValueV1 } from './v1/ExtrinsicPayload';
|
||||
import ExtrinsicPayloadV2, { ExtrinsicPayloadValueV2 } from './v2/ExtrinsicPayload';
|
||||
import ExtrinsicPayloadV1 from './v1/ExtrinsicPayload';
|
||||
import ExtrinsicPayloadV2 from './v2/ExtrinsicPayload';
|
||||
import ExtrinsicPayloadV3 from './v3/ExtrinsicPayload';
|
||||
import ExtrinsicEra from './ExtrinsicEra';
|
||||
import { DEFAULT_VERSION } from './constants';
|
||||
|
||||
@@ -26,14 +27,14 @@ interface ExtrinsicPayloadOptions {
|
||||
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
|
||||
* on the contents included
|
||||
*/
|
||||
export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | ExtrinsicPayloadV2> {
|
||||
public constructor (value: Partial<ExtrinsicPayloadValueV1> | Partial<ExtrinsicPayloadValueV2> | Uint8Array | string | undefined, { version }: ExtrinsicPayloadOptions = {}) {
|
||||
export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | ExtrinsicPayloadV2 | ExtrinsicPayloadV3> {
|
||||
public constructor (value: Partial<ExtrinsicPayloadValue> | Uint8Array | string | undefined, { version }: ExtrinsicPayloadOptions = {}) {
|
||||
super(
|
||||
ExtrinsicPayload.decodeExtrinsicPayload(value, version)
|
||||
ExtrinsicPayload.decodeExtrinsicPayload(value as ExtrinsicPayloadValue, version)
|
||||
);
|
||||
}
|
||||
|
||||
public static decodeExtrinsicPayload (value: ExtrinsicPayload | Partial<ExtrinsicPayloadValueV1> | Partial<ExtrinsicPayloadValueV2> | Uint8Array | string | undefined, version: number = DEFAULT_VERSION): ExtrinsicPayloadV1 | ExtrinsicPayloadV2 {
|
||||
public static decodeExtrinsicPayload (value: ExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string | undefined, version: number = DEFAULT_VERSION): ExtrinsicPayloadV1 | ExtrinsicPayloadV2 | ExtrinsicPayloadV3 {
|
||||
if (value instanceof ExtrinsicPayload) {
|
||||
return value.raw;
|
||||
}
|
||||
@@ -41,6 +42,7 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | Extrinsi
|
||||
switch (version) {
|
||||
case 1: return new ExtrinsicPayloadV1(value as Uint8Array);
|
||||
case 2: return new ExtrinsicPayloadV2(value as Uint8Array);
|
||||
case 3: return new ExtrinsicPayloadV3(value as Uint8Array);
|
||||
default: throw new Error(`Unsupported extrinsic version ${version}`);
|
||||
}
|
||||
}
|
||||
@@ -52,13 +54,6 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | Extrinsi
|
||||
return this.raw.blockHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[U8a]] contained in the payload
|
||||
*/
|
||||
public get method (): U8a {
|
||||
return this.raw.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicEra]]
|
||||
*/
|
||||
@@ -66,6 +61,21 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | Extrinsi
|
||||
return this.raw.era;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The genesis block [[Hash]] the signature applies to
|
||||
*/
|
||||
public get genesisHash (): Hash {
|
||||
// NOTE only v3
|
||||
return (this.raw as ExtrinsicPayloadV3).genesisHash || createType('Hash');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[U8a]] contained in the payload
|
||||
*/
|
||||
public get method (): U8a {
|
||||
return this.raw.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Index]]
|
||||
*/
|
||||
@@ -77,7 +87,8 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadV1 | Extrinsi
|
||||
* @description The [[Balance]]
|
||||
*/
|
||||
public get tip (): Compact<Balance> {
|
||||
return (this.raw as ExtrinsicPayloadV2).tip || createType('Compact<Balance>', 0);
|
||||
// NOTE from v2
|
||||
return (this.raw as ExtrinsicPayloadV2).tip || createType('Compact<Balance>');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from '../../types';
|
||||
|
||||
export interface ExtrinsicOptions {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
|
||||
export interface ExtrinsicSignatureOptions {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
|
||||
export interface ExtrinsicExtraValue {
|
||||
era?: Uint8Array;
|
||||
nonce?: AnyNumber;
|
||||
tip?: AnyNumber;
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicOptions } from '../types';
|
||||
|
||||
import { isU8a } from '@polkadot/util';
|
||||
|
||||
@@ -16,10 +17,6 @@ export interface ExtrinsicValueV1 {
|
||||
signature?: ExtrinsicSignature;
|
||||
}
|
||||
|
||||
interface ExtrinsicV1Options {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
|
||||
const TRANSACTION_VERSION = 1;
|
||||
|
||||
/**
|
||||
@@ -28,7 +25,7 @@ const TRANSACTION_VERSION = 1;
|
||||
* The first generation of compact extrinsics
|
||||
*/
|
||||
export default class ExtrinsicV1 extends Struct implements IExtrinsicImpl {
|
||||
public constructor (value?: Uint8Array | ExtrinsicValueV1, { isSigned }: ExtrinsicV1Options = {}) {
|
||||
public constructor (value?: Uint8Array | ExtrinsicValueV1, { isSigned }: ExtrinsicOptions = {}) {
|
||||
super({
|
||||
signature: ExtrinsicSignature,
|
||||
method: Call
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Hash, Index } from '../../../interfaces/runtime';
|
||||
import { AnyNumber, AnyU8a, ExtrinsicPayloadValue, IExtrinsicEra, IKeyringPair, IMethod } from '../../../types';
|
||||
import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
|
||||
|
||||
import { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
@@ -12,13 +12,6 @@ import U8a from '../../../codec/U8a';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import { sign } from '../util';
|
||||
|
||||
export interface ExtrinsicPayloadValueV1 {
|
||||
blockHash: AnyU8a;
|
||||
era: AnyU8a | IExtrinsicEra;
|
||||
method: AnyU8a | IMethod;
|
||||
nonce: AnyNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicPayloadV1
|
||||
* @description
|
||||
@@ -31,7 +24,7 @@ export interface ExtrinsicPayloadValueV1 {
|
||||
* 32 bytes: The hash of the authoring block implied by the Transaction Era and the current block.
|
||||
*/
|
||||
export default class ExtrinsicPayloadV1 extends Struct {
|
||||
public constructor (value?: ExtrinsicPayloadValue | ExtrinsicPayloadValueV1 | Uint8Array | string) {
|
||||
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
|
||||
super({
|
||||
nonce: ClassOf('Compact<Index>'),
|
||||
method: U8a,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import { Address, Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicSignatureOptions } from '../types';
|
||||
|
||||
import createType, { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
@@ -13,10 +14,6 @@ import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
|
||||
import ExtrinsicPayload from './ExtrinsicPayload';
|
||||
|
||||
interface ExtrinsicSignatureV1Options {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicSignature
|
||||
* @description
|
||||
@@ -28,7 +25,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
|
||||
// 64 bytes: The sr25519/ed25519 signature of the Signing Payload
|
||||
// 1-8 bytes: The Compact<Nonce> of the signing account
|
||||
// 1/2 bytes: The Transaction Era
|
||||
public constructor (value?: ExtrinsicSignatureV1 | Uint8Array, { isSigned }: ExtrinsicSignatureV1Options = {}) {
|
||||
public constructor (value?: ExtrinsicSignatureV1 | Uint8Array, { isSigned }: ExtrinsicSignatureOptions = {}) {
|
||||
super({
|
||||
signer: ClassOf('Address'),
|
||||
signature: ClassOf('Signature'),
|
||||
@@ -123,13 +120,15 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
|
||||
/**
|
||||
* @description Generate a payload and pplies the signature from a keypair
|
||||
*/
|
||||
public sign (method: Call, account: IKeyringPair, { blockHash, era, nonce }: SignatureOptions): IExtrinsicSignature {
|
||||
public sign (method: Call, account: IKeyringPair, { blockHash, era, genesisHash, nonce }: SignatureOptions): IExtrinsicSignature {
|
||||
const signer = createType('Address', account.publicKey);
|
||||
const payload = new ExtrinsicPayload({
|
||||
nonce,
|
||||
method: method.toU8a(),
|
||||
blockHash,
|
||||
era: era || IMMORTAL_ERA,
|
||||
blockHash
|
||||
genesisHash,
|
||||
method: method.toU8a(),
|
||||
nonce,
|
||||
tip: 0
|
||||
});
|
||||
const signature = createType('Signature', payload.sign(account));
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ describe('ExtrinsicV2', (): void => {
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
|
||||
).sign(keyring.alice, {
|
||||
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
nonce: 1,
|
||||
tip: 2
|
||||
}).toHex()
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicOptions } from '../types';
|
||||
|
||||
import { isU8a } from '@polkadot/util';
|
||||
|
||||
@@ -18,17 +19,13 @@ export interface ExtrinsicValueV2 {
|
||||
signature?: ExtrinsicSignature;
|
||||
}
|
||||
|
||||
interface ExtrinsicV2Options {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicV2
|
||||
* @description
|
||||
* The second generation of compact extrinsics
|
||||
*/
|
||||
export default class ExtrinsicV2 extends Struct implements IExtrinsicImpl {
|
||||
public constructor (value?: Uint8Array | ExtrinsicValueV2 | Call, { isSigned }: ExtrinsicV2Options = {}) {
|
||||
public constructor (value?: Uint8Array | ExtrinsicValueV2 | Call, { isSigned }: ExtrinsicOptions = {}) {
|
||||
super({
|
||||
signature: ExtrinsicSignature,
|
||||
method: Call
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Index } from '../../../interfaces/runtime';
|
||||
import { AnyNumber } from '../../../types';
|
||||
import { ExtrinsicExtraValue } from '../types';
|
||||
|
||||
import { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
@@ -11,19 +11,13 @@ import Struct from '../../../codec/Struct';
|
||||
import Address from '../../Generic/Address';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
|
||||
interface ExtrinsicExtraValueV2 {
|
||||
era?: Uint8Array;
|
||||
nonce?: AnyNumber;
|
||||
tip?: AnyNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicExtraV2
|
||||
* @description
|
||||
* A container for the extra information in an extrinsic
|
||||
*/
|
||||
export default class ExtrinsicExtraV2 extends Struct {
|
||||
public constructor (value?: ExtrinsicExtraValueV2 | Uint8Array) {
|
||||
public constructor (value?: ExtrinsicExtraValue | Uint8Array) {
|
||||
super({
|
||||
era: ExtrinsicEra,
|
||||
nonce: ClassOf('Compact<Index>'),
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Hash, Index } from '../../../interfaces/runtime';
|
||||
import { AnyNumber, AnyU8a, ExtrinsicPayloadValue, IExtrinsicEra, IKeyringPair, IMethod } from '../../../types';
|
||||
import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
|
||||
|
||||
import { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
@@ -12,14 +12,6 @@ import U8a from '../../../codec/U8a';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import { sign } from '../util';
|
||||
|
||||
export interface ExtrinsicPayloadValueV2 {
|
||||
blockHash: AnyU8a;
|
||||
era: AnyU8a | IExtrinsicEra;
|
||||
method: AnyU8a | IMethod;
|
||||
nonce: AnyNumber;
|
||||
tip: AnyNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicPayloadV2
|
||||
* @description
|
||||
@@ -27,7 +19,7 @@ export interface ExtrinsicPayloadValueV2 {
|
||||
* on the contents included
|
||||
*/
|
||||
export default class ExtrinsicPayloadV2 extends Struct {
|
||||
public constructor (value?: ExtrinsicPayloadValue | ExtrinsicPayloadValueV2 | Uint8Array | string) {
|
||||
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
|
||||
super({
|
||||
method: U8a,
|
||||
era: ExtrinsicEra,
|
||||
@@ -44,13 +36,6 @@ export default class ExtrinsicPayloadV2 extends Struct {
|
||||
return this.get('blockHash') as Hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[U8a]] contained in the payload
|
||||
*/
|
||||
public get method (): U8a {
|
||||
return this.get('method') as U8a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicEra]]
|
||||
*/
|
||||
@@ -58,6 +43,13 @@ export default class ExtrinsicPayloadV2 extends Struct {
|
||||
return this.get('era') as ExtrinsicEra;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[U8a]] contained in the payload
|
||||
*/
|
||||
public get method (): U8a {
|
||||
return this.get('method') as U8a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Index]]
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import { Address, Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicSignatureOptions } from '../types';
|
||||
|
||||
import createType, { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
@@ -12,11 +13,7 @@ import Call from '../../Generic/Call';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import ExtrinsicPayload from './ExtrinsicPayload';
|
||||
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
|
||||
import ExtrinsicExtra from './ExtrinsicExtra';
|
||||
|
||||
interface ExtrinsicSignatureV2Options {
|
||||
isSigned?: boolean;
|
||||
}
|
||||
import ExtrinsicExtra from '../v2/ExtrinsicExtra';
|
||||
|
||||
/**
|
||||
* @name ExtrinsicSignature
|
||||
@@ -24,7 +21,7 @@ interface ExtrinsicSignatureV2Options {
|
||||
* A container for the [[Signature]] associated with a specific [[Extrinsic]]
|
||||
*/
|
||||
export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSignature {
|
||||
public constructor (value: ExtrinsicSignatureV2 | Uint8Array | undefined, { isSigned }: ExtrinsicSignatureV2Options = {}) {
|
||||
public constructor (value: ExtrinsicSignatureV2 | Uint8Array | undefined, { isSigned }: ExtrinsicSignatureOptions = {}) {
|
||||
super({
|
||||
signer: ClassOf('Address'),
|
||||
signature: ClassOf('Signature'),
|
||||
@@ -127,11 +124,12 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
|
||||
/**
|
||||
* @description Generate a payload and pplies the signature from a keypair
|
||||
*/
|
||||
public sign (method: Call, account: IKeyringPair, { blockHash, era, nonce, tip }: SignatureOptions): IExtrinsicSignature {
|
||||
public sign (method: Call, account: IKeyringPair, { blockHash, era, genesisHash, nonce, tip }: SignatureOptions): IExtrinsicSignature {
|
||||
const signer = createType('Address', account.publicKey);
|
||||
const payload = new ExtrinsicPayload({
|
||||
blockHash,
|
||||
era: era || IMMORTAL_ERA,
|
||||
genesisHash,
|
||||
method: method.toU8a(),
|
||||
nonce,
|
||||
tip: tip || 0
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
|
||||
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||
|
||||
import Call from '../../Generic/Call';
|
||||
import Extrinsic from './Extrinsic';
|
||||
|
||||
const keyring = testingPairs({ type: 'ed25519' }, false);
|
||||
|
||||
describe('ExtrinsicV3', (): void => {
|
||||
beforeEach((): void => {
|
||||
Call.injectMethods(extrinsics);
|
||||
});
|
||||
|
||||
it('constructs a sane Uint8Array (default)', (): void => {
|
||||
expect(
|
||||
new Extrinsic().toU8a()
|
||||
).toEqual(new Uint8Array([0, 0]));
|
||||
});
|
||||
|
||||
it('creates a unsigned extrinsic', (): void => {
|
||||
expect(
|
||||
new Extrinsic(
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
|
||||
).toHex()
|
||||
).toEqual(
|
||||
'0x' +
|
||||
'0500' + // balance.transfer
|
||||
'ff' +
|
||||
'd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9' +
|
||||
'e56c'
|
||||
);
|
||||
});
|
||||
|
||||
it('creates a signed extrinsic', (): void => {
|
||||
expect(
|
||||
new Extrinsic(
|
||||
extrinsics.balances.transfer(keyring.bob.publicKey, 6969)
|
||||
).sign(keyring.alice, {
|
||||
blockHash: '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f',
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
nonce: 1,
|
||||
tip: 2
|
||||
}).toHex()
|
||||
).toEqual(
|
||||
'0x' +
|
||||
'ff' +
|
||||
'd172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f' +
|
||||
'2bb401483999053d2d69a4aebefb9682c8710ddd495d1298d4eedd241c73869f' +
|
||||
'9d32c82dba98b12ef74bea5f987851b592956de3cf3e946533da815e3b6a2904' +
|
||||
'000408' + // era. nonce, tip
|
||||
'0500' +
|
||||
'ff' +
|
||||
'd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9' +
|
||||
'e56c'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicOptions } from '../types';
|
||||
|
||||
import { isU8a } from '@polkadot/util';
|
||||
|
||||
import Struct from '../../../codec/Struct';
|
||||
import Call from '../../Generic/Call';
|
||||
import Address from '../../Generic/Address';
|
||||
import ExtrinsicSignature from './ExtrinsicSignature';
|
||||
|
||||
const TRANSACTION_VERSION = 3;
|
||||
|
||||
export interface ExtrinsicValueV3 {
|
||||
method?: Call;
|
||||
signature?: ExtrinsicSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ExtrinsicV3
|
||||
* @description
|
||||
* The second generation of compact extrinsics
|
||||
*/
|
||||
export default class ExtrinsicV3 extends Struct implements IExtrinsicImpl {
|
||||
public constructor (value?: Uint8Array | ExtrinsicValueV3 | Call, { isSigned }: ExtrinsicOptions = {}) {
|
||||
super({
|
||||
signature: ExtrinsicSignature,
|
||||
method: Call
|
||||
}, ExtrinsicV3.decodeExtrinsic(value, isSigned));
|
||||
}
|
||||
|
||||
public static decodeExtrinsic (value?: Call | Uint8Array | ExtrinsicValueV3, isSigned: boolean = false): ExtrinsicValueV3 {
|
||||
if (!value) {
|
||||
return {};
|
||||
} else if (value instanceof ExtrinsicV3) {
|
||||
return value;
|
||||
} else if (value instanceof Call) {
|
||||
return { method: value };
|
||||
} else if (isU8a(value)) {
|
||||
// here we decode manually since we need to pull through the version information
|
||||
const signature = new ExtrinsicSignature(value, { isSigned });
|
||||
const method = new Call(value.subarray(signature.encodedLength));
|
||||
|
||||
return {
|
||||
method,
|
||||
signature
|
||||
};
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Call]] this extrinsic wraps
|
||||
*/
|
||||
public get method (): Call {
|
||||
return this.get('method') as Call;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicSignature]]
|
||||
*/
|
||||
public get signature (): ExtrinsicSignature {
|
||||
return this.get('signature') as ExtrinsicSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The version for the signature
|
||||
*/
|
||||
public get version (): number {
|
||||
return TRANSACTION_VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Add an [[ExtrinsicSignature]] to the extrinsic (already generated)
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): ExtrinsicV3 {
|
||||
this.signature.addSignature(signer, signature, payload);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Sign the extrinsic with a specific keypair
|
||||
*/
|
||||
public sign (account: IKeyringPair, options: SignatureOptions): ExtrinsicV3 {
|
||||
this.signature.sign(this.method, account, options);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Balance, Hash, Index } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IKeyringPair } from '../../../types';
|
||||
|
||||
import { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
import Struct from '../../../codec/Struct';
|
||||
import U8a from '../../../codec/U8a';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import { sign } from '../util';
|
||||
|
||||
/**
|
||||
* @name ExtrinsicPayloadV3
|
||||
* @description
|
||||
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
|
||||
* on the contents included
|
||||
*/
|
||||
export default class ExtrinsicPayloadV3 extends Struct {
|
||||
public constructor (value?: ExtrinsicPayloadValue | Uint8Array | string) {
|
||||
super({
|
||||
method: U8a,
|
||||
era: ExtrinsicEra,
|
||||
nonce: ClassOf('Compact<Index>'),
|
||||
tip: ClassOf('Compact<Balance>'),
|
||||
genesisHash: ClassOf('Hash'),
|
||||
blockHash: ClassOf('Hash')
|
||||
}, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The block [[Hash]] the signature applies to (mortal/immortal)
|
||||
*/
|
||||
public get blockHash (): Hash {
|
||||
return this.get('blockHash') as Hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicEra]]
|
||||
*/
|
||||
public get era (): ExtrinsicEra {
|
||||
return this.get('era') as ExtrinsicEra;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The genesis [[Hash]] the signature applies to (mortal/immortal)
|
||||
*/
|
||||
public get genesisHash (): Hash {
|
||||
return this.get('genesisHash') as Hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[U8a]] contained in the payload
|
||||
*/
|
||||
public get method (): U8a {
|
||||
return this.get('method') as U8a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Index]]
|
||||
*/
|
||||
public get nonce (): Compact<Index> {
|
||||
return this.get('nonce') as Compact<Index>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The tip [[Balance]]
|
||||
*/
|
||||
public get tip (): Compact<Balance> {
|
||||
return this.get('tip') as Compact<Balance>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Sign the payload with the keypair
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
public sign (signerPair: IKeyringPair): Uint8Array {
|
||||
return sign(signerPair, this.toU8a());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import '../../../injector';
|
||||
|
||||
import ExtrinsicSignature from './ExtrinsicSignature';
|
||||
|
||||
describe('ExtrinsicSignatureV3', (): void => {
|
||||
it('encodes to a sane Uint8Array', (): void => {
|
||||
const u8a = new Uint8Array([
|
||||
// signer as an AccountIndex
|
||||
0x09,
|
||||
// signature
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
// extra stuff
|
||||
0x00, // immortal,
|
||||
0x04, // nonce, compact
|
||||
0x08 // tip, compact
|
||||
]);
|
||||
|
||||
expect(
|
||||
new ExtrinsicSignature(u8a, { isSigned: true }).toU8a()
|
||||
).toEqual(u8a);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,151 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Address, Balance, Index, Signature } from '../../../interfaces/runtime';
|
||||
import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
|
||||
import { ExtrinsicSignatureOptions } from '../types';
|
||||
|
||||
import createType, { ClassOf } from '../../../codec/createType';
|
||||
import Compact from '../../../codec/Compact';
|
||||
import Struct from '../../../codec/Struct';
|
||||
import Call from '../../Generic/Call';
|
||||
import ExtrinsicEra from '../ExtrinsicEra';
|
||||
import ExtrinsicPayload from './ExtrinsicPayload';
|
||||
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
|
||||
import ExtrinsicExtra from '../v2/ExtrinsicExtra';
|
||||
|
||||
/**
|
||||
* @name ExtrinsicSignatureV3
|
||||
* @description
|
||||
* A container for the [[Signature]] associated with a specific [[Extrinsic]]
|
||||
*/
|
||||
export default class ExtrinsicSignatureV3 extends Struct implements IExtrinsicSignature {
|
||||
public constructor (value: ExtrinsicSignatureV3 | Uint8Array | undefined, { isSigned }: ExtrinsicSignatureOptions = {}) {
|
||||
super({
|
||||
signer: ClassOf('Address'),
|
||||
signature: ClassOf('Signature'),
|
||||
extra: ExtrinsicExtra
|
||||
}, ExtrinsicSignatureV3.decodeExtrinsicSignature(value, isSigned));
|
||||
}
|
||||
|
||||
public static decodeExtrinsicSignature (value: ExtrinsicSignatureV3 | Uint8Array | undefined, isSigned: boolean = false): ExtrinsicSignatureV3 | Uint8Array {
|
||||
if (!value) {
|
||||
return EMPTY_U8A;
|
||||
} else if (value instanceof ExtrinsicSignatureV3) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return isSigned
|
||||
? value
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.isSigned
|
||||
? super.encodedLength
|
||||
: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description `true` if the signature is valid
|
||||
*/
|
||||
public get isSigned (): boolean {
|
||||
return !this.signature.isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the extra extrinsic info
|
||||
*/
|
||||
public get extra (): ExtrinsicExtra {
|
||||
return this.get('extra') as ExtrinsicExtra;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
|
||||
*/
|
||||
public get era (): ExtrinsicEra {
|
||||
return this.extra.era;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Index]] for the signature
|
||||
*/
|
||||
public get nonce (): Compact<Index> {
|
||||
return this.extra.nonce;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The actuall [[Signature]] hash
|
||||
*/
|
||||
public get signature (): Signature {
|
||||
return this.get('signature') as Signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Address]] that signed
|
||||
*/
|
||||
public get signer (): Address {
|
||||
return this.get('signer') as Address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Balance]] tip
|
||||
*/
|
||||
public get tip (): Compact<Balance> {
|
||||
return this.extra.tip;
|
||||
}
|
||||
|
||||
private injectSignature (signer: Address, signature: Signature, { era, nonce, tip }: ExtrinsicPayload): IExtrinsicSignature {
|
||||
this.extra.set('era', era);
|
||||
this.extra.set('nonce', nonce);
|
||||
this.extra.set('tip', tip);
|
||||
|
||||
this.set('signer', signer);
|
||||
this.set('signature', signature);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Adds a raw signature
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature {
|
||||
return this.injectSignature(
|
||||
createType('Address', signer),
|
||||
createType('Signature', signature),
|
||||
new ExtrinsicPayload(payload)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Generate a payload and pplies the signature from a keypair
|
||||
*/
|
||||
public sign (method: Call, account: IKeyringPair, { blockHash, era, genesisHash, nonce, tip }: SignatureOptions): IExtrinsicSignature {
|
||||
const signer = createType('Address', account.publicKey);
|
||||
const payload = new ExtrinsicPayload({
|
||||
blockHash,
|
||||
era: era || IMMORTAL_ERA,
|
||||
genesisHash,
|
||||
method: method.toU8a(),
|
||||
nonce,
|
||||
tip: tip || 0
|
||||
});
|
||||
const signature = createType('Signature', payload.sign(account));
|
||||
|
||||
return this.injectSignature(signer, signature, payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
return this.isSigned
|
||||
? super.toU8a(isBare)
|
||||
: EMPTY_U8A;
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@ export interface RuntimeVersionInterface {
|
||||
export interface SignatureOptions {
|
||||
blockHash: AnyU8a;
|
||||
era?: IExtrinsicEra;
|
||||
genesisHash: AnyU8a;
|
||||
nonce: AnyNumber;
|
||||
tip?: AnyNumber;
|
||||
version?: RuntimeVersionInterface;
|
||||
@@ -173,8 +174,10 @@ interface ExtrinsicSignatureBase {
|
||||
}
|
||||
|
||||
export interface ExtrinsicPayloadValue {
|
||||
era: IExtrinsicEra | AnyU8a;
|
||||
method: AnyU8a;
|
||||
blockHash: AnyU8a;
|
||||
era: AnyU8a | IExtrinsicEra;
|
||||
genesisHash: AnyU8a;
|
||||
method: AnyU8a | IMethod;
|
||||
nonce: AnyNumber;
|
||||
tip: AnyNumber;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user