Skip to content

Commit

Permalink
ledger: Rename UserAddress to UserCredential.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Jun 13, 2024
1 parent ccb2135 commit a171498
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
54 changes: 32 additions & 22 deletions src/ledger/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use secp256k1::{rand, Keypair, PublicKey, Secp256k1, SecretKey};

/// User's keys and generated address.
#[derive(Clone, Debug)]
pub struct UserAddress {
pub struct UserCredential {
pub secret_key: SecretKey,
pub public_key: PublicKey,
pub x_only_public_key: XOnlyPublicKey,
Expand All @@ -24,34 +24,41 @@ impl Ledger {
public_key: PublicKey,
x_only_public_key: XOnlyPublicKey,
address: Address,
) -> UserAddress {
let addresses = UserAddress {
) -> UserCredential {
let credentials = UserCredential {
secret_key,
public_key,
x_only_public_key,
address,
};

add_item!(self.addresses, addresses.clone());
add_item!(self.credentials, credentials.clone());

addresses
credentials
}
/// Returns secret/public key + address list of the user.
pub fn _get_address(&self) -> Vec<UserAddress> {
get_item!(self.addresses);
pub fn _get_address(&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) -> UserAddress {
pub fn generate_address(&self) -> UserCredential {
let secp = Secp256k1::new();
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
let (x_only_public_key, _parity) =
XOnlyPublicKey::from_keypair(&Keypair::from_secret_key(&secp, &secret_key));
// 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(secret_key, public_key, x_only_public_key, address)
self.add_address(
keypair.secret_key(),
keypair.public_key(),
keypair.x_only_public_key().0,
address,
)
}
}

Expand All @@ -63,24 +70,27 @@ mod tests {
#[test]
fn addresses() {
let ledger = Ledger::new();
assert_eq!(ledger.addresses.take().len(), 0);
assert_eq!(ledger.credentials.take().len(), 0);

ledger.generate_address();
let addresses = ledger.addresses.take();
assert_eq!(addresses.len(), 1);
let credentials = ledger.credentials.take();
assert_eq!(credentials.len(), 1);

let address = addresses.get(0).unwrap().to_owned();
let credential = credentials.get(0).unwrap().to_owned();

assert_eq!(address.address.address_type().unwrap(), AddressType::P2tr);
assert!(address
assert_eq!(
credential.address.address_type().unwrap(),
AddressType::P2tr
);
assert!(credential
.address
.as_unchecked()
.is_valid_for_network(bitcoin::Network::Regtest));
// assert!(address
// assert!(credential
// .address
// .is_related_to_pubkey(&address.public_key.into()));
// assert!(address
// .is_related_to_xonly_pubkey(&credential.x_only_public_key));
// assert!(credential
// .address
// .is_related_to_xonly_pubkey(&address.x_only_public_key));
// .is_related_to_pubkey(&credential.public_key.into()));
}
}
6 changes: 3 additions & 3 deletions src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! This crate is designed to be used as immutable, because of the `RpcApi`'s
//! immutable nature.
use address::UserAddress;
use address::UserCredential;
use bitcoin::{Transaction, TxOut};
use bitcoin_simulator::database::Database;
use std::{
Expand All @@ -25,7 +25,7 @@ pub struct Ledger {
/// to use this mock in an asynchronous environment, like `async` or threads.
database: Arc<Mutex<Database>>,
/// User's keys and address.
addresses: Cell<Vec<UserAddress>>,
credentials: Cell<Vec<UserCredential>>,
/// User's unspent transaction outputs.
utxos: Cell<Vec<TxOut>>,
/// User's transactions.
Expand All @@ -42,7 +42,7 @@ impl Ledger {
pub fn new() -> Self {
Self {
database: Arc::new(Mutex::new(Database::connect_temporary_database().unwrap())),
addresses: Cell::new(Vec::new()),
credentials: Cell::new(Vec::new()),
utxos: Cell::new(Vec::new()),
transactions: Cell::new(Vec::new()),
}
Expand Down

0 comments on commit a171498

Please sign in to comment.