Move btc mod from cashier

This commit is contained in:
Janus
2021-07-29 15:11:54 -05:00
parent a0a27e0d3b
commit 0816bada60
5 changed files with 88 additions and 70 deletions

View File

@@ -4,7 +4,6 @@ use std::sync::Arc;
use std::{path::Path, path::PathBuf};
//use toml;
use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
use drk::cli::{CashierdCli, CashierdConfig, Config};
use drk::service::CashierService;
use drk::wallet::{CashierDb, CashierDbPtr};

View File

@@ -1,9 +1,10 @@
use crate::blockchain::{rocks::columns, RocksColumn};
use crate::service::cashier::CashierClient;
use crate::service::btc::PubAddress;
use crate::wallet::WalletDb;
use crate::{Error, Result};
use async_std::sync::Arc;
use bitcoin::util::address::Address;
use log::*;
use std::net::SocketAddr;
//use std::sync::Arc;
@@ -79,7 +80,7 @@ impl RpcAdapter {
Ok(())
}
pub async fn deposit(&mut self) -> Result<Address> {
pub async fn deposit(&mut self) -> Result<PubAddress> {
debug!(target: "deposit", "deposit: START");
let (public, private) = self.wallet.key_gen();
self.wallet.put_keypair(public, private)?;

74
src/service/btc.rs Normal file
View File

@@ -0,0 +1,74 @@
use crate::{serial::deserialize, serial::serialize, Error, Result};
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use secp256k1::key::SecretKey;
use bitcoin::util::ecdsa::{PrivateKey, PublicKey};
use bitcoin::util::address::Address;
use bitcoin::network::constants::Network;
use bitcoin::hash_types::PubkeyHash;
pub type PubAddress = Address;
pub struct BitcoinKeys {
secret_key: SecretKey,
bitcoin_private_key: PrivateKey,
pub bitcoin_public_key: PublicKey,
pub pub_address: Address,
}
impl BitcoinKeys {
pub fn new(
) -> Result<BitcoinKeys> {
let context = secp256k1::Secp256k1::new();
// Probably not good enough for release
let rand: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect();
let rand_hex = hex::encode(rand);
// Generate simple byte array from rand
let data_slice: &[u8] = rand_hex.as_bytes();
let secret_key = SecretKey::from_slice(&hex::decode(data_slice).unwrap()).unwrap();
// Use Testnet
let bitcoin_private_key = PrivateKey::new(secret_key, Network::Testnet);
let bitcoin_public_key = PublicKey::from_private_key(&context, &bitcoin_private_key);
//let pubkey_serialized = bitcoin_public_key.to_bytes();
let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Testnet);
Ok(Self {
secret_key,
bitcoin_private_key,
bitcoin_public_key,
pub_address,
})
}
// This should do a db lookup to return the same obj
pub fn address_from_slice(key: &[u8]) -> Result<Address> {
let pub_key = PublicKey::from_slice(key).unwrap();
let address = Address::p2pkh(&pub_key, Network::Testnet);
Ok(address)
}
pub fn get_deposit_address(&self) -> Result<&Address> {
Ok(&self.pub_address)
}
pub fn get_pubkey(&self) -> &PublicKey {
&self.bitcoin_public_key
}
}

View File

@@ -1,16 +1,8 @@
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use secp256k1::key::SecretKey;
use bitcoin::util::ecdsa::{PrivateKey, PublicKey as BitcoinPubKey};
use bitcoin::util::address::Address;
use bitcoin::network::constants::Network;
use bitcoin::hash_types::PubkeyHash;
use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
use crate::{serial::deserialize, serial::serialize, Error, Result};
use super::btc::{BitcoinKeys, PubAddress};
use crate::{serial::deserialize, serial::serialize, Error, Result};
use crate::wallet::{CashierDb, CashierDbPtr};
use std::net::SocketAddr;
@@ -30,58 +22,6 @@ enum CashierCommand {
GetBTC,
}
// Move to bitcoin.rs
pub struct BitcoinKeys {
secret_key: SecretKey,
bitcoin_private_key: PrivateKey,
pub bitcoin_public_key: BitcoinPubKey,
pub pub_address: Address,
}
impl BitcoinKeys {
pub fn new(
) -> Result<BitcoinKeys> {
let context = secp256k1::Secp256k1::new();
// Probably not good enough for release
let rand: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(32)
.map(char::from)
.collect();
let rand_hex = hex::encode(rand);
// Generate simple byte array from rand
let data_slice: &[u8] = rand_hex.as_bytes();
let secret_key = SecretKey::from_slice(&hex::decode(data_slice).unwrap()).unwrap();
// Use Testnet
let bitcoin_private_key = PrivateKey::new(secret_key, Network::Testnet);
let bitcoin_public_key = BitcoinPubKey::from_private_key(&context, &bitcoin_private_key);
//let pubkey_serialized = bitcoin_public_key.to_bytes();
let pub_address = Address::p2pkh(&bitcoin_public_key, Network::Testnet);
Ok(Self {
secret_key,
bitcoin_private_key,
bitcoin_public_key,
pub_address,
})
}
pub fn get_deposit_address(&self) -> Result<&Address> {
Ok(&self.pub_address)
}
pub fn get_pubkey(&self) -> &BitcoinPubKey {
&self.bitcoin_public_key
}
}
pub struct CashierService {
addr: SocketAddr,
wallet: Arc<CashierDb>,
@@ -202,7 +142,7 @@ impl CashierClient {
Ok(())
}
pub async fn get_address(&mut self, index: jubjub::SubgroupPoint) -> Result<Option<Address>> {
pub async fn get_address(&mut self, index: jubjub::SubgroupPoint) -> Result<Option<PubAddress>> {
let handle_error = Arc::new(handle_error);
let rep = self
.protocol
@@ -214,9 +154,9 @@ impl CashierClient {
.await?;
if let Some(key) = rep {
let pubkey = BitcoinPubKey::from_slice(&key).unwrap();
let address: Address = Address::p2pkh(&pubkey, Network::Testnet);
//let pubkey = BitcoinPubKey::from_slice(&key).unwrap();
//let address: Address = Address::p2pkh(&pubkey, Network::Testnet);
let address = BitcoinKeys::address_from_slice(&key).unwrap();
return Ok(Some(address));
}
Ok(None)

View File

@@ -2,6 +2,10 @@ pub mod cashier;
pub mod gateway;
pub mod reqrep;
pub mod btc;
pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
pub use cashier::{BitcoinKeys, CashierClient, CashierService};
pub use cashier::{CashierClient, CashierService};
pub use btc::{BitcoinKeys, PubAddress};