From 80ded0febe5acbfa64cef3d84846b4a1300ea97f Mon Sep 17 00:00:00 2001 From: Antoine A <> Date: Mon, 18 Mar 2024 19:44:50 +0100 Subject: Update dependencies and clean code --- btc-wire/Cargo.toml | 6 +++--- btc-wire/src/bin/segwit-demo.rs | 2 ++ btc-wire/src/lib.rs | 6 +++--- btc-wire/src/rpc.rs | 2 +- btc-wire/src/segwit.rs | 47 +++++++++++++++++++---------------------- 5 files changed, 31 insertions(+), 32 deletions(-) (limited to 'btc-wire') diff --git a/btc-wire/Cargo.toml b/btc-wire/Cargo.toml index 5955a66..afe71c6 100644 --- a/btc-wire/Cargo.toml +++ b/btc-wire/Cargo.toml @@ -17,9 +17,9 @@ bitcoin = { version = "0.31.0", features = [ ], default-features = false } # Cli args parser clap = { version = "4.4.6", features = ["derive"] } -clap_lex = "0.6.0" +clap_lex = "0.7.0" # Bech32 encoding and decoding -bech32 = "0.9.1" +bech32 = "0.11.0" # Serialization library serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.107" @@ -30,7 +30,7 @@ data-encoding = "2.4.0" # Common lib common = { path = "../common" } # Ini parser -rust-ini = "0.20.0" +rust-ini = "0.21.0" # Hexadecimal encoding hex = { package = "const-hex", version = "1.9.1" } diff --git a/btc-wire/src/bin/segwit-demo.rs b/btc-wire/src/bin/segwit-demo.rs index 579f0da..ca9cc6d 100644 --- a/btc-wire/src/bin/segwit-demo.rs +++ b/btc-wire/src/bin/segwit-demo.rs @@ -1,5 +1,6 @@ use std::str::FromStr; +use bech32::Hrp; use bitcoin::{Address, Amount, Network}; use btc_wire::segwit::decode_segwit_msg; use btc_wire::{rpc_utils, segwit::encode_segwit_addr}; @@ -60,6 +61,7 @@ pub fn main() { Network::Regtest => "bcrt", _ => unimplemented!(), }; + let hrp = Hrp::parse(hrp).unwrap(); let first = encode_segwit_addr(hrp, first_half[..].try_into().unwrap()); let second = encode_segwit_addr(hrp, second_half[..].try_into().unwrap()); println!( diff --git a/btc-wire/src/lib.rs b/btc-wire/src/lib.rs index 05842b1..6365e4a 100644 --- a/btc-wire/src/lib.rs +++ b/btc-wire/src/lib.rs @@ -61,9 +61,9 @@ impl Rpc { metadata: &[u8; 32], ) -> rpc::Result { let hrp = match to.network() { - Network::Bitcoin => "bc", - Network::Testnet | Network::Signet => "tb", - Network::Regtest => "bcrt", + Network::Bitcoin => bech32::hrp::BC, + Network::Testnet | Network::Signet => bech32::hrp::TB, + Network::Regtest => bech32::hrp::BCRT, _ => unimplemented!(), }; let addresses = encode_segwit_key(hrp, metadata); diff --git a/btc-wire/src/rpc.rs b/btc-wire/src/rpc.rs index 83c55ad..fe600f9 100644 --- a/btc-wire/src/rpc.rs +++ b/btc-wire/src/rpc.rs @@ -341,7 +341,7 @@ impl Rpc { .collect(); let nb_outputs = outputs.len(); if let Some(data) = data { - assert!(data.len() > 0, "No medatata"); + assert!(!data.is_empty(), "No medatata"); assert!(data.len() <= 80, "Max 80 bytes"); outputs.push(json!({ "data".to_string(): hex::encode(data) })); } diff --git a/btc-wire/src/segwit.rs b/btc-wire/src/segwit.rs index e754d71..956594a 100644 --- a/btc-wire/src/segwit.rs +++ b/btc-wire/src/segwit.rs @@ -13,21 +13,18 @@ You should have received a copy of the GNU Affero General Public License along with TALER; see the file COPYING. If not, see */ -use bech32::{u5, FromBase32, ToBase32, Variant}; +use bech32::Hrp; use common::{rand::rngs::OsRng, rand_slice}; use std::cmp::Ordering; /// Encode metadata into a segwit address -pub fn encode_segwit_addr(hrp: &str, metada: &[u8; 20]) -> String { - // We use the version 0 with bech32 encoding - let mut buf = vec![u5::try_from_u8(0).unwrap()]; - buf.extend_from_slice(&metada.to_base32()); - bech32::encode(hrp, buf, Variant::Bech32).unwrap() +pub fn encode_segwit_addr(hrp: Hrp, metada: &[u8; 20]) -> String { + bech32::segwit::encode_v0(hrp, metada).unwrap() } /// Encode half of a 32B key into a segwit address fn encode_segwit_key_half( - hrp: &str, + hrp: Hrp, is_first: bool, prefix: &[u8; 4], key_half: &[u8; 16], @@ -47,7 +44,7 @@ fn encode_segwit_key_half( } /// Encode a 32B key into two segwit adresses -pub fn encode_segwit_key(hrp: &str, msg: &[u8; 32]) -> [String; 2] { +pub fn encode_segwit_key(hrp: Hrp, msg: &[u8; 32]) -> [String; 2] { // Generate a random prefix let prefix = rand_slice(); // Split key in half; @@ -75,20 +72,20 @@ pub fn decode_segwit_msg(segwit_addrs: &[impl AsRef]) -> Result<[u8; 32], D let decoded: Vec<(bool, [u8; 4], [u8; 16])> = segwit_addrs .iter() .filter_map(|addr| { - bech32::decode(addr.as_ref()).ok().and_then(|(_, wp, _)| { - // Skip version - let pg: Vec = Vec::from_base32(&wp[1..]).unwrap(); - if pg.len() == 20 { - let mut prefix: [u8; 4] = pg[..4].try_into().unwrap(); - let key_half: [u8; 16] = pg[4..].try_into().unwrap(); - let is_first = !pg[0] & 0b1000_0000 == 0; - // Clear first bit - prefix[0] &= 0b0111_1111; - Some((is_first, prefix, key_half)) - } else { - None - } - }) + bech32::segwit::decode(addr.as_ref()) + .ok() + .and_then(|(_, _, pg)| { + if pg.len() == 20 { + let mut prefix: [u8; 4] = pg[..4].try_into().unwrap(); + let key_half: [u8; 16] = pg[4..].try_into().unwrap(); + let is_first = !pg[0] & 0b1000_0000 == 0; + // Clear first bit + prefix[0] &= 0b0111_1111; + Some((is_first, prefix, key_half)) + } else { + None + } + }) }) .collect(); @@ -122,7 +119,7 @@ pub fn decode_segwit_msg(segwit_addrs: &[impl AsRef]) -> Result<[u8; 32], D } // TODO find a way to hide that function while using it in test and benchmark -pub fn rand_addresses(hrp: &str, key: &[u8; 32]) -> Vec { +pub fn rand_addresses(hrp: Hrp, key: &[u8; 32]) -> Vec { use common::rand::prelude::SliceRandom; let mut rng_address: Vec = @@ -149,7 +146,7 @@ mod test { fn test_shuffle() { for _ in 0..1000 { let key = rand_slice(); - let mut addresses = encode_segwit_key("test", &key); + let mut addresses = encode_segwit_key(bech32::hrp::TB, &key); addresses.shuffle(&mut OsRng); let decoded = decode_segwit_msg(&addresses.iter().map(|s| s.as_str()).collect::>()) @@ -162,7 +159,7 @@ mod test { fn test_shuffle_many() { for _ in 0..1000 { let key = rand_slice(); - let addresses = rand_addresses("test", &key); + let addresses = rand_addresses(bech32::hrp::TB, &key); let decoded = decode_segwit_msg(&addresses.iter().map(|s| s.as_str()).collect::>()) .unwrap(); -- cgit v1.2.3