Extrinsic v2 (#1150)

* Extrinsic v2

* Save after last move of files

* Signature V2

* Rework signature decoding

* Simplify V1 and V2 underlying impl.

* Detect chain extrinsic version

* SignaturePayload

* remove Raw payload variant link (Payload is now raw)

* Remove v2 comments referring to v1 data

* Pass isSigned to Extrinsic{V1, V2}

* Cleanup era injection

* It flows :)

* Add SignerPayload and signPayload for Signer (#1152)

* Add SignerPayload and signPayload for Signer

* Submittables use signPayload

* Don't pass version on deprecated interface

* Update after all e2e tx tests passes

* Comment around payload generation

* Adjust blocktime to default (nice round 50 block eras)

* Fix calc

* Re-add test comment

* Update packages/api/src/types.ts

Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>

* Update packages/types/src/primitive/BalanceCompact.ts

Co-Authored-By: Amaury Martiny <amaury.martiny@protonmail.com>

* Backwards compatibility for current in-store signers

* Apply options suggestions

* Add missing Compact params (linting)
This commit is contained in:
Jaco Greeff
2019-07-22 17:08:06 +02:00
committed by GitHub
co-authored by Amaury Martiny
parent ed0979a50c
commit 50a2314544
45 changed files with 1843 additions and 956 deletions
+60 -1
View File
@@ -2,14 +2,73 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyJson, Codec } from '../types';
/**
* @name Base
* @description A type extends the Base class, when it holds a value
*/
export default class Base<T> {
export default abstract class Base<T extends Codec> implements Codec {
protected raw: T;
public constructor (value?: any) {
this.raw = value;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.toU8a().length;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.raw.isEmpty;
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
}
/**
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
*/
public toHex (isLe?: boolean): string {
return this.raw.toHex(isLe);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.raw.toJSON();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
}
/**
* @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.raw.toU8a(isBare);
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return 'Base';
}
}
+2 -37
View File
@@ -7,7 +7,7 @@ import { bnToBn, compactAddLength, compactFromU8a, compactStripLength, compactTo
import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
import Moment from '../primitive/Moment';
import { AnyNumber, Codec, Constructor } from '../types';
import { AnyNumber, Constructor } from '../types';
import { UIntBitLength } from './AbstractInt';
import Base from './Base';
import UInt from './UInt';
@@ -20,7 +20,7 @@ import UInt from './UInt';
* used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
* a number and making the compact representation thereof
*/
export default class Compact<T extends UInt | Moment> extends Base<T> implements Codec {
export default class Compact<T extends UInt | Moment> extends Base<T> {
public constructor (Type: Constructor<T>, value: Compact<T> | AnyNumber = 0) {
super(Compact.decodeCompact<T>(Type, value));
}
@@ -68,20 +68,6 @@ export default class Compact<T extends UInt | Moment> extends Base<T> implements
return new Type(_value);
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.toU8a().length;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.raw.isEmpty;
}
/**
* @description Returns the number of bits in the value
*/
@@ -107,20 +93,6 @@ export default class Compact<T extends UInt | Moment> extends Base<T> implements
return this.raw.toBn();
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (isLe?: boolean): any {
return this.raw.toHex(isLe);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string | number {
return this.raw.toJSON();
}
/**
* @description Returns the number representation for the value
*/
@@ -135,13 +107,6 @@ export default class Compact<T extends UInt | Moment> extends Base<T> implements
return `Compact<${this.raw.toRawType()}>`;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
}
/**
* @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)
+1 -8
View File
@@ -29,7 +29,7 @@ interface Decoded {
// TODO:
// - As per Enum, actually use TS enum
// - It should rather probably extend Enum instead of copying code
export default class Enum extends Base<Codec> implements Codec {
export default class Enum extends Base<Codec> {
private _def: TypesDef;
private _index: number;
@@ -172,13 +172,6 @@ export default class Enum extends Base<Codec> implements Codec {
return this._index;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.isEmpty;
}
/**
* @description Checks if the Enum points to a [[Null]] type
*/
+2 -16
View File
@@ -5,7 +5,7 @@
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import Base from './Base';
import { AnyJson, Codec, Constructor } from '../types';
import { Codec, Constructor } from '../types';
import Null from '../primitive/Null';
/**
@@ -16,7 +16,7 @@ import Null from '../primitive/Null';
* implements that - decodes, checks for optionality and wraps the required structure
* with a value if/as required/found.
*/
export default class Option<T extends Codec> extends Base<T> implements Codec {
export default class Option<T extends Codec> extends Base<T> {
private _Type: Constructor;
public constructor (Type: Constructor, value?: any) {
@@ -112,13 +112,6 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
: u8aToHex(this.toU8a().subarray(1));
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.raw.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -126,13 +119,6 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
return `Option<${new this._Type().toRawType()}>`;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
}
/**
* @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)
+1 -1
View File
@@ -30,7 +30,7 @@ export default class Struct<
protected _Types: S;
public constructor (Types: S, value: V | Map<any, any> | any[] = {} as unknown as V, jsonMap: Map<keyof S, string> = new Map()) {
public constructor (Types: S, value: V | Map<any, any> | any[] | string = {} as unknown as V, jsonMap: Map<keyof S, string> = new Map()) {
const decoded = Struct.decodeStruct<S, V, T>(Types, value, jsonMap);
super(
+1 -1
View File
@@ -7,7 +7,7 @@ import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import { Codec, Constructor } from './types';
import * as Classes from './index.types';
const Types = Classes as { [index: string]: Constructor };
const Types = Classes as Record<string, Constructor>;
const UNCONSTRUCTABLE = ['origin', 'usize', 'vote'];
describe('types', (): void => {
+1 -31
View File
@@ -2,8 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '../types';
import BN from 'bn.js';
import { hexToU8a, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
@@ -24,7 +22,7 @@ export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
* we extend from Base with an AccountId/AccountIndex wrapper. Basically the Address
* is encoded as `[ <prefix-byte>, ...publicKey/...bytes ]` as per spec
*/
export default class Address extends Base<AccountId | AccountIndex> implements Codec {
export default class Address extends Base<AccountId | AccountIndex> {
public constructor (value: AnyAddress = new Uint8Array()) {
super(
Address.decodeAddress(value)
@@ -77,13 +75,6 @@ export default class Address extends Base<AccountId | AccountIndex> implements C
);
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.raw.isEmpty;
}
/**
* @description The length of the raw value, either AccountIndex or AccountId
*/
@@ -93,13 +84,6 @@ export default class Address extends Base<AccountId | AccountIndex> implements C
: this.raw.encodedLength;
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
}
/**
* @description Returns a hex string representation of the value
*/
@@ -107,20 +91,6 @@ export default class Address extends Base<AccountId | AccountIndex> implements C
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.raw.toJSON();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -0,0 +1,15 @@
// 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 Compact from '../codec/Compact';
import Balance from './Balance';
/**
* @name BalanceCompact
* @description
* The Compact<Balance> or number of transactions sent by a specific account. Generally used
* with extrinsics to determine the order of execution.
*/
export default class BalanceCompact extends Compact.with(Balance) {
}
@@ -0,0 +1,123 @@
// 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 { hexToU8a } from '@polkadot/util';
import Method from '../Method';
import Extrinsic from './Extrinsic';
describe('Extrinsic', (): void => {
beforeAll((): void => {
Method.injectMethods(extrinsics);
});
describe('V1', (): void => {
it('decodes an actual transaction (length prefix)', (): void => {
const extrinsic = new Extrinsic(
'0x' +
'2502' +
'81' +
'ff' +
'bfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080' +
'c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6' +
'b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e' +
'0c' + // nonce
'00' + // era
'0500' + // balances.transfer
'ff' +
'4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8' +
'e5c0'
);
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signer.toU8a()).toEqual(new Uint8Array([255, 191, 200, 35, 170, 117, 195, 0, 88, 238, 236, 33, 171, 226, 194, 214, 183, 36, 116, 24, 164, 175, 137, 214, 122, 32, 132, 194, 172, 134, 77, 160, 128]));
expect(extrinsic.signature.toHex()).toEqual('0xc0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e');
expect(extrinsic.nonce.toNumber()).toEqual(3);
expect(extrinsic.era.toU8a()).toEqual(new Uint8Array([0]));
expect(extrinsic.callIndex).toEqual(new Uint8Array([5, 0]));
expect(`${extrinsic.method.sectionName}.${extrinsic.method.methodName}`).toEqual('balances.transfer');
expect(extrinsic.args[0].toString()).toEqual('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo');
});
it('decodes an actual transaction (no length prefix, old version)', (): void => {
const extrinsic = new Extrinsic(
'0x' +
'81' +
'ff' +
'bfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080' +
'c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6' +
'b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e' +
'0c' +
'00' +
'0500' + // balances.transfer
'ff' +
'4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8' +
'e5c0'
);
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signer.toU8a()).toEqual(new Uint8Array([255, 191, 200, 35, 170, 117, 195, 0, 88, 238, 236, 33, 171, 226, 194, 214, 183, 36, 116, 24, 164, 175, 137, 214, 122, 32, 132, 194, 172, 134, 77, 160, 128]));
expect(extrinsic.signature.toHex()).toEqual('0xc0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e');
expect(extrinsic.nonce.toNumber()).toEqual(3);
expect(extrinsic.era.toU8a()).toEqual(new Uint8Array([0]));
expect(extrinsic.callIndex).toEqual(new Uint8Array([5, 0]));
expect(`${extrinsic.method.sectionName}.${extrinsic.method.methodName}`).toEqual('balances.transfer');
expect(extrinsic.args[0].toString()).toEqual('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo');
});
});
describe('V2', (): void => {
it('decodes an actual transaction (length prefix)', (): void => {
const extrinsic = new Extrinsic(
'0x' +
'2902' + // yes, longer than in v1, 1 byte for the tip
'82' +
'ff' +
'bfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080' +
'c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6' +
'b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e' +
'00' + // era
'0c' + // nonce
'08' + // tip
'0500' + // balances.transfer
'ff' +
'4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8' +
'e5c0'
);
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signer.toU8a()).toEqual(new Uint8Array([255, 191, 200, 35, 170, 117, 195, 0, 88, 238, 236, 33, 171, 226, 194, 214, 183, 36, 116, 24, 164, 175, 137, 214, 122, 32, 132, 194, 172, 134, 77, 160, 128]));
expect(extrinsic.signature.toHex()).toEqual('0xc0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e');
expect(extrinsic.era.toU8a()).toEqual(new Uint8Array([0]));
expect(extrinsic.nonce.toNumber()).toEqual(3);
expect(extrinsic.tip.toNumber()).toEqual(2);
expect(extrinsic.callIndex).toEqual(new Uint8Array([5, 0]));
expect(`${extrinsic.method.sectionName}.${extrinsic.method.methodName}`).toEqual('balances.transfer');
expect(extrinsic.args[0].toString()).toEqual('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo');
});
it('constructs from itself', (): void => {
const input = '0x' +
'2902' +
'82' +
'ff' +
'bfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080' +
'c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6' +
'b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e' +
'00' + // era
'00' + // nonce
'00' + // tip
'0500' + // balances.transfer
'ff' +
'4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8' +
'e5c0';
expect(
new Extrinsic(new Extrinsic(input)).toU8a()
).toEqual(hexToU8a(input));
});
});
});
@@ -0,0 +1,295 @@
// 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 { AnyU8a, ArgsDef, Codec, ExtrinsicPayloadValue, IExtrinsic, IHash, IKeyringPair, SignatureOptions } from '../../types';
import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Base from '../../codec/Base';
import Compact from '../../codec/Compact';
import { FunctionMetadata } from '../../Metadata/v6/Calls';
import NonceCompact from '../../type/NonceCompact';
import Address from '../Address';
import BalanceCompact from '../BalanceCompact';
import Method from '../Method';
import Hash from '../Hash';
import ExtrinsicV1, { ExtrinsicValueV1 } from './v1/Extrinsic';
import ExtrinsicV2, { ExtrinsicValueV2 } from './v2/Extrinsic';
import ExtrinsicEra from './ExtrinsicEra';
import { BIT_SIGNED, BIT_UNSIGNED, DEFAULT_VERSION, UNMASK_VERSION } from './constants';
type ExtrinsicValue = ExtrinsicValueV1 | ExtrinsicValueV2;
interface ExtrinsicOptions {
version?: number;
}
/**
* @name Extrinsic
* @description
* Representation of an Extrinsic in the system. It contains the actual call,
* (optional) signature and encodes with an actual length prefix
*
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*
* Can be:
* - signed, to create a transaction
* - left as is, to create an inherent
*/
export default class Extrinsic extends Base<ExtrinsicV1 | ExtrinsicV2> implements IExtrinsic {
public constructor (value: Extrinsic | ExtrinsicValue | AnyU8a | Method | undefined, { version }: ExtrinsicOptions = {}) {
super(Extrinsic.decodeExtrinsic(value, version));
}
private static newFromValue (value: any, version: number): ExtrinsicV1 | ExtrinsicV2 {
if (value instanceof Extrinsic) {
return value.raw;
}
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
const type = version & UNMASK_VERSION;
switch (type) {
case 1: return new ExtrinsicV1(value, { isSigned });
case 2: return new ExtrinsicV2(value, { isSigned });
default: throw new Error(`Unsupported extrinsic version ${type}`);
}
}
public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Method | undefined, version: number = DEFAULT_VERSION): ExtrinsicV1 | ExtrinsicV2 {
if (Array.isArray(value) || isHex(value)) {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = u8aToU8a(value);
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches
const [offset, length] = Compact.decodeU8a(u8a);
const withPrefix = u8a.length === (offset + length.toNumber());
return Extrinsic.decodeExtrinsic(
withPrefix
? u8a
: Compact.addLengthPrefix(u8a),
version
);
} else if (isU8a(value)) {
if (!value.length) {
return Extrinsic.newFromValue(new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`);
return Extrinsic.decodeU8a(value.subarray(offset, total));
} else if (value instanceof Method) {
return Extrinsic.newFromValue({ method: value }, version);
}
return Extrinsic.newFromValue(value, version);
}
private static decodeU8a (value: Uint8Array): ExtrinsicV1 | ExtrinsicV2 {
return Extrinsic.newFromValue(value.subarray(1), value[0]);
}
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Method]]
*/
public get args (): Codec[] {
return this.method.args;
}
/**
* @description Thge argument defintions, compatible with [[Method]]
*/
public get argsDef (): ArgsDef {
return this.method.argsDef;
}
/**
* @description The actual `[sectionIndex, methodIndex]` as used in the Method
*/
public get callIndex (): Uint8Array {
return this.method.callIndex;
}
/**
* @description The actual data for the Method
*/
public get data (): Uint8Array {
return this.method.data;
}
/**
* @description The era for thios extrinsic
*/
public get era (): ExtrinsicEra {
return this.raw.signature.era;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.toU8a().length;
}
/**
* @description Convernience function, encodes the extrinsic and returns the actual hash
*/
public get hash (): Hash {
return new Hash(
blake2AsU8a(this.toU8a(), 256)
);
}
/**
* @description `true` is method has `Origin` argument (compatibility with [[Method]])
*/
public get hasOrigin (): boolean {
return this.method.hasOrigin;
}
/**
* @description `true` id the extrinsic is signed
*/
public get isSigned (): boolean {
return this.raw.signature.isSigned;
}
/**
* @description The length of the actual data, excluding prefix
*/
public get length (): number {
return this.toU8a(true).length;
}
/**
* @description The [[FunctionMetadata]] that describes the extrinsic
*/
public get meta (): FunctionMetadata {
return this.method.meta;
}
/**
* @description The [[Method]] this extrinsic wraps
*/
public get method (): Method {
return this.raw.method;
}
/**
* @description The nonce for this extrinsic
*/
public get nonce (): NonceCompact {
return this.raw.signature.nonce;
}
/**
* @description The [[ExtrinsicSignature]]
*/
public get signature (): IHash {
return this.raw.signature.signature;
}
/**
* @description The [[Address]] that signed
*/
public get signer (): Address {
return this.raw.signature.signer;
}
/**
* @description Forwards compat
*/
public get tip (): BalanceCompact {
return this.raw.signature.tip;
}
/**
* @description Returns the raw transaction version (not flagged with signing information)
*/
public get type (): number {
return this.raw.version;
}
/**
* @description Returns the encoded version flag
*/
public get version (): number {
return this.type | (this.isSigned ? BIT_SIGNED : BIT_UNSIGNED);
}
/**
* @description Add an [[ExtrinsicSignature]] to the extrinsic (already generated)
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, ...args: [ExtrinsicPayloadValue | Uint8Array | string]): Extrinsic {
// FIXME Support for current extensions where 2 values are being passed in here, i.e.
// addSignature(signer, signature, nonce, era);
// The above signature should be changed to the correct format in the next cycle, i.e.
// payload: ExtrinsicPayloadValue | Uint8Array | string
let payload = args[0];
// @ts-ignore
if (args.length === 2) {
payload = {
// @ts-ignore
era: args[1] as string,
method: this.method.toHex(),
nonce: args[0] as string,
tip: 0
};
}
this.raw.addSignature(signer, signature, payload);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): Extrinsic {
this.raw.sign(account, options);
return this;
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.toHex();
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return 'Extrinsic';
}
/**
* @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 {
const encoded = u8aConcat(new Uint8Array([this.version]), this.raw.toU8a(isBare));
return isBare
? encoded
: Compact.addLengthPrefix(encoded);
}
}
@@ -2,15 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyU8a, IExtrinsicEra } from '../types';
import { AnyU8a, IExtrinsicEra } from '../../types';
import BN from 'bn.js';
import { assert, bnToBn, hexToU8a, isHex, isU8a, isObject, u8aToBn } from '@polkadot/util';
import Enum from '../codec/Enum';
import Tuple from '../codec/Tuple';
import U8a from '../codec/U8a';
import U64 from '../primitive/U64';
import Enum from '../../codec/Enum';
import Tuple from '../../codec/Tuple';
import U8a from '../../codec/U8a';
import U64 from '../U64';
import { IMMORTAL_ERA } from './constants';
type MortalEraValue = [U64, U64];
@@ -27,8 +28,6 @@ interface ImmortalEnumDef {
ImmortalEra: string;
}
const VALID_IMMORTAL = new U8a([0]);
/**
* @name ImmortalEra
* @description
@@ -39,7 +38,7 @@ export class ImmortalEra extends U8a {
public constructor (value?: AnyU8a) {
// For immortals, we always provide the known value (i.e. treated as a
// constant no matter how it is constructed - it is a fixed structure)
super(VALID_IMMORTAL);
super(IMMORTAL_ERA);
}
}
@@ -193,13 +192,15 @@ export default class ExtrinsicEra extends Enum implements IExtrinsicEra {
// eslint-disable-next-line @typescript-eslint/ban-types
private static decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
if (value instanceof ExtrinsicEra) {
if (!value) {
return new Uint8Array([0]);
} else if (value instanceof ExtrinsicEra) {
return ExtrinsicEra.decodeExtrinsicEra(value.toU8a());
} else if (isHex(value)) {
return ExtrinsicEra.decodeExtrinsicEra(hexToU8a(value));
} else if (isU8a(value)) {
if (!value.length || value[0] === 0) {
return new Uint8Array([0, 0]);
return new Uint8Array([0]);
} else {
return new Uint8Array([1, value[0], value[1]]);
}
@@ -0,0 +1,118 @@
// 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 { IKeyringPair } from '../../types';
import { u8aToHex } from '@polkadot/util';
import Base from '../../codec/Base';
import U8a from '../../codec/U8a';
import NonceCompact from '../../type/NonceCompact';
import BalanceCompact from '../BalanceCompact';
import Hash from '../Hash';
import SignaturePayloadV1, { SignaturePayloadValueV1 } from './v1/SignaturePayload';
import SignaturePayloadV2, { SignaturePayloadValueV2 } from './v2/SignaturePayload';
import ExtrinsicEra from './ExtrinsicEra';
import { DEFAULT_VERSION } from './constants';
interface SignaturePayloadOptions {
version?: number;
}
/**
* @name SignaturePayload
* @description
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
* on the contents included
*/
export default class SignaturePayload extends Base<SignaturePayloadV1 | SignaturePayloadV2> {
public constructor (value: SignaturePayloadValueV1 | SignaturePayloadValueV2 | Uint8Array | string | undefined, { version }: SignaturePayloadOptions = {}) {
super(
SignaturePayload.decodeSignaturePayload(value, version)
);
}
public static decodeSignaturePayload (value: SignaturePayload | SignaturePayloadValueV1 | SignaturePayloadValueV2 | Uint8Array | string | undefined, version: number = DEFAULT_VERSION): SignaturePayloadV1 | SignaturePayloadV2 {
if (value instanceof SignaturePayload) {
return value.raw;
}
switch (version) {
case 1: return new SignaturePayloadV1(value as Uint8Array);
case 2: return new SignaturePayloadV2(value as Uint8Array);
default: throw new Error(`Unsupported extrinsic version ${version}`);
}
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
return this.raw.blockHash;
}
/**
* @description The [[U8a]] contained in the payload
*/
public get method (): U8a {
return this.raw.method;
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.raw.era;
}
/**
* @description The [[NonceCompact]]
*/
public get nonce (): NonceCompact {
return this.raw.nonce;
}
/**
* @description The [[BalanceCompact]]
*/
public get tip (): BalanceCompact {
return (this.raw as SignaturePayloadV2).tip || new BalanceCompact(0);
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
}
/**
* @description Sign the payload with the keypair
*/
public sign (signerPair: IKeyringPair): { signature: string } {
const signature = this.raw.sign(signerPair);
// This is extensible, so we could quite readily extend to send back extra
// information, such as for instance the payload, i.e. `payload: this.toHex()`
// For the case here we sign via the extrinsic, we ignore the return, so generally
// thisis applicable for external signing
return {
signature: u8aToHex(signature)
};
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): any {
return this.toHex();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.toHex();
}
}
@@ -0,0 +1,17 @@
// 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.
export const BIT_SIGNED = 0b10000000;
export const BIT_UNSIGNED = 0;
export const EMPTY_U8A = new Uint8Array();
// TODO We really want to swap this to V2, however all the test data is setup
// for V1, so this will take some time to convert... "some" time :)
export const DEFAULT_VERSION = 1;
export const IMMORTAL_ERA = new Uint8Array([0]);
export const UNMASK_VERSION = 0b01111111;
@@ -0,0 +1,16 @@
// 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 { IKeyringPair } from '../../types';
import { blake2AsU8a } from '@polkadot/util-crypto';
// a helper function for both types of payloads, Raw and metadata-known
export function sign (signerPair: IKeyringPair, u8a: Uint8Array): Uint8Array {
const encoded = u8a.length > 256
? blake2AsU8a(u8a)
: u8a;
return signerPair.sign(encoded);
}
@@ -0,0 +1,102 @@
// 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 { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Method';
import Address from '../../Address';
import ExtrinsicSignature from './ExtrinsicSignature';
export interface ExtrinsicValueV1 {
method?: Method;
signature?: ExtrinsicSignature;
}
interface ExtrinsicV1Options {
isSigned?: boolean;
}
const TRANSACTION_VERSION = 1;
/**
* @name ExtrinsicV1
* @description
* The first generation of compact extrinsics
*/
export default class ExtrinsicV1 extends Struct implements IExtrinsicImpl {
public constructor (value?: Uint8Array | ExtrinsicValueV1, { isSigned }: ExtrinsicV1Options = {}) {
super({
signature: ExtrinsicSignature,
method: Method
}, ExtrinsicV1.decodeExtrinsic(value, isSigned));
}
public static decodeExtrinsic (value?: Uint8Array | ExtrinsicValueV1, isSigned: boolean = false): ExtrinsicValueV1 {
if (!value) {
return {};
} else if (value instanceof ExtrinsicV1) {
return 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 Method(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 [[Method]] this extrinsic wraps
*/
public get method (): Method {
return this.get('method') as Method;
}
/**
* @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): ExtrinsicV1 {
this.signature.addSignature(signer, signature, payload);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): ExtrinsicV1 {
this.signature.sign(this.method, account, options);
return this;
}
}
@@ -0,0 +1,140 @@
// 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, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import BalanceCompact from '../../BalanceCompact';
import Method from '../../Method';
import Signature from '../../Signature';
import NonceCompact from '../../../type/NonceCompact';
import ExtrinsicEra from '../ExtrinsicEra';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
import SignaturePayload from './SignaturePayload';
interface ExtrinsicSignatureV1Options {
isSigned?: boolean;
}
/**
* @name ExtrinsicSignature
* @description
* A container for the [[Signature]] associated with a specific [[Extrinsic]]
*/
export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSignature {
// Signature Information.
// 1/3/5/9/33 bytes: The signing account identity, in Address format
// 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 = {}) {
super({
signer: Address,
signature: Signature,
nonce: NonceCompact,
era: ExtrinsicEra
}, ExtrinsicSignatureV1.decodeExtrinsicSignature(value, isSigned));
}
public static decodeExtrinsicSignature (value: ExtrinsicSignatureV1 | Uint8Array | undefined, isSigned: boolean = false): ExtrinsicSignatureV1 | Uint8Array {
if (!value) {
return EMPTY_U8A;
} else if (value instanceof ExtrinsicSignatureV1) {
return value;
}
return isSigned
? value
: EMPTY_U8A;
}
/**
* @description `true` if the signature is valid
*/
public get isSigned (): boolean {
return !this.signature.isEmpty;
}
/**
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[Nonce]] for the signature
*/
public get nonce (): NonceCompact {
return this.get('nonce') as NonceCompact;
}
/**
* @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 Forwards compat
*/
public get tip (): BalanceCompact {
return new BalanceCompact(0);
}
private injectSignature (signer: Address, signature: Signature, { era, nonce }: SignaturePayload): IExtrinsicSignature {
this.set('era', era);
this.set('nonce', nonce);
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(
new Address(signer),
new Signature(signature),
new SignaturePayload(payload)
);
}
/**
* @description Generate a payload and pplies the signature from a keypair
*/
public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce }: SignatureOptions): IExtrinsicSignature {
const signer = new Address(account.publicKey);
const payload = new SignaturePayload({
nonce,
method: method.toU8a(),
era: era || IMMORTAL_ERA,
blockHash
});
const signature = new 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)
: new Uint8Array();
}
}
@@ -0,0 +1,77 @@
// 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, AnyU8a, ExtrinsicPayloadValue, IExtrinsicEra, IKeyringPair, IMethod } from '../../../types';
import Struct from '../../../codec/Struct';
import U8a from '../../../codec/U8a';
import Hash from '../../Hash';
import NonceCompact from '../../../type/NonceCompact';
import ExtrinsicEra from '../ExtrinsicEra';
import { sign } from '../util';
export interface SignaturePayloadValueV1 {
blockHash: AnyU8a;
era: AnyU8a | IExtrinsicEra;
method: AnyU8a | IMethod;
nonce: AnyNumber;
}
/**
* @name SignaturePayloadV1
* @description
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
* on the contents included
*
* 1-8 bytes: The Transaction Compact<Index/Nonce> as provided in the transaction itself.
* 2+ bytes: The Function Descriptor as provided in the transaction itself.
* 1/2 bytes: The Transaction Era as provided in the transaction itself.
* 32 bytes: The hash of the authoring block implied by the Transaction Era and the current block.
*/
export default class SignaturePayloadV1 extends Struct {
public constructor (value?: ExtrinsicPayloadValue | SignaturePayloadValueV1 | Uint8Array | string) {
super({
nonce: NonceCompact,
method: U8a,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
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]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[NonceCompact]]
*/
public get nonce (): NonceCompact {
return this.get('nonce') as NonceCompact;
}
/**
* @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,60 @@
// 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 Method from '../../Method';
import Extrinsic from './Extrinsic';
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('ExtrinsicV2', (): void => {
beforeEach((): void => {
Method.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',
nonce: 1,
tip: 2
}).toHex()
).toEqual(
'0x' +
'ff' +
'd172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f' +
'975c79b3c7c57898153faa55a97744f71aa8b4c44ec319978c1206f7941db65e' +
'6cea25fe0bc700f8cb3c5eeef3894b21ed88398e8d8ab93b6af2e9d2fd9f5404' +
'000408' + // era. nonce, tip
'0500' +
'ff' +
'd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9' +
'e56c'
);
});
});
@@ -0,0 +1,104 @@
// 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 { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Method';
import Address from '../../Address';
import ExtrinsicSignature from './ExtrinsicSignature';
const TRANSACTION_VERSION = 2;
export interface ExtrinsicValueV2 {
method?: Method;
signature?: ExtrinsicSignature;
}
interface ExtrinsicV2Options {
isSigned?: boolean;
}
/**
* @name ExtrinsicV1
* @description
* The first generation of compact extrinsics
*/
export default class ExtrinsicV2 extends Struct implements IExtrinsicImpl {
public constructor (value?: Uint8Array | ExtrinsicValueV2 | Method, { isSigned }: ExtrinsicV2Options = {}) {
super({
signature: ExtrinsicSignature,
method: Method
}, ExtrinsicV2.decodeExtrinsic(value, isSigned));
}
public static decodeExtrinsic (value?: Method | Uint8Array | ExtrinsicValueV2, isSigned: boolean = false): ExtrinsicValueV2 {
if (!value) {
return {};
} else if (value instanceof ExtrinsicV2) {
return value;
} else if (value instanceof Method) {
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 Method(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 [[Method]] this extrinsic wraps
*/
public get method (): Method {
return this.get('method') as Method;
}
/**
* @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): ExtrinsicV2 {
this.signature.addSignature(signer, signature, payload);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): ExtrinsicV2 {
this.signature.sign(this.method, account, options);
return this;
}
}
@@ -0,0 +1,25 @@
// 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 ExtrinsicExtra from './ExtrinsicExtra';
describe('ExtrinsicExtraV2', (): void => {
it('encodes to a sane Uint8Array (default construction)', (): void => {
expect(
new ExtrinsicExtra().toU8a()
).toEqual(new Uint8Array([0, 0, 0]));
});
it('encodes to a sane Uint8Array (struct in)', (): void => {
expect(
new ExtrinsicExtra({ nonce: 1, tip: 2 }).toU8a()
).toEqual(new Uint8Array([0, 4, 8])); // numbers are Compact
});
it('encodes to a sane Uint8Array (Uint8Array in)', (): void => {
expect(
new ExtrinsicExtra(new Uint8Array([0, 8, 4])).toU8a()
).toEqual(new Uint8Array([0, 8, 4]));
});
});
@@ -0,0 +1,62 @@
// 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';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import BalanceCompact from '../../BalanceCompact';
import ExtrinsicEra from '../ExtrinsicEra';
import NonceCompact from '../../../type/NonceCompact';
interface ExtrinsicExtraValueV2 {
era?: Uint8Array;
nonce?: AnyNumber;
tip?: AnyNumber;
}
export const extraDefinition = {
era: ExtrinsicEra,
nonce: NonceCompact,
tip: BalanceCompact
};
/**
* @name ExtrinsicExtraV2
* @description
* A container for the extra information in an extrinsic
*/
export default class ExtrinsicExtraV2 extends Struct {
public constructor (value?: ExtrinsicExtraValueV2 | Uint8Array) {
super(extraDefinition, value);
}
/**
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[NonceCompact]] for the signature
*/
public get nonce (): NonceCompact {
return this.get('nonce') as NonceCompact;
}
/**
* @description The [[Address]] that signed
*/
public get signer (): Address {
return this.get('signer') as Address;
}
/**
* @description The [[BalanceCompact]] tip
*/
public get tip (): BalanceCompact {
return this.get('tip') as BalanceCompact;
}
}
@@ -0,0 +1,27 @@
// 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 ExtrinsicSignature from './ExtrinsicSignature';
describe('ExtrinsicSignatureV2', (): 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,145 @@
// 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, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../../../types';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import BalanceCompact from '../../BalanceCompact';
import Method from '../../Method';
import Signature from '../../Signature';
import ExtrinsicEra from '../ExtrinsicEra';
import NonceCompact from '../../../type/NonceCompact';
import SignaturePayload from './SignaturePayload';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
import ExtrinsicExtra from './ExtrinsicExtra';
interface ExtrinsicSignatureV2Options {
isSigned?: boolean;
}
/**
* @name ExtrinsicSignature
* @description
* 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 = {}) {
super({
signer: Address,
signature: Signature,
extra: ExtrinsicExtra
}, ExtrinsicSignatureV2.decodeExtrinsicSignature(value, isSigned));
}
public static decodeExtrinsicSignature (value: ExtrinsicSignatureV2 | Uint8Array | undefined, isSigned: boolean = false): ExtrinsicSignatureV2 | Uint8Array {
if (!value) {
return EMPTY_U8A;
} else if (value instanceof ExtrinsicSignatureV2) {
return value;
}
return isSigned
? value
: EMPTY_U8A;
}
/**
* @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 [[NonceCompact]] for the signature
*/
public get nonce (): NonceCompact {
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 (): BalanceCompact {
return this.extra.tip;
}
private injectSignature (signer: Address, signature: Signature, { era, nonce, tip }: SignaturePayload): 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(
new Address(signer),
new Signature(signature),
new SignaturePayload(payload)
);
}
/**
* @description Generate a payload and pplies the signature from a keypair
*/
public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce, tip }: SignatureOptions): IExtrinsicSignature {
const signer = new Address(account.publicKey);
const payload = new SignaturePayload({
blockHash,
era: era || IMMORTAL_ERA,
method: method.toU8a(),
nonce,
tip: tip || 0
});
const signature = new 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)
: new Uint8Array();
}
}
@@ -0,0 +1,85 @@
// 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, AnyU8a, ExtrinsicPayloadValue, IExtrinsicEra, IKeyringPair, IMethod } from '../../../types';
import Struct from '../../../codec/Struct';
import U8a from '../../../codec/U8a';
import BalanceCompact from '../../BalanceCompact';
import Hash from '../../Hash';
import NonceCompact from '../../../type/NonceCompact';
import ExtrinsicEra from '../ExtrinsicEra';
import { extraDefinition } from './ExtrinsicExtra';
import { sign } from '../util';
export interface SignaturePayloadValueV2 {
blockHash: AnyU8a;
era: AnyU8a | IExtrinsicEra;
method: AnyU8a | IMethod;
nonce: AnyNumber;
tip: AnyNumber;
}
const basePayload = {
...extraDefinition,
blockHash: Hash
};
/**
* @name SignaturePayloadV2
* @description
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
* on the contents included
*/
export default class SignaturePayloadV2 extends Struct {
public constructor (value?: ExtrinsicPayloadValue | SignaturePayloadValueV2 | Uint8Array | string) {
super({
method: U8a,
...basePayload
}, value);
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
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]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[NonceCompact]]
*/
public get nonce (): NonceCompact {
return this.get('nonce') as NonceCompact;
}
/**
* @description The tip [[BalanceCompact]]
*/
public get tip (): BalanceCompact {
return this.get('tip') as BalanceCompact;
}
/**
* @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());
}
}
+3
View File
@@ -22,6 +22,9 @@ export { default as EventRecord, EventRecord0to76 } from './EventRecord';
export { default as Bool, default as bool } from './Bool';
export { default as Bytes } from './Bytes';
export { default as Data } from './Data';
export { default as Extrinsic } from './Extrinsic/Extrinsic';
export { default as ExtrinsicEra, MortalEra, ImmortalEra } from './Extrinsic/ExtrinsicEra';
export { default as SignaturePayload } from './Extrinsic/SignaturePayload';
export { default as H160 } from './H160';
export { default as H256 } from './H256';
export { default as H512 } from './H512';
-54
View File
@@ -1,54 +0,0 @@
// 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 Method from '../primitive/Method';
import Extrinsic from './Extrinsic';
describe('Extrinsic', (): void => {
beforeAll((): void => {
Method.injectMethods(extrinsics);
});
it.skip('decodes a non-signed properly via JSON', (): void => {
const extrinsic = new Extrinsic('0x010300ea51b75b00000000');
expect(extrinsic.isSigned).toEqual(false);
expect(extrinsic.callIndex).toEqual(new Uint8Array([3, 0]));
expect(extrinsic.data).toEqual(new Uint8Array([234, 81, 183, 91, 0, 0, 0, 0]));
});
it('decodes an actual transaction (new format)', (): void => {
const extrinsic = new Extrinsic('0x250281ffbfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e0c00' +
'0500' + // balances.transfer
'ff4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8e5c0');
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signature.signer.toU8a()).toEqual(new Uint8Array([255, 191, 200, 35, 170, 117, 195, 0, 88, 238, 236, 33, 171, 226, 194, 214, 183, 36, 116, 24, 164, 175, 137, 214, 122, 32, 132, 194, 172, 134, 77, 160, 128]));
expect(extrinsic.signature.signature.toHex()).toEqual('0xc0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e');
expect(extrinsic.signature.nonce.toNumber()).toEqual(3);
expect(extrinsic.signature.era.toU8a()).toEqual(new Uint8Array([0]));
expect(extrinsic.callIndex).toEqual(new Uint8Array([5, 0]));
expect(`${extrinsic.method.sectionName}.${extrinsic.method.methodName}`).toEqual('balances.transfer');
expect(extrinsic.args[0].toString()).toEqual('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo');
});
it('decodes an actual transaction (old format)', (): void => {
const extrinsic = new Extrinsic(
'0x81ffbfc823aa75c30058eeec21abe2c2d6b7247418a4af89d67a2084c2ac864da080c0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e0c00' +
'0500' + // balances.transfer
'ff4a83f1c09be797bc3d9adce29818368b276a84e6b545ced492c25c948978d7f8e5c0'
);
expect(extrinsic.isSigned).toEqual(true);
expect(extrinsic.signature.signer.toU8a()).toEqual(new Uint8Array([255, 191, 200, 35, 170, 117, 195, 0, 88, 238, 236, 33, 171, 226, 194, 214, 183, 36, 116, 24, 164, 175, 137, 214, 122, 32, 132, 194, 172, 134, 77, 160, 128]));
expect(extrinsic.signature.signature.toHex()).toEqual('0xc0aa4df3b4926c3cd78bbdced31d8bdccb8604b779b71b90e58b2848df4a9ad6b0aa1aae6be7a05c9413a172b0325e4d214e5ff2b25098028b30f1a50be9c90e');
expect(extrinsic.signature.nonce.toNumber()).toEqual(3);
expect(extrinsic.signature.era.toU8a()).toEqual(new Uint8Array([0]));
expect(extrinsic.callIndex).toEqual(new Uint8Array([5, 0]));
expect(`${extrinsic.method.sectionName}.${extrinsic.method.methodName}`).toEqual('balances.transfer');
expect(extrinsic.args[0].toString()).toEqual('5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo');
});
});
-239
View File
@@ -1,239 +0,0 @@
// 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, AnyU8a, ArgsDef, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '../types';
import { assert, isHex, isU8a, u8aToU8a } from '@polkadot/util';
import Base from '../codec/Base';
import Compact from '../codec/Compact';
import { FunctionMetadata } from '../Metadata/v6/Calls';
import Method from '../primitive/Method';
import Address from '../primitive/Address';
import Hash from '../primitive/Hash';
import ExtrinsicSignature from './ExtrinsicSignature';
import ExtrinsicV1, { ExtrinsicValueV1 } from './ExtrinsicV1';
type ExtrinsicValue = ExtrinsicValueV1;
const UNMASK_VERSION = 0b01111111;
/**
* @name Extrinsic
* @description
* Representation of an Extrinsic in the system. It contains the actual call,
* (optional) signature and encodes with an actual length prefix
*
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*
* Can be:
* - signed, to create a transaction
* - left as is, to create an inherent
*/
export default class Extrinsic extends Base<ExtrinsicV1> implements IExtrinsic, Codec {
public constructor (value?: ExtrinsicValue | AnyU8a | Method) {
super(Extrinsic.decodeExtrinsic(value));
}
public static decodeExtrinsic (value?: ExtrinsicValue | AnyU8a | Method): ExtrinsicV1 {
if (Array.isArray(value) || isHex(value)) {
// Instead of the block below, it should simply be:
// return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
const u8a = u8aToU8a(value);
// HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
// extrinsics didn't have the length, cater for both approaches
const [offset, length] = Compact.decodeU8a(u8a);
const withPrefix = u8a.length === (offset + length.toNumber());
return Extrinsic.decodeExtrinsic(
withPrefix
? u8a
: Compact.addLengthPrefix(u8a)
);
} else if (isU8a(value)) {
if (!value.length) {
return new ExtrinsicV1(new Uint8Array());
}
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`);
return Extrinsic.decodeU8a(value.subarray(offset, total));
} else if (value instanceof Method) {
return new ExtrinsicV1({
method: value
});
}
return new ExtrinsicV1(value);
}
public static decodeU8a (value: Uint8Array): ExtrinsicV1 {
// decode the actual version string
const version = value[0] & UNMASK_VERSION;
switch (version) {
case 1:
return new ExtrinsicV1(value);
default:
throw new Error(`Unsupported extrinsic version ${version}`);
}
}
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Method]]
*/
public get args (): Codec[] {
return this.method.args;
}
/**
* @description Thge argument defintions, compatible with [[Method]]
*/
public get argsDef (): ArgsDef {
return this.method.argsDef;
}
/**
* @description The actual `[sectionIndex, methodIndex]` as used in the Method
*/
public get callIndex (): Uint8Array {
return this.method.callIndex;
}
/**
* @description The actual data for the Method
*/
public get data (): Uint8Array {
return this.method.data;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.raw.encodedLength;
}
/**
* @description Convernience function, encodes the extrinsic and returns the actual hash
*/
public get hash (): Hash {
return this.raw.hash;
}
/**
* @description `true` is method has `Origin` argument (compatibility with [[Method]])
*/
public get hasOrigin (): boolean {
return this.raw.hasOrigin;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.raw.isEmpty;
}
/**
* @description `true` id the extrinsic is signed
*/
public get isSigned (): boolean {
return this.raw.isSigned;
}
/**
* @description The length of the encoded value
*/
public get length (): number {
return this.raw.length;
}
/**
* @description The [[FunctionMetadata]] that describes the extrinsic
*/
public get meta (): FunctionMetadata {
return this.raw.meta;
}
/**
* @description The [[Method]] this extrinsic wraps
*/
public get method (): Method {
return this.raw.method;
}
/**
* @description The [[ExtrinsicSignature]]
*/
public get signature (): ExtrinsicSignature {
return this.raw.signature;
}
/**
* @description Add an [[ExtrinsicSignature]] to the extrinsic (already generated)
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, nonce: AnyNumber, era: Uint8Array | IExtrinsicEra): Extrinsic {
this.raw.addSignature(signer, signature, nonce, era);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): Extrinsic {
this.raw.sign(account, options);
return this;
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (): string {
return this.raw.toHex();
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.raw.toJSON();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return 'Extrinsic';
}
/**
* @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.raw.toU8a(isBare);
}
}
@@ -1,171 +0,0 @@
// 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, IExtrinsicEra, IExtrinsicSignature, IKeyringPair, SignatureOptions } from '../types';
import Struct from '../codec/Struct';
import Address from '../primitive/Address';
import Method from '../primitive/Method';
import Signature from '../primitive/Signature';
import U8 from '../primitive/U8';
import RuntimeVersion from '../rpc/RuntimeVersion';
import ExtrinsicEra from './ExtrinsicEra';
import Nonce from './NonceCompact';
import SignaturePayload from './SignaturePayload';
export const IMMORTAL_ERA = new Uint8Array([0]);
const BIT_SIGNED = 0b10000000;
const BIT_UNSIGNED = 0;
const BIT_VERSION = 0b0000001;
/**
* @name ExtrinsicSignature
* @description
* A container for the [[Signature]] associated with a specific [[Extrinsic]]
*/
export default class ExtrinsicSignature extends Struct implements IExtrinsicSignature {
// Signature Information.
// 1 byte version: BIT_VERSION | (isSigned ? BIT_SIGNED : BIT_UNSIGNED)
// 1/3/5/9/33 bytes: The signing account identity, in Address format
// 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?: Uint8Array) {
super({
version: U8,
signer: Address,
signature: Signature,
nonce: Nonce,
era: ExtrinsicEra
}, ExtrinsicSignature.decodeExtrinsicSignature(value));
}
public static decodeExtrinsicSignature (value?: Uint8Array): object | Uint8Array {
if (!value) {
return {
// we always explicitly set the unsigned version
version: BIT_VERSION | BIT_UNSIGNED
};
}
const version = value[0];
// only decode the full Uint8Array if we have the signed indicator,
// alternatively only return the version (default for others)
return (version & BIT_SIGNED) === BIT_SIGNED
? value
: { version };
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.isSigned
? super.encodedLength
: 1;
}
/**
* @description `true` if the signature is valid
*/
public get isSigned (): boolean {
return (this.version & BIT_SIGNED) === BIT_SIGNED;
}
/**
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[ExtrinsicEra]] (mortal or immortal) this signature applies to
*/
public set era (era: ExtrinsicEra) {
this.set('era', era);
}
/**
* @description The [[Nonce]] for the signature
*/
public get nonce (): Nonce {
return this.get('nonce') as 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 encoded version for the signature
*/
public get version (): number {
// Version Information.
// 1 byte: version information:
// - 7 low bits: version identifier (should be 0b0000001).
// - 1 high bit: signed flag: 1 if this is a transaction (e.g. contains a signature).
return (this.get('version') as U8).toNumber();
}
private injectSignature (signature: Signature, signer: Address, nonce: Nonce, era: ExtrinsicEra): ExtrinsicSignature {
this.set('era', era);
this.set('nonce', nonce);
this.set('signer', signer);
this.set('signature', signature);
this.set('version', new U8(BIT_VERSION | BIT_SIGNED));
return this;
}
/**
* @description Adds a raw signature
*/
public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | string, _nonce: AnyNumber, _era: Uint8Array | IExtrinsicEra): ExtrinsicSignature {
const signer = new Address(_signer);
const nonce = new Nonce(_nonce);
const era = new ExtrinsicEra(_era);
const signature = new Signature(_signature);
return this.injectSignature(signature, signer, nonce, era);
}
/**
* @description Generate a payload and pplies the signature from a keypair
*/
public sign (method: Method, account: IKeyringPair, { blockHash, era, nonce, version }: SignatureOptions): ExtrinsicSignature {
const signer = new Address(account.publicKey);
const signingPayload = new SignaturePayload({
nonce,
method,
era: era || this.era || IMMORTAL_ERA,
blockHash
});
const signature = new Signature(signingPayload.sign(account, version as RuntimeVersion));
return this.injectSignature(signature, signer, signingPayload.nonce, signingPayload.era);
}
/**
* @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)
: new Uint8Array([this.version]);
}
}
-175
View File
@@ -1,175 +0,0 @@
// 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, ArgsDef, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '../types';
import { u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from '../codec/Compact';
import Struct from '../codec/Struct';
import { FunctionMetadata } from '../Metadata/v6/Calls';
import Method from '../primitive/Method';
import Address from '../primitive/Address';
import Hash from '../primitive/Hash';
import ExtrinsicSignature from './ExtrinsicSignature';
export interface ExtrinsicValueV1 {
method?: Method;
signature?: ExtrinsicSignature;
}
/**
* @name ExtrinsicV1
* @description
* The first generation of compact extrinsics
*/
export default class ExtrinsicV1 extends Struct implements IExtrinsic {
public constructor (value?: Uint8Array | ExtrinsicValueV1) {
super({
signature: ExtrinsicSignature,
method: Method
}, value);
}
/**
* @description The arguments passed to for the call, exposes args so it is compatible with [[Method]]
*/
public get args (): Codec[] {
return this.method.args;
}
/**
* @description Thge argument defintions, compatible with [[Method]]
*/
public get argsDef (): ArgsDef {
return this.method.argsDef;
}
/**
* @description The actual `[sectionIndex, methodIndex]` as used in the Method
*/
public get callIndex (): Uint8Array {
return this.method.callIndex;
}
/**
* @description The actual data for the Method
*/
public get data (): Uint8Array {
return this.method.data;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
const length = this.length;
return length + Compact.encodeU8a(length).length;
}
/**
* @description Convernience function, encodes the extrinsic and returns the actual hash
*/
public get hash (): Hash {
return new Hash(
blake2AsU8a(this.toU8a(), 256)
);
}
/**
* @description `true` is method has `Origin` argument (compatibility with [[Method]])
*/
public get hasOrigin (): boolean {
return this.method.hasOrigin;
}
/**
* @description `true` id the extrinsic is signed
*/
public get isSigned (): boolean {
return this.signature.isSigned;
}
/**
* @description The length of the encoded value
*/
public get length (): number {
return this.toU8a(true).length;
}
/**
* @description The [[FunctionMetadata]] that describes the extrinsic
*/
public get meta (): FunctionMetadata {
return this.method.meta;
}
/**
* @description The [[Method]] this extrinsic wraps
*/
public get method (): Method {
return this.get('method') as Method;
}
/**
* @description The [[ExtrinsicSignature]]
*/
public get signature (): ExtrinsicSignature {
return this.get('signature') as ExtrinsicSignature;
}
/**
* @description Add an [[ExtrinsicSignature]] to the extrinsic (already generated)
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, nonce: AnyNumber, era: Uint8Array | IExtrinsicEra): ExtrinsicV1 {
this.signature.addSignature(signer, signature, nonce, era);
return this;
}
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): ExtrinsicV1 {
this.signature.sign(this.method, account, options);
return this;
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.toHex();
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
// We are treating this in the same way we do a primitive, this is known
return 'Extrinsic';
}
/**
* @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 {
const encoded = super.toU8a();
return isBare
? encoded
: Compact.addLengthPrefix(encoded);
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import Vector from '../codec/Vector';
import Extrinsic from './Extrinsic';
import Extrinsic from '../primitive/Extrinsic/Extrinsic';
/**
* @name Extrinsics
-141
View File
@@ -1,141 +0,0 @@
// 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, AnyU8a, IExtrinsicEra, IKeyringPair } from '../types';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Struct from '../codec/Struct';
import U8a from '../codec/U8a';
import Hash from '../primitive/Hash';
import Method from '../primitive/Method';
import RuntimeVersion from '../rpc/RuntimeVersion';
import ExtrinsicEra from './ExtrinsicEra';
import Nonce from './NonceCompact';
interface SignaturePayloadValue {
nonce?: AnyNumber;
method?: Method;
era?: AnyU8a | IExtrinsicEra;
blockHash?: AnyU8a;
}
// a helper function for both types of payloads, Raw and metadata-known
function sign (signerPair: IKeyringPair, u8a: Uint8Array): Uint8Array {
const encoded = u8a.length > 256
? blake2AsU8a(u8a)
: u8a;
return signerPair.sign(encoded);
}
/**
* @name SignaturePayload
* @description
* A signing payload for an [[Extrinsic]]. For the final encoding, it is variable length based
* on the contents included
*
* 1-8 bytes: The Transaction Compact<Index/Nonce> as provided in the transaction itself.
* 2+ bytes: The Function Descriptor as provided in the transaction itself.
* 1/2 bytes: The Transaction Era as provided in the transaction itself.
* 32 bytes: The hash of the authoring block implied by the Transaction Era and the current block.
*/
export default class SignaturePayload extends Struct {
protected _signature?: Uint8Array;
public constructor (value?: SignaturePayloadValue | Uint8Array) {
super({
nonce: Nonce,
method: Method,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description `true` if the payload refers to a valid signature
*/
public get isSigned (): boolean {
return !!(this._signature && this._signature.length === 64);
}
/**
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
return this.get('blockHash') as Hash;
}
/**
* @description The [[Method]] contained in the payload
*/
public get method (): Method {
return this.get('method') as Method;
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description The [[Nonce]]
*/
public get nonce (): Nonce {
return this.get('nonce') as Nonce;
}
/**
* @description The raw signature as a `Uint8Array`
*/
public get signature (): Uint8Array {
if (!this.isSigned) {
throw new Error('Transaction is not signed');
}
return this._signature as Uint8Array;
}
/**
* @description Sign the payload with the keypair
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sign (signerPair: IKeyringPair, version?: RuntimeVersion): Uint8Array {
this._signature = sign(signerPair, this.toU8a());
return this._signature;
}
}
/**
* @name SignaturePayloadRaw
* @description
* A version of [[SignaturePayload]] where it does not rely on [[Method]] being initalized with metadata. When constructing, it treats the [[Method]] as a raw stream of bytes, so will always apply the signature over this without any additional checking. Unlike the [[SignaturePayload]], it assumed that you will only construct and sign, thereby providing no insigt into constructed values
*/
export class SignaturePayloadRaw extends Struct {
public constructor (value?: any) {
super({
nonce: Nonce,
method: U8a,
era: ExtrinsicEra,
blockHash: Hash
}, value);
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.get('era') as ExtrinsicEra;
}
/**
* @description Sign the payload with the keypair
*/
public sign (signerPair: IKeyringPair): Uint8Array {
return sign(signerPair, this.toU8a());
}
}
-4
View File
@@ -19,9 +19,6 @@ export { default as BlockNumber } from './BlockNumber';
export { default as Conviction } from './Conviction';
export { default as EraIndex } from './EraIndex';
export { default as Exposure } from './Exposure';
export { default as Extrinsic } from './Extrinsic';
export { default as ExtrinsicEra, MortalEra, ImmortalEra } from './ExtrinsicEra';
export { default as ExtrinsicSignature } from './ExtrinsicSignature';
// NOTE Only used internally, exported as PendingExtrinsics
// export { default as Extrinsics } from './Extrinsics';
export { default as IndividualExposure } from './IndividualExposure';
@@ -52,7 +49,6 @@ export { default as SessionIndex } from './SessionIndex';
export { default as SessionKey } from './SessionKey';
export { default as SessionKeys } from './SessionKeys';
export { default as SetIndex } from './SetIndex';
export { default as SignaturePayload, SignaturePayloadRaw } from './SignaturePayload';
export { default as StakingLedger } from './StakingLedger';
export { default as TreasuryProposal } from './TreasuryProposal';
export { default as UncleEntryItem } from './UncleEntryItem';
+41 -13
View File
@@ -6,8 +6,10 @@ import BN from 'bn.js';
import U8a from './codec/U8a';
import { FunctionMetadata } from './Metadata/v6/Calls';
import BalanceCompact from './primitive/BalanceCompact';
import Method from './primitive/Method';
import Address from './primitive/Address';
import NonceCompact from './type/NonceCompact';
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IKeyringPair {
@@ -62,7 +64,6 @@ export interface Codec {
/**
* @description Compares the value of the input to see if there is a match
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
eq (other?: any): boolean;
/**
@@ -123,12 +124,11 @@ export interface SignatureOptions {
blockHash: AnyU8a;
era?: IExtrinsicEra;
nonce: AnyNumber;
tip?: AnyNumber;
version?: RuntimeVersionInterface;
}
export interface ArgsDef {
[index: string]: Constructor;
}
export type ArgsDef = Record<string, Constructor>;
// eslint-disable-next-line @typescript-eslint/interface-name-prefix,@typescript-eslint/no-empty-interface
export interface IHash extends U8a { }
@@ -143,24 +143,52 @@ export interface IMethod extends Codec {
readonly meta: FunctionMetadata;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsicSignature extends Codec {
interface ExtrinsicSignatureBase {
readonly isSigned: boolean;
readonly era: IExtrinsicEra;
readonly nonce: NonceCompact;
readonly signature: IHash;
readonly signer: Address;
readonly tip: BalanceCompact;
}
export interface ExtrinsicPayloadValue {
era: IExtrinsicEra | AnyU8a;
method: AnyU8a;
nonce: AnyNumber;
tip: AnyNumber;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsicEra {
export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec {
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: Uint8Array | string): IExtrinsicSignature;
sign (method: Method, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsicEra extends Codec {
asImmortalEra: Codec;
asMortalEra: Codec;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsic extends IMethod {
hash: IHash;
isSigned: boolean;
method: Method;
signature: IExtrinsicSignature;
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, nonce: AnyNumber, era: Uint8Array | IExtrinsicEra): IExtrinsic;
export interface IExtrinsicImpl extends Codec {
readonly method: Method;
readonly signature: IExtrinsicSignature;
readonly version: number;
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicImpl;
sign (account: IKeyringPair, options: SignatureOptions): IExtrinsicImpl;
}
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IExtrinsic extends ExtrinsicSignatureBase, IMethod {
readonly hash: IHash;
readonly length: number;
readonly method: Method;
readonly type: number;
readonly version: number;
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsic;
sign (account: IKeyringPair, options: SignatureOptions): IExtrinsic;
}