Skip to content

Commit

Permalink
restore transaction net wallet balance
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Mar 15, 2021
1 parent 74d1978 commit 879bd86
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,6 @@ pub mod tests {
}

pub fn rnd_string() -> String {
thread_rng().sample_iter(&Alphanumeric).take(20).collect()
thread_rng().sample_iter(&Alphanumeric).take(10).collect()
}
}
4 changes: 4 additions & 0 deletions lib/src/entities/persisted.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// https://dreampuf.github.io/GraphvizOnline/#digraph%20G%20%7B%0A%20%20%22.firma%22%20-%3E%20%22%5Bnetwork%5D%22%0A%20%20%0A%20%20%22%5Bnetwork%5D%22%20-%3E%20wallets%0A%20%20%22%5Bnetwork%5D%22%20-%3E%20keys%0A%20%20%22%5Bnetwork%5D%22%20-%3E%20psbts%0A%20%20%22%5Bnetwork%5D%22%20-%3E%20%22daemon_opts%22%20%0A%20%20%0A%20%20keys%20-%3E%20%22%5Bkey%20name%5D%22%0A%20%20%22master_secret%22%20%5Bshape%3DSquare%5D%0A%20%20%22descriptor_public_key%22%20%5Bshape%3DSquare%5D%0A%20%20%22%5Bkey%20name%5D%22%20-%3E%20%22master_secret%22%20%0A%20%20%22%5Bkey%20name%5D%22%20-%3E%20%22descriptor_public_key%22%20%0A%20%20%0A%20%20wallets%20-%3E%20%22%5Bwallet%20name%5D%22%0A%20%20%22wallet%22%20%5Bshape%3DSquare%5D%0A%20%20%22wallet_indexes%22%20%5Bshape%3DSquare%5D%0A%20%20%22daemon_opts%22%20%5Bshape%3DSquare%5D%0A%20%20%22wallet_signature%22%20%5Bshape%3DSquare%5D%0A%20%20%22%5Bwallet%20name%5D%22%20-%3E%20%22wallet%22%20%0A%20%20%22%5Bwallet%20name%5D%22%20-%3E%20%22wallet_indexes%22%20%0A%20%20%22%5Bwallet%20name%5D%22%20-%3E%20%22wallet_signature%22%20%0A%20%20%0A%20%20psbts%20-%3E%20%22%5Bpsbt%20name%5D%22%0A%20%20%22psbt%22%20%5Bshape%3DSquare%5D%0A%20%20%22%5Bpsbt%20name%5D%22%20-%3E%20%22psbt%22%20%0A%7D

use crate::mnemonic::Mnemonic;
use crate::offline::descriptor::{parse_descriptor_with_checksum, ExtendedDescriptor};
use crate::offline::sign::get_psbt_name;
use crate::offline::sign_wallet::WALLET_SIGN_DERIVATION;
use crate::{
Expand Down Expand Up @@ -107,6 +108,9 @@ impl Wallet {
}
result
}
pub fn descriptor(&self) -> Result<ExtendedDescriptor> {
parse_descriptor_with_checksum(&self.descriptor)
}
}

