bittensor-rs 0.1.1

Standalone Rust SDK for Bittensor blockchain interactions
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
//! # Wallet Module
//!
//! Wallet management for Bittensor, including key loading, signing, and
//! transaction creation.
//!
//! # Example
//!
//! ```rust,no_run
//! use bittensor_rs::wallet::Wallet;
//!
//! // Load an existing wallet
//! let wallet = Wallet::load("my_wallet", "my_hotkey")?;
//!
//! // Sign data with the hotkey
//! let signature = wallet.sign(b"message");
//!
//! // Get the hotkey address
//! let hotkey = wallet.hotkey();
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

mod keyfile;
mod signer;

pub use keyfile::{KeyfileData, KeyfileError};
pub use signer::WalletSigner;

use crate::error::BittensorError;
use crate::types::Hotkey;
use crate::AccountId;
use sp_core::{sr25519, Pair};
use std::path::{Path, PathBuf};

/// Bittensor wallet for managing keys and signing transactions
///
/// A wallet contains:
/// - A hotkey (required) for signing transactions
/// - An optional coldkey for staking operations
///
/// # Example
///
/// ```rust,no_run
/// use bittensor_rs::wallet::Wallet;
///
/// // Load from default ~/.bittensor/wallets path
/// let wallet = Wallet::load("my_wallet", "my_hotkey")?;
/// println!("Hotkey: {}", wallet.hotkey());
/// # Ok::<(), bittensor_rs::BittensorError>(())
/// ```
#[derive(Clone)]
pub struct Wallet {
    /// Wallet name
    pub name: String,
    /// Hotkey name
    pub hotkey_name: String,
    /// Path to the wallet directory
    pub path: PathBuf,
    /// Hotkey keypair
    hotkey_pair: sr25519::Pair,
    /// Optional coldkey keypair (requires unlock)
    coldkey_pair: Option<sr25519::Pair>,
}

impl std::fmt::Debug for Wallet {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Wallet")
            .field("name", &self.name)
            .field("hotkey_name", &self.hotkey_name)
            .field("path", &self.path)
            .field("hotkey", &self.hotkey().to_string())
            .field("coldkey_unlocked", &self.is_coldkey_unlocked())
            .finish()
    }
}

impl Wallet {
    /// Load a wallet from the default Bittensor wallet path
    ///
    /// Wallets are stored in `~/.bittensor/wallets/<wallet_name>/hotkeys/<hotkey_name>`
    ///
    /// # Arguments
    ///
    /// * `wallet_name` - Name of the wallet directory
    /// * `hotkey_name` - Name of the hotkey file
    ///
    /// # Returns
    ///
    /// * `Ok(Wallet)` if the wallet was loaded successfully
    /// * `Err(BittensorError)` if the wallet could not be loaded
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::load("default", "default")?;
    /// # Ok::<(), bittensor_rs::BittensorError>(())
    /// ```
    pub fn load(wallet_name: &str, hotkey_name: &str) -> Result<Self, BittensorError> {
        let wallet_path = Self::default_wallet_path()?;
        Self::load_from_path(wallet_name, hotkey_name, &wallet_path)
    }

    /// Load a wallet from a custom path
    ///
    /// # Arguments
    ///
    /// * `wallet_name` - Name of the wallet directory
    /// * `hotkey_name` - Name of the hotkey file
    /// * `base_path` - Base path where wallets are stored
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use bittensor_rs::wallet::Wallet;
    /// use std::path::PathBuf;
    ///
    /// let base_path = PathBuf::from("/custom/wallets");
    /// let wallet = Wallet::load_from_path("my_wallet", "my_hotkey", &base_path)?;
    /// # Ok::<(), bittensor_rs::BittensorError>(())
    /// ```
    pub fn load_from_path(
        wallet_name: &str,
        hotkey_name: &str,
        base_path: &Path,
    ) -> Result<Self, BittensorError> {
        let hotkey_path = base_path
            .join(wallet_name)
            .join("hotkeys")
            .join(hotkey_name);

        if !hotkey_path.exists() {
            return Err(BittensorError::WalletError {
                message: format!("Hotkey file not found: {}", hotkey_path.display()),
            });
        }

        let keyfile_data = keyfile::load_keyfile(&hotkey_path)?;
        let hotkey_pair = keyfile_data.to_keypair()?;

        Ok(Self {
            name: wallet_name.to_string(),
            hotkey_name: hotkey_name.to_string(),
            path: base_path.join(wallet_name),
            hotkey_pair,
            coldkey_pair: None,
        })
    }

    /// Create a new wallet with a random seed
    ///
    /// # Arguments
    ///
    /// * `wallet_name` - Name of the wallet
    /// * `hotkey_name` - Name of the hotkey
    ///
    /// # Returns
    ///
    /// A new wallet with a randomly generated keypair (not saved to disk)
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test_wallet", "test_hotkey").unwrap();
    /// assert!(!wallet.hotkey().as_str().is_empty());
    /// ```
    pub fn create_random(wallet_name: &str, hotkey_name: &str) -> Result<Self, BittensorError> {
        let (pair, _) = sr25519::Pair::generate();
        let path = Self::default_wallet_path()?;

        Ok(Self {
            name: wallet_name.to_string(),
            hotkey_name: hotkey_name.to_string(),
            path: path.join(wallet_name),
            hotkey_pair: pair,
            coldkey_pair: None,
        })
    }

