#[cfg(feature = "std")]
use std::error;
+use core::convert::TryFrom;
use core::fmt;
use core::fmt::{Display, Formatter};
use core::num::ParseIntError;
use bech32::{u5, FromBase32};
+use bitcoin::{PubkeyHash, ScriptHash};
+use bitcoin::util::address::WitnessVersion;
use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256;
use crate::prelude::*;
Ok(TaggedField::PrivateRoute(PrivateRoute::from_base32(field_data)?)),
constants::TAG_PAYMENT_SECRET =>
Ok(TaggedField::PaymentSecret(PaymentSecret::from_base32(field_data)?)),
+ constants::TAG_PAYMENT_METADATA =>
+ Ok(TaggedField::PaymentMetadata(Vec::<u8>::from_base32(field_data)?)),
constants::TAG_FEATURES =>
Ok(TaggedField::Features(InvoiceFeatures::from_base32(field_data)?)),
_ => {
if bytes.len() < 2 || bytes.len() > 40 {
return Err(ParseError::InvalidSegWitProgramLength);
}
-
+ let version = WitnessVersion::try_from(version).expect("0 through 16 are valid SegWit versions");
Ok(Fallback::SegWitProgram {
version,
program: bytes
})
},
17 => {
- if bytes.len() != 20 {
- return Err(ParseError::InvalidPubKeyHashLength);
- }
- //TODO: refactor once const generics are available
- let mut pkh = [0u8; 20];
- pkh.copy_from_slice(&bytes);
+ let pkh = match PubkeyHash::from_slice(&bytes) {
+ Ok(pkh) => pkh,
+ Err(bitcoin_hashes::Error::InvalidLength(_, _)) => return Err(ParseError::InvalidPubKeyHashLength),
+ };
Ok(Fallback::PubKeyHash(pkh))
}
18 => {
- if bytes.len() != 20 {
- return Err(ParseError::InvalidScriptHashLength);
- }
- let mut sh = [0u8; 20];
- sh.copy_from_slice(&bytes);
+ let sh = match ScriptHash::from_slice(&bytes) {
+ Ok(sh) => sh,
+ Err(bitcoin_hashes::Error::InvalidLength(_, _)) => return Err(ParseError::InvalidScriptHashLength),
+ };
Ok(Fallback::ScriptHash(sh))
}
_ => Err(ParseError::Skip)
fn test_parse_fallback() {
use crate::Fallback;
use bech32::FromBase32;
+ use bitcoin::{PubkeyHash, ScriptHash};
+ use bitcoin::util::address::WitnessVersion;
+ use bitcoin_hashes::Hash;
let cases = vec![
(
from_bech32("3x9et2e20v6pu37c5d9vax37wxq72un98".as_bytes()),
- Ok(Fallback::PubKeyHash([
+ Ok(Fallback::PubKeyHash(PubkeyHash::from_slice(&[
0x31, 0x72, 0xb5, 0x65, 0x4f, 0x66, 0x83, 0xc8, 0xfb, 0x14, 0x69, 0x59, 0xd3,
0x47, 0xce, 0x30, 0x3c, 0xae, 0x4c, 0xa7
- ]))
+ ]).unwrap()))
),
(
from_bech32("j3a24vwu6r8ejrss3axul8rxldph2q7z9".as_bytes()),
- Ok(Fallback::ScriptHash([
+ Ok(Fallback::ScriptHash(ScriptHash::from_slice(&[
0x8f, 0x55, 0x56, 0x3b, 0x9a, 0x19, 0xf3, 0x21, 0xc2, 0x11, 0xe9, 0xb9, 0xf3,
0x8c, 0xdf, 0x68, 0x6e, 0xa0, 0x78, 0x45
- ]))
+ ]).unwrap()))
),
(
from_bech32("qw508d6qejxtdg4y5r3zarvary0c5xw7k".as_bytes()),
Ok(Fallback::SegWitProgram {
- version: u5::try_from_u8(0).unwrap(),
+ version: WitnessVersion::V0,
program: Vec::from(&[
0x75u8, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45,
0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6