@layerzerolabs/chain-utils 0.0.66 → 0.0.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { AccAddress } from '@initia/initia.js';
2
+ import { StrKey } from '@stellar/stellar-sdk';
2
3
  import type { Address } from '@ton/ton';
3
4
  import bs58 from 'bs58';
4
5
 
@@ -21,10 +22,12 @@ import type {
21
22
  InitiaString,
22
23
  NativeAddress,
23
24
  ResolvedEncodingForChainName,
25
+ StellarString,
24
26
  } from '@layerzerolabs/layerzero-definitions';
25
27
  import type { ChainNativeAddress } from '@layerzerolabs/layerzero-definitions';
26
28
  import { ChainName } from '@layerzerolabs/layerzero-definitions';
27
29
  import { StaticChainConfigs } from '@layerzerolabs/static-chain-info';
30
+ import type { MaybeUndefinedMapped } from '@layerzerolabs/typescript-utils';
28
31
 
29
32
  type NativeOrString<T extends ChainName> = ChainNativeAddress<T> | ResolvedEncodingForChainName<T>;
30
33
 
@@ -33,6 +36,7 @@ export enum AddressEncoding {
33
36
  BASE58 = 'base58',
34
37
  TON = 'ton',
35
38
  INITIA = 'initia',
39
+ STELLAR = 'stellar',
36
40
  }
37
41
 
38
42
  export type ExtensionByChainType = {
@@ -47,21 +51,35 @@ export type InferExtensionByChainName<T extends ChainName> =
47
51
  : {};
48
52
 
49
53
  export type AddressParser<T extends ChainName> = {
50
- normalizedToNative: (address: NormalizedHexString) => ChainNativeAddress<T>;
51
- normalizedToNativeString: (address: NormalizedHexString) => ResolvedEncodingForChainName<T>;
52
- nativeToNormalized: (nativeAddress: NativeOrString<T>) => NormalizedHexString;
53
- nativeToBytes: (nativeAddress: NativeOrString<T>) => Uint8Array;
54
- nativeToBytes32Hex: (nativeAddress: NativeOrString<T>) => HexString;
55
- nativeToBytes32: (nativeAddress: NativeOrString<T>) => Uint8Array;
56
- validateNative: (nativeAddress: string) => nativeAddress is ResolvedEncodingForChainName<T>;
54
+ normalizedToNative: <Address extends NormalizedHexString | undefined>(
55
+ address: Address,
56
+ ) => MaybeUndefinedMapped<Address, ChainNativeAddress<T>>;
57
+ normalizedToNativeString: <Address extends NormalizedHexString | undefined>(
58
+ address: Address,
59
+ ) => MaybeUndefinedMapped<Address, ResolvedEncodingForChainName<T>>;
60
+ nativeToNormalized: <Address extends NativeOrString<T> | undefined>(
61
+ nativeAddress: Address,
62
+ ) => MaybeUndefinedMapped<Address, NormalizedHexString>;
63
+ nativeToBytes: <Address extends NativeOrString<T> | undefined>(
64
+ nativeAddress: Address,
65
+ ) => MaybeUndefinedMapped<Address, Uint8Array>;
66
+ nativeToBytes32Hex: <Address extends NativeOrString<T> | undefined>(
67
+ nativeAddress: Address,
68
+ ) => MaybeUndefinedMapped<Address, HexString>;
69
+ nativeToBytes32: <Address extends NativeOrString<T> | undefined>(
70
+ nativeAddress: Address,
71
+ ) => MaybeUndefinedMapped<Address, Uint8Array>;
72
+ validateNative: (
73
+ nativeAddress: string | undefined,
74
+ ) => nativeAddress is ResolvedEncodingForChainName<T>;
57
75
  /**
58
76
  * Parse a native address to a string if it is valid.
59
77
  * Throws an error if the native address is invalid.
60
78
  * @param nativeAddress - The native address to parse.
61
79
  * @returns The parsed native address.
62
80
  */
63
- parseNative: (nativeAddress: string) => NormalizedHexString;
64
- parseNativeToHex: (nativeAddress: string) => HexString;
81
+ parseNative: (nativeAddress: string | undefined) => NormalizedHexString;
82
+ parseNativeToUnpaddedHex: (nativeAddress: string | undefined) => HexString;
65
83
  encoding: AddressEncoding;
66
84
  } & InferExtensionByChainName<T>;
67
85
 
@@ -73,6 +91,10 @@ export function addressParser<T extends ChainName>(chainName: T): AddressParser<
73
91
  return base58AddressParser(chainName) as unknown as AddressParser<T>;
74
92
  if (chainName === ChainName.INITIA)
75
93
  return initiaAddressParser(chainName) as unknown as AddressParser<T>;
94
+ if (chainName === ChainName.STELLAR)
95
+ return stellarAddressParser(chainName) as unknown as AddressParser<T>;
96
+ if (chainName === ChainName.STARKNET)
97
+ return starknetAddressParser(chainName) as unknown as AddressParser<T>;
76
98
  return hexAddressParser(chainName) as unknown as AddressParser<T>;
77
99
  }
78
100
 
