Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

full segwit support #953

Merged
merged 12 commits into from
Jun 1, 2021
28 changes: 18 additions & 10 deletions Cargo.lock

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

24 changes: 16 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ async-std = { version = "1.5", features = ["unstable"] }
async-trait = "0.1"
atomic = "0.4"
bigdecimal = { version = "0.1", features = ["serde"] }
bitcrypto = { git = "https://github.com/artemii235/parity-bitcoin.git" }
bitcrypto = { path = "mm2src/mm2_bitcoin/crypto" }
blake2 = "0.9.1"
bytes = "0.4"
chain = { git = "https://github.com/artemii235/parity-bitcoin.git" }
chain = { path = "mm2src/mm2_bitcoin/chain" }
coins = { path = "mm2src/coins" }
common = { path = "mm2src/common" }
crc = "1.8"
Expand All @@ -72,34 +72,34 @@ hex = "0.3.2"
hex-literal = "0.3.1"
http = "0.2"
itertools = "0.9"
keys = { git = "https://github.com/artemii235/parity-bitcoin.git" }
keys = { path = "mm2src/mm2_bitcoin/keys" }
lazy_static = "1.3"
libc = "0.2"
metrics = "0.12"
mm2-libp2p = { path = "mm2src/mm2_libp2p" }
num-rational = { version = "0.2", features = ["serde", "bigint", "bigint-std"] }
num-traits = "0.2"
rpc = { git = "https://github.com/artemii235/parity-bitcoin.git" }
rpc = { path = "mm2src/mm2_bitcoin/rpc" }
parking_lot = { version = "0.11", features = ["nightly"] }
parity-util-mem = "0.7"
# AP: portfolio RPCs are not documented and not used as of now
# so the crate is disabled to speed up the entire removal of C code
# portfolio = { path = "mm2src/portfolio" }
primitives = { git = "https://github.com/artemii235/parity-bitcoin.git" }
primitives = { path = "mm2src/mm2_bitcoin/primitives" }
rand = { version = "0.7", features = ["std", "small_rng"] }
rmp-serde = "0.14.3"
# TODO: Reduce the size of regex by disabling the features we don't use.
# cf. https://github.com/rust-lang/regex/issues/583
regex = "1"
script = { git = "https://github.com/artemii235/parity-bitcoin.git" }
script = { path = "mm2src/mm2_bitcoin/script" }
serde = "1.0"
serde_bencode = "0.2"
serde_json = { version = "1.0", features = ["preserve_order"] }
serde_derive = "1.0"
ser_error = { path = "mm2src/derives/ser_error" }
ser_error_derive = { path = "mm2src/derives/ser_error_derive" }
serialization = { git = "https://github.com/artemii235/parity-bitcoin.git" }
serialization_derive = { git = "https://github.com/artemii235/parity-bitcoin.git" }
serialization = { path = "mm2src/mm2_bitcoin/serialization" }
serialization_derive = { path = "mm2src/mm2_bitcoin/serialization_derive" }
sp-trie = "2.0.0"
sql-builder = "3.1.1"

