Skip to content

Commit 02b56da

Browse files
committed
upgrade to bitcoin 0.32
1 parent 25dc2d2 commit 02b56da

File tree

8 files changed

+112
-54
lines changed

8 files changed

+112
-54
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ json-contract = [ "serde_json" ]
2222
base64 = ["bitcoin/base64"]
2323

2424
[dependencies]
25-
bitcoin = "0.31.0"
26-
secp256k1-zkp = { version = "0.10.0", features = [ "global-context", "hashes" ] }
25+
bech32 = "0.11.0"
26+
bitcoin = "0.32.2"
27+
secp256k1-zkp = { version = "0.11.0", features = [ "global-context", "hashes" ] }
2728

2829
# Used for ContractHash::from_json_contract.
2930
serde_json = { version = "1.0", optional = true }

src/address.rs

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ use std::fmt;
2121
use std::fmt::Write as _;
2222
use std::str::FromStr;
2323

24-
use bitcoin::base58;
25-
use bitcoin::PublicKey;
26-
use crate::bech32::{Bech32, Bech32m, ByteIterExt, Fe32, Hrp, Fe32IterExt};
24+
use crate::bech32::{Bech32, Bech32m, ByteIterExt, Fe32, Fe32IterExt, Hrp};
2725
use crate::blech32::{Blech32, Blech32m};
2826
use crate::hashes::Hash;
27+
use bitcoin::base58;
28+
use bitcoin::PublicKey;
2929
use secp256k1_zkp;
3030
use secp256k1_zkp::Secp256k1;
3131
use secp256k1_zkp::Verification;
@@ -35,8 +35,8 @@ use serde;
3535
use crate::schnorr::{TapTweak, TweakedPublicKey, UntweakedPublicKey};
3636
use crate::taproot::TapNodeHash;
3737

38-
use crate::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
3938
use crate::{opcodes, script};
39+
use crate::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
4040

4141
/// Encoding error
4242
#[derive(Debug, PartialEq)]
@@ -62,6 +62,8 @@ pub enum AddressError {
6262

6363
/// An invalid blinding pubkey was encountered.
6464
InvalidBlindingPubKey(secp256k1_zkp::UpstreamError),
65+
66+
TodoInvalid,
6567
}
6668

