Skip to main content

kaspa_bip32/
lib.rs

1#![allow(clippy::arithmetic_side_effects)]
2use zeroize::Zeroizing;
3
4pub use secp256k1;
5pub use secp256k1::SecretKey;
6
7mod private_key;
8mod public_key;
9mod xkey;
10mod xprivate_key;
11mod xpublic_key;
12
13mod address_type;
14mod attrs;
15mod child_number;
16mod derivation_path;
17mod error;
18mod mnemonic;
19mod prefix;
20mod result;
21pub mod types;
22
23pub mod wasm {
24    //! WASM bindings for the `bip32` module.
25    pub use crate::mnemonic::{Language, Mnemonic, WordCount};
26}
27
28pub use address_type::AddressType;
29pub use attrs::ExtendedKeyAttrs;
30pub use child_number::ChildNumber;
31pub use derivation_path::DerivationPath;
32pub use mnemonic::{Language, Mnemonic, WordCount};
33pub use prefix::Prefix;
34pub use private_key::PrivateKey;
35pub use public_key::PublicKey;
36pub use types::*;
37pub use xkey::ExtendedKey;
38pub use xprivate_key::ExtendedPrivateKey;
39pub use xpublic_key::ExtendedPublicKey;
40
41/// Extension for [`secp256k1::SecretKey`] that provides access
42/// to [`secp256k1::PublicKey`] and the public key string representation.
43pub trait SecretKeyExt {
44    fn get_public_key(&self) -> secp256k1::PublicKey;
45    fn as_str(&self, attrs: ExtendedKeyAttrs, prefix: Prefix) -> Zeroizing<String>;
46}
47
48impl SecretKeyExt for secp256k1::SecretKey {
49    fn get_public_key(&self) -> secp256k1::PublicKey {
50        secp256k1::PublicKey::from_secret_key_global(self)
51    }
52    fn as_str(&self, attrs: ExtendedKeyAttrs, prefix: Prefix) -> Zeroizing<String> {
53        // Add leading `0` byte
54        let mut key_bytes = [0u8; KEY_SIZE + 1];
55        key_bytes[1..].copy_from_slice(&self.to_bytes());
56
57        let key = ExtendedKey { prefix, attrs, key_bytes };
58
59        Zeroizing::new(key.to_string())
60    }
61}