AccountId33 (#5197)
* Support for ink! v4 metadata * Adjust * Support for AccountId33 * Revert "Adjust" This reverts commit 862e20c3a88de40c6c639b1f51034c296d7f9d16. * Revert "Support for ink! v4 metadata" This reverts commit 07d1fc0dba4c4c2e2a52b349e6d84b046ebbd205. * Adjust * Adjust, keep exports * Adjust test * Adjust * Sizing
This commit is contained in:
@@ -7,7 +7,6 @@ import { Raw } from '@polkadot/types-codec';
|
||||
import jsonVec from '@polkadot/types-support/json/AccountIdVec.001.json' assert { type: 'json' };
|
||||
|
||||
import { TypeRegistry } from '../create';
|
||||
import { GenericAccountId as AccountId } from '.';
|
||||
|
||||
describe('AccountId', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -37,7 +36,7 @@ describe('AccountId', (): void => {
|
||||
});
|
||||
|
||||
describe('decoding', (): void => {
|
||||
const testDecode = (type: string, input: Uint8Array | string | AccountId, expected: string): void =>
|
||||
const testDecode = (type: string, input: Uint8Array | string, expected: string): void =>
|
||||
it(`can decode from ${type}`, (): void => {
|
||||
expect(
|
||||
registry
|
||||
@@ -57,6 +56,11 @@ describe('AccountId', (): void => {
|
||||
registry.createType('AccountId', '0x0102030405060708010203040506070801020304050607080102030405060708'),
|
||||
'5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'
|
||||
);
|
||||
testDecode(
|
||||
'AccountId33',
|
||||
registry.createType('AccountId33', '0x098765430987654309876543098765430987654309876543098765430987654309'),
|
||||
'5CHCWUYMmDGeJjiuaQ1LnrsAWacDhiTAV6vCfytSxoqBdCCb'
|
||||
);
|
||||
testDecode('hex', '0x0102030405060708010203040506070801020304050607080102030405060708', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
|
||||
testDecode('string', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF');
|
||||
testDecode(
|
||||
|
||||
@@ -22,20 +22,14 @@ function decodeAccountId (value?: AnyU8a | AnyString): Uint8Array {
|
||||
throw new Error(`Unknown type passed to AccountId constructor, found typeof ${typeof value}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name GenericAccountId
|
||||
* @description
|
||||
* A wrapper around an AccountId/PublicKey representation. Since we are dealing with
|
||||
* underlying PublicKeys (32 bytes in length), we extend from U8aFixed which is
|
||||
* just a Uint8Array wrapper with a fixed length.
|
||||
*/
|
||||
export class GenericAccountId extends U8aFixed {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
class BaseAccountId extends U8aFixed {
|
||||
constructor (registry: Registry, allowedBits = 256 | 264, value?: AnyU8a) {
|
||||
const decoded = decodeAccountId(value);
|
||||
const decodedBits = decoded.length * 8;
|
||||
|
||||
// Part of stream containing >= 32 bytes or a all empty (defaults)
|
||||
if (decoded.length < 32 && decoded.some((b) => b)) {
|
||||
throw new Error(`Invalid AccountId provided, expected 32 bytes, found ${decoded.length}`);
|
||||
if (decodedBits < allowedBits && decoded.some((b) => b)) {
|
||||
throw new Error(`Invalid AccountId provided, expected ${allowedBits >> 3} bytes, found ${decoded.length}`);
|
||||
}
|
||||
|
||||
super(registry, decoded, 256);
|
||||
@@ -83,3 +77,22 @@ export class GenericAccountId extends U8aFixed {
|
||||
return 'AccountId';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name GenericAccountId
|
||||
* @description
|
||||
* A wrapper around an AccountId/PublicKey representation. Since we are dealing with
|
||||
* underlying PublicKeys (32 bytes in length), we extend from U8aFixed which is
|
||||
* just a Uint8Array wrapper with a fixed length.
|
||||
*/
|
||||
export class GenericAccountId extends BaseAccountId {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, 256, value);
|
||||
}
|
||||
}
|
||||
|
||||
export class GenericAccountId33 extends BaseAccountId {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, 264, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
export * from '../ethereum';
|
||||
|
||||
export { GenericAccountId } from './AccountId';
|
||||
export { GenericAccountId, GenericAccountId as GenericAccountId32, GenericAccountId33 } from './AccountId';
|
||||
export { GenericAccountIndex } from './AccountIndex';
|
||||
export { GenericBlock } from './Block';
|
||||
export { GenericCall } from './Call';
|
||||
|
||||
@@ -53,7 +53,8 @@ export default {
|
||||
types: objectSpread({}, numberTypes, {
|
||||
AccountId: 'AccountId32',
|
||||
AccountId20: 'GenericEthereumAccountId',
|
||||
AccountId32: 'GenericAccountId',
|
||||
AccountId32: 'GenericAccountId32',
|
||||
AccountId33: 'GenericAccountId33',
|
||||
AccountIdOf: 'AccountId',
|
||||
AccountIndex: 'GenericAccountIndex',
|
||||
Address: 'MultiAddress',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
|
||||
/* eslint-disable */
|
||||
|
||||
import type { GenericAccountId, GenericAccountIndex, GenericBlock, GenericCall, GenericConsensusEngineId, GenericEthereumAccountId, GenericLookupSource, GenericMultiAddress, StorageKey } from '@polkadot/types';
|
||||
import type { GenericAccountId32, GenericAccountId33, GenericAccountIndex, GenericBlock, GenericCall, GenericConsensusEngineId, GenericEthereumAccountId, GenericLookupSource, GenericMultiAddress, StorageKey } from '@polkadot/types';
|
||||
import type { Bytes, Compact, DoNotConstruct, Enum, Int, Null, Option, Struct, U8aFixed, UInt, Vec, u16, u32, u64, u8 } from '@polkadot/types-codec';
|
||||
import type { ITuple } from '@polkadot/types-codec/types';
|
||||
import type { AuthorityId } from '@polkadot/types/interfaces/consensus';
|
||||
@@ -15,7 +15,10 @@ export interface AccountId extends AccountId32 {}
|
||||
export interface AccountId20 extends GenericEthereumAccountId {}
|
||||
|
||||
/** @name AccountId32 */
|
||||
export interface AccountId32 extends GenericAccountId {}
|
||||
export interface AccountId32 extends GenericAccountId32 {}
|
||||
|
||||
/** @name AccountId33 */
|
||||
export interface AccountId33 extends GenericAccountId33 {}
|
||||
|
||||
/** @name AccountIdOf */
|
||||
export interface AccountIdOf extends AccountId {}
|
||||
|
||||
Reference in New Issue
Block a user