@bsv/sdk 1.0.15 → 1.0.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -7,6 +7,7 @@ import Transaction from '../../transaction/Transaction.js'
7
7
  import PrivateKey from '../../primitives/PrivateKey.js'
8
8
  import TransactionSignature from '../../primitives/TransactionSignature.js'
9
9
  import { sha256 } from '../../primitives/Hash.js'
10
+ import Script from '../Script.js'
10
11
 
11
12
  /**
12
13
  * P2PKH (Pay To Public Key Hash) class implementing ScriptTemplate.
@@ -49,12 +50,16 @@ export default class P2PKH implements ScriptTemplate {
49
50
  * @param {PrivateKey} privateKey - The private key used for signing the transaction.
50
51
  * @param {'all'|'none'|'single'} signOutputs - The signature scope for outputs.
51
52
  * @param {boolean} anyoneCanPay - Flag indicating if the signature allows for other inputs to be added later.
53
+ * @param {number} sourceSatoshis - Optional. The amount being unlocked. Otherwise the input.sourceTransaction is required.
54
+ * @param {Script} lockingScript - Optional. The lockinScript. Otherwise the input.sourceTransaction is required.
52
55
  * @returns {Object} - An object containing the `sign` and `estimateLength` functions.
53
56
  */
54
57
  unlock(
55
58
  privateKey: PrivateKey,
56
59
  signOutputs: 'all' | 'none' | 'single' = 'all',
57
- anyoneCanPay: boolean = false
60
+ anyoneCanPay: boolean = false,
61
+ sourceSatoshis?: number,
62
+ lockingScript?: Script
58
63
  ): {
59
64
  sign: (tx: Transaction, inputIndex: number) => Promise<UnlockingScript>
60
65
  estimateLength: () => Promise<106>
@@ -74,24 +79,40 @@ export default class P2PKH implements ScriptTemplate {
74
79
  if (anyoneCanPay) {
75
80
  signatureScope |= TransactionSignature.SIGHASH_ANYONECANPAY
76
81
  }
82
+
77
83
  const input = tx.inputs[inputIndex]
84
+
78
85
  const otherInputs = tx.inputs.filter((_, index) => index !== inputIndex)
79
- if (typeof input.sourceTransaction !== 'object') {
80
- // Question: Should the library support use-cases where the source transaction is not provided? This is to say, is it ever acceptable for someone to sign an input spending some output from a transaction they have not provided? Some elements (such as the satoshi value and output script) are always required. A merkle proof is also always required, and verifying it (while also verifying that the claimed output is contained within the claimed transaction) is also always required. This seems to require the entire input transaction.
86
+
87
+ const sourceTXID = input.sourceTXID ? input.sourceTXID : input.sourceTransaction?.id('hex') as string
88
+ if (!sourceTXID) {
89
+ throw new Error(
90
+ 'The input sourceTXID or sourceTransaction is required for transaction signing.'
91
+ )
92
+ }
93
+ sourceSatoshis ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].satoshis
94
+ if (!sourceSatoshis) {
81
95
  throw new Error(
82
- 'The source transaction is needed for transaction signing.'
96
+ 'The sourceSatoshis or input sourceTransaction is required for transaction signing.'
83
97
  )
84
98
  }
99
+ lockingScript ||= input.sourceTransaction?.outputs[input.sourceOutputIndex].lockingScript
100
+ if (!lockingScript) {
101
+ throw new Error(
102
+ 'The lockingScript or input sourceTransaction is required for transaction signing.'
103
+ )
104
+ }
105
+
85
106
  const preimage = TransactionSignature.format({
86
- sourceTXID: input.sourceTransaction.id('hex') as string,
107
+ sourceTXID,
87
108
  sourceOutputIndex: input.sourceOutputIndex,
88
- sourceSatoshis: input.sourceTransaction.outputs[input.sourceOutputIndex].satoshis,
109
+ sourceSatoshis,
89
110
  transactionVersion: tx.version,
90
111
  otherInputs,
91
112
  inputIndex,
92
113
  outputs: tx.outputs,
93
114
  inputSequence: input.sequence,
94
- subscript: input.sourceTransaction.outputs[input.sourceOutputIndex].lockingScript,
115
+ subscript: lockingScript,
95
116
  lockTime: tx.lockTime,
96
117
  scope: signatureScope
97
118
  })