1 | import { AbstractBase } from '@polkadot/types-codec';
|
2 | import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
3 | import { decodeAddress } from '@polkadot/util-crypto';
|
4 | import { GenericAccountId } from './AccountId.js';
|
5 | import { GenericAccountIndex } from './AccountIndex.js';
|
6 | export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
|
7 |
|
8 | function decodeString(registry, value) {
|
9 | const decoded = decodeAddress(value);
|
10 | return decoded.length === 32
|
11 | ? registry.createTypeUnsafe('AccountId', [decoded])
|
12 | : registry.createTypeUnsafe('AccountIndex', [u8aToBn(decoded)]);
|
13 | }
|
14 |
|
15 | function decodeU8a(registry, value) {
|
16 |
|
17 |
|
18 | if (value.length === 32) {
|
19 | return registry.createTypeUnsafe('AccountId', [value]);
|
20 | }
|
21 | else if (value[0] === 0xff) {
|
22 | return registry.createTypeUnsafe('AccountId', [value.subarray(1)]);
|
23 | }
|
24 | const [offset, length] = GenericAccountIndex.readLength(value);
|
25 | return registry.createTypeUnsafe('AccountIndex', [u8aToBn(value.subarray(offset, offset + length))]);
|
26 | }
|
27 |
|
28 | function decodeAddressOrIndex(registry, value) {
|
29 | return value instanceof GenericLookupSource
|
30 | ? value.inner
|
31 | : value instanceof GenericAccountId || value instanceof GenericAccountIndex
|
32 | ? value
|
33 | : isBn(value) || isNumber(value) || isBigInt(value)
|
34 | ? registry.createTypeUnsafe('AccountIndex', [value])
|
35 | : Array.isArray(value) || isHex(value) || isU8a(value)
|
36 | ? decodeU8a(registry, u8aToU8a(value))
|
37 | : decodeString(registry, value);
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | export class GenericLookupSource extends AbstractBase {
|
48 | constructor(registry, value = new Uint8Array()) {
|
49 | super(registry, decodeAddressOrIndex(registry, value));
|
50 | }
|
51 | |
52 |
|
53 |
|
54 | get encodedLength() {
|
55 | const rawLength = this._rawLength;
|
56 | return rawLength + (
|
57 |
|
58 | rawLength > 1
|
59 | ? 1
|
60 | : 0);
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | get _rawLength() {
|
66 | return this.inner instanceof GenericAccountIndex
|
67 | ? GenericAccountIndex.calcLength(this.inner)
|
68 | : this.inner.encodedLength;
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | inspect() {
|
74 | const value = this.inner.toU8a().subarray(0, this._rawLength);
|
75 | return {
|
76 | outer: [
|
77 | new Uint8Array(this.inner instanceof GenericAccountIndex
|
78 | ? GenericAccountIndex.writeLength(value)
|
79 | : ACCOUNT_ID_PREFIX),
|
80 | value
|
81 | ]
|
82 | };
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | toHex() {
|
88 | return u8aToHex(this.toU8a());
|
89 | }
|
90 | |
91 |
|
92 |
|
93 | toRawType() {
|
94 | return 'Address';
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 | toU8a(isBare) {
|
101 | const encoded = this.inner.toU8a().subarray(0, this._rawLength);
|
102 | return isBare
|
103 | ? encoded
|
104 | : u8aConcat(this.inner instanceof GenericAccountIndex
|
105 | ? GenericAccountIndex.writeLength(encoded)
|
106 | : ACCOUNT_ID_PREFIX, encoded);
|
107 | }
|
108 | }
|