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