@@ -215,6 +215,78 @@ impl WalletManager {
215215 } )
216216 }
217217
218+ /// Create wallet from 32-byte seed
219+ pub async fn create_wallet_from_seed (
220+ & self ,
221+ name : & str ,
222+ seed_hex : & str ,
223+ password : Option < & str > ,
224+ ) -> Result < WalletInfo > {
225+ // Check if wallet already exists
226+ let keystore = Keystore :: new ( & self . wallets_dir ) ;
227+ if keystore. load_wallet ( name) ?. is_some ( ) {
228+ return Err ( WalletError :: AlreadyExists . into ( ) ) ;
229+ }
230+
231+ // Validate seed hex format (should be 64 hex characters for 32 bytes)
232+ if seed_hex. len ( ) != 64 {
233+ return Err ( WalletError :: InvalidMnemonic . into ( ) ) ; // Reusing error type
234+ }
235+
236+ // Convert hex to bytes
237+ let seed_bytes = hex:: decode ( seed_hex) . map_err ( |_| WalletError :: InvalidMnemonic ) ?;
238+ if seed_bytes. len ( ) != 32 {
239+ return Err ( WalletError :: InvalidMnemonic . into ( ) ) ;
240+ }
241+
242+ // Create DilithiumPair from seed
243+ let seed_array: [ u8 ; 32 ] =
244+ seed_bytes. try_into ( ) . map_err ( |_| WalletError :: InvalidMnemonic ) ?;
245+
246+ println ! ( "Debug: seed_array length: {}" , seed_array. len( ) ) ;
247+ println ! ( "Debug: seed_hex: {}" , seed_hex) ;
248+ println ! ( "Debug: calling DilithiumPair::from_seed" ) ;
249+
250+ let dilithium_pair = qp_dilithium_crypto:: types:: DilithiumPair :: from_seed ( & seed_array)
251+ . map_err ( |e| {
252+ println ! ( "Debug: DilithiumPair::from_seed failed with error: {:?}" , e) ;
253+ WalletError :: InvalidMnemonic
254+ } ) ?;
255+
256+ println ! ( "Debug: DilithiumPair created successfully" ) ;
257+
258+ // Convert to QuantumKeyPair
259+ let quantum_keypair = QuantumKeyPair :: from_resonance_pair ( & dilithium_pair) ;
260+
261+ // Create wallet data
262+ let mut metadata = std:: collections:: HashMap :: new ( ) ;
263+ metadata. insert ( "version" . to_string ( ) , "1.0.0" . to_string ( ) ) ;
264+ metadata. insert ( "algorithm" . to_string ( ) , "ML-DSA-87" . to_string ( ) ) ;
265+ metadata. insert ( "from_seed" . to_string ( ) , "true" . to_string ( ) ) ;
266+
267+ // Generate address from public key
268+ let address = quantum_keypair. to_account_id_ss58check ( ) ;
269+
270+ let wallet_data = WalletData {
271+ name : name. to_string ( ) ,
272+ keypair : quantum_keypair,
273+ mnemonic : None , // No mnemonic for seed-based wallets
274+ metadata,
275+ } ;
276+
277+ // Encrypt and save the wallet
278+ let password = password. unwrap_or ( "" ) ; // Use empty password if none provided
279+ let encrypted_wallet = keystore. encrypt_wallet_data ( & wallet_data, password) ?;
280+ keystore. save_wallet ( & encrypted_wallet) ?;
281+
282+ Ok ( WalletInfo {
283+ name : name. to_string ( ) ,
284+ address,
285+ created_at : encrypted_wallet. created_at ,
286+ key_type : "Dilithium ML-DSA-87" . to_string ( ) ,
287+ } )
288+ }
289+
218290 /// Get wallet by name with password for decryption
219291 pub fn get_wallet ( & self , name : & str , password : Option < & str > ) -> Result < Option < WalletInfo > > {
220292 let keystore = Keystore :: new ( & self . wallets_dir ) ;
0 commit comments