mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-13 11:35:40 +01:00
core: Fix type annotation on TapscriptControlBlock.apply()
(#5951)
* core: Fix type annotation on TapscriptControlBlock.apply() * Fix bugs in TapscriptControlBlock helper methods
This commit is contained in:
parent
ec65fe615c
commit
aeca55f5ef
2 changed files with 24 additions and 6 deletions
|
@ -24,7 +24,7 @@ class TaprootWitnessTest extends BitcoinSUnitTest {
|
||||||
"3052c652d53f6ecb8a55586614e8950cde9ab6fe8e22802e93b3b9139112250b80ebc589aba231af535bb20f7eeec2e412f698c17f3fdc0a2" +
|
"3052c652d53f6ecb8a55586614e8950cde9ab6fe8e22802e93b3b9139112250b80ebc589aba231af535bb20f7eeec2e412f698c17f3fdc0a2" +
|
||||||
"e20924a5e38b21a628a9e3b2a61e35958e60c7f5087c"
|
"e20924a5e38b21a628a9e3b2a61e35958e60c7f5087c"
|
||||||
|
|
||||||
val controlBlock = ControlBlock.fromHex(controlBlockHex)
|
val controlBlock = TapscriptControlBlock.fromHex(controlBlockHex)
|
||||||
|
|
||||||
val merkleRootHex =
|
val merkleRootHex =
|
||||||
"c5c62d7fc595ba5fbe61602eb1a29e2e4763408fe1e2b161beb7cb3c71ebcad9"
|
"c5c62d7fc595ba5fbe61602eb1a29e2e4763408fe1e2b161beb7cb3c71ebcad9"
|
||||||
|
@ -138,4 +138,12 @@ class TaprootWitnessTest extends BitcoinSUnitTest {
|
||||||
val stack = stackHex.map(ByteVector.fromValidHex(_))
|
val stack = stackHex.map(ByteVector.fromValidHex(_))
|
||||||
assert(!TaprootKeyPath.isValid(stack))
|
assert(!TaprootKeyPath.isValid(stack))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it must "have a correct constructor" in {
|
||||||
|
val x = TapscriptControlBlock.apply(controlBlock.bytes.head,
|
||||||
|
controlBlock.p,
|
||||||
|
leafHashes = controlBlock.hashes)
|
||||||
|
assert(x.bytes.toHex == controlBlock.bytes.toHex)
|
||||||
|
assert(x == controlBlock)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ sealed abstract class ControlBlock extends NetworkElement {
|
||||||
|
|
||||||
case class TapscriptControlBlock(bytes: ByteVector) extends ControlBlock {
|
case class TapscriptControlBlock(bytes: ByteVector) extends ControlBlock {
|
||||||
require(TapscriptControlBlock.isValid(bytes),
|
require(TapscriptControlBlock.isValid(bytes),
|
||||||
s"Invalid leaf version for tapscript control block, got=$bytes")
|
s"Invalid tapscript control block, got=$bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A control block that does not have a leaf version defined as per BIP342 This
|
/** A control block that does not have a leaf version defined as per BIP342 This
|
||||||
|
@ -86,11 +86,21 @@ object TapscriptControlBlock extends Factory[TapscriptControlBlock] {
|
||||||
new TapscriptControlBlock(bytes)
|
new TapscriptControlBlock(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
def apply(
|
def fromLeaves(
|
||||||
|
leafVersion: Byte,
|
||||||
internalKey: XOnlyPubKey,
|
internalKey: XOnlyPubKey,
|
||||||
leafHashes: Vector[TapLeaf]): ControlBlock = {
|
leafs: Vector[TapLeaf]): TapscriptControlBlock = {
|
||||||
val bytes = internalKey.bytes ++ ByteVector.concat(leafHashes.map(_.bytes))
|
TapscriptControlBlock(leafVersion, internalKey, leafs.map(_.sha256))
|
||||||
ControlBlock(bytes)
|
}
|
||||||
|
|
||||||
|
def apply(
|
||||||
|
leafVersion: Byte,
|
||||||
|
internalKey: XOnlyPubKey,
|
||||||
|
leafHashes: Vector[Sha256Digest]): TapscriptControlBlock = {
|
||||||
|
val bytes =
|
||||||
|
(leafVersion +: internalKey.bytes) ++ ByteVector
|
||||||
|
.concat(leafHashes.map(_.bytes))
|
||||||
|
TapscriptControlBlock(bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue