UncheckedEncoding (#192)
* Re add type-extrinsics * Add descriptor * Add uncheked mortal extrinsic * Add e2e test * Add versionInfo and signatureInfo * Skip e2e test * Small updates * Additions for era & extra types * Move types to types, fixup tests * Small test updates * Update test for full decoding
This commit is contained in:
@@ -13,4 +13,4 @@
|
||||
"@babel/runtime": "^7.0.0",
|
||||
"@polkadot/util": "^0.30.7"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,9 +24,11 @@ describe('fromMetadata', () => {
|
||||
expect(newExtrinsics.timestamp.set([10101]).toU8a()).toEqual(
|
||||
new Uint8Array([
|
||||
// length (encoded)
|
||||
10 << 2,
|
||||
11 << 2,
|
||||
// version, no signature
|
||||
1,
|
||||
// index
|
||||
3, 0,
|
||||
2, 0,
|
||||
// values
|
||||
117, 39, 0, 0, 0, 0, 0, 0
|
||||
])
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import camelCase from '@polkadot/util/string/camelCase';
|
||||
import Metadata, { RuntimeModuleMetadata } from '@polkadot/types/Metadata';
|
||||
|
||||
import createExtrinsic from './utils/createExtrinsic';
|
||||
import createUnchecked from './utils/createUnchecked';
|
||||
import { Extrinsics, ModuleExtrinsics } from './types';
|
||||
|
||||
/**
|
||||
@@ -23,20 +23,26 @@ export default function fromMetadata (extrinsics: Extrinsics, metadata: Metadata
|
||||
return result;
|
||||
}, {} as Extrinsics);
|
||||
|
||||
return metadata.modules.reduce((result, moduleMetadata: RuntimeModuleMetadata, index) => {
|
||||
if (!moduleMetadata.module.call) {
|
||||
// Only increment indexes (starting at 0), for modules with actual functions
|
||||
let index = 0;
|
||||
|
||||
return metadata.modules.reduce((result, meta: RuntimeModuleMetadata) => {
|
||||
if (!meta.module.call || !meta.module.call.functions.length) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const prefix = moduleMetadata.prefix.toString();
|
||||
const prefix = camelCase(meta.prefix.toString());
|
||||
|
||||
result[moduleMetadata.prefix.toString()] = moduleMetadata.module.call.functions
|
||||
.reduce((newModule, func) => {
|
||||
// extrinsics.balances.set_balance -> extrinsics.balances.setBalance
|
||||
newModule[camelCase(func.name.toString())] = createExtrinsic(prefix, func.name, index as number, func);
|
||||
result[prefix] = meta.module.call.functions.reduce((newModule, funcMeta) => {
|
||||
// extrinsics.balances.set_balance -> extrinsics.balances.setBalance
|
||||
const funcName = camelCase(funcMeta.name.toString());
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleExtrinsics);
|
||||
newModule[funcName] = createUnchecked(prefix, funcName, index, funcMeta);
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleExtrinsics);
|
||||
|
||||
index++;
|
||||
|
||||
return result;
|
||||
}, result);
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import hexToU8a from '@polkadot/util/hex/toU8a';
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
|
||||
import extrinsics from './testing';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
describe('extrinsics', () => {
|
||||
it('encodes extrinsic correctly (nobody)', () => {
|
||||
expect(
|
||||
extrinsics.timestamp.set(
|
||||
10101
|
||||
).sign(
|
||||
keyring.nobody,
|
||||
1234
|
||||
).toU8a(true)
|
||||
).toEqual(
|
||||
new Uint8Array([
|
||||
// version
|
||||
0x81,
|
||||
// prefix
|
||||
0xff,
|
||||
// publicKey
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
// signature
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
// account nonce
|
||||
210, 4, 0, 0, 0, 0, 0, 0,
|
||||
// era
|
||||
0,
|
||||
// call index
|
||||
2, 0,
|
||||
// values
|
||||
117, 39, 0, 0, 0, 0, 0, 0
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('encodes an actual transfer (actual data)', () => {
|
||||
expect(
|
||||
extrinsics.balances
|
||||
.transfer(keyring.bob.publicKey(), 6969)
|
||||
.sign(keyring.alice, 0, '0xec7afaf1cca720ce88c1d1b689d81f0583cc15a97d621cf046dd9abf605ef22f')
|
||||
.toU8a()
|
||||
).toEqual(
|
||||
hexToU8a(
|
||||
'0x' +
|
||||
// length
|
||||
'7902' +
|
||||
// version
|
||||
'81' +
|
||||
// prefix + alice's publicKey
|
||||
'ffd172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f' +
|
||||
// signature
|
||||
'2b0d3b52ed0c145542f7b20a2fee2cf082b1d90c2a7bf7b3ec1e20a3eaf2aa92' +
|
||||
'9349c137d5df7f8499532fe3d4abe4e15f0ffd1c184b4c0277729d549120e805' +
|
||||
// nonce
|
||||
'0000000000000000' +
|
||||
// era
|
||||
'00' +
|
||||
// call index
|
||||
'0100' +
|
||||
// prefix + bob's publicKey
|
||||
'ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9' +
|
||||
'391b0000000000000000000000000000'
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import metadataRpc from '@polkadot/types/Metadata.rpc';
|
||||
import Metadata from '@polkadot/types/Metadata';
|
||||
|
||||
import extrinsics from './index';
|
||||
import fromMetadata from './fromMetadata';
|
||||
|
||||
export default fromMetadata(
|
||||
extrinsics,
|
||||
new Metadata().fromJSON(metadataRpc)
|
||||
);
|
||||
+7
-4
@@ -2,12 +2,15 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { UncheckedMortalExtrinsic } from '@polkadot/types/index';
|
||||
import { FunctionMetadata } from '@polkadot/types/Metadata';
|
||||
|
||||
export interface ExtrinsicFunction {
|
||||
(...args: any[]): void,
|
||||
method: string,
|
||||
section: string
|
||||
(...args: any[]): UncheckedMortalExtrinsic,
|
||||
meta: FunctionMetadata,
|
||||
method: string;
|
||||
section: string;
|
||||
toJSON: () => any;
|
||||
}
|
||||
|
||||
export interface ModuleExtrinsics {
|
||||
|
||||
+21
-22
@@ -2,22 +2,20 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { createType } from '@polkadot/types/codec';
|
||||
import { FunctionMetadata } from '@polkadot/types/Metadata';
|
||||
import { Extrinsic, Text } from '@polkadot/types/index';
|
||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||
|
||||
import { ExtrinsicFunction } from '../types';
|
||||
|
||||
import { FunctionMetadata } from '@polkadot/types/Metadata';
|
||||
import { Call, UncheckedMortalExtrinsic } from '@polkadot/types/index';
|
||||
|
||||
/**
|
||||
* From the metadata of a function in the module's storage, generate the function
|
||||
* that will return the correctly-encoded Extrinsic.
|
||||
* that will return the an UncheckExtrinsic.
|
||||
*
|
||||
* @param index - Index of the module section in the modules array.
|
||||
*/
|
||||
export default function createExtrinsic (
|
||||
section: string | Text,
|
||||
method: string | Text,
|
||||
export default function createDescriptor (
|
||||
section: string,
|
||||
method: string,
|
||||
index: number,
|
||||
meta: FunctionMetadata
|
||||
): ExtrinsicFunction {
|
||||
@@ -25,28 +23,29 @@ export default function createExtrinsic (
|
||||
|
||||
// If the extrinsic function has an argument of type `Origin`, we ignore it
|
||||
// FIXME should be `arg.type !== Origin`, but doesn't work...
|
||||
const expectedArgs = meta.arguments.filter((arg) => arg.type.toString() !== 'Origin');
|
||||
const expectedArgs = meta.arguments.filter((arg) =>
|
||||
arg.type.toString() !== 'Origin'
|
||||
);
|
||||
|
||||
extrinsicFn = (...args: any[]): Extrinsic => {
|
||||
extrinsicFn = (...args: any[]): UncheckedMortalExtrinsic => {
|
||||
if (expectedArgs.length.valueOf() !== args.length) {
|
||||
throw new Error(`Extrinsic ${section}.${method} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`);
|
||||
}
|
||||
|
||||
return new Extrinsic(
|
||||
u8aConcat(
|
||||
new Uint8Array([index, meta.id.toNumber()]),
|
||||
...expectedArgs.map((argument, index) => {
|
||||
const type = argument.type.toString(); // Argument type, as string
|
||||
|
||||
return createType(type, args[index]).toU8a();
|
||||
})
|
||||
)
|
||||
const call = new Call(
|
||||
new Uint8Array([index, meta.id.toNumber()]),
|
||||
meta,
|
||||
args
|
||||
);
|
||||
|
||||
return new UncheckedMortalExtrinsic(call);
|
||||
};
|
||||
|
||||
extrinsicFn.meta = meta;
|
||||
extrinsicFn.method = method.toString();
|
||||
extrinsicFn.section = section.toString();
|
||||
extrinsicFn.method = method;
|
||||
extrinsicFn.section = section;
|
||||
extrinsicFn.toJSON = (): any =>
|
||||
meta.toJSON();
|
||||
|
||||
return extrinsicFn as ExtrinsicFunction;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import Api from '@polkadot/api';
|
||||
import testingPairs from '@polkadot/util-keyring/testingPairs';
|
||||
import Ws from '@polkadot/api-provider/ws';
|
||||
|
||||
import extrinsics from '../../src/testing';
|
||||
|
||||
const keyring = testingPairs();
|
||||
|
||||
describe.skip('e2e transfer', () => {
|
||||
let api;
|
||||
|
||||
beforeAll(() => {
|
||||
// check other e2e tests here...
|
||||
api = new Api(new Ws('ws://127.0.0.1:9944'));
|
||||
});
|
||||
|
||||
// Error: [1002]: Inherent transactions cannot be queued.
|
||||
it.skip('inherent test', () => {
|
||||
const inherent = extrinsics.timestamp.set(Math.round(Date.now() / 1000));
|
||||
|
||||
return api.author.submitExtrinsic(inherent.toU8a()).then(console.log);
|
||||
});
|
||||
|
||||
it('makes a transfer for a transaction', () => {
|
||||
return api.chain
|
||||
.getBlockHash(0)
|
||||
.then((genesisHash) => {
|
||||
const extrinsic = extrinsics.balances.transfer(keyring.bob.publicKey(), 6969);
|
||||
|
||||
extrinsic.sign(keyring.alice, 0, genesisHash);
|
||||
|
||||
return api.author.submitExtrinsic(extrinsic.toU8a());
|
||||
})
|
||||
.then(console.log);
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,14 @@ const getBlock: MethodOpt = {
|
||||
type: 'SignedBlock'
|
||||
};
|
||||
|
||||
const getBlockHash: MethodOpt = {
|
||||
description: 'Get the block hash for a specific block',
|
||||
params: [
|
||||
createParam('blockNumber', 'BlockNumber', { isOptional: true })
|
||||
],
|
||||
type: 'Hash'
|
||||
};
|
||||
|
||||
const getHead: MethodOpt = {
|
||||
description: 'Retrieves the best headerHash',
|
||||
params: [],
|
||||
@@ -65,6 +73,7 @@ export default {
|
||||
section,
|
||||
methods: {
|
||||
getBlock: createMethod(section, 'getBlock', getBlock),
|
||||
getBlockHash: createMethod(section, 'getBlockHash', getBlockHash),
|
||||
getHead: createMethod(section, 'getHead', getHead),
|
||||
getHeader: createMethod(section, 'getHeader', getHeader),
|
||||
getRuntimeVersion: createMethod(section, 'getRuntimeVersion', getRuntimeVersion),
|
||||
|
||||
Vendored
+1
@@ -22,6 +22,7 @@ export type MethodOpt = {
|
||||
};
|
||||
|
||||
export type Method = {
|
||||
alias?: string,
|
||||
description: string,
|
||||
isDeprecated: boolean,
|
||||
isHidden: boolean,
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a } from './types';
|
||||
|
||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||
|
||||
import createType from './codec/createType';
|
||||
import Base from './codec/Base';
|
||||
import CallIndex from './CallIndex';
|
||||
import { FunctionMetadata } from './Metadata';
|
||||
|
||||
/**
|
||||
* Extrinsic function descriptor, as defined in
|
||||
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
|
||||
*/
|
||||
export default class Call extends CallIndex {
|
||||
protected _args: Array<Base>;
|
||||
protected _meta: FunctionMetadata;
|
||||
|
||||
constructor (index: Call | AnyU8a, meta: FunctionMetadata, args: Array<any>) {
|
||||
super(index);
|
||||
|
||||
if (index instanceof Call) {
|
||||
this._args = args || index.args;
|
||||
this._meta = meta || index.meta;
|
||||
} else {
|
||||
this._args = args;
|
||||
this._meta = meta;
|
||||
}
|
||||
}
|
||||
|
||||
get args () {
|
||||
return this._args;
|
||||
}
|
||||
|
||||
get index () {
|
||||
return this.raw;
|
||||
}
|
||||
|
||||
get meta () {
|
||||
return this._meta;
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
const args = this._meta.arguments
|
||||
.filter((arg) =>
|
||||
arg.type.toString() !== 'Origin'
|
||||
)
|
||||
.map((argument, index) =>
|
||||
createType(argument.type, this._args[index]).toU8a(isBare)
|
||||
);
|
||||
|
||||
return u8aConcat(
|
||||
super.toU8a(isBare),
|
||||
...args
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import CallIndex from './CallIndex';
|
||||
|
||||
describe('CallIndex', () => {
|
||||
it('handles u8a encoding correctly', () => {
|
||||
expect(
|
||||
new CallIndex([1, 2]).toU8a()
|
||||
).toEqual(new Uint8Array([1, 2]));
|
||||
});
|
||||
|
||||
it('provides access to section/method indexes', () => {
|
||||
const ci = new CallIndex([1, 2]);
|
||||
|
||||
expect(ci.methodIndex).toEqual(2);
|
||||
expect(ci.sectionIndex).toEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a } from './types';
|
||||
|
||||
import U8aFixed from './codec/U8aFixed';
|
||||
|
||||
export default class CallIndex extends U8aFixed {
|
||||
constructor (value?: AnyU8a) {
|
||||
super(value, 16);
|
||||
}
|
||||
|
||||
get methodIndex (): number {
|
||||
return this.raw[1];
|
||||
}
|
||||
|
||||
get sectionIndex (): number {
|
||||
return this.raw[0];
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import U32 from './U32';
|
||||
import U64 from './U64';
|
||||
|
||||
// The Nonce or number of transactiosn sent by a specific account. Generally used
|
||||
// with extrinsics to determine the order of execution.
|
||||
export default class Index extends U32 {
|
||||
export default class Index extends U64 {
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import Address from './Address';
|
||||
|
||||
// Wrapper for a RawAddress.
|
||||
// NOTE Not 100% on the differences between this and Address in the runtime
|
||||
export default class RawAddress extends Address {
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { KeyringPair } from '@polkadot/util-keyring/types';
|
||||
import { AnyNumber, AnyU8a } from './types';
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import Call from './Call';
|
||||
import Hash from './Hash';
|
||||
import Nonce from './Nonce';
|
||||
import TransactionEra from './TransactionEra';
|
||||
|
||||
type SignaturePayloadValue = {
|
||||
nonce?: AnyNumber,
|
||||
call?: Call,
|
||||
era?: AnyU8a | TransactionEra
|
||||
blockHash?: AnyU8a
|
||||
};
|
||||
|
||||
// Signing Payload.
|
||||
// 8 bytes: The Transaction Index/Nonce as provided in the transaction itself.
|
||||
// 2+ bytes: The Function Descriptor as provided in the transaction itself.
|
||||
// 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;
|
||||
|
||||
constructor (value?: SignaturePayloadValue) {
|
||||
super({
|
||||
nonce: Nonce,
|
||||
call: Call,
|
||||
era: TransactionEra,
|
||||
blockHash: Hash
|
||||
}, value);
|
||||
}
|
||||
|
||||
get isSigned (): boolean {
|
||||
return !!(this._signature && this._signature.length === 64);
|
||||
}
|
||||
|
||||
get blockHash (): Hash {
|
||||
return this.raw.blockHash as Hash;
|
||||
}
|
||||
|
||||
get call (): Call {
|
||||
return this.raw.call as Call;
|
||||
}
|
||||
|
||||
get era (): TransactionEra {
|
||||
return this.raw.era as TransactionEra;
|
||||
}
|
||||
|
||||
get nonce (): Nonce {
|
||||
return this.raw.nonce as Nonce;
|
||||
}
|
||||
|
||||
get signature (): Uint8Array {
|
||||
if (!this.isSigned) {
|
||||
throw new Error('Transaction is not signed');
|
||||
}
|
||||
|
||||
return this._signature as Uint8Array;
|
||||
}
|
||||
|
||||
sign (signerPair: KeyringPair): Uint8Array {
|
||||
this._signature = signerPair.sign(this.toU8a());
|
||||
|
||||
return this._signature;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a } from './types';
|
||||
|
||||
import U8aFixed from './codec/U8aFixed';
|
||||
|
||||
// Description of a transaction era
|
||||
// FIXME should be a enum?
|
||||
export default class TransactionEra extends U8aFixed {
|
||||
constructor (value?: AnyU8a) {
|
||||
// FIXME this actually indicates an Immortal (i.e. 1 byte)
|
||||
super(value, 8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, AnyU8a } from './types';
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import Address from './Address';
|
||||
import TransactionEra from './TransactionEra';
|
||||
import Nonce from './Nonce';
|
||||
import Signature from './Signature';
|
||||
|
||||
type TransactionSignatureValue = {
|
||||
signer?: AnyU8a | Address,
|
||||
signature?: AnyU8a
|
||||
nonce?: AnyNumber,
|
||||
era?: AnyU8a
|
||||
};
|
||||
|
||||
// Signature Information.
|
||||
// 1/3/5/9/33 bytes: The signing account identity, in Address format
|
||||
// 64 bytes: The Ed25519 signature of the Signing Payload
|
||||
// 8 bytes: The Transaction Index of the signing account
|
||||
// 2 bytes: The Transaction Era
|
||||
export default class TransactionSignature extends Struct {
|
||||
constructor (value?: TransactionSignatureValue) {
|
||||
super({
|
||||
signer: Address,
|
||||
signature: Signature,
|
||||
nonce: Nonce,
|
||||
era: TransactionEra
|
||||
}, value);
|
||||
}
|
||||
|
||||
get era (): TransactionEra {
|
||||
return this.raw.era as TransactionEra;
|
||||
}
|
||||
|
||||
get nonce (): Nonce {
|
||||
return this.raw.nonce as Nonce;
|
||||
}
|
||||
|
||||
get signature (): Signature {
|
||||
return this.raw.signature as Signature;
|
||||
}
|
||||
|
||||
get signer (): Address {
|
||||
return this.raw.signer as Address;
|
||||
}
|
||||
}
|
||||
@@ -58,9 +58,10 @@ export default class Type extends Text {
|
||||
// alias String -> Text (compat with jsonrpc methods)
|
||||
this._alias('String', 'Text'),
|
||||
// alias Vec<u8> -> Bytes
|
||||
this._alias('Vec<u8>', 'Bytes')
|
||||
this._alias('Vec<u8>', 'Bytes'),
|
||||
// alias RawAddress -> Address
|
||||
this._alias('RawAddress', 'Address')
|
||||
// TODO Check these for possibly matching -
|
||||
// `RawAddress` -> `Address` (implementation looks the same)
|
||||
// `PropIndex` -> `ProposalIndex` (implementation looks the same, however meant as diff)
|
||||
];
|
||||
|
||||
|
||||
@@ -8,6 +8,6 @@ import UInt from './codec/UInt';
|
||||
|
||||
export default class U16 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 16);
|
||||
super(value, 16, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ import UInt from './codec/UInt';
|
||||
|
||||
export default class U32 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 32);
|
||||
super(value, 32, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ import UInt from './codec/UInt';
|
||||
|
||||
export default class U64 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 64);
|
||||
super(value, 64, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,6 @@ import UInt from './codec/UInt';
|
||||
|
||||
export default class U8 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 8);
|
||||
super(value, 8, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright 2017-2018 @polkadot/extrinsics authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { KeyringPair } from '@polkadot/util-keyring/types';
|
||||
import { AnyNumber, AnyU8a } from './types';
|
||||
|
||||
import u8aConcat from '@polkadot/util/u8a/concat';
|
||||
|
||||
import Address from './Address';
|
||||
import Call from './Call';
|
||||
import Extrinsic from './Extrinsic';
|
||||
import SignaturePayload from './SignaturePayload';
|
||||
import TransactionSignature from './TransactionSignature';
|
||||
|
||||
const EMPTY_U8A = new Uint8Array();
|
||||
const IMMORTAL_ERA = new Uint8Array([0]);
|
||||
|
||||
/**
|
||||
* Unchecked mortal extrinsic, as defined here:
|
||||
* {@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 UncheckedMortalExtrinsic extends Extrinsic {
|
||||
protected _call: Call;
|
||||
protected _signature?: Uint8Array;
|
||||
|
||||
constructor (call: Call) {
|
||||
super();
|
||||
|
||||
this._call = call;
|
||||
this.raw = UncheckedMortalExtrinsic.encode(call);
|
||||
}
|
||||
|
||||
static encode (call: Call, signature?: TransactionSignature): Uint8Array {
|
||||
// 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).
|
||||
const highBit = signature ? 0b10000000 : 0;
|
||||
const version = 0b00000001;
|
||||
|
||||
return u8aConcat(
|
||||
new Uint8Array([highBit | version]),
|
||||
signature
|
||||
? signature.toU8a()
|
||||
: EMPTY_U8A,
|
||||
call.toU8a()
|
||||
);
|
||||
}
|
||||
|
||||
get isSigned () {
|
||||
return this._signature;
|
||||
}
|
||||
|
||||
sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a): UncheckedMortalExtrinsic {
|
||||
const signature = this.createSignature(this._call, signerPair, nonce, blockHash);
|
||||
|
||||
this._signature = signature.toU8a();
|
||||
this.raw = UncheckedMortalExtrinsic.encode(this._call, signature);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private createSignature (call: Call, signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a, era: Uint8Array = IMMORTAL_ERA): TransactionSignature {
|
||||
const signer = new Address(signerPair.publicKey());
|
||||
|
||||
const signingPayload = new SignaturePayload({
|
||||
nonce,
|
||||
call,
|
||||
era,
|
||||
blockHash
|
||||
});
|
||||
const signature = signingPayload.sign(signerPair);
|
||||
|
||||
return new TransactionSignature({
|
||||
era: signingPayload.era,
|
||||
nonce: signingPayload.nonce,
|
||||
signer,
|
||||
signature
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import Base from './Base';
|
||||
// it needs to decoded in the specific defined order.
|
||||
export default class Struct <
|
||||
// The actual Class structure, i.e. key -> Class
|
||||
S = { [index: string]: { new(value?: any): Base } },
|
||||
S = { [index: string]: { new(...value: Array<any>): Base } },
|
||||
// internal type, instance of classes mapped by key
|
||||
T = { [K in keyof S]: Base },
|
||||
// input values, mapped by key can be anything (construction)
|
||||
|
||||
@@ -21,7 +21,7 @@ describe('Tuple', () => {
|
||||
tuple.toJSON()
|
||||
).toEqual([
|
||||
'foo',
|
||||
'0x00000045'
|
||||
69
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -38,7 +38,7 @@ describe('Tuple', () => {
|
||||
]).toJSON()
|
||||
).toEqual([
|
||||
'bazzing',
|
||||
'0x00000020',
|
||||
32,
|
||||
''
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import { AnyU8a } from '../types';
|
||||
|
||||
import U8a from './U8a';
|
||||
|
||||
type BitLength = 64 | 128 | 256 | 512;
|
||||
type BitLength = 8 | 16 | 32 | 64 | 128 | 256 | 512;
|
||||
|
||||
// A U8a that manages a a sequence of bytes up to the specified bitLength. Not meant
|
||||
// to be used directly, rather is should be subclassed with the specific lengths.
|
||||
|
||||
@@ -43,4 +43,9 @@ describe('UInt', () => {
|
||||
new UInt(a).toNumber()
|
||||
).toEqual(123);
|
||||
});
|
||||
|
||||
it('converts to JSON depending on flags', () => {
|
||||
expect(new UInt(0x12, 16).toJSON()).toEqual('0x0012');
|
||||
expect(new UInt(0x12, 16, false).toJSON()).toEqual(0x12);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,13 +26,15 @@ type BitLength = 8 | 16 | 32 | 64 | 128 | 256;
|
||||
// - Apart from encoding/decoding we don't actuall keep check on the sizes, is this good enough?
|
||||
export default class UInt extends Base<BN> {
|
||||
private _bitLength: BitLength;
|
||||
private _isHexJson: boolean;
|
||||
|
||||
constructor (value: AnyNumber = 0, bitLength: BitLength = 64) {
|
||||
constructor (value: AnyNumber = 0, bitLength: BitLength = 64, isHexJson: boolean = true) {
|
||||
super(
|
||||
UInt.decode(value)
|
||||
);
|
||||
|
||||
this._bitLength = bitLength;
|
||||
this._isHexJson = isHexJson;
|
||||
}
|
||||
|
||||
static decode (value: AnyNumber): BN {
|
||||
@@ -71,7 +73,9 @@ export default class UInt extends Base<BN> {
|
||||
}
|
||||
|
||||
toJSON (): any {
|
||||
return this.toHex();
|
||||
return this._isHexJson
|
||||
? this.toHex()
|
||||
: this.toNumber();
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
import * as Types from '../index';
|
||||
import Text from '../Text';
|
||||
import Base from './Base';
|
||||
import Tuple from './Tuple';
|
||||
import Vector from './Vector';
|
||||
@@ -74,8 +75,8 @@ export function typeSplit (type: string): Array<string> {
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getTypeDef (_type: string): TypeDef {
|
||||
const type = _type.trim();
|
||||
export function getTypeDef (_type: Text | string): TypeDef {
|
||||
const type = _type.toString().trim();
|
||||
const value: TypeDef = {
|
||||
info: TypeDefInfo.Plain,
|
||||
type
|
||||
@@ -135,7 +136,7 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
return Type;
|
||||
}
|
||||
|
||||
export default function createType (type: string, value?: any): Base {
|
||||
export default function createType (type: Text | string, value?: any): Base {
|
||||
// l.debug(() => ['createType', { type, value }]);
|
||||
|
||||
const Type = getTypeClass(
|
||||
|
||||
@@ -18,6 +18,8 @@ export { default as Block } from './Block';
|
||||
export { default as BlockNumber } from './BlockNumber';
|
||||
export { default as Bool } from './Bool';
|
||||
export { default as Bytes } from './Bytes';
|
||||
export { default as Call } from './Call';
|
||||
export { default as CallIndex } from './CallIndex';
|
||||
export { default as Extrinsic } from './Extrinsic';
|
||||
// NOTE Only used internally, exported as PendingExtrinsics
|
||||
// export { default as Extrinsics } from './Extrinsics';
|
||||
@@ -42,7 +44,6 @@ export { default as Perbill } from './Perbill';
|
||||
export { default as PropIndex } from './PropIndex';
|
||||
export { default as Proposal } from './Proposal';
|
||||
export { default as ProposalIndex } from './ProposalIndex';
|
||||
export { default as RawAddress } from './RawAddress';
|
||||
export { default as ReferendumIndex } from './ReferendumIndex';
|
||||
export { default as RuntimeVersion } from './RuntimeVersion';
|
||||
export { default as Signature } from './Signature';
|
||||
@@ -51,6 +52,8 @@ export { default as StorageChangeSet } from './StorageChangeSet';
|
||||
export { default as StorageData } from './StorageData';
|
||||
export { default as StorageKey } from './StorageKey';
|
||||
export { default as Text } from './Text';
|
||||
export { default as TransactionEra } from './TransactionEra';
|
||||
export { default as TransactionSignature } from './TransactionSignature';
|
||||
// NOTE Type is currently only used internally (possibly in codec-related work)
|
||||
// export { default as Type } from './Type';
|
||||
// NOTE We are exporting the U* classes as lowercase, matching with Rust
|
||||
@@ -60,6 +63,7 @@ export { default as u32 } from './U32';
|
||||
export { default as u64 } from './U64';
|
||||
export { default as u128 } from './U128';
|
||||
export { default as u256 } from './U256';
|
||||
export { default as UncheckedMortalExtrinsic } from './UncheckedMortalExtrinsic';
|
||||
export { default as ValidatorPrefs } from './ValidatorPrefs';
|
||||
export { default as VoteThreshold } from './VoteThreshold';
|
||||
export { default as VoteIndex } from './VoteIndex';
|
||||
|
||||
Reference in New Issue
Block a user