Skip to content

Commit

Permalink
FromStr & ToHex for Witness
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 24, 2021
1 parent 33032a3 commit 9488204
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use consensus::{Decodable, Encodable, WriteExt};
use io::{self, Read, Write};
use core::fmt::{Display, Formatter, self};
use hashes::hex::ToHex;
use core::str::FromStr;
use hashes::hex;
use hashes::hex::{FromHex, ToHex};
use prelude::*;
use VarInt;

Expand Down Expand Up @@ -188,6 +191,23 @@ impl Display for Witness {
}
}

impl FromStr for Witness {
type Err = hex::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let data = s.split('\n').map(Vec::<u8>::from_hex).collect::<Result<Vec<_>, _>>()?;
Ok(Witness::from(data))
}
}

impl ToHex for Witness {
fn to_hex(&self) -> String {
let mut vec = Vec::with_capacity(self.content.len());
self.consensus_encode(&mut vec).expect("witness memory hex encoder");
vec.to_hex()
}
}

impl Default for Witness {
fn default() -> Self {
// from https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new
Expand Down

0 comments on commit 9488204

Please sign in to comment.