1 | import { U8aFixed } from '@polkadot/types-codec';
|
2 | import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
3 | import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
4 |
|
5 | function decodeAccountId(value) {
|
6 | if (isU8a(value) || Array.isArray(value)) {
|
7 | return u8aToU8a(value);
|
8 | }
|
9 | else if (!value) {
|
10 | return new Uint8Array();
|
11 | }
|
12 | else if (isHex(value)) {
|
13 | return hexToU8a(value);
|
14 | }
|
15 | else if (isString(value)) {
|
16 | return decodeAddress(value.toString());
|
17 | }
|
18 | throw new Error(`Unknown type passed to AccountId constructor, found typeof ${typeof value}`);
|
19 | }
|
20 | class BaseAccountId extends U8aFixed {
|
21 | constructor(registry, allowedBits = 256 | 264, value) {
|
22 | const decoded = decodeAccountId(value);
|
23 | const decodedBits = decoded.length * 8;
|
24 |
|
25 | if (decodedBits < allowedBits && decoded.some((b) => b)) {
|
26 | throw new Error(`Invalid AccountId provided, expected ${allowedBits >> 3} bytes, found ${decoded.length}`);
|
27 | }
|
28 | super(registry, decoded, allowedBits);
|
29 | }
|
30 | |
31 |
|
32 |
|
33 | eq(other) {
|
34 | return super.eq(decodeAccountId(other));
|
35 | }
|
36 | |
37 |
|
38 |
|
39 | toHuman() {
|
40 | return this.toJSON();
|
41 | }
|
42 | |
43 |
|
44 |
|
45 | toJSON() {
|
46 | return this.toString();
|
47 | }
|
48 | |
49 |
|
50 |
|
51 | toPrimitive() {
|
52 | return this.toJSON();
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | toString() {
|
58 | return encodeAddress(this, this.registry.chainSS58);
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | toRawType() {
|
64 | return 'AccountId';
|
65 | }
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | export class GenericAccountId extends BaseAccountId {
|
75 | constructor(registry, value) {
|
76 | super(registry, 256, value);
|
77 | }
|
78 | }
|
79 | export class GenericAccountId33 extends BaseAccountId {
|
80 | constructor(registry, value) {
|
81 | super(registry, 264, value);
|
82 | }
|
83 | }
|