@btc-vision/bitcoin 6.4.5 → 6.4.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/src/psbt.ts CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  import { checkForInput, checkForOutput } from 'bip174/src/lib/utils.js';
19
19
  import { BIP32Interface } from 'bip32';
20
20
  import { ECPairInterface } from 'ecpair';
21
- import { fromOutputScript, toOutputScript } from './address.js';
21
+ import { fromOutputScript, isUnknownSegwitVersion, toOutputScript } from './address.js';
22
22
  import { cloneBuffer, reverseBuffer } from './bufferutils.js';
23
23
  import { P2WSHPayment, payments } from './index.js';
24
24
  import { bitcoin as btcNetwork, Network } from './networks.js';
@@ -1486,6 +1486,7 @@ export function getFinalScripts(
1486
1486
  isP2SH: boolean,
1487
1487
  isP2WSH: boolean,
1488
1488
  canRunChecks: boolean = true,
1489
+ solution?: Buffer[],
1489
1490
  ): {
1490
1491
  finalScriptSig: Buffer | undefined;
1491
1492
  finalScriptWitness: Buffer | undefined;
@@ -1495,7 +1496,15 @@ export function getFinalScripts(
1495
1496
  throw new Error(`Can not finalize input #${inputIndex}`);
1496
1497
  }
1497
1498
 
1498
- return prepareFinalScripts(script, scriptType, input.partialSig!, isSegwit, isP2SH, isP2WSH);
1499
+ return prepareFinalScripts(
1500
+ script,
1501
+ scriptType,
1502
+ input.partialSig!,
1503
+ isSegwit,
1504
+ isP2SH,
1505
+ isP2WSH,
1506
+ solution,
1507
+ );
1499
1508
  }
1500
1509
 
1501
1510
  export function prepareFinalScripts(
@@ -1505,6 +1514,7 @@ export function prepareFinalScripts(
1505
1514
  isSegwit: boolean,
1506
1515
  isP2SH: boolean,
1507
1516
  isP2WSH: boolean,
1517
+ solution?: Buffer[],
1508
1518
  ): {
1509
1519
  finalScriptSig: Buffer | undefined;
1510
1520
  finalScriptWitness: Buffer | undefined;
@@ -1520,17 +1530,25 @@ export function prepareFinalScripts(
1520
1530
  if (isSegwit) {
1521
1531
  if (p2wsh) {
1522
1532
  finalScriptWitness = witnessStackToScriptWitness(p2wsh.witness!);
1523
- } else {
1533
+ } else if (payment) {
1524
1534
  finalScriptWitness = witnessStackToScriptWitness(payment.witness!);
1535
+ } else {
1536
+ // nonstandard segwit script
1537
+ finalScriptWitness = witnessStackToScriptWitness(solution ?? [Buffer.from([0x00])]);
1525
1538
  }
1526
1539
  if (p2sh) {
1527
- finalScriptSig = p2sh.input;
1540
+ finalScriptSig = p2sh?.input;
1528
1541
  }
1529
1542
  } else {
1530
1543
  if (p2sh) {
1531
- finalScriptSig = p2sh.input;
1544
+ finalScriptSig = p2sh?.input;
1532
1545
  } else {
1533
- finalScriptSig = payment.input;
1546
+ if (!payment) {
1547
+ finalScriptSig =
1548
+ Array.isArray(solution) && solution[0] ? solution[0] : Buffer.from([0x01]);
1549
+ } else {
1550
+ finalScriptSig = payment.input;
1551
+ }
1534
1552
  }
1535
1553
  }
1536
1554
  return {
@@ -1837,9 +1855,18 @@ function getScriptFromInput(
1837
1855
  res.script = input.witnessUtxo.script;
1838
1856
  }
1839
1857
  }
1858
+
1840
1859
  if (input.witnessScript || isP2WPKH(res.script!)) {
1841
1860
  res.isSegwit = true;
1861
+ } else {
1862
+ try {
1863
+ const output = res.script;
1864
+ if (!output) throw new TypeError('Invalid script for segwit address');
1865
+
1866
+ res.isSegwit = isUnknownSegwitVersion(output);
1867
+ } catch (e) {}
1842
1868
  }
1869
+
1843
1870
  return res;
1844
1871
  }
1845
1872
 
@@ -1990,7 +2017,9 @@ function inputFinalizeGetAmts(
1990
2017
  const fee = inputAmount - outputAmount;
1991
2018
  if (!disableOutputChecks) {
1992
2019
  if (fee < 0) {
1993
- throw new Error('Outputs are spending more than Inputs');
2020
+ throw new Error(
2021
+ `Outputs are spending more than Inputs ${inputAmount} < ${outputAmount}`,
2022
+ );
1994
2023
  }
1995
2024
  }
1996
2025
  const bytes = tx.virtualSize();