Skip to content

Commit

Permalink
[bitcoin] taproot builder return result (#2420)
Browse files Browse the repository at this point in the history
* [bitcoin] taproot builder return result

* fixup
  • Loading branch information
jolestar authored Aug 13, 2024
1 parent 527e6d1 commit a03f867
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions frameworks/rooch-nursery/doc/multisign_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Bitcoin multisign account module
<b>use</b> <a href="">0x3::ecdsa_k1</a>;
<b>use</b> <a href="">0x4::opcode</a>;
<b>use</b> <a href="">0x4::script_buf</a>;
<b>use</b> <a href="result.md#0xa_result">0xa::result</a>;
<b>use</b> <a href="taproot_builder.md#0xa_taproot_builder">0xa::taproot_builder</a>;
</code></pre>

Expand Down
5 changes: 4 additions & 1 deletion frameworks/rooch-nursery/doc/taproot_builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Taproot is a module that provides Bitcoin Taproot related functions.
<b>use</b> <a href="">0x2::compare</a>;
<b>use</b> <a href="">0x2::hash</a>;
<b>use</b> <a href="">0x4::script_buf</a>;
<b>use</b> <a href="result.md#0xa_result">0xa::result</a>;
</code></pre>


Expand Down Expand Up @@ -150,7 +151,9 @@ Tapscript leaf version.

## Function `finalize`

Finalize the builder, return the state root,
We use the address to represent the hash.


<pre><code><b>public</b> <b>fun</b> <a href="taproot_builder.md#0xa_taproot_builder_finalize">finalize</a>(builder: <a href="taproot_builder.md#0xa_taproot_builder_TaprootBuilder">taproot_builder::TaprootBuilder</a>): <b>address</b>
<pre><code><b>public</b> <b>fun</b> <a href="taproot_builder.md#0xa_taproot_builder_finalize">finalize</a>(builder: <a href="taproot_builder.md#0xa_taproot_builder_TaprootBuilder">taproot_builder::TaprootBuilder</a>): <a href="result.md#0xa_result_Result">result::Result</a>&lt;<b>address</b>, <a href="taproot_builder.md#0xa_taproot_builder_TaprootBuilder">taproot_builder::TaprootBuilder</a>&gt;
</code></pre>
4 changes: 3 additions & 1 deletion frameworks/rooch-nursery/sources/multisign_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module rooch_nursery::multisign_account{
use rooch_framework::ecdsa_k1;
use rooch_framework::bitcoin_address::{Self, BitcoinAddress};
use rooch_nursery::taproot_builder;
use rooch_nursery::result;

const PROPOSAL_STATUS_PENDING: u8 = 0;
const PROPOSAL_STATUS_APPROVED: u8 = 1;
Expand Down Expand Up @@ -164,7 +165,8 @@ module rooch_nursery::multisign_account{
let multisign_script = create_multisign_script(threshold, to_x_only_public_keys);
let builder = taproot_builder::new();
taproot_builder::add_leaf(&mut builder, 0, multisign_script);
taproot_builder::finalize(builder)
let result = taproot_builder::finalize(builder);
result::unwrap(result)
}

fun create_multisign_script(threshold: u64, to_x_only_public_keys: &vector<vector<u8>>) : ScriptBuf {
Expand Down
10 changes: 6 additions & 4 deletions frameworks/rooch-nursery/sources/taproot_builder.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module rooch_nursery::taproot_builder {
use moveos_std::bcs;
use moveos_std::compare;
use bitcoin_move::script_buf::{Self,ScriptBuf};
use rooch_nursery::result::{err, ok, Result};

/// Tapscript leaf version.
// https://github.com/bitcoin/bitcoin/blob/e826b22da252e0599c61d21c98ff89f366b3120f/src/script_buf/interpreter.h#L226
Expand Down Expand Up @@ -110,17 +111,18 @@ module rooch_nursery::taproot_builder {
hash
}

public fun finalize(builder: TaprootBuilder): address {
/// Finalize the builder, return the state root,
/// We use the address to represent the hash.
public fun finalize(builder: TaprootBuilder): Result<address, TaprootBuilder> {
let len = vector::length(&builder.branch);
if (len == 0) {
//TODO return Result<address, TaprootBuilder> after refactor the Result
return @0x0
return err(builder)
};
let last_node_opt = vector::pop_back(&mut builder.branch);
//This should not happen, Builder guarantees the last element is Some
assert!(is_some(&last_node_opt), ErrorUnreachable);
let last_node = destroy_some(last_node_opt);
last_node.hash
ok(last_node.hash)
}

fun tagged_hash(tag: vector<u8>, msg: vector<u8>): address {
Expand Down
7 changes: 4 additions & 3 deletions frameworks/rooch-nursery/tests/taproot_builder_test.move
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[test_only]
module rooch_nursery::taproot_builder_tests {
use rooch_nursery::taproot_builder;
use bitcoin_move::opcode;
use bitcoin_move::script_buf;
use rooch_nursery::taproot_builder;
use rooch_nursery::result;

#[test]
fun test_taproot_builder() {
Expand All @@ -14,7 +15,7 @@ module rooch_nursery::taproot_builder_tests {
taproot_builder::add_leaf(&mut builder, 1, script1);
taproot_builder::add_leaf(&mut builder, 1, script2);

let root = taproot_builder::finalize(builder);
let root = result::unwrap(taproot_builder::finalize(builder));

//std::debug::print(&root);
let expected_root = @0x15526cd6108b4765640abe555e75f4bd11d9b1453b9db4cd36cf4189577a6f63;
Expand All @@ -33,7 +34,7 @@ module rooch_nursery::taproot_builder_tests {
taproot_builder::add_leaf(&mut builder, 2, script2);
taproot_builder::add_leaf(&mut builder, 2, script3);

let root = taproot_builder::finalize(builder);
let root = result::unwrap(taproot_builder::finalize(builder));

//std::debug::print(&root);
let expected_root = @0xd847514fba3bdcfed383ce109a2700baafd6a629e290b22678c8c21ca93aca86;
Expand Down

0 comments on commit a03f867

Please sign in to comment.