Skip to content

Commit

Permalink
feat: implement necessary traits to replace Coin with Blockchain in n…
Browse files Browse the repository at this point in the history
…ode (#266)
  • Loading branch information
h4sh3d authored Jul 12, 2022
1 parent c5b79ce commit d979e45
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ default = ["experimental", "taproot"]
amplify = "3"
base58-monero = { version = "0.3", default-features = false, features = ["check"] }
bitvec = { version = "1.0" }
clap = { version = "3", features = ["derive"] }
fixed-hash = { version = "0.7", default-features = false }
hex = "0.4"
inet2_addr = { version = "0.6", default-features = false, features = ["tor", "strict_encoding", "serde"] }
Expand Down
31 changes: 30 additions & 1 deletion src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,47 @@ use std::fmt::{self, Debug, Display};
use std::io;
use std::str::FromStr;

use strict_encoding::{StrictDecode, StrictEncode};
use thiserror::Error;

use crate::consensus::{self, deserialize, serialize, CanonicalBytes, Decodable, Encodable};
use crate::transaction::{Buyable, Cancelable, Fundable, Lockable, Punishable, Refundable};

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Display, Serialize, Deserialize)]
/// The list of supported blockchains (coins) by this library.
#[derive(
Debug,
Clone,
Copy,
Hash,
PartialEq,
Eq,
Parser,
Display,
Serialize,
Deserialize,
StrictEncode,
StrictDecode,
)]
#[display(Debug)]
pub enum Blockchain {
/// The Bitcoin (BTC) blockchain.
Bitcoin,
/// The Monero (XMR) blockchain.
Monero,
}

impl FromStr for Blockchain {
type Err = consensus::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Bitcoin" | "bitcoin" | "btc" | "BTC" => Ok(Blockchain::Bitcoin),
"Monero" | "monero" | "xmr" | "XMR" => Ok(Blockchain::Monero),
_ => Err(consensus::Error::UnknownType),
}
}
}

impl Decodable for Blockchain {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
match Decodable::consensus_decode(d)? {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ extern crate amplify;
#[macro_use]
extern crate serde;

#[macro_use]
extern crate clap;

use thiserror::Error;

#[macro_use]
Expand Down

0 comments on commit d979e45

Please sign in to comment.