rusnap_api/
bip32.rs

1use rusnap_utils::bytes;
2use serde::{Deserialize, Serialize};
3
4use crate::Result;
5
6use super::request;
7
8/// Curve for Bip32
9#[derive(Debug, Serialize, Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum Curve {
12    /// Ed25519 Curve
13    Ed25519,
14    /// Secp256k1 Curve
15    Secp256k1,
16}
17
18#[derive(Debug, Serialize, Deserialize)]
19struct Bip32Params {
20    path: Vec<String>,
21    curve: Curve,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    compressed: Option<bool>,
25}
26
27/// Entropy for Bip32
28#[derive(Debug, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct Bip32Entropy {
31    #[serde(with = "bytes")]
32    pub chain_code: Vec<u8>,
33    pub curve: Curve,
34    pub depth: u32,
35    pub index: u64,
36    pub master_fingerprint: u64,
37    pub parent_fingerprint: u64,
38    #[serde(with = "bytes")]
39    pub private_key: Vec<u8>,
40    #[serde(with = "bytes")]
41    pub public_key: Vec<u8>,
42}
43
44/// Get entropy in BIP32.
45pub async fn get_bip32_entropy(path: &str, curve: Curve) -> Result<Bip32Entropy> {
46    let path: Vec<String> = path.split('/').map(String::from).collect();
47    let req = Bip32Params {
48        path,
49        curve,
50        compressed: None,
51    };
52    request("snap_getBip32Entropy", req).await
53}
54
55/// Get public key in BIP32.
56pub async fn get_bip32_public_key(path: &str, curve: Curve, compressed: bool) -> Result<Vec<u8>> {
57    let path: Vec<String> = path.split('/').map(String::from).collect();
58    let req = Bip32Params {
59        path,
60        curve,
61        compressed: Some(compressed),
62    };
63    let r: String = request("snap_getBip32PublicKey", req).await?;
64
65    Ok(const_hex::decode(r)?)
66}