btwallet 4.0.1

Bittensor wallet — Substrate-based key management, encryption, and signing.
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
use base64::{engine::general_purpose, Engine as _};
use bip39::Mnemonic;
use schnorrkel::{PublicKey, SecretKey};
use scrypt::{scrypt, Params as ScryptParams};
use serde::{Deserialize, Serialize};
use sodiumoxide::crypto::secretbox;
use sodiumoxide::crypto::secretbox::{Key, Nonce};
use sp_core::crypto::Ss58Codec;
use sp_core::{sr25519, ByteArray, Pair};
use std::fmt;

const PKCS8_HEADER: &[u8] = &[48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32];
const PKCS8_DIVIDER: &[u8] = &[161, 35, 3, 33, 0];
const SEC_LENGTH: usize = 64;
const PUB_LENGTH: usize = 32;

#[derive(Serialize, Deserialize, Debug)]
struct Encoding {
    content: Vec<String>,
    #[serde(rename = "type")]
    enc_type: Vec<String>,
    version: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Meta {
    #[serde(rename = "genesisHash")]
    genesis_hash: Option<String>,
    name: String,
    #[serde(rename = "whenCreated")]
    when_created: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug)]
struct JsonStructure {
    encoded: String,
    encoding: Encoding,
    address: String,
    meta: Meta,
}

#[derive(Clone)]
pub struct Keypair {
    ss58_address: Option<String>,
    public_key: Option<String>,
    private_key: Option<String>,
    ss58_format: u8,
    seed_hex: Option<Vec<u8>>,
    crypto_type: u8,
    mnemonic: Option<String>,
    pair: Option<sr25519::Pair>,
}

impl Keypair {
    /// Creates a new Keypair instance.
    ///
    ///     Arguments:
    ///         ss58_address (Option<String>): Optional SS58-formatted address.
    ///         public_key (Option<String>): Optional public key as hex string.
    ///         private_key (Option<String>): Optional private key as hex string.
    ///         ss58_format (u8): The SS58 format number for address encoding.
    ///         seed_hex (Option<Vec<u8>>): Optional seed bytes.
    ///         crypto_type (u8): The cryptographic algorithm type (1 for SR25519).
    ///     Returns:
    ///         keypair (Keypair): A new Keypair instance.
    pub fn new(
        ss58_address: Option<String>,
        public_key: Option<String>,
        private_key: Option<String>,
        ss58_format: u8,
        seed_hex: Option<Vec<u8>>,
        crypto_type: u8,
    ) -> Result<Self, String> {
        if crypto_type != 1 {
            return Err(format!("Unsupported crypto type: {}.", crypto_type));
        }

        let mut ss58_address_res = ss58_address.clone();
        let mut public_key_res = public_key;

        if let Some(private_key_str) = &private_key {
            let private_key_bytes =
                hex::decode(private_key_str.trim_start_matches("0x")).expect("");

            if private_key_bytes.len() != 64 {
                return Err("Secret key should be 64 bytes long.".to_string());
            }

            // TODO: add logic creation pair from private key
        }

        // if public_key is passed
        if let Some(public_key_str) = &public_key_res {
            let public_key_vec = hex::decode(public_key_str.trim_start_matches("0x"))
                .map_err(|e| format!("Invalid `public_key` string: {}", e))?;

            let public_key_array: [u8; 32] = public_key_vec
                .try_into()
                .map_err(|_| "Public key must be 32 bytes long.")?;

            let public_key = sr25519::Public::from_raw(public_key_array);

            ss58_address_res = Option::from(public_key.to_ss58check());
        }

        // If ss58_address is passed, decode the public key
        if let Some(ss58_address_str) = ss58_address.clone() {
            let public_key = sr25519::Public::from_ss58check(&ss58_address_str)
                .map_err(|e| format!("Invalid SS58 address: {}", e))?;

            public_key_res = Some(hex::encode(public_key.to_raw()));
        }

        let kp = Keypair {
            ss58_address: ss58_address_res,
            public_key: public_key_res,
            private_key,
            ss58_format,
            seed_hex,
            crypto_type,
            mnemonic: None,
            pair: None,
        };

        // If public_key is missing (ss58_address wasn't created), return an error
        if kp.public_key.is_none() {
            return Err("No SS58 formatted address or public key provided.".to_string());
        }
        Ok(kp)
    }

    fn __str__(&self) -> Result<String, String> {
        match self.ss58_address() {
            Some(address) => Ok(format!("<Keypair (address={})>", address)),
            None => Ok("<Keypair (address=None)>".to_string()),
        }
    }

