Skip to content

Commit

Permalink
Add get_new_change_address()
Browse files Browse the repository at this point in the history
A lot of wallets treat change addresses vs external addresses
differently. Being able to specificy what the address is being used for
lets the wallet generate the correct kind.
  • Loading branch information
benthecarman committed Dec 8, 2023
1 parent 6b5d0da commit 2079433
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ impl Wallet for BitcoinCoreProvider {
.map_err(rpc_err_to_manager_err)
}

fn get_new_change_address(&self) -> Result<Address, ManagerError> {
self.get_new_address()
}

fn get_new_secret_key(&self) -> Result<SecretKey, ManagerError> {
let sk = SecretKey::new(&mut thread_rng());
let network = self.get_network()?;
Expand Down
2 changes: 2 additions & 0 deletions dlc-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub trait Signer {
pub trait Wallet: Signer {
/// Returns a new (unused) address.
fn get_new_address(&self) -> Result<Address, Error>;
/// Returns a new (unused) change address.
fn get_new_change_address(&self) -> Result<Address, Error>;
/// Generate a new secret key and store it in the wallet so that it can later
/// be retrieved.
fn get_new_secret_key(&self) -> Result<SecretKey, Error>;
Expand Down
2 changes: 1 addition & 1 deletion dlc-manager/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
let payout_addr = wallet.get_new_address()?;
let payout_spk = payout_addr.script_pubkey();
let payout_serial_id = get_new_serial_id();
let change_addr = wallet.get_new_address()?;
let change_addr = wallet.get_new_change_address()?;
let change_spk = change_addr.script_pubkey();
let change_serial_id = get_new_serial_id();

Expand Down
4 changes: 4 additions & 0 deletions mocks/src/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Wallet for MockWallet {
Ok(get_address())
}

fn get_new_change_address(&self) -> Result<Address, dlc_manager::error::Error> {
Ok(get_address())
}

fn get_new_secret_key(&self) -> Result<SecretKey, dlc_manager::error::Error> {
Ok(get_secret_key())
}
Expand Down
4 changes: 4 additions & 0 deletions simple-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ where
Ok(address)
}

fn get_new_change_address(&self) -> Result<Address> {
self.get_new_address()
}

fn get_new_secret_key(&self) -> Result<SecretKey> {
let seckey = SecretKey::new(&mut thread_rng());
let pubkey = PublicKey::from_secret_key(&self.secp_ctx, &seckey);
Expand Down

0 comments on commit 2079433

Please sign in to comment.