-
Notifications
You must be signed in to change notification settings - Fork 47
Add wrapper classes for various Taproot objects #97
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d2da3fd
Add wrapper classes for Musig2 nonces
t-bast 5ce45dd
Add wrapper classes for taproot `ScriptTree`
t-bast 75f9f12
Add wrapper classes for schnorr tweaks
t-bast 044f62c
Set minor version
t-bast 77e62f4
nit: rename `LocalNonce` fields
t-bast 1d87e43
Expose `weight()` on `TxIn` and `TxOut`
t-bast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/scala/fr/acinq/bitcoin/scalacompat/ScriptTree.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package fr.acinq.bitcoin.scalacompat | ||
|
|
||
| import scodec.bits.ByteVector | ||
|
|
||
| /** Simple binary tree structure containing taproot spending scripts. */ | ||
| sealed trait ScriptTree { | ||
|
|
||
| /** Compute the merkle root of the script tree. */ | ||
| def hash(): ByteVector32 = KotlinUtils.kmp2scala(KotlinUtils.scala2kmp(this).hash()) | ||
|
|
||
| /** Return the first leaf with a matching script, if any. */ | ||
| def findScript(script: ByteVector): Option[ScriptTree.Leaf] = this match { | ||
| case leaf: ScriptTree.Leaf if leaf.script == script => Some(leaf) | ||
| case _: ScriptTree.Leaf => None | ||
| case branch: ScriptTree.Branch => branch.left.findScript(script).orElse(branch.right.findScript(script)) | ||
| } | ||
|
|
||
| /** Return the first leaf with a matching leaf hash, if any. */ | ||
| def findScript(leafHash: ByteVector32): Option[ScriptTree.Leaf] = this match { | ||
| case leaf: ScriptTree.Leaf if leaf.hash() == leafHash => Some(leaf) | ||
| case _: ScriptTree.Leaf => None | ||
| case branch: ScriptTree.Branch => branch.left.findScript(leafHash).orElse(branch.right.findScript(leafHash)) | ||
| } | ||
|
|
||
| /** | ||
| * Compute a merkle proof for the given script leaf. | ||
| * This merkle proof is encoded for creating control blocks in taproot script path witnesses. | ||
| * If the leaf doesn't belong to the script tree, this function will return None. | ||
| */ | ||
| def merkleProof(leafHash: ByteVector32): Option[ByteVector] = { | ||
| val proof_opt = KotlinUtils.scala2kmp(this).merkleProof(KotlinUtils.scala2kmp(leafHash)) | ||
| if (proof_opt == null) None else Some(ByteVector(proof_opt)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object ScriptTree { | ||
| /** | ||
| * Multiple spending scripts can be placed in the leaves of a taproot tree. When using one of those scripts to spend | ||
| * funds, we only need to reveal that specific script and a merkle proof that it is a leaf of the tree. | ||
| * | ||
| * @param script serialized spending script. | ||
| * @param leafVersion tapscript version. | ||
| */ | ||
| case class Leaf(script: ByteVector, leafVersion: Int) extends ScriptTree | ||
|
|
||
| object Leaf { | ||
| // @formatter:off | ||
| def apply(script: ByteVector): Leaf = Leaf(script, fr.acinq.bitcoin.Script.TAPROOT_LEAF_TAPSCRIPT) | ||
| def apply(script: Seq[ScriptElt]): Leaf = Leaf(script, fr.acinq.bitcoin.Script.TAPROOT_LEAF_TAPSCRIPT) | ||
| def apply(script: Seq[ScriptElt], leafVersion: Int): Leaf = Leaf(Script.write(script), leafVersion) | ||
| // @formatter:on | ||
| } | ||
|
|
||
| case class Branch(left: ScriptTree, right: ScriptTree) extends ScriptTree | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.