colonylib 0.6.0

A library implementing the Colony metadata framework on Autonomi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use autonomi::client::key_derivation::{DerivationIndex, MainSecretKey};
use autonomi::{PublicKey, SecretKey};
use bip39::Error as Bip39Error;
use bip39::{Language, Mnemonic};
use blsttc::Error as BlsttcError;
use borsh::{BorshDeserialize, BorshSerialize};
use cocoon::Cocoon;
use cocoon::Error as CocoonError;
use hex;
use k256::ecdsa::{SigningKey, VerifyingKey};
use serde;
use sn_bls_ckd::derive_master_sk;
use sn_curv::elliptic::curves::ECScalar;
use std::collections::HashMap;
use std::fmt;
use std::io::Error as IoError;
use thiserror;
use tracing::{debug, error, info, instrument, warn};

// Error handling
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("{0:?}")]
    Cocoon(CocoonError),
    #[error(transparent)]
    Io(#[from] IoError),
    #[error(transparent)]
    Bip39(#[from] Bip39Error),
    #[error(transparent)]
    Blsttc(#[from] BlsttcError),
    #[error(transparent)]
    Hex(#[from] hex::FromHexError),
    #[error(transparent)]
    K256(#[from] k256::elliptic_curve::Error),
    #[error(transparent)]
    K256Ecdsa(#[from] k256::ecdsa::Error),
}

// Removed manual Display implementation to avoid conflict with thiserror::Error

#[derive(serde::Serialize)]
#[serde(tag = "kind", content = "message")]
#[serde(rename_all = "camelCase")]
pub enum ErrorKind {
    Cocoon(String),
    Io(String),
    Bip39(String),
    Blsttc(String),
    Hex(String),
    K256(String),
    K256Ecdsa(String),
}

impl serde::Serialize for Error {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        let error_message = self.to_string();
        let error_kind = match self {
            Self::Cocoon(_) => ErrorKind::Cocoon(error_message),
            Self::Io(_) => ErrorKind::Io(error_message),
            Self::Bip39(_) => ErrorKind::Bip39(error_message),
            Self::Blsttc(_) => ErrorKind::Blsttc(error_message),
            Self::Hex(_) => ErrorKind::Hex(error_message),
            Self::K256(_) => ErrorKind::K256(error_message),
            Self::K256Ecdsa(_) => ErrorKind::K256Ecdsa(error_message),
        };
        error_kind.serialize(serializer)
    }
}

#[derive(BorshDeserialize, BorshSerialize, Clone)]
pub struct KeyStore {
    wallet_key: HashMap<String, Vec<u8>>,
    mnemonic: String,
    main_sk: Vec<u8>,
    pointers: HashMap<Vec<u8>, Vec<u8>>,
    scratchpads: HashMap<Vec<u8>, Vec<u8>>,
    bad_keys: HashMap<Vec<u8>, Vec<u8>>,
    free_pointers: HashMap<Vec<u8>, Vec<u8>>,
    free_scratchpads: HashMap<Vec<u8>, Vec<u8>>,
}

impl fmt::Debug for KeyStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let wallet_keys_debug: HashMap<String, String> = self
            .wallet_key
            .iter()
            .map(|(k, v)| (k.clone(), hex::encode(v)))
            .collect();
        f.debug_struct("KeyStore")
            .field("wallet_key", &wallet_keys_debug)
            .field("mnemonic", &self.mnemonic)
            .field("main_sk", &hex::encode(&self.main_sk))
            .field("pointers", &self.get_pointers())
            .field("scratchpads", &self.get_scratchpads())
            .field("bad_keys", &self.get_bad_keys())
            .finish()
    }
}

impl KeyStore {
    #[instrument]
    pub fn from_file<R: std::io::Read + std::fmt::Debug>(
        file: &mut R,
        password: &str,
    ) -> Result<Self, Error> {
        let cocoon = Cocoon::new(password.as_bytes());
        let encoded = cocoon.parse(file).map_err(Error::Cocoon)?;
        debug!("Read from file: {:?}", file);
        let key_store = KeyStore::try_from_slice(&encoded)?;
        debug!("Parsed key store: {:?}", key_store);
        info!("Key store loaded successfully");
        Ok(key_store)
    }

    #[instrument]
    pub fn to_file<W: std::io::Write + std::fmt::Debug>(
        &self,
        file: &mut W,
        password: &str,
    ) -> Result<(), Error> {
        let mut cocoon = Cocoon::new(password.as_bytes());
        let encoded = borsh::to_vec(&self)?;
        cocoon.dump(encoded, file).map_err(Error::Cocoon)?;
        debug!("Wrote to file: {:?}", file);
        info!("Key store saved successfully");
        Ok(())
    }

    #[instrument]
    pub fn from_mnemonic(mnemonic: &str) -> Result<Self, Error> {
        // Generate a new mnemonic from the given phrase
        let mnemonic = Mnemonic::parse_in_normalized(Language::English, mnemonic)?;
        let seed = mnemonic.to_seed_normalized("");

        // Derive BLS12-381 master secret key from seed using EIP-2333 standard.
        // Guarantees a valid, non-zero scalar represented as 32 Big-Endian bytes.
        let key_bytes: [u8; 32] = derive_master_sk(&seed)
            .expect("derive_master_sk failed; seed length requirement is >= 32 bytes")
            .serialize() // Get the 32-byte Big-Endian representation
            .into(); // Convert GenericArray<u8, 32> to [u8; 32]

        // Create a SecretKey from the 32-byte array
        let secret_key = SecretKey::from_bytes(key_bytes)?;

        let hex_key = hex::encode(secret_key.to_bytes());
        let hex_key = hex_key.as_str();

        Self::from_hex(hex_key, mnemonic.to_string().as_str())
    }

    #[instrument]
    pub fn from_hex(key: &str, mnemonic: &str) -> Result<Self, Error> {
        let secret_key = SecretKey::from_hex(key)?;

        // Generate a new main keys from the mnemonic
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        //let main_pk: MainPubkey = main_sk.public_key();

        // Create a new pods hashmap
        let mut pointers: HashMap<PublicKey, SecretKey> = HashMap::new();
        let mut scratchpads: HashMap<PublicKey, SecretKey> = HashMap::new();
        let bad_keys: HashMap<PublicKey, SecretKey> = HashMap::new();
        let free_pointers: HashMap<PublicKey, SecretKey> = HashMap::new();
        let free_scratchpads: HashMap<PublicKey, SecretKey> = HashMap::new();
        //let pod_key: SecretKey = main_sk.derive_key(&index(0)).into();
        //let pod_pubkey: PublicKey = pod_key.public_key();
        //pods.insert(pod_pubkey, pod_key.clone());

        // Add configuratino pod pointer key
        let pointer_key: SecretKey = main_sk.derive_key(&index(0)).into();
        let pointer_pubkey: PublicKey = pointer_key.clone().public_key();
        pointers.insert(pointer_pubkey, pointer_key);

        // Add configuration pod scratchpad key
        let scratchpad_key: SecretKey = main_sk.derive_key(&index(1)).into();
        let scratchpad_pubkey: PublicKey = scratchpad_key.clone().public_key();
        scratchpads.insert(scratchpad_pubkey, scratchpad_key);

        Ok(KeyStore {
            wallet_key: HashMap::new(),
            mnemonic: mnemonic.to_string(),
            main_sk: main_sk.to_bytes(),
            pointers: pointers
                .iter()
                .map(|(k, v)| (k.to_bytes().to_vec(), v.to_bytes().to_vec()))
                .collect(),
            scratchpads: scratchpads
                .iter()
                .map(|(k, v)| (k.to_bytes().to_vec(), v.to_bytes().to_vec()))
                .collect(),
            bad_keys: bad_keys
                .iter()
                .map(|(k, v)| (k.to_bytes().to_vec(), v.to_bytes().to_vec()))
                .collect(),
            free_pointers: free_pointers
                .iter()
                .map(|(k, v)| (k.to_bytes().to_vec(), v.to_bytes().to_vec()))
                .collect(),
            free_scratchpads: free_scratchpads
                .iter()
                .map(|(k, v)| (k.to_bytes().to_vec(), v.to_bytes().to_vec()))
                .collect(),
        })
    }

    pub fn get_seed_phrase(&self) -> String {
        debug!("Seed phrase: {}", self.mnemonic);
        self.mnemonic.clone()
    }

    pub fn add_wallet_key(&mut self, name: &str, wallet_key: &str) -> Result<(), Error> {
        let wallet_key = remove_0x_prefix(wallet_key);
        // Verify that the decoded key is a valid Ethereum private key (32 bytes)
        let decoded_key = hex::decode(&wallet_key)?;
        if decoded_key.len() != 32 {
            return Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Ethereum private key must be exactly 32 bytes",
            )));
        }
        // Verify it's a valid secp256k1 private key
        let _signing_key = SigningKey::from_slice(&decoded_key)?;

        self.wallet_key
            .insert(name.to_string(), decoded_key.clone());
        debug!(
            "Wallet key added for '{}': {}",
            name,
            hex::encode(decoded_key)
        );
        Ok(())
    }

    pub fn remove_wallet_key(&mut self, name: &str) -> Result<(), Error> {
        match self.wallet_key.remove(name) {
            Some(_) => {
                debug!("Wallet key removed for '{}'", name);
                Ok(())
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Wallet key '{name}' not found"),
            ))),
        }
    }

    pub fn get_wallet_key(&self, name: &str) -> Result<String, Error> {
        match self.wallet_key.get(name) {
            Some(key) => {
                let encoded_key = hex::encode(key);
                debug!("Wallet key for '{}': {}", name, encoded_key);
                Ok(encoded_key)
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Wallet key '{name}' not found"),
            ))),
        }
    }

    pub fn get_wallet_keys(&self) -> HashMap<String, String> {
        self.wallet_key
            .iter()
            .map(|(k, v)| (k.clone(), hex::encode(v)))
            .collect()
    }

    pub fn set_active_wallet(&mut self, name: &str) -> Result<(String, String), Error> {
        if !self.wallet_key.contains_key(name) {
            return Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Wallet key '{name}' not found"),
            )));
        }
        let active_wallet = name;
        let active_wallet_address = self.get_wallet_address(name)?;
        debug!("Active wallet set to '{}'", name);
        Ok((active_wallet.to_string(), active_wallet_address))
    }

    pub fn get_wallet_address(&self, name: &str) -> Result<String, Error> {
        match self.wallet_key.get(name) {
            Some(key_bytes) => {
                if key_bytes.len() == 32 {
                    match SigningKey::from_slice(key_bytes) {
                        Ok(signing_key) => {
                            let verifying_key = signing_key.verifying_key();
                            let address = ethereum_address_from_public_key(verifying_key);
                            debug!("Wallet address for '{}': {}", name, address);
                            Ok(address)
                        }
                        Err(e) => {
                            warn!(
                                "Invalid wallet key for '{}': {}. Using default address.",
                                name, e
                            );
                            // Return a default Ethereum address (all zeros)
                            Ok("0x0000000000000000000000000000000000000000".to_string())
                        }
                    }
                } else {
                    warn!(
                        "Invalid wallet key length for '{}' (expected 32 bytes, got {}). Using default address.",
                        name,
                        key_bytes.len()
                    );
                    // Return a default Ethereum address (all zeros)
                    Ok("0x0000000000000000000000000000000000000000".to_string())
                }
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Wallet key '{name}' not found"),
            ))),
        }
    }

    pub fn get_wallet_addresses(&self) -> HashMap<String, String> {
        // Get the list of wallet keys
        let wallet_keys = self.get_wallet_keys();

        // Return the list of wallet addresses by deriving them from the wallet keys
        wallet_keys
            .keys()
            .map(|k| {
                // Derive the Ethereum address from the wallet key
                let address = self
                    .get_wallet_address(k.as_str())
                    .unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string());
                (k.clone(), address)
            })
            .collect()
    }

    pub fn get_configuration_address(&self) -> Result<String, Error> {
        // Get the first derived key
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let key: SecretKey = main_sk.derive_key(&index(0)).into();
        let pubkey: PublicKey = key.clone().public_key();
        debug!("Configuration pod address: {}", pubkey.to_hex());
        Ok(pubkey.to_hex())
    }

    pub fn get_configuration_scratchpad_address(&self) -> Result<String, Error> {
        // Get the first derived key
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let key: SecretKey = main_sk.derive_key(&index(1)).into();
        let pubkey: PublicKey = key.clone().public_key();
        debug!("Configuration pod address: {}", pubkey.to_hex());
        Ok(pubkey.to_hex())
    }

    pub fn get_pointer_key(&self, pod_pubkey: String) -> Result<String, Error> {
        let decoded_key = hex::decode(pod_pubkey.as_str())?;
        match self.pointers.get(&decoded_key) {
            Some(value) => {
                debug!("Pointer key: {}", hex::encode(value));
                Ok(hex::encode(value))
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Key not found",
            ))),
        }
    }

    pub fn get_scratchpad_key(&self, pod_pubkey: String) -> Result<String, Error> {
        let decoded_key = hex::decode(pod_pubkey.as_str())?;
        match self.scratchpads.get(&decoded_key) {
            Some(value) => {
                debug!("Scratchpad key: {}", hex::encode(value));
                Ok(hex::encode(value))
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Key not found",
            ))),
        }
    }

    pub fn get_free_pointer_key(&self, pod_pubkey: String) -> Result<String, Error> {
        let decoded_key = hex::decode(pod_pubkey.as_str())?;
        match self.free_pointers.get(&decoded_key) {
            Some(value) => {
                debug!("Pointer key: {}", hex::encode(value));
                Ok(hex::encode(value))
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Key not found",
            ))),
        }
    }

    pub fn get_free_scratchpad_key(&self, pod_pubkey: String) -> Result<String, Error> {
        let decoded_key = hex::decode(pod_pubkey.as_str())?;
        match self.free_scratchpads.get(&decoded_key) {
            Some(value) => {
                debug!("Scratchpad key: {}", hex::encode(value));
                Ok(hex::encode(value))
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Key not found",
            ))),
        }
    }

    pub fn get_bad_key(&self, pod_pubkey: String) -> Result<String, Error> {
        let decoded_key = hex::decode(pod_pubkey.as_str())?;
        match self.bad_keys.get(&decoded_key) {
            Some(value) => {
                debug!("Bad key: {}", hex::encode(value));
                Ok(hex::encode(value))
            }
            None => Err(Error::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Key not found",
            ))),
        }
    }

    pub fn address_is_pointer(&self, address: &str) -> bool {
        self.pointers
            .contains_key(&hex::decode(address).unwrap_or_default())
    }

    pub fn add_pointer_key(&mut self) -> Result<(String, String), Error> {
        // Check for unused keys first
        let key_pair = self.free_pointers.iter().next();
        if let Some((pubkey, key)) = key_pair {
            let pubkey = pubkey.clone();
            let key = key.clone();
            self.free_pointers.remove(&pubkey);
            self.pointers.insert(pubkey.clone(), key.clone());
            debug!(
                "Reusing unused key at address: {}",
                hex::encode(pubkey.clone())
            );
            return Ok((hex::encode(pubkey), hex::encode(key)));
        }

        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let num_keys = self.get_num_keys();
        let pod_key: SecretKey = main_sk.derive_key(&index(num_keys)).into();
        let pod_pubkey: PublicKey = pod_key.clone().public_key();
        self.pointers.insert(
            pod_pubkey.to_bytes().to_vec(),
            pod_key.clone().to_bytes().to_vec(),
        );
        Ok((
            pod_pubkey.to_hex().to_string(),
            pod_key.to_hex().to_string(),
        ))
    }

    pub fn remove_pointer_key(&mut self, address: &str) -> Result<(), Error> {
        let pubkey = hex::decode(address)?;
        let key = self.pointers.remove(&pubkey);
        match key {
            Some(value) => {
                self.free_pointers.insert(pubkey, value);
            }
            None => {
                return Err(Error::Io(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Key not found",
                )));
            }
        }
        Ok(())
    }

    pub fn add_scratchpad_key(&mut self) -> Result<(String, String), Error> {
        // Check for unused keys first
        let key_pair = self.free_scratchpads.iter().next();
        if let Some((pubkey, key)) = key_pair {
            let pubkey = pubkey.clone();
            let key = key.clone();
            self.free_scratchpads.remove(&pubkey);
            self.scratchpads.insert(pubkey.clone(), key.clone());
            debug!(
                "Reusing unused key at address: {}",
                hex::encode(pubkey.clone())
            );
            return Ok((hex::encode(pubkey), hex::encode(key)));
        }
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let num_keys = self.get_num_keys();
        let pod_key: SecretKey = main_sk.derive_key(&index(num_keys)).into();
        let pod_pubkey: PublicKey = pod_key.clone().public_key();
        self.scratchpads.insert(
            pod_pubkey.to_bytes().to_vec(),
            pod_key.clone().to_bytes().to_vec(),
        );
        Ok((
            pod_pubkey.to_hex().to_string(),
            pod_key.to_hex().to_string(),
        ))
    }

    pub fn remove_scratchpad_key(&mut self, address: &str) -> Result<(), Error> {
        let pubkey = hex::decode(address)?;
        let key = self.scratchpads.remove(&pubkey);
        match key {
            Some(value) => {
                self.free_scratchpads.insert(pubkey, value);
            }
            None => {
                return Err(Error::Io(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Key not found",
                )));
            }
        }
        Ok(())
    }

    pub fn add_bad_key(&mut self) -> Result<String, Error> {
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let num_keys = self.get_num_keys();
        let pod_key: SecretKey = main_sk.derive_key(&index(num_keys)).into();
        let pod_pubkey: PublicKey = pod_key.clone().public_key();
        self.bad_keys.insert(
            pod_pubkey.to_bytes().to_vec(),
            pod_key.clone().to_bytes().to_vec(),
        );
        Ok(pod_key.to_hex().to_string())
    }

    pub fn add_free_pointer_key(&mut self) -> Result<String, Error> {
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let num_keys = self.get_num_keys();
        let pod_key: SecretKey = main_sk.derive_key(&index(num_keys)).into();
        let pod_pubkey: PublicKey = pod_key.clone().public_key();
        self.free_pointers.insert(
            pod_pubkey.to_bytes().to_vec(),
            pod_key.clone().to_bytes().to_vec(),
        );
        Ok(pod_key.to_hex().to_string())
    }

    pub fn add_free_scratchpad_key(&mut self) -> Result<String, Error> {
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let num_keys = self.get_num_keys();
        let pod_key: SecretKey = main_sk.derive_key(&index(num_keys)).into();
        let pod_pubkey: PublicKey = pod_key.clone().public_key();
        self.free_scratchpads.insert(
            pod_pubkey.to_bytes().to_vec(),
            pod_key.clone().to_bytes().to_vec(),
        );
        Ok(pod_key.to_hex().to_string())
    }

    pub fn clear_keys(&mut self) -> Result<(), Error> {
        self.pointers.clear();
        self.scratchpads.clear();
        self.bad_keys.clear();
        self.free_pointers.clear();
        self.free_scratchpads.clear();
        Ok(())
    }

    pub fn get_num_pointer_keys(&self) -> u64 {
        debug!("Number of pointer keys: {}", self.pointers.len());
        self.pointers.len() as u64
    }

    pub fn get_num_scratchpad_keys(&self) -> u64 {
        debug!("Number of scratchpad keys: {}", self.scratchpads.len());
        self.scratchpads.len() as u64
    }

    pub fn get_num_bad_keys(&self) -> u64 {
        debug!("Number of bad derived keys: {}", self.bad_keys.len());
        self.bad_keys.len() as u64
    }

    pub fn get_num_keys(&self) -> u64 {
        self.get_num_pointer_keys() + self.get_num_scratchpad_keys() + self.get_num_bad_keys()
    }

    pub fn get_pointers(&self) -> HashMap<String, String> {
        self.pointers
            .iter()
            .map(|(k, v)| (hex::encode(k), hex::encode(v)))
            .collect()
    }

    pub fn get_scratchpads(&self) -> HashMap<String, String> {
        self.scratchpads
            .iter()
            .map(|(k, v)| (hex::encode(k), hex::encode(v)))
            .collect()
    }

    pub fn get_bad_keys(&self) -> HashMap<String, String> {
        self.bad_keys
            .iter()
            .map(|(k, v)| (hex::encode(k), hex::encode(v)))
            .collect()
    }

    pub fn get_free_pointers(&self) -> HashMap<String, String> {
        self.free_pointers
            .iter()
            .map(|(k, v)| (hex::encode(k), hex::encode(v)))
            .collect()
    }

    pub fn get_free_scratchpads(&self) -> HashMap<String, String> {
        self.free_scratchpads
            .iter()
            .map(|(k, v)| (hex::encode(k), hex::encode(v)))
            .collect()
    }

    pub fn get_address_at_index(&self, count: u64) -> Result<String, Error> {
        let main_sk_array: [u8; 32] = self
            .main_sk
            .clone()
            .try_into()
            .expect("main_sk must be 32 bytes");
        let secret_key: SecretKey = SecretKey::from_bytes(main_sk_array)?;
        let main_sk: MainSecretKey = MainSecretKey::new(secret_key);
        let pod_key: SecretKey = main_sk.derive_key(&index(count)).into();
        Ok(pod_key.public_key().to_hex())
    }
}

fn index(i: u64) -> DerivationIndex {
    let mut bytes = [0u8; 32];
    bytes[..8].copy_from_slice(&i.to_ne_bytes());
    DerivationIndex::from_bytes(bytes)
}

fn remove_0x_prefix(input: &str) -> String {
    input.strip_prefix("0x").unwrap_or(input).to_string()
}

fn ethereum_address_from_public_key(verifying_key: &VerifyingKey) -> String {
    use sha3::{Digest, Keccak256};

    // Get the uncompressed public key (65 bytes: 0x04 + 32 bytes x + 32 bytes y)
    let public_key_bytes = verifying_key.to_encoded_point(false);
    let public_key_bytes = public_key_bytes.as_bytes();

    // Skip the first byte (0x04) and hash the remaining 64 bytes
    let hash = Keccak256::digest(&public_key_bytes[1..]);

    // Take the last 20 bytes and format as hex with 0x prefix
    let address_bytes = &hash[12..];
    format!("0x{}", hex::encode(address_bytes))
}