    /// Create a wallet from a mnemonic phrase
    ///
    /// # Arguments
    ///
    /// * `wallet_name` - Name of the wallet
    /// * `hotkey_name` - Name of the hotkey
    /// * `mnemonic` - BIP39 mnemonic phrase (12 or 24 words)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
    /// let wallet = Wallet::from_mnemonic("test", "test", mnemonic)?;
    /// # Ok::<(), bittensor_rs::BittensorError>(())
    /// ```
    pub fn from_mnemonic(
        wallet_name: &str,
        hotkey_name: &str,
        mnemonic: &str,
    ) -> Result<Self, BittensorError> {
        let pair = sr25519::Pair::from_string(mnemonic, None).map_err(|e| {
            BittensorError::WalletError {
                message: format!("Invalid mnemonic: {e:?}"),
            }
        })?;

        let path =
            Self::default_wallet_path().unwrap_or_else(|_| PathBuf::from("~/.bittensor/wallets"));

        Ok(Self {
            name: wallet_name.to_string(),
            hotkey_name: hotkey_name.to_string(),
            path: path.join(wallet_name),
            hotkey_pair: pair,
            coldkey_pair: None,
        })
    }

    /// Create a wallet from a hex seed
    ///
    /// # Arguments
    ///
    /// * `wallet_name` - Name of the wallet
    /// * `hotkey_name` - Name of the hotkey
    /// * `seed_hex` - Hex-encoded seed (32 bytes, optionally prefixed with "0x")
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let seed = "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
    /// let wallet = Wallet::from_seed_hex("test", "test", seed).unwrap();
    /// ```
    pub fn from_seed_hex(
        wallet_name: &str,
        hotkey_name: &str,
        seed_hex: &str,
    ) -> Result<Self, BittensorError> {
        let hex_str = seed_hex.strip_prefix("0x").unwrap_or(seed_hex);
        let seed_bytes = hex::decode(hex_str).map_err(|e| BittensorError::WalletError {
            message: format!("Invalid hex seed: {e}"),
        })?;

        if seed_bytes.len() != 32 {
            return Err(BittensorError::WalletError {
                message: format!("Seed must be 32 bytes, got {} bytes", seed_bytes.len()),
            });
        }

        let mut seed_array = [0u8; 32];
        seed_array.copy_from_slice(&seed_bytes);
        let pair = sr25519::Pair::from_seed(&seed_array);

        let path =
            Self::default_wallet_path().unwrap_or_else(|_| PathBuf::from("~/.bittensor/wallets"));

        Ok(Self {
            name: wallet_name.to_string(),
            hotkey_name: hotkey_name.to_string(),
            path: path.join(wallet_name),
            hotkey_pair: pair,
            coldkey_pair: None,
        })
    }

    /// Get the hotkey address as a `Hotkey` type
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test", "test").unwrap();
    /// let hotkey = wallet.hotkey();
    /// println!("Address: {}", hotkey);
    /// ```
    pub fn hotkey(&self) -> Hotkey {
        let public = self.hotkey_pair.public();
        let account_id = AccountId::from(public.0);
        Hotkey::from_account_id(&account_id)
    }

    /// Get the hotkey as an AccountId
    pub fn account_id(&self) -> AccountId {
        AccountId::from(self.hotkey_pair.public().0)
    }

    /// Sign data with the hotkey
    ///
    /// # Arguments
    ///
    /// * `data` - The data to sign
    ///
    /// # Returns
    ///
    /// A 64-byte signature
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test", "test").unwrap();
    /// let signature = wallet.sign(b"hello world");
    /// assert_eq!(signature.len(), 64);
    /// ```
    pub fn sign(&self, data: &[u8]) -> Vec<u8> {
        let signature = self.hotkey_pair.sign(data);
        signature.0.to_vec()
    }

    /// Sign data and return hex-encoded signature
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test", "test").unwrap();
    /// let sig_hex = wallet.sign_hex(b"hello");
    /// assert_eq!(sig_hex.len(), 128); // 64 bytes = 128 hex chars
    /// ```
    pub fn sign_hex(&self, data: &[u8]) -> String {
        hex::encode(self.sign(data))
    }

    /// Get a subxt-compatible signer for this wallet
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test", "test").unwrap();
    /// let signer = wallet.signer();
    /// ```
    pub fn signer(&self) -> WalletSigner {
        WalletSigner::from_sp_core_pair(self.hotkey_pair.clone())
    }

    /// Get the underlying keypair (for advanced usage)
    pub fn keypair(&self) -> &sr25519::Pair {
        &self.hotkey_pair
    }

