@xchainjs/xchain-radix 2.0.3 → 2.0.4

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.
package/README.md CHANGED
@@ -63,7 +63,7 @@ second one submits the transaction to the ledger
63
63
  ```
64
64
  import { Network, TxParams, XChainClientParams } from '@xchainjs/xchain-client'
65
65
  import { Client, XrdAssetStokenet } from '@xchainjs/xchain-radix'
66
- import { baseAmount } from '@xchainjs/xchain-util/lib'
66
+ import { baseAmount } from '@xchainjs/xchain-util'
67
67
 
68
68
  const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
69
69
  const params: XChainClientParams = {
@@ -93,7 +93,7 @@ main().catch(console.error)
93
93
  ```
94
94
  import { Network, TxParams, XChainClientParams } from '@xchainjs/xchain-client'
95
95
  import { Client, XrdAssetStokenet } from '@xchainjs/xchain-radix'
96
- import { baseAmount } from '@xchainjs/xchain-util/lib'
96
+ import { baseAmount } from '@xchainjs/xchain-util'
97
97
 
98
98
  const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
99
99
  const params: XChainClientParams = {
@@ -121,7 +121,7 @@ main().catch(console.error)
121
121
  ### Getting a transaction data
122
122
 
123
123
  ```
124
- import { Network, Tx, TxParams, XChainClientParams } from '@xchainjs/xchain-client/lib'
124
+ import { Network, Tx, TxParams, XChainClientParams } from '@xchainjs/xchain-client'
125
125
  import { Client } from '@xchainjs/xchain-radix'
126
126
  import { XrdAsset } from '@xchainjs/xchain-radix/src/const'
127
127
  import { baseAmount } from '@xchainjs/xchain-util'
@@ -146,9 +146,9 @@ console.log(transaction)
146
146
  ### Getting balances
147
147
 
148
148
  ```
149
- import { Balance, Network, XChainClientParams } from '@xchainjs/xchain-client/lib'
149
+ import { Balance, Network, XChainClientParams } from '@xchainjs/xchain-client'
150
150
  import { Client } from '@xchainjs/xchain-radix'
151
- import { Asset } from '@xchainjs/xchain-util/lib'
151
+ import { Asset } from '@xchainjs/xchain-util'
152
152
 
153
153
  const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
154
154
  const params: XChainClientParams = {
@@ -174,7 +174,7 @@ console.log(balances)
174
174
  ### Getting fees
175
175
 
176
176
  ```
177
- import { Fees, Network, XChainClientParams } from '@xchainjs/xchain-client/lib'
177
+ import { Fees, Network, XChainClientParams } from '@xchainjs/xchain-client'
178
178
  import { Client } from '@xchainjs/xchain-radix'
179
179
 
180
180
  const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
@@ -191,7 +191,7 @@ console.log(fees)
191
191
  ### Getting transactions history
192
192
 
193
193
  ```
194
- import { Network, XChainClientParams } from '@xchainjs/xchain-client/lib'
194
+ import { Network, XChainClientParams } from '@xchainjs/xchain-client'
195
195
  import { Client } from '@xchainjs/xchain-radix'
196
196
 
197
197
  const phrase = 'rural bright ball negative already grass good grant nation screen model pizza'
package/lib/index.esm.js CHANGED
@@ -2,7 +2,7 @@ import { NetworkId, generateRandomNonce, RadixEngineToolkit, Convert, ManifestBu
2
2
  import { Network, BaseXChainClient, singleFee, FeeType, TxType } from '@xchainjs/xchain-client';
3
3
  import { getSeed } from '@xchainjs/xchain-crypto';
4
4
  import { AssetType, assetToBase, assetAmount, eqAsset, baseAmount } from '@xchainjs/xchain-util';
5
- import { bech32m } from 'bech32';
5
+ import { createBase58check, bech32m } from '@scure/base';
6
6
  import { derivePath } from 'ed25519-hd-key';
7
7
  import { GatewayApiClient } from '@radixdlt/babylon-gateway-api-sdk';
8
8
 
@@ -52,17 +52,17 @@ const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? global
52
52
  // Makes the utils un-importable in browsers without a bundler.
53
53
  // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
54
54
  /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
55
- function isBytes$1(a) {
55
+ function isBytes(a) {
56
56
  return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
57
57
  }
58
58
  /** Asserts something is positive integer. */
59
- function anumber$1(n) {
59
+ function anumber(n) {
60
60
  if (!Number.isSafeInteger(n) || n < 0)
61
61
  throw new Error('positive integer expected, got ' + n);
62
62
  }
63
63
  /** Asserts something is Uint8Array. */
64
64
  function abytes(b, ...lengths) {
65
- if (!isBytes$1(b))
65
+ if (!isBytes(b))
66
66
  throw new Error('Uint8Array expected');
67
67
  if (lengths.length > 0 && !lengths.includes(b.length))
68
68
  throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
@@ -71,8 +71,8 @@ function abytes(b, ...lengths) {
71
71
  function ahash(h) {
72
72
  if (typeof h !== 'function' || typeof h.create !== 'function')
73
73
  throw new Error('Hash should be wrapped by utils.createHasher');
74
- anumber$1(h.outputLen);
75
- anumber$1(h.blockLen);
74
+ anumber(h.outputLen);
75
+ anumber(h.blockLen);
76
76
  }
77
77
  /** Asserts a hash instance has not been destroyed / finished */
78
78
  function aexists(instance, checkFinished = true) {
@@ -280,7 +280,7 @@ function ensureBytes(title, hex, expectedLength) {
280
280
  throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);
281
281
  }
282
282
  }
283
- else if (isBytes$1(hex)) {
283
+ else if (isBytes(hex)) {
284
284
  // Uint8Array.from() instead of hash.slice() because node.js Buffer
285
285
  // is instance of Uint8Array, and its slice() creates **mutable** copy
286
286
  res = Uint8Array.from(hex);
@@ -714,7 +714,7 @@ function FpLegendre(Fp, n) {
714
714
  function nLength(n, nBitLength) {
715
715
  // Bit size, byte size of CURVE.n
716
716
  if (nBitLength !== undefined)
717
- anumber$1(nBitLength);
717
+ anumber(nBitLength);
718
718
  const _nBitLength = nBitLength !== undefined ? nBitLength : n.toString(2).length;
719
719
  const nByteLength = Math.ceil(_nBitLength / 8);
720
720
  return { nBitLength: _nBitLength, nByteLength };
@@ -2760,7 +2760,7 @@ function ecdsa(Point, ecdsaOpts, curveOpts = {}) {
2760
2760
  throw new Error('options.strict was renamed to lowS');
2761
2761
  if (format !== undefined && !['compact', 'der', 'js'].includes(format))
2762
2762
  throw new Error('format must be "compact", "der" or "js"');
2763
- const isHex = typeof sg === 'string' || isBytes$1(sg);
2763
+ const isHex = typeof sg === 'string' || isBytes(sg);
2764
2764
  const isObj = !isHex &&
2765
2765
  !format &&
2766
2766
  typeof sg === 'object' &&
@@ -3120,228 +3120,6 @@ class RIPEMD160 extends HashMD {
3120
3120
  */
3121
3121
  const ripemd160 = /* @__PURE__ */ createHasher(() => new RIPEMD160());
3122
3122
 
3123
- /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3124
- function isBytes(a) {
3125
- return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
3126
- }
3127
- function isArrayOf(isString, arr) {
3128
- if (!Array.isArray(arr))
3129
- return false;
3130
- if (arr.length === 0)
3131
- return true;
3132
- if (isString) {
3133
- return arr.every((item) => typeof item === 'string');
3134
- }
3135
- else {
3136
- return arr.every((item) => Number.isSafeInteger(item));
3137
- }
3138
- }
3139
- // no abytes: seems to have 10% slowdown. Why?!
3140
- function afn(input) {
3141
- if (typeof input !== 'function')
3142
- throw new Error('function expected');
3143
- return true;
3144
- }
3145
- function astr(label, input) {
3146
- if (typeof input !== 'string')
3147
- throw new Error(`${label}: string expected`);
3148
- return true;
3149
- }
3150
- function anumber(n) {
3151
- if (!Number.isSafeInteger(n))
3152
- throw new Error(`invalid integer: ${n}`);
3153
- }
3154
- function aArr(input) {
3155
- if (!Array.isArray(input))
3156
- throw new Error('array expected');
3157
- }
3158
- function astrArr(label, input) {
3159
- if (!isArrayOf(true, input))
3160
- throw new Error(`${label}: array of strings expected`);
3161
- }
3162
- function anumArr(label, input) {
3163
- if (!isArrayOf(false, input))
3164
- throw new Error(`${label}: array of numbers expected`);
3165
- }
3166
- /**
3167
- * @__NO_SIDE_EFFECTS__
3168
- */
3169
- function chain(...args) {
3170
- const id = (a) => a;
3171
- // Wrap call in closure so JIT can inline calls
3172
- const wrap = (a, b) => (c) => a(b(c));
3173
- // Construct chain of args[-1].encode(args[-2].encode([...]))
3174
- const encode = args.map((x) => x.encode).reduceRight(wrap, id);
3175
- // Construct chain of args[0].decode(args[1].decode(...))
3176
- const decode = args.map((x) => x.decode).reduce(wrap, id);
3177
- return { encode, decode };
3178
- }
3179
- /**
3180
- * Encodes integer radix representation to array of strings using alphabet and back.
3181
- * Could also be array of strings.
3182
- * @__NO_SIDE_EFFECTS__
3183
- */
3184
- function alphabet(letters) {
3185
- // mapping 1 to "b"
3186
- const lettersA = typeof letters === 'string' ? letters.split('') : letters;
3187
- const len = lettersA.length;
3188
- astrArr('alphabet', lettersA);
3189
- // mapping "b" to 1
3190
- const indexes = new Map(lettersA.map((l, i) => [l, i]));
3191
- return {
3192
- encode: (digits) => {
3193
- aArr(digits);
3194
- return digits.map((i) => {
3195
- if (!Number.isSafeInteger(i) || i < 0 || i >= len)
3196
- throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
3197
- return lettersA[i];
3198
- });
3199
- },
3200
- decode: (input) => {
3201
- aArr(input);
3202
- return input.map((letter) => {
3203
- astr('alphabet.decode', letter);
3204
- const i = indexes.get(letter);
3205
- if (i === undefined)
3206
- throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
3207
- return i;
3208
- });
3209
- },
3210
- };
3211
- }
3212
- /**
3213
- * @__NO_SIDE_EFFECTS__
3214
- */
3215
- function join(separator = '') {
3216
- astr('join', separator);
3217
- return {
3218
- encode: (from) => {
3219
- astrArr('join.decode', from);
3220
- return from.join(separator);
3221
- },
3222
- decode: (to) => {
3223
- astr('join.decode', to);
3224
- return to.split(separator);
3225
- },
3226
- };
3227
- }
3228
- /**
3229
- * Slow: O(n^2) time complexity
3230
- */
3231
- function convertRadix(data, from, to) {
3232
- // base 1 is impossible
3233
- if (from < 2)
3234
- throw new Error(`convertRadix: invalid from=${from}, base cannot be less than 2`);
3235
- if (to < 2)
3236
- throw new Error(`convertRadix: invalid to=${to}, base cannot be less than 2`);
3237
- aArr(data);
3238
- if (!data.length)
3239
- return [];
3240
- let pos = 0;
3241
- const res = [];
3242
- const digits = Array.from(data, (d) => {
3243
- anumber(d);
3244
- if (d < 0 || d >= from)
3245
- throw new Error(`invalid integer: ${d}`);
3246
- return d;
3247
- });
3248
- const dlen = digits.length;
3249
- while (true) {
3250
- let carry = 0;
3251
- let done = true;
3252
- for (let i = pos; i < dlen; i++) {
3253
- const digit = digits[i];
3254
- const fromCarry = from * carry;
3255
- const digitBase = fromCarry + digit;
3256
- if (!Number.isSafeInteger(digitBase) ||
3257
- fromCarry / from !== carry ||
3258
- digitBase - digit !== fromCarry) {
3259
- throw new Error('convertRadix: carry overflow');
3260
- }
3261
- const div = digitBase / to;
3262
- carry = digitBase % to;
3263
- const rounded = Math.floor(div);
3264
- digits[i] = rounded;
3265
- if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase)
3266
- throw new Error('convertRadix: carry overflow');
3267
- if (!done)
3268
- continue;
3269
- else if (!rounded)
3270
- pos = i;
3271
- else
3272
- done = false;
3273
- }
3274
- res.push(carry);
3275
- if (done)
3276
- break;
3277
- }
3278
- for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
3279
- res.push(0);
3280
- return res.reverse();
3281
- }
3282
- /**
3283
- * @__NO_SIDE_EFFECTS__
3284
- */
3285
- function radix(num) {
3286
- anumber(num);
3287
- const _256 = 2 ** 8;
3288
- return {
3289
- encode: (bytes) => {
3290
- if (!isBytes(bytes))
3291
- throw new Error('radix.encode input should be Uint8Array');
3292
- return convertRadix(Array.from(bytes), _256, num);
3293
- },
3294
- decode: (digits) => {
3295
- anumArr('radix.decode', digits);
3296
- return Uint8Array.from(convertRadix(digits, num, _256));
3297
- },
3298
- };
3299
- }
3300
- function checksum(len, fn) {
3301
- anumber(len);
3302
- afn(fn);
3303
- return {
3304
- encode(data) {
3305
- if (!isBytes(data))
3306
- throw new Error('checksum.encode: input should be Uint8Array');
3307
- const sum = fn(data).slice(0, len);
3308
- const res = new Uint8Array(data.length + len);
3309
- res.set(data);
3310
- res.set(sum, data.length);
3311
- return res;
3312
- },
3313
- decode(data) {
3314
- if (!isBytes(data))
3315
- throw new Error('checksum.decode: input should be Uint8Array');
3316
- const payload = data.slice(0, -len);
3317
- const oldChecksum = data.slice(-len);
3318
- const newChecksum = fn(payload).slice(0, len);
3319
- for (let i = 0; i < len; i++)
3320
- if (newChecksum[i] !== oldChecksum[i])
3321
- throw new Error('Invalid checksum');
3322
- return payload;
3323
- },
3324
- };
3325
- }
3326
- // base58 code
3327
- // -----------
3328
- const genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => chain(radix(58), alphabet(abc), join(''));
3329
- /**
3330
- * base58: base64 without ambigous characters +, /, 0, O, I, l.
3331
- * Quadratic (O(n^2)) - so, can't be used on large inputs.
3332
- * @example
3333
- * ```js
3334
- * base58.decode('01abcdef');
3335
- * // => '3UhJW'
3336
- * ```
3337
- */
3338
- const base58 = genBase58('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
3339
- /**
3340
- * Method, which creates base58check encoder.
3341
- * Requires function, calculating sha256.
3342
- */
3343
- const createBase58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), base58);
3344
-
3345
3123
  /**
3346
3124
  * @module BIP32 hierarchical deterministic (HD) wallets over secp256k1.
3347
3125
  * @example
package/lib/index.js CHANGED
@@ -4,7 +4,7 @@ var radixEngineToolkit = require('@radixdlt/radix-engine-toolkit');
4
4
  var xchainClient = require('@xchainjs/xchain-client');
5
5
  var xchainCrypto = require('@xchainjs/xchain-crypto');
6
6
  var xchainUtil = require('@xchainjs/xchain-util');
7
- var bech32 = require('bech32');
7
+ var base = require('@scure/base');
8
8
  var ed25519HdKey = require('ed25519-hd-key');
9
9
  var babylonGatewayApiSdk = require('@radixdlt/babylon-gateway-api-sdk');
10
10
 
@@ -54,17 +54,17 @@ const crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? global
54
54
  // Makes the utils un-importable in browsers without a bundler.
55
55
  // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
56
56
  /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
57
- function isBytes$1(a) {
57
+ function isBytes(a) {
58
58
  return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
59
59
  }
60
60
  /** Asserts something is positive integer. */
61
- function anumber$1(n) {
61
+ function anumber(n) {
62
62
  if (!Number.isSafeInteger(n) || n < 0)
63
63
  throw new Error('positive integer expected, got ' + n);
64
64
  }
65
65
  /** Asserts something is Uint8Array. */
66
66
  function abytes(b, ...lengths) {
67
- if (!isBytes$1(b))
67
+ if (!isBytes(b))
68
68
  throw new Error('Uint8Array expected');
69
69
  if (lengths.length > 0 && !lengths.includes(b.length))
70
70
  throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
@@ -73,8 +73,8 @@ function abytes(b, ...lengths) {
73
73
  function ahash(h) {
74
74
  if (typeof h !== 'function' || typeof h.create !== 'function')
75
75
  throw new Error('Hash should be wrapped by utils.createHasher');
76
- anumber$1(h.outputLen);
77
- anumber$1(h.blockLen);
76
+ anumber(h.outputLen);
77
+ anumber(h.blockLen);
78
78
  }
79
79
  /** Asserts a hash instance has not been destroyed / finished */
80
80
  function aexists(instance, checkFinished = true) {
@@ -282,7 +282,7 @@ function ensureBytes(title, hex, expectedLength) {
282
282
  throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);
283
283
  }
284
284
  }
285
- else if (isBytes$1(hex)) {
285
+ else if (isBytes(hex)) {
286
286
  // Uint8Array.from() instead of hash.slice() because node.js Buffer
287
287
  // is instance of Uint8Array, and its slice() creates **mutable** copy
288
288
  res = Uint8Array.from(hex);
@@ -716,7 +716,7 @@ function FpLegendre(Fp, n) {
716
716
  function nLength(n, nBitLength) {
717
717
  // Bit size, byte size of CURVE.n
718
718
  if (nBitLength !== undefined)
719
- anumber$1(nBitLength);
719
+ anumber(nBitLength);
720
720
  const _nBitLength = nBitLength !== undefined ? nBitLength : n.toString(2).length;
721
721
  const nByteLength = Math.ceil(_nBitLength / 8);
722
722
  return { nBitLength: _nBitLength, nByteLength };
@@ -2762,7 +2762,7 @@ function ecdsa(Point, ecdsaOpts, curveOpts = {}) {
2762
2762
  throw new Error('options.strict was renamed to lowS');
2763
2763
  if (format !== undefined && !['compact', 'der', 'js'].includes(format))
2764
2764
  throw new Error('format must be "compact", "der" or "js"');
2765
- const isHex = typeof sg === 'string' || isBytes$1(sg);
2765
+ const isHex = typeof sg === 'string' || isBytes(sg);
2766
2766
  const isObj = !isHex &&
2767
2767
  !format &&
2768
2768
  typeof sg === 'object' &&
@@ -3122,228 +3122,6 @@ class RIPEMD160 extends HashMD {
3122
3122
  */
3123
3123
  const ripemd160 = /* @__PURE__ */ createHasher(() => new RIPEMD160());
3124
3124
 
3125
- /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
3126
- function isBytes(a) {
3127
- return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
3128
- }
3129
- function isArrayOf(isString, arr) {
3130
- if (!Array.isArray(arr))
3131
- return false;
3132
- if (arr.length === 0)
3133
- return true;
3134
- if (isString) {
3135
- return arr.every((item) => typeof item === 'string');
3136
- }
3137
- else {
3138
- return arr.every((item) => Number.isSafeInteger(item));
3139
- }
3140
- }
3141
- // no abytes: seems to have 10% slowdown. Why?!
3142
- function afn(input) {
3143
- if (typeof input !== 'function')
3144
- throw new Error('function expected');
3145
- return true;
3146
- }
3147
- function astr(label, input) {
3148
- if (typeof input !== 'string')
3149
- throw new Error(`${label}: string expected`);
3150
- return true;
3151
- }
3152
- function anumber(n) {
3153
- if (!Number.isSafeInteger(n))
3154
- throw new Error(`invalid integer: ${n}`);
3155
- }
3156
- function aArr(input) {
3157
- if (!Array.isArray(input))
3158
- throw new Error('array expected');
3159
- }
3160
- function astrArr(label, input) {
3161
- if (!isArrayOf(true, input))
3162
- throw new Error(`${label}: array of strings expected`);
3163
- }
3164
- function anumArr(label, input) {
3165
- if (!isArrayOf(false, input))
3166
- throw new Error(`${label}: array of numbers expected`);
3167
- }
3168
- /**
3169
- * @__NO_SIDE_EFFECTS__
3170
- */
3171
- function chain(...args) {
3172
- const id = (a) => a;
3173
- // Wrap call in closure so JIT can inline calls
3174
- const wrap = (a, b) => (c) => a(b(c));
3175
- // Construct chain of args[-1].encode(args[-2].encode([...]))
3176
- const encode = args.map((x) => x.encode).reduceRight(wrap, id);
3177
- // Construct chain of args[0].decode(args[1].decode(...))
3178
- const decode = args.map((x) => x.decode).reduce(wrap, id);
3179
- return { encode, decode };
3180
- }
3181
- /**
3182
- * Encodes integer radix representation to array of strings using alphabet and back.
3183
- * Could also be array of strings.
3184
- * @__NO_SIDE_EFFECTS__
3185
- */
3186
- function alphabet(letters) {
3187
- // mapping 1 to "b"
3188
- const lettersA = typeof letters === 'string' ? letters.split('') : letters;
3189
- const len = lettersA.length;
3190
- astrArr('alphabet', lettersA);
3191
- // mapping "b" to 1
3192
- const indexes = new Map(lettersA.map((l, i) => [l, i]));
3193
- return {
3194
- encode: (digits) => {
3195
- aArr(digits);
3196
- return digits.map((i) => {
3197
- if (!Number.isSafeInteger(i) || i < 0 || i >= len)
3198
- throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
3199
- return lettersA[i];
3200
- });
3201
- },
3202
- decode: (input) => {
3203
- aArr(input);
3204
- return input.map((letter) => {
3205
- astr('alphabet.decode', letter);
3206
- const i = indexes.get(letter);
3207
- if (i === undefined)
3208
- throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
3209
- return i;
3210
- });
3211
- },
3212
- };
3213
- }
3214
- /**
3215
- * @__NO_SIDE_EFFECTS__
3216
- */
3217
- function join(separator = '') {
3218
- astr('join', separator);
3219
- return {
3220
- encode: (from) => {
3221
- astrArr('join.decode', from);
3222
- return from.join(separator);
3223
- },
3224
- decode: (to) => {
3225
- astr('join.decode', to);
3226
- return to.split(separator);
3227
- },
3228
- };
3229
- }
3230
- /**
3231
- * Slow: O(n^2) time complexity
3232
- */
3233
- function convertRadix(data, from, to) {
3234
- // base 1 is impossible
3235
- if (from < 2)
3236
- throw new Error(`convertRadix: invalid from=${from}, base cannot be less than 2`);
3237
- if (to < 2)
3238
- throw new Error(`convertRadix: invalid to=${to}, base cannot be less than 2`);
3239
- aArr(data);
3240
- if (!data.length)
3241
- return [];
3242
- let pos = 0;
3243
- const res = [];
3244
- const digits = Array.from(data, (d) => {
3245
- anumber(d);
3246
- if (d < 0 || d >= from)
3247
- throw new Error(`invalid integer: ${d}`);
3248
- return d;
3249
- });
3250
- const dlen = digits.length;
3251
- while (true) {
3252
- let carry = 0;
3253
- let done = true;
3254
- for (let i = pos; i < dlen; i++) {
3255
- const digit = digits[i];
3256
- const fromCarry = from * carry;
3257
- const digitBase = fromCarry + digit;
3258
- if (!Number.isSafeInteger(digitBase) ||
3259
- fromCarry / from !== carry ||
3260
- digitBase - digit !== fromCarry) {
3261
- throw new Error('convertRadix: carry overflow');
3262
- }
3263
- const div = digitBase / to;
3264
- carry = digitBase % to;
3265
- const rounded = Math.floor(div);
3266
- digits[i] = rounded;
3267
- if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase)
3268
- throw new Error('convertRadix: carry overflow');
3269
- if (!done)
3270
- continue;
3271
- else if (!rounded)
3272
- pos = i;
3273
- else
3274
- done = false;
3275
- }
3276
- res.push(carry);
3277
- if (done)
3278
- break;
3279
- }
3280
- for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
3281
- res.push(0);
3282
- return res.reverse();
3283
- }
3284
- /**
3285
- * @__NO_SIDE_EFFECTS__
3286
- */
3287
- function radix(num) {
3288
- anumber(num);
3289
- const _256 = 2 ** 8;
3290
- return {
3291
- encode: (bytes) => {
3292
- if (!isBytes(bytes))
3293
- throw new Error('radix.encode input should be Uint8Array');
3294
- return convertRadix(Array.from(bytes), _256, num);
3295
- },
3296
- decode: (digits) => {
3297
- anumArr('radix.decode', digits);
3298
- return Uint8Array.from(convertRadix(digits, num, _256));
3299
- },
3300
- };
3301
- }
3302
- function checksum(len, fn) {
3303
- anumber(len);
3304
- afn(fn);
3305
- return {
3306
- encode(data) {
3307
- if (!isBytes(data))
3308
- throw new Error('checksum.encode: input should be Uint8Array');
3309
- const sum = fn(data).slice(0, len);
3310
- const res = new Uint8Array(data.length + len);
3311
- res.set(data);
3312
- res.set(sum, data.length);
3313
- return res;
3314
- },
3315
- decode(data) {
3316
- if (!isBytes(data))
3317
- throw new Error('checksum.decode: input should be Uint8Array');
3318
- const payload = data.slice(0, -len);
3319
- const oldChecksum = data.slice(-len);
3320
- const newChecksum = fn(payload).slice(0, len);
3321
- for (let i = 0; i < len; i++)
3322
- if (newChecksum[i] !== oldChecksum[i])
3323
- throw new Error('Invalid checksum');
3324
- return payload;
3325
- },
3326
- };
3327
- }
3328
- // base58 code
3329
- // -----------
3330
- const genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => chain(radix(58), alphabet(abc), join(''));
3331
- /**
3332
- * base58: base64 without ambigous characters +, /, 0, O, I, l.
3333
- * Quadratic (O(n^2)) - so, can't be used on large inputs.
3334
- * @example
3335
- * ```js
3336
- * base58.decode('01abcdef');
3337
- * // => '3UhJW'
3338
- * ```
3339
- */
3340
- const base58 = genBase58('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
3341
- /**
3342
- * Method, which creates base58check encoder.
3343
- * Requires function, calculating sha256.
3344
- */
3345
- const createBase58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), base58);
3346
-
3347
3125
  /**
3348
3126
  * @module BIP32 hierarchical deterministic (HD) wallets over secp256k1.
3349
3127
  * @example
@@ -3363,7 +3141,7 @@ const createBase58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(
3363
3141
  */
3364
3142
  /*! scure-bip32 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) */
3365
3143
  const Point = secp256k1.ProjectivePoint;
3366
- const base58check = createBase58check(sha256);
3144
+ const base58check = base.createBase58check(sha256);
3367
3145
  function bytesToNumber(bytes) {
3368
3146
  abytes(bytes);
3369
3147
  const h = bytes.length === 0 ? '0' : bytesToHex(bytes);
@@ -4152,7 +3930,7 @@ class Client extends xchainClient.BaseXChainClient {
4152
3930
  }
4153
3931
  validateAddress(address) {
4154
3932
  try {
4155
- const decodedAddress = bech32.bech32m.decode(address);
3933
+ const decodedAddress = base.bech32m.decode(address);
4156
3934
  if (!decodedAddress.prefix.startsWith('account_')) {
4157
3935
  return false;
4158
3936
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-radix",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Custom Radix client and utilities used by XChainJS clients",
5
5
  "keywords": [
6
6
  "XRD",
@@ -40,10 +40,10 @@
40
40
  "dependencies": {
41
41
  "@radixdlt/babylon-gateway-api-sdk": "^1.4.1",
42
42
  "@radixdlt/radix-engine-toolkit": "^1.0.3",
43
- "@xchainjs/xchain-client": "2.0.3",
44
- "@xchainjs/xchain-crypto": "1.0.1",
45
- "@xchainjs/xchain-util": "2.0.2",
46
- "bech32": "^2.0.0",
43
+ "@scure/base": "^1.2.6",
44
+ "@xchainjs/xchain-client": "2.0.4",
45
+ "@xchainjs/xchain-crypto": "1.0.2",
46
+ "@xchainjs/xchain-util": "2.0.3",
47
47
  "bip32": "^4.0.0",
48
48
  "bip39": "^3.1.0",
49
49
  "ed25519-hd-key": "^1.3.0"