Adjust Address <-> LookupSource definitions (#2502)

This commit is contained in:
Jaco Greeff
2020-08-22 11:43:19 +02:00
committed by GitHub
parent aec6694028
commit 277ea92fda
9 changed files with 54 additions and 41 deletions
+4 -1
View File
@@ -34,7 +34,7 @@ import { ProxyAnnouncement, ProxyDefinition, ProxyType } from '@polkadot/types/i
import { AccountStatus, AccountValidity } from '@polkadot/types/interfaces/purchase';
import { ActiveRecovery, RecoveryConfig } from '@polkadot/types/interfaces/recovery';
import { RpcMethods } from '@polkadot/types/interfaces/rpc';
import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, CallHash, CallHashOf, ChangesTrieConfiguration, Consensus, ConsensusEngineId, Digest, DigestItem, ExtrinsicsWeight, Fixed128, Fixed64, FixedI128, FixedI64, FixedU128, FixedU64, H160, H256, H512, Hash, Header, I32F32, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, LookupSource, LookupTarget, ModuleId, Moment, OpaqueCall, Origin, Pays, PerU16, Perbill, Percent, Permill, Perquintill, Phantom, PhantomData, PreRuntime, Releases, RuntimeDbWeight, Seal, SealV0, SignedBlock, StorageData, U32F32, ValidatorId, Weight, WeightMultiplier } from '@polkadot/types/interfaces/runtime';
import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, CallHash, CallHashOf, ChangesTrieConfiguration, Consensus, ConsensusEngineId, Digest, DigestItem, ExtrinsicsWeight, Fixed128, Fixed64, FixedI128, FixedI64, FixedU128, FixedU64, GenericAddress, H160, H256, H512, Hash, Header, I32F32, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, LookupSource, LookupTarget, ModuleId, Moment, OpaqueCall, Origin, Pays, PerU16, Perbill, Percent, Permill, Perquintill, Phantom, PhantomData, PreRuntime, Releases, RuntimeDbWeight, Seal, SealV0, SignedBlock, StorageData, U32F32, ValidatorId, Weight, WeightMultiplier } from '@polkadot/types/interfaces/runtime';
import { Period, Priority, SchedulePeriod, SchedulePriority, Scheduled, TaskAddress } from '@polkadot/types/interfaces/scheduler';
import { FullIdentification, IdentificationTuple, Keys, MembershipProof, SessionIndex, SessionKeys1, SessionKeys2, SessionKeys3, SessionKeys4, SessionKeys5, SessionKeys6, ValidatorCount } from '@polkadot/types/interfaces/session';
import { Bid, BidKind, SocietyJudgement, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
@@ -227,6 +227,9 @@ declare module '@polkadot/types/types/registry' {
ExtrinsicsWeight: ExtrinsicsWeight;
'Option<ExtrinsicsWeight>': Option<ExtrinsicsWeight>;
'Vec<ExtrinsicsWeight>': Vec<ExtrinsicsWeight>;
GenericAddress: GenericAddress;
'Option<GenericAddress>': Option<GenericAddress>;
'Vec<GenericAddress>': Vec<GenericAddress>;
H160: H160;
'Option<H160>': Option<H160>;
'Vec<H160>': Vec<H160>;
@@ -5,12 +5,12 @@
import { TypeRegistry } from '../create';
import AccountId from './AccountId';
import AccountIndex from './AccountIndex';
import Address from './Address';
import LookupSource from './LookupSource';
describe('Address', (): void => {
describe('LookupSource', (): void => {
const registry = new TypeRegistry();
const testDecode = (type: string, input: Address | AccountId | AccountIndex | number[] | Uint8Array, expected: string): void =>
const testDecode = (type: string, input: LookupSource | AccountId | AccountIndex | number[] | Uint8Array, expected: string): void =>
it(`can decode from ${type}`, (): void => {
const a = registry.createType('Address', input);
@@ -21,19 +21,19 @@ describe('Address', (): void => {
it('equals on AccountId', (): void => {
const addr = '5DkQbYAExs3M2sZgT1Ec3mKfZnAQCL4Dt9beTCknkCUn5jzo';
expect(registry.createType('Address', addr).eq(addr)).toBe(true);
expect(registry.createType('LookupSource', addr).eq(addr)).toBe(true);
});
it('equals on AccountIndex', (): void => {
// see the test below - these are equivalent (with different prefix encoding)
expect(registry.createType('Address', '2jpAFn').eq('25GUyv')).toBe(true);
expect(registry.createType('LookupSource', '2jpAFn').eq('25GUyv')).toBe(true);
});
});
describe('decoding', (): void => {
testDecode(
'Address',
registry.createType('Address', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
registry.createType('LookupSource', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'),
'5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'
);
testDecode(
@@ -43,7 +43,7 @@ describe('Address', (): void => {
);
testDecode(
'AccountIndex (mixed prefixes)',
registry.createType('Address', '2jpAFn'),
registry.createType('LookupSource', '2jpAFn'),
// NOTE Expected adress here is encoded with prefix 42, input above with 68
'25GUyv'
);
@@ -12,7 +12,7 @@ import Base from '../codec/Base';
import AccountId from './AccountId';
import AccountIndex from './AccountIndex';
type AnyAddress = BN | Address | AccountId | AccountIndex | number[] | Uint8Array | number | string;
type AnyAddress = BN | LookupSource | AccountId | AccountIndex | number[] | Uint8Array | number | string;
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
@@ -41,23 +41,23 @@ function decodeU8a (registry: Registry, value: Uint8Array): AccountId | AccountI
}
/**
* @name Address
* @name LookupSource
* @description
* A wrapper around an AccountId and/or AccountIndex that is encoded with a prefix.
* Since we are dealing with underlying publicKeys (or shorter encoded addresses),
* 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> {
export default class LookupSource extends Base<AccountId | AccountIndex> {
constructor (registry: Registry, value: AnyAddress = new Uint8Array()) {
super(registry, Address._decodeAddress(registry, value));
super(registry, LookupSource._decodeAddress(registry, value));
}
/** @internal */
private static _decodeAddress (registry: Registry, value: AnyAddress): AccountId | AccountIndex {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof Address) {
} else if (value instanceof LookupSource) {
return value._raw;
} else if (isBn(value) || isNumber(value)) {
return registry.createType('AccountIndex', value);
+1 -1
View File
@@ -4,9 +4,9 @@
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 GenericCall } from './Call';
export { default as GenericConsensusEngineId } from './ConsensusEngineId';
export { default as GenericEvent } from './Event';
export { default as GenericLookupSource } from './LookupSource';
export { default as GenericVote } from './Vote';
@@ -62,6 +62,7 @@ export default {
normal: 'Weight',
operational: 'Weight'
},
GenericAddress: 'LookupSource',
H160: '[u8; 20; H160]',
H256: '[u8; 32; H256]',
H512: '[u8; 64; H512]',
@@ -78,7 +79,7 @@ export default {
KeyValue: '(StorageKey, StorageData)',
KeyTypeId: 'u32',
LockIdentifier: '[u8; 8]',
LookupSource: 'Address',
LookupSource: 'GenericLookupSource',
LookupTarget: 'AccountId',
ModuleId: 'LockIdentifier',
Moment: 'u64',
@@ -3,7 +3,7 @@
import { ITuple } from '@polkadot/types/types';
import { Compact, Enum, Int, Struct, U8aFixed, UInt, Vec } from '@polkadot/types/codec';
import { GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId } from '@polkadot/types/generic';
import { GenericAccountId, GenericAccountIndex, GenericBlock, GenericCall, GenericConsensusEngineId, GenericLookupSource } from '@polkadot/types/generic';
import { Bytes, DoNotConstruct, Null, StorageKey, u128, u16, u32, u64, u8 } from '@polkadot/types/primitive';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { Signature } from '@polkadot/types/interfaces/extrinsics';
@@ -103,6 +103,9 @@ export interface FixedU128 extends UInt {}
/** @name FixedU64 */
export interface FixedU64 extends UInt {}
/** @name GenericAddress */
export interface GenericAddress extends LookupSource {}
/** @name H160 */
export interface H160 extends U8aFixed {}
@@ -143,7 +146,7 @@ export interface KeyValue extends ITuple<[StorageKey, StorageData]> {}
export interface LockIdentifier extends U8aFixed {}
/** @name LookupSource */
export interface LookupSource extends Address {}
export interface LookupSource extends GenericLookupSource {}
/** @name LookupTarget */
export interface LookupTarget extends AccountId {}