Skip to content

Commit

Permalink
Use bitcoin-kmp 0.13.0 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstone authored Aug 21, 2023
1 parent 56de0ac commit 89f7928
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@
<dependency>
<groupId>fr.acinq.bitcoin</groupId>
<artifactId>bitcoin-kmp-jvm</artifactId>
<version>0.12.0</version>
<version>0.13.0</version>
</dependency>
<dependency>
<groupId>fr.acinq.secp256k1</groupId>
<artifactId>secp256k1-kmp-jni-jvm</artifactId>
<version>0.10.0</version>
<version>0.10.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down
19 changes: 10 additions & 9 deletions src/main/scala/fr/acinq/bitcoin/scalacompat/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,22 @@ package object scalacompat {
* @param script public key script
* @return the address of this public key script on this chain
*/
def computeScriptAddress(chainHash: ByteVector32, script: Seq[ScriptElt]): Option[String] = {
bitcoin.Bitcoin.addressFromPublicKeyScript(chainHash, script.map(scala2kmp).asJava) match {
case null => None
case address => Some(address)
}
}
def computeScriptAddress(chainHash: ByteVector32, script: Seq[ScriptElt]): Either[AddressFromPublicKeyScriptResult.Failure, String] = addressFromPublicKeyScript(chainHash, script)

/**
* @param chainHash hash of the chain (i.e. hash of the genesis block of the chain we're on)
* @param script public key script
* @return the address of this public key script on this chain
*/
def computeScriptAddress(chainHash: ByteVector32, script: ByteVector): Option[String] = computeScriptAddress(chainHash, Script.parse(script))
def computeScriptAddress(chainHash: ByteVector32, script: ByteVector): Either[AddressFromPublicKeyScriptResult.Failure, String] = computeScriptAddress(chainHash, Script.parse(script))

def addressToPublicKeyScript(chainHash: ByteVector32, address: String): Seq[ScriptElt] = fr.acinq.bitcoin.Bitcoin.addressToPublicKeyScript(chainHash, address).asScala.map(kmp2scala).toList
def addressToPublicKeyScript(chainHash: ByteVector32, address: String): Either[AddressToPublicKeyScriptResult.Failure, Seq[ScriptElt]] = fr.acinq.bitcoin.Bitcoin.addressToPublicKeyScript(chainHash, address) match {
case success: AddressToPublicKeyScriptResult.Success => Right(success.getResult.asScala.map(kmp2scala).toList)
case failure: AddressToPublicKeyScriptResult.Failure => Left(failure)
}

def addressFromPublicKeyScript(chainHash: ByteVector32, script: Seq[ScriptElt]): String = fr.acinq.bitcoin.Bitcoin.addressFromPublicKeyScript(chainHash, script.map(scala2kmp).asJava)
def addressFromPublicKeyScript(chainHash: ByteVector32, script: Seq[ScriptElt]): Either[AddressFromPublicKeyScriptResult.Failure, String] = fr.acinq.bitcoin.Bitcoin.addressFromPublicKeyScript(chainHash, script.map(scala2kmp).asJava) match {
case success: AddressFromPublicKeyScriptResult.Success => Right(success.getAddress)
case failure: AddressFromPublicKeyScriptResult.Failure => Left(failure)
}
}
12 changes: 6 additions & 6 deletions src/test/scala/fr/acinq/bitcoin/scalacompat/TaprootSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.acinq.bitcoin.scalacompat