impl DescriptorPublicKey {
Expand Down
23 changes: 13 additions & 10 deletions lib/src/offline/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use miniscript::{DescriptorTrait, TranslatePk2};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

pub type ExtendedDescriptor = miniscript::Descriptor<miniscript::DescriptorPublicKey>;

#[derive(Debug, Serialize, Deserialize)]
pub struct DeriveAddressOptions {
pub descriptor: String,
Expand All @@ -19,16 +21,19 @@ impl DeriveAddressOptions {
}
}

pub fn parse_descriptor_with_checksum(descriptor: &str) -> Result<ExtendedDescriptor> {
// checksum not supported at the moment, stripping out
// TODO check
let end = descriptor.find('#').unwrap_or_else(|| descriptor.len());
let descriptor: miniscript::Descriptor<miniscript::DescriptorPublicKey> =
descriptor[..end].parse()?;
Ok(descriptor)
}

/// derive address from descriptor in the form "wsh(multi({n},{x}/{c}/*,{y}/{c}/*,...))#5wstxmwd"
pub fn derive_address(network: Network, opt: &DeriveAddressOptions) -> Result<GetAddressOutput> {
opt.validate()?;
// checksum not supported at the moment, stripping out
let end = opt
.descriptor
.find('#')
.unwrap_or_else(|| opt.descriptor.len());
let descriptor: miniscript::Descriptor<miniscript::DescriptorPublicKey> =
opt.descriptor[..end].parse()?;
let descriptor = parse_descriptor_with_checksum(&opt.descriptor)?;

let secp = Secp256k1::verification_only();
//let context = DescriptorPublicKeyCtx::new(&secp, ChildNumber::from_normal_idx(opt.index)?);
Expand All @@ -37,9 +42,7 @@ pub fn derive_address(network: Network, opt: &DeriveAddressOptions) -> Result<Ge
.translate_pk2(|xpk| xpk.derive_public_key(&secp))
.unwrap()
.address(network)?;
/*let address = descriptor
.address(network, context)
.ok_or(Error::AddressFromDescriptorFails)?;*/

let path = DerivationPath::from_str(&format!("m/0/{}", opt.index))?;

Ok(GetAddressOutput {
Expand Down
24 changes: 22 additions & 2 deletions lib/src/offline/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ fn wallet_with_path(
for (_, (finger, path)) in hd_keypaths.iter() {
if wallet.fingerprints().contains(finger) {
let path_vec: Vec<ChildNumber> = path.clone().into();
if let ChildNumber::Normal { index } = path_vec.first()? {
let len = path_vec.len();
if let ChildNumber::Normal { index } = path_vec.get(len - 2)? {
let descriptor = match index {
0 => &wallet.descriptor,
_ => return None,
Expand All @@ -259,7 +260,9 @@ fn wallet_with_path(

#[cfg(test)]
mod tests {
use crate::offline::print::{biggest_dividing_pow, script_type};
use crate::offline::print::{biggest_dividing_pow, pretty_print, script_type};
use crate::{psbt_from_base64, Wallet};
use bitcoin::Network;

#[test]
fn test_biggest_dividing_pow() {
Expand Down Expand Up @@ -291,4 +294,21 @@ mod tests {
let s = hex_script!("00201775ead41acefa14d2d534d6272da610cc35855d0de4cab0f5c1a3f894921989");
assert_eq!(script_type(&s), Some(4usize));
}

#[test]
fn test_pretty_print() {
let (_, to_carol_psbt) = psbt_from_base64("cHNidP8BAH4CAAAAAQQYGYyRDjWA/D08BEjU3Q9P34Sv8q0mW9UV5niEqBZ4AQAAAAD+////AiDLAAAAAAAAF6kUaV+OwCj7iV87pOHOFXNLuZMc7tyHBwIAAAAAAAAiACAGYNwSo/z0dYfDuCUPL2Li/SSY10gjxu8hZ9pREpEaCwAAAAAM/AVma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V95rv9mb7W7jAPMTohyEYpbhVS8FbmTsuQsFRxDWPJX2ZFEeRPMFz3R1gh/0/*))#szg2xsau", Network::Testnet);
let name = wallet.id.name.clone();

let result = pretty_print(&to_carol_psbt, Network::Testnet, &[wallet]).unwrap();
assert_eq!(format!("{}: -0.00052381 BTC", name), result.balances);

assert_eq!("Privacy: outputs have different script types https://en.bitcoin.it/wiki/Privacy#Sending_to_a_different_script_type", result.info[0]);
assert_eq!("Privacy: outputs have different precision https://en.bitcoin.it/wiki/Privacy#Round_numbers", result.info[1]);

assert_eq!(result.fee.absolute, 381);

dbg!(result);
}
}
7 changes: 7 additions & 0 deletions lib/src/online/create_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ mod tests {
}

impl Wallet {
pub fn new(descriptor: &str, network: Network) -> Self {
Wallet {
id: Identifier::new(network, Kind::Wallet, &rnd_string()),
descriptor: descriptor.to_string(),
created_at_height: 0,
}
}
pub fn new_random(required_sig: u8, keys: &[MasterSecret]) -> Self {
let desc_pub_keys: Vec<_> = keys
.iter()
Expand Down

0 comments on commit 879bd86

Please sign in to comment.