From 103d4af005a94baa37ae8f706cffe76e3e7c06d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 13 Jun 2024 17:07:16 +0300 Subject: [PATCH] ledger: Rename address functions with credential. --- src/client/rpc_api.rs | 14 +++++++------- src/ledger/address.rs | 16 +++++++--------- src/ledger/transactions.rs | 4 ++-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/client/rpc_api.rs b/src/client/rpc_api.rs index b9076cd..c6ce570 100644 --- a/src/client/rpc_api.rs +++ b/src/client/rpc_api.rs @@ -149,7 +149,7 @@ impl RpcApi for Client { ) -> bitcoincore_rpc::Result> { Ok(self .ledger - .generate_address() + .generate_credential() .address .as_unchecked() .to_owned()) @@ -197,7 +197,7 @@ mod tests { let rpc = Client::new("", bitcoincore_rpc::Auth::None).unwrap(); // Create and address for the user. - let address = rpc.ledger.generate_address().address; + let address = rpc.ledger.generate_credential().address; // First, add some funds to user, for free. let txout = test_common::create_txout(100_000_000, None); @@ -213,7 +213,7 @@ mod tests { let txin = test_common::create_txin(inserted_tx1.compute_txid()); let txout = TxOut { value: Amount::from_sat(0x45), - script_pubkey: rpc.ledger.generate_address().address.script_pubkey(), + script_pubkey: rpc.ledger.generate_credential().address.script_pubkey(), }; let inserted_tx2 = test_common::create_transaction(vec![txin], vec![txout]); rpc.send_raw_transaction(&inserted_tx2).unwrap(); @@ -249,7 +249,7 @@ mod tests { }; let txout = TxOut { value: Amount::from_sat(0x1F), - script_pubkey: rpc.ledger.generate_address().address.script_pubkey(), + script_pubkey: rpc.ledger.generate_credential().address.script_pubkey(), }; let inserted_tx = test_common::create_transaction(vec![txin], vec![txout]); rpc.send_raw_transaction(&inserted_tx).unwrap(); @@ -266,7 +266,7 @@ mod tests { fn send_to_address() { let rpc = Client::new("", bitcoincore_rpc::Auth::None).unwrap(); - let address = rpc.ledger.generate_address().address; + let address = rpc.ledger.generate_credential().address; let txid = rpc .send_to_address( @@ -296,7 +296,7 @@ mod tests { assert!(!address.is_valid_for_network(Network::Signet)); assert!(!address.is_valid_for_network(Network::Bitcoin)); assert_eq!( - *rpc.ledger._get_address()[0].address.as_unchecked(), + *rpc.ledger._get_credentials()[0].address.as_unchecked(), address ); @@ -311,7 +311,7 @@ mod tests { assert!(!curr.is_valid_for_network(Network::Signet)); assert!(!curr.is_valid_for_network(Network::Bitcoin)); assert_eq!( - *rpc.ledger._get_address()[i + 1].address.as_unchecked(), + *rpc.ledger._get_credentials()[i + 1].address.as_unchecked(), curr ); diff --git a/src/ledger/address.rs b/src/ledger/address.rs index fb4f3ab..5cd97ec 100644 --- a/src/ledger/address.rs +++ b/src/ledger/address.rs @@ -18,7 +18,7 @@ pub struct UserCredential { impl Ledger { /// Adds a new secret/public key + address for the user. - pub fn add_address( + pub fn add_credential( &self, secret_key: SecretKey, public_key: PublicKey, @@ -37,23 +37,21 @@ impl Ledger { credentials } /// Returns secret/public key + address list of the user. - pub fn _get_address(&self) -> Vec { + pub fn _get_credentials(&self) -> Vec { get_item!(self.credentials); } - /// Generates a random secret/public key pair and creates a new address from - /// them. - pub fn generate_address(&self) -> UserCredential { + /// Generates a random secret/public key pair and creates a new Bicoin + /// address from them. + pub fn generate_credential(&self) -> UserCredential { let secp = Secp256k1::new(); - // let secret_key = PrivateKey::generate(Network::Regtest); - // let public_key = PublicKey::from_private_key(&secp, &secret_key); let (secret_key, _public_key) = secp.generate_keypair(&mut rand::thread_rng()); let keypair = Keypair::from_secret_key(&secp, &secret_key); let (x_only_public_key, _parity) = XOnlyPublicKey::from_keypair(&keypair); let address = Address::p2tr(&secp, x_only_public_key, None, Network::Regtest); - self.add_address( + self.add_credential( keypair.secret_key(), keypair.public_key(), keypair.x_only_public_key().0, @@ -72,7 +70,7 @@ mod tests { let ledger = Ledger::new(); assert_eq!(ledger.credentials.take().len(), 0); - ledger.generate_address(); + ledger.generate_credential(); let credentials = ledger.credentials.take(); assert_eq!(credentials.len(), 1); diff --git a/src/ledger/transactions.rs b/src/ledger/transactions.rs index b818376..9cf37c7 100644 --- a/src/ledger/transactions.rs +++ b/src/ledger/transactions.rs @@ -73,12 +73,12 @@ mod tests { assert_eq!(ledger._get_utxos().len(), 0); // Generate a random address. - ledger.generate_address(); + ledger.generate_credential(); // Insert a dummy UTXO. let utxo = TxOut { value: Amount::from_sat(0x45), - script_pubkey: ledger._get_address()[0].address.script_pubkey(), + script_pubkey: ledger._get_credentials()[0].address.script_pubkey(), }; ledger.add_utxo(utxo);