Struct cardano_serialization_lib::crypto::Bip32PrivateKey
source · pub struct Bip32PrivateKey(_);
Implementations§
source§impl Bip32PrivateKey
impl Bip32PrivateKey
sourcepub fn derive(&self, index: u32) -> Bip32PrivateKey
pub fn derive(&self, index: u32) -> Bip32PrivateKey
derive this private key with the given index.
Security considerations
- hard derivation index cannot be soft derived with the public key
Hard derivation vs Soft derivation
If you pass an index below 0x80000000 then it is a soft derivation. The advantage of soft derivation is that it is possible to derive the public key too. I.e. derivation the private key with a soft derivation index and then retrieving the associated public key is equivalent to deriving the public key associated to the parent private key.
Hard derivation index does not allow public key derivation.
This is why deriving the private key should not fail while deriving the public key may fail (if the derivation index is invalid).
sourcepub fn from_128_xprv(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError>
pub fn from_128_xprv(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError>
128-byte xprv a key format in Cardano that some software still uses or requires the traditional 96-byte xprv is simply encoded as prv | chaincode however, because some software may not know how to compute a public key from a private key, the 128-byte inlines the public key in the following format prv | pub | chaincode so be careful if you see the term “xprv” as it could refer to either one our library does not require the pub (instead we compute the pub key when needed)
sourcepub fn to_128_xprv(&self) -> Vec<u8>
pub fn to_128_xprv(&self) -> Vec<u8>
see from_128_xprv
sourcepub fn generate_ed25519_bip32() -> Result<Bip32PrivateKey, JsError>
pub fn generate_ed25519_bip32() -> Result<Bip32PrivateKey, JsError>
sourcepub fn to_raw_key(&self) -> PrivateKey
pub fn to_raw_key(&self) -> PrivateKey
Examples found in repository?
More examples
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
pub fn make_icarus_bootstrap_witness(
tx_body_hash: &TransactionHash,
addr: &ByronAddress,
key: &Bip32PrivateKey,
) -> BootstrapWitness {
let chain_code = key.chaincode();
let raw_key = key.to_raw_key();
let vkey = Vkey::new(&raw_key.to_public());
let signature = raw_key.sign(&tx_body_hash.to_bytes());
BootstrapWitness::new(&vkey, &signature, chain_code, addr.attributes())
}
sourcepub fn to_public(&self) -> Bip32PublicKey
pub fn to_public(&self) -> Bip32PublicKey
Examples found in repository?
More examples
sourcepub fn from_bytes(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError>
pub fn from_bytes(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError>
Examples found in repository?
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
pub fn from_128_xprv(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError> {
let mut buf = [0; 96];
buf[0..64].clone_from_slice(&bytes[0..64]);
buf[64..96].clone_from_slice(&bytes[96..128]);
Bip32PrivateKey::from_bytes(&buf)
}
/// see from_128_xprv
pub fn to_128_xprv(&self) -> Vec<u8> {
let prv_key = self.to_raw_key().as_bytes();
let pub_key = self.to_public().to_raw_key().as_bytes();
let cc = self.chaincode();
let mut buf = [0; 128];
buf[0..64].clone_from_slice(&prv_key);
buf[64..96].clone_from_slice(&pub_key);
buf[96..128].clone_from_slice(&cc);
buf.to_vec()
}
pub fn generate_ed25519_bip32() -> Result<Bip32PrivateKey, JsError> {
OsRng::new()
.map(crypto::SecretKey::<crypto::Ed25519Bip32>::generate)
.map(Bip32PrivateKey)
.map_err(|e| JsError::from_str(&format!("{}", e)))
}
pub fn to_raw_key(&self) -> PrivateKey {
PrivateKey(key::EitherEd25519SecretKey::Extended(
crypto::derive::to_raw_sk(&self.0),
))
}
pub fn to_public(&self) -> Bip32PublicKey {
Bip32PublicKey(self.0.to_public().into())
}
pub fn from_bytes(bytes: &[u8]) -> Result<Bip32PrivateKey, JsError> {
crypto::SecretKey::<crypto::Ed25519Bip32>::from_binary(bytes)
.map_err(|e| JsError::from_str(&format!("{}", e)))
.map(Bip32PrivateKey)
}
pub fn as_bytes(&self) -> Vec<u8> {
self.0.as_ref().to_vec()
}
pub fn from_bech32(bech32_str: &str) -> Result<Bip32PrivateKey, JsError> {
crypto::SecretKey::try_from_bech32_str(&bech32_str)
.map(Bip32PrivateKey)
.map_err(|_| JsError::from_str("Invalid secret key"))
}
pub fn to_bech32(&self) -> String {
self.0.to_bech32_str()
}
pub fn from_bip39_entropy(entropy: &[u8], password: &[u8]) -> Bip32PrivateKey {
Bip32PrivateKey(crypto::derive::from_bip39_entropy(&entropy, &password))
}
pub fn chaincode(&self) -> Vec<u8> {
const ED25519_PRIVATE_KEY_LENGTH: usize = 64;
const XPRV_SIZE: usize = 96;
self.0.as_ref()[ED25519_PRIVATE_KEY_LENGTH..XPRV_SIZE].to_vec()
}
pub fn to_hex(&self) -> String {
hex::encode(self.as_bytes())
}
pub fn from_hex(hex_str: &str) -> Result<Bip32PrivateKey, JsError> {
match hex::decode(hex_str) {
Ok(data) => Ok(Self::from_bytes(data.as_ref())?),
Err(e) => Err(JsError::from_str(&e.to_string())),
}
}
More examples
51 52 53 54 55 56 57 58 59 60 61 62
pub(crate) fn fake_private_key() -> Bip32PrivateKey {
Bip32PrivateKey::from_bytes(&[
0xb8, 0xf2, 0xbe, 0xce, 0x9b, 0xdf, 0xe2, 0xb0, 0x28, 0x2f, 0x5b, 0xad, 0x70, 0x55, 0x62,
0xac, 0x99, 0x6e, 0xfb, 0x6a, 0xf9, 0x6b, 0x64, 0x8f, 0x44, 0x45, 0xec, 0x44, 0xf4, 0x7a,
0xd9, 0x5c, 0x10, 0xe3, 0xd7, 0x2f, 0x26, 0xed, 0x07, 0x54, 0x22, 0xa3, 0x6e, 0xd8, 0x58,
0x5c, 0x74, 0x5a, 0x0e, 0x11, 0x50, 0xbc, 0xce, 0xba, 0x23, 0x57, 0xd0, 0x58, 0x63, 0x69,
0x91, 0xf3, 0x8a, 0x37, 0x91, 0xe2, 0x48, 0xde, 0x50, 0x9c, 0x07, 0x0d, 0x81, 0x2a, 0xb2,
0xfd, 0xa5, 0x78, 0x60, 0xac, 0x87, 0x6b, 0xc4, 0x89, 0x19, 0x2c, 0x1e, 0xf4, 0xce, 0x25,
0x3c, 0x19, 0x7e, 0xe2, 0x19, 0xa4,
])
.unwrap()
}
pub fn from_bech32(bech32_str: &str) -> Result<Bip32PrivateKey, JsError>
pub fn to_bech32(&self) -> String
pub fn from_bip39_entropy(entropy: &[u8], password: &[u8]) -> Bip32PrivateKey
sourcepub fn chaincode(&self) -> Vec<u8>
pub fn chaincode(&self) -> Vec<u8>
Examples found in repository?
More examples
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
pub fn make_icarus_bootstrap_witness(
tx_body_hash: &TransactionHash,
addr: &ByronAddress,
key: &Bip32PrivateKey,
) -> BootstrapWitness {
let chain_code = key.chaincode();
let raw_key = key.to_raw_key();
let vkey = Vkey::new(&raw_key.to_public());
let signature = raw_key.sign(&tx_body_hash.to_bytes());
BootstrapWitness::new(&vkey, &signature, chain_code, addr.attributes())
}