]> Untitled Git - bdk/commitdiff
Merge bitcoindevkit/bdk#471: moving the function wallet_name_from_descriptor from...
authorSteve Myers <steve@notmandatory.org>
Thu, 25 Nov 2021 04:44:27 +0000 (20:44 -0800)
committerSteve Myers <steve@notmandatory.org>
Thu, 25 Nov 2021 04:44:58 +0000 (20:44 -0800)
2fc81141806ace8f12e5a019c0866f16fa8a02dc moving the function wallet_name_from_descriptor from blockchain/rpc.rs to wallet/mod.rs as it can be useful not only for rpc (Richard Ulrich)

Pull request description:

  ### Description

  Moving the function wallet_name_from_descriptor from rpc.rs to mod.rs
  Since the local cache for compact filters should be separate per wallet, this function can be useful not only for rpc.

  ### Notes to the reviewers

  I thought about renaming it, but waited for opinions on that.

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

ACKs for top commit:
  notmandatory:
    re-ACK  2fc8114

Tree-SHA512: d5732e74f7a54f54dde39fff77f94f12c611a419bed9683025ecf7be95cde330209f676dfc9346ebcd29194325589710eafdd1d533e8073d0662cb397577119f

1  2 
src/wallet/mod.rs

index 832626e3102b0bed1c797d018b32deac87b55109,a89f53f77eabd7ce4ccdc9cef8b3964511482e01..20de75683480a11ad7d76218e4884f2b64bd894c
@@@ -3994,15 -3994,33 +3994,44 @@@ pub(crate) mod test 
              }
          );
      }
 +
 +    #[test]
 +    fn test_sending_to_bip350_bech32m_address() {
 +        let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
 +        let addr =
 +            Address::from_str("tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c")
 +                .unwrap();
 +        let mut builder = wallet.build_tx();
 +        builder.add_recipient(addr.script_pubkey(), 45_000);
 +        builder.finish().unwrap();
 +    }
  }
+ /// Deterministically generate a unique name given the descriptors defining the wallet
+ pub fn wallet_name_from_descriptor<T>(
+     descriptor: T,
+     change_descriptor: Option<T>,
+     network: Network,
+     secp: &SecpCtx,
+ ) -> Result<String, Error>
+ where
+     T: IntoWalletDescriptor,
+ {
+     //TODO check descriptors contains only public keys
+     let descriptor = descriptor
+         .into_wallet_descriptor(secp, network)?
+         .0
+         .to_string();
+     let mut wallet_name = get_checksum(&descriptor[..descriptor.find('#').unwrap()])?;
+     if let Some(change_descriptor) = change_descriptor {
+         let change_descriptor = change_descriptor
+             .into_wallet_descriptor(secp, network)?
+             .0
+             .to_string();
+         wallet_name.push_str(
+             get_checksum(&change_descriptor[..change_descriptor.find('#').unwrap()])?.as_str(),
+         );
+     }
+     Ok(wallet_name)
+ }