-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! SFT-UNKN: Add foundation-psbt crate.
- Loading branch information
Showing
9 changed files
with
264 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,44 @@ | ||
use crate::{hash_types, taproot}; | ||
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use nom::{ | ||
bytes::complete::take, | ||
combinator::map_res, | ||
error::{FromExternalError, ParseError}, | ||
IResult, | ||
error::ParseError, multi::fill, number::complete::u8, IResult, InputIter, InputLength, Slice, | ||
}; | ||
|
||
use bitcoin_hashes::Hash; | ||
|
||
use crate::{hash_types, taproot}; | ||
|
||
/// Parses a [`bitcoin_hashes::Hash`]. | ||
pub fn hash<'a, Hash, Error>(i: &'a [u8]) -> IResult<&'a [u8], Hash, Error> | ||
/// | ||
/// # Why N instead of [`bitcoin_hashes::Hash::LEN`]. | ||
/// | ||
/// NOTE(jeandudey): Using `N` because on Rust 1.70.0 it somehow can't use the | ||
/// associated constant `LEN` of `bitcoin_hashes::Hash`` trait and shows the | ||
/// following error: | ||
/// | ||
/// ```text | ||
/// error: generic parameters may not be used in const operations | ||
/// | ||
/// let mut buf = [0; Hash::LEN]; | ||
/// ``` | ||
/// | ||
/// Maybe a bug, perhaps report this upstream, I think rustc is broken in | ||
/// this edge case. | ||
/// | ||
/// Ideally one would use fill with the aforementioned `buf` variable to | ||
/// avoid this verbose loop. | ||
pub fn hash<Input, Hash, Error, const N: usize>(i: Input) -> IResult<Input, Hash, Error> | ||
where | ||
Input: | ||
Clone + PartialEq + InputLength + InputIter<Item = u8> + Slice<core::ops::RangeFrom<usize>>, | ||
Hash: bitcoin_hashes::Hash, | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], bitcoin_hashes::FromSliceError>, | ||
Error: ParseError<Input>, | ||
{ | ||
map_res(take(Hash::LEN), Hash::from_slice)(i) | ||
let mut buf = [0; N]; | ||
let (next_i, ()) = fill(u8, &mut buf)(i)?; | ||
let hash = Hash::from_slice(&buf).expect("should have the correct length"); | ||
Ok((next_i, hash)) | ||
} | ||
|
||
/// Define an alias for a hash parser. | ||
|
@@ -25,14 +50,14 @@ where | |
macro_rules! define_hash_aliases { | ||
($($name:ident),* $(,)?) => { | ||
$( | ||
pub fn $name<'a, Error>( | ||
i: &'a [u8], | ||
) -> IResult<&'a [u8], ::bitcoin_hashes::$name::Hash, Error> | ||
pub fn $name<Input, Error>( | ||
i: Input, | ||
) -> IResult<Input, ::bitcoin_hashes::$name::Hash, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], bitcoin_hashes::FromSliceError>, | ||
Input: Clone + PartialEq + InputLength + InputIter<Item = u8> + Slice<core::ops::RangeFrom<usize>>, | ||
Error: ParseError<Input>, | ||
{ | ||
hash::<'a, ::bitcoin_hashes::$name::Hash, Error>(i) | ||
hash::<_, ::bitcoin_hashes::$name::Hash, Error, { ::bitcoin_hashes::$name::Hash::LEN }>(i) | ||
} | ||
)* | ||
}; | ||
|
@@ -45,26 +70,29 @@ define_hash_aliases! { | |
hash160, | ||
} | ||
|
||
pub fn taproot_leaf_hash<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], taproot::LeafHash, Error> | ||
pub fn taproot_leaf_hash<Input, Error>(i: Input) -> IResult<Input, taproot::LeafHash, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], bitcoin_hashes::FromSliceError>, | ||
Input: | ||
Clone + PartialEq + InputLength + InputIter<Item = u8> + Slice<core::ops::RangeFrom<usize>>, | ||
Error: ParseError<Input>, | ||
{ | ||
hash::<'a, taproot::LeafHash, Error>(i) | ||
hash::<_, taproot::LeafHash, Error, { taproot::LeafHash::LEN }>(i) | ||
} | ||
|
||
pub fn taproot_node_hash<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], taproot::TapNodeHash, Error> | ||
pub fn taproot_node_hash<Input, Error>(i: Input) -> IResult<Input, taproot::TapNodeHash, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], bitcoin_hashes::FromSliceError>, | ||
Input: | ||
Clone + PartialEq + InputLength + InputIter<Item = u8> + Slice<core::ops::RangeFrom<usize>>, | ||
Error: ParseError<Input>, | ||
{ | ||
hash::<'a, taproot::TapNodeHash, Error>(i) | ||
hash::<_, taproot::TapNodeHash, Error, { taproot::TapNodeHash::LEN }>(i) | ||
} | ||
|
||
pub fn txid<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], hash_types::Txid, Error> | ||
pub fn txid<Input, Error>(i: Input) -> IResult<Input, hash_types::Txid, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], bitcoin_hashes::FromSliceError>, | ||
Input: | ||
Clone + PartialEq + InputLength + InputIter<Item = u8> + Slice<core::ops::RangeFrom<usize>>, | ||
Error: ParseError<Input>, | ||
{ | ||
hash::<'a, hash_types::Txid, Error>(i) | ||
hash::<_, hash_types::Txid, Error, { hash_types::Txid::LEN }>(i) | ||
} |
Oops, something went wrong.