Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add high-level helpers for using Musig2 with Taproot #114

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Crypto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,6 @@ public object Crypto {
return sig
}

/** Produce a signature that will be included in the witness of a taproot key path spend. */
@JvmStatic
public fun signTaprootKeyPath(privateKey: PrivateKey, tx: Transaction, inputIndex: Int, inputs: List<TxOut>, sighashType: Int, scriptTree: ScriptTree?, annex: ByteVector? = null, auxrand32: ByteVector32? = null): ByteVector64 {
val data = Transaction.hashForSigningTaprootKeyPath(tx, inputIndex, inputs, sighashType, annex)
val tweak = when (scriptTree) {
null -> TaprootTweak.NoScriptTweak
else -> TaprootTweak.ScriptTweak(scriptTree.hash())
}
return signSchnorr(data, privateKey, tweak, auxrand32)
}

/** Produce a signature that will be included in the witness of a taproot script path spend. */
@JvmStatic
public fun signTaprootScriptPath(privateKey: PrivateKey, tx: Transaction, inputIndex: Int, inputs: List<TxOut>, sighashType: Int, tapleaf: ByteVector32, annex: ByteVector? = null, auxrand32: ByteVector32? = null): ByteVector64 {
val data = Transaction.hashForSigningTaprootScriptPath(tx, inputIndex, inputs, sighashType, tapleaf, annex)
return signSchnorr(data, privateKey, SchnorrTweak.NoTweak, auxrand32)
}

@JvmStatic
public fun verifySignatureSchnorr(data: ByteVector32, signature: ByteVector, publicKey: XonlyPublicKey): Boolean {
return Secp256k1.verifySchnorr(signature.toByteArray(), data.toByteArray(), publicKey.value.toByteArray())
Expand Down
47 changes: 47 additions & 0 deletions src/commonMain/kotlin/fr/acinq/bitcoin/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,53 @@ public data class Transaction(
return hashForSigningSchnorr(tx, inputIndex, inputs, sighashType, SigVersion.SIGVERSION_TAPSCRIPT, tapleaf, annex)
}

/**
* Sign a taproot tx input, using the internal key path.
*
* @param privateKey private key.
* @param tx input transaction.
* @param inputIndex index of the tx input that is being signed.
* @param inputs list of all UTXOs spent by this transaction.
* @param sighashType signature hash type, which will be appended to the signature (if not default).
* @param scriptTree tapscript tree of the signed input, if it has script paths.
* @return the schnorr signature of this tx for this specific tx input.
*/
@JvmStatic
public fun signInputTaprootKeyPath(privateKey: PrivateKey, tx: Transaction, inputIndex: Int, inputs: List<TxOut>, sighashType: Int, scriptTree: ScriptTree?, annex: ByteVector? = null, auxrand32: ByteVector32? = null): ByteVector64 {
val data = hashForSigningTaprootKeyPath(tx, inputIndex, inputs, sighashType, annex)
val tweak = when (scriptTree) {
null -> Crypto.TaprootTweak.NoScriptTweak
else -> Crypto.TaprootTweak.ScriptTweak(scriptTree.hash())
}
return Crypto.signSchnorr(data, privateKey, tweak, auxrand32)
}

/**
* Sign a taproot tx input, using one of its script paths.
*
* @param privateKey private key.
* @param tx input transaction.
* @param inputIndex index of the tx input that is being signed.
* @param inputs list of all UTXOs spent by this transaction.
* @param sighashType signature hash type, which will be appended to the signature (if not default).
* @param tapleaf tapscript leaf hash of the script that is being spent.
* @return the schnorr signature of this tx for this specific tx input and the given script leaf.
*/
@JvmStatic
public fun signInputTaprootScriptPath(
privateKey: PrivateKey,
tx: Transaction,
inputIndex: Int,
inputs: List<TxOut>,
sighashType: Int,
tapleaf: ByteVector32,
annex: ByteVector? = null,
auxrand32: ByteVector32? = null
): ByteVector64 {
val data = hashForSigningTaprootScriptPath(tx, inputIndex, inputs, sighashType, tapleaf, annex)
return Crypto.signSchnorr(data, privateKey, Crypto.SchnorrTweak.NoTweak, auxrand32)
}

@JvmStatic
public fun correctlySpends(tx: Transaction, previousOutputs: Map<OutPoint, TxOut>, scriptFlags: Int) {
val prevouts = tx.txIn.map { previousOutputs[it.outPoint]!! }
Expand Down
Loading
Loading