Skip to content

Commit

Permalink
Merge branch 'v0.5' into v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Mar 11, 2022
2 parents 8daef3f + 629d6b0 commit 0b7c6e4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 92 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2021 LNP/BP Standards Association
Copyright (c) 2020-2022 LNP/BP Standards Association

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 11 additions & 13 deletions bech32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ extern crate strict_encoding;
extern crate serde_crate as serde;

use std::convert::{Infallible, TryFrom};
use std::fmt::{self, Debug, Formatter};
use std::str::FromStr;

use amplify::hex::ToHex;
use bech32::{FromBase32, ToBase32, Variant};
use bitcoin_hashes::{sha256t, Hash};
#[cfg(feature = "zip")]
Expand Down Expand Up @@ -113,20 +115,10 @@ impl From<Infallible> for Error {
serde(crate = "serde_crate", transparent)
)]
#[derive(
Wrapper,
Clone,
Ord,
PartialOrd,
Eq,
PartialEq,
Hash,
Default,
Debug,
Display,
From,
StrictEncode,
StrictDecode
Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Display,
From
)]
#[derive(StrictEncode, StrictDecode)]
#[wrap(
Index,
IndexMut,
Expand All @@ -149,6 +141,12 @@ impl AsRef<[u8]> for Blob {
fn as_ref(&self) -> &[u8] { &self.0 }
}

impl Debug for Blob {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Blob({})", self.0.to_hex())
}
}

impl FromStr for Blob {
type Err = Error;

Expand Down
1 change: 0 additions & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ strict_encoding = { version = "1.8.0", git = "https://github.com/LNP-BP/client_s
lightning_encoding = { version = "0.6.0", git = "https://github.com/LNP-BP/lnp-core", branch = "v0.6" }
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }
serde_with = { version = "1.8", features = ["hex"], optional = true }
lazy_static = "1.4.0" # TODO: #213 Remove dependency

[features]
serde = ["serde_crate", "serde_with", "bitcoin_hashes/serde", "bitcoin/use-serde"]
172 changes: 95 additions & 77 deletions chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@
extern crate amplify;
#[macro_use]
extern crate bitcoin_hashes;
#[macro_use]
extern crate strict_encoding;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_crate as serde;
#[macro_use]
extern crate lazy_static;

use std::cmp::Ordering;
use std::convert::TryFrom;
Expand All @@ -46,8 +42,10 @@ use bitcoin::hashes::hex::{self, FromHex, ToHex};
use bitcoin::hashes::{sha256d, Hash};
use bitcoin::network::constants::Network;
use bitcoin::BlockHash;
use once_cell::sync::Lazy;
use strict_encoding::{
strict_deserialize, strict_serialize, StrictDecode, StrictEncode,
strict_decode_self, strict_deserialize, strict_encode_list,
strict_serialize, StrictDecode, StrictEncode,
};

/// P2P network magic number: prefix identifying network on which node operates
Expand Down Expand Up @@ -285,9 +283,9 @@ pub const GENESIS_HASH_LIQUIDV1: &[u8] = &[
0x37, 0x92, 0x96, 0x88, 0x8a, 0x20, 0x60, 0x03,
];

lazy_static! {
/// Bitcoin mainnet chain parameters
static ref CHAIN_PARAMS_MAINNET: ChainParams = ChainParams {
/// Bitcoin mainnet chain parameters
static CHAIN_PARAMS_MAINNET: Lazy<ChainParams> = Lazy::new(|| {
ChainParams {
name: "bitcoin".to_string(),
p2p_magic: P2pNetworkId::Mainnet,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_MAINNET)
Expand All @@ -312,14 +310,17 @@ lazy_static! {
},
is_testnet: false,
is_pow: true,
};
}
});

/// Bitcoin testnet chain parameters
static ref CHAIN_PARAMS_TESTNET: ChainParams = ChainParams {
/// Bitcoin testnet chain parameters
static CHAIN_PARAMS_TESTNET: Lazy<ChainParams> = Lazy::new(|| {
ChainParams {
name: "testnet".to_string(),
p2p_magic: P2pNetworkId::Testnet,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_TESTNET)
.expect("Bitcoin testnet genesis hash contains invalid binary data"),
genesis_hash: BlockHash::from_slice(GENESIS_HASH_TESTNET).expect(
"Bitcoin testnet genesis hash contains invalid binary data",
),
bip70_name: "test".to_string(),
bip173_prefix: "tb".to_string(),
p2p_port: 18333,
Expand All @@ -334,72 +335,77 @@ lazy_static! {
unit_of_accounting: "Test Bitcoin".to_string(),
indivisible_unit: "Test satoshi".to_string(),
divisibility: 100_000_000,
asset_id: AssetId::from_slice(GENESIS_HASH_TESTNET)
.expect("Bitcoin testnet genesis hash contains invalid binary data"),
asset_id: AssetId::from_slice(GENESIS_HASH_TESTNET).expect(
"Bitcoin testnet genesis hash contains invalid binary data",
),
asset_system: AssetSystem::NativeBlockchain,
},
is_testnet: true,
is_pow: true,
};

/// Bitcoin regtest chain parameters
static ref CHAIN_PARAMS_REGTEST: ChainParams = ChainParams {
name: "regtest".to_string(),
p2p_magic: P2pNetworkId::Regtest,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_REGTEST)
.expect("Bitcoin regtest genesis hash contains invalid binary data"),
bip70_name: "regtest".to_string(),
bip173_prefix: "tb".to_string(),
p2p_port: 28333,
rpc_port: 28332,
ln_height: 1,
rgb_height: 1,
format: ChainFormat::Bitcoin,
dust_limit: 546,
native_asset: AssetParams {
ticker: "tBTC".to_string(),
unit_of_accounting: "Test Bitcoin".to_string(),
indivisible_unit: "Test satoshi".to_string(),
divisibility: 100_000_000,
asset_id: AssetId::from_slice(GENESIS_HASH_REGTEST)
.expect("Bitcoin regtest genesis hash contains invalid binary data"),
asset_system: AssetSystem::NativeBlockchain,
},
is_testnet: true,
is_pow: false,
};

/// Bitcoin signet chain parameters
static ref CHAIN_PARAMS_SIGNET: ChainParams = ChainParams {
name: "signet".to_string(),
p2p_magic: P2pNetworkId::Signet,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_SIGNET)
}
});

