Skip to content

Commit

Permalink
consensus: make InvalidPubkey error generic over key length
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 7, 2023
1 parent 74b676d commit 722d6b3
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions consensus/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::ops::BitXor;
use std::{cmp, io, slice, vec};

use amplify::confinement::{Confined, U32};
use amplify::{confinement, Bytes32, Wrapper};
use amplify::{confinement, Array, Bytes32, Wrapper};
use commit_verify::{DigestExt, Sha256};
use secp256k1::{PublicKey, Scalar, XOnlyPublicKey};
use strict_encoding::{
Expand Down Expand Up @@ -57,10 +57,10 @@ const MIDSTATE_TAPSIGHASH: [u8; 10] = *b"TapSighash";

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display("invalid public key")]
pub struct InvalidPubkey(pub Bytes32);
pub struct InvalidPubkey<const LEN: usize>(pub Array<u8, LEN>);

impl From<InvalidPubkey> for DecodeError {
fn from(e: InvalidPubkey) -> Self {
impl<const LEN: usize> From<InvalidPubkey<LEN>> for DecodeError {
fn from(e: InvalidPubkey<LEN>) -> Self {

Check warning on line 63 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L63

Added line #L63 was not covered by tests
DecodeError::DataIntegrityError(format!("invalid x-only public key value '{:x}'", e.0))
}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ macro_rules! dumb_key {
pub struct TaprootPk(XOnlyPublicKey);

impl TaprootPk {
pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey> {
pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey<32>> {

Check warning on line 92 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L92

Added line #L92 was not covered by tests
XOnlyPublicKey::from_slice(data.as_ref())
.map(Self)
.map_err(|_| InvalidPubkey(data.into()))
Expand Down Expand Up @@ -142,7 +142,7 @@ impl InternalPk {
#[inline]
pub fn from_unchecked(pk: TaprootPk) -> Self { Self(pk.0) }

pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey> {
pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey<32>> {

Check warning on line 145 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L145

Added line #L145 was not covered by tests
XOnlyPublicKey::from_slice(&data)
.map(Self)
.map_err(|_| InvalidPubkey(data.into()))
Expand Down Expand Up @@ -220,7 +220,7 @@ impl OutputPk {
#[inline]
pub fn from_unchecked(pk: TaprootPk) -> Self { Self(pk.0) }

pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey> {
pub fn from_byte_array(data: [u8; 32]) -> Result<Self, InvalidPubkey<32>> {

Check warning on line 223 in consensus/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

consensus/src/taproot.rs#L223

Added line #L223 was not covered by tests
XOnlyPublicKey::from_slice(&data)
.map(Self)
.map_err(|_| InvalidPubkey(data.into()))
Expand Down

0 comments on commit 722d6b3

Please sign in to comment.