    fn __repr__(&self) -> Result<String, String> {
        self.__str__()
    }

    /// Generates a new mnemonic phrase.
    ///
    ///     Arguments:
    ///         n_words (usize): The number of words in the mnemonic (e.g., 12, 15, 18, 21, 24).
    ///     Returns:
    ///         mnemonic (String): The generated mnemonic phrase.
    pub fn generate_mnemonic(n_words: usize) -> Result<String, String> {
        let mnemonic = Mnemonic::generate(n_words).map_err(|e| e.to_string())?;
        Ok(mnemonic.to_string())
    }

    /// Creates a Keypair from a mnemonic phrase.
    ///
    ///     Arguments:
    ///         mnemonic (str): The mnemonic phrase to create the keypair from.
    ///     Returns:
    ///         keypair (Keypair): The Keypair created from the mnemonic.
    pub fn create_from_mnemonic(mnemonic: &str) -> Result<Self, String> {
        let (pair, seed_vec) =
            sr25519::Pair::from_phrase(mnemonic, None).map_err(|e| e.to_string())?;

        let kp = Keypair {
            mnemonic: Some(mnemonic.to_string()),
            seed_hex: Some(seed_vec.to_vec()),
            pair: Some(pair),
            ..Default::default()
        };
        Ok(kp)
    }

    /// Creates a Keypair from a seed.
    ///
    ///     Arguments:
    ///         seed (Vec<u8>): The seed bytes to create the keypair from.
    ///     Returns:
    ///         keypair (Keypair): The Keypair created from the seed.
    pub fn create_from_seed(seed: Vec<u8>) -> Result<Self, String> {
        let pair = sr25519::Pair::from_seed_slice(&seed)
            .map_err(|e| format!("Failed to create pair from seed: {}", e))?;

        let kp = Keypair {
            seed_hex: Some(seed.to_vec()),
            pair: Some(pair),
            ..Default::default()
        };
        Ok(kp)
    }

    /// Creates a Keypair from a private key.
    ///
    ///     Arguments:
    ///         private_key (str): The private key as hex string to create the keypair from.
    ///     Returns:
    ///         keypair (Keypair): The Keypair created from the private key.
    pub fn create_from_private_key(private_key: &str) -> Result<Self, String> {
        let private_key_vec = hex::decode(private_key.trim_start_matches("0x"))
            .map_err(|e| format!("Invalid `private_key` string: {}", e))?;

        let pair = sr25519::Pair::from_seed_slice(&private_key_vec)
            .map_err(|e| format!("Failed to create pair from private key: {}", e))?;

        let kp = Keypair {
            pair: Some(pair),
            ..Default::default()
        };
        Ok(kp)
    }

    /// Creates a Keypair from encrypted JSON data.
    ///
    ///     Arguments:
    ///         json_data (str): The encrypted JSON data containing the keypair.
    ///         passphrase (str): The passphrase to decrypt the JSON data.
    ///     Returns:
    ///         keypair (Keypair): The Keypair created from the encrypted JSON.
    pub fn create_from_encrypted_json(
        json_data: &str,
        passphrase: &str,
    ) -> Result<Keypair, String> {
        /// rust version of python .rjust
        fn pad_right(mut data: Vec<u8>, total_len: usize, pad_byte: u8) -> Vec<u8> {
            if data.len() < total_len {
                let pad_len = total_len - data.len();
                data.extend(vec![pad_byte; pad_len]);
            }
            data
        }

        pub fn pair_from_ed25519_secret_key(secret: &[u8], pubkey: &[u8]) -> ([u8; 64], [u8; 32]) {
            match (
                SecretKey::from_ed25519_bytes(secret),
                PublicKey::from_bytes(pubkey),
            ) {
                (Ok(s), Ok(k)) => (s.to_bytes(), k.to_bytes()),
                _ => panic!("Invalid secret or pubkey provided."),
            }
        }

        /// Decodes a PKCS8-encoded key pair from the provided byte slice.
        /// Returns a tuple containing the private key and public key as vectors of bytes.
        fn decode_pkcs8(
            ciphertext: &[u8],
        ) -> Result<([u8; SEC_LENGTH], [u8; PUB_LENGTH]), &'static str> {
            let mut current_offset = 0;
            let header = &ciphertext[current_offset..current_offset + PKCS8_HEADER.len()];
            if header != PKCS8_HEADER {
                return Err("Invalid Pkcs8 header found in body");
            }
            current_offset += PKCS8_HEADER.len();
            let secret_key = &ciphertext[current_offset..current_offset + SEC_LENGTH];
            let mut secret_key_array = [0u8; SEC_LENGTH];
            secret_key_array.copy_from_slice(secret_key);
            current_offset += SEC_LENGTH;
            let divider = &ciphertext[current_offset..current_offset + PKCS8_DIVIDER.len()];
            if divider != PKCS8_DIVIDER {
                return Err("Invalid Pkcs8 divider found in body");
            }
            current_offset += PKCS8_DIVIDER.len();
            let public_key = &ciphertext[current_offset..current_offset + PUB_LENGTH];
            let mut public_key_array = [0u8; PUB_LENGTH];
            public_key_array.copy_from_slice(public_key);
            Ok((secret_key_array, public_key_array))
        }

