Skip to content

Commit

Permalink
[CLI] Implement rooch bitcoin command to suppor taproot multisign and…
Browse files Browse the repository at this point in the history
… bump bitcoin to 0.32.2 (#2595)
  • Loading branch information
jolestar authored Sep 11, 2024
1 parent 5c208d0 commit 9046211
Show file tree
Hide file tree
Showing 55 changed files with 1,965 additions and 456 deletions.
238 changes: 50 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ bcs = "0.1.3"
bytes = "1.7.1"
bech32 = "0.11.0"
better_any = "0.1.1"
bitcoin = { version = "0.31.0", features = ["rand-std"] }
bitcoin_hashes = { version = "0.13.0", features = ["serde-std"]}
bitcoincore-rpc = "0.18.0"
bitcoin = { version = "0.32.2", features = ["rand-std", "bitcoinconsensus"] }
bitcoin_hashes = { version = "0.14.0", features = ["serde"] }
bitcoincore-rpc = "0.19.0"
bitcoincore-rpc-json = "0.19.0"
bip32 = "0.4.0"
byteorder = "1.4.3"
clap = { version = "4.5.13", features = ["derive", "env"] }
Expand Down Expand Up @@ -242,7 +243,6 @@ hyper = { version = "1.0.0", features = ["full"] }
num_enum = "0.7.3"
libc = "^0.2"
include_dir = { version = "0.6.2" }
nostr = "0.22"
serde-reflection = "0.3.6"
serde-generate = "0.25.1"
bcs-ext = { path = "moveos/moveos-commons/bcs_ext" }
Expand Down Expand Up @@ -316,7 +316,6 @@ pprof = { version = "0.13.0", features = ["flamegraph", "criterion", "cpp", "fra
celestia-rpc = { git = "https://github.com/eigerco/celestia-node-rs.git", rev = "129272e8d926b4c7badf27a26dea915323dd6489" }
celestia-types = { git = "https://github.com/eigerco/celestia-node-rs.git", rev = "129272e8d926b4c7badf27a26dea915323dd6489" }
opendal = { version = "0.47.3", features = ["services-fs", "services-gcs"] }
bitcoincore-rpc-json = "0.18.0"
toml = "0.8.19"
csv = "1.2.1"
revm-precompile = "7.0.0"
Expand All @@ -336,8 +335,6 @@ rustc-hash = { version = "2.0.0" }
xorf = { version = "0.11.0" }
vergen-git2 = { version = "1.0.0", features = ["build", "cargo", "rustc"] }
vergen-pretty = "0.3.4"
musig2 = { version = "0.0.11" }
miniscript = "12.2.0"
crossbeam-channel = "0.5.13"

# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
Expand Down
2 changes: 0 additions & 2 deletions crates/rooch-framework-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tempfile = { workspace = true }
include_dir = { workspace = true }
musig2 = { workspace = true }
miniscript = { workspace = true }
coerce = { workspace = true }
tokio = { workspace = true }
clap = { features = ["derive", ], workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-framework-tests/src/bitcoin_block_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl BitcoinBlockTester {
let mut utxo_set = HashMap::<OutPoint, TxOut>::new();
let block_data = self.executed_block.as_ref().unwrap();
for tx in block_data.block.txdata.as_slice() {
let txid = tx.txid();
let txid = tx.compute_txid();
for (index, tx_out) in tx.output.iter().enumerate() {
let vout = index as u32;
let out_point = OutPoint::new(txid, vout);
Expand Down Expand Up @@ -486,7 +486,7 @@ impl TesterGenesisBuilder {
);

for tx in &block.txdata {
self.block_txids.insert(tx.txid());
self.block_txids.insert(tx.compute_txid());
}

let depdent_txids = block
Expand Down
5 changes: 3 additions & 2 deletions crates/rooch-framework-tests/src/tests/bitcoin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ fn check_utxo(txs: Vec<Transaction>, binding_test: &binding_test::RustBindingTes
for tx in txs.as_slice() {
for (index, tx_out) in tx.output.iter().enumerate() {
let vout = index as u32;
let out_point = OutPoint::new(tx.txid(), vout);
let txid = tx.compute_txid();
let out_point = OutPoint::new(txid, vout);
utxo_set.insert(out_point, tx_out.clone());
}
for tx_in in tx.input.iter() {
Expand Down Expand Up @@ -147,7 +148,7 @@ fn check_utxo(txs: Vec<Transaction>, binding_test: &binding_test::RustBindingTes
let inscriptions = txs
.iter()
.flat_map(|tx| {
let txid = tx.txid();
let txid = tx.compute_txid();
let rooch_btc_tx = rooch_types::bitcoin::types::Transaction::from(tx.clone());
ord_module
.parse_inscription_from_tx(&rooch_btc_tx)
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-framework-tests/src/tests/brc20_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn decode_tx(btx_tx_hex: &str) {
let btc_tx_bytes = Vec::from_hex(btx_tx_hex).unwrap();
let btc_tx: bitcoin::Transaction =
Decodable::consensus_decode(&mut btc_tx_bytes.as_slice()).unwrap();
debug!("tx_id: {}", btc_tx.txid());
let txid = btc_tx.compute_txid();
debug!("tx_id: {}", txid);
for (i, input) in btc_tx.input.iter().enumerate() {
debug!("{}. input: {:?}", i, input.previous_output);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-framework-tests/src/tests/ord_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ fn decode_inscription(
binding_test: &mut binding_test::RustBindingTest,
btc_tx: Transaction,
) -> Vec<Envelope<InscriptionRecord>> {
debug!("tx_id: {}", btc_tx.txid());
let txid = btc_tx.compute_txid();
debug!("tx_id: {}", txid);
for (i, input) in btc_tx.input.iter().enumerate() {
debug!("{}. input: {:?}", i, input.previous_output);
}
Expand Down
13 changes: 13 additions & 0 deletions crates/rooch-key/src/keystore/account_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,21 @@ pub trait AccountKeystore {
Ok(())
}

/// Get all local accounts
//TODO refactor the keystore, save the public key out of the encryption data, so that we don't need to require password to get the public key
fn get_accounts(&self, password: Option<String>) -> Result<Vec<LocalAccount>, anyhow::Error>;

/// Get local account by address
fn get_account(
&self,
address: &RoochAddress,
password: Option<String>,
) -> Result<Option<LocalAccount>, anyhow::Error> {
let accounts = self.get_accounts(password)?;
let account = accounts.iter().find(|account| account.address == *address);
Ok(account.cloned())
}

fn contains_address(&self, address: &RoochAddress) -> bool;

fn add_address_encryption_data_to_keys(
Expand Down
10 changes: 5 additions & 5 deletions crates/rooch-key/src/keystore/base_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::keystore::account_keystore::AccountKeystore;
use anyhow::{ensure, Ok};
use rooch_types::framework::session_key::SessionKey;
use rooch_types::key_struct::{MnemonicData, MnemonicResult};
use rooch_types::to_bech32::ToBech32;
use rooch_types::{
address::RoochAddress,
authentication_key::AuthenticationKey,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl AccountKeystore for BaseKeyStore {
let keypair: RoochKeyPair = encryption.decrypt_with_type(password.clone())?;
let public_key = keypair.public();
let bitcoin_address = public_key.bitcoin_address()?;
let nostr_bech32_public_key = public_key.nostr_bech32_public_key()?;
let nostr_bech32_public_key = public_key.xonly_public_key()?.to_bech32()?;
let has_session_key = self.session_keys.contains_key(address);
let local_account = LocalAccount {
address: *address,
Expand Down Expand Up @@ -102,10 +103,9 @@ impl AccountKeystore for BaseKeyStore {
let keypair: RoochKeyPair = encryption.decrypt_with_type::<RoochKeyPair>(password)?;
Ok(keypair)
} else {
Err(anyhow::Error::new(RoochError::SignMessageError(format!(
"Cannot find key for address: [{:?}]",
address
))))
Err(anyhow::Error::new(RoochError::CommandArgumentError(
format!("Cannot find key for address: [{:?}]", address),
)))
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
],
"properties": {
"txid": {
"$ref": "#/components/schemas/bitcoin::hash_types::newtypes::Txid"
"$ref": "#/components/schemas/bitcoin::blockdata::transaction::Txid"
},
"vout": {
"type": "integer",
Expand Down Expand Up @@ -3104,7 +3104,7 @@
"description": "The txid of the UTXO",
"allOf": [
{
"$ref": "#/components/schemas/bitcoin::hash_types::newtypes::Txid"
"$ref": "#/components/schemas/bitcoin::blockdata::transaction::Txid"
}
]
},
Expand Down Expand Up @@ -3248,7 +3248,7 @@
"alloc::vec::Vec<u8>": {
"type": "string"
},
"bitcoin::hash_types::newtypes::Txid": {
"bitcoin::blockdata::transaction::Txid": {
"type": "string"
},
"move_binary_format::file_format::Ability": {
Expand Down
1 change: 0 additions & 1 deletion crates/rooch-rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
schemars = { workspace = true }
bitcoin = { workspace = true }
nostr = { workspace = true }

move-core-types = { workspace = true }
move-resource-viewer = { workspace = true }
Expand Down
36 changes: 35 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

use crate::jsonrpc_types::StrView;
use anyhow::Result;
use bitcoin::XOnlyPublicKey;
use move_core_types::account_address::AccountAddress;
use nostr::{key::XOnlyPublicKey, prelude::FromBech32};
use rooch_types::{
address::{BitcoinAddress, NostrPublicKey, RoochAddress},
bitcoin::network::Network,
to_bech32::FromBech32,
};
use std::str::FromStr;

Expand Down Expand Up @@ -66,6 +67,7 @@ impl From<RoochAddressView> for AccountAddress {
}
}

//TODO directly use UnitedAddress and remove UnitedAddressView
#[derive(Debug, Clone)]
pub struct UnitedAddress {
pub rooch_address: RoochAddress,
Expand All @@ -90,6 +92,7 @@ impl std::fmt::Display for UnitedAddressView {
impl FromStr for UnitedAddressView {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
//TODO use the prefix to determine the type of address
match RoochAddress::from_str(s) {
Ok(rooch_address) => Ok(StrView(UnitedAddress {
rooch_address,
Expand Down Expand Up @@ -165,3 +168,34 @@ impl TryFrom<UnitedAddressView> for NostrPublicKey {
}
}
}

impl From<BitcoinAddress> for UnitedAddressView {
fn from(value: BitcoinAddress) -> Self {
StrView(UnitedAddress {
rooch_address: value.to_rooch_address(),
bitcoin_address: Some(value),
nostr_public_key: None,
})
}
}

impl From<RoochAddress> for UnitedAddressView {
fn from(value: RoochAddress) -> Self {
StrView(UnitedAddress {
rooch_address: value,
bitcoin_address: None,
nostr_public_key: None,
})
}
}

impl From<bitcoin::Address> for UnitedAddressView {
fn from(value: bitcoin::Address) -> Self {
let value = BitcoinAddress::from(value);
StrView(UnitedAddress {
rooch_address: value.to_rooch_address(),
bitcoin_address: Some(value),
nostr_public_key: None,
})
}
}
14 changes: 4 additions & 10 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

use crate::jsonrpc_types::StrView;
use anyhow::Result;
use bitcoin::hashes::Hash;
use bitcoin::Txid;
use std::fmt;
use std::str::FromStr;

pub type TxidView = StrView<Txid>;

impl fmt::Display for TxidView {
//TODO check display format
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
write!(f, "{}", self.0)
}
}

Expand All @@ -30,12 +28,6 @@ impl From<TxidView> for Txid {
}
}

pub fn hex_to_txid(hex_string: &str) -> Result<Txid> {
let mut bytes = hex::decode(hex_string)?;
bytes.reverse();
Ok(Txid::from_slice(&bytes)?)
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -44,8 +36,10 @@ mod test {
#[test]
fn test_txid() -> Result<()> {
let txid_str = "5fddcbdc3eb21a93e8dd1dd3f9087c3677f422b82d5ba39a6b1ec37338154af6";
let txid = hex_to_txid(txid_str)?;
let txid_view = TxidView::from_str(txid_str)?;
let txid = Txid::from_str(txid_str)?;
let txid_str2 = txid.to_string();
assert!(txid_view.0 == txid);
assert!(txid_str == txid_str2);

Ok(())
Expand Down
Loading

0 comments on commit 9046211

Please sign in to comment.