Skip to content

Commit

Permalink
ledger: Rename address functions with credential.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jun 13, 2024
1 parent a171498 commit 103d4af
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl RpcApi for Client {
) -> bitcoincore_rpc::Result<Address<bitcoin::address::NetworkUnchecked>> {
Ok(self
.ledger
.generate_address()
.generate_credential()
.address
.as_unchecked()
.to_owned())
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -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
);

Expand All @@ -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
);

Expand Down
16 changes: 7 additions & 9 deletions src/ledger/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -37,23 +37,21 @@ impl Ledger {
credentials
}
/// Returns secret/public key + address list of the user.
pub fn _get_address(&self) -> Vec<UserCredential> {
pub fn _get_credentials(&self) -> Vec<UserCredential> {
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,
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 103d4af

Please sign in to comment.