/// Bitcoin regtest chain parameters
static CHAIN_PARAMS_REGTEST: Lazy<ChainParams> = Lazy::new(|| ChainParams {
name: "regtest".to_string(),
p2p_magic: P2pNetworkId::Regtest,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_REGTEST)
.expect("Bitcoin regtest genesis hash contains invalid binary data"),
bip70_name: "regtest".to_string(),
bip173_prefix: "tb".to_string(),
p2p_port: 28333,
rpc_port: 28332,
ln_height: 1,
rgb_height: 1,
format: ChainFormat::Bitcoin,
dust_limit: 546,
native_asset: AssetParams {
ticker: "tBTC".to_string(),
unit_of_accounting: "Test Bitcoin".to_string(),
indivisible_unit: "Test satoshi".to_string(),
divisibility: 100_000_000,
asset_id: AssetId::from_slice(GENESIS_HASH_REGTEST).expect(
"Bitcoin regtest genesis hash contains invalid binary data",
),
asset_system: AssetSystem::NativeBlockchain,
},
is_testnet: true,
is_pow: false,
});

/// Bitcoin signet chain parameters
static CHAIN_PARAMS_SIGNET: Lazy<ChainParams> = Lazy::new(|| ChainParams {
name: "signet".to_string(),
p2p_magic: P2pNetworkId::Signet,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_SIGNET)
.expect("Bitcoin signet genesis hash contains invalid binary data"),
bip70_name: "signet".to_string(),
bip173_prefix: "tb".to_string(),
p2p_port: 38333,
rpc_port: 38332,
ln_height: 1,
rgb_height: 1,
format: ChainFormat::Bitcoin,
dust_limit: 546,
native_asset: AssetParams {
ticker: "sBTC".to_string(),
unit_of_accounting: "Signet Bitcoin".to_string(),
indivisible_unit: "Signet satoshi".to_string(),
divisibility: 100_000_000,
asset_id: AssetId::from_slice(GENESIS_HASH_SIGNET)
.expect("Bitcoin signet genesis hash contains invalid binary data"),
bip70_name: "signet".to_string(),
bip173_prefix: "tb".to_string(),
p2p_port: 38333,
rpc_port: 38332,
ln_height: 1,
rgb_height: 1,
format: ChainFormat::Bitcoin,
dust_limit: 546,
native_asset: AssetParams {
ticker: "sBTC".to_string(),
unit_of_accounting: "Signet Bitcoin".to_string(),
indivisible_unit: "Signet satoshi".to_string(),
divisibility: 100_000_000,
asset_id: AssetId::from_slice(GENESIS_HASH_SIGNET)
.expect("Bitcoin signet genesis hash contains invalid binary data"),
asset_system: AssetSystem::NativeBlockchain,
},
is_testnet: true,
is_pow: false,
};

/// Liquid V1 chain parameters
static ref CHAIN_PARAMS_LIQUIDV1: ChainParams = ChainParams {
asset_system: AssetSystem::NativeBlockchain,
},
is_testnet: true,
is_pow: false,
});

/// Liquid V1 chain parameters
static CHAIN_PARAMS_LIQUIDV1: Lazy<ChainParams> = Lazy::new(|| {
ChainParams {
name: "liquidv1".to_string(),
// TODO #216: check Liquid network magic number and change this if needed
// TODO #216: check Liquid network magic number and change this if
// needed
p2p_magic: P2pNetworkId::Mainnet,
genesis_hash: BlockHash::from_slice(GENESIS_HASH_LIQUIDV1)
.expect("Liquid V1 genesis hash contains invalid binary data"),
Expand All @@ -422,8 +428,8 @@ lazy_static! {
},
is_testnet: false,
is_pow: false,
};
}
}
});

/// Enum identifying format for transaction & block structure in a given chain.
/// Right now only two structures are supported: Bitcoin format and
Expand Down Expand Up @@ -739,6 +745,16 @@ impl std::hash::Hash for Chain {
}

impl Chain {
/// Enumerates all known standard bitcoin (BP) chains
pub fn all_standard() -> &'static [Chain] {
&[
Chain::Mainnet,
Chain::Testnet3,
Chain::Signet,
Chain::LiquidV1,
]
}

/// Returns chain parameters [ChainParams] for a given chain id
pub fn chain_params(&self) -> ChainParams {
match self {
Expand Down Expand Up @@ -1006,8 +1022,10 @@ impl FromStr for Chain {

#[cfg(test)]
mod test {
#![allow(deprecated)] // TODO: #210 Refactor with strict_encoding_test crate
use strict_encoding::test_helpers::*;
use strict_encoding_test::{
test_encoding_enum, test_encoding_enum_by_values,
test_encoding_enum_u8_exhaustive, test_encoding_roundtrip,
};

use super::*;

Expand Down

0 comments on commit 0b7c6e4

Please sign in to comment.