move SignerPayload to @polkadot/types (#1360)
* move SignerPayload to @polkadot/types * fix errors * update changelog * improve naming and code style
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Compact, Option, Vec } from './codec';
|
||||
import { Bytes, Data, Fixed64, H160, H256, H512, Null, StorageData, StorageHasher, StorageKey, Text, Type, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive';
|
||||
import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PhantomData, PreRuntime, Seal, SealV0, Signature, SignedBlock, Sr25519Signature, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
|
||||
import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PhantomData, PreRuntime, Seal, SealV0, Signature, SignedBlock, SignerPayload, Sr25519Signature, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
|
||||
import { InclusionHeight, Uncle, UncleEntryItem } from './interfaces/authorship';
|
||||
import { RawAuraPreDigest } from './interfaces/aura';
|
||||
import { BabeAuthorityWeight, BabeBlockWeight, BabeWeight, RawBabePreDigest, RawBabePreDigestPrimary, RawBabePreDigestSecondary, SlotNumber } from './interfaces/babe';
|
||||
@@ -241,6 +241,9 @@ export interface InterfaceRegistry {
|
||||
SignedBlock: SignedBlock;
|
||||
'Option<SignedBlock>': Option<SignedBlock>;
|
||||
'Vec<SignedBlock>': Vec<SignedBlock>;
|
||||
SignerPayload: SignerPayload;
|
||||
'Option<SignerPayload>': Option<SignerPayload>;
|
||||
'Vec<SignerPayload>': Vec<SignerPayload>;
|
||||
ValidatorId: ValidatorId;
|
||||
'Option<ValidatorId>': Option<ValidatorId>;
|
||||
'Vec<ValidatorId>': Vec<ValidatorId>;
|
||||
|
||||
@@ -52,6 +52,7 @@ export default {
|
||||
block: 'Block',
|
||||
justification: 'Justification'
|
||||
},
|
||||
SignerPayload: 'GenericSignerPayload',
|
||||
ValidatorId: 'AccountId',
|
||||
Weight: 'u32',
|
||||
WeightMultiplier: 'Fixed64',
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { Codec } from '../../types';
|
||||
import { Compact, Struct } from '../../codec';
|
||||
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericImmortalEra, GenericMortalEra, GenericOrigin, H256, H512, Null, StorageData, StorageKey, u128, u32, u64 } from '../../primitive';
|
||||
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericImmortalEra, GenericMortalEra, GenericOrigin, GenericSignerPayload, H256, H512, Null, StorageData, StorageKey, u128, u32, u64 } from '../../primitive';
|
||||
|
||||
/** GenericAccountId */
|
||||
export type AccountId = GenericAccountId;
|
||||
@@ -159,6 +159,9 @@ export interface SignedBlock extends Struct {
|
||||
readonly justification: Justification;
|
||||
}
|
||||
|
||||
/** GenericSignerPayload */
|
||||
export type SignerPayload = GenericSignerPayload;
|
||||
|
||||
/** Signature */
|
||||
export type Sr25519Signature = Signature;
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright 2017-2019 @polkadot/api 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 { createType, GenericCall } from '@polkadot/types';
|
||||
|
||||
import SignerPayload from './SignerPayload';
|
||||
|
||||
describe('SignerPayload', (): void => {
|
||||
const TEST = {
|
||||
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
|
||||
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
|
||||
blockNumber: '0x00231d30',
|
||||
era: '0x0703',
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
method: '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
|
||||
nonce: '0x00001234',
|
||||
specVersion: '0x00000000',
|
||||
tip: '0x00000000000000000000000000005678',
|
||||
version: 3
|
||||
};
|
||||
|
||||
beforeEach((): void => {
|
||||
GenericCall.injectMethods(extrinsics);
|
||||
});
|
||||
|
||||
it('creates a valid JSON output', (): void => {
|
||||
expect(
|
||||
new SignerPayload({
|
||||
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
|
||||
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
|
||||
blockNumber: '0x231d30',
|
||||
era: createType('ExtrinsicEra', { current: 2301232, period: 200 }),
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
method: createType('Call', '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c'),
|
||||
nonce: 0x1234,
|
||||
tip: 0x5678,
|
||||
version: 3
|
||||
}).toPayload()
|
||||
).toEqual({
|
||||
address: '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE',
|
||||
blockHash: '0xde8f69eeb5e065e18c6950ff708d7e551f68dc9bf59a07c52367c0280f805ec7',
|
||||
blockNumber: '0x00231d30',
|
||||
era: '0x0703',
|
||||
genesisHash: '0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b',
|
||||
method: '0x0500ffd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9e56c',
|
||||
nonce: '0x00001234',
|
||||
specVersion: '0x00000000',
|
||||
tip: '0x00000000000000000000000000005678',
|
||||
version: 3
|
||||
});
|
||||
});
|
||||
|
||||
it('re-constructs from JSON', (): void => {
|
||||
expect(
|
||||
new SignerPayload(TEST).toPayload()
|
||||
).toEqual(TEST);
|
||||
});
|
||||
|
||||
it('re-constructs from itself', (): void => {
|
||||
expect(
|
||||
new SignerPayload(
|
||||
new SignerPayload(TEST)
|
||||
).toPayload()
|
||||
).toEqual(TEST);
|
||||
});
|
||||
|
||||
it('can be used as a feed to ExtrinsicPayload', (): void => {
|
||||
const signer = new SignerPayload(TEST).toPayload();
|
||||
const payload = createType('ExtrinsicPayload', signer, { version: signer.version });
|
||||
|
||||
expect(payload.era.toHex()).toEqual(TEST.era);
|
||||
expect(payload.method.toHex()).toEqual(TEST.method);
|
||||
expect(payload.blockHash.toHex()).toEqual(TEST.blockHash);
|
||||
expect(payload.nonce.eq(TEST.nonce)).toBe(true);
|
||||
expect(payload.tip.eq(TEST.tip)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
// Copyright 2017-2019 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../../interfaces';
|
||||
import Compact from '../../codec/Compact';
|
||||
import Struct from '../../codec/Struct';
|
||||
import { createType } from '../../codec';
|
||||
import { Codec, Constructor, ISignerPayload, SignerPayloadJSON, SignerPayloadRaw } from '../../types';
|
||||
import u8 from '../U8';
|
||||
|
||||
export interface SignerPayloadType extends Codec {
|
||||
address: Address;
|
||||
blockHash: Hash;
|
||||
blockNumber: BlockNumber;
|
||||
era: ExtrinsicEra;
|
||||
genesisHash: Hash;
|
||||
method: Call;
|
||||
nonce: Compact<Index>;
|
||||
runtimeVersion: RuntimeVersion;
|
||||
tip: Compact<Balance>;
|
||||
version: u8;
|
||||
}
|
||||
|
||||
// We explicitly cast the type here to get the actual TypeScript exports right
|
||||
// We can ignore the properties, added via Struct.with
|
||||
const _Payload: Constructor<SignerPayloadType> = Struct.with({
|
||||
address: 'Address',
|
||||
blockHash: 'Hash',
|
||||
blockNumber: 'BlockNumber',
|
||||
era: 'ExtrinsicEra',
|
||||
genesisHash: 'Hash',
|
||||
method: 'Call',
|
||||
nonce: 'Compact<Index>',
|
||||
runtimeVersion: 'RuntimeVersion',
|
||||
tip: 'Compact<Balance>',
|
||||
version: 'u8'
|
||||
}) as any;
|
||||
|
||||
export default class SignerPayload extends _Payload implements ISignerPayload {
|
||||
/**
|
||||
* @description Creates an representation of the structure as an ISignerPayload JSON
|
||||
*/
|
||||
public toPayload (): SignerPayloadJSON {
|
||||
const { address, blockHash, blockNumber, era, genesisHash, method, nonce, runtimeVersion: { specVersion }, tip, version } = this;
|
||||
|
||||
return {
|
||||
address: address.toString(),
|
||||
blockHash: blockHash.toHex(),
|
||||
blockNumber: blockNumber.toHex(),
|
||||
era: era.toHex(),
|
||||
genesisHash: genesisHash.toHex(),
|
||||
method: method.toHex(),
|
||||
nonce: nonce.toHex(),
|
||||
specVersion: specVersion.toHex(),
|
||||
tip: tip.toHex(),
|
||||
version: version.toNumber()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Creates a representation of the payload in raw Exrinsic form
|
||||
*/
|
||||
public toRaw (): SignerPayloadRaw {
|
||||
const payload = this.toPayload();
|
||||
const data = createType('ExtrinsicPayload', payload, { version: payload.version }).toHex();
|
||||
|
||||
return {
|
||||
address: payload.address,
|
||||
data,
|
||||
type: 'payload'
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -13,3 +13,4 @@ export { default as GenericExtrinsicV2 } from './v2/Extrinsic';
|
||||
export { default as GenericExtrinsicPayloadV2 } from './v2/ExtrinsicPayload';
|
||||
export { default as GenericExtrinsicV3 } from './v3/Extrinsic';
|
||||
export { default as GenericExtrinsicPayloadV3 } from './v3/ExtrinsicPayload';
|
||||
export { default as GenericSignerPayload } from './SignerPayload';
|
||||
|
||||
@@ -216,3 +216,85 @@ export interface IExtrinsic extends IExtrinsicSignable<IExtrinsic>, ExtrinsicSig
|
||||
readonly type: number;
|
||||
readonly version: number;
|
||||
}
|
||||
|
||||
export interface SignerPayloadJSON {
|
||||
/**
|
||||
* @description The ss-58 encoded address
|
||||
*/
|
||||
address: string;
|
||||
|
||||
/**
|
||||
* @description The checkpoint hash of the block, in hex
|
||||
*/
|
||||
blockHash: string;
|
||||
|
||||
/**
|
||||
* @description The checkpoint block number, in hex
|
||||
*/
|
||||
blockNumber: string;
|
||||
|
||||
/**
|
||||
* @description The era for this transaction, in hex
|
||||
*/
|
||||
era: string;
|
||||
|
||||
/**
|
||||
* @description The genesis hash of the chain, in hex
|
||||
*/
|
||||
genesisHash: string;
|
||||
|
||||
/**
|
||||
* @description The encoded method (with arguments) in hex
|
||||
*/
|
||||
method: string;
|
||||
|
||||
/**
|
||||
* @description The nonce for this transaction, in hex
|
||||
*/
|
||||
nonce: string;
|
||||
|
||||
/**
|
||||
* @description The current spec version for the runtime
|
||||
*/
|
||||
specVersion: string;
|
||||
|
||||
/**
|
||||
* @description The tip for this transaction, in hex
|
||||
*/
|
||||
tip: string;
|
||||
|
||||
/**
|
||||
* @description The version of the extrinsic we are dealing with
|
||||
*/
|
||||
version: number;
|
||||
}
|
||||
|
||||
export interface SignerPayloadRawBase {
|
||||
/**
|
||||
* @description The hex-encoded data for this request
|
||||
*/
|
||||
data: string;
|
||||
|
||||
/**
|
||||
* @description The type of the contained data
|
||||
*/
|
||||
type?: 'bytes' | 'payload';
|
||||
}
|
||||
|
||||
export interface SignerPayloadRaw extends SignerPayloadRawBase {
|
||||
/**
|
||||
* @description The ss-58 encoded address
|
||||
*/
|
||||
address: string;
|
||||
|
||||
/**
|
||||
* @description The type of the contained data
|
||||
*/
|
||||
type: 'bytes' | 'payload';
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
|
||||
export interface ISignerPayload {
|
||||
toPayload (): SignerPayloadJSON;
|
||||
toRaw (): SignerPayloadRaw;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user