Expand Down Expand Up @@ -140,6 +140,14 @@ regex = "1"
members = [
"mm2src/coins",
"mm2src/floodsub",
"mm2src/mm2_bitcoin/crypto",
"mm2src/mm2_bitcoin/chain",
"mm2src/mm2_bitcoin/keys",
"mm2src/mm2_bitcoin/rpc",
"mm2src/mm2_bitcoin/primitives",
"mm2src/mm2_bitcoin/script",
"mm2src/mm2_bitcoin/serialization",
"mm2src/mm2_bitcoin/serialization_derive",
"mm2src/mm2_libp2p",
"mm2src/gossipsub",
"mm2src/derives/ser_error",
Expand Down
16 changes: 8 additions & 8 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ async-trait = "0.1"
base64 = "0.10.0"
bigdecimal = { version = "0.1.0", features = ["serde"] }
bitcoin-cash-slp = "0.3.1"
bitcrypto = { git = "https://github.com/artemii235/parity-bitcoin.git" }
bitcrypto = { path = "../mm2_bitcoin/crypto" }
byteorder = "1.3"
bytes = "0.4"
chain = { git = "https://github.com/artemii235/parity-bitcoin.git" }
chain = { path = "../mm2_bitcoin/chain" }
common = { path = "../common" }
derive_more = "0.99"
ethabi = { git = "https://github.com/artemii235/ethabi" }
Expand All @@ -34,26 +34,26 @@ hex = "0.3.2"
http = "0.2"
itertools = "0.9"
jsonrpc-core = "8.0.1"
keys = { git = "https://github.com/artemii235/parity-bitcoin.git" }
keys = { path = "../mm2_bitcoin/keys" }
lazy_static = "1.3"
libc = "0.2"
libsecp256k1 = "0.3.5"
metrics = "0.12"
mocktopus = "0.7.0"
num-traits = "0.2"
primitives = { git = "https://github.com/artemii235/parity-bitcoin.git" }
primitives = { path = "../mm2_bitcoin/primitives" }
rand = { version = "0.7", features = ["std", "small_rng"] }
rlp = { git = "https://github.com/artemii235/parity-common" }
rpc = { git = "https://github.com/artemii235/parity-bitcoin.git" }
script = { git = "https://github.com/artemii235/parity-bitcoin.git" }
rpc = { path = "../mm2_bitcoin/rpc" }
script = { path = "../mm2_bitcoin/script" }
secp256k1_bindings = { version = "0.20", package = "secp256k1" }
ser_error = { path = "../derives/ser_error" }
ser_error_derive = { path = "../derives/ser_error_derive" }
serde = "1.0"
serde_derive = "1.0"
serde_json = { version = "1.0", features = ["preserve_order", "raw_value"] }
serialization = { git = "https://github.com/artemii235/parity-bitcoin.git" }
serialization_derive = { git = "https://github.com/artemii235/parity-bitcoin.git" }
serialization = { path = "../mm2_bitcoin/serialization" }
serialization_derive = { path = "../mm2_bitcoin/serialization_derive" }
sha2 = "0.8"
sha3 = "0.8"
# One of web3 dependencies is the old `tokio-uds 0.1.7` which fails cross-compiling to ARM.
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ fn test_get_fee_to_send_taker_fee_insufficient_balance() {
.unwrap_err();
log!((error));
assert!(
matches!(error.get_inner(), TradePreimageError::NotSufficientBalance {..}),
matches!(error.get_inner(), TradePreimageError::NotSufficientBalance { .. }),
"Expected TradePreimageError::NotSufficientBalance"
);
}
Expand Down
15 changes: 13 additions & 2 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use futures::lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
use futures::stream::StreamExt;
use futures01::Future;
use keys::bytes::Bytes;
pub use keys::{Address, KeyPair, Private, Public, Secret};
pub use keys::{Address, BTCNetwork, KeyPair, Private, Public, Secret};
#[cfg(test)] use mocktopus::macros::*;
use num_traits::ToPrimitive;
use primitives::hash::{H256, H264, H512};
Expand Down Expand Up @@ -216,6 +216,10 @@ pub enum UtxoAddressFormat {
/// In Bitcoin Cash context the standard format also known as 'legacy'.
#[serde(rename = "standard")]
Standard,
/// Segwit Address
/// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
#[serde(rename = "segwit")]
Segwit(BTCNetwork),
/// Bitcoin Cash specific address format.
/// https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/cashaddr.md
#[serde(rename = "cashaddress")]
Expand Down Expand Up @@ -387,6 +391,9 @@ pub struct UtxoCoinConf {
/// will be the Segwit (starting from 3 for BTC case) instead of legacy
/// https://en.bitcoin.it/wiki/Segregated_Witness
pub segwit: bool,
/// If true - allows generation and withdrawal to P2WPKH and P2WSH addresses (starting with bc1 for bitcoin and tb1 for testnet).
/// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Examples
pub full_segwit: bool,
/// Does coin require transactions to be notarized to be considered as confirmed?
/// https://komodoplatform.com/security-delayed-proof-of-work-dpow/
pub requires_notarization: AtomicBool,
Expand Down Expand Up @@ -849,6 +856,7 @@ impl<'a> UtxoConfBuilder<'a> {

let is_pos = self.is_pos();
let segwit = self.segwit();
let full_segwit = self.full_segwit();
let force_min_relay_fee = self.conf["force_min_relay_fee"].as_bool().unwrap_or(false);
let mtp_block_count = self.mtp_block_count();
let estimate_fee_mode = self.estimate_fee_mode();
Expand All @@ -864,6 +872,7 @@ impl<'a> UtxoConfBuilder<'a> {
pub_t_addr_prefix,
p2sh_t_addr_prefix,
segwit,
full_segwit,
wif_prefix,
tx_version,
address_format,
Expand Down Expand Up @@ -918,7 +927,7 @@ impl<'a> UtxoConfBuilder<'a> {

fn address_format(&self) -> Result<UtxoAddressFormat, String> {
let conf = self.conf;
if conf["address_format"].is_null() {
if (conf["address_format"].is_null() || conf["address_format"]["format"] == "segwit") && !self.full_segwit() {
artemii235 marked this conversation as resolved.
Show resolved Hide resolved
Ok(UtxoAddressFormat::Standard)
} else {
json::from_value(self.conf["address_format"].clone()).map_err(|e| ERRL!("{}", e))
Expand Down Expand Up @@ -1021,6 +1030,8 @@ impl<'a> UtxoConfBuilder<'a> {

fn segwit(&self) -> bool { self.conf["segwit"].as_bool().unwrap_or(false) }

fn full_segwit(&self) -> bool { self.req["full_segwit"].as_bool().unwrap_or(false) }
artemii235 marked this conversation as resolved.
Show resolved Hide resolved

fn mtp_block_count(&self) -> NonZeroU64 {
json::from_value(self.conf["mtp_block_count"].clone()).unwrap_or(KMD_MTP_BLOCK_COUNT)
}
Expand Down
6 changes: 5 additions & 1 deletion mm2src/coins/utxo/qtum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ pub trait QtumBasedCoin: AsRef<UtxoCoinFields> + UtxoCommonOps + MarketCoinOps {
let balance = self
.as_ref()
.rpc_client
.display_balance(self.as_ref().my_address.clone(), self.as_ref().decimals)
.display_balance(
self.as_ref().my_address.clone(),
&self.as_ref().conf.address_format,
self.as_ref().decimals,
)
.compat()
.await?;

Expand Down
24 changes: 19 additions & 5 deletions mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![cfg_attr(target_arch = "wasm32", allow(unused_macros))]
#![cfg_attr(target_arch = "wasm32", allow(dead_code))]

use crate::utxo::sat_from_big_decimal;
use crate::utxo::{sat_from_big_decimal, UtxoAddressFormat};
use crate::{NumConversError, RpcTransportEventHandler, RpcTransportEventHandlerShared};
use bigdecimal::BigDecimal;
use chain::{BlockHeader, OutPoint, Transaction as UtxoTx};
Expand Down Expand Up @@ -228,7 +228,8 @@ pub trait UtxoRpcClientOps: fmt::Debug + Send + Sync + 'static {

fn get_block_count(&self) -> RpcRes<u64>;

fn display_balance(&self, address: Address, decimals: u8) -> RpcRes<BigDecimal>;
fn display_balance(&self, address: Address, address_format: &UtxoAddressFormat, decimals: u8)
-> RpcRes<BigDecimal>;

/// returns fee estimation per KByte in satoshis
fn estimate_fee_sat(
Expand Down Expand Up @@ -586,7 +587,12 @@ impl UtxoRpcClientOps for NativeClient {

fn get_block_count(&self) -> RpcRes<u64> { self.0.get_block_count() }

fn display_balance(&self, address: Address, _decimals: u8) -> RpcRes<BigDecimal> {
fn display_balance(
&self,
address: Address,
_address_format: &UtxoAddressFormat,
_decimals: u8,
) -> RpcRes<BigDecimal> {
Box::new(
self.list_unspent_impl(0, std::i32::MAX, vec![address.to_string()])
.map(|unspents| {
Expand Down Expand Up @@ -1493,8 +1499,16 @@ impl UtxoRpcClientOps for ElectrumClient {

fn get_block_count(&self) -> RpcRes<u64> { Box::new(self.blockchain_headers_subscribe().map(|r| r.block_height())) }

fn display_balance(&self, address: Address, decimals: u8) -> RpcRes<BigDecimal> {
let hash = electrum_script_hash(&Builder::build_p2pkh(&address.hash));
fn display_balance(
&self,
address: Address,
address_format: &UtxoAddressFormat,
decimals: u8,
) -> RpcRes<BigDecimal> {
let hash = match address_format {
UtxoAddressFormat::Segwit(_) => electrum_script_hash(&Builder::build_p2wpkh(&address.hash)),
_ => electrum_script_hash(&Builder::build_p2pkh(&address.hash)),
};
let hash_str = hex::encode(hash);
Box::new(self.scripthash_get_balance(&hash_str).map(move |result| {
BigDecimal::from(result.confirmed + result.unconfirmed) / BigDecimal::from(10u64.pow(decimals as u32))
Expand Down
Loading