@@ -192,7 +192,7 @@ object Transaction extends BtcSerializer[Transaction] {
192
192
* @return a new transaction with proper inputs and outputs according to SIGHASH_TYPE rules
193
193
*/
194
194
def prepareForSigning (tx : Transaction , inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int ): Transaction = {
195
- fr.acinq.bitcoin. Transaction . prepareForSigning(tx, inputIndex, previousOutputScript.toArray , sighashType)
195
+ tx. prepareForSigning(inputIndex, previousOutputScript, sighashType)
196
196
}
197
197
198
198
/**
@@ -231,7 +231,7 @@ object Transaction extends BtcSerializer[Transaction] {
231
231
* @return a hash which can be used to sign the referenced tx input
232
232
*/
233
233
def hashForSigning (tx : Transaction , inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int , amount : Satoshi , signatureVersion : Int ): ByteVector32 = {
234
- ByteVector32 ( ByteVector .view(fr.acinq.bitcoin. Transaction . hashForSigning(tx, inputIndex, previousOutputScript.toArray , sighashType, amount, signatureVersion)) )
234
+ tx. hashForSigning(inputIndex, previousOutputScript, sighashType, amount, signatureVersion)
235
235
}
236
236
237
237
/**
@@ -257,17 +257,17 @@ object Transaction extends BtcSerializer[Transaction] {
257
257
* @param annex_opt (optional) taproot annex
258
258
*/
259
259
def hashForSigningSchnorr (tx : Transaction , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , sigVersion : Int , tapleaf_opt : Option [ByteVector32 ] = None , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
260
- bitcoin. Transaction . hashForSigningSchnorr(tx, inputIndex, inputs.map(scala2kmp).asJava , sighashType, sigVersion, tapleaf_opt.map(scala2kmp).orNull , annex_opt.map(scala2kmp).orNull, null )
260
+ tx. hashForSigningSchnorr(inputIndex, inputs, sighashType, sigVersion, tapleaf_opt, annex_opt)
261
261
}
262
262
263
263
/** Use this function when spending a taproot key path. */
264
264
def hashForSigningTaprootKeyPath (tx : Transaction , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
265
- bitcoin. Transaction . hashForSigningTaprootKeyPath(tx, inputIndex, inputs.map(scala2kmp).asJava , sighashType, annex_opt.map(scala2kmp).orNull )
265
+ tx. hashForSigningTaprootKeyPath(inputIndex, inputs, sighashType, annex_opt)
266
266
}
267
267
268
268
/** Use this function when spending a taproot script path. */
269
269
def hashForSigningTaprootScriptPath (tx : Transaction , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , tapleaf : ByteVector32 , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
270
- bitcoin. Transaction . hashForSigningTaprootScriptPath(tx, inputIndex, inputs.map(scala2kmp).asJava , sighashType, scala2kmp( tapleaf) , annex_opt.map(scala2kmp).orNull )
270
+ tx. hashForSigningTaprootScriptPath(inputIndex, inputs, sighashType, tapleaf, annex_opt)
271
271
}
272
272
273
273
/**
@@ -283,7 +283,7 @@ object Transaction extends BtcSerializer[Transaction] {
283
283
* @return the encoded signature of this tx for this specific tx input
284
284
*/
285
285
def signInput (tx : Transaction , inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int , amount : Satoshi , signatureVersion : Int , privateKey : PrivateKey ): ByteVector = {
286
- ByteVector .view(fr.acinq.bitcoin. Transaction . signInput(tx, inputIndex, scala2kmp( previousOutputScript) , sighashType, amount, signatureVersion, privateKey.priv) )
286
+ tx. signInput(inputIndex, previousOutputScript, sighashType, amount, signatureVersion, privateKey)
287
287
}
288
288
289
289
/**
@@ -313,7 +313,7 @@ object Transaction extends BtcSerializer[Transaction] {
313
313
* @return the schnorr signature of this tx for this specific tx input.
314
314
*/
315
315
def signInputTaprootKeyPath (privateKey : PrivateKey , tx : Transaction , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , scriptTree_opt : Option [bitcoin.ScriptTree ], annex_opt : Option [ByteVector ] = None , auxrand32 : Option [ByteVector32 ] = None ): ByteVector64 = {
316
- bitcoin. Transaction . signInputTaprootKeyPath(privateKey, tx, inputIndex, inputs.map(scala2kmp).asJava , sighashType, scriptTree_opt.orNull , annex_opt.map(scala2kmp).orNull , auxrand32.map(scala2kmp).orNull )
316
+ tx. signInputTaprootKeyPath(privateKey, inputIndex, inputs, sighashType, scriptTree_opt, annex_opt, auxrand32)
317
317
}
318
318
319
319
/**
@@ -328,20 +328,15 @@ object Transaction extends BtcSerializer[Transaction] {
328
328
* @return the schnorr signature of this tx for this specific tx input and the given script leaf.
329
329
*/
330
330
def signInputTaprootScriptPath (privateKey : PrivateKey , tx : Transaction , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , tapleaf : ByteVector32 , annex_opt : Option [ByteVector ] = None , auxrand32 : Option [ByteVector32 ] = None ): ByteVector64 = {
331
- bitcoin. Transaction . signInputTaprootScriptPath(privateKey, tx, inputIndex, inputs.map(scala2kmp).asJava , sighashType, tapleaf, annex_opt.map(scala2kmp).orNull , auxrand32.map(scala2kmp).orNull )
331
+ tx. signInputTaprootScriptPath(privateKey, inputIndex, inputs, sighashType, tapleaf, annex_opt, auxrand32)
332
332
}
333
333
334
334
def correctlySpends (tx : Transaction , previousOutputs : Map [OutPoint , TxOut ], scriptFlags : Int ): Unit = {
335
- fr.acinq.bitcoin. Transaction . correctlySpends(tx, previousOutputs.map { case (o, t) => scala2kmp(o) -> scala2kmp(t) }.asJava , scriptFlags)
335
+ tx. correctlySpends(previousOutputs, scriptFlags)
336
336
}
337
337
338
338
def correctlySpends (tx : Transaction , inputs : Seq [Transaction ], scriptFlags : Int ): Unit = {
339
- val prevouts = tx.txIn.map(_.outPoint).map(outpoint => {
340
- val prevTx = inputs.find(_.txid == outpoint.txid).get
341
- val prevOutput = prevTx.txOut(outpoint.index.toInt)
342
- outpoint -> prevOutput
343
- }).toMap
344
- correctlySpends(tx, prevouts, scriptFlags)
339
+ tx.correctlySpends(inputs, scriptFlags)
345
340
}
346
341
}
347
342
@@ -428,5 +423,129 @@ case class Transaction(version: Long, txIn: Seq[TxIn], txOut: Seq[TxOut], lockTi
428
423
429
424
def weight (protocolVersion : Long = PROTOCOL_VERSION ): Int = Transaction .weight(this , protocolVersion)
430
425
426
+ /**
427
+ * prepare a transaction for signing a specific input
428
+ *
429
+ * @param inputIndex index of the tx input that is being processed
430
+ * @param previousOutputScript public key script of the output claimed by this tx input
431
+ * @param sighashType signature hash type
432
+ * @return a new transaction with proper inputs and outputs according to SIGHASH_TYPE rules
433
+ */
434
+ def prepareForSigning (inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int ): Transaction = {
435
+ scala2kmp(this ).prepareForSigning(inputIndex, previousOutputScript.toArray, sighashType)
436
+ }
437
+
438
+ /**
439
+ * hash a tx for signing
440
+ *
441
+ * @param inputIndex index of the tx input that is being processed
442
+ * @param previousOutputScript public key script of the output claimed by this tx input
443
+ * @param sighashType signature hash type
444
+ * @param amount amount of the output claimed by this input
445
+ * @return a hash which can be used to sign the referenced tx input
446
+ */
447
+ def hashForSigning (inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int , amount : Satoshi , signatureVersion : Int ): ByteVector32 = {
448
+ ByteVector32 (ByteVector .view(scala2kmp(this ).hashForSigning(inputIndex, previousOutputScript.toArray, sighashType, amount, signatureVersion)))
449
+ }
450
+
451
+ /**
452
+ * hash a tx for signing
453
+ *
454
+ * @param inputIndex index of the tx input that is being processed
455
+ * @param previousOutputScript public key script of the output claimed by this tx input
456
+ * @param sighashType signature hash type
457
+ * @param amount amount of the output claimed by this input
458
+ * @return a hash which can be used to sign the referenced tx input
459
+ */
460
+ def hashForSigning (inputIndex : Int , previousOutputScript : Seq [ScriptElt ], sighashType : Int , amount : Satoshi , signatureVersion : Int ): ByteVector32 =
461
+ hashForSigning(inputIndex, Script .write(previousOutputScript), sighashType, amount, signatureVersion)
462
+
463
+ /**
464
+ * @param inputIndex index of the transaction input being signed
465
+ * @param inputs UTXOs spent by this transaction
466
+ * @param sighashType signature hash type
467
+ * @param sigVersion signature version
468
+ * @param tapleaf_opt when spending a tapscript, the hash of the corresponding script leaf must be provided
469
+ * @param annex_opt (optional) taproot annex
470
+ */
471
+ def hashForSigningSchnorr (inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , sigVersion : Int , tapleaf_opt : Option [ByteVector32 ] = None , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
472
+ scala2kmp(this ).hashForSigningSchnorr(inputIndex, inputs.map(scala2kmp).asJava, sighashType, sigVersion, tapleaf_opt.map(scala2kmp).orNull, annex_opt.map(scala2kmp).orNull, null )
473
+ }
474
+
475
+ /** Use this function when spending a taproot key path. */
476
+ def hashForSigningTaprootKeyPath (inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
477
+ scala2kmp(this ).hashForSigningTaprootKeyPath(inputIndex, inputs.map(scala2kmp).asJava, sighashType, annex_opt.map(scala2kmp).orNull)
478
+ }
479
+
480
+ /** Use this function when spending a taproot script path. */
481
+ def hashForSigningTaprootScriptPath (inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , tapleaf : ByteVector32 , annex_opt : Option [ByteVector ] = None ): ByteVector32 = {
482
+ scala2kmp(this ).hashForSigningTaprootScriptPath(inputIndex, inputs.map(scala2kmp).asJava, sighashType, scala2kmp(tapleaf), annex_opt.map(scala2kmp).orNull)
483
+ }
484
+
485
+ /**
486
+ * sign a tx input
487
+ *
488
+ * @param inputIndex index of the tx input that is being processed
489
+ * @param previousOutputScript public key script of the output claimed by this tx input
490
+ * @param sighashType signature hash type, which will be appended to the signature
491
+ * @param amount amount of the output claimed by this tx input
492
+ * @param signatureVersion signature version (1: segwit, 0: pre-segwit)
493
+ * @param privateKey private key
494
+ * @return the encoded signature of this tx for this specific tx input
495
+ */
496
+ def signInput (inputIndex : Int , previousOutputScript : ByteVector , sighashType : Int , amount : Satoshi , signatureVersion : Int , privateKey : PrivateKey ): ByteVector = {
497
+ ByteVector .view(scala2kmp(this ).signInput(inputIndex, scala2kmp(previousOutputScript), sighashType, amount, signatureVersion, privateKey.priv))
498
+ }
499
+
500
+ /**
501
+ * sign a tx input
502
+ *
503
+ * @param inputIndex index of the tx input that is being processed
504
+ * @param previousOutputScript public key script of the output claimed by this tx input
505
+ * @param sighashType signature hash type, which will be appended to the signature
506
+ * @param amount amount of the output claimed by this tx input
507
+ * @param signatureVersion signature version (1: segwit, 0: pre-segwit)
508
+ * @param privateKey private key
509
+ * @return the encoded signature of this tx for this specific tx input
510
+ */
511
+ def signInput (inputIndex : Int , previousOutputScript : Seq [ScriptElt ], sighashType : Int , amount : Satoshi , signatureVersion : Int , privateKey : PrivateKey ): ByteVector =
512
+ signInput(inputIndex, Script .write(previousOutputScript), sighashType, amount, signatureVersion, privateKey)
513
+
514
+ /**
515
+ * Sign a taproot tx input, using the internal key path.
516
+ *
517
+ * @param privateKey private key.
518
+ * @param inputIndex index of the tx input that is being signed.
519
+ * @param inputs list of all UTXOs spent by this transaction.
520
+ * @param sighashType signature hash type, which will be appended to the signature (if not default).
521
+ * @param scriptTree_opt tapscript tree of the signed input, if it has script paths.
522
+ * @return the schnorr signature of this tx for this specific tx input.
523
+ */
524
+ def signInputTaprootKeyPath (privateKey : PrivateKey , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , scriptTree_opt : Option [bitcoin.ScriptTree ], annex_opt : Option [ByteVector ] = None , auxrand32 : Option [ByteVector32 ] = None ): ByteVector64 = {
525
+ scala2kmp(this ).signInputTaprootKeyPath(privateKey, inputIndex, inputs.map(scala2kmp).asJava, sighashType, scriptTree_opt.orNull, annex_opt.map(scala2kmp).orNull, auxrand32.map(scala2kmp).orNull)
526
+ }
527
+
528
+ /**
529
+ * Sign a taproot tx input, using one of its script paths.
530
+ *
531
+ * @param privateKey private key.
532
+ * @param inputIndex index of the tx input that is being signed.
533
+ * @param inputs list of all UTXOs spent by this transaction.
534
+ * @param sighashType signature hash type, which will be appended to the signature (if not default).
535
+ * @param tapleaf tapscript leaf hash of the script that is being spent.
536
+ * @return the schnorr signature of this tx for this specific tx input and the given script leaf.
537
+ */
538
+ def signInputTaprootScriptPath (privateKey : PrivateKey , inputIndex : Int , inputs : Seq [TxOut ], sighashType : Int , tapleaf : ByteVector32 , annex_opt : Option [ByteVector ] = None , auxrand32 : Option [ByteVector32 ] = None ): ByteVector64 = {
539
+ scala2kmp(this ).signInputTaprootScriptPath(privateKey, inputIndex, inputs.map(scala2kmp).asJava, sighashType, tapleaf, annex_opt.map(scala2kmp).orNull, auxrand32.map(scala2kmp).orNull)
540
+ }
541
+
542
+ def correctlySpends (previousOutputs : Map [OutPoint , TxOut ], scriptFlags : Int ): Unit = {
543
+ scala2kmp(this ).correctlySpends(previousOutputs.map { case (o, t) => scala2kmp(o) -> scala2kmp(t) }.asJava, scriptFlags)
544
+ }
545
+
546
+ def correctlySpends (inputs : Seq [Transaction ], scriptFlags : Int ): Unit = {
547
+ scala2kmp(this ).correctlySpends(inputs.map(scala2kmp).asJava, scriptFlags)
548
+ }
549
+
431
550
override def serializer : BtcSerializer [Transaction ] = Transaction
432
551
}
0 commit comments