        let json_data: JsonStructure = serde_json::from_str(json_data).unwrap();

        if json_data.encoding.version != "3" {
            return Err("Unsupported JSON format".to_string());
        }

        let mut encrypted = general_purpose::STANDARD
            .decode(json_data.encoded)
            .map_err(|e| e.to_string())?;

        let password = if json_data.encoding.enc_type.contains(&"scrypt".to_string()) {
            let salt = &encrypted[0..32];
            let n = u32::from_le_bytes(encrypted[32..36].try_into().unwrap());
            let p = u32::from_le_bytes(encrypted[36..40].try_into().unwrap());
            let r = u32::from_le_bytes(encrypted[40..44].try_into().unwrap());
            let log_n: u8 = n.ilog2() as u8;

            let params = ScryptParams::new(log_n, r, p, 32).map_err(|e| e.to_string())?;
            let mut derived_key = vec![0u8; 32];
            scrypt(passphrase.as_bytes(), salt, &params, &mut derived_key)
                .map_err(|e| e.to_string())?;
            encrypted = encrypted[44..].to_vec();
            derived_key
        } else {
            let mut derived_key = passphrase.as_bytes().to_vec();
            derived_key = pad_right(derived_key, 32, 0x00);
            derived_key
        };

        let nonce_bytes = &encrypted[0..24];
        let nonce = Nonce::from_slice(nonce_bytes)
            .ok_or("Invalid nonce length")
            .map_err(|e| e.to_string())?;
        let message = &encrypted[24..];

        let key = Key::from_slice(&password).ok_or("Invalid key length")?;
        let decrypted_data = secretbox::open(message, &nonce, &key)
            .map_err(|_| "Failed to decrypt data".to_string())?;
        let (private_key, public_key) =
            decode_pkcs8(&decrypted_data).map_err(|_| "Failed to decode PKCS8 data".to_string())?;

        let (secret, converted_public_key) =
            pair_from_ed25519_secret_key(&private_key[..], &public_key[..]);

        let keypair = match json_data.encoding.content.iter().any(|c| c == "sr25519") {
            true => {
                assert_eq!(public_key, converted_public_key);
                Keypair::create_from_private_key(&hex::encode(secret))
            }
            _ => return Err("Unsupported keypair type.".to_string()),
        };

