-
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.
Signed-off-by: Jean-Pierre De Jesus DIAZ <[email protected]>
- Loading branch information
Showing
4 changed files
with
172 additions
and
74 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
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use nom::branch::alt; | ||
use nom::combinator::{eof, map, rest}; | ||
use nom::error::{FromExternalError, ParseError}; | ||
use nom::number::complete::le_u64; | ||
use nom::multi::fold_many0; | ||
use nom::IResult; | ||
|
||
use secp256k1::{PublicKey, XOnlyPublicKey}; | ||
|
||
use foundation_bip32::{parser::key_source, KeySource}; | ||
|
||
use crate::parser::keypair::key_pair; | ||
use crate::parser::secp::{public_key, x_only_public_key}; | ||
|
||
pub fn output_map<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], OutputMap<'a>, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
fold_many0( | ||
output_key_pair, | ||
OutputMap::default, | ||
|mut map, key_pair| { | ||
match key_pair { | ||
KeyPair::RedeemScript(v) => map.redeem_script = Some(v), | ||
KeyPair::WitnessScript(v) => map.witness_script = Some(v), | ||
KeyPair::Bip32Derivation(_, _) => (), // TODO | ||
KeyPair::Amount(v) => map.amount = Some(v), | ||
KeyPair::Script(v) => map.script = Some(v), | ||
KeyPair::TapInternalKey(v) => map.tap_internal_key = Some(v), | ||
KeyPair::TapTree(v) => map.tap_tree = Some(v), | ||
KeyPair::TapBip32Derivation(_, _) => (), // TODO | ||
}; | ||
|
||
map | ||
}, | ||
)(i) | ||
} | ||
|
||
#[rustfmt::skip] | ||
fn output_key_pair<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], KeyPair<'a>, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
let redeem_script = key_pair(0x00, eof, rest); | ||
let witness_script = key_pair(0x01, eof, rest); | ||
let bip32_derivation = key_pair(0x02, public_key, key_source); | ||
let amount = key_pair(0x03, eof, le_u64); | ||
let script = key_pair(0x04, eof, rest); | ||
let tap_internal_key = key_pair(0x05, eof, x_only_public_key); | ||
let tap_tree = key_pair(0x06, eof, rest); | ||
let tap_bip32_derivation = key_pair(0x06, x_only_public_key, rest); | ||
|
||
alt(( | ||
map(redeem_script, |(_, v)| KeyPair::RedeemScript(v)), | ||
map(witness_script, |(_, v)| KeyPair::WitnessScript(v)), | ||
map(bip32_derivation, |(k, v)| KeyPair::Bip32Derivation(k, v)), | ||
map(amount, |(_, v)| KeyPair::Amount(v)), | ||
map(script, |(_, v)| KeyPair::Script(v)), | ||
map(tap_internal_key, |(_, v)| KeyPair::TapInternalKey(v)), | ||
map(tap_tree, |(_, v)| KeyPair::TapTree(v)), | ||
map(tap_bip32_derivation, |(k, v)| KeyPair::TapBip32Derivation(k, v)), | ||
))(i) | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct OutputMap<'a> { | ||
pub redeem_script: Option<&'a [u8]>, | ||
pub witness_script: Option<&'a [u8]>, | ||
pub amount: Option<u64>, | ||
pub script: Option<&'a [u8]>, | ||
pub tap_internal_key: Option<XOnlyPublicKey>, | ||
pub tap_tree: Option<&'a [u8]>, | ||
} | ||
|
||
enum KeyPair<'a> { | ||
RedeemScript(&'a [u8]), | ||
WitnessScript(&'a [u8]), | ||
Bip32Derivation(PublicKey, KeySource<'a>), | ||
Amount(u64), | ||
Script(&'a [u8]), | ||
TapInternalKey(XOnlyPublicKey), | ||
TapTree(&'a [u8]), | ||
TapBip32Derivation(XOnlyPublicKey, &'a [u8]), | ||
} |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use nom::bytes::complete::take; | ||
use nom::combinator::map_res; | ||
use nom::error::ErrorKind; | ||
use nom::error::FromExternalError; | ||
use nom::error::ParseError; | ||
use nom::Err; | ||
use nom::IResult; | ||
|
||
use secp256k1::{ecdsa, schnorr, PublicKey, XOnlyPublicKey}; | ||
|
||
pub fn x_only_public_key<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], XOnlyPublicKey, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
map_res(take(32usize), |data| XOnlyPublicKey::from_slice(data))(i) | ||
} | ||
|
||
pub fn public_key<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], PublicKey, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
match PublicKey::from_slice(i) { | ||
Ok(p) => Ok((&i[i.len()..], p)), | ||
Err(e) => Err(Err::Failure(Error::from_external_error( | ||
i, | ||
ErrorKind::Fail, | ||
e, | ||
))), | ||
} | ||
} | ||
|
||
pub fn ecdsa_signature<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], ecdsa::Signature, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
match ecdsa::Signature::from_der(i) { | ||
Ok(p) => Ok((&i[i.len()..], p)), | ||
Err(e) => Err(Err::Failure(Error::from_external_error( | ||
i, | ||
ErrorKind::Fail, | ||
e, | ||
))), | ||
} | ||
} | ||
|
||
pub fn schnorr_signature<'a, Error>(i: &'a [u8]) -> IResult<&'a [u8], schnorr::Signature, Error> | ||
where | ||
Error: ParseError<&'a [u8]>, | ||
Error: FromExternalError<&'a [u8], secp256k1::Error>, | ||
{ | ||
match schnorr::Signature::from_slice(i) { | ||
Ok(p) => Ok((&i[i.len()..], p)), | ||
Err(e) => Err(Err::Failure(Error::from_external_error( | ||
i, | ||
ErrorKind::Fail, | ||
e, | ||
))), | ||
} | ||
} |