|
7 | 7 | pub mod cardano; |
8 | 8 | pub mod waves; |
9 | 9 |
|
10 | | -use bip32::ChildNumber; |
| 10 | +use bip32::{ChainCode, ChildNumber}; |
11 | 11 | use tw_keypair::{ed25519, tw::Curve}; |
12 | 12 |
|
13 | 13 | use crate::crypto_hd_node::error::{Error, Result}; |
@@ -40,9 +40,28 @@ impl BIP32PrivateKey for ed25519::sha512::PrivateKey { |
40 | 40 | } |
41 | 41 |
|
42 | 42 | impl BIP32PublicKey for ed25519::sha512::PublicKey { |
43 | | - fn derive_child(&self, other: &[u8], child_number: ChildNumber) -> Result<Self> { |
| 43 | + fn curve() -> Curve { |
| 44 | + Curve::Ed25519 |
| 45 | + } |
| 46 | + |
| 47 | + fn derive_child( |
| 48 | + &self, |
| 49 | + chain_code: &ChainCode, |
| 50 | + child_number: ChildNumber, |
| 51 | + ) -> Result<(Self, ChainCode)> { |
| 52 | + let (tweak, chain_code) = self.derive_tweak(chain_code, child_number)?; |
| 53 | + // We should technically loop here if the tweak is zero or overflows |
| 54 | + // the order of the underlying elliptic curve group, incrementing the |
| 55 | + // index, however per "Child key derivation (CKD) functions": |
| 56 | + // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions |
| 57 | + // |
| 58 | + // > "Note: this has probability lower than 1 in 2^127." |
| 59 | + // |
| 60 | + // ...so instead, we simply return an error if this were ever to happen, |
| 61 | + // as the chances of it happening are vanishingly small. |
44 | 62 | if child_number.is_hardened() { |
45 | | - Self::try_from(other).map_err(|_| Error::InvalidKeyData) |
| 63 | + let public_key = Self::try_from(&tweak[..]).map_err(|_| Error::InvalidKeyData)?; |
| 64 | + Ok((public_key, chain_code)) |
46 | 65 | } else { |
47 | 66 | Err(Error::InvalidChildNumber) |
48 | 67 | } |
@@ -74,9 +93,28 @@ impl BIP32PrivateKey for ed25519::blake2b::PrivateKey { |
74 | 93 | } |
75 | 94 |
|
76 | 95 | impl BIP32PublicKey for ed25519::blake2b::PublicKey { |
77 | | - fn derive_child(&self, other: &[u8], child_number: ChildNumber) -> Result<Self> { |
| 96 | + fn curve() -> Curve { |
| 97 | + Curve::Ed25519Blake2bNano |
| 98 | + } |
| 99 | + |
| 100 | + fn derive_child( |
| 101 | + &self, |
| 102 | + chain_code: &ChainCode, |
| 103 | + child_number: ChildNumber, |
| 104 | + ) -> Result<(Self, ChainCode)> { |
| 105 | + let (tweak, chain_code) = self.derive_tweak(chain_code, child_number)?; |
| 106 | + // We should technically loop here if the tweak is zero or overflows |
| 107 | + // the order of the underlying elliptic curve group, incrementing the |
| 108 | + // index, however per "Child key derivation (CKD) functions": |
| 109 | + // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions |
| 110 | + // |
| 111 | + // > "Note: this has probability lower than 1 in 2^127." |
| 112 | + // |
| 113 | + // ...so instead, we simply return an error if this were ever to happen, |
| 114 | + // as the chances of it happening are vanishingly small. |
78 | 115 | if child_number.is_hardened() { |
79 | | - Self::try_from(other).map_err(|_| Error::InvalidKeyData) |
| 116 | + let public_key = Self::try_from(&tweak[..]).map_err(|_| Error::InvalidKeyData)?; |
| 117 | + Ok((public_key, chain_code)) |
80 | 118 | } else { |
81 | 119 | Err(Error::InvalidChildNumber) |
82 | 120 | } |
|
0 commit comments