        keypair
    }

    /// Creates a Keypair from a URI string.
    ///
    ///     Arguments:
    ///         uri (str): The URI string to create the keypair from.
    ///     Returns:
    ///         keypair (Keypair): The Keypair created from the URI.
    pub fn create_from_uri(uri: &str) -> Result<Self, String> {
        let pair = Pair::from_string(uri, None).map_err(|e| e.to_string())?;

        let kp = Keypair {
            pair: Some(pair),
            ..Default::default()
        };
        Ok(kp)
    }

    /// Signs data with the keypair's private key.
    ///
    ///     Arguments:
    ///         data (Vec<u8>): The data to sign as bytes.
    ///     Returns:
    ///         signature (Vec<u8>): The signature as bytes.
    pub fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, String> {
        // Check if private key exists
        let pair = self
            .pair
            .as_ref()
            .ok_or_else(|| "No private key set to create signatures".to_string())?;

        // Generate a signature depending on the type of cryptographic key
        let signature = match self.crypto_type {
            1 => {
                // SR25519
                pair.sign(&data)
            }
            _ => {
                return Err("Crypto type not supported.".to_string());
            }
        };

        Ok(signature.to_vec())
    }

    /// Verifies a signature against data using the keypair's public key.
    ///
    ///     Arguments:
    ///         data (Vec<u8>): The data that was signed as bytes.
    ///         signature (Vec<u8>): The signature to verify as bytes.
    ///     Returns:
    ///         verified (bool): ``True`` if the signature is valid, ``False`` otherwise.
    pub fn verify(&self, data: Vec<u8>, signature: Vec<u8>) -> Result<bool, String> {
        // Check if public key exists
        let public_key = if let Some(public_key_str) = &self.public_key {
            hex::decode(public_key_str.trim_start_matches("0x"))
                .map_err(|e| format!("Invalid `public_key` string: {:?}", e))?
        } else if let Some(pair) = &self.pair {
            pair.public().to_vec()
        } else {
            return Err("No public key or pair available.".to_string());
        };

        let public = sr25519::Public::from_raw(
            <[u8; 32]>::try_from(public_key)
                .map_err(|e| format!("Invalid public key length: {:?}", e))?,
        );

        // Convert signature bytes to the type expected by the verify function
        let signature = sr25519::Signature::from_slice(&signature)
            .map_err(|_| "Invalid signature".to_string())?;

        // Verify signature depending on the type of crypto key
        let verified = match self.crypto_type {
            1 => {
                // SR25519
                sr25519::Pair::verify(&signature, &data, &public)
            }
            _ => {
                return Err("Crypto type not supported".to_string());
            }
        };

        // If not verified, try to verify with data wrapper
        if !verified {
            let wrapped_data = [b"<Bytes>", data.as_slice(), b"</Bytes>"].concat();
            let verified_wrapped = match self.crypto_type {
                1 => {
                    // SR25519
                    sr25519::Pair::verify(&signature, wrapped_data, &public)
                }
                _ => {
                    return Err("Crypto type not supported".to_string());
                }
            };

            Ok(verified_wrapped)
        } else {
            Ok(verified)
        }
    }

    /// Returns the SS58 address of the keypair.
    pub fn ss58_address(&self) -> Option<String> {
        match &self.pair {
            Some(pair) => {
                let ss58_address = pair.public().to_ss58check();
                Some(ss58_address)
            }
            None => self.ss58_address.clone(),
        }
    }

    /// Returns the public key of the keypair as bytes.
    pub fn public_key(&self) -> Result<Option<Vec<u8>>, String> {
        if let Some(pair) = &self.pair {
            let public_key_vec = pair.public().to_vec();
            Ok(Some(public_key_vec))
        } else if let Some(public_key) = &self.public_key {
            let public_key_vec = hex::decode(public_key.trim_start_matches("0x"))
                .map_err(|e| format!("Invalid `public_key` string: {}", e))?;
            Ok(Some(public_key_vec))
        } else {
            Ok(None)
        }
    }

    /// Returns the SS58 format number.
    pub fn ss58_format(&self) -> u8 {
        self.ss58_format
    }

    pub fn seed_hex(&self) -> Option<Vec<u8>> {
        self.seed_hex.clone()
    }

    /// Returns the cryptographic algorithm type.
    pub fn crypto_type(&self) -> u8 {
        self.crypto_type
    }

    /// Sets the cryptographic algorithm type.
    ///
    ///     Arguments:
    ///         crypto_type (u8): The cryptographic algorithm type (1 for SR25519).
    pub fn set_crypto_type(&mut self, crypto_type: u8) {
        self.crypto_type = crypto_type;
    }

    /// Returns the mnemonic phrase of the keypair.
    pub fn mnemonic(&self) -> Option<String> {
        self.mnemonic.clone()
    }

    /// Returns the private key of the keypair as bytes.
    pub fn private_key(&self) -> Result<Option<Vec<u8>>, String> {
        match &self.pair {
            Some(pair) => {
                let seed = pair.to_raw_vec();
                Ok(Some(seed))
            }
            None => {
                if let Some(private_key) = &self.private_key {
                    Ok(Some(private_key.as_bytes().to_vec()))
                } else {
                    Ok(None)
                }
            }
        }
    }
}

// Default values for Keypair
impl Default for Keypair {
    fn default() -> Self {
        Keypair {
            ss58_address: None,
            public_key: None,
            private_key: None,
            ss58_format: 42,
            seed_hex: None,
            crypto_type: 1,
            mnemonic: None,
            pair: None,
        }
    }
}

impl fmt::Display for Keypair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let address = self.ss58_address();
        match address {
            Some(addr) => write!(f, "<Keypair (address={})>", addr),
            None => write!(f, "<Keypair (address=None)>"),
        }
    }
}

impl fmt::Debug for Keypair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let address = self.ss58_address();
        match address {
            Some(addr) => write!(f, "<Keypair (address={})>", addr),
            None => write!(f, "<Keypair (address=None)>"),
        }
    }
}