Skip to content

Commit

Permalink
[Bitcoin] Verify the bitcoin header (#1786)
Browse files Browse the repository at this point in the history
* [Bitcoin] Verify the bitcoin header

* [Bitcoin] update test
  • Loading branch information
vegetabledogdog authored Jun 1, 2024
1 parent 688edb0 commit d26e1be
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions frameworks/bitcoin-move/doc/bitcoin.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Function `get_genesis_block_height`](#0x4_bitcoin_get_genesis_block_height)
- [Function `get_latest_block_height`](#0x4_bitcoin_get_latest_block_height)
- [Function `get_bitcoin_time`](#0x4_bitcoin_get_bitcoin_time)
- [Function `verify_header`](#0x4_bitcoin_verify_header)


<pre><code><b>use</b> <a href="">0x1::option</a>;
Expand Down Expand Up @@ -197,3 +198,14 @@ Get the bitcoin time, if the latest block is not exist, return 0

<pre><code><b>public</b> <b>fun</b> <a href="bitcoin.md#0x4_bitcoin_get_bitcoin_time">get_bitcoin_time</a>(): u32
</code></pre>



<a name="0x4_bitcoin_verify_header"></a>

## Function `verify_header`



<pre><code><b>public</b> <b>fun</b> <a href="bitcoin.md#0x4_bitcoin_verify_header">verify_header</a>(block_header: <a href="types.md#0x4_types_Header">types::Header</a>): bool
</code></pre>
27 changes: 27 additions & 0 deletions frameworks/bitcoin-move/doc/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- [Function `time`](#0x4_types_time)
- [Function `bits`](#0x4_types_bits)
- [Function `nonce`](#0x4_types_nonce)
- [Function `header_to_bytes`](#0x4_types_header_to_bytes)
- [Function `header_to_hash`](#0x4_types_header_to_hash)
- [Function `tx_id`](#0x4_types_tx_id)
- [Function `tx_version`](#0x4_types_tx_version)
- [Function `tx_lock_time`](#0x4_types_tx_lock_time)
Expand All @@ -47,7 +49,10 @@


<pre><code><b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::vector</a>;
<b>use</b> <a href="">0x2::address</a>;
<b>use</b> <a href="">0x2::bcs</a>;
<b>use</b> <a href="">0x2::hash</a>;
<b>use</b> <a href="">0x3::bitcoin_address</a>;
<b>use</b> <a href="script_buf.md#0x4_script_buf">0x4::script_buf</a>;
</code></pre>
Expand Down Expand Up @@ -258,6 +263,28 @@



<a name="0x4_types_header_to_bytes"></a>

## Function `header_to_bytes`



<pre><code><b>public</b> <b>fun</b> <a href="types.md#0x4_types_header_to_bytes">header_to_bytes</a>(self: <a href="types.md#0x4_types_Header">types::Header</a>): <a href="">vector</a>&lt;u8&gt;
</code></pre>



<a name="0x4_types_header_to_hash"></a>

## Function `header_to_hash`



<pre><code><b>public</b> <b>fun</b> <a href="types.md#0x4_types_header_to_hash">header_to_hash</a>(self: <a href="types.md#0x4_types_Header">types::Header</a>): <b>address</b>
</code></pre>



<a name="0x4_types_tx_id"></a>

## Function `tx_id`
Expand Down
34 changes: 34 additions & 0 deletions frameworks/bitcoin-move/sources/bitcoin.move
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ module bitcoin_move::bitcoin{
};
}

public fun verify_header(block_header : Header) : bool {
let block_hash = types::header_to_hash(block_header);
let btc_block_store_obj = borrow_block_store();
let btc_block_store = object::borrow(btc_block_store_obj);
if(table::contains(&btc_block_store.blocks, block_hash)){
true
}else{
false
}
}

#[test_only]
public fun submit_block_for_test(block_height: u64, block_hash: address, block_header: &Header){
let btc_block_store_obj = borrow_block_store_mut();
Expand All @@ -418,4 +429,27 @@ module bitcoin_move::bitcoin{
let module_signer = signer::module_signer<BitcoinBlockStore>();
timestamp::try_update_global_time(&module_signer, timestamp::seconds_to_milliseconds(timestamp_seconds));
}

#[test_only]
use moveos_std::address;
use std::ascii;

#[test]
fun verify_header_test() {
let version = 536879108;
let prev_blockhash = option::destroy_some(address::from_ascii_string(ascii::string(b"00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046")));
let merkle_root = option::destroy_some(address::from_ascii_string(ascii::string(b"e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3")));
let time = 1644403033;
let bits = 0x170a8bb4;
let nonce = 1693537958;

let block_header = types::new_header_for_test(version, prev_blockhash, merkle_root, time, bits, nonce);
let block_hash = types::header_to_hash(block_header);
assert!(block_hash == address::from_bytes(x"00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1"), 1);

let module_signer = signer::module_signer<BitcoinBlockStore>();
genesis_init(&module_signer, 0);
submit_block_for_test(1, block_hash, &block_header);
assert!(verify_header(block_header), 2);
}
}
22 changes: 22 additions & 0 deletions frameworks/bitcoin-move/sources/types.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module bitcoin_move::types{
use std::vector;
use std::option::{Self, Option};
use moveos_std::address;
use moveos_std::bcs;
use moveos_std::hash;
use rooch_framework::bitcoin_address::{Self, BitcoinAddress};
use bitcoin_move::script_buf::{Self, ScriptBuf};

Expand Down Expand Up @@ -68,6 +70,26 @@ module bitcoin_move::types{
self.nonce
}

public fun header_to_bytes(self: Header) : vector<u8> {
let output = vector::empty<u8>();
vector::append(&mut output, bcs::to_bytes(&self.version));
vector::append(&mut output, address::to_bytes(self.prev_blockhash));
vector::append(&mut output, address::to_bytes(self.merkle_root));
vector::append(&mut output, bcs::to_bytes(&self.time));
vector::append(&mut output, bcs::to_bytes(&self.bits));
vector::append(&mut output, bcs::to_bytes(&self.nonce));
output
}

public fun header_to_hash(self: Header) : address {
let header = header_to_bytes(self);
let hash1 = hash::sha2_256(header);
let hash2 = hash::sha2_256(hash1);
vector::reverse(&mut hash2);
let block_hash = address::from_bytes(hash2);
block_hash
}

#[data_struct]
struct Transaction has store, copy, drop {
/// The txid
Expand Down

0 comments on commit d26e1be

Please sign in to comment.