Move Event, Address, AccountId, AccountIndex to generic (#1202)

* Move Event, Address, AccountId, AccountIndex to generic

* Move AccountId, AccountIndex, Address & Event to generic

* Move Vote
This commit is contained in:
Jaco Greeff
2019-07-26 16:39:28 +02:00
committed by GitHub
parent 894f07e3c9
commit 02cd1331aa
69 changed files with 247 additions and 246 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
import '../injector';
import { ClassOf } from '../codec/createType';
import AccountId from '../primitive/AccountId';
import AccountId from '../primitive/Generic/AccountId';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import { CodecTo } from '../types';
+1 -1
View File
@@ -7,7 +7,7 @@ import { Codec, CodecTo } from '../types';
import extrinsics from '@polkadot/api-metadata/extrinsics/static';
import AccountId from '../primitive/AccountId';
import AccountId from '../primitive/Generic/AccountId';
import Method from '../primitive/Method';
import Text from '../primitive/Text';
import { createTypeUnsafe } from './createType';
+36 -23
View File
@@ -1,12 +1,12 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Compact, Option, Vec } from './codec';
import { AccountId, AccountIndex, Address, Bool, Bytes, Data, Event, Extrinsic, ExtrinsicEra, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H160, H256, H512, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, Vote, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive';
import { AccountIdOf, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, SessionKeys, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
import { Bytes, Data, Extrinsic, ExtrinsicEra, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericEvent, GenericVote, H160, H256, H512, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive';
import { AccountId, AccountIdOf, AccountIndex, Address, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, SessionKeys, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime';
import { AuthorityId } from './interfaces/consensus';
import { Keys, SessionIndex } from './interfaces/session';
import { AuthorityWeight, NextAuthority, PendingPause, PendingResume, StoredPendingChange, StoredState } from './interfaces/grandpa';
import { ApprovalFlag, SetIndex, VoteIndex, VoteThreshold, VoterInfo } from './interfaces/elections';
import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from './interfaces/elections';
import { InclusionHeight, Uncle, UncleEntryItem } from './interfaces/authorship';
import { BabeWeight } from './interfaces/babe';
import { BalanceLock, VestingSchedule, WithdrawReasons } from './interfaces/balances';
@@ -17,27 +17,11 @@ import { AccountInfo, Amount, AssetOf, InherentOfflineReport, LockPeriods, NewAc
import { Heartbeat, OpaqueMultiaddr, OpaqueNetworkState, OpaquePeerId } from './interfaces/imOnline';
import { AttestedCandidate, AuctionIndex, BalanceUpload, Bidder, CandidateReceipt, CollatorSignature, EgressQueueRoot, HeadData, IncomingParachain, IncomingParachainDeploy, IncomingParachainFixed, LeasePeriod, LeasePeriodOf, NewBidder, ParaId, ParaIdOf, ParachainDispatchOrigin, SlotRange, SubId, UpwardMessage, ValidatorIndex, ValidityAttestation, ValidityVote, WinningData, WinningDataEntry } from './interfaces/parachains';
import { EraIndex, EraRewards, Exposure, IndividualExposure, MomentOf, RewardDestination, StakingLedger, UnlockChunk, ValidatorPrefs } from './interfaces/staking';
import { DigestOf, EventId, EventIndex, EventRecord, EventRecord0to76, Key, Phase } from './interfaces/system';
import { DigestOf, Event, EventId, EventIndex, EventRecord, EventRecord0to76, Key, Phase } from './interfaces/system';
import { TreasuryProposal } from './interfaces/treasury';
import { ApiId, ChainProperties, ExtrinsicStatus, Health, KeyValueOption, NetworkState, PeerInfo, RuntimeVersion, RuntimeVersionApi, StorageChangeSet } from './interfaces/rpc';
export interface InterfaceRegistry {
AccountId: AccountId;
'Option<AccountId>': Option<AccountId>;
'Vec<AccountId>': Vec<AccountId>;
AccountIndex: AccountIndex;
'Compact<AccountIndex>': Compact<AccountIndex>;
'Option<AccountIndex>': Option<AccountIndex>;
'Vec<AccountIndex>': Vec<AccountIndex>;
Address: Address;
'Option<Address>': Option<Address>;
'Vec<Address>': Vec<Address>;
Event: Event;
'Option<Event>': Option<Event>;
'Vec<Event>': Vec<Event>;
Bool: Bool;
'Option<Bool>': Option<Bool>;
'Vec<Bool>': Vec<Bool>;
bool: bool;
'Option<bool>': Option<bool>;
'Vec<bool>': Vec<bool>;
@@ -172,9 +156,6 @@ export interface InterfaceRegistry {
'Compact<usize>': Compact<usize>;
'Option<usize>': Option<usize>;
'Vec<usize>': Vec<usize>;
Vote: Vote;
'Option<Vote>': Option<Vote>;
'Vec<Vote>': Vec<Vote>;
Extrinsic: Extrinsic;
'Option<Extrinsic>': Option<Extrinsic>;
'Vec<Extrinsic>': Vec<Extrinsic>;
@@ -190,6 +171,16 @@ export interface InterfaceRegistry {
SignaturePayload: SignaturePayload;
'Option<SignaturePayload>': Option<SignaturePayload>;
'Vec<SignaturePayload>': Vec<SignaturePayload>;
GenericAccountId: GenericAccountId;
'Option<GenericAccountId>': Option<GenericAccountId>;
'Vec<GenericAccountId>': Vec<GenericAccountId>;
GenericAccountIndex: GenericAccountIndex;
'Compact<GenericAccountIndex>': Compact<GenericAccountIndex>;
'Option<GenericAccountIndex>': Option<GenericAccountIndex>;
'Vec<GenericAccountIndex>': Vec<GenericAccountIndex>;
GenericAddress: GenericAddress;
'Option<GenericAddress>': Option<GenericAddress>;
'Vec<GenericAddress>': Vec<GenericAddress>;
GenericBlock: GenericBlock;
'Option<GenericBlock>': Option<GenericBlock>;
'Vec<GenericBlock>': Vec<GenericBlock>;
@@ -197,15 +188,31 @@ export interface InterfaceRegistry {
'Compact<GenericConsensusEngineId>': Compact<GenericConsensusEngineId>;
'Option<GenericConsensusEngineId>': Option<GenericConsensusEngineId>;
'Vec<GenericConsensusEngineId>': Vec<GenericConsensusEngineId>;
GenericEvent: GenericEvent;
'Option<GenericEvent>': Option<GenericEvent>;
'Vec<GenericEvent>': Vec<GenericEvent>;
GenericDigest: GenericDigest;
'Option<GenericDigest>': Option<GenericDigest>;
'Vec<GenericDigest>': Vec<GenericDigest>;
GenericDigestItem: GenericDigestItem;
'Option<GenericDigestItem>': Option<GenericDigestItem>;
'Vec<GenericDigestItem>': Vec<GenericDigestItem>;
GenericVote: GenericVote;
'Option<GenericVote>': Option<GenericVote>;
'Vec<GenericVote>': Vec<GenericVote>;
AccountId: AccountId;
'Option<AccountId>': Option<AccountId>;
'Vec<AccountId>': Vec<AccountId>;
AccountIdOf: AccountIdOf;
'Option<AccountIdOf>': Option<AccountIdOf>;
'Vec<AccountIdOf>': Vec<AccountIdOf>;
AccountIndex: AccountIndex;
'Compact<AccountIndex>': Compact<AccountIndex>;
'Option<AccountIndex>': Option<AccountIndex>;
'Vec<AccountIndex>': Vec<AccountIndex>;
Address: Address;
'Option<Address>': Option<Address>;
'Vec<Address>': Vec<Address>;
Balance: Balance;
'Compact<Balance>': Compact<Balance>;
'Option<Balance>': Option<Balance>;
@@ -341,6 +348,9 @@ export interface InterfaceRegistry {
'Compact<SetIndex>': Compact<SetIndex>;
'Option<SetIndex>': Option<SetIndex>;
'Vec<SetIndex>': Vec<SetIndex>;
Vote: Vote;
'Option<Vote>': Option<Vote>;
'Vec<Vote>': Vec<Vote>;
VoteIndex: VoteIndex;
'Compact<VoteIndex>': Compact<VoteIndex>;
'Option<VoteIndex>': Option<VoteIndex>;
@@ -596,6 +606,9 @@ export interface InterfaceRegistry {
Phase: Phase;
'Option<Phase>': Option<Phase>;
'Vec<Phase>': Vec<Phase>;
Event: Event;
'Option<Event>': Option<Event>;
'Vec<Event>': Vec<Event>;
EventRecord0to76: EventRecord0to76;
'Option<EventRecord0to76>': Option<EventRecord0to76>;
'Vec<EventRecord0to76>': Vec<EventRecord0to76>;
@@ -2,8 +2,7 @@
import { Codec } from '../../types';
import { Enum, Option } from '../../codec';
import { AccountId } from '../../primitive';
import { BlockNumber, Hash } from '../runtime';
import { AccountId, BlockNumber, Hash } from '../runtime';
/** BlockNumber */
export type InclusionHeight = BlockNumber;
@@ -1,7 +1,8 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Struct, Vec } from '../../codec';
import { AccountId, u32, u64 } from '../../primitive';
import { u32, u64 } from '../../primitive';
import { AccountId } from '../runtime';
/** u64 */
export type MemberCount = u64;
@@ -1,6 +1,6 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { AccountId } from '../../primitive';
import { AccountId } from '../runtime';
/** AccountId */
export type AuthorityId = AccountId;
@@ -45,7 +45,7 @@ export default {
maxEventTopics: 'u32',
maxStackHeight: 'u32',
maxMemoryPages: 'u32',
enablePrintln: 'Bool',
enablePrintln: 'bool',
maxSubjectLen: 'u32'
},
SeedOf: 'Hash'
@@ -1,7 +1,7 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Compact, Enum, Option, Struct, Vec } from '../../codec';
import { Bool, Bytes, Null, u32, u64, u8 } from '../../primitive';
import { Bytes, Null, bool, u32, u64, u8 } from '../../primitive';
import { Balance, BlockNumber, Hash } from '../runtime';
/** Struct */
@@ -86,8 +86,8 @@ export interface Schedule extends Struct {
readonly maxStackHeight: u32;
/** u32 */
readonly maxMemoryPages: u32;
/** Bool */
readonly enablePrintln: Bool;
/** bool */
readonly enablePrintln: bool;
/** u32 */
readonly maxSubjectLen: u32;
}
@@ -1,8 +1,8 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Enum, Struct } from '../../codec';
import { AccountId, Bytes, Null, i8, u32, u64 } from '../../primitive';
import { Balance } from '../runtime';
import { Bytes, Null, i8, u32, u64 } from '../../primitive';
import { AccountId, Balance } from '../runtime';
/** Struct */
export interface AccountInfo extends Struct {
@@ -6,6 +6,7 @@ export default {
types: {
ApprovalFlag: 'u32',
SetIndex: 'u32',
Vote: 'GenericVote',
VoteIndex: 'u32',
VoterInfo: {
lastActive: 'VoteIndex',
@@ -1,7 +1,7 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Enum, Struct } from '../../codec';
import { u32 } from '../../primitive';
import { GenericVote, u32 } from '../../primitive';
import { Balance } from '../runtime';
/** u32 */
@@ -10,6 +10,9 @@ export type ApprovalFlag = u32;
/** u32 */
export type SetIndex = u32;
/** GenericVote */
export type Vote = GenericVote;
/** u32 */
export type VoteIndex = u32;
@@ -2,8 +2,8 @@
import { Codec } from '../../types';
import { Enum, Struct, Vec } from '../../codec';
import { AccountId, Bytes, u32, u64 } from '../../primitive';
import { BalanceOf, BlockNumber, Hash, Signature } from '../runtime';
import { Bytes, u32, u64 } from '../../primitive';
import { AccountId, BalanceOf, BlockNumber, Hash, Signature } from '../runtime';
/** Struct */
export interface AttestedCandidate extends Struct {
@@ -4,7 +4,10 @@
export default {
types: {
AccountId: 'GenericAccountId',
AccountIdOf: 'AccountId',
AccountIndex: 'GenericAccountIndex',
Address: 'GenericAddress',
Balance: 'u128',
BalanceOf: 'Balance',
BlockNumber: 'u64',
+10 -1
View File
@@ -2,11 +2,20 @@
import { Codec } from '../../types';
import { Compact, Struct, Vec } from '../../codec';
import { AccountId, Bytes, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive';
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive';
/** GenericAccountId */
export type AccountId = GenericAccountId;
/** AccountId */
export type AccountIdOf = AccountId;
/** GenericAccountIndex */
export type AccountIndex = GenericAccountIndex;
/** GenericAddress */
export type Address = GenericAddress;
/** u128 */
export type Balance = u128;
@@ -1,8 +1,8 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Compact, Enum, Struct, Vec } from '../../codec';
import { AccountId, Moment, u32 } from '../../primitive';
import { Balance, BlockNumber } from '../runtime';
import { Moment, u32 } from '../../primitive';
import { AccountId, Balance, BlockNumber } from '../runtime';
/** u32 */
export type EraIndex = u32;
@@ -10,7 +10,7 @@ import createType from '../../codec/createType';
import Vec from '../../codec/Vec';
import json1 from '../../json/EventRecord.001.json';
import json3 from '../../json/EventRecord.003.json';
import Event from '../../primitive/Event';
import GenericEvent from '../../primitive/Generic/Event';
import Metadata from '../../Metadata';
import metadataV0 from '../../Metadata/v0/static';
import metadata from '../../Metadata/static';
@@ -18,7 +18,7 @@ import metadata from '../../Metadata/static';
describe('EventRecord', (): void => {
describe('EventRecord_0_76', (): void => {
beforeEach((): void => {
Event.injectMetadata(
GenericEvent.injectMetadata(
new Metadata(metadataV0)
);
});
@@ -33,7 +33,7 @@ describe('EventRecord', (): void => {
describe('EventRecord (current)', (): void => {
beforeEach((): void => {
Event.injectMetadata(
GenericEvent.injectMetadata(
new Metadata(metadata)
);
});
@@ -14,6 +14,7 @@ export default {
Finalization: 'Null'
}
},
Event: 'GenericEvent',
EventRecord0to76: {
phase: 'Phase',
event: 'Event'
@@ -1,12 +1,15 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Enum, Struct, Vec } from '../../codec';
import { Bytes, Event, u32, u8 } from '../../primitive';
import { Bytes, GenericEvent, u32, u8 } from '../../primitive';
import { Digest, Hash } from '../runtime';
/** Digest */
export type DigestOf = Digest;
/** GenericEvent */
export type Event = GenericEvent;
/** Vec<u8> */
export type EventId = Vec<u8>;
@@ -1,8 +1,7 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { Struct } from '../../codec';
import { AccountId } from '../../primitive';
import { Balance } from '../runtime';
import { AccountId, Balance } from '../runtime';
/** Struct */
export interface TreasuryProposal extends Struct {
@@ -1,51 +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 { Hash } from '../interfaces/runtime';
import { Phase } from '../interfaces/system';
import { ClassOf } from '../codec/createType';
import Struct from '../codec/Struct';
import Vec from '../codec/Vec';
import Event from './Event';
/**
* @name EventRecord
* @description
* A record for an [[Event]] (as specified by [[Metadata]]) with the specific [[Phase]] of
* application.
*/
export default class EventRecord extends Struct {
public constructor (value: any) {
super({
phase: ClassOf('Phase'),
event: Event,
topics: ClassOf('Vec<Hash>')
}, value);
}
// TODO We want these injected by createType
public static Fallback = ClassOf('EventRecord0to76');
/**
* @description The [[Event]] this record refers to
*/
public get event (): Event {
return this.get('event') as Event;
}
/**
* @description The [[Phase]] where the event was generated
*/
public get phase (): Phase {
return this.get('phase') as Phase;
}
/**
* @description The [[Hash]] topics for this event
*/
public get topics (): Vec<Hash> {
return this.get('topics') as Vec<Hash>;
}
}
@@ -10,7 +10,7 @@ import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/u
import Base from '../../codec/Base';
import Compact from '../../codec/Compact';
import { FunctionMetadata } from '../../Metadata/v7/Calls';
import Address from '../Address';
import Address from '../Generic/Address';
import Method from '../Method';
import ExtrinsicV1, { ExtrinsicValueV1 } from './v1/Extrinsic';
import ExtrinsicV2, { ExtrinsicValueV2 } from './v2/Extrinsic';
@@ -8,7 +8,7 @@ import { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Method';
import Address from '../../Address';
import Address from '../../Generic/Address';
import ExtrinsicSignature from './ExtrinsicSignature';
export interface ExtrinsicValueV1 {
@@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti
import createType, { ClassOf } from '../../../codec/createType';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import Address from '../../Generic/Address';
import Method from '../../Method';
import ExtrinsicEra from '../ExtrinsicEra';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
@@ -106,7 +106,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature {
return this.injectSignature(
new Address(signer),
createType('Address', signer),
createType('Signature', signature),
new SignaturePayload(payload)
);
@@ -116,7 +116,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
* @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 signer = createType('Address', account.publicKey);
const payload = new SignaturePayload({
nonce,
method: method.toU8a(),
@@ -8,7 +8,7 @@ import { isU8a } from '@polkadot/util';
import Struct from '../../../codec/Struct';
import Method from '../../Method';
import Address from '../../Address';
import Address from '../../Generic/Address';
import ExtrinsicSignature from './ExtrinsicSignature';
const TRANSACTION_VERSION = 2;
@@ -8,7 +8,7 @@ import { AnyNumber } from '../../../types';
import { ClassOf } from '../../../codec/createType';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import Address from '../../Generic/Address';
import ExtrinsicEra from '../ExtrinsicEra';
interface ExtrinsicExtraValueV2 {
@@ -8,7 +8,7 @@ import { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, SignatureOpti
import createType, { ClassOf } from '../../../codec/createType';
import Compact from '../../../codec/Compact';
import Struct from '../../../codec/Struct';
import Address from '../../Address';
import Address from '../../Generic/Address';
import Method from '../../Method';
import ExtrinsicEra from '../ExtrinsicEra';
import SignaturePayload from './SignaturePayload';
@@ -110,7 +110,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature {
return this.injectSignature(
new Address(signer),
createType('Address', signer),
createType('Signature', signature),
new SignaturePayload(payload)
);
@@ -120,7 +120,7 @@ export default class ExtrinsicSignatureV2 extends Struct implements IExtrinsicSi
* @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 signer = createType('Address', account.publicKey);
const payload = new SignaturePayload({
blockHash,
era: era || IMMORTAL_ERA,
@@ -2,19 +2,18 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import '../../injector';
import { setAddressPrefix } from '@polkadot/util-crypto';
import createType from '../codec/createType';
import U8a from '../codec/U8a';
import Vec from '../codec/Vec';
import jsonVec from '../json/AccountIdVec.001.json';
import createType from '../../codec/createType';
import U8a from '../../codec/U8a';
import jsonVec from '../../json/AccountIdVec.001.json';
import AccountId from './AccountId';
describe('AccountId', (): void => {
describe('defaults', (): void => {
const id = new AccountId();
const id = createType('AccountId');
it('has a 32-byte length', (): void => {
expect(id).toHaveLength(32);
@@ -32,13 +31,13 @@ describe('AccountId', (): void => {
describe('decoding', (): void => {
const testDecode = (type: string, input: Uint8Array | string | AccountId, expected: string): void =>
it(`can decode from ${type}`, (): void => {
const a = new AccountId(input);
const a = createType('AccountId', input);
expect(a.toString()).toBe(expected);
});
testDecode(
'AccountId',
new AccountId('0x0102030405060708010203040506070801020304050607080102030405060708'),
createType('AccountId', '0x0102030405060708010203040506070801020304050607080102030405060708'),
'5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'
);
testDecode('hex', '0x0102030405060708010203040506070801020304050607080102030405060708', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
@@ -64,7 +63,7 @@ describe('AccountId', (): void => {
describe('encoding', (): void => {
const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string, input: string = '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'): void =>
it(`can encode ${to}`, (): void => {
const a = new AccountId(input);
const a = createType('AccountId', input);
expect(a[to]()).toEqual(expected);
});
@@ -79,7 +78,7 @@ describe('AccountId', (): void => {
]));
it('decodes to a non-empty value', (): void => {
expect(new AccountId('7qT1BvpawNbqb3BZaBTMFMMAKrpJKLPf1LmEHR1JyarWJdMX').isEmpty).toBe(false);
expect(createType('AccountId', '7qT1BvpawNbqb3BZaBTMFMMAKrpJKLPf1LmEHR1JyarWJdMX').isEmpty).toBe(false);
});
});
@@ -88,7 +87,7 @@ describe('AccountId', (): void => {
setAddressPrefix(68);
const data = createType('StorageData', jsonVec.params.result.changes[0][1]);
const list = new Vec(AccountId, data).map((accountId): string => accountId.toString());
const list = createType('Vec<AccountId>', data).map((accountId): string => accountId.toString());
expect(list).toEqual([
'7qVJujLF3EDbZt5WfQXWvueFedMS4Vfk2Hb4GyR8jwksTLup',
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyString, AnyU8a } from '../types';
import { AnyString, AnyU8a } from '../../types';
import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import U8aFixed from '../codec/U8aFixed';
import U8aFixed from '../../codec/U8aFixed';
/**
* @name AccountId
@@ -2,40 +2,44 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import BN from 'bn.js';
import createType from '../../codec/createType';
import AccountIndex from './AccountIndex';
describe('AccountIndex', (): void => {
it('creates a BN representation', (): void => {
expect(
new AccountIndex(new Uint8Array([17, 18, 19, 20])).toNumber()
createType('AccountIndex', new Uint8Array([17, 18, 19, 20])).toNumber()
).toEqual(336794129);
});
it('creates a BN representation (from ss-58)', (): void => {
expect(
new AccountIndex('Mwz15xP2').toNumber()
createType('AccountIndex', 'Mwz15xP2').toNumber()
).toEqual(336794129);
});
it('constructs 2-byte from number', (): void => {
expect(
new AccountIndex(256 * 1).toString()
createType('AccountIndex', 256 * 1).toString()
).toEqual('25GUyv');
});
it('constructs from number', (): void => {
expect(
new AccountIndex(new BN(336794129)).toString()
createType('AccountIndex', new BN(336794129)).toString()
).toEqual('Mwz15xP2');
});
it('compares ss-58 values', (): void => {
expect(new AccountIndex(256 * 1).eq('25GUyv')).toBe(true);
expect(createType('AccountIndex', 256 * 1).eq('25GUyv')).toBe(true);
});
it('compares numbers', (): void => {
expect(new AccountIndex('2jpAFn').eq(256 * 1)).toBe(true);
expect(createType('AccountIndex', '2jpAFn').eq(256 * 1)).toBe(true);
});
describe('calcLength', (): void => {
@@ -2,13 +2,14 @@
// 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 { AnyNumber } from '../../types';
import BN from 'bn.js';
import { bnToBn, isBn, isNumber, isU8a, isHex } from '@polkadot/util';
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
import U32 from './U32';
import createType from '../../codec/createType';
import U32 from '../U32';
export const ENUMSET_SIZE = new BN(64);
@@ -92,7 +93,7 @@ export default class AccountIndex extends U32 {
}
// convert and compare
return super.eq(new AccountIndex(other));
return super.eq(createType('AccountIndex', other));
}
/**
@@ -2,6 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../../injector';
import createType from '../../codec/createType';
import AccountId from './AccountId';
import AccountIndex from './AccountIndex';
import Address from './Address';
@@ -9,7 +12,7 @@ import Address from './Address';
describe('Address', (): void => {
const testDecode = (type: string, input: Address | AccountId | AccountIndex | number[] | Uint8Array, expected: string): void =>
it(`can decode from ${type}`, (): void => {
const a = new Address(input);
const a = createType('Address', input);
expect(a.toString()).toBe(expected);
});
@@ -17,35 +20,35 @@ describe('Address', (): void => {
it('equals on AccountId', (): void => {
const addr = '5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo';
expect(new Address(addr).eq(addr)).toBe(true);
expect(createType('Address', addr).eq(addr)).toBe(true);
});
it('equals on AccountIndex', (): void => {
// see the test below - these are equivalent (with different prefix encoding)
expect(new Address('2jpAFn').eq('25GUyv')).toBe(true);
expect(createType('Address', '2jpAFn').eq('25GUyv')).toBe(true);
});
});
describe('decoding', (): void => {
testDecode(
'Address',
new Address('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
createType('Address', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
'5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'
);
testDecode(
'AccountId',
new AccountId('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
createType('AccountId', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
'5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'
);
testDecode(
'AccountIndex (mixed prefixes)',
new Address('2jpAFn'),
createType('Address', '2jpAFn'),
// NOTE Expected adress here is encoded with prefix 42, input above with 68
'25GUyv'
);
testDecode(
'AccountIndex (hex)',
new AccountIndex('0x0100'),
createType('AccountIndex', '0x0100'),
'25GUyv'
);
testDecode(
@@ -94,7 +97,7 @@ describe('Address', (): void => {
describe('encoding', (): void => {
const testEncode = (to: 'toHex' | 'toString' | 'toU8a', expected: string | Uint8Array): void =>
it(`can encode ${to}`, (): void => {
const a = new Address('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
const a = createType('Address', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
expect(a[to]()).toEqual(expected);
});
@@ -6,7 +6,8 @@ import BN from 'bn.js';
import { hexToU8a, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import Base from '../codec/Base';
import createType from '../../codec/createType';
import Base from '../../codec/Base';
import AccountId from './AccountId';
import AccountIndex from './AccountIndex';
@@ -33,7 +34,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (isBn(value) || isNumber(value)) {
return new AccountIndex(value);
return createType('AccountIndex', value);
} else if (value instanceof Address) {
return value.raw;
} else if (Array.isArray(value)) {
@@ -42,14 +43,14 @@ export default class Address extends Base<AccountId | AccountIndex> {
// This allows us to instantiate an address with a raw publicKey. Do this first before
// we checking the first byte, otherwise we may split an already-existent valid address
if (value.length === 32) {
return new AccountId(value);
return createType('AccountId', value);
} else if (value[0] === 0xff) {
return new AccountId(value.subarray(1));
return createType('AccountId', value.subarray(1));
}
const [offset, length] = AccountIndex.readLength(value);
return new AccountIndex(u8aToBn(value.subarray(offset, offset + length), true));
return createType('AccountIndex', u8aToBn(value.subarray(offset, offset + length), true));
} else if (isHex(value)) {
return Address.decodeAddress(hexToU8a(value));
}
@@ -57,8 +58,8 @@ export default class Address extends Base<AccountId | AccountIndex> {
const decoded = decodeAddress(value);
return decoded.length === 32
? new AccountId(decoded)
: new AccountIndex(u8aToBn(decoded, true));
? createType('AccountId', decoded)
: createType('AccountIndex', u8aToBn(decoded, true));
}
/**
@@ -2,18 +2,18 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventId } from '../interfaces/system';
import { Constructor, Codec } from '../types';
import { EventId } from '../../interfaces/system';
import { Constructor, Codec } from '../../types';
import { assert, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
import { ClassOf, TypeDef, getTypeClass, getTypeDef } from '../codec/createType';
import Struct from '../codec/Struct';
import Tuple from '../codec/Tuple';
import Metadata from '../Metadata';
import { EventMetadata as EventMetadataV7 } from '../Metadata/v7/Events';
import Null from './Null';
import Unconstructable from './Unconstructable';
import { ClassOf, TypeDef, getTypeClass, getTypeDef } from '../../codec/createType';
import Struct from '../../codec/Struct';
import Tuple from '../../codec/Tuple';
import Metadata from '../../Metadata';
import { EventMetadata as EventMetadataV7 } from '../../Metadata/v7/Events';
import Null from '../Null';
import Unconstructable from '../Unconstructable';
const EventTypes: Record<string, Constructor<EventData>> = {};
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import '../injector';
import '../../injector';
import Vote from './Vote';
@@ -2,13 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Conviction } from '../interfaces/democracy';
import { Conviction } from '../../interfaces/democracy';
import { isBoolean, isNumber, isObject, isU8a, isUndefined } from '@polkadot/util';
import createType from '../codec/createType';
import U8a from '../codec/U8a';
import Bool from './Bool';
import createType from '../../codec/createType';
import U8a from '../../codec/U8a';
import Bool from '../Bool';
/**
* @name Vote
@@ -36,7 +36,9 @@ export default class Vote extends U8a {
}
private static decodeVote (value?: any): Uint8Array {
if (isBoolean(value)) {
if (isUndefined(value)) {
return new Uint8Array([0]);
} else if (isBoolean(value)) {
return value
? new Uint8Array([0b10000000])
: new Uint8Array([0b0]);
@@ -53,8 +55,10 @@ export default class Vote extends U8a {
const result = convictionIndex | (aye.eq(true) ? 0b1 << 7 : 0b0);
return new Uint8Array([result]);
} else if (isU8a(value) && value.length > 0) {
return value;
} else if (isU8a(value)) {
return value.length
? value
: new Uint8Array([0]);
}
throw new Error(`Unable to convert input ${value} to Vote`);
@@ -2,6 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as GenericAccountId } from './AccountId';
export { default as GenericAccountIndex } from './AccountIndex';
export { default as GenericAddress } from './Address';
export { default as GenericBlock } from './Block';
export { default as GenericConsensusEngineId } from './ConsensusEngineId';
export { default as GenericEvent } from './Event';
export { default as GenericDigest, DigestItem as GenericDigestItem } from './Digest';
export { default as GenericVote } from './Vote';
+1 -6
View File
@@ -10,11 +10,7 @@
*/
// NOTE We are exporting Bool as bool to match with Rust (however also using Bool internally),
// so with the renamed versions, epose both cases, allowing createType to work
export { default as AccountId } from './AccountId';
export { default as AccountIndex } from './AccountIndex';
export { default as Address } from './Address';
export { default as Event } from './Event';
export { default as Bool, default as bool } from './Bool';
export { default as bool } from './Bool';
export { default as Bytes } from './Bytes';
export { default as Data } from './Data';
export * from './Extrinsic';
@@ -44,4 +40,3 @@ export { default as u64, default as U64 } from './U64';
export { default as u128, default as U128 } from './U128';
export { default as u256, default as U256 } from './U256';
export { default as usize } from './USize';
export { default as Vote } from './Vote';
+1 -1
View File
@@ -10,7 +10,7 @@ import Compact from './codec/Compact';
import U8a from './codec/U8a';
import { FunctionMetadata } from './Metadata/v7/Calls';
import Method from './primitive/Method';
import Address from './primitive/Address';
import Address from './primitive/Generic/Address';
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IKeyringPair {