import fr.acinq.bitcoin.Crypto.{SchnorrTweak, TaprootTweak}
import fr.acinq.bitcoin.Crypto.TaprootTweak
import fr.acinq.bitcoin.scalacompat.Crypto.{PrivateKey, PublicKey, XonlyPublicKey}
import fr.acinq.bitcoin.scalacompat.KotlinUtils._
import fr.acinq.bitcoin.scalacompat.Transaction.hashForSigningSchnorr
Expand Down Expand Up @@ -66,7 +66,7 @@ class TaprootSpec extends FunSuite {
assert(Script.pay2tr(outputKey) == Script.parse(tx.txOut(1).publicKeyScript))

// we want to spend
val outputScript = addressToPublicKeyScript(Block.TestnetGenesisBlock.hash, "tb1pn3g330w4n5eut7d4vxq0pp303267qc6vg8d2e0ctjuqre06gs3yqnc5yx0")
val Right(outputScript) = addressToPublicKeyScript(Block.TestnetGenesisBlock.hash, "tb1pn3g330w4n5eut7d4vxq0pp303267qc6vg8d2e0ctjuqre06gs3yqnc5yx0")
val tx1 = Transaction(
2,
TxIn(OutPoint(tx, 1), ByteVector.empty, fr.acinq.bitcoin.TxIn.SEQUENCE_FINAL) :: Nil,
Expand Down Expand Up @@ -165,7 +165,7 @@ class TaprootSpec extends FunSuite {
val tmp = Transaction(
version = 2,
txIn = TxIn(OutPoint(fundingTx, 0), ByteVector.empty, fr.acinq.bitcoin.TxIn.SEQUENCE_FINAL) :: Nil,
txOut = TxOut(fundingTx.txOut(0).amount - Satoshi(5000), addressToPublicKeyScript(Block.RegtestGenesisBlock.hash, "bcrt1qdtu5cwyngza8hw8s5uk2erlrkh8ceh3msp768v")) :: Nil,
txOut = TxOut(fundingTx.txOut(0).amount - Satoshi(5000), addressToPublicKeyScript(Block.RegtestGenesisBlock.hash, "bcrt1qdtu5cwyngza8hw8s5uk2erlrkh8ceh3msp768v").toOption.get) :: Nil,
lockTime = 0
)
val hash = hashForSigningSchnorr(tmp, 0, Seq(fundingTx.txOut(0)), SigHash.SIGHASH_DEFAULT, SigVersion.SIGVERSION_TAPSCRIPT, Script.ExecutionData(annex = None, tapleafHash = Some(merkleRoot)))
Expand Down Expand Up @@ -224,14 +224,14 @@ class TaprootSpec extends FunSuite {
val script = Script.write(Seq(OP_1, OP_PUSHDATA(tweakedKey)))
val bip350Address = Bech32.encodeWitnessAddress(Bech32.hrp(blockchain), 1.toByte, tweakedKey.pub.value.toByteArray)
assert(bip350Address == "tb1p78gx95syx0qz8w5nftk8t7nce78zlpqpsxugcvq5xpfy4tvn6rasd7wk0y")
val sweepPublicKeyScript = addressToPublicKeyScript(blockchain, "tb1qxy9hhxkw7gt76qrm4yzw4j06gkk4evryh8ayp7")
val Right(sweepPublicKeyScript) = addressToPublicKeyScript(blockchain, "tb1qxy9hhxkw7gt76qrm4yzw4j06gkk4evryh8ayp7")

// see https://mempool.space/signet/tx/c284010f06b5182e9f4722ce3474980339b1fc76e5ff29ece812f5d2162595c1
val fundingTx = Transaction.read("020000000001017034061243a7770f791aa2afdb118be900f4f8fc755a36d8632213acc139bab20100000000feffffff0200e1f50500000000225120f1d062d20433c023ba934aec75fa78cf8e2f840181b88c301430524aad93d0fbc192ac1700000000160014b66f2e807b9f4adecb99ad811dde501ca3f0fd5f02473044022046a2fd077e12b1d7ba74f6e7ac469deb3e3755c100216abad667980fc39463dc022018b63cfaf72fde0b5ca10c617aeaa0015013bd06ef08f82eea500c6467d963cc0121030b50ec81d958ae79d34d3579faf72456213d7d581a908e2b64d21b96777882043ab10100")

// output #1 is the one we want to spend
assert(fundingTx.txOut(0).publicKeyScript == script)
assert(addressToPublicKeyScript(blockchain, bip350Address) == Seq(OP_1, OP_PUSHDATA(tweakedKey)))
assert(addressToPublicKeyScript(blockchain, bip350Address) == Right(Seq(OP_1, OP_PUSHDATA(tweakedKey))))

// spending with the key path: no need to provide any script
val tx = {
Expand Down Expand Up @@ -314,7 +314,7 @@ class TaprootSpec extends FunSuite {
val tmp = Transaction(
version = 2,
txIn = Seq(TxIn(OutPoint(fundingTx3, 1), ByteVector.empty, TxIn.SEQUENCE_FINAL)),
txOut = Seq(TxOut(fundingTx3.txOut(0).amount - Satoshi(5000), addressToPublicKeyScript(blockchain, "tb1qxy9hhxkw7gt76qrm4yzw4j06gkk4evryh8ayp7"))),
txOut = Seq(TxOut(fundingTx3.txOut(0).amount - Satoshi(5000), addressToPublicKeyScript(blockchain, "tb1qxy9hhxkw7gt76qrm4yzw4j06gkk4evryh8ayp7").toOption.get)),
lockTime = 0
)
// to re-compute the merkle root we need to provide branch(#1, #2)
Expand Down

0 comments on commit 89f7928

Please sign in to comment.