cml_chain/crypto/
utils.rs1use std::convert::{TryFrom, TryInto};
2
3use super::{BootstrapWitness, Vkeywitness};
4use crate::byron::AddressContent;
5
6use cml_crypto::{
7 chain_crypto::{self, derive::combine_pk_and_chaincode},
8 CryptoError, PrivateKey, RawBytesEncoding, TransactionHash,
9};
10
11impl BootstrapWitness {
12 pub fn to_address(&self) -> Result<AddressContent, CryptoError> {
19 AddressContent::try_from(self.clone()).map_err(Into::into)
20 }
21}
22
23impl TryInto<chain_crypto::PublicKey<chain_crypto::ed25519_derive::Ed25519Bip32>>
24 for BootstrapWitness
25{
26 type Error = CryptoError;
27
28 fn try_into(
29 self,
30 ) -> Result<chain_crypto::PublicKey<chain_crypto::ed25519_derive::Ed25519Bip32>, Self::Error>
31 {
32 combine_pk_and_chaincode(self.public_key.0, &self.chain_code).map_err(Into::into)
33 }
34}
35
36impl TryFrom<BootstrapWitness> for AddressContent {
37 type Error = CryptoError;
38
39 fn try_from(wit: BootstrapWitness) -> Result<Self, Self::Error> {
40 let protocol_magic = wit.attributes.protocol_magic;
41 let key: chain_crypto::PublicKey<chain_crypto::ed25519_derive::Ed25519Bip32> =
42 wit.try_into()?;
43 let address_content =
44 AddressContent::new_simple(cml_crypto::Bip32PublicKey::from(key), protocol_magic);
45 Ok(address_content)
46 }
47}
48
49pub fn make_vkey_witness(tx_body_hash: &TransactionHash, sk: &PrivateKey) -> Vkeywitness {
50 let sig = sk.sign(tx_body_hash.to_raw_bytes());
51 Vkeywitness::new(sk.to_public(), sig)
52}