Skip to content

Commit

Permalink
update to edition 2018, use bitcoin 0.18, update some other deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Melnik committed Mar 26, 2019
1 parent 0c7cd40 commit df2e717
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 117 deletions.
11 changes: 9 additions & 2 deletions bitcoin_core_io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
name = "bitcoin_core_io"
version = "0.1.0"
authors = ["evgeniy.scherbina <[email protected]>"]
edition = "2018"

[dependencies.bitcoin]
version = "0.18"
features = ["use-serde"]

[dependencies.bitcoin_hashes]
version = "0.3"

[dependencies]
bitcoin = "0.14.2"
bitcoin_rpc_client = { git = "https://github.com/rust-bitcoin-tools/bitcoinrpc_rust_client.git", branch = "enhancement" }
bitcoin_rpc_client = { git = "https://github.com/LightningPeach/bitcoinrpc-rust-client.git" }
wallet = { path = "../wallet" }
12 changes: 5 additions & 7 deletions bitcoin_core_io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate bitcoin;
extern crate bitcoin_rpc_client;
extern crate wallet;

use bitcoin::{
Block, BlockHeader, Transaction,
util::hash::Sha256dHash,
};
use bitcoin_rpc_client::{BitcoinCoreClient, BitcoinRpcApi, SerializedRawTransaction};
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin_rpc_client::{BitcoinCoreClient, BitcoinRpcApi, rpc::SerializedRawTransaction};
use wallet::interface::BlockChainIO;

pub struct BitcoinCoreIO(BitcoinCoreClient);
Expand All @@ -42,6 +38,8 @@ impl BlockChainIO for BitcoinCoreIO {
}

