|
| 1 | +use base64::Engine; |
| 2 | +use hpke::{ |
| 3 | + Deserializable, OpModeS, Serializable, aead::ChaCha20Poly1305, kdf::HkdfSha256, |
| 4 | + kem::DhP256HkdfSha256, |
| 5 | +}; |
| 6 | +use progenitor_client::{Error, ResponseValue}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + generated::types::{ |
| 10 | + PrivateKeySubmitInput, Wallet, WalletImportInitializationResponse, |
| 11 | + WalletImportSubmissionRequest, WalletImportSubmissionRequestOwner, |
| 12 | + WalletImportSubmissionRequestWallet, WalletImportSupportedChains, |
| 13 | + }, |
| 14 | + subclients::WalletsClient, |
| 15 | +}; |
| 16 | + |
| 17 | +pub struct WalletImport { |
| 18 | + client: WalletsClient, |
| 19 | + initialization_response: WalletImportInitializationResponse, |
| 20 | + address: String, |
| 21 | + chain_type: WalletImportSupportedChains, |
| 22 | +} |
| 23 | + |
| 24 | +impl WalletImport { |
| 25 | + pub(crate) fn new( |
| 26 | + client: WalletsClient, |
| 27 | + initialization_response: WalletImportInitializationResponse, |
| 28 | + address: String, |
| 29 | + chain_type: WalletImportSupportedChains, |
| 30 | + ) -> Self { |
| 31 | + Self { |
| 32 | + client, |
| 33 | + initialization_response, |
| 34 | + address, |
| 35 | + chain_type, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + fn encrypt_private_key( |
| 40 | + &self, |
| 41 | + private_key_hex: &str, |
| 42 | + ) -> Result<(String, String), Box<dyn std::error::Error>> { |
| 43 | + // Decode the public key from base64 |
| 44 | + let public_key_bytes = base64::engine::general_purpose::STANDARD |
| 45 | + .decode(&self.initialization_response.encryption_public_key)?; |
| 46 | + |
| 47 | + // Deserialize the public key using HPKE trait |
| 48 | + let public_key = <DhP256HkdfSha256 as hpke::Kem>::PublicKey::from_bytes(&public_key_bytes) |
| 49 | + .map_err(|e| format!("Failed to deserialize public key: {:?}", e))?; |
| 50 | + |
| 51 | + // Convert hex private key to bytes (remove 0x prefix if present) |
| 52 | + let private_key_hex = private_key_hex |
| 53 | + .strip_prefix("0x") |
| 54 | + .unwrap_or(private_key_hex); |
| 55 | + let private_key_bytes = hex::decode(private_key_hex)?; |
| 56 | + |
| 57 | + // Setup HPKE sender context |
| 58 | + let mut rng = rand::rng(); |
| 59 | + let (encapsulated_key, mut encryption_context) = |
| 60 | + hpke::setup_sender::<ChaCha20Poly1305, HkdfSha256, DhP256HkdfSha256, _>( |
| 61 | + &OpModeS::Base, |
| 62 | + &public_key, |
| 63 | + &[], |
| 64 | + &mut rng, |
| 65 | + ) |
| 66 | + .map_err(|e| format!("HPKE setup failed: {:?}", e))?; |
| 67 | + |
| 68 | + // Encrypt the private key |
| 69 | + let ciphertext = encryption_context |
| 70 | + .seal(&private_key_bytes, &[]) |
| 71 | + .map_err(|e| format!("HPKE encryption failed: {:?}", e))?; |
| 72 | + |
| 73 | + // Encode results as base64 |
| 74 | + let ciphertext_b64 = base64::engine::general_purpose::STANDARD.encode(&ciphertext); |
| 75 | + let encapsulated_key_b64 = |
| 76 | + base64::engine::general_purpose::STANDARD.encode(&encapsulated_key.to_bytes()); |
| 77 | + |
| 78 | + Ok((ciphertext_b64, encapsulated_key_b64)) |
| 79 | + } |
| 80 | + |
| 81 | + pub async fn submit( |
| 82 | + self, |
| 83 | + private_key_hex: &str, |
| 84 | + owner: Option<WalletImportSubmissionRequestOwner>, |
| 85 | + policy_ids: Vec<String>, |
| 86 | + additional_signers: Vec< |
| 87 | + crate::generated::types::WalletImportSubmissionRequestAdditionalSignersItem, |
| 88 | + >, |
| 89 | + ) -> Result<ResponseValue<Wallet>, Error<()>> { |
| 90 | + // Encrypt the private key using HPKE |
| 91 | + let (ciphertext, encapsulated_key) = self |
| 92 | + .encrypt_private_key(private_key_hex) |
| 93 | + .map_err(|_| Error::InvalidRequest("Failed to encrypt private key".to_string()))?; |
| 94 | + |
| 95 | + // Create the wallet submission input |
| 96 | + let wallet_input = PrivateKeySubmitInput { |
| 97 | + address: self.address, |
| 98 | + chain_type: self.chain_type, |
| 99 | + ciphertext, |
| 100 | + encapsulated_key, |
| 101 | + encryption_type: self.initialization_response.encryption_type, |
| 102 | + entropy_type: crate::generated::types::PrivateKeySubmitInputEntropyType::PrivateKey, |
| 103 | + }; |
| 104 | + |
| 105 | + // Create the submission request |
| 106 | + let submission_request = WalletImportSubmissionRequest { |
| 107 | + wallet: WalletImportSubmissionRequestWallet::PrivateKeySubmitInput(wallet_input), |
| 108 | + owner, |
| 109 | + owner_id: None, |
| 110 | + policy_ids, |
| 111 | + additional_signers, |
| 112 | + }; |
| 113 | + |
| 114 | + // Submit the import request |
| 115 | + self.client.submit_import(&submission_request).await |
| 116 | + } |
| 117 | +} |
0 commit comments