Merge rust-bitcoin/rust-bitcoin#1658: Changing docs and examples to use correct function signature

f69363d71a adding suggested documentation for path arg (Lorenzo Maturano)
673ca2d2fe changing docs and examples to use reference to slice in `derive_pub` (Lorenzo Maturano)

Pull request description:

  `P: AsRef<[ChildNumber]>` means that path can be borrowed as a reference to a slice of `ChildNumber`.

  While it does work with `Vec`, the documentation and examples mislead the user into thinking that he/she has to use a vec, which is not true. A simple reference to a slice of `ChildNumber` also works and does not consume heap

ACKs for top commit:
  tcharding:
    ACK f69363d71a
  Kixunil:
    ACK f69363d71a

Tree-SHA512: 9f0a2764c676ecaa45de69663c83c3008ee01f534c324ceca627c69d689d6ca6d0a3dda9361eaa402bacc68975e998840b78a3856e0572db3de10325b354ac6a
This commit is contained in:
Andrew Poelstra 2023-02-22 14:59:44 +00:00
commit 97c925ab36
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 3 additions and 3 deletions

View File

@ -52,7 +52,7 @@ fn main() {
// generate first receiving address at m/0/0
// manually creating indexes this time
let zero = ChildNumber::from_normal_idx(0).unwrap();
let public_key = xpub.derive_pub(&secp, &vec![zero, zero]).unwrap().public_key;
let public_key = xpub.derive_pub(&secp, &[zero, zero]).unwrap().public_key;
let address = Address::p2wpkh(&PublicKey::new(public_key), network).unwrap();
println!("First receiving address: {}", address);
}

View File

@ -265,7 +265,7 @@ impl WatchOnly {
&self,
secp: &Secp256k1<C>,
) -> Result<(PublicKey, Address, DerivationPath)> {
let path = vec![ChildNumber::from_normal_idx(1)?, ChildNumber::from_normal_idx(0)?];
let path = [ChildNumber::from_normal_idx(1)?, ChildNumber::from_normal_idx(0)?];
let derived = self.account_0_xpub.derive_pub(secp, &path)?;
let pk = derived.to_pub();

View File

@ -683,7 +683,7 @@ impl ExtendedPubKey {
/// Attempts to derive an extended public key from a path.
///
/// The `path` argument can be both of type `DerivationPath` or `Vec<ChildNumber>`.
/// The `path` argument can be any type implementing `AsRef<ChildNumber>`, such as `DerivationPath`, for instance.
pub fn derive_pub<C: secp256k1::Verification, P: AsRef<[ChildNumber]>>(
&self,
secp: &Secp256k1<C>,