    /// Verify a signature against this wallet's hotkey
    ///
    /// # Arguments
    ///
    /// * `data` - The original data that was signed
    /// * `signature` - The 64-byte signature
    ///
    /// # Returns
    ///
    /// `true` if the signature is valid, `false` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// use bittensor_rs::wallet::Wallet;
    ///
    /// let wallet = Wallet::create_random("test", "test").unwrap();
    /// let message = b"hello world";
    /// let signature = wallet.sign(message);
    /// assert!(wallet.verify(message, &signature));
    /// ```
    pub fn verify(&self, data: &[u8], signature: &[u8]) -> bool {
        if signature.len() != 64 {
            return false;
        }

        let mut sig_array = [0u8; 64];
        sig_array.copy_from_slice(signature);
        let sig = sr25519::Signature::from_raw(sig_array);

        use sp_runtime::traits::Verify;
        sig.verify(data, &self.hotkey_pair.public())
    }

    /// Load and unlock the coldkey with a password
    ///
    /// The coldkey is stored in `<wallet_path>/coldkey` and is encrypted.
    ///
    /// # Arguments
    ///
    /// * `password` - The password to decrypt the coldkey
    ///
    /// # Returns
    ///
    /// * `Ok(())` if the coldkey was loaded and decrypted
    /// * `Err(BittensorError)` if loading or decryption failed
    pub fn unlock_coldkey(&mut self, password: &str) -> Result<(), BittensorError> {
        let coldkey_path = self.path.join("coldkey");

        if !coldkey_path.exists() {
            return Err(BittensorError::WalletError {
                message: format!("Coldkey file not found: {}", coldkey_path.display()),
            });
        }

        let keyfile_data = keyfile::load_encrypted_keyfile(&coldkey_path, password)?;
        let coldkey_pair = keyfile_data.to_keypair()?;

        self.coldkey_pair = Some(coldkey_pair);
        Ok(())
    }

    /// Check if the coldkey is unlocked
    pub fn is_coldkey_unlocked(&self) -> bool {
        self.coldkey_pair.is_some()
    }

    /// Get the coldkey address if unlocked
    pub fn coldkey(&self) -> Option<Hotkey> {
        self.coldkey_pair.as_ref().map(|pair| {
            let public = pair.public();
            let account_id = AccountId::from(public.0);
            Hotkey::from_account_id(&account_id)
        })
    }

    /// Get the default Bittensor wallet path
    fn default_wallet_path() -> Result<PathBuf, BittensorError> {
        home::home_dir()
            .map(|home| home.join(".bittensor").join("wallets"))
            .ok_or_else(|| BittensorError::WalletError {
                message: "Could not determine home directory".to_string(),
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_random_wallet() {
        let wallet = Wallet::create_random("test_wallet", "test_hotkey").unwrap();
        assert_eq!(wallet.name, "test_wallet");
        assert_eq!(wallet.hotkey_name, "test_hotkey");
        // Check that we have a valid hotkey
        let hotkey = wallet.hotkey();
        assert!(!hotkey.as_str().is_empty());
    }

    #[test]
    fn test_sign_and_verify() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        let message = b"test message";
        let signature = wallet.sign(message);

        assert_eq!(signature.len(), 64);
        assert!(wallet.verify(message, &signature));
    }

    #[test]
    fn test_sign_hex() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        let sig_hex = wallet.sign_hex(b"test");
        assert_eq!(sig_hex.len(), 128);
        assert!(hex::decode(&sig_hex).is_ok());
    }

    #[test]
    fn test_from_seed_hex() {
        let seed = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
        let wallet1 = Wallet::from_seed_hex("test", "test", seed).unwrap();
        let wallet2 = Wallet::from_seed_hex("test", "test", &format!("0x{}", seed)).unwrap();

        // Same seed should produce same hotkey
        assert_eq!(wallet1.hotkey().as_str(), wallet2.hotkey().as_str());
    }

    #[test]
    fn test_from_seed_hex_invalid() {
        // Too short
        let result = Wallet::from_seed_hex("test", "test", "0123");
        assert!(result.is_err());

        // Invalid hex
        let result = Wallet::from_seed_hex("test", "test", "not_hex_at_all!");
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_wrong_signature() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        let wrong_sig = vec![0u8; 64];
        assert!(!wallet.verify(b"test", &wrong_sig));
    }

    #[test]
    fn test_verify_wrong_length() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        let short_sig = vec![0u8; 32];
        assert!(!wallet.verify(b"test", &short_sig));
    }

    #[test]
    fn test_account_id() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        let account_id = wallet.account_id();
        let hotkey = wallet.hotkey();

        // Account ID and hotkey should be consistent
        assert_eq!(account_id.to_string(), hotkey.as_str());
    }

    #[test]
    fn test_coldkey_not_unlocked() {
        let wallet = Wallet::create_random("test", "test").unwrap();
        assert!(!wallet.is_coldkey_unlocked());
        assert!(wallet.coldkey().is_none());
    }
}