6769
impl From<crate::bech32::primitives::decode::SegwitHrpstringError> for AddressError {
@@ -89,10 +91,18 @@ impl fmt::Display for AddressError {
8991
write!(f, "invalid witness script version: {}", wver)
9092
}
9193
AddressError::InvalidWitnessProgramLength(ref len) => {
92-
write!(f, "the witness program must be between 2 and 40 bytes in length, not {}", len)
94+
write!(
95+
f,
96+
"the witness program must be between 2 and 40 bytes in length, not {}",
97+
len
98+
)
9399
}
94100
AddressError::InvalidSegwitV0ProgramLength(ref len) => {
95-
write!(f, "a v0 witness program must be length 20 or 32, not {}", len)
101+
write!(
102+
f,
103+
"a v0 witness program must be length 20 or 32, not {}",
104+
len
105+
)
96106
}
97107
AddressError::InvalidBlindingPubKey(ref e) => {
98108
write!(f, "an invalid blinding pubkey was encountered: {}", e)
@@ -103,6 +113,7 @@ impl fmt::Display for AddressError {
103113
AddressError::InvalidSegwitV0Encoding => {
104114
write!(f, "v0 witness program must use b(l)ech32 not b(l)ech32m")
105115
}
116+
AddressError::TodoInvalid => todo!(),
106117
}
107118
}
108119
}
@@ -212,7 +223,8 @@ impl Address {
212223
params: &'static AddressParams,
213224
) -> Address {
214225
let mut hash_engine = PubkeyHash::engine();
215-
pk.write_into(&mut hash_engine).expect("engines don't error");
226+
pk.write_into(&mut hash_engine)
227+
.expect("engines don't error");
216228

217229
Address {
218230
params,
@@ -244,7 +256,8 @@ impl Address {
244256
params: &'static AddressParams,
245257
) -> Address {
246258
let mut hash_engine = WPubkeyHash::engine();
247-
pk.write_into(&mut hash_engine).expect("engines don't error");
259+
pk.write_into(&mut hash_engine)
260+
.expect("engines don't error");
248261

249262
Address {
250263
params,
@@ -264,7 +277,8 @@ impl Address {
264277
params: &'static AddressParams,
265278
) -> Address {
266279
let mut hash_engine = ScriptHash::engine();
267-
pk.write_into(&mut hash_engine).expect("engines don't error");
280+
pk.write_into(&mut hash_engine)
281+
.expect("engines don't error");
268282

269283
let builder = script::Builder::new()
270284
.push_int(0)
@@ -401,7 +415,9 @@ impl Address {
401415
Payload::WitnessProgram {
402416
version: witver,
403417
program: ref witprog,
404-
} => script::Builder::new().push_int(witver.to_u8() as i64).push_slice(witprog),
418+
} => script::Builder::new()
419+
.push_int(witver.to_u8() as i64)
420+
.push_slice(witprog),
405421
}
406422
.into_script()
407423
}
@@ -450,10 +466,7 @@ impl Address {
450466

451467
Ok(Address {
452468
params,
453-
payload: Payload::WitnessProgram {
454-
version,
455-
program,
456-
},
469+
payload: Payload::WitnessProgram { version, program },
457470
blinding_pubkey,
458471
})
459472
}
@@ -468,13 +481,15 @@ impl Address {
468481
let (blinded, prefix) = match data[0] == params.blinded_prefix {
469482
true => {
470483
if data.len() != 55 {
471-
return Err(base58::Error::InvalidLength(data.len()).into());
484+
// return Err(base58::Error::InvalidLength(data.len()).into());
485+
return Err(AddressError::TodoInvalid);
472486
}
473487
(true, data[1])
474488
}
475489
false => {
476490
if data.len() != 21 {
477-
return Err(base58::Error::InvalidLength(data.len()).into());
491+
// return Err(base58::Error::InvalidLength(data.len()).into());
492+
return Err(AddressError::TodoInvalid);
478493
}
479494
(false, data[0])
480495
}
@@ -496,7 +511,8 @@ impl Address {
496511
} else if prefix == params.p2sh_prefix {
497512
Payload::ScriptHash(ScriptHash::from_slice(payload_data).unwrap())
498513
} else {
499-
return Err(base58::Error::InvalidAddressVersion(prefix).into());
514+
// return Err(base58::Error::InvalidAddressVersion(prefix).into());
515+
return Err(AddressError::TodoInvalid);
500516
};
501517

502518
Ok(Address {
@@ -522,7 +538,8 @@ impl Address {
522538

523539
// Base58.
524540
if s.len() > 150 {
525-
return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
541+
// return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
542+
return Err(AddressError::TodoInvalid);
526543
}
527544
let data = base58::decode_check(s)?;
528545
Address::from_base58(&data, params)
@@ -574,14 +591,23 @@ impl fmt::Display for Address {
574591
// FIXME: surely we can fix this logic to not be so repetitive.
575592
if self.is_blinded() {
576593
if let Some(ref blinder) = self.blinding_pubkey {
577-
let byte_iter = IntoIterator::into_iter(blinder.serialize()).chain(witprog.iter().copied());
594+
let byte_iter = IntoIterator::into_iter(blinder.serialize())
595+
.chain(witprog.iter().copied());
578596
let fe_iter = byte_iter.bytes_to_fes();
579597
if witver.to_u8() == 0 {
580-
for c in fe_iter.with_checksum::<Blech32>(&hrp).with_witness_version(witver).chars() {
598+
for c in fe_iter
599+
.with_checksum::<Blech32>(&hrp)
600+
.with_witness_version(witver)
601+
.chars()
602+
{
581603
fmt.write_char(c)?;
582604
}
583605
} else {
584-
for c in fe_iter.with_checksum::<Blech32m>(&hrp).with_witness_version(witver).chars() {
606+
for c in fe_iter
607+
.with_checksum::<Blech32m>(&hrp)
608+
.with_witness_version(witver)
609+
.chars()
610+
{
585611
fmt.write_char(c)?;
586612
}
587613
}
@@ -592,11 +618,19 @@ impl fmt::Display for Address {
592618
let byte_iter = witprog.iter().copied();
593619
let fe_iter = byte_iter.bytes_to_fes();
594620
if witver.to_u8() == 0 {
595-
for c in fe_iter.with_checksum::<Bech32>(&hrp).with_witness_version(witver).chars() {
621+
for c in fe_iter
622+
.with_checksum::<Bech32>(&hrp)
623+
.with_witness_version(witver)
624+
.chars()
625+
{
596626
fmt.write_char(c)?;
597627
}
598628
} else {
599-
for c in fe_iter.with_checksum::<Bech32m>(&hrp).with_witness_version(witver).chars() {
629+
for c in fe_iter
630+
.with_checksum::<Bech32m>(&hrp)
631+
.with_witness_version(witver)
632+
.chars()
633+
{
600634
fmt.write_char(c)?;
601635
}
602636
}
@@ -660,11 +694,13 @@ impl FromStr for Address {
660694

661695
// Base58.
662696
if s.len() > 150 {
663-
return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
697+
// return Err(base58::Error::InvalidLength(s.len() * 11 / 15).into());
698+
return Err(AddressError::TodoInvalid);
664699
}
665700
let data = base58::decode_check(s)?;
666701
if data.is_empty() {
667-
return Err(base58::Error::InvalidLength(data.len()).into());
702+
// return Err(base58::Error::InvalidLength(data.len()).into());
703+
return Err(AddressError::TodoInvalid);
668704
}
669705

670706
let p = data[0];
@@ -734,9 +770,9 @@ impl serde::Serialize for Address {
734770
#[cfg(test)]
735771
mod test {
736772
use super::*;
773+
use crate::Script;
737774
use bitcoin::key;
738775
use secp256k1_zkp::{PublicKey, Secp256k1};
739-
use crate::Script;
740776
#[cfg(feature = "serde")]
741777
use serde_json;
742778

@@ -755,7 +791,9 @@ mod test {
755791
);
756792
#[cfg(feature = "serde")]
757793
assert_eq!(
758-
serde_json::from_value::<Address>(serde_json::to_value(addr).unwrap()).ok().as_ref(),
794+
serde_json::from_value::<Address>(serde_json::to_value(addr).unwrap())
795+
.ok()
796+
.as_ref(),
759797
Some(addr)
760798
);
761799
}
@@ -831,7 +869,12 @@ mod test {
831869

832870
for &(a, blinded, ref params) in &addresses {
833871
let result = a.parse();
834-
assert!(result.is_ok(), "vector: {}, err: \"{}\"", a, result.unwrap_err());
872+
assert!(
873+
result.is_ok(),
874+
"vector: {}, err: \"{}\"",
875+
a,
876+
result.unwrap_err()
877+
);
835878
let addr: Address = result.unwrap();
836879
assert_eq!(a, &addr.to_string(), "vector: {}", a);
837880
assert_eq!(blinded, addr.is_blinded());
@@ -858,7 +901,8 @@ mod test {
858901
"blech32 error: invalid checksum", // is valid blech32m, but should be blech32
859902
);
860903

861-
let address: Result<Address, _> = "ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys".parse();
904+
let address: Result<Address, _> =
905+
"ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys".parse();
862906
assert_eq!(
863907
address.err().unwrap().to_string(),
864908
"bech32 error: invalid segwit witness version: 3", // FIXME https://github.com/rust-bitcoin/rust-bech32/issues/162 should be 17
@@ -879,14 +923,18 @@ mod test {
879923
);
880924
}
881925

882-
883926
#[test]
884927
fn test_fixed_addresses() {
885-
let pk = bitcoin::PublicKey::from_str("0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea")
886-
.unwrap();
928+
let pk = bitcoin::PublicKey::from_str(
929+
"0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea",
930+
)
931+
.unwrap();
887932
let script = Script::default();
888933
let secp = Secp256k1::verification_only();
889-
let internal_key = UntweakedPublicKey::from_str("93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51").unwrap();
934+
let internal_key = UntweakedPublicKey::from_str(
935+
"93c7378d96518a75448821c4f7c8f4bae7ce60f804d03d1f0628dd5dd0f5de51",
936+
)
937+
.unwrap();
890938
let tap_node_hash = TapNodeHash::all_zeros();
891939

892940
let mut expected = IntoIterator::into_iter([
@@ -934,9 +982,12 @@ mod test {
934982
"tlq1pqgft7r4ytdenml0gaj67393sd3qkt3nxex0ut5dt3plhzwf6jaww5vx8c8vs0ywzejta7jjcc5f4asnacdtu0wlaas0upmsq90enaz2lekytucqf82vs",
935983
]);
936984

937-
for params in [&AddressParams::ELEMENTS, &AddressParams::LIQUID, &AddressParams::LIQUID_TESTNET] {
985+
for params in [
986+
&AddressParams::ELEMENTS,
987+
&AddressParams::LIQUID,
988+
&AddressParams::LIQUID_TESTNET,
989+
] {
938990
for blinder in [None, Some(pk.inner)] {
939-
940991
let addr = Address::p2pkh(&pk, blinder, params);
941992
assert_eq!(&addr.to_string(), expected.next().unwrap());
942993

@@ -959,6 +1010,5 @@ mod test {
9591010
assert_eq!(&addr.to_string(), expected.next().unwrap());
9601011
}
9611012
}
962-
9631013
}
9641014
}

src/blech32/decode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'s> UncheckedHrpstring<'s> {
150150
}
151151

152152
let mut checksum_eng = checksum::Engine::<Ck>::new();
153-
checksum_eng.input_hrp(&self.hrp());
153+
checksum_eng.input_hrp(self.hrp());
154154

155155
// Unwrap ok since we checked all characters in our constructor.
156156
for fe in self.data.iter().map(|&b| Fe32::from_char(b.into()).unwrap()) {

src/blech32/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ impl crate::bech32::Checksum for Blech32 {
3737
0x7093e5a608865b,
3838
];
3939
const TARGET_RESIDUE: u64 = 1;
40+
41+
const CODE_LENGTH: usize = 9999; // TODO
4042
}
4143

4244
/// The blech32m checksum algorithm.
@@ -54,5 +56,6 @@ impl crate::bech32::Checksum for Blech32m {
5456
0x7093e5a608865b,
5557
];
5658
const TARGET_RESIDUE: u64 = 0x455972a3350f7a1;
57-
}
5859

60+
const CODE_LENGTH: usize = 9999; // TODO
61+
}

src/blind.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ use secp256k1_zkp::{Generator, RangeProof, Secp256k1, Signing, SurjectionProof};
2626

2727
use crate::{AddressParams, Script, TxIn};
2828

29+
use crate::hashes;
2930
use crate::{
3031
confidential::{Asset, AssetBlindingFactor, Nonce, Value, ValueBlindingFactor},
3132
Address, AssetId, Transaction, TxOut, TxOutWitness,
3233
};
33-
use crate::hashes;
3434

3535
/// Transaction Output related errors
3636
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -229,7 +229,11 @@ impl RangeProofMessage {
229229
}
230230

231231
/// Information about Transaction Input Asset
232-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "actual_serde"))]
232+
#[cfg_attr(
233+
feature = "serde",
234+
derive(Serialize, Deserialize),
235+
serde(crate = "actual_serde")
236+
)]
233237
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
234238
pub struct TxOutSecrets {
235239
/// Asset
@@ -1462,7 +1466,7 @@ mod tests {
14621466
SECP256K1,
14631467
&PrivateKey {
14641468
compressed: true,
1465-
network: Network::Regtest,
1469+
network: bitcoin::NetworkKind::Test,
14661470
inner: sk,
14671471
},
14681472
);
@@ -1471,7 +1475,7 @@ mod tests {
14711475
SECP256K1,
14721476
&PrivateKey {
14731477
compressed: true,
1474-
network: Network::Regtest,
1478+
network: bitcoin::NetworkKind::Test,
14751479
inner: blinding_sk,
14761480
},
14771481
);

0 commit comments

Comments
 (0)