/
address.ts
255 lines (225 loc) · 6.58 KB
/
address.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { FuelError } from '@fuel-ts/errors';
import { AbstractAddress } from '@fuel-ts/interfaces';
import type { Bech32Address, B256Address, EvmAddress, AssetId } from '@fuel-ts/interfaces';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { sha256 } from '@noble/hashes/sha256';
import {
normalizeBech32,
isBech32,
toB256,
getBytesFromBech32,
toBech32,
getRandomB256,
isPublicKey,
isB256,
clearFirst12BytesFromB256,
isEvmAddress,
padFirst12BytesOfEvmAddress,
} from './utils';
/**
* `Address` provides a type safe wrapper for converting between different address formats
* ands comparing them for equality.
*/
export default class Address extends AbstractAddress {
// #region address-2
readonly bech32Address: Bech32Address;
// #endregion address-2
/**
* @param address - A Bech32 address
*/
constructor(address: Bech32Address) {
super();
this.bech32Address = normalizeBech32(address);
if (!isBech32(this.bech32Address)) {
throw new FuelError(
FuelError.CODES.INVALID_BECH32_ADDRESS,
`Invalid Bech32 Address: ${address}.`
);
}
}
/**
* Returns the `bech32Address` property
*
* @returns The `bech32Address` property
*/
toAddress(): Bech32Address {
return this.bech32Address;
}
/**
* Converts and returns the `bech32Address` property to a 256 bit hash string
*
* @returns The `bech32Address` property as a 256 bit hash string
*/
toB256(): B256Address {
return toB256(this.bech32Address);
}
/**
* Converts and returns the `bech32Address` property to a byte array
*
* @returns The `bech32Address` property as a byte array
*/
toBytes(): Uint8Array {
return getBytesFromBech32(this.bech32Address);
}
/**
* Converts
*
* @returns The `bech32Address` property as a 256 bit hash string
*/
toHexString(): B256Address {
return this.toB256();
}
/**
* Converts and returns the `bech32Address` property as a string
*
* @returns The `bech32Address` property as a string
*/
toString(): string {
return this.bech32Address;
}
/**
* Converts and returns the `bech32Address` property as a string
*
* @returns The `bech32Address` property as a string
*/
toJSON(): string {
return this.bech32Address;
}
/**
* Clears the first 12 bytes of the `bech32Address` property and returns it as a `EvmAddress`
*
* @returns The `bech32Address` property as an {@link EvmAddress | `EvmAddress`}
*/
toEvmAddress(): EvmAddress {
const b256Address = toB256(this.bech32Address);
return {
value: clearFirst12BytesFromB256(b256Address),
} as EvmAddress;
}
/**
* Wraps the `bech32Address` property and returns as an `AssetId`.
*
* @returns The `bech32Address` property as an {@link AssetId | `AssetId`}
*/
toAssetId(): AssetId {
return {
value: this.toB256(),
} as AssetId;
}
/**
* Returns the value of the `bech32Address` property
*
* @returns The value of `bech32Address` property
*/
valueOf(): string {
return this.bech32Address;
}
/**
* Compares this the `bech32Address` property to another for direct equality
*
* @param other - Another address to compare against
* @returns The equality of the comparison
*/
equals(other: Address): boolean {
return this.bech32Address === other.bech32Address;
}
/**
* Takes a Public Key, hashes it, and creates an `Address`
*
* @param publicKey - A wallets public key
* @returns A new `Address` instance
*/
static fromPublicKey(publicKey: string): Address {
if (!isPublicKey(publicKey)) {
throw new FuelError(FuelError.CODES.INVALID_PUBLIC_KEY, `Invalid Public Key: ${publicKey}.`);
}
const b256Address = hexlify(sha256(arrayify(publicKey)));
return new Address(toBech32(b256Address));
}
/**
* Takes a B256 Address and creates an `Address`
*
* @param b256Address - A b256 hash
* @returns A new `Address` instance
*/
static fromB256(b256Address: string): Address {
if (!isB256(b256Address)) {
throw new FuelError(
FuelError.CODES.INVALID_B256_ADDRESS,
`Invalid B256 Address: ${b256Address}.`
);
}
return new Address(toBech32(b256Address));
}
/**
* Creates an `Address` with a randomized `bech32Address` property
*
* @returns A new `Address` instance
*/
static fromRandom(): Address {
return this.fromB256(getRandomB256());
}
/**
* Takes an ambiguous string and attempts to create an `Address`
*
* @param address - An ambiguous string
* @returns A new `Address` instance
*/
static fromString(address: string): Address {
return isBech32(address) ? new Address(address as Bech32Address) : this.fromB256(address);
}
/**
* Takes an ambiguous string or address and creates an `Address`
*
* @returns a new `Address` instance
*/
static fromAddressOrString(address: string | AbstractAddress): AbstractAddress {
return typeof address === 'string' ? this.fromString(address) : address;
}
/**
* Takes a dynamic string or `AbstractAddress` and creates an `Address`
*
* @param addressId - A string containing Bech32, B256, or Public Key
* @throws Error - Unknown address if the format is not recognised
* @returns A new `Address` instance
*/
static fromDynamicInput(address: string | AbstractAddress): Address {
// If address is a object than we assume it's a AbstractAddress
// we don't check by instanceof because it's possible to
// the host app to have a different reference to this same class type
if (typeof address !== 'string' && 'toB256' in address) {
return Address.fromB256(address.toB256());
}
if (isPublicKey(address)) {
return Address.fromPublicKey(address);
}
if (isBech32(address)) {
return new Address(address as Bech32Address);
}
if (isB256(address)) {
return Address.fromB256(address);
}
if (isEvmAddress(address)) {
return Address.fromEvmAddress(address);
}
throw new FuelError(
FuelError.CODES.PARSE_FAILED,
`Unknown address format: only 'Bech32', 'B256', or 'Public Key (512)' are supported.`
);
}
/**
* Takes an Evm Address and returns back an `Address`
*
* @returns A new `Address` instance
*/
static fromEvmAddress(evmAddress: string): Address {
if (!isEvmAddress(evmAddress)) {
throw new FuelError(
FuelError.CODES.INVALID_EVM_ADDRESS,
`Invalid Evm Address: ${evmAddress}.`
);
}
const paddedAddress = padFirst12BytesOfEvmAddress(evmAddress);
return new Address(toBech32(paddedAddress));
}
}