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