From 72c1a3e43c00728b841fd499545c88cca01ce336 Mon Sep 17 00:00:00 2001 From: Dan <39316940+dogebonker@users.noreply.github.com> Date: Tue, 2 Jul 2024 01:06:41 +0200 Subject: [PATCH 1/3] feat: update error message for utxo.ts to explicitly tell the issue with types --- src/utxo.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utxo.ts b/src/utxo.ts index f976bd4..ea919e8 100644 --- a/src/utxo.ts +++ b/src/utxo.ts @@ -284,7 +284,7 @@ function getScript(o: Output, opts: TxOpts = {}, network = NETWORK) { script = OutScript.encode(Address(network).decode(o.address)); } if (!script) throw new Error('Estimator: wrong output script'); - if (typeof o.amount !== 'bigint') throw new Error(`Estimator: wrong output amount=${o.amount}`); + if (typeof o.amount !== 'bigint') throw new Error(`Estimator: wrong output amount=${o.amount}, should be of type bigint but got ${typeof o.amount}.`); if (script && !opts.allowUnknownOutputs && OutScript.decode(script).type === 'unknown') { throw new Error( 'Estimator: unknown output script type, there is a chance that input is unspendable. Pass allowUnknownOutputs=true, if you sure' From a77353dc93198c5688bc02a3d064213ca82d2d61 Mon Sep 17 00:00:00 2001 From: Dan Krutoholov Date: Tue, 2 Jul 2024 01:10:17 +0200 Subject: [PATCH 2/3] feat: better error message for when the type is wrong --- src/transaction.ts | 2 +- src/utxo.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transaction.ts b/src/transaction.ts index 858a67f..c0cba0d 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -518,7 +518,7 @@ export class Transaction { ): psbt.TransactionOutput { let { amount, script } = o; if (amount === undefined) amount = cur?.amount; - if (typeof amount !== 'bigint') throw new Error('amount must be bigint sats'); + if (typeof amount !== 'bigint') throw new Error(`Wrong amount type, should be of type bigint in sats, but got ${amount} of type ${typeof amount}`); if (typeof script === 'string') script = hex.decode(script); if (script === undefined) script = cur?.script; let res: psbt.PSBTKeyMapKeys = { ...cur, ...o, amount, script }; diff --git a/src/utxo.ts b/src/utxo.ts index ea919e8..4a5e754 100644 --- a/src/utxo.ts +++ b/src/utxo.ts @@ -333,9 +333,9 @@ export class _Estimator { private opts: EstimatorOpts ) { if (typeof opts.feePerByte !== 'bigint') - throw new Error(`Estimator: wrong feePerByte=${opts.feePerByte}`); + throw new Error(`Estimator: wrong feePerByte=${opts.feePerByte}, should be of type bigint but got ${typeof opts.feePerByte}.`); if (opts.dust) { - if (typeof opts.dust !== 'bigint') throw new Error(`Estimator: wrong dust=${opts.dust}`); + if (typeof opts.dust !== 'bigint') throw new Error(`Estimator: wrong dust=${opts.dust}, should be of type bigint but got ${typeof opts.dust}.`); this.dust = opts.dust; } if (opts.requiredInputs !== undefined && !Array.isArray(opts.requiredInputs)) From f3ea3db1ccafc5cc426e0177c05bfde3ee7869ec Mon Sep 17 00:00:00 2001 From: Dan Krutoholov Date: Tue, 9 Jul 2024 00:02:33 +0200 Subject: [PATCH 3/3] fix: linter issues --- src/transaction.ts | 5 ++++- src/utxo.ts | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/transaction.ts b/src/transaction.ts index c0cba0d..e9cc54a 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -518,7 +518,10 @@ export class Transaction { ): psbt.TransactionOutput { let { amount, script } = o; if (amount === undefined) amount = cur?.amount; - if (typeof amount !== 'bigint') throw new Error(`Wrong amount type, should be of type bigint in sats, but got ${amount} of type ${typeof amount}`); + if (typeof amount !== 'bigint') + throw new Error( + `Wrong amount type, should be of type bigint in sats, but got ${amount} of type ${typeof amount}` + ); if (typeof script === 'string') script = hex.decode(script); if (script === undefined) script = cur?.script; let res: psbt.PSBTKeyMapKeys = { ...cur, ...o, amount, script }; diff --git a/src/utxo.ts b/src/utxo.ts index 4a5e754..5df85d4 100644 --- a/src/utxo.ts +++ b/src/utxo.ts @@ -284,7 +284,12 @@ function getScript(o: Output, opts: TxOpts = {}, network = NETWORK) { script = OutScript.encode(Address(network).decode(o.address)); } if (!script) throw new Error('Estimator: wrong output script'); - if (typeof o.amount !== 'bigint') throw new Error(`Estimator: wrong output amount=${o.amount}, should be of type bigint but got ${typeof o.amount}.`); + if (typeof o.amount !== 'bigint') + throw new Error( + `Estimator: wrong output amount=${ + o.amount + }, should be of type bigint but got ${typeof o.amount}.` + ); if (script && !opts.allowUnknownOutputs && OutScript.decode(script).type === 'unknown') { throw new Error( 'Estimator: unknown output script type, there is a chance that input is unspendable. Pass allowUnknownOutputs=true, if you sure' @@ -333,9 +338,18 @@ export class _Estimator { private opts: EstimatorOpts ) { if (typeof opts.feePerByte !== 'bigint') - throw new Error(`Estimator: wrong feePerByte=${opts.feePerByte}, should be of type bigint but got ${typeof opts.feePerByte}.`); + throw new Error( + `Estimator: wrong feePerByte=${ + opts.feePerByte + }, should be of type bigint but got ${typeof opts.feePerByte}.` + ); if (opts.dust) { - if (typeof opts.dust !== 'bigint') throw new Error(`Estimator: wrong dust=${opts.dust}, should be of type bigint but got ${typeof opts.dust}.`); + if (typeof opts.dust !== 'bigint') + throw new Error( + `Estimator: wrong dust=${ + opts.dust + }, should be of type bigint but got ${typeof opts.dust}.` + ); this.dust = opts.dust; } if (opts.requiredInputs !== undefined && !Array.isArray(opts.requiredInputs))