UNPKG

1.79 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/types authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { U8aFixed } from '@polkadot/types-codec';
4import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util';
5import { ethereumEncode, isEthereumAddress } from '@polkadot/util-crypto';
6/** @internal */
7
8function decodeAccountId(value) {
9 if (isU8a(value) || Array.isArray(value)) {
10 return u8aToU8a(value);
11 } else if (isHex(value) || isEthereumAddress(value.toString())) {
12 return hexToU8a(value.toString());
13 } else if (isString(value)) {
14 return u8aToU8a(value);
15 }
16
17 return value;
18}
19/**
20 * @name GenericEthereumAccountId
21 * @description
22 * A wrapper around an Ethereum-compatible AccountId. Since we are dealing with
23 * underlying addresses (20 bytes in length), we extend from U8aFixed which is
24 * just a Uint8Array wrapper with a fixed length.
25 */
26
27
28export class GenericEthereumAccountId extends U8aFixed {
29 constructor(registry, value = new Uint8Array()) {
30 super(registry, decodeAccountId(value), 160);
31 }
32 /**
33 * @description Compares the value of the input to see if there is a match
34 */
35
36
37 eq(other) {
38 return super.eq(decodeAccountId(other));
39 }
40 /**
41 * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
42 */
43
44
45 toHuman() {
46 return this.toJSON();
47 }
48 /**
49 * @description Converts the Object to JSON, typically used for RPC transfers
50 */
51
52
53 toJSON() {
54 return this.toString();
55 }
56 /**
57 * @description Returns the string representation of the value
58 */
59
60
61 toString() {
62 return ethereumEncode(this);
63 }
64 /**
65 * @description Returns the base runtime type name for this instance
66 */
67
68
69 toRawType() {
70 return 'AccountId';
71 }
72
73}
\No newline at end of file