|
|
@@ -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)
|