fn get_block(&self, header_hash: &Sha256dHash) -> Block {
use bitcoin_hashes::hex::FromHex;

let block = self.0.get_block(header_hash).unwrap().unwrap();

// TODO(evg): review it
Expand All @@ -60,7 +58,7 @@ impl BlockChainIO for BitcoinCoreIO {
.get_raw_transaction_serialized(&txid)
.unwrap()
.unwrap();
let tx: Transaction = Transaction::from(tx_hex);
let tx: Transaction = tx_hex.into();
txdata.push(tx);
}

Expand Down
32 changes: 20 additions & 12 deletions wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,44 @@ documentation = "https://github.com/rust-bitcoin/rust-wallet/"
description = "Wallet library for Bitcoin"
keywords = [ "crypto", "bitcoin" ]
readme = "README.md"
edition = "2018"

[lib]
name = "wallet"
path = "src/lib.rs"

[dependencies.bitcoin]
version = "0.14.2"
features = ["serde"]
version = "0.18"
features = ["use-serde"]

[dependencies.bitcoin_hashes]
version = "0.3"

[dependencies.secp256k1]
version = "0.11.0"
version = "0.12"
features = ["serde"]

[dependencies.serde]
version = "1.0"
features = ["derive"]

[dependencies.serde_json]
version = "1.0"

[dependencies]
rust-crypto = "0.2"
log = "0.4"
simple_logger = "0.5"
rand = "0.5"
simple_logger = "1.0"
rand = "0.6"
hex = "0.3"
bitcoin-bech32 = "0.8.0"
rocksdb = "0.10.1"
byteorder = "1.2.6"
serde = "1"
serde_derive = "1"
serde_json = "1.0.31"
bitcoin-bech32 = "0.9"
rocksdb = "0.12"
byteorder = "1.3"
electrumx_client = { git = "https://github.com/evgeniy-scherbina/rust-electrumx-client.git" }

[dev-dependencies]
bitcoin_core_io = { path = "../bitcoin_core_io" }
rustc-serialize = "0.3"
bitcoin_rpc_client = { git = "https://github.com/rust-bitcoin-tools/bitcoinrpc_rust_client.git", branch = "enhancement" }
bitcoin_rpc_client = { git = "https://github.com/LightningPeach/bitcoinrpc-rust-client.git" }
tc_coblox_bitcoincore = { git = "https://github.com/rust-bitcoin-tools/testcontainers-rs.git", branch = "zmq_support" }
testcontainers = { git = "https://github.com/rust-bitcoin-tools/testcontainers-rs.git", branch = "zmq_support" }
41 changes: 22 additions & 19 deletions wallet/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
//!
use bitcoin::{
util::{
bip32::{ExtendedPubKey, ExtendedPrivKey,ChildNumber},
bip32::{ExtendedPubKey, ExtendedPrivKey, ChildNumber},
address::Address,
},
blockdata::{
script::Script,
transaction::OutPoint,
},
network::constants::Network,
PrivateKey,
PublicKey
};
use secp256k1::{Secp256k1, SecretKey, PublicKey};
use db::DB;
use secp256k1::Secp256k1;
use serde::{Serialize, Deserialize};

use super::DB;

use std::{
sync::{Arc, RwLock},
Expand Down Expand Up @@ -208,8 +212,8 @@ impl Account {
}
}

pub fn get_sk(&self, key_path: &KeyPath) -> SecretKey {
let path: &[ChildNumber] = &[
pub fn get_sk(&self, key_path: &KeyPath) -> PrivateKey {
let path = &[
ChildNumber::Normal {
index: key_path.addr_chain.clone().into(),
}, // TODO(evg): use addr chain enum instead?
Expand All @@ -221,7 +225,7 @@ impl Account {
.account_key
.derive_priv(&Secp256k1::new(), path)
.unwrap();
extended_priv_key.secret_key
extended_priv_key.private_key
}

pub fn grab_utxo(&mut self, utxo: Utxo) {
Expand All @@ -234,7 +238,7 @@ impl Account {
}

pub fn next_external_pk(&mut self) -> Result<PublicKey, Box<Error>> {
let path: &[ChildNumber] = &[
let path = &[
ChildNumber::Normal { index: 0 }, // TODO(evg): use addr chain enum instead?
ChildNumber::Normal {
index: self.external_index,
Expand Down Expand Up @@ -262,7 +266,7 @@ impl Account {
}

pub fn next_internal_pk(&mut self) -> Result<PublicKey, Box<Error>> {
let path: &[ChildNumber] = &[
let path = &[
ChildNumber::Normal { index: 1 },
ChildNumber::Normal {
index: self.internal_index,
Expand Down Expand Up @@ -362,15 +366,14 @@ pub fn p2wkh_script_from_public_key(pk: &PublicKey, network: Network) -> Script
mod test {
use bitcoin::{
network::constants::Network,
util::hash::Sha256dHash,
Block, Transaction,
};
use hex;
};
use bitcoin_hashes::sha256d::Hash as Sha256dHash;

use walletlibrary::{WalletConfigBuilder, WalletLibraryMode, KeyGenConfig};
use default::WalletWithTrustedFullNode;
use account::AccountAddressType;
use interface::BlockChainIO;
use crate::walletlibrary::{WalletConfigBuilder, WalletLibraryMode, KeyGenConfig};
use crate::default::WalletWithTrustedFullNode;
use crate::account::AccountAddressType;
use crate::interface::BlockChainIO;

struct FakeBlockChainIO;

Expand Down Expand Up @@ -427,12 +430,12 @@ mod test {

for expected_pk in get_external_pk_vec() {
let pk = account.next_external_pk().unwrap();
assert_eq!(hex::encode(&pk.serialize()[..]), expected_pk);
assert_eq!(hex::encode(&pk.key.serialize()[..]), expected_pk);
}

for expected_pk in get_internal_pk_vec() {
let pk = account.next_internal_pk().unwrap();
assert_eq!(hex::encode(&pk.serialize()[..]), expected_pk);
assert_eq!(hex::encode(&pk.key.serialize()[..]), expected_pk);
}
}

Expand Down Expand Up @@ -468,12 +471,12 @@ mod test {

for expected_pk in external_pk_vec {
let pk = account.next_external_pk().unwrap();
assert_eq!(hex::encode(&pk.serialize()[..]), expected_pk);
assert_eq!(hex::encode(&pk.key.serialize()[..]), expected_pk);
}

for expected_pk in internal_pk_vec {
let pk = account.next_internal_pk().unwrap();
assert_eq!(hex::encode(&pk.serialize()[..]), expected_pk);
assert_eq!(hex::encode(&pk.key.serialize()[..]), expected_pk);
}
}
}
16 changes: 9 additions & 7 deletions wallet/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use bitcoin::OutPoint;
use secp256k1::{Secp256k1, PublicKey};
use bitcoin::util::key::PublicKey;
use rocksdb::{DB as RocksDB, ColumnFamilyDescriptor, Options, IteratorMode};
use byteorder::{ByteOrder, BigEndian};
use serde_json;

use std::collections::HashMap;

use account::{Utxo, SecretKeyHelper, AccountAddressType};
use walletlibrary::{LockId, LockGroup};
use super::account::{Utxo, SecretKeyHelper, AccountAddressType};
use super::walletlibrary::{LockId, LockGroup};

static BIP39_RANDOMNESS: &'static [u8] = b"bip39_randomness";
static LAST_SEEN_BLOCK_HEIGHT: &'static [u8] = b"lsbh";
Expand Down Expand Up @@ -126,8 +126,9 @@ impl DB {
let mut vec = Vec::new();
for (key, val) in db_iterator {
let key_helper: SecretKeyHelper = serde_json::from_slice(&key).unwrap();
let pk: Vec<u8> = serde_json::from_slice(&val).unwrap();
let pk = PublicKey::from_slice(&Secp256k1::new(), pk.as_slice()).unwrap();
let pk: String = serde_json::from_slice(&val).unwrap();
let pk = hex::decode(&pk).unwrap();
let pk = PublicKey::from_slice(pk.as_slice()).unwrap();
vec.push((key_helper, pk));
}
vec
Expand All @@ -140,8 +141,9 @@ impl DB {
let mut vec = Vec::new();
for (key, val) in db_iterator {
let key_helper: SecretKeyHelper = serde_json::from_slice(&key).unwrap();
let pk: Vec<u8> = serde_json::from_slice(&val).unwrap();
let pk = PublicKey::from_slice(&Secp256k1::new(), pk.as_slice()).unwrap();
let pk: String = serde_json::from_slice(&val).unwrap();
let pk = hex::decode(&pk).unwrap();
let pk = PublicKey::from_slice(pk.as_slice()).unwrap();
vec.push((key_helper, pk));
}
vec
Expand Down
8 changes: 4 additions & 4 deletions wallet/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use bitcoin::{Block, Transaction, OutPoint};

use std::error::Error;

use walletlibrary::{WalletLibrary, WalletConfig, LockId, WalletLibraryMode};
use interface::{BlockChainIO, WalletLibraryInterface, Wallet};
use error::WalletError;
use mnemonic::Mnemonic;
use super::walletlibrary::{WalletLibrary, WalletConfig, LockId, WalletLibraryMode};
use super::interface::{BlockChainIO, WalletLibraryInterface, Wallet};
use super::error::WalletError;
use super::mnemonic::Mnemonic;

// a factory for TREZOR (BIP44) compatible accounts
pub struct WalletWithTrustedFullNode {
Expand Down
12 changes: 6 additions & 6 deletions wallet/src/electrumx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
use bitcoin::{
Transaction, OutPoint,
network::serialize::{serialize_hex, deserialize},
consensus::encode::{serialize_hex, deserialize},
};
use hex;

Expand All @@ -27,10 +27,10 @@ use electrumx_client::{
electrumx_client::ElectrumxClient,
interface::Electrumx,
};
use walletlibrary::{WalletLibrary, WalletConfig, LockId, WalletLibraryMode};
use interface::{WalletLibraryInterface, Wallet};
use error::WalletError;
use mnemonic::Mnemonic;
use super::walletlibrary::{WalletLibrary, WalletConfig, LockId, WalletLibraryMode};
use super::interface::{WalletLibraryInterface, Wallet};
use super::error::WalletError;
use super::mnemonic::Mnemonic;

pub struct ElectrumxWallet {
pub wallet_lib: Box<WalletLibraryInterface + Send>,
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Wallet for ElectrumxWallet {
}

fn publish_tx(&mut self, tx: &Transaction) {
let tx = serialize_hex(tx).unwrap();
let tx = serialize_hex(tx);
self.electrumx_client.broadcast_transaction(tx).unwrap();
}

Expand Down
6 changes: 3 additions & 3 deletions wallet/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use bitcoin::{
util::hash::Sha256dHash,
Block, Transaction, OutPoint,
};
use account::{Account, AccountAddressType, Utxo};
use walletlibrary::LockId;
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use super::account::{Account, AccountAddressType, Utxo};
use super::walletlibrary::LockId;

use std::error::Error;

Expand Down
9 changes: 4 additions & 5 deletions wallet/src/keyfactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
use bitcoin::network::constants::Network;
use bitcoin::util::bip32::{ExtendedPubKey, ExtendedPrivKey,ChildNumber};
use secp256k1::Secp256k1;
use error::WalletError;
use super::error::WalletError;
use crypto::pbkdf2::pbkdf2;
use crypto::hmac::Hmac;
use crypto::sha2::Sha512;
use rand::{OsRng, RngCore};
use mnemonic::Mnemonic;
use rand::{rngs::OsRng, RngCore};
use super::mnemonic::Mnemonic;

/// a fabric of keys
pub struct KeyFactory;
Expand Down Expand Up @@ -81,7 +81,6 @@ impl KeyFactory {
seed: &Seed,
) -> Result<ExtendedPrivKey, WalletError> {
Ok(ExtendedPrivKey::new_master(
&Secp256k1::new(),
network,
&seed.0,
)?)
Expand Down Expand Up @@ -142,7 +141,7 @@ mod test {
use std::io::Read;
use bitcoin::network::constants::Network;
use bitcoin::util::bip32::ChildNumber;
use keyfactory::Seed;
use crate::keyfactory::Seed;

extern crate rustc_serialize;
extern crate hex;
Expand Down
20 changes: 3 additions & 17 deletions wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
extern crate crypto;
extern crate secp256k1;
extern crate bitcoin;
extern crate rand;
extern crate hex;
// extern crate bitcoin_rpc_client;
extern crate bitcoin_bech32;
extern crate rocksdb;
extern crate byteorder;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate log;
extern crate simple_logger;
extern crate electrumx_client;

pub mod mnemonic;
pub mod error;
Expand All @@ -37,5 +21,7 @@ pub mod walletlibrary;
pub mod default;
pub mod electrumx;
pub mod account;
mod db;
pub mod interface;

mod db;
use self::db::DB;
Loading

0 comments on commit df2e717

Please sign in to comment.