Ethereum-compatible AccountId/Address (#2501)

* Ethereum-compatible AccountId/Address

* With tests

* Adjust definitions

* Update packages/types/src/ethereum/LookupSource.ts

* Renames
This commit is contained in:
Jaco Greeff
2020-08-22 12:30:24 +02:00
committed by GitHub
parent e0626c3dad
commit 68856e9692
13 changed files with 441 additions and 1 deletions
+7
View File
@@ -18,6 +18,7 @@ import { AliveContractInfo, CodeHash, ContractCallRequest, ContractExecResult, C
import { AccountVote, AccountVoteSplit, AccountVoteStandard, Conviction, Delegations, PreimageStatus, PreimageStatusAvailable, PriorLock, PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo, ReferendumInfoFinished, ReferendumInfoTo239, ReferendumStatus, Tally, Voting, VotingDelegating, VotingDirect, VotingDirectVote } from '@polkadot/types/interfaces/democracy';
import { ApprovalFlag, DefunctVoter, Renouncing, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from '@polkadot/types/interfaces/elections';
import { CreatedBlock, ImportedAux } from '@polkadot/types/interfaces/engine';
import { EthereumAccountId, EthereumLookupSource } from '@polkadot/types/interfaces/ethereum';
import { Account, Log, Vicinity } from '@polkadot/types/interfaces/evm';
import { EcdsaSignature, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicPayloadV4, ExtrinsicSignatureV1, ExtrinsicSignatureV2, ExtrinsicSignatureV3, ExtrinsicSignatureV4, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, ExtrinsicV4, ImmortalEra, MortalEra, MultiSignature, Signature, SignerPayload, Sr25519Signature } from '@polkadot/types/interfaces/extrinsics';
import { AssetOptions, Owner, PermissionLatest, PermissionVersions, PermissionsV1 } from '@polkadot/types/interfaces/genericAsset';
@@ -1346,6 +1347,12 @@ declare module '@polkadot/types/types/registry' {
AccountValidity: AccountValidity;
'Option<AccountValidity>': Option<AccountValidity>;
'Vec<AccountValidity>': Vec<AccountValidity>;
EthereumAccountId: EthereumAccountId;
'Option<EthereumAccountId>': Option<EthereumAccountId>;
'Vec<EthereumAccountId>': Vec<EthereumAccountId>;
EthereumLookupSource: EthereumLookupSource;
'Option<EthereumLookupSource>': Option<EthereumLookupSource>;
'Vec<EthereumLookupSource>': Vec<EthereumLookupSource>;
CallMetadataV0: CallMetadataV0;
'Option<CallMetadataV0>': Option<CallMetadataV0>;
'Vec<CallMetadataV0>': Vec<CallMetadataV0>;
@@ -0,0 +1,83 @@
// Copyright 2017-2020 @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.
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { TypeRegistry } from '../create';
import Raw from '../codec/Raw';
import AccountId from './AccountId';
describe('EthereumAccountId', (): void => {
const registry = new TypeRegistry();
describe('defaults', (): void => {
const id = registry.createType('EthereumAccountId');
it('has a 20-byte length', (): void => {
expect(id).toHaveLength(20);
});
it('is empty by default', (): void => {
expect(id.isEmpty).toBe(true);
});
it('equals the empty address', (): void => {
expect(id.eq('0x0000000000000000000000000000000000000000')).toBe(true);
});
});
describe('decoding', (): void => {
const testDecode = (type: string, input: Uint8Array | string | AccountId, expected: string): void =>
it(`can decode from ${type}`, (): void => {
const a = registry.createType('EthereumAccountId', input);
expect(a.toString()).toBe(expected);
});
testDecode(
'AccountId',
registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'),
'0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'
);
testDecode('hex', '0x4119b2e6c3cb618f4f0B93ac77f9Beec7ff02887', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
testDecode(
'Raw',
new Raw(registry, [
1, 2, 3, 4, 5, 6, 7, 8, 1, 2,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2
]),
'0x0102030405060708010201020304050607080102'
);
testDecode(
'Uint8Array',
Uint8Array.from([
1, 2, 3, 4, 5, 6, 7, 8, 1, 2,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2
]),
'0x0102030405060708010201020304050607080102'
);
});
describe('encoding', (): void => {
const testEncode = (to: 'toHex' | 'toJSON' | 'toString' | 'toU8a', expected: Uint8Array | string, input = '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'): void =>
it(`can encode ${to}`, (): void => {
const a = registry.createType('EthereumAccountId', input);
expect(a[to]()).toEqual(expected);
});
testEncode('toHex', '0x4119b2e6c3cb618f4f0b93ac77f9beec7ff02887');
testEncode('toJSON', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
testEncode('toString', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
testEncode('toString', '0x0000000000000000000000000000000000000000', '0x00');
testEncode('toU8a', Uint8Array.from([
0x41, 0x19, 0xb2, 0xe6, 0xc3, 0xcb, 0x61, 0x8f, 0x4f, 0x0b,
0x93, 0xac, 0x77, 0xf9, 0xbe, 0xec, 0x7f, 0xf0, 0x28, 0x87
]));
it('decodes to a non-empty value', (): void => {
expect(registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887').isEmpty).toBe(false);
});
});
});
+75
View File
@@ -0,0 +1,75 @@
// Copyright 2017-2020 @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 { AnyString, AnyU8a, Registry } from '../types';
import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { ethereumEncode, isEthereumAddress } from '@polkadot/util-crypto';
import U8aFixed from '../codec/U8aFixed';
/** @internal */
function decodeAccountId (value: AnyU8a | AnyString): AnyU8a {
if (isU8a(value) || Array.isArray(value)) {
return u8aToU8a(value);
} else if (isHex(value) || isEthereumAddress(value)) {
return hexToU8a(value.toString());
} else if (isString(value)) {
return u8aToU8a((value as string).toString());
}
return value;
}
/**
* @name EthereumAccountId
* @description
* A wrapper around an Ethereum-compatible AccountId. Since we are dealing with
* underlying addresses (20 bytes in length), we extend from U8aFixed which is
* just a Uint8Array wrapper with a fixed length.
*/
export default class AccountId extends U8aFixed {
constructor (registry: Registry, value: AnyU8a = new Uint8Array()) {
super(registry, decodeAccountId(value), 160);
}
public static encode (value: Uint8Array): string {
return ethereumEncode(value);
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: unknown): boolean {
return super.eq(decodeAccountId(other as AnyU8a));
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (): string {
return this.toJSON();
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.toString();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return AccountId.encode(this);
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return 'AccountId';
}
}
@@ -0,0 +1,106 @@
// Copyright 2017-2020 @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 { TypeRegistry } from '../create';
import AccountIndex from '../generic/AccountIndex';
import AccountId from './AccountId';
import Address from './LookupSource';
describe('EthereumLookupSource', (): void => {
const registry = new TypeRegistry();
const testDecode = (type: string, input: Address | AccountId | AccountIndex | number[] | Uint8Array, expected: string): void =>
it(`can decode from ${type}`, (): void => {
const a = registry.createType('EthereumLookupSource', input);
expect(a.toString()).toBe(expected);
});
describe('decoding', (): void => {
testDecode(
'Address',
registry.createType('EthereumLookupSource', '0x00a329c0648769a73afac7f9381e08fb43dbea72'),
'0x00a329c0648769A73afAc7F9381E08FB43dBEA72'
);
testDecode(
'AccountId',
registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'),
'0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'
);
testDecode(
'AccountIndex (mixed prefixes)',
registry.createType('EthereumLookupSource', '2jpAFn'),
// NOTE Expected address here is encoded with prefix 42, input above with 68
'25GUyv'
);
testDecode(
'AccountIndex (hex)',
registry.createType('AccountIndex', '0x0100'),
'25GUyv'
);
testDecode(
'Uint8Array (with prefix 255)',
Uint8Array.from([
255,
0x41, 0x19, 0xb2, 0xe6, 0xc3, 0xcb, 0x61, 0x8f, 0x4f, 0x0b,
0x93, 0xac, 0x77, 0xf9, 0xbe, 0xec, 0x7f, 0xf0, 0x28, 0x87
]),
'0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'
);
testDecode(
'Uint8Array (with prefix 1 byte)',
Uint8Array.from([1]),
'F7NZ'
);
testDecode(
'Uint8Array (with prefix 2 bytes)',
Uint8Array.from([0xfc, 0, 1]),
'25GUyv'
);
testDecode(
'Uint8Array (with prefix 4 bytes)',
Uint8Array.from([0xfd, 17, 18, 19, 20]),
'Mwz15xP2'
);
});
describe('encoding', (): void => {
const testEncode = (to: 'toHex' | 'toString' | 'toU8a', expected: string | Uint8Array): void =>
it(`can encode ${to}`, (): void => {
const a = registry.createType('EthereumLookupSource', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
expect(a[to]()).toEqual(expected);
});
testEncode(
'toHex',
'0xff4119b2e6c3cb618f4f0b93ac77f9beec7ff02887'
);
testEncode(
'toString',
'0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'
);
testEncode(
'toU8a',
Uint8Array.from([
255,
0x41, 0x19, 0xb2, 0xe6, 0xc3, 0xcb, 0x61, 0x8f, 0x4f, 0x0b,
0x93, 0xac, 0x77, 0xf9, 0xbe, 0xec, 0x7f, 0xf0, 0x28, 0x87
])
);
});
describe('utility', (): void => {
it('equals on AccountId', (): void => {
const addr = '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887';
expect(registry.createType('EthereumLookupSource', addr).eq(addr)).toBe(true);
});
it('equals on AccountIndex', (): void => {
// see the test below - these are equivalent (with different prefix encoding)
expect(registry.createType('EthereumLookupSource', '2jpAFn').eq('25GUyv')).toBe(true);
});
});
});
+124
View File
@@ -0,0 +1,124 @@
// Copyright 2017-2020 @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 { Registry } from '../types';
import BN from 'bn.js';
import { isBn, isHex, isNumber, isU8a, u8aConcat, u8aToHex, u8aToU8a, u8aToBn } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import Base from '../codec/Base';
import AccountIndex from '../generic/AccountIndex';
import EthereumAccountId from './AccountId';
type AnyAddress = BN | LookupSource | EthereumAccountId | AccountIndex | number[] | Uint8Array | number | string;
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
/** @internal */
function decodeString (registry: Registry, value: string): EthereumAccountId | AccountIndex {
const decoded = decodeAddress(value);
return decoded.length === 20
? registry.createType('EthereumAccountId', decoded)
: registry.createType('AccountIndex', u8aToBn(decoded, true));
}
/** @internal */
function decodeU8a (registry: Registry, value: Uint8Array): EthereumAccountId | 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 === 20) {
return registry.createType('EthereumAccountId', value);
} else if (value[0] === 0xff) {
return registry.createType('EthereumAccountId', value.subarray(1));
}
const [offset, length] = AccountIndex.readLength(value);
return registry.createType('AccountIndex', u8aToBn(value.subarray(offset, offset + length), true));
}
/**
* @name LookupSource
* @description
* A wrapper around an EthereumAccountId 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 LookupSource extends Base<EthereumAccountId | AccountIndex> {
constructor (registry: Registry, value: AnyAddress = new Uint8Array()) {
super(registry, LookupSource._decodeAddress(registry, value));
}
/** @internal */
private static _decodeAddress (registry: Registry, value: AnyAddress): EthereumAccountId | AccountIndex {
if (value instanceof EthereumAccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof LookupSource) {
return value._raw;
} else if (isBn(value) || isNumber(value)) {
return registry.createType('AccountIndex', value);
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
return decodeU8a(registry, u8aToU8a(value));
}
return decodeString(registry, value);
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
const rawLength = this._rawLength;
return rawLength + (
// for 1 byte AccountIndexes, we are not adding a specific prefix
rawLength > 1
? 1
: 0
);
}
/**
* @description The length of the raw value, either AccountIndex or AccountId
*/
protected get _rawLength (): number {
return this._raw instanceof AccountIndex
? AccountIndex.calcLength(this._raw)
: this._raw.encodedLength;
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (): string {
return u8aToHex(this.toU8a());
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return 'Address';
}
/**
* @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 = this._raw.toU8a().subarray(0, this._rawLength);
return isBare
? encoded
: u8aConcat(
this._raw instanceof AccountIndex
? AccountIndex.writeLength(encoded)
: ACCOUNT_ID_PREFIX,
encoded
);
}
}
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2020 @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 { default as GenericEthereumAccountId } from './AccountId';
export { default as GenericEthereumLookupSource } from './LookupSource';
+2
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from '../ethereum';
export { default as GenericAccountId } from './AccountId';
export { default as GenericAccountIndex } from './AccountIndex';
export { default as GenericBlock } from './Block';
@@ -43,6 +43,9 @@ export { default as parachains } from './parachains/definitions';
export { default as poll } from './poll/definitions';
export { default as purchase } from './purchase/definitions';
// other useful types
export { default as ethereum } from './ethereum/definitions';
// pull in metadata & rpc last, assuming that is uses info from above
export { default as metadata } from './metadata/definitions';
export { default as rpc } from './rpc/definitions';
@@ -0,0 +1,16 @@
// Copyright 2017-2020 @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.
// order important in structs... :)
/* eslint-disable sort-keys */
import { Definitions } from '../../types';
export default {
rpc: {},
types: {
EthereumAccountId: 'GenericEthereumAccountId',
EthereumLookupSource: 'GenericEthereumLookupSource'
}
} as Definitions;
@@ -0,0 +1,4 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
export * from './types';
@@ -0,0 +1,12 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { GenericEthereumAccountId, GenericEthereumLookupSource } from '@polkadot/types/generic';
/** @name EthereumAccountId */
export interface EthereumAccountId extends GenericEthereumAccountId {}
/** @name EthereumLookupSource */
export interface EthereumLookupSource extends GenericEthereumLookupSource {}
export type PHANTOM_ETHEREUM = 'ethereum';
+1
View File
@@ -36,6 +36,7 @@ export * from './claims/types';
export * from './parachains/types';
export * from './poll/types';
export * from './purchase/types';
export * from './ethereum/types';
export * from './metadata/types';
export * from './rpc/types';
export * from './author/types';