@@ -80,34 +102,39 @@ export namespace addressParser {
80
102
  export const normalizedToBytes32Hex = (address: NormalizedHexString) => {
81
103
  return hexZeroPad(address as unknown as HexString, 32);
82
104
  };
83
- export const normalizedToUnpaddedString = (address: NormalizedHexString) => {
84
- return address.toString() as HexString;
105
+
106
+ export const normalizedToAlignedString = (address: NormalizedHexString) => {
107
+ const hex = address.toString();
108
+ // Unpadded but aligned to a byte length, so its transformable to a byte array
109
+ return (hex.length % 2 === 0 ? hex : `0x0${hex.slice(2)}`) as HexString;
85
110
  };
111
+
86
112
  export const normalizedToBytes32 = (address: NormalizedHexString) => {
87
113
  return hexToBytes(normalizedToBytes32Hex(address));
88
114
  };
115
+
89
116
  export const normalizedToBigInt = (address: NormalizedHexString) => {
90
117
  return BigInt(address as unknown as HexString);
91
118
  };
119
+
92
120
  export const parseNative = (nativeAddress: NativeAddress) => {
93
121
  return addressParser(nativeAddress.chainName).parseNative(nativeAddress.nativeAddress);
94
122
  };
95
123
  }
96
124
 
97
- const validateNative = <T extends ChainName>(
98
- nativeAddress: string,
125
+ function assertNative<T extends ChainName>(
126
+ nativeAddress: string | undefined,
99
127
  expectedChainName: T,
100
- ): nativeAddress is ResolvedEncodingForChainName<T> => {
128
+ ): asserts nativeAddress is ResolvedEncodingForChainName<T> {
101
129
  if (!addressParser(expectedChainName).validateNative(nativeAddress)) {
102
130
  throw new Error(
103
- 'Cannot convert: ' +
131
+ 'Cannot convert: "' +
104
132
  nativeAddress +
105
- ' - The native address does not match the expected format for the chain: ' +
133
+ '" - The native address does not match the expected format for the chain: ' +
106
134
  expectedChainName,
107
135
  );
108
136
  }
109
- return true;
110
- };
137
+ }
111
138
 
112
139
  const getAddress = <T extends ChainName>(nativeAddress: NativeOrString<T>) => {
113
140
  return typeof nativeAddress === 'object' ? nativeAddress.nativeAddress : nativeAddress;
@@ -118,6 +145,9 @@ export const tonAddressParser: (
118
145
  ) => AddressParser<InferChainNamesForChainType<ChainType.TON>> = (chainName) => {
119
146
  return {
120
147
  validateNative: (nativeAddress): nativeAddress is HexString => {
148
+ if (nativeAddress === undefined) {
149
+ return false;
150
+ }
121
151
  try {
122
152
  parseTonAddress(nativeAddress);
123
153
  return true;
@@ -126,6 +156,7 @@ export const tonAddressParser: (
126
156
  }
127
157
  },
128
158
  normalizedToNative: (address) => {
159
+ if (address === undefined) return undefined as any;
129
160
  const numericalValue = addressParser.normalizedToBigInt(address);
130
161
  return {
131
162
  // FIXME this is wrong
@@ -134,33 +165,36 @@ export const tonAddressParser: (
134
165
  };
135
166
  },
136
167
  normalizedToNativeString: (address) => {
168
+ if (address === undefined) return undefined as any;
137
169
  return addressParser(chainName).normalizedToNative(address).nativeAddress;
138
170
  },
139
171
  nativeToNormalized: (nativeAddress) => {
172
+ if (nativeAddress === undefined) return undefined as any;
140
173
  const address = getAddress(nativeAddress);
141
- validateNative(address, chainName);
174
+ assertNative(address, chainName);
142
175
  return normalizeHex(addressToHex(address));
143
176
  },
144
177
  nativeToBytes32Hex: (nativeAddress) => {
145
178
  // naturally ton is 32 bytes, pad it anyway
179
+ if (nativeAddress === undefined) return undefined as any;
146
180
  return hexZeroPad(
147
181
  addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,
148
182
  32,
149
183
  );
150
184
  },
151
185
  nativeToBytes: (nativeAddress) => {
186
+ if (nativeAddress === undefined) return undefined as any;
152
187
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
153
188
  },
154
189
  nativeToBytes32: (nativeAddress) => {
190
+ if (nativeAddress === undefined) return undefined as any;
155
191
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
156
192
  },
157
193
  parseNative: (nativeAddress) => {
158
- if (!validateNative(nativeAddress, chainName)) {
159
- throw new Error('Invalid native address: ' + nativeAddress);
160
- }
194
+ assertNative(nativeAddress, chainName);
161
195
  return addressParser(chainName).nativeToNormalized(nativeAddress);
162
196
  },
163
- parseNativeToHex: (nativeAddress) => {
197
+ parseNativeToUnpaddedHex: (nativeAddress) => {
164
198
  return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;
165
199
  },
166
200
  tonToNormalized: (tonAddress) => {
@@ -175,6 +209,9 @@ export const initiaAddressParser: (
175
209
  ) => AddressParser<ChainsByEncoding[InitiaString]> = (chainName) => {
176
210
  return {
177
211
  validateNative: (nativeAddress): nativeAddress is InitiaString => {
212
+ if (nativeAddress === undefined) {
213
+ return false;
214
+ }
178
215
  try {
179
216
  // Both formats are native for the chain and are used kind of randomly.
180
217
  return (
@@ -186,41 +223,48 @@ export const initiaAddressParser: (
186
223
  }
187
224
  },
188
225
  normalizedToNative: (address) => {
226
+ if (address === undefined) {
227
+ return undefined as any;
228
+ }
189
229
  return {
190
230
  nativeAddress: AccAddress.fromHex(address as unknown as HexString) as InitiaString,
191
231
  chainName,
192
232
  };
193
233
  },
194
234
  normalizedToNativeString: (address) => {
235
+ if (address === undefined) return undefined as any;
195
236
  return addressParser(chainName).normalizedToNative(address).nativeAddress;
196
237
  },
197
238
  nativeToNormalized: (nativeAddress) => {
239
+ if (nativeAddress === undefined) return undefined as any;
198
240
  const address = getAddress(nativeAddress);
199
- validateNative(address, chainName);
241
+ assertNative(address, chainName);
200
242
  if (isHexString(address)) {
201
243
  return normalizeHex(address);
202
244
  }
203
245
  return normalizeHex(AccAddress.toHex(nativeAddress.toString()) as HexString);
204
246
  },
205
247
  nativeToBytes32Hex: (nativeAddress) => {
248
+ if (nativeAddress === undefined) return undefined as any;
249
+ // naturally ton is 32 bytes, pad it anyway
206
250
  return hexZeroPad(
207
251
  addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,
208
252
  32,
209
253
  );
210
254
  },
211
255
  nativeToBytes: (nativeAddress) => {
256
+ if (nativeAddress === undefined) return undefined as any;
212
257
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
213
258
  },
214
259
  nativeToBytes32: (nativeAddress) => {
260
+ if (nativeAddress === undefined) return undefined as any;
215
261
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
216
262
  },
217
263
  parseNative: (nativeAddress) => {
218
- if (!validateNative(nativeAddress, chainName)) {
219
- throw new Error('Invalid native address: ' + nativeAddress);
220
- }
264
+ assertNative(nativeAddress, chainName);
221
265
  return addressParser(chainName).nativeToNormalized(nativeAddress);
222
266
  },
223
- parseNativeToHex: (nativeAddress) => {
267
+ parseNativeToUnpaddedHex: (nativeAddress) => {
224
268
  return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;
225
269
  },
226
270
  encoding: AddressEncoding.INITIA,
@@ -232,9 +276,11 @@ export const base58AddressParser = (
232
276
  ): AddressParser<ChainsByEncoding[Base58String]> => {
233
277
  return {
234
278
  validateNative: (nativeAddress): nativeAddress is Base58String => {
279
+ if (nativeAddress === undefined) return false;
235
280
  return isBase58(nativeAddress);
236
281
  },
237
282
  normalizedToNative: (address) => {
283
+ if (address === undefined) return undefined as any;
238
284
  return {
239
285
  nativeAddress: bs58.encode(
240
286
  hexToBytes(address as unknown as HexString),
@@ -243,36 +289,39 @@ export const base58AddressParser = (
243
289
  };
244
290
  },
245
291
  normalizedToNativeString: (address) => {
292
+ if (address === undefined) return undefined as any;
246
293
  return addressParser(chainName).normalizedToNative(address).nativeAddress;
247
294
  },
248
295
  nativeToNormalized: (nativeAddress) => {
296
+ if (nativeAddress === undefined) return undefined as any;
249
297
  const address = getAddress(nativeAddress);
250
- validateNative(address, chainName);
298
+ assertNative(address, chainName);
251
299
  const hex = bytesToHexPrefixed(bs58.decode(address));
252
300
  return normalizeHex(hex);
253
301
  },
254
302
  nativeToBytes32Hex: (nativeAddress) => {
255
303
  // naturally solana is 32 bytes, pad it anyway
304
+ if (nativeAddress === undefined) return undefined as any;
256
305
  return hexZeroPad(
257
306
  addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,
258
307
  32,
259
308
  );
260
309
  },
261
310
  nativeToBytes: (nativeAddress) => {
311
+ if (nativeAddress === undefined) return undefined as any;
262
312
  const address = getAddress(nativeAddress);
263
- validateNative(address, chainName);
313
+ assertNative(address, chainName);
264
314
  return bs58.decode(address);
265
315
  },
266
316
  nativeToBytes32: (nativeAddress) => {
317
+ if (nativeAddress === undefined) return undefined as any;
267
318
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
268
319
  },
269
320
  parseNative: (nativeAddress) => {
270
- if (!validateNative(nativeAddress, chainName)) {
271
- throw new Error('Invalid native address: ' + nativeAddress);
272
- }
321
+ assertNative(nativeAddress, chainName);
273
322
  return addressParser(chainName).nativeToNormalized(nativeAddress);
274
323
  },
275
- parseNativeToHex: (nativeAddress) => {
324
+ parseNativeToUnpaddedHex: (nativeAddress) => {
276
325
  return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;
277
326
  },
278
327
  encoding: AddressEncoding.BASE58,
@@ -282,13 +331,16 @@ export const base58AddressParser = (
282
331
  export const hexAddressParser = (
283
332
  chainName: ChainsByEncoding[HexString],
284
333
  ): AddressParser<ChainsByEncoding[HexString]> => {
285
- const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName as ChainName);
286
-
287
334
  return {
288
335
  validateNative: (nativeAddress): nativeAddress is HexString => {
336
+ if (nativeAddress === undefined) return false;
289
337
  return isHexString(nativeAddress);
290
338
  },
291
339
  normalizedToNative: (address) => {
340
+ if (address === undefined) return undefined as any;
341
+ // we fetch here instead of storing by chain because some chains may not have this defined,
342
+ // so we should only throw on those chains if they try to use these methods that need the native byte length
343
+ const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName);
292
344
  return {
293
345
  // we could have EIP-55 here, but omitted for now
294
346
  nativeAddress: hexZeroPad(address as unknown as HexString, nativeByteLength),
@@ -296,42 +348,215 @@ export const hexAddressParser = (
296
348
  };
297
349
  },
298
350
  normalizedToNativeString: (address) => {
351
+ if (address === undefined) return undefined as any;
352
+ const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName);
299
353
  return hexZeroPad(address as unknown as HexString, nativeByteLength);
300
354
  },
301
355
  nativeToNormalized: (nativeAddress) => {
356
+ if (nativeAddress === undefined) return undefined as any;
302
357
  const address = getAddress(nativeAddress);
303
- validateNative(address, chainName);
358
+ assertNative(address, chainName);
304
359
  return normalizeHex(address);
305
360
  },
306
361
  nativeToBytes: (nativeAddress) => {
362
+ if (nativeAddress === undefined) return undefined as any;
307
363
  const address = getAddress(nativeAddress);
308
- validateNative(address, chainName);
364
+ assertNative(address, chainName);
309
365
  return hexToBytes(address);
310
366
  },
311
367
  nativeToBytes32Hex: (nativeAddress) => {
368
+ if (nativeAddress === undefined) return undefined as any;
312
369
  const address = getAddress(nativeAddress);
313
- validateNative(address, chainName);
370
+ assertNative(address, chainName);
314
371
  return hexZeroPad(address, 32);
315
372
  },
316
373
  nativeToBytes32: (nativeAddress) => {
374
+ if (nativeAddress === undefined) return undefined as any;
317
375
  return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));
318
376
  },
319
377
  parseNative: (nativeAddress) => {
320
378
  // Specific case to make tron a little less flaky, handle the 41 prefix
321
- if (chainName === ChainName.TRON) {
379
+ if (chainName === ChainName.TRON && nativeAddress) {
322
380
  nativeAddress = nativeAddress.startsWith('41')
323
381
  ? `0x${nativeAddress.slice(2)}`
324
382
  : nativeAddress;
325
383
  }
326
384
 
327
- if (!validateNative(nativeAddress, chainName)) {
328
- throw new Error('Invalid native address: ' + nativeAddress);
329
- }
385
+ assertNative(nativeAddress, chainName);
330
386
  return addressParser(chainName).nativeToNormalized(nativeAddress);
331
387
  },
332
- parseNativeToHex: (nativeAddress) => {
388
+ parseNativeToUnpaddedHex: (nativeAddress) => {
333
389
  return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;
334
390
  },
335
391
  encoding: AddressEncoding.HEX,
336
392
  };
337
393
  };
394
+
395
+ export const stellarAddressParser: (
396
+ chainName: InferChainNamesForChainType<ChainType.STELLAR>,
397
+ ) => AddressParser<InferChainNamesForChainType<ChainType.STELLAR>> = (chainName) => {
398
+ /**
399
+ * Validate if a string is a valid Stellar address.
400
+ *
401
+ * According to SEP-0023 (https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md),
402
+ * Stellar supports multiple address types encoded as base32 ASCII strings:
403
+ * - Account addresses starting with 'G' (56 chars, Ed25519 public key)
404
+ * - Contract addresses starting with 'C' (56 chars, contract ID)
405
+ * - Muxed addresses starting with 'M' (69 chars, muxed account with ID)
406
+ * - Liquidity pool addresses starting with 'L' (56 chars, pool ID)
407
+ * - Claimable balance addresses starting with 'B' (56 chars, balance ID)
408
+ * - Signed payload starting with 'P' (variable length)
409
+ *
410
+ * All addresses are stored in their native ASCII format to preserve type information.
411
+ */
412
+ const isValidStellarAddress = (address: string): boolean => {
413
+ if (!address) return false;
414
+ // Use the StrKey validation methods for all supported address types
415
+ return (
416
+ StrKey.isValidEd25519PublicKey(address) ||
417
+ StrKey.isValidContract(address) ||
418
+ StrKey.isValidMed25519PublicKey(address) ||
419
+ StrKey.isValidLiquidityPool(address) ||
420
+ StrKey.isValidClaimableBalance(address) ||
421
+ StrKey.isValidSignedPayload(address)
422
+ );
423
+ };
424
+
425
+ /**
426
+ * Convert Stellar ASCII address to normalized hex by encoding the ASCII string as UTF-8 bytes.
427
+ * This preserves the full address including type prefix and allows round-trip conversion.
428
+ */
429
+ const stellarTextEncoder = new TextEncoder();
430
+ const stellarAsciiToNormalized = (address: string): NormalizedHexString => {
431
+ // Convert ASCII address string to UTF-8 bytes, then to hex
432
+ const bytes = stellarTextEncoder.encode(address);
433
+ return normalizeHex(bytesToHexPrefixed(bytes));
434
+ };
435
+
436
+ /**
437
+ * Convert normalized hex back to Stellar ASCII address by decoding UTF-8 bytes.
438
+ */
439
+ const stellarTextDecoder = new TextDecoder();
440
+ const normalizedToStellarAscii = (normalizedHex: NormalizedHexString): string => {
441
+ // Convert hex to bytes, then decode as UTF-8 string
442
+ const bytes = hexToBytes(normalizedHex as unknown as HexString);
443
+ return stellarTextDecoder.decode(bytes);
444
+ };
445
+
446
+ return {
447
+ validateNative: (nativeAddress): nativeAddress is StellarString => {
448
+ if (nativeAddress === undefined) return false;
449
+ return isValidStellarAddress(nativeAddress);
450
+ },
451
+ normalizedToNative: (address) => {
452
+ if (address === undefined) return undefined as any;
453
+ // Decode normalized hex back to ASCII Stellar address
454
+ const stellarAddress = normalizedToStellarAscii(address);
455
+ return {
456
+ nativeAddress: stellarAddress as StellarString,
457
+ chainName,
458
+ };
459
+ },
460
+ normalizedToNativeString: (address) => {
461
+ if (address === undefined) return undefined as any;
462
+ return normalizedToStellarAscii(address) as StellarString;
463
+ },
464
+ nativeToNormalized: (nativeAddress) => {
465
+ if (nativeAddress === undefined) return undefined as any;
466
+ const address = getAddress(nativeAddress);
467
+ assertNative(address, chainName);
468
+ // Convert ASCII address to normalized hex
469
+ return stellarAsciiToNormalized(address);
470
+ },
471
+ nativeToBytes32Hex: (nativeAddress) => {
472
+ if (nativeAddress === undefined) return undefined as any;
473
+ const address = getAddress(nativeAddress);
474
+ assertNative(address, chainName);
475
+ // Decode the Stellar address to its raw 32-byte payload based on type
476
+ let rawBytes: Buffer;
477
+ if (StrKey.isValidEd25519PublicKey(address)) {
478
+ // Account addresses (G...) - Ed25519 public key (32 bytes)
479
+ rawBytes = StrKey.decodeEd25519PublicKey(address);
480
+ } else if (StrKey.isValidContract(address)) {
481
+ // Contract addresses (C...) - contract ID (32 bytes)
482
+ rawBytes = StrKey.decodeContract(address);
483
+ } else if (StrKey.isValidLiquidityPool(address)) {
484
+ // Liquidity pool addresses (L...) - pool ID (32 bytes)
485
+ rawBytes = StrKey.decodeLiquidityPool(address);
486
+ } else if (StrKey.isValidClaimableBalance(address)) {
487
+ // Claimable balance addresses (B...) - balance ID (33 bytes: 1-byte version + 32-byte hash)
488
+ // Per SEP-0023: first byte is the claimable balance type (0x00 = V0), followed by 32-byte SHA256 hash
489
+ const fullBytes = StrKey.decodeClaimableBalance(address);
490
+ rawBytes = fullBytes.subarray(1); // Skip the first byte (version), return the 32-byte hash
491
+ } else if (StrKey.isValidMed25519PublicKey(address)) {
492
+ // Muxed addresses (M...) contain additional data beyond 32 bytes
493
+ throw new Error(
494
+ 'nativeToBytes32 is not supported for Stellar muxed addresses (M...)',
495
+ );
496
+ } else if (StrKey.isValidSignedPayload(address)) {
497
+ // Signed payload (P...) has variable length
498
+ throw new Error(
499
+ 'nativeToBytes32 is not supported for Stellar signed payload addresses (P...)',
500
+ );
501
+ } else {
502
+ throw new Error(`Unknown Stellar address type: ${address}`);
503
+ }
504
+ return bytesToHexPrefixed(rawBytes);
505
+ },
506
+ nativeToBytes: (nativeAddress) => {
507
+ if (nativeAddress === undefined) return undefined as any;
508
+ const address = getAddress(nativeAddress);
509
+ assertNative(address, chainName);
510
+ // Convert ASCII address to UTF-8 bytes
511
+ return stellarTextEncoder.encode(address);
512
+ },
513
+ nativeToBytes32: (nativeAddress) => {
514
+ if (nativeAddress === undefined) return undefined as any;
515
+ // Use nativeToBytes32Hex and convert to Uint8Array
516
+ const hex = addressParser(chainName).nativeToBytes32Hex(nativeAddress);
517
+ return hexToBytes(hex);
518
+ },
519
+ parseNative: (nativeAddress) => {
520
+ assertNative(nativeAddress, chainName);
521
+ return addressParser(chainName).nativeToNormalized(nativeAddress);
522
+ },
523
+ parseNativeToUnpaddedHex: (nativeAddress) => {
524
+ assertNative(nativeAddress, chainName);
525
+ const normalized = addressParser(chainName).nativeToNormalized(nativeAddress);
526
+ return normalized as unknown as HexString;
527
+ },
528
+ encoding: AddressEncoding.STELLAR,
529
+ };
530
+ };
531
+
532
+ // Starknet addresses do not have handle hexzero padding, so we just return the address as is
533
+ export const starknetAddressParser = (
534
+ chainName: ChainsByEncoding[HexString],
535
+ ): AddressParser<ChainsByEncoding[HexString]> => {
536
+ return {
537
+ ...hexAddressParser(chainName),
538
+ validateNative: (nativeAddress): nativeAddress is HexString => {
539
+ if (nativeAddress === undefined) return false;
540
+ return isHexString(nativeAddress);
541
+ },
542
+ parseNative: (nativeAddress) => {
543
+ if (nativeAddress === undefined) return undefined as any;
544
+ // We can't trust starknet.js strings, they may actually be bigints!
545
+ // so just convert everything to hex first, then convert to normalized
546
+ nativeAddress = `0x${BigInt(nativeAddress).toString(16)}`;
547
+ assertNative(nativeAddress, chainName);
548
+ return addressParser(chainName).nativeToNormalized(nativeAddress);
549
+ },
550
+ normalizedToNative: (address) => {
551
+ if (address === undefined) return undefined as any;
552
+ return {
553
+ nativeAddress: address,
554
+ chainName,
555
+ };
556
+ },
557
+ normalizedToNativeString: (address) => {
558
+ if (address === undefined) return undefined as any;
559
+ return address;
560
+ },
561
+ };
562
+ };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/addressParser.ts"],"names":["init_cjs_shims","AddressEncoding","addressParser","chainName","ChainName","TON","tonAddressParser","SOLANA","base58AddressParser","INITIA","initiaAddressParser","hexAddressParser","normalizedToBytes32Hex","address","hexZeroPad","normalizedToUnpaddedString","toString","normalizedToBytes32","hexToBytes","normalizedToBigInt","BigInt","parseNative","nativeAddress","validateNative","__name","expectedChainName","Error","getAddress","parseTonAddress","normalizedToNative","numericalValue","bigintToAddress","normalizedToNativeString","nativeToNormalized","normalizeHex","addressToHex","nativeToBytes32Hex","nativeToBytes","nativeToBytes32","parseNativeToHex","tonToNormalized","tonAddress","encoding","isHexString","test","AccAddress","fromHex","toHex","isBase58","bs58","encode","hex","bytesToHexPrefixed","decode","nativeByteLength","StaticChainConfigs","getAddressSizeInBytes","TRON","startsWith","slice"],"mappings":";;;;;;;;;;;;;;;;AAAAA,2BAAA,EAAA;AA8BO,IAAKC,eAAAA,6BAAAA,gBAAAA,EAAAA;;;;;AAAAA,EAAAA,OAAAA,gBAAAA;;AAqCL,SAASC,cAAmCC,SAAAA,EAAY;AAE3D,EAAA,IAAIA,SAAAA,KAAcC,8BAAAA,CAAUC,GAAAA,EACxB,OAAOC,iBAAiBH,SAAAA,CAAA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AAA,CAAA,GACnC,KAAKpC,aAAAA,CAAcqC,KAAAA,CAAM,CAAA,CAAA,CAAA,CAAA,GACzBrC,aAAAA;AACV,MAAA;AAEA,MAAA,IAAI,CAACC,cAAAA,CAAeD,aAAAA,EAAenB,SAAAA,CAAAA,EAAY;AAC3C,QAAA,MAAM,IAAIuB,KAAAA,CAAM,0BAAA,GAA6BJ,aAAAA,CAAAA;AACjD,MAAA;AACA,MAAA,OAAOpB,aAAAA,CAAcC,SAAAA,CAAAA,CAAW8B,kBAAAA,CAAmBX,aAAAA,CAAAA;IACvD,CAAA,EAZa,aAAA,CAAA;AAabiB,IAAAA,gBAAAA,uCAAmBjB,aAAAA,KAAAA;AACf,MAAA,OAAOpB,aAAAA,CAAcC,SAAAA,CAAAA,CAAWkB,WAAAA,CAAYC,aAAAA,CAAAA;IAChD,CAAA,EAFkB,kBAAA,CAAA;IAGlBoB,QAAAA,EAAQ;AACZ,GAAA;AACJ,CAAA,EAvDgC,kBAAA","file":"K6ILVJZQ.cjs","sourcesContent":["import { AccAddress } from '@initia/initia.js';\nimport type { Address } from '@ton/ton';\nimport bs58 from 'bs58';\n\nimport type { HexString, NormalizedHexString } from '@layerzerolabs/common-chain-model';\nimport { normalizeHex } from '@layerzerolabs/common-chain-model';\nimport { addressToHex, bigintToAddress, parseTonAddress } from '@layerzerolabs/common-ton';\nimport {\n bytesToHexPrefixed,\n hexToBytes,\n hexZeroPad,\n isBase58,\n isHexString,\n} from '@layerzerolabs/common-utils';\nimport type {\n Base58String,\n ChainsByEncoding,\n ChainType,\n InferChainNamesForChainType,\n InferChainTypeForChainName,\n InitiaString,\n NativeAddress,\n ResolvedEncodingForChainName,\n} from '@layerzerolabs/layerzero-definitions';\nimport type { ChainNativeAddress } from '@layerzerolabs/layerzero-definitions';\nimport { ChainName } from '@layerzerolabs/layerzero-definitions';\nimport { StaticChainConfigs } from '@layerzerolabs/static-chain-info';\n\ntype NativeOrString<T extends ChainName> = ChainNativeAddress<T> | ResolvedEncodingForChainName<T>;\n\nexport enum AddressEncoding {\n HEX = 'hex',\n BASE58 = 'base58',\n TON = 'ton',\n INITIA = 'initia',\n}\n\nexport type ExtensionByChainType = {\n ton: {\n tonToNormalized: (tonAddress: Address) => NormalizedHexString;\n };\n};\n\nexport type InferExtensionByChainName<T extends ChainName> =\n InferChainTypeForChainName<T> extends keyof ExtensionByChainType\n ? ExtensionByChainType[InferChainTypeForChainName<T>]\n : {};\n\nexport type AddressParser<T extends ChainName> = {\n normalizedToNative: (address: NormalizedHexString) => ChainNativeAddress<T>;\n normalizedToNativeString: (address: NormalizedHexString) => ResolvedEncodingForChainName<T>;\n nativeToNormalized: (nativeAddress: NativeOrString<T>) => NormalizedHexString;\n nativeToBytes: (nativeAddress: NativeOrString<T>) => Uint8Array;\n nativeToBytes32Hex: (nativeAddress: NativeOrString<T>) => HexString;\n nativeToBytes32: (nativeAddress: NativeOrString<T>) => Uint8Array;\n validateNative: (nativeAddress: string) => nativeAddress is ResolvedEncodingForChainName<T>;\n /**\n * Parse a native address to a string if it is valid.\n * Throws an error if the native address is invalid.\n * @param nativeAddress - The native address to parse.\n * @returns The parsed native address.\n */\n parseNative: (nativeAddress: string) => NormalizedHexString;\n parseNativeToHex: (nativeAddress: string) => HexString;\n encoding: AddressEncoding;\n} & InferExtensionByChainName<T>;\n\nexport function addressParser<T extends ChainName>(chainName: T): AddressParser<T> {\n // FIXME: figure out a way to not have to cast here\n if (chainName === ChainName.TON)\n return tonAddressParser(chainName) as unknown as AddressParser<T>;\n if (chainName === ChainName.SOLANA)\n return base58AddressParser(chainName) as unknown as AddressParser<T>;\n if (chainName === ChainName.INITIA)\n return initiaAddressParser(chainName) as unknown as AddressParser<T>;\n return hexAddressParser(chainName) as unknown as AddressParser<T>;\n}\n\nexport namespace addressParser {\n export const normalizedToBytes32Hex = (address: NormalizedHexString) => {\n return hexZeroPad(address as unknown as HexString, 32);\n };\n export const normalizedToUnpaddedString = (address: NormalizedHexString) => {\n return address.toString() as HexString;\n };\n export const normalizedToBytes32 = (address: NormalizedHexString) => {\n return hexToBytes(normalizedToBytes32Hex(address));\n };\n export const normalizedToBigInt = (address: NormalizedHexString) => {\n return BigInt(address as unknown as HexString);\n };\n export const parseNative = (nativeAddress: NativeAddress) => {\n return addressParser(nativeAddress.chainName).parseNative(nativeAddress.nativeAddress);\n };\n}\n\nconst validateNative = <T extends ChainName>(\n nativeAddress: string,\n expectedChainName: T,\n): nativeAddress is ResolvedEncodingForChainName<T> => {\n if (!addressParser(expectedChainName).validateNative(nativeAddress)) {\n throw new Error(\n 'Cannot convert: ' +\n nativeAddress +\n ' - The native address does not match the expected format for the chain: ' +\n expectedChainName,\n );\n }\n return true;\n};\n\nconst getAddress = <T extends ChainName>(nativeAddress: NativeOrString<T>) => {\n return typeof nativeAddress === 'object' ? nativeAddress.nativeAddress : nativeAddress;\n};\n\nexport const tonAddressParser: (\n chainName: InferChainNamesForChainType<ChainType.TON>,\n) => AddressParser<InferChainNamesForChainType<ChainType.TON>> = (chainName) => {\n return {\n validateNative: (nativeAddress): nativeAddress is HexString => {\n try {\n parseTonAddress(nativeAddress);\n return true;\n } catch {\n return false;\n }\n },\n normalizedToNative: (address) => {\n const numericalValue = addressParser.normalizedToBigInt(address);\n return {\n // FIXME this is wrong\n nativeAddress: bigintToAddress(numericalValue).toString() as HexString,\n chainName,\n };\n },\n normalizedToNativeString: (address) => {\n return addressParser(chainName).normalizedToNative(address).nativeAddress;\n },\n nativeToNormalized: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n return normalizeHex(addressToHex(address));\n },\n nativeToBytes32Hex: (nativeAddress) => {\n // naturally ton is 32 bytes, pad it anyway\n return hexZeroPad(\n addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,\n 32,\n );\n },\n nativeToBytes: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n nativeToBytes32: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n parseNative: (nativeAddress) => {\n if (!validateNative(nativeAddress, chainName)) {\n throw new Error('Invalid native address: ' + nativeAddress);\n }\n return addressParser(chainName).nativeToNormalized(nativeAddress);\n },\n parseNativeToHex: (nativeAddress) => {\n return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;\n },\n tonToNormalized: (tonAddress) => {\n return addressParser(chainName).parseNative(addressToHex(tonAddress));\n },\n encoding: AddressEncoding.TON,\n };\n};\n\nexport const initiaAddressParser: (\n chainName: ChainsByEncoding[InitiaString],\n) => AddressParser<ChainsByEncoding[InitiaString]> = (chainName) => {\n return {\n validateNative: (nativeAddress): nativeAddress is InitiaString => {\n try {\n // Both formats are native for the chain and are used kind of randomly.\n return (\n isHexString(nativeAddress) ||\n /^(init)1(['qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,66})$/.test(nativeAddress)\n );\n } catch {\n return false;\n }\n },\n normalizedToNative: (address) => {\n return {\n nativeAddress: AccAddress.fromHex(address as unknown as HexString) as InitiaString,\n chainName,\n };\n },\n normalizedToNativeString: (address) => {\n return addressParser(chainName).normalizedToNative(address).nativeAddress;\n },\n nativeToNormalized: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n if (isHexString(address)) {\n return normalizeHex(address);\n }\n return normalizeHex(AccAddress.toHex(nativeAddress.toString()) as HexString);\n },\n nativeToBytes32Hex: (nativeAddress) => {\n return hexZeroPad(\n addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,\n 32,\n );\n },\n nativeToBytes: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n nativeToBytes32: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n parseNative: (nativeAddress) => {\n if (!validateNative(nativeAddress, chainName)) {\n throw new Error('Invalid native address: ' + nativeAddress);\n }\n return addressParser(chainName).nativeToNormalized(nativeAddress);\n },\n parseNativeToHex: (nativeAddress) => {\n return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;\n },\n encoding: AddressEncoding.INITIA,\n };\n};\n\nexport const base58AddressParser = (\n chainName: ChainsByEncoding[Base58String],\n): AddressParser<ChainsByEncoding[Base58String]> => {\n return {\n validateNative: (nativeAddress): nativeAddress is Base58String => {\n return isBase58(nativeAddress);\n },\n normalizedToNative: (address) => {\n return {\n nativeAddress: bs58.encode(\n hexToBytes(address as unknown as HexString),\n ) as Base58String,\n chainName,\n };\n },\n normalizedToNativeString: (address) => {\n return addressParser(chainName).normalizedToNative(address).nativeAddress;\n },\n nativeToNormalized: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n const hex = bytesToHexPrefixed(bs58.decode(address));\n return normalizeHex(hex);\n },\n nativeToBytes32Hex: (nativeAddress) => {\n // naturally solana is 32 bytes, pad it anyway\n return hexZeroPad(\n addressParser(chainName).nativeToNormalized(nativeAddress) as unknown as HexString,\n 32,\n );\n },\n nativeToBytes: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n return bs58.decode(address);\n },\n nativeToBytes32: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n parseNative: (nativeAddress) => {\n if (!validateNative(nativeAddress, chainName)) {\n throw new Error('Invalid native address: ' + nativeAddress);\n }\n return addressParser(chainName).nativeToNormalized(nativeAddress);\n },\n parseNativeToHex: (nativeAddress) => {\n return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;\n },\n encoding: AddressEncoding.BASE58,\n };\n};\n\nexport const hexAddressParser = (\n chainName: ChainsByEncoding[HexString],\n): AddressParser<ChainsByEncoding[HexString]> => {\n const nativeByteLength = StaticChainConfigs.getAddressSizeInBytes(chainName as ChainName);\n\n return {\n validateNative: (nativeAddress): nativeAddress is HexString => {\n return isHexString(nativeAddress);\n },\n normalizedToNative: (address) => {\n return {\n // we could have EIP-55 here, but omitted for now\n nativeAddress: hexZeroPad(address as unknown as HexString, nativeByteLength),\n chainName,\n };\n },\n normalizedToNativeString: (address) => {\n return hexZeroPad(address as unknown as HexString, nativeByteLength);\n },\n nativeToNormalized: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n return normalizeHex(address);\n },\n nativeToBytes: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n return hexToBytes(address);\n },\n nativeToBytes32Hex: (nativeAddress) => {\n const address = getAddress(nativeAddress);\n validateNative(address, chainName);\n return hexZeroPad(address, 32);\n },\n nativeToBytes32: (nativeAddress) => {\n return hexToBytes(addressParser(chainName).nativeToBytes32Hex(nativeAddress));\n },\n parseNative: (nativeAddress) => {\n // Specific case to make tron a little less flaky, handle the 41 prefix\n if (chainName === ChainName.TRON) {\n nativeAddress = nativeAddress.startsWith('41')\n ? `0x${nativeAddress.slice(2)}`\n : nativeAddress;\n }\n\n if (!validateNative(nativeAddress, chainName)) {\n throw new Error('Invalid native address: ' + nativeAddress);\n }\n return addressParser(chainName).nativeToNormalized(nativeAddress);\n },\n parseNativeToHex: (nativeAddress) => {\n return addressParser(chainName).parseNative(nativeAddress) as unknown as HexString;\n },\n encoding: AddressEncoding.HEX,\n };\n};\n"]}