@chainvue/verus-sdk 0.4.1 → 0.5.1
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/LICENSE +202 -21
- package/NOTICE +31 -0
- package/README.md +47 -63
- package/dist/VerusSDK.js +7 -7
- package/dist/VerusSDK.js.map +1 -1
- package/dist/bundle.js +1112 -1030
- package/dist/constants/index.d.ts +4 -4
- package/dist/constants/index.d.ts.map +1 -1
- package/dist/constants/index.js +4 -4
- package/dist/constants/index.js.map +1 -1
- package/dist/currency/classify.d.ts +1 -1
- package/dist/currency/classify.d.ts.map +1 -1
- package/dist/currency/index.d.ts.map +1 -1
- package/dist/currency/index.js +5 -4
- package/dist/currency/index.js.map +1 -1
- package/dist/errors.d.ts +8 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +12 -1
- package/dist/errors.js.map +1 -1
- package/dist/fork-shims.d.ts +50 -0
- package/dist/identity/index.d.ts +8 -8
- package/dist/identity/index.d.ts.map +1 -1
- package/dist/identity/index.js +29 -29
- package/dist/identity/index.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/keys/index.d.ts.map +1 -1
- package/dist/keys/index.js +4 -3
- package/dist/keys/index.js.map +1 -1
- package/dist/signing/index.d.ts +2 -2
- package/dist/signing/index.d.ts.map +1 -1
- package/dist/signing/index.js +12 -6
- package/dist/signing/index.js.map +1 -1
- package/dist/transfer/index.d.ts.map +1 -1
- package/dist/transfer/index.js +65 -39
- package/dist/transfer/index.js.map +1 -1
- package/dist/types/index.d.ts +26 -26
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/index.d.ts +24 -5
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +71 -11
- package/dist/utils/index.js.map +1 -1
- package/dist/utxo/index.d.ts +8 -2
- package/dist/utxo/index.d.ts.map +1 -1
- package/dist/utxo/index.js +58 -31
- package/dist/utxo/index.js.map +1 -1
- package/package.json +20 -7
package/dist/types/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { Network } from '../constants/index.js';
|
|
|
6
6
|
export interface Utxo {
|
|
7
7
|
txid: string;
|
|
8
8
|
outputIndex: number;
|
|
9
|
-
satoshis:
|
|
9
|
+
satoshis: bigint;
|
|
10
10
|
script: string;
|
|
11
11
|
/** Height where this UTXO was created (0 = mempool) */
|
|
12
12
|
height?: number;
|
|
@@ -14,18 +14,18 @@ export interface Utxo {
|
|
|
14
14
|
/** Decoded currency values on a UTXO */
|
|
15
15
|
export interface DecodedUtxo extends Utxo {
|
|
16
16
|
/** Currency i-address → satoshi amount */
|
|
17
|
-
currencyValues: Map<string,
|
|
17
|
+
currencyValues: Map<string, bigint>;
|
|
18
18
|
}
|
|
19
19
|
/** Result of UTXO selection */
|
|
20
20
|
export interface SelectionResult {
|
|
21
21
|
/** Selected UTXOs */
|
|
22
22
|
selected: Utxo[];
|
|
23
23
|
/** Native (VRSC) change in satoshis */
|
|
24
|
-
nativeChange:
|
|
24
|
+
nativeChange: bigint;
|
|
25
25
|
/** Currency changes: i-address → satoshi amount */
|
|
26
|
-
currencyChanges: Map<string,
|
|
26
|
+
currencyChanges: Map<string, bigint>;
|
|
27
27
|
/** Estimated fee in satoshis */
|
|
28
|
-
fee:
|
|
28
|
+
fee: bigint;
|
|
29
29
|
}
|
|
30
30
|
/** SDK configuration */
|
|
31
31
|
export interface VerusSDKConfig {
|
|
@@ -35,7 +35,7 @@ export interface VerusSDKConfig {
|
|
|
35
35
|
export interface SignedTxResult {
|
|
36
36
|
signedTx: string;
|
|
37
37
|
txid: string;
|
|
38
|
-
fee:
|
|
38
|
+
fee: bigint;
|
|
39
39
|
}
|
|
40
40
|
/** Transfer parameters */
|
|
41
41
|
export interface TransferParams {
|
|
@@ -43,7 +43,7 @@ export interface TransferParams {
|
|
|
43
43
|
/** Recipient address (R-address) */
|
|
44
44
|
to: string;
|
|
45
45
|
/** Amount in satoshis */
|
|
46
|
-
amount:
|
|
46
|
+
amount: bigint;
|
|
47
47
|
utxos: Utxo[];
|
|
48
48
|
changeAddress: string;
|
|
49
49
|
expiryHeight?: number;
|
|
@@ -54,7 +54,7 @@ export interface TransferTokenParams {
|
|
|
54
54
|
/** Recipient address (R-address or i-address) */
|
|
55
55
|
to: string;
|
|
56
56
|
/** Amount in satoshis */
|
|
57
|
-
amount:
|
|
57
|
+
amount: bigint;
|
|
58
58
|
/** Currency i-address */
|
|
59
59
|
currency: string;
|
|
60
60
|
/** Address type: 'PKH' (R-address) or 'ID' (i-address) */
|
|
@@ -67,7 +67,7 @@ export interface TransferTokenParams {
|
|
|
67
67
|
export interface ConvertParams {
|
|
68
68
|
wif: string;
|
|
69
69
|
/** Amount in satoshis */
|
|
70
|
-
amount:
|
|
70
|
+
amount: bigint;
|
|
71
71
|
/** Source currency i-address */
|
|
72
72
|
currency: string;
|
|
73
73
|
/** Target currency i-address */
|
|
@@ -82,8 +82,8 @@ export interface ConvertParams {
|
|
|
82
82
|
export interface CurrencyOutput {
|
|
83
83
|
/** Currency i-address (use system ID for native VRSC) */
|
|
84
84
|
currency: string;
|
|
85
|
-
/** Amount in satoshis
|
|
86
|
-
satoshis:
|
|
85
|
+
/** Amount in satoshis */
|
|
86
|
+
satoshis: bigint;
|
|
87
87
|
/** Recipient address */
|
|
88
88
|
address: string;
|
|
89
89
|
/** Address type: 'PKH' (R-address), 'ID' (i-address), 'ETH' (Ethereum) */
|
|
@@ -99,7 +99,7 @@ export interface CurrencyOutput {
|
|
|
99
99
|
/** Fee currency i-address */
|
|
100
100
|
feeCurrency?: string;
|
|
101
101
|
/** Fee amount in satoshis */
|
|
102
|
-
feeSatoshis?:
|
|
102
|
+
feeSatoshis?: bigint;
|
|
103
103
|
/** Pre-conversion flag */
|
|
104
104
|
preconvert?: boolean;
|
|
105
105
|
}
|
|
@@ -114,7 +114,7 @@ export interface SendCurrencyParams {
|
|
|
114
114
|
/** Send currency result */
|
|
115
115
|
export interface SendCurrencyResult extends SignedTxResult {
|
|
116
116
|
inputsUsed: number;
|
|
117
|
-
nativeChange:
|
|
117
|
+
nativeChange: bigint;
|
|
118
118
|
}
|
|
119
119
|
/** Simple P2PKH build+sign parameters */
|
|
120
120
|
export interface BuildAndSignParams {
|
|
@@ -123,13 +123,13 @@ export interface BuildAndSignParams {
|
|
|
123
123
|
txid: string;
|
|
124
124
|
vout: number;
|
|
125
125
|
scriptPubKey: string;
|
|
126
|
-
amount:
|
|
126
|
+
amount: bigint;
|
|
127
127
|
}>;
|
|
128
128
|
outputs: Array<{
|
|
129
129
|
address: string;
|
|
130
|
-
amount:
|
|
130
|
+
amount: bigint;
|
|
131
131
|
}>;
|
|
132
|
-
fee?:
|
|
132
|
+
fee?: bigint;
|
|
133
133
|
expiryHeight?: number;
|
|
134
134
|
}
|
|
135
135
|
/** Commitment data returned from createCommitment */
|
|
@@ -169,12 +169,12 @@ export interface RegisterIdentityParams {
|
|
|
169
169
|
recoveryAuthority?: string;
|
|
170
170
|
/** Referral chain: i-addresses tracing referral levels (up to 3) */
|
|
171
171
|
referralChain?: string[];
|
|
172
|
-
/** Registration fee in VRSC satoshis (default:
|
|
173
|
-
registrationFee?:
|
|
172
|
+
/** Registration fee in VRSC satoshis (default: 10000000000n = 100 VRSC) */
|
|
173
|
+
registrationFee?: bigint;
|
|
174
174
|
/** Registration fee in parent currency satoshis (for sub-IDs) */
|
|
175
|
-
registrationFeeAmount?:
|
|
175
|
+
registrationFeeAmount?: bigint;
|
|
176
176
|
/** Native import fee in VRSC satoshis (for sub-IDs on fractional currencies) */
|
|
177
|
-
nativeImportFee?:
|
|
177
|
+
nativeImportFee?: bigint;
|
|
178
178
|
/** Number of referral levels (default: 3) */
|
|
179
179
|
referralLevels?: number;
|
|
180
180
|
expiryHeight?: number;
|
|
@@ -182,11 +182,11 @@ export interface RegisterIdentityParams {
|
|
|
182
182
|
/** Register identity result */
|
|
183
183
|
export interface RegisterIdentityResult extends SignedTxResult {
|
|
184
184
|
identityAddress: string;
|
|
185
|
-
registrationFee:
|
|
185
|
+
registrationFee: bigint;
|
|
186
186
|
referralPayments: number;
|
|
187
|
-
referralAmountEach:
|
|
187
|
+
referralAmountEach: bigint;
|
|
188
188
|
inputsUsed: number;
|
|
189
|
-
nativeChange:
|
|
189
|
+
nativeChange: bigint;
|
|
190
190
|
}
|
|
191
191
|
/** Update identity parameters */
|
|
192
192
|
export interface UpdateIdentityParams {
|
|
@@ -208,7 +208,7 @@ export interface UpdateIdentityResult extends SignedTxResult {
|
|
|
208
208
|
identityAddress: string;
|
|
209
209
|
operation: string;
|
|
210
210
|
inputsUsed: number;
|
|
211
|
-
nativeChange:
|
|
211
|
+
nativeChange: bigint;
|
|
212
212
|
}
|
|
213
213
|
/** Lock identity parameters */
|
|
214
214
|
export interface LockIdentityParams {
|
|
@@ -256,7 +256,7 @@ export interface DefineCurrencyParams {
|
|
|
256
256
|
identityHex: string;
|
|
257
257
|
identityUtxo: Utxo;
|
|
258
258
|
currencyDefScript: string;
|
|
259
|
-
currencyDefValue?:
|
|
259
|
+
currencyDefValue?: bigint;
|
|
260
260
|
utxos: Utxo[];
|
|
261
261
|
changeAddress: string;
|
|
262
262
|
expiryHeight?: number;
|
|
@@ -265,7 +265,7 @@ export interface DefineCurrencyParams {
|
|
|
265
265
|
export interface DefineCurrencyResult extends SignedTxResult {
|
|
266
266
|
identityAddress: string;
|
|
267
267
|
inputsUsed: number;
|
|
268
|
-
nativeChange:
|
|
268
|
+
nativeChange: bigint;
|
|
269
269
|
}
|
|
270
270
|
/** Sign message parameters */
|
|
271
271
|
export interface SignMessageParams {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAErD,oEAAoE;AACpE,MAAM,WAAW,IAAI;IACnB,IA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M,EAAE,QAAQ,GAAG,SAAS,GAAG,QAAQ,GAAG,SAAS,CAAC;IACpD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACnC,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,iCAAiC;AACjC,MAAM,WAAW,eAAe;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;CACd"}
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -13,10 +13,29 @@ export declare function writeCompactSize(value: number): Buffer;
|
|
|
13
13
|
* Decode an i-address to its 20-byte hash
|
|
14
14
|
*/
|
|
15
15
|
export declare function iAddressToHash(iAddress: string): Buffer;
|
|
16
|
-
/**
|
|
17
|
-
export declare
|
|
18
|
-
/**
|
|
19
|
-
export declare
|
|
16
|
+
/** Number of decimal places in a coin amount */
|
|
17
|
+
export declare const AMOUNT_DECIMALS = 8;
|
|
18
|
+
/** Satoshis per coin unit */
|
|
19
|
+
export declare const SATS_PER_COIN = 100000000n;
|
|
20
|
+
/**
|
|
21
|
+
* Parse a decimal coin-amount string into bigint satoshis. Exact arithmetic
|
|
22
|
+
* only — rejects anything outside the grammar (negative, exponent notation,
|
|
23
|
+
* more than 8 fraction digits, empty, non-numeric) with a typed error.
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseSats(decimal: string): bigint;
|
|
26
|
+
/** Convert coin units (decimal string) to satoshis — alias of parseSats */
|
|
27
|
+
export declare function toSatoshis(coins: string): bigint;
|
|
28
|
+
/**
|
|
29
|
+
* Convert satoshis to a decimal coin-unit string (minimal form, trailing
|
|
30
|
+
* fraction zeros trimmed). Exact — never goes through float64.
|
|
31
|
+
*/
|
|
32
|
+
export declare function toCoins(satoshis: bigint): string;
|
|
33
|
+
/**
|
|
34
|
+
* Convert bigint satoshis to the `number` that @bitgo/utxo-lib expects at its
|
|
35
|
+
* boundary. The SDK is exact-integer internally; this is the single checked
|
|
36
|
+
* crossing into the signing library, which still models values as float64.
|
|
37
|
+
*/
|
|
38
|
+
export declare function toSafeNumber(sats: bigint): number;
|
|
20
39
|
/**
|
|
21
40
|
* Convert an R-address or P2SH address to a scriptPubKey Buffer
|
|
22
41
|
*/
|
|
@@ -28,7 +47,7 @@ export interface DecodedInput {
|
|
|
28
47
|
}
|
|
29
48
|
/** One output of a signed transaction. `address` is null for smart outputs. */
|
|
30
49
|
export interface DecodedOutput {
|
|
31
|
-
valueSat:
|
|
50
|
+
valueSat: bigint;
|
|
32
51
|
scriptHex: string;
|
|
33
52
|
/** Base58 address when the script is plain P2PKH/P2SH, else null. */
|
|
34
53
|
address: string | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH;;GAEG;AACH,wBAAgB,OAAO,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAMpD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAkBtD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAGvD;AAID,gDAAgD;AAChD,eAAO,MAAM,eAAe,IAAI,CAAC;AAEjC,6BAA6B;AAC7B,eAAO,MAAM,aAAa,aAAe,CAAC;AAS1C;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAMjD;AAED,2EAA2E;AAC3E,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAWhD;AAID;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAQjD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CA8B7D;AAID,yEAAyE;AACzE,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,+EAA+E;AAC/E,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,qEAAqE;IACrE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,GAAG,SAAS,GAC7B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,YAAY,EAAE,CAAC;IAAC,OAAO,EAAE,aAAa,EAAE,CAAA;CAAE,CAqCpE"}
|
package/dist/utils/index.js
CHANGED
|
@@ -6,17 +6,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
7
|
};
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.SATS_PER_COIN = exports.AMOUNT_DECIMALS = void 0;
|
|
9
10
|
exports.sha256d = sha256d;
|
|
10
11
|
exports.writeCompactSize = writeCompactSize;
|
|
11
12
|
exports.iAddressToHash = iAddressToHash;
|
|
13
|
+
exports.parseSats = parseSats;
|
|
12
14
|
exports.toSatoshis = toSatoshis;
|
|
13
15
|
exports.toCoins = toCoins;
|
|
16
|
+
exports.toSafeNumber = toSafeNumber;
|
|
14
17
|
exports.addressToScriptPubKey = addressToScriptPubKey;
|
|
15
18
|
exports.summarizeSignedTransaction = summarizeSignedTransaction;
|
|
16
19
|
const crypto_1 = require("crypto");
|
|
17
20
|
const bs58check_1 = __importDefault(require("bs58check"));
|
|
18
21
|
const verus_typescript_primitives_1 = require("verus-typescript-primitives");
|
|
19
22
|
const index_js_1 = require("../constants/index.js");
|
|
23
|
+
const errors_js_1 = require("../errors.js");
|
|
20
24
|
/**
|
|
21
25
|
* SHA256d (double SHA-256) of one or more buffers
|
|
22
26
|
*/
|
|
@@ -59,19 +63,73 @@ function iAddressToHash(iAddress) {
|
|
|
59
63
|
const { hash } = (0, verus_typescript_primitives_1.fromBase58Check)(iAddress);
|
|
60
64
|
return Buffer.from(hash);
|
|
61
65
|
}
|
|
62
|
-
|
|
66
|
+
// ─── Exact money conversions (bigint satoshis ↔ decimal strings) ─────────
|
|
67
|
+
/** Number of decimal places in a coin amount */
|
|
68
|
+
exports.AMOUNT_DECIMALS = 8;
|
|
69
|
+
/** Satoshis per coin unit */
|
|
70
|
+
exports.SATS_PER_COIN = 100000000n;
|
|
71
|
+
/**
|
|
72
|
+
* Grammar for decimal coin amounts: non-negative, no leading zeros in the
|
|
73
|
+
* integer part, optional `.` with 1–8 fraction digits. No exponents, no
|
|
74
|
+
* signs, no whitespace.
|
|
75
|
+
*/
|
|
76
|
+
const DECIMAL_AMOUNT_RE = /^(?:0|[1-9]\d*)(?:\.\d{1,8})?$/;
|
|
77
|
+
/**
|
|
78
|
+
* Parse a decimal coin-amount string into bigint satoshis. Exact arithmetic
|
|
79
|
+
* only — rejects anything outside the grammar (negative, exponent notation,
|
|
80
|
+
* more than 8 fraction digits, empty, non-numeric) with a typed error.
|
|
81
|
+
*/
|
|
82
|
+
function parseSats(decimal) {
|
|
83
|
+
if (typeof decimal !== 'string' || !DECIMAL_AMOUNT_RE.test(decimal)) {
|
|
84
|
+
throw new errors_js_1.InvalidAmountError(String(decimal));
|
|
85
|
+
}
|
|
86
|
+
const [whole = '0', fraction = ''] = decimal.split('.');
|
|
87
|
+
return BigInt(whole) * exports.SATS_PER_COIN + BigInt(fraction.padEnd(exports.AMOUNT_DECIMALS, '0'));
|
|
88
|
+
}
|
|
89
|
+
/** Convert coin units (decimal string) to satoshis — alias of parseSats */
|
|
63
90
|
function toSatoshis(coins) {
|
|
64
|
-
return
|
|
91
|
+
return parseSats(coins);
|
|
65
92
|
}
|
|
66
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* Convert satoshis to a decimal coin-unit string (minimal form, trailing
|
|
95
|
+
* fraction zeros trimmed). Exact — never goes through float64.
|
|
96
|
+
*/
|
|
67
97
|
function toCoins(satoshis) {
|
|
68
|
-
|
|
98
|
+
const sign = satoshis < 0n ? '-' : '';
|
|
99
|
+
const abs = satoshis < 0n ? -satoshis : satoshis;
|
|
100
|
+
const whole = abs / exports.SATS_PER_COIN;
|
|
101
|
+
const fraction = abs % exports.SATS_PER_COIN;
|
|
102
|
+
if (fraction === 0n)
|
|
103
|
+
return `${sign}${whole}`;
|
|
104
|
+
const fractionStr = fraction
|
|
105
|
+
.toString()
|
|
106
|
+
.padStart(exports.AMOUNT_DECIMALS, '0')
|
|
107
|
+
.replace(/0+$/, '');
|
|
108
|
+
return `${sign}${whole}.${fractionStr}`;
|
|
109
|
+
}
|
|
110
|
+
const MAX_SAFE_SATS = BigInt(Number.MAX_SAFE_INTEGER);
|
|
111
|
+
/**
|
|
112
|
+
* Convert bigint satoshis to the `number` that @bitgo/utxo-lib expects at its
|
|
113
|
+
* boundary. The SDK is exact-integer internally; this is the single checked
|
|
114
|
+
* crossing into the signing library, which still models values as float64.
|
|
115
|
+
*/
|
|
116
|
+
function toSafeNumber(sats) {
|
|
117
|
+
if (sats < 0n || sats > MAX_SAFE_SATS) {
|
|
118
|
+
throw new errors_js_1.InvalidAmountError(sats.toString(), 'outside the safe-integer range supported by the underlying signing library');
|
|
119
|
+
}
|
|
120
|
+
return Number(sats);
|
|
69
121
|
}
|
|
70
122
|
/**
|
|
71
123
|
* Convert an R-address or P2SH address to a scriptPubKey Buffer
|
|
72
124
|
*/
|
|
73
125
|
function addressToScriptPubKey(address) {
|
|
74
|
-
|
|
126
|
+
let decoded;
|
|
127
|
+
try {
|
|
128
|
+
decoded = bs58check_1.default.decode(address);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
throw new errors_js_1.InvalidAddressError(address, err.message);
|
|
132
|
+
}
|
|
75
133
|
const prefix = decoded[0];
|
|
76
134
|
const hash = decoded.slice(1);
|
|
77
135
|
if (prefix === 0x3c) {
|
|
@@ -91,7 +149,7 @@ function addressToScriptPubKey(address) {
|
|
|
91
149
|
]);
|
|
92
150
|
}
|
|
93
151
|
else {
|
|
94
|
-
throw new
|
|
152
|
+
throw new errors_js_1.InvalidAddressError(address, `Unsupported address prefix: 0x${(prefix ?? 0).toString(16)}. Use sendCurrency for i-address destinations.`);
|
|
95
153
|
}
|
|
96
154
|
}
|
|
97
155
|
/**
|
|
@@ -102,9 +160,11 @@ function addressToScriptPubKey(address) {
|
|
|
102
160
|
* transaction parsing.
|
|
103
161
|
*/
|
|
104
162
|
function summarizeSignedTransaction(hex, network) {
|
|
105
|
-
// Lazy require keeps utils' import graph light for non-tx consumers.
|
|
106
|
-
//
|
|
107
|
-
|
|
163
|
+
// Lazy require keeps utils' import graph light for non-tx consumers. The
|
|
164
|
+
// fork's ambient module declaration (bitgo-utxo-lib.d.ts) types the shape.
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports -- deliberate lazy load; see above
|
|
166
|
+
const utxolib = require('@bitgo/utxo-lib');
|
|
167
|
+
const { Transaction, address: addressLib, networks, smarttxs } = utxolib;
|
|
108
168
|
const net = network === 'testnet' ? networks.verustest : networks.verus;
|
|
109
169
|
const chainId = network === 'testnet' ? index_js_1.NETWORK_CONFIG.testnet.chainId : index_js_1.NETWORK_CONFIG.mainnet.chainId;
|
|
110
170
|
const tx = Transaction.fromHex(hex, net);
|
|
@@ -127,14 +187,14 @@ function summarizeSignedTransaction(hex, network) {
|
|
|
127
187
|
const unpacked = smarttxs.unpackOutput({ script: out.script, value: out.value }, chainId);
|
|
128
188
|
const evalCodes = (unpacked.params ?? []).map((p) => p.eval);
|
|
129
189
|
if (unpacked.destinations.length === 1 && evalCodes.every((code) => code === 0)) {
|
|
130
|
-
addr = unpacked.destinations[0];
|
|
190
|
+
addr = unpacked.destinations[0] ?? null;
|
|
131
191
|
}
|
|
132
192
|
}
|
|
133
193
|
catch {
|
|
134
194
|
addr = null; // exotic CC output — no address form
|
|
135
195
|
}
|
|
136
196
|
}
|
|
137
|
-
return { valueSat: out.value, scriptHex: out.script.toString('hex'), address: addr };
|
|
197
|
+
return { valueSat: BigInt(out.value), scriptHex: out.script.toString('hex'), address: addr };
|
|
138
198
|
});
|
|
139
199
|
return { txid: tx.getId(), inputs, outputs };
|
|
140
200
|
}
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";AAAA;;GAEG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;AAWH,0BAMC;AAKD,4CAkBC;AAKD,wCAGC;AAsBD,8BAMC;AAGD,gCAEC;AAMD,0BAWC;AASD,oCAQC;AAKD,sDA8BC;AAyBD,gEAwCC;AArND,mCAAoC;AACpC,0DAAkC;AAClC,6EAA8D;AAC9D,oDAAuD;AACvD,4CAAuE;AAEvE;;GAEG;AACH,SAAgB,OAAO,CAAC,GAAG,OAAiB;IAC1C,MAAM,KAAK,GAAG,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC;IACnC,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;QAC1B,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC;IACD,OAAO,IAAA,mBAAU,EAAC,QAAQ,CAAC,CAAC,MAAM,CAAC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AAE,CAAC;AAC/C,CAAC"}
|
package/dist/utxo/index.d.ts
CHANGED
|
@@ -3,19 +3,25 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Selects UTXOs to cover required native + currency amounts,
|
|
5
5
|
* calculates fees, and determines change outputs.
|
|
6
|
+
*
|
|
7
|
+
* All amounts are bigint satoshis — exact integer arithmetic only.
|
|
6
8
|
*/
|
|
7
9
|
import type { Utxo, DecodedUtxo, SelectionResult } from '../types/index.js';
|
|
8
10
|
export type { Utxo, DecodedUtxo, SelectionResult };
|
|
9
11
|
/**
|
|
10
12
|
* Decode a UTXO's script to extract currency values
|
|
13
|
+
*
|
|
14
|
+
* Throws TransactionBuildError if a smart output fails to decode — a failed
|
|
15
|
+
* token decode must never be silently reclassified as native-only, because
|
|
16
|
+
* selection would then spend token value it did not account for.
|
|
11
17
|
*/
|
|
12
18
|
export declare function decodeUtxo(utxo: Utxo, systemId: string): DecodedUtxo;
|
|
13
19
|
/**
|
|
14
20
|
* Estimate transaction fee based on input/output counts
|
|
15
21
|
*/
|
|
16
|
-
export declare function estimateFee(numInputs: number, numOutputs: number, feePerKb?:
|
|
22
|
+
export declare function estimateFee(numInputs: number, numOutputs: number, feePerKb?: bigint, hasSmartOutputs?: boolean): bigint;
|
|
17
23
|
/**
|
|
18
24
|
* Select UTXOs to cover required amounts
|
|
19
25
|
*/
|
|
20
|
-
export declare function selectUtxos(utxos: Utxo[], requiredNative:
|
|
26
|
+
export declare function selectUtxos(utxos: Utxo[], requiredNative: bigint, requiredCurrencies?: Map<string, bigint>, numOutputs?: number, systemId?: string, feePerKb?: bigint, hasSmartOutputs?: boolean): SelectionResult;
|
|
21
27
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/utxo/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utxo/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utxo/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAK5E,YAAY,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe,EAAE,CAAC;AA6BnD;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAG,WAAW,CA2BpE;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,QAAQ,GAAE,MAA2B,EACrC,eAAe,GAAE,OAAe,GAC/B,MAAM,CAKR;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,IAAI,EAAE,EACb,cAAc,EAAE,MAAM,EACtB,kBAAkB,GAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAa,EACnD,UAAU,GAAE,MAAU,EACtB,QAAQ,GAAE,MAAuC,EACjD,QAAQ,GAAE,MAA2B,EACrC,eAAe,GAAE,OAAe,GAC/B,eAAe,CA+GjB"}
|
package/dist/utxo/index.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Selects UTXOs to cover required native + currency amounts,
|
|
6
6
|
* calculates fees, and determines change outputs.
|
|
7
|
+
*
|
|
8
|
+
* All amounts are bigint satoshis — exact integer arithmetic only.
|
|
7
9
|
*/
|
|
8
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
11
|
exports.decodeUtxo = decodeUtxo;
|
|
@@ -12,33 +14,58 @@ exports.selectUtxos = selectUtxos;
|
|
|
12
14
|
const utxo_lib_1 = require("@bitgo/utxo-lib");
|
|
13
15
|
const index_js_1 = require("../constants/index.js");
|
|
14
16
|
const errors_js_1 = require("../errors.js");
|
|
17
|
+
const index_js_2 = require("../utils/index.js");
|
|
15
18
|
const { unpackOutput } = utxo_lib_1.smarttxs;
|
|
16
19
|
// Transaction size estimation constants
|
|
17
20
|
const INPUT_SIZE = 180;
|
|
18
21
|
const P2PKH_OUTPUT_SIZE = 34;
|
|
19
22
|
const SMART_OUTPUT_SIZE = 200;
|
|
20
23
|
const TX_OVERHEAD = 60;
|
|
24
|
+
/** Absolute fee floor in satoshis */
|
|
25
|
+
const MIN_FEE = 10000n;
|
|
26
|
+
/**
|
|
27
|
+
* True when the script is a Verus smart (CryptoCondition) output. Used to
|
|
28
|
+
* separate "plain script that unpackOutput doesn't model" (fine — native
|
|
29
|
+
* only) from "smart output that failed to decode" (dangerous — the UTXO may
|
|
30
|
+
* carry token value we cannot see).
|
|
31
|
+
*/
|
|
32
|
+
function isSmartTransactionScript(script) {
|
|
33
|
+
try {
|
|
34
|
+
const chunks = utxo_lib_1.script.decompile(script);
|
|
35
|
+
if (chunks === null)
|
|
36
|
+
return false;
|
|
37
|
+
return chunks.some((chunk) => typeof chunk === 'number' && chunk === utxo_lib_1.opcodes.OP_CHECKCRYPTOCONDITION);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
21
43
|
/**
|
|
22
44
|
* Decode a UTXO's script to extract currency values
|
|
45
|
+
*
|
|
46
|
+
* Throws TransactionBuildError if a smart output fails to decode — a failed
|
|
47
|
+
* token decode must never be silently reclassified as native-only, because
|
|
48
|
+
* selection would then spend token value it did not account for.
|
|
23
49
|
*/
|
|
24
50
|
function decodeUtxo(utxo, systemId) {
|
|
25
51
|
const currencyValues = new Map();
|
|
52
|
+
const script = Buffer.from(utxo.script, 'hex');
|
|
53
|
+
const nativeValue = (0, index_js_2.toSafeNumber)(utxo.satoshis);
|
|
26
54
|
try {
|
|
27
|
-
const
|
|
28
|
-
value: utxo.satoshis,
|
|
29
|
-
script: Buffer.from(utxo.script, 'hex'),
|
|
30
|
-
};
|
|
31
|
-
const unpacked = unpackOutput(output, systemId, true);
|
|
55
|
+
const unpacked = unpackOutput({ value: nativeValue, script }, systemId, true);
|
|
32
56
|
for (const [currency, value] of Object.entries(unpacked.values)) {
|
|
33
|
-
const amount =
|
|
34
|
-
if (amount >
|
|
57
|
+
const amount = BigInt(value.toString(10));
|
|
58
|
+
if (amount > 0n) {
|
|
35
59
|
currencyValues.set(currency, amount);
|
|
36
60
|
}
|
|
37
61
|
}
|
|
38
62
|
}
|
|
39
|
-
catch {
|
|
40
|
-
|
|
41
|
-
|
|
63
|
+
catch (err) {
|
|
64
|
+
if (isSmartTransactionScript(script)) {
|
|
65
|
+
throw new errors_js_1.TransactionBuildError(`cannot decode smart output ${utxo.txid}:${utxo.outputIndex}: ${err.message}`);
|
|
66
|
+
}
|
|
67
|
+
// Plain script that unpackOutput doesn't model (e.g. P2SH) — native-only
|
|
68
|
+
if (utxo.satoshis > 0n) {
|
|
42
69
|
currencyValues.set(systemId, utxo.satoshis);
|
|
43
70
|
}
|
|
44
71
|
}
|
|
@@ -50,8 +77,8 @@ function decodeUtxo(utxo, systemId) {
|
|
|
50
77
|
function estimateFee(numInputs, numOutputs, feePerKb = index_js_1.DEFAULT_FEE_PER_KB, hasSmartOutputs = false) {
|
|
51
78
|
const outputSize = hasSmartOutputs ? SMART_OUTPUT_SIZE : P2PKH_OUTPUT_SIZE;
|
|
52
79
|
const txSize = TX_OVERHEAD + numInputs * INPUT_SIZE + numOutputs * outputSize;
|
|
53
|
-
const fee =
|
|
54
|
-
return
|
|
80
|
+
const fee = (BigInt(txSize) * feePerKb + 999n) / 1000n; // ceil(txSize * feePerKb / 1000)
|
|
81
|
+
return fee > MIN_FEE ? fee : MIN_FEE;
|
|
55
82
|
}
|
|
56
83
|
/**
|
|
57
84
|
* Select UTXOs to cover required amounts
|
|
@@ -70,8 +97,8 @@ function selectUtxos(utxos, requiredNative, requiredCurrencies = new Map(), numO
|
|
|
70
97
|
for (const [currency, needed] of remaining) {
|
|
71
98
|
if (currency === systemId)
|
|
72
99
|
continue;
|
|
73
|
-
const utxoAmount = utxo.currencyValues.get(currency) ||
|
|
74
|
-
if (utxoAmount >
|
|
100
|
+
const utxoAmount = utxo.currencyValues.get(currency) || 0n;
|
|
101
|
+
if (utxoAmount > 0n && needed > 0n) {
|
|
75
102
|
hasNeededCurrency = true;
|
|
76
103
|
break;
|
|
77
104
|
}
|
|
@@ -79,20 +106,20 @@ function selectUtxos(utxos, requiredNative, requiredCurrencies = new Map(), numO
|
|
|
79
106
|
if (hasNeededCurrency) {
|
|
80
107
|
selected.push(utxo);
|
|
81
108
|
for (const [currency, amount] of utxo.currencyValues) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
109
|
+
// The UTXO's native value is credited once below via utxo.satoshis;
|
|
110
|
+
// unpackOutput also reports it under systemId in currencyValues, so
|
|
111
|
+
// skipping it here avoids double-counting.
|
|
112
|
+
if (currency === systemId)
|
|
113
|
+
continue;
|
|
114
|
+
const needed = remaining.get(currency) || 0n;
|
|
115
|
+
remaining.set(currency, needed - amount);
|
|
89
116
|
}
|
|
90
117
|
remainingNative -= utxo.satoshis;
|
|
91
118
|
}
|
|
92
119
|
}
|
|
93
120
|
for (const [currency, needed] of remaining) {
|
|
94
|
-
if (currency !== systemId && needed >
|
|
95
|
-
const totalAvailable = decoded.reduce((sum, u) => sum + (u.currencyValues.get(currency) ||
|
|
121
|
+
if (currency !== systemId && needed > 0n) {
|
|
122
|
+
const totalAvailable = decoded.reduce((sum, u) => sum + (u.currencyValues.get(currency) || 0n), 0n);
|
|
96
123
|
throw new errors_js_1.InsufficientFundsError(needed + totalAvailable, totalAvailable, currency);
|
|
97
124
|
}
|
|
98
125
|
}
|
|
@@ -100,18 +127,18 @@ function selectUtxos(utxos, requiredNative, requiredCurrencies = new Map(), numO
|
|
|
100
127
|
// Phase 2: Select UTXOs for native amount + fee
|
|
101
128
|
const nativeOnly = decoded
|
|
102
129
|
.filter((u) => !selected.includes(u))
|
|
103
|
-
.sort((a, b) => b.satoshis
|
|
130
|
+
.sort((a, b) => (b.satoshis > a.satoshis ? 1 : b.satoshis < a.satoshis ? -1 : 0));
|
|
104
131
|
let changeOutputCount = 0;
|
|
105
132
|
for (const [currency, needed] of remaining) {
|
|
106
|
-
if (currency !== systemId && needed <
|
|
133
|
+
if (currency !== systemId && needed < 0n) {
|
|
107
134
|
changeOutputCount++;
|
|
108
135
|
}
|
|
109
136
|
}
|
|
110
137
|
let fee = estimateFee(selected.length + 1, numOutputs + 1 + changeOutputCount, feePerKb, hasSmartOutputs);
|
|
111
|
-
while (remainingNative + fee >
|
|
138
|
+
while (remainingNative + fee > 0n) {
|
|
112
139
|
const next = nativeOnly.shift();
|
|
113
140
|
if (!next) {
|
|
114
|
-
const totalAvailable = decoded.reduce((sum, u) => sum + u.satoshis,
|
|
141
|
+
const totalAvailable = decoded.reduce((sum, u) => sum + u.satoshis, 0n);
|
|
115
142
|
throw new errors_js_1.InsufficientFundsError(requiredNative + fee, totalAvailable, 'VRSC');
|
|
116
143
|
}
|
|
117
144
|
selected.push(next);
|
|
@@ -119,18 +146,18 @@ function selectUtxos(utxos, requiredNative, requiredCurrencies = new Map(), numO
|
|
|
119
146
|
fee = estimateFee(selected.length, numOutputs + 1 + changeOutputCount, feePerKb, hasSmartOutputs);
|
|
120
147
|
}
|
|
121
148
|
// Calculate change
|
|
122
|
-
const totalNativeIn = selected.reduce((sum, u) => sum + u.satoshis,
|
|
149
|
+
const totalNativeIn = selected.reduce((sum, u) => sum + u.satoshis, 0n);
|
|
123
150
|
const actualNativeChange = totalNativeIn - requiredNative - fee;
|
|
124
151
|
const currencyChanges = new Map();
|
|
125
152
|
for (const [currency, needed] of remaining) {
|
|
126
|
-
if (currency !== systemId && needed <
|
|
153
|
+
if (currency !== systemId && needed < 0n) {
|
|
127
154
|
currencyChanges.set(currency, -needed);
|
|
128
155
|
}
|
|
129
156
|
}
|
|
130
|
-
const finalNativeChange = actualNativeChange > index_js_1.DUST_THRESHOLD ? actualNativeChange :
|
|
157
|
+
const finalNativeChange = actualNativeChange > index_js_1.DUST_THRESHOLD ? actualNativeChange : 0n;
|
|
131
158
|
const finalFee = actualNativeChange > index_js_1.DUST_THRESHOLD ? fee : fee + actualNativeChange;
|
|
132
159
|
return {
|
|
133
|
-
selected: selected.map(({ currencyValues, ...utxo }) => utxo),
|
|
160
|
+
selected: selected.map(({ currencyValues: _currencyValues, ...utxo }) => utxo),
|
|
134
161
|
nativeChange: finalNativeChange,
|
|
135
162
|
currencyChanges,
|
|
136
163
|
fee: finalFee,
|