Skip to content

Commit

Permalink
bitcoin 0.32.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed May 31, 2024
1 parent cd69f3b commit f4147c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions substrate/client/src/networks/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ impl TryFrom<Vec<u8>> for Address {
}

fn try_to_vec(addr: &Address) -> Result<Vec<u8>, ()> {
let witness_program = |addr: &Address| {
let program_push = addr.0.as_script().instructions().last().ok_or(())?.map_err(|_| ())?;
let program = program_push.push_bytes().ok_or(())?.as_bytes();
Ok::<_, ()>(program.to_vec())
};

let parsed_addr =
BAddress::<NetworkChecked>::from_script(&addr.0, Network::Bitcoin).map_err(|_| ())?;
Ok(
Expand All @@ -100,21 +94,19 @@ fn try_to_vec(addr: &Address) -> Result<Vec<u8>, ()> {
EncodedAddress::P2SH(*parsed_addr.script_hash().unwrap().as_raw_hash().as_byte_array())
}
Some(AddressType::P2wpkh) => {
let program = witness_program(addr)?;
let mut buf = [0; 20];
buf.copy_from_slice(program.as_ref());
EncodedAddress::P2WPKH(buf)
let program = parsed_addr.witness_program().ok_or(())?;
let program = program.program().as_bytes();
EncodedAddress::P2WPKH(program.try_into().map_err(|_| ())?)
}
Some(AddressType::P2wsh) => {
let program = witness_program(addr)?;
let mut buf = [0; 32];
buf.copy_from_slice(program.as_ref());
EncodedAddress::P2WSH(buf)
let program = parsed_addr.witness_program().ok_or(())?;
let program = program.program().as_bytes();
EncodedAddress::P2WSH(program.try_into().map_err(|_| ())?)
}
Some(AddressType::P2tr) => {
let program = witness_program(addr)?;
let program_ref: &[u8] = program.as_ref();
EncodedAddress::P2TR(program_ref.try_into().map_err(|_| ())?)
let program = parsed_addr.witness_program().ok_or(())?;
let program = program.program().as_bytes();
EncodedAddress::P2TR(program.try_into().map_err(|_| ())?)
}
_ => Err(())?,
})
Expand Down

0 comments on commit f4147c3

Please sign in to comment.