Skip to content

Commit 9a70b83

Browse files
committed
Fixup: der2compact() and compac2der() methods have been removed, signInputCompact() has been added
1 parent 676e769 commit 9a70b83

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

src/main/scala/fr/acinq/bitcoin/scalacompat/Crypto.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ object Crypto {
240240

241241
def isDefinedHashTypeSignature(sig: ByteVector): Boolean = bitcoin.Crypto.isDefinedHashTypeSignature(sig.toArray)
242242

243-
def compact2der(signature: ByteVector64): ByteVector = bitcoin.Crypto.compact2der(signature)
244-
245-
def der2compact(signature: ByteVector): ByteVector64 = bitcoin.Crypto.der2compact(signature.toArray)
246-
247243
/**
248244
* @param data data
249245
* @param signature signature

src/main/scala/fr/acinq/bitcoin/scalacompat/Transaction.scala

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ object Transaction extends BtcSerializer[Transaction] {
282282
* @param amount amount of the output claimed by this tx input
283283
* @param signatureVersion signature version (1: segwit, 0: pre-segwit)
284284
* @param privateKey private key
285-
* @return the encoded signature of this tx for this specific tx input
285+
* @return the encoded signature (DER + sighash byte) of this tx for this specific tx input
286286
*/
287287
def signInput(tx: Transaction, inputIndex: Int, previousOutputScript: ByteVector, sighashType: Int, amount: Satoshi, signatureVersion: Int, privateKey: PrivateKey): ByteVector = {
288288
tx.signInput(inputIndex, previousOutputScript, sighashType, amount, signatureVersion, privateKey)
@@ -298,7 +298,7 @@ object Transaction extends BtcSerializer[Transaction] {
298298
* @param amount amount of the output claimed by this tx input
299299
* @param signatureVersion signature version (1: segwit, 0: pre-segwit)
300300
* @param privateKey private key
301-
* @return the encoded signature of this tx for this specific tx input
301+
* @return the encoded signature (DER + sighash byte) of this tx for this specific tx input
302302
*/
303303
def signInput(tx: Transaction, inputIndex: Int, previousOutputScript: Seq[ScriptElt], sighashType: Int, amount: Satoshi, signatureVersion: Int, privateKey: PrivateKey): ByteVector =
304304
signInput(tx, inputIndex, Script.write(previousOutputScript), sighashType, amount, signatureVersion, privateKey)
@@ -484,6 +484,36 @@ case class Transaction(version: Long, txIn: Seq[TxIn], txOut: Seq[TxOut], lockTi
484484
scala2kmp(this).hashForSigningTaprootScriptPath(inputIndex, inputs.map(scala2kmp).asJava, sighashType, scala2kmp(tapleaf), annex_opt.map(scala2kmp).orNull)
485485
}
486486

487+
/**
488+
* sign a tx input
489+
*
490+
* @param inputIndex index of the tx input that is being processed
491+
* @param previousOutputScript public key script of the output claimed by this tx input
492+
* @param sighashType signature hash type, which will be appended to the signature
493+
* @param amount amount of the output claimed by this tx input
494+
* @param signatureVersion signature version (1: segwit, 0: pre-segwit)
495+
* @param privateKey private key
496+
* @return the encoded signature of this tx for this specific tx input in compact 64 bytes format
497+
*/
498+
def signInputCompact(inputIndex: Int, previousOutputScript: ByteVector, sighashType: Int, amount: Satoshi, signatureVersion: Int, privateKey: PrivateKey): ByteVector64 = {
499+
scala2kmp(this).signInputCompact(inputIndex, scala2kmp(previousOutputScript), sighashType, amount, signatureVersion, privateKey.priv)
500+
}
501+
502+
/**
503+
* sign a tx input
504+
*
505+
* @param inputIndex index of the tx input that is being processed
506+
* @param previousOutputScript public key script of the output claimed by this tx input
507+
* @param sighashType signature hash type, which will be appended to the signature
508+
* @param amount amount of the output claimed by this tx input
509+
* @param signatureVersion signature version (1: segwit, 0: pre-segwit)
510+
* @param privateKey private key
511+
* @return the encoded signature of this tx for this specific tx input in compact 64 bytes format
512+
*/
513+
def signInputCompact(inputIndex: Int, previousOutputScript: Seq[ScriptElt], sighashType: Int, amount: Satoshi, signatureVersion: Int, privateKey: PrivateKey): ByteVector64 =
514+
signInputCompact(inputIndex, Script.write(previousOutputScript), sighashType, amount, signatureVersion, privateKey)
515+
516+
487517
/**
488518
* sign a tx input
489519
*

src/test/scala/fr/acinq/bitcoin/scalacompat/CryptoSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fr.acinq.bitcoin.scalacompat
33
import fr.acinq.bitcoin.Base58.Prefix
44
import fr.acinq.bitcoin.scalacompat.Crypto._
55
import fr.acinq.bitcoin.{Base58, Base58Check}
6+
import fr.acinq.secp256k1.Secp256k1
67
import org.scalatest.FlatSpec
78
import scodec.bits._
89

@@ -157,7 +158,7 @@ class CryptoSpec extends FlatSpec {
157158

158159
dataset.map {
159160
case (k, m, s) =>
160-
val sig: ByteVector = Crypto.compact2der(Crypto.sign(Crypto.sha256(ByteVector.view(m.getBytes("UTF-8"))), PrivateKey(k)))
161+
val sig: ByteVector = ByteVector.view(Secp256k1.get().compact2der(Crypto.sign(Crypto.sha256(ByteVector.view(m.getBytes("UTF-8"))), PrivateKey(k)).toArray))
161162
assert(sig == s)
162163
}
163164
}

src/test/scala/fr/acinq/bitcoin/scalacompat/SegwitSpec.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fr.acinq.bitcoin.scalacompat
22

33
import fr.acinq.bitcoin.scalacompat.Crypto.PrivateKey
44
import fr.acinq.bitcoin.{Base58, Base58Check, ScriptFlags, SigHash, SigVersion}
5+
import fr.acinq.secp256k1.Secp256k1
56
import org.scalatest.FunSuite
67
import scodec.bits._
78

@@ -30,7 +31,8 @@ class SegwitSpec extends FunSuite {
3031
val priv = PrivateKey(hex"619c335025c7f4012e556c2a58b2506e30b8511b53ade95ea316fd8c3286feb901")
3132
val pub = priv.publicKey
3233
val sig = hex"304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee"
33-
assert(Crypto.verifySignature(hash, Crypto.der2compact(sig), pub))
34+
val compact = ByteVector64(ByteVector(Secp256k1.get().der2compact(sig.toArray)))
35+
assert(Crypto.verifySignature(hash, ByteVector64(compact), pub))
3436

3537
val sigScript = hex"4830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01"
3638
val tx1 = tx.updateSigScript(0, sigScript)

0 commit comments

Comments
 (0)