@bitpay-labs/crypto-wallet-core 11.11.4 → 11.11.6
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/package.json +9 -3
- package/ts_build/src/constants/index.d.ts +18 -1
- package/ts_build/src/constants/index.d.ts.map +1 -1
- package/ts_build/src/constants/tokens.d.ts +14 -2
- package/ts_build/src/constants/tokens.d.ts.map +1 -1
- package/ts_build/src/constants/tokens.js +8 -2
- package/ts_build/src/constants/tokens.js.map +1 -1
- package/ts_build/src/constants/units.d.ts +11 -0
- package/ts_build/src/constants/units.d.ts.map +1 -1
- package/ts_build/src/constants/units.js +11 -0
- package/ts_build/src/constants/units.js.map +1 -1
- package/ts_build/src/transactions/index.d.ts +1 -1
- package/ts_build/src/transactions/index.d.ts.map +1 -1
- package/ts_build/src/transactions/index.js.map +1 -1
- package/ts_build/src/types/derivation.d.ts +1 -0
- package/ts_build/src/types/derivation.d.ts.map +1 -1
- package/.mocharc.json +0 -6
- package/LICENSE +0 -21
- package/mcr.config.js +0 -17
- package/src/constants/address.ts +0 -7
- package/src/constants/algorithms.ts +0 -9
- package/src/constants/arc.ts +0 -4
- package/src/constants/chains.ts +0 -68
- package/src/constants/feeMinimums.ts +0 -23
- package/src/constants/index.ts +0 -40
- package/src/constants/tokens.ts +0 -227
- package/src/constants/units.ts +0 -300
- package/src/derivation/arb/index.ts +0 -3
- package/src/derivation/base/index.ts +0 -3
- package/src/derivation/bch/index.ts +0 -11
- package/src/derivation/btc/index.ts +0 -68
- package/src/derivation/doge/index.ts +0 -6
- package/src/derivation/eth/index.ts +0 -86
- package/src/derivation/index.ts +0 -140
- package/src/derivation/ltc/index.ts +0 -6
- package/src/derivation/matic/index.ts +0 -3
- package/src/derivation/op/index.ts +0 -3
- package/src/derivation/paths.ts +0 -45
- package/src/derivation/sol/index.ts +0 -81
- package/src/derivation/xrp/index.ts +0 -63
- package/src/index.ts +0 -48
- package/src/message/btc.ts +0 -28
- package/src/message/eth.ts +0 -47
- package/src/message/index.ts +0 -71
- package/src/message/sol.ts +0 -3
- package/src/message/xrp.ts +0 -4
- package/src/transactions/arb/index.ts +0 -14
- package/src/transactions/base/index.ts +0 -13
- package/src/transactions/bch/index.ts +0 -25
- package/src/transactions/btc/index.ts +0 -316
- package/src/transactions/doge/index.ts +0 -31
- package/src/transactions/erc20/abi.ts +0 -418
- package/src/transactions/erc20/index.ts +0 -57
- package/src/transactions/eth/index.ts +0 -192
- package/src/transactions/eth-multisig/abi.ts +0 -724
- package/src/transactions/eth-multisig/index.ts +0 -172
- package/src/transactions/index.ts +0 -85
- package/src/transactions/ltc/index.ts +0 -31
- package/src/transactions/matic/index.ts +0 -14
- package/src/transactions/matic-multisig/index.ts +0 -172
- package/src/transactions/op/index.ts +0 -14
- package/src/transactions/sol/index.ts +0 -306
- package/src/transactions/spl/index.ts +0 -192
- package/src/transactions/xrp/index.ts +0 -167
- package/src/types/derivation.ts +0 -34
- package/src/types/index.ts +0 -3
- package/src/types/message.ts +0 -15
- package/src/types/utils.ts +0 -1
- package/src/types/validation.ts +0 -4
- package/src/utils/bigint.ts +0 -238
- package/src/utils/index.ts +0 -188
- package/src/validation/arb/index.ts +0 -8
- package/src/validation/arc/index.ts +0 -8
- package/src/validation/base/index.ts +0 -8
- package/src/validation/bch/index.ts +0 -17
- package/src/validation/btc/index.ts +0 -17
- package/src/validation/doge/index.ts +0 -16
- package/src/validation/eth/index.ts +0 -29
- package/src/validation/index.ts +0 -45
- package/src/validation/ltc/index.ts +0 -16
- package/src/validation/matic/index.ts +0 -8
- package/src/validation/op/index.ts +0 -8
- package/src/validation/sol/index.ts +0 -34
- package/src/validation/xrp/index.ts +0 -50
- package/test/address.test.ts +0 -190
- package/test/bigint.test.ts +0 -373
- package/test/deriver.test.ts +0 -126
- package/test/transactions.test.ts +0 -2103
- package/test/utils.test.ts +0 -446
- package/test/validation.test.ts +0 -282
- package/tsconfig.json +0 -29
package/src/types/derivation.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export interface Key {
|
|
2
|
-
address: string;
|
|
3
|
-
privKey?: string;
|
|
4
|
-
pubKey?: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface IDeriver {
|
|
8
|
-
deriveAddress(network: string, xPub: string, addressIndex: number, isChange: boolean, addressType?: string): string;
|
|
9
|
-
|
|
10
|
-
derivePrivateKey(network: string, xPriv: string, addressIndex: number, isChange: boolean, addressType?: string): Key;
|
|
11
|
-
|
|
12
|
-
deriveAddressWithPath(network: string, xpubKey: string, path: string, addressType: string): string;
|
|
13
|
-
|
|
14
|
-
derivePrivateKeyWithPath(network: string, xprivKey: string, path: string, addressType: string): Key;
|
|
15
|
-
|
|
16
|
-
getAddress(network: string, pubKey, addressType: string): string;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Derive the public key for a given chain-native private key representation.
|
|
20
|
-
* Used when importing plaintext private keys that may not include `pubKey`.
|
|
21
|
-
* Caller should clean up buffer after use
|
|
22
|
-
*/
|
|
23
|
-
getPublicKey(network: string, privKey: Buffer): string;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Used to normalize output of Key.privKey
|
|
27
|
-
*/
|
|
28
|
-
privateKeyToBuffer(privKey: any): Buffer;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Temporary - converts decrypted private key buffer to lib-specific private key format
|
|
32
|
-
*/
|
|
33
|
-
bufferToPrivateKey_TEMP(buf: Buffer, network: string): string;
|
|
34
|
-
}
|
package/src/types/index.ts
DELETED
package/src/types/message.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type BitcoreLib from '@bitpay-labs/bitcore-lib';
|
|
2
|
-
|
|
3
|
-
export type KeyType = string | BitcoreLib.PrivateKey;
|
|
4
|
-
export type HDKeyType = string | BitcoreLib.HDPrivateKey;
|
|
5
|
-
export type Encoding = BufferEncoding | 'base58';
|
|
6
|
-
export interface ISignedMessage<T = Buffer | string> {
|
|
7
|
-
signature: T;
|
|
8
|
-
publicKey: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IMessageClass {
|
|
12
|
-
getMessageHash(args: { message: string; encoding?: Encoding }): Buffer | string;
|
|
13
|
-
signMessage(args: { message: string; privateKey: KeyType; encoding?: Encoding }): ISignedMessage;
|
|
14
|
-
signMessageWithPath(args: { message: string; hdPrivateKey: HDKeyType; derivationPath: string; encoding?: Encoding }): ISignedMessage;
|
|
15
|
-
}
|
package/src/types/utils.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type BigIntLike = bigint | number | string;
|
package/src/types/validation.ts
DELETED
package/src/utils/bigint.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import type { BigIntLike } from '../types/utils';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function isValidDenominator(val: BigIntLike): boolean {
|
|
5
|
-
return isBigIntLike(val) && BigInt(val) !== 0n;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Checks if a value is a BigInt-like type (bigint, number, or numeric string)
|
|
10
|
-
* @param value
|
|
11
|
-
* @returns {boolean}
|
|
12
|
-
*/
|
|
13
|
-
export function isBigIntLike(value?: any): boolean {
|
|
14
|
-
return value != null &&
|
|
15
|
-
value !== '' &&
|
|
16
|
-
(
|
|
17
|
-
typeof value === 'bigint' ||
|
|
18
|
-
(typeof value === 'number' && !isNaN(value)) ||
|
|
19
|
-
!isNaN(Number(value))
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* This is a special wrapper for BigInt() that assumes a string _without_ a prefix is hex.
|
|
25
|
-
* @example sToBigInt('0x12') => 18n
|
|
26
|
-
* @example sToBigInt('12') => 18n // assumes hex
|
|
27
|
-
* @example sToBigInt(12) => 12n
|
|
28
|
-
* @example sToBigInt('0o12') => 10n // octal
|
|
29
|
-
* @returns {bigint}
|
|
30
|
-
*/
|
|
31
|
-
export function sToBigInt(value: number | string): bigint {
|
|
32
|
-
const isNonPrefixedString = typeof value === 'string' &&
|
|
33
|
-
!value?.startsWith('0x') &&
|
|
34
|
-
!value?.startsWith('0o');
|
|
35
|
-
return BigInt(isNonPrefixedString ? ('0x' + value) : value);
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Math.max() for a mixed array of BigInts, Numbers, and Strings
|
|
40
|
-
* @param {Array<T = BigIntLike>} arr Array of T (T by default can be a mix of BigInts, Numbers, and/or Strings)
|
|
41
|
-
* @returns {T = BigIntLike} Returns the max entry
|
|
42
|
-
*/
|
|
43
|
-
export function max<T = BigIntLike>(arr: Array<T>): T {
|
|
44
|
-
if (!Array.isArray(arr)) throw new Error('Input must be an array');
|
|
45
|
-
if (!arr.every(isBigIntLike)) throw new Error('Array must contain only BigInt-like values');
|
|
46
|
-
return arr.reduce((max, cur) => cur > max ? cur : max, arr[0]);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Math.min() for a mixed array of BigInts, Numbers, and Strings
|
|
52
|
-
* @param {Array<T = BigIntLike>} arr Array of T (T by default can be a mix of BigInts, Numbers, and/or Strings)
|
|
53
|
-
* @returns {T = BigIntLike} Returns the min entry
|
|
54
|
-
*/
|
|
55
|
-
export function min<T = BigIntLike>(arr: Array<T>): T {
|
|
56
|
-
if (!Array.isArray(arr)) throw new Error('Input must be an array');
|
|
57
|
-
if (!arr.every(isBigIntLike)) throw new Error('Array must contain only BigInt-like values');
|
|
58
|
-
return arr.reduce((min, cur) => cur < min ? cur : min, arr[0]);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Divide BigInts with precision. This works for Numbers and strings as well since it converts the inputs to BigInts
|
|
64
|
-
* @param {BigIntLike} numerator
|
|
65
|
-
* @param {BigIntLike} denominator
|
|
66
|
-
* @param {BigIntLike} precision (optional) Default: 18
|
|
67
|
-
* @returns {number}
|
|
68
|
-
*/
|
|
69
|
-
export function divToFloat(numerator: BigIntLike, denominator: BigIntLike, precision?: number) {
|
|
70
|
-
if (!isBigIntLike(numerator)) throw new Error('Invalid numerator');
|
|
71
|
-
if (!isBigIntLike(denominator)) throw new Error('Invalid denominator');
|
|
72
|
-
if (!isValidDenominator(denominator)) throw new Error('Division by zero');
|
|
73
|
-
const [nLeft, nRightInitial = ''] = numerator.toString().split('.');
|
|
74
|
-
let nRight = nRightInitial;
|
|
75
|
-
const [, dRight = ''] = denominator.toString().split('.');
|
|
76
|
-
if (precision == null) {
|
|
77
|
-
precision = Math.max(nRight.length, dRight.length, 18);
|
|
78
|
-
}
|
|
79
|
-
nRight = nRight.padEnd(precision, '0');
|
|
80
|
-
const scaledNumerator = BigInt(nLeft + nRight);
|
|
81
|
-
const quotient = scaledNumerator / BigInt(denominator);
|
|
82
|
-
return Number(quotient) / (10 ** precision);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Divide BigInts with precision. This works for Numbers and strings as well since it converts the inputs to BigInts
|
|
88
|
-
* @param {BigIntLike} numerator
|
|
89
|
-
* @param {BigIntLike} denominator
|
|
90
|
-
* @returns {bigint} Rounded BigInt
|
|
91
|
-
*/
|
|
92
|
-
export function div(numerator: BigIntLike, denominator: BigIntLike): bigint {
|
|
93
|
-
if (!isBigIntLike(numerator)) throw new Error('Invalid numerator');
|
|
94
|
-
if (!isBigIntLike(denominator)) throw new Error('Invalid denominator');
|
|
95
|
-
if (!isValidDenominator(denominator)) throw new Error('Division by zero');
|
|
96
|
-
const [nLeft, nRightInitial = ''] = numerator.toString().split('.');
|
|
97
|
-
let nRight = nRightInitial;
|
|
98
|
-
const [, dRight = ''] = denominator.toString().split('.');
|
|
99
|
-
const precision = Math.max(nRight.length, dRight.length, 18);
|
|
100
|
-
nRight = nRight.padEnd(precision, '0');
|
|
101
|
-
|
|
102
|
-
const scaledNumerator = BigInt(nLeft + nRight);
|
|
103
|
-
const quotient = (scaledNumerator / BigInt(denominator)).toString();
|
|
104
|
-
return BigInt(quotient.slice(0, -precision)) + BigInt(Math.round(Number('.' + quotient.slice(-precision))));
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Multiply mixed numbers. If decimals are given, this maintains precision of any given decimals until the very end to get the most precise result.
|
|
110
|
-
* @example mul(2n, 1.4) => 2.8 => 3n
|
|
111
|
-
* @example mul(1n, 1.1) => 1.1 => 1n
|
|
112
|
-
*
|
|
113
|
-
* @param {...BigIntLike} nums
|
|
114
|
-
* @returns {bigint} Returns a rounded bigint
|
|
115
|
-
*/
|
|
116
|
-
export function mul(...nums: Array<BigIntLike>): bigint {
|
|
117
|
-
if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
|
|
118
|
-
const precision = nums.reduce<number>((p: number, num: BigIntLike) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
|
|
119
|
-
|
|
120
|
-
if (precision == 0) { // all numbers are integers
|
|
121
|
-
return nums.reduce<bigint>((acc: bigint, cur: BigIntLike) => acc * BigInt(cur), 1n);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
let productScaled = 1n;
|
|
125
|
-
for (const cur of nums) {
|
|
126
|
-
const [left, rightInitial = ''] = cur.toString().split('.');
|
|
127
|
-
const right = rightInitial.padEnd(precision, '0');
|
|
128
|
-
productScaled *= BigInt(left + right);
|
|
129
|
-
}
|
|
130
|
-
const whole = productScaled.toString().slice(0, -(precision * nums.length));
|
|
131
|
-
const decimal = productScaled.toString().slice(-(precision * nums.length));
|
|
132
|
-
return BigInt(whole) + BigInt(Math.round(Number('.' + decimal)));
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Same as mul() but returns the floor of the result
|
|
138
|
-
* @example mul(2n, 1.4) => 2.8 => 2n
|
|
139
|
-
* @example mul(1n, 1.1) => 1.1 => 1n
|
|
140
|
-
*
|
|
141
|
-
* @param {...BigIntLike} nums
|
|
142
|
-
* @returns {bigint} Returns a floor-rounded BigInt
|
|
143
|
-
*/
|
|
144
|
-
export function mulFloor(...nums: Array<BigIntLike>): bigint {
|
|
145
|
-
if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
|
|
146
|
-
const precision = nums.reduce<number>((p: number, num: BigIntLike) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
|
|
147
|
-
|
|
148
|
-
if (precision == 0) {
|
|
149
|
-
return nums.reduce<bigint>((acc: bigint, cur: BigIntLike) => acc * BigInt(cur), 1n);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
let productScaled = 1n;
|
|
153
|
-
for (const cur of nums) {
|
|
154
|
-
const [left, rightInitial = ''] = cur.toString().split('.');
|
|
155
|
-
const right = rightInitial.padEnd(precision, '0');
|
|
156
|
-
productScaled *= BigInt(left + right);
|
|
157
|
-
}
|
|
158
|
-
const whole = productScaled.toString().slice(0, -(precision * nums.length));
|
|
159
|
-
// disregard the decimal since this is a floor
|
|
160
|
-
return BigInt(whole);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Same as mul() but returns the ceiling of the result
|
|
166
|
-
* @example mul(2n, 1.4) => 2.8 => 3n
|
|
167
|
-
* @example mul(1n, 1.1) => 1.1 => 2n
|
|
168
|
-
*
|
|
169
|
-
* @param {...BigIntLike} nums
|
|
170
|
-
* @returns {bigint} Returns a ceiling-rounded BigInt
|
|
171
|
-
*/
|
|
172
|
-
export function mulCeil(...nums: Array<BigIntLike>): bigint {
|
|
173
|
-
if (!nums.length || !nums.every(isBigIntLike)) throw new Error('Input must contain only BigInt-like values');
|
|
174
|
-
const precision = nums.reduce<number>((p, num) => Math.max(p, num.toString().split('.')[1]?.length || 0), 0);
|
|
175
|
-
|
|
176
|
-
if (precision == 0) { // All nums are integers, so can just convert all to bigints
|
|
177
|
-
return nums.reduce<bigint>((acc, cur) => acc * BigInt(cur), 1n);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
let productScaled = 1n;
|
|
181
|
-
for (const cur of nums) {
|
|
182
|
-
const [left, rightInitial = ''] = cur.toString().split('.');
|
|
183
|
-
const right = rightInitial.padEnd(precision, '0');
|
|
184
|
-
productScaled *= BigInt(left + right);
|
|
185
|
-
}
|
|
186
|
-
const whole = productScaled.toString().slice(0, -(precision * nums.length));
|
|
187
|
-
const decimal = productScaled.toString().slice(-(precision * nums.length));
|
|
188
|
-
return BigInt(whole) + (BigInt(decimal) > 0n ? 1n : 0n);
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* To be used with JSON.stringify to convert BigInt values to a serializable format
|
|
194
|
-
* @example
|
|
195
|
-
* const obj = { a: 123n, b: 'test' };
|
|
196
|
-
* JSON.stringify(obj, JSONStringifyBigIntReplacer);
|
|
197
|
-
* // Result: '{"a":{"data":"123","type":"BigInt"},"b":"test"}'
|
|
198
|
-
*/
|
|
199
|
-
export function JSONStringifyBigIntReplacer(_key: string, value: any) {
|
|
200
|
-
return typeof value === 'bigint' ? { data: value.toString(), type: 'BigInt' } : value;
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* To be used with JSON.parse to revive BigInt values from the serializable format
|
|
206
|
-
* @example
|
|
207
|
-
* const str = '{"a":{"data":"123","type":"BigInt"},"b":"test"}';
|
|
208
|
-
* JSON.parse(str, JSONParseBigIntReviver);
|
|
209
|
-
* // Result: { a: 123n, b: 'test' }
|
|
210
|
-
*/
|
|
211
|
-
export function JSONParseBigIntReviver(_key: string, value: any) {
|
|
212
|
-
if (value && typeof value === 'object' && value.type === 'BigInt' && typeof value.data === 'string') {
|
|
213
|
-
return BigInt(value.data);
|
|
214
|
-
}
|
|
215
|
-
return value;
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Converts any BigInt values in an object to Numbers recursively
|
|
221
|
-
* @param {object|array} obj Object or Array to be scrubbed
|
|
222
|
-
* @param {'number'|'string'|'hex'} [destType='number'] Destination type for BigInt conversion
|
|
223
|
-
* @returns {object|array}
|
|
224
|
-
*/
|
|
225
|
-
export function scrubBigIntsInObject<T>(obj: T, destType: 'number' | 'string' | 'hex' = 'number'): T {
|
|
226
|
-
if (!obj || typeof obj !== 'object') {
|
|
227
|
-
return obj;
|
|
228
|
-
}
|
|
229
|
-
const toDestType = destType === 'number' ? Number : destType === 'string' ? (val) => val.toString() : (val) => '0x' + val.toString(16);
|
|
230
|
-
for (const [key, val] of Object.entries(obj)) {
|
|
231
|
-
if (typeof val === 'bigint') {
|
|
232
|
-
obj[key] = toDestType(val);
|
|
233
|
-
} else if (val && typeof val === 'object') {
|
|
234
|
-
obj[key] = scrubBigIntsInObject(val, destType);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return obj;
|
|
238
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import BitcoreLib from '@bitpay-labs/bitcore-lib';
|
|
2
|
-
import * as xrpl from 'xrpl';
|
|
3
|
-
import { Constants } from '../constants';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export * as BI from './bigint';
|
|
7
|
-
|
|
8
|
-
export function isNativeSegwit(addressType) {
|
|
9
|
-
return [
|
|
10
|
-
Constants.SCRIPT_TYPES.P2WPKH,
|
|
11
|
-
Constants.SCRIPT_TYPES.P2WSH,
|
|
12
|
-
Constants.SCRIPT_TYPES.P2TR,
|
|
13
|
-
].includes(addressType);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function getSegwitVersion(addressType) {
|
|
17
|
-
switch (addressType) {
|
|
18
|
-
case Constants.SCRIPT_TYPES.P2WPKH:
|
|
19
|
-
case Constants.SCRIPT_TYPES.P2WSH:
|
|
20
|
-
return 0;
|
|
21
|
-
case Constants.SCRIPT_TYPES.P2TR:
|
|
22
|
-
return 1;
|
|
23
|
-
default:
|
|
24
|
-
return undefined; // non-segwit addressType
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isUtxoChain(chain: string): boolean {
|
|
29
|
-
return Constants.UTXO_CHAINS.includes(chain.toLowerCase());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function isSvmChain(chain: string): boolean {
|
|
33
|
-
return Constants.SVM_CHAINS.includes(chain.toLowerCase());
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function isEvmChain(chain: string): boolean {
|
|
37
|
-
return Constants.EVM_CHAINS.includes(chain.toLowerCase());
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function isXrpChain(chain: string): boolean {
|
|
41
|
-
return ['xrp'].includes(chain.toLowerCase());
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function isSingleAddressChain(chain: string): boolean {
|
|
45
|
-
return !isUtxoChain(chain);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function encodeBuffer(buffer: Buffer, encoding: BufferEncoding | 'base58'): Buffer | string {
|
|
49
|
-
if (!encoding) {
|
|
50
|
-
return buffer;
|
|
51
|
-
} else if (encoding === 'base58') {
|
|
52
|
-
return BitcoreLib.encoding.Base58.encode(buffer);
|
|
53
|
-
} else {
|
|
54
|
-
return buffer.toString(encoding);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Evaluates if the given string is a valid hex string (with or without 0x prefix)
|
|
60
|
-
* @param {string} str The string to evaluate
|
|
61
|
-
* @returns {boolean} True if the string is a valid hex string (with or without 0x prefix)
|
|
62
|
-
*/
|
|
63
|
-
export function isHexString(str: string): boolean {
|
|
64
|
-
if (typeof str !== 'string' || str === '') {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
let normalizedStr = str.toLowerCase().slice(0, 2) === '0x' ? str.toLowerCase().slice(2) : str.toLowerCase();
|
|
68
|
-
if (normalizedStr.length % 2) {
|
|
69
|
-
normalizedStr = '0' + normalizedStr; // add leading zero if odd length
|
|
70
|
-
}
|
|
71
|
-
return Buffer.from(normalizedStr, 'hex').toString('hex') === normalizedStr;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Converts a number, string, or bigint to a hex string with 0x prefix
|
|
76
|
-
* This function makes the following assumption(s):
|
|
77
|
-
* 1. If the input is a non-prefixed numeric string (e.g. '123'), it is assumed to be a decimal number
|
|
78
|
-
* 2. If the input is a non-prefixed hex string (e.g. '123a'), it is assumed to be a hex number (as opposed to a utf8 string to be converted to hex)
|
|
79
|
-
* @param {number|string|bigint} input The input number, string, or bigint
|
|
80
|
-
* @returns {string} The hex string with 0x prefix
|
|
81
|
-
*/
|
|
82
|
-
export function toHex(input: number | string | bigint): string {
|
|
83
|
-
if ((typeof input !== 'number' && typeof input !== 'string' && typeof input !== 'bigint') || input === '') {
|
|
84
|
-
throw new Error(`Input for toHex must be a number, string (non-empty), or bigint. Got ${!input ? JSON.stringify(input) : `typeof ${typeof input}`}`);
|
|
85
|
-
}
|
|
86
|
-
try {
|
|
87
|
-
return '0x' + BigInt(input).toString(16);
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
89
|
-
} catch (e) {
|
|
90
|
-
if (typeof input === 'string' && isHexString(input)) {
|
|
91
|
-
// BigInt() fails on non-prefixed hex strings (e.g. 'abc123')
|
|
92
|
-
return toHex('0x' + input);
|
|
93
|
-
}
|
|
94
|
-
throw new Error(`Invalid input for toHex: ${input === '' ? "''" : input}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Returns the elements that are in arr1 but not in arr2.
|
|
100
|
-
* Note, this does a primitive comparison of elements.
|
|
101
|
-
* @returns An array containing the elements that are in arr1 but not in arr2
|
|
102
|
-
*
|
|
103
|
-
* @example difference([1,2,3], [2,3,4]) => [1]
|
|
104
|
-
* @example difference([{a:1}], [{a:1}]) => [{a:1}]
|
|
105
|
-
* @example const obj = {a:1};
|
|
106
|
-
* difference([obj], [obj]) => []
|
|
107
|
-
*/
|
|
108
|
-
export function difference<T>(arr1: T[], arr2: T[]): T[] {
|
|
109
|
-
arr1 = arr1 || [];
|
|
110
|
-
arr2 = arr2 || [];
|
|
111
|
-
const arr2Set = new Set(arr2);
|
|
112
|
-
return arr1.filter(x => !arr2Set.has(x));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Deeply compares two objects for equality, handling circular references.
|
|
117
|
-
* @returns True if the objects are deeply equal, false otherwise
|
|
118
|
-
*/
|
|
119
|
-
export function isEqual(obj1: object, obj2: object): boolean {
|
|
120
|
-
if (obj1 === obj2) return true;
|
|
121
|
-
if (obj1 == null || obj2 == null) return false;
|
|
122
|
-
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return false;
|
|
123
|
-
const stack = [obj1, obj2];
|
|
124
|
-
// Use a WeakSet to track already compared objects to handle circular references
|
|
125
|
-
const weakRefs = new WeakSet();
|
|
126
|
-
const isWeak = (val) => val !== null && (typeof val === 'object' || typeof val === 'function');
|
|
127
|
-
|
|
128
|
-
while (stack.length) {
|
|
129
|
-
const a = stack.pop();
|
|
130
|
-
const b = stack.pop();
|
|
131
|
-
if (a === b) continue;
|
|
132
|
-
if (a == null || b == null) return false;
|
|
133
|
-
if (typeof a !== 'object' || typeof b !== 'object') return false;
|
|
134
|
-
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
|
|
135
|
-
if (a instanceof Date) {
|
|
136
|
-
if (!(b instanceof Date) || a.getTime() !== b.getTime()) return false;
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
if (a instanceof RegExp) {
|
|
140
|
-
if (!(b instanceof RegExp) || a.source !== b.source || a.flags !== b.flags) return false;
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
for (const key of Object.keys(a)) {
|
|
144
|
-
if (!(key in b)) return false;
|
|
145
|
-
if (isWeak(a[key])) {
|
|
146
|
-
if (weakRefs.has(a[key])) continue;
|
|
147
|
-
try { weakRefs.add(a[key]); } catch { /* ignore TypeError if a[key] is not a valid WeakSet key */ }
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
stack.push(a[key], b[key]);
|
|
151
|
-
}
|
|
152
|
-
const addtlBKeys = difference(Object.keys(b), Object.keys(a));
|
|
153
|
-
for (const key of addtlBKeys) {
|
|
154
|
-
if (!(key in a)) return false;
|
|
155
|
-
if (isWeak(b[key])) {
|
|
156
|
-
if (weakRefs.has(b[key])) continue;
|
|
157
|
-
try { weakRefs.add(b[key]); } catch { /* ignore TypeError if b[key] is not a valid WeakSet key */ }
|
|
158
|
-
}
|
|
159
|
-
stack.push(a[key], b[key]);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function normalizeXrpFlag(flag: string | number, flagEnum?: typeof xrpl.AccountSetTfFlags | typeof xrpl.PaymentFlags): string {
|
|
166
|
-
flagEnum = flagEnum || xrpl.AccountSetTfFlags; // default to AccountSet flags if not provided
|
|
167
|
-
const normalizedFlag = isNaN(parseInt(flag as string))
|
|
168
|
-
? (flag as string) // flag is already a non-numeric string (e.g. 'tfRequireDestTag')
|
|
169
|
-
: (flagEnum[flag as string]);
|
|
170
|
-
|
|
171
|
-
if (!flagEnum[normalizedFlag]) {
|
|
172
|
-
const enumName =
|
|
173
|
-
flagEnum === xrpl.AccountSetTfFlags ? 'AccountSetTfFlags' :
|
|
174
|
-
flagEnum === xrpl.PaymentFlags ? 'PaymentFlags' :
|
|
175
|
-
'<unknown>';
|
|
176
|
-
throw new Error(`Invalid XRP flag: ${flag}. Flag is not in enum ${enumName}`);
|
|
177
|
-
}
|
|
178
|
-
return normalizedFlag;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function tryParse(json: unknown, fallback?: any) {
|
|
182
|
-
try {
|
|
183
|
-
if (typeof json !== 'string') return json; // already parsed
|
|
184
|
-
return JSON.parse(json);
|
|
185
|
-
} catch {
|
|
186
|
-
return fallback;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import BitcoreCash from '@bitpay-labs/bitcore-lib-cash';
|
|
2
|
-
import type { IValidation } from '../../types/validation';
|
|
3
|
-
|
|
4
|
-
export class BchValidation implements IValidation {
|
|
5
|
-
validateAddress(network: string, address: string): boolean {
|
|
6
|
-
const AddressCash = BitcoreCash.Address;
|
|
7
|
-
// Regular Address: try Bitcoin Cash
|
|
8
|
-
return AddressCash.isValid(address, network);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
validateUri(addressUri: string): boolean {
|
|
12
|
-
// Check if the input is a valid uri or address
|
|
13
|
-
const URICash = BitcoreCash.URI;
|
|
14
|
-
// Bip21 uri
|
|
15
|
-
return URICash.isValid(addressUri);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Bitcore from '@bitpay-labs/bitcore-lib';
|
|
2
|
-
import type { IValidation } from '../../types/validation';
|
|
3
|
-
|
|
4
|
-
export class BtcValidation implements IValidation {
|
|
5
|
-
validateAddress(network: string, address: string): boolean {
|
|
6
|
-
const Address = Bitcore.Address;
|
|
7
|
-
// Regular Address: try Bitcoin
|
|
8
|
-
return Address.isValid(address, network);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
validateUri(addressUri: string): boolean {
|
|
12
|
-
// Check if the input is a valid uri or address
|
|
13
|
-
const URI = Bitcore.URI;
|
|
14
|
-
// Bip21 uri
|
|
15
|
-
return URI.isValid(addressUri);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import BitcoreDoge from '@bitpay-labs/bitcore-lib-doge';
|
|
2
|
-
import type { IValidation } from '../../types/validation';
|
|
3
|
-
|
|
4
|
-
export class DogeValidation implements IValidation {
|
|
5
|
-
validateAddress(network: string, address: string): boolean {
|
|
6
|
-
const Address = BitcoreDoge.Address;
|
|
7
|
-
return Address.isValid(address, network);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
validateUri(addressUri: string): boolean {
|
|
11
|
-
// Check if the input is a valid uri or address
|
|
12
|
-
const URICash = BitcoreDoge.URI;
|
|
13
|
-
// Bip21 uri
|
|
14
|
-
return URICash.isValid(addressUri);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { utils } from 'web3';
|
|
2
|
-
import type { IValidation } from '../../types/validation';
|
|
3
|
-
|
|
4
|
-
export class EthValidation implements IValidation {
|
|
5
|
-
regex: RegExp;
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
this.regex = /ethereum/i;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
validateAddress(_network: string, address: string): boolean {
|
|
12
|
-
return utils.isAddress(address);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
validateUri(addressUri: string): boolean {
|
|
16
|
-
if (!addressUri) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
const address = this.extractAddress(addressUri);
|
|
20
|
-
const prefix = this.regex.exec(addressUri);
|
|
21
|
-
return !!prefix && utils.isAddress(address);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
protected extractAddress(data) {
|
|
25
|
-
const prefix = /^[a-z]+:/i;
|
|
26
|
-
const params = /([?&](value|gas|gasPrice|gasLimit)=(\d+([,.]\d+)?))+/i;
|
|
27
|
-
return data.replace(prefix, '').replace(params, '');
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/validation/index.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { ArbValidation } from './arb';
|
|
2
|
-
import { ArcValidation } from './arc';
|
|
3
|
-
import { BaseValidation } from './base';
|
|
4
|
-
import { BchValidation } from './bch';
|
|
5
|
-
import { BtcValidation } from './btc';
|
|
6
|
-
import { DogeValidation } from './doge';
|
|
7
|
-
import { EthValidation } from './eth';
|
|
8
|
-
import { LtcValidation } from './ltc';
|
|
9
|
-
import { MaticValidation } from './matic';
|
|
10
|
-
import { OpValidation } from './op';
|
|
11
|
-
import { SolValidation } from './sol';
|
|
12
|
-
import { XrpValidation } from './xrp';
|
|
13
|
-
import type { IValidation } from '../types/validation';
|
|
14
|
-
|
|
15
|
-
const validation: { [chain: string]: IValidation } = {
|
|
16
|
-
BTC: new BtcValidation(),
|
|
17
|
-
BCH: new BchValidation(),
|
|
18
|
-
ETH: new EthValidation(),
|
|
19
|
-
XRP: new XrpValidation(),
|
|
20
|
-
DOGE: new DogeValidation(),
|
|
21
|
-
LTC: new LtcValidation(),
|
|
22
|
-
MATIC: new MaticValidation(),
|
|
23
|
-
ARB: new ArbValidation(),
|
|
24
|
-
BASE: new BaseValidation(),
|
|
25
|
-
OP: new OpValidation(),
|
|
26
|
-
ARC: new ArcValidation(),
|
|
27
|
-
SOL: new SolValidation(),
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export class ValidationProxy {
|
|
31
|
-
get(chain) {
|
|
32
|
-
const normalizedChain = chain.toUpperCase();
|
|
33
|
-
return validation[normalizedChain];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
validateAddress(chain, network, address) {
|
|
37
|
-
return this.get(chain).validateAddress(network, address);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
validateUri(chain, addressUri) {
|
|
41
|
-
return this.get(chain).validateUri(addressUri);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export default new ValidationProxy();
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import BitcoreLtc from '@bitpay-labs/bitcore-lib-ltc';
|
|
2
|
-
import type { IValidation } from '../../types/validation';
|
|
3
|
-
|
|
4
|
-
export class LtcValidation implements IValidation {
|
|
5
|
-
validateAddress(network: string, address: string): boolean {
|
|
6
|
-
const Address = BitcoreLtc.Address;
|
|
7
|
-
return Address.isValid(address, network);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
validateUri(addressUri: string): boolean {
|
|
11
|
-
// Check if the input is a valid uri or address
|
|
12
|
-
const URICash = BitcoreLtc.URI;
|
|
13
|
-
// Bip21 uri
|
|
14
|
-
return URICash.isValid(addressUri);
|
|
15
|
-
}
|
|
16
|
-
}
|