gsigner 2.0.0

Universal cryptographic signer supporting secp256k1 (Ethereum), ed25519, and sr25519 (Substrate)
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
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Shared keyring helpers and trait abstraction.

use crate::cli::{
    commands::{StorageLocationArgs, StorageLocationPathArgs},
    scheme::{KeyGenerationResult, KeyInfo, SchemeResult},
    util::storage_root,
};
use anyhow::Result;
pub const MAX_VANITY_ATTEMPTS: usize = 100_000;
use secrecy::ExposeSecret;
use std::path::PathBuf;

#[cfg(feature = "keyring")]
use crate::keyring::{KeyCodec, SubstrateKeystore};

/// Trait for scheme-specific secret display in CLI output.
///
/// Different schemes display their secrets differently:
/// - secp256k1: redacted display via `to_string()`
/// - ed25519/sr25519: hex-encoded bytes
#[cfg(feature = "keyring")]
pub trait PrivateKeyDisplay {
    type PrivateKey;

    /// Format the private key for CLI display.
    fn display_secret(private_key: &Self::PrivateKey) -> String;
}

/// Trait for scheme-specific vanity address matching.
///
/// Different schemes use different address formats:
/// - secp256k1: hex address prefix matching
/// - ed25519/sr25519: SS58 address prefix matching
#[cfg(feature = "keyring")]
pub trait VanityMatcher {
    type PrivateKey;
    type Address;

    /// Normalize user-provided prefix (remove 0x for hex, etc).
    fn normalize_prefix(prefix: &str) -> Result<String>;

    /// Check if a generated key's address matches the vanity prefix.
    fn matches_vanity(private_key: &Self::PrivateKey, prefix: &str) -> Result<bool>;
}

/// Trait for scheme-specific hex import handling.
///
/// Sr25519 has special handling for 32-byte seeds vs 96-byte keypairs.
#[cfg(feature = "keyring")]
pub trait HexImporter {
    type PrivateKey;

    /// Import private key from hex string.
    /// Default implementation uses KeyCodec::decode_private.
    fn import_hex(hex: &str) -> Result<Self::PrivateKey>;
}

/// Trait for scheme-specific public key extraction from keystore.
///
/// Sr25519 dynamically derives public key from private key on list.
#[cfg(feature = "keyring")]
pub trait PublicKeyExtractor<C: KeyCodec> {
    /// Extract public key hex from keystore entry.
    /// Default uses the stored public_key field.
    fn extract_public_key(keystore: &SubstrateKeystore<C>) -> String {
        keystore.public_key.clone()
    }
}

#[cfg(feature = "keyring")]
pub(crate) enum KeyringLocation {
    Disk(PathBuf),
    Memory,
}

#[cfg(feature = "keyring")]
impl KeyringLocation {
    fn display(&self) -> String {
        match self {
            KeyringLocation::Disk(path) => path.display().to_string(),
            KeyringLocation::Memory => "<memory>".to_string(),
        }
    }
}

#[cfg(feature = "keyring")]
pub struct KeyringEntry {
    pub public_key: String,
    pub address: String,
    pub name: String,
    pub secret: Option<String>,
}

#[cfg(feature = "keyring")]
pub trait KeyringOps {
    type Keyring;

    fn namespace() -> &'static str;
    fn load(path: PathBuf) -> Result<Self::Keyring>;
    fn memory() -> Self::Keyring;
    fn create(
        keyring: &mut Self::Keyring,
        name: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry>;
    fn add_hex(
        keyring: &mut Self::Keyring,
        name: &str,
        hex: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry>;
    fn import_suri(
        keyring: &mut Self::Keyring,
        name: &str,
        suri: &str,
        suri_password: Option<&str>,
        encryption_password: Option<&str>,
    ) -> Result<KeyringEntry>;
    fn list(keyring: &Self::Keyring) -> Result<Vec<KeyringEntry>>;
    fn vanity(
        keyring: &mut Self::Keyring,
        name: &str,
        prefix: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry>;
}

#[cfg(feature = "keyring")]
fn resolve_keyring_location(
    args: &StorageLocationArgs,
    namespace: &'static str,
) -> Result<KeyringLocation> {
    if args.memory {
        Ok(KeyringLocation::Memory)
    } else {
        let path = crate::keyring::resolve_namespaced_path(storage_root(&args.path), namespace);
        Ok(KeyringLocation::Disk(path))
    }
}

#[cfg(feature = "keyring")]
pub(crate) fn with_keyring_instance<K, LoadFn, MemFn, F, R>(
    storage: StorageLocationArgs,
    namespace: &'static str,
    load_fn: LoadFn,
    memory_fn: MemFn,
    f: F,
) -> Result<R>
where
    LoadFn: Fn(PathBuf) -> Result<K>,
    MemFn: Fn() -> K,
    F: FnOnce(KeyringLocation, &mut K, Option<String>) -> Result<R>,
{
    let password = storage
        .key_password
        .as_ref()
        .map(|p: &secrecy::SecretString| p.expose_secret().to_owned());
    let location = resolve_keyring_location(&storage, namespace)?;
    let mut keyring = match &location {
        KeyringLocation::Disk(path) => load_fn(path.clone())?,
        KeyringLocation::Memory => memory_fn(),
    };
    f(location, &mut keyring, password)
}

#[cfg(feature = "keyring")]
pub fn keygen_from_entry<S: crate::scheme::CryptoScheme>(
    entry: KeyringEntry,
) -> KeyGenerationResult {
    KeyGenerationResult {
        public_key: entry.public_key,
        address: entry.address,
        scheme: S::NAME.to_string(),
        secret: entry.secret,
        name: Some(entry.name),
    }
}

#[cfg(feature = "keyring")]
pub fn keyinfo_from_entry<S: crate::scheme::CryptoScheme>(entry: KeyringEntry) -> KeyInfo {
    KeyInfo {
        public_key: entry.public_key,
        address: entry.address,
        scheme: S::NAME.to_string(),
        secret: entry.secret,
        name: Some(entry.name),
    }
}

#[cfg(feature = "keyring")]
pub fn keyring_init<Ops: KeyringOps>(storage: StorageLocationArgs) -> Result<SchemeResult> {
    with_keyring_instance(
        storage,
        Ops::namespace(),
        Ops::load,
        Ops::memory,
        |location, _, _| {
            Ok(SchemeResult::Message(crate::cli::scheme::MessageResult {
                message: format!("Initialised keyring at {}", location.display()),
            }))
        },
    )
}

#[cfg(feature = "keyring")]
pub fn keyring_generate<Ops, S>(
    storage: StorageLocationArgs,
    name: String,
    show_secret: bool,
) -> Result<SchemeResult>
where
    Ops: KeyringOps,
    S: crate::scheme::CryptoScheme,
{
    with_keyring_instance(
        storage,
        Ops::namespace(),
        Ops::load,
        Ops::memory,
        |_location, keyring, password| {
            let entry = Ops::create(keyring, &name, password.as_deref())?;
            let mut entry = entry;
            if !show_secret {
                entry.secret = None;
            }
            Ok(SchemeResult::Generate(keygen_from_entry::<S>(entry)))
        },
    )
}

#[cfg(feature = "keyring")]
pub fn keyring_import<Ops, S, F>(
    storage: StorageLocationArgs,
    _name: String,
    show_secret: bool,
    as_import: bool,
    f: F,
) -> Result<SchemeResult>
where
    Ops: KeyringOps,
    S: crate::scheme::CryptoScheme,
    F: FnOnce(&mut Ops::Keyring, Option<&str>) -> Result<KeyringEntry>,
{
    with_keyring_instance(
        storage,
        Ops::namespace(),
        Ops::load,
        Ops::memory,
        |_location, keyring, password| {
            let entry = f(keyring, password.as_deref())?;
            let mut entry = entry;
            if !show_secret {
                entry.secret = None;
            }
            let keygen = keygen_from_entry::<S>(entry);
            if as_import {
                Ok(SchemeResult::Import(crate::cli::scheme::KeyImportResult {
                    public_key: keygen.public_key,
                    address: keygen.address,
                    scheme: keygen.scheme,
                    secret: keygen.secret,
                    name: keygen.name,
                }))
            } else {
                Ok(SchemeResult::Generate(keygen))
            }
        },
    )
}

#[cfg(feature = "keyring")]
pub fn keyring_list<Ops, S>(storage: StorageLocationArgs) -> Result<SchemeResult>
where
    Ops: KeyringOps,
    S: crate::scheme::CryptoScheme,
{
    with_keyring_instance(
        storage,
        Ops::namespace(),
        Ops::load,
        Ops::memory,
        |_location, keyring, _| {
            let keys: Vec<KeyInfo> = Ops::list(keyring)?
                .into_iter()
                .map(keyinfo_from_entry::<S>)
                .collect();
            Ok(SchemeResult::List(crate::cli::scheme::ListKeysResult {
                keys,
            }))
        },
    )
}

#[cfg(feature = "keyring")]
pub fn keyring_vanity<Ops, S>(
    storage: StorageLocationArgs,
    name: String,
    prefix: String,
    show_secret: bool,
) -> Result<SchemeResult>
where
    Ops: KeyringOps,
    S: crate::scheme::CryptoScheme,
{
    with_keyring_instance(
        storage,
        Ops::namespace(),
        Ops::load,
        Ops::memory,
        |_location, keyring, password| {
            let entry = Ops::vanity(keyring, &name, &prefix, password.as_deref())?;
            let mut entry = entry;
            if !show_secret {
                entry.secret = None;
            }
            Ok(SchemeResult::Generate(keygen_from_entry::<S>(entry)))
        },
    )
}

/// Trait for scheme-specific keyring command handling.
///
/// This trait allows schemes to provide custom implementations for commands
/// that have scheme-specific behavior (Sign, Show, Import) while sharing
/// the common infrastructure.
#[cfg(feature = "keyring")]
pub trait KeyringCommandHandler: KeyringOps {
    /// The scheme type for this handler.
    type Scheme: crate::scheme::CryptoScheme + crate::keyring::KeyringScheme;

    /// Handle the Sign command.
    fn handle_sign(
        storage: &StorageLocationArgs,
        public_key: &str,
        data: &str,
        prefix: &Option<String>,
        context: &Option<String>,
        contract: &Option<String>,
    ) -> Result<SchemeResult>;

    /// Handle the Show command.
    ///
    /// Default implementation parses public key from hex and uses `show_key_for_public`.
    /// Secp256k1 overrides this to also accept addresses.
    #[cfg(any(feature = "ed25519", feature = "sr25519"))]
    fn handle_show(
        storage: &StorageLocationArgs,
        key: &str,
        show_secret: bool,
    ) -> Result<SchemeResult> {
        use crate::scheme::CryptoScheme;
        let public = Self::Scheme::public_key_from_hex(key)?;
        let password = storage
            .key_password
            .as_ref()
            .map(|secret| secret.expose_secret().as_str());
        let result = crate::cli::storage::show_key_for_public::<Self::Scheme>(
            storage,
            public,
            show_secret,
            password,
        )?;
        Ok(SchemeResult::List(result))
    }

    /// Handle the Show command (required when only secp256k1 is enabled).
    #[cfg(not(any(feature = "ed25519", feature = "sr25519")))]
    fn handle_show(
        storage: &StorageLocationArgs,
        key: &str,
        show_secret: bool,
    ) -> Result<SchemeResult>;

    /// Handle the Import command.
    fn handle_import(
        storage: StorageLocationArgs,
        suri: Option<String>,
        seed: Option<String>,
        private_key: Option<String>,
        suri_password: Option<String>,
        name: Option<String>,
        show_secret: bool,
    ) -> Result<SchemeResult>;

    /// Handle the Clear command.
    fn handle_clear(storage: StorageLocationPathArgs) -> Result<SchemeResult> {
        let storage_args = storage.into_storage_args();
        let result = crate::cli::storage::clear_keys_command::<Self::Scheme>(&storage_args)?;
        Ok(SchemeResult::Clear(result))
    }
}

/// Execute a keyring command using a unified handler.
#[cfg(feature = "keyring")]
pub fn execute_keyring_command<H>(
    command: crate::cli::commands::SchemeKeyringCommands,
) -> Result<SchemeResult>
where
    H: KeyringCommandHandler,
{
    use crate::cli::commands::SchemeKeyringCommands;

    match command {
        SchemeKeyringCommands::Generate {
            storage,
            show_secret,
        } => keyring_generate::<H, H::Scheme>(storage, generate_key_name(), show_secret),
        SchemeKeyringCommands::Clear { storage } => H::handle_clear(storage),
        SchemeKeyringCommands::Sign {
            public_key,
            data,
            prefix,
            storage,
            context,
            contract,
        } => H::handle_sign(&storage, &public_key, &data, &prefix, &context, &contract),
        SchemeKeyringCommands::Show {
            storage,
            key,
            show_secret,
        } => H::handle_show(&storage, &key, show_secret),
        SchemeKeyringCommands::Vanity {
            storage,
            name,
            prefix,
            show_secret,
        } => keyring_vanity::<H, H::Scheme>(storage, name, prefix, show_secret),
        SchemeKeyringCommands::Init { storage } => keyring_init::<H>(storage),
        SchemeKeyringCommands::Create {
            storage,
            name,
            show_secret,
        } => keyring_generate::<H, H::Scheme>(storage, name, show_secret),
        SchemeKeyringCommands::Import {
            suri,
            seed,
            private_key,
            suri_password,
            name,
            storage,
            show_secret,
        } => H::handle_import(
            storage,
            suri,
            seed,
            private_key,
            suri_password,
            name,
            show_secret,
        ),
        SchemeKeyringCommands::List { storage } => {
            keyring_list::<H, H::Scheme>(storage.into_storage_args())
        }
    }
}

/// Generate a default key name based on timestamp.
#[cfg(feature = "keyring")]
fn generate_key_name() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    format!("key_{}", timestamp)
}

/// Generic `KeyringOps` implementation for schemes using `SubstrateKeystore<C>`.
///
/// This struct provides a unified implementation for all cryptographic schemes,
/// using the extension traits to handle scheme-specific behavior.
#[cfg(feature = "keyring")]
pub struct GenericKeyringOps<C, Ext> {
    _codec: std::marker::PhantomData<C>,
    _ext: std::marker::PhantomData<Ext>,
}

#[cfg(feature = "keyring")]
impl<C, Ext> Default for GenericKeyringOps<C, Ext> {
    fn default() -> Self {
        Self {
            _codec: std::marker::PhantomData,
            _ext: std::marker::PhantomData,
        }
    }
}

/// Combined extension trait for GenericKeyringOps.
#[cfg(feature = "keyring")]
pub trait KeyringOpsExt<C: KeyCodec>:
    PrivateKeyDisplay<PrivateKey = C::PrivateKey>
    + VanityMatcher<PrivateKey = C::PrivateKey>
    + HexImporter<PrivateKey = C::PrivateKey>
    + PublicKeyExtractor<C>
{
    /// The keyring namespace (e.g., "secp", "ed", "sr").
    const NAMESPACE: &'static str;
}

#[cfg(feature = "keyring")]
impl<C, Ext> KeyringOps for GenericKeyringOps<C, Ext>
where
    C: KeyCodec + 'static,
    Ext: KeyringOpsExt<C> + 'static,
{
    type Keyring = crate::keyring::Keyring<SubstrateKeystore<C>>;

    fn namespace() -> &'static str {
        Ext::NAMESPACE
    }

    fn load(path: PathBuf) -> Result<Self::Keyring> {
        Self::Keyring::load(path)
    }

    fn memory() -> Self::Keyring {
        Self::Keyring::memory()
    }

    fn create(
        keyring: &mut Self::Keyring,
        name: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry> {
        let (keystore, private_key) = keyring.create(name, password)?;
        Ok(KeyringEntry {
            public_key: Ext::extract_public_key(&keystore),
            address: keystore.address.clone(),
            name: keystore.name.clone(),
            secret: Some(Ext::display_secret(&private_key)),
        })
    }

    fn add_hex(
        keyring: &mut Self::Keyring,
        name: &str,
        hex: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry> {
        let private_key = Ext::import_hex(hex)?;
        let keystore = keyring.add(name, private_key.clone(), password)?;
        Ok(KeyringEntry {
            public_key: Ext::extract_public_key(&keystore),
            address: keystore.address.clone(),
            name: keystore.name.clone(),
            secret: Some(Ext::display_secret(&private_key)),
        })
    }

    fn import_suri(
        keyring: &mut Self::Keyring,
        name: &str,
        suri: &str,
        suri_password: Option<&str>,
        encryption_password: Option<&str>,
    ) -> Result<KeyringEntry> {
        let (keystore, private_key) =
            keyring.import_suri(name, suri, suri_password, encryption_password)?;
        Ok(KeyringEntry {
            public_key: Ext::extract_public_key(&keystore),
            address: keystore.address.clone(),
            name: keystore.name.clone(),
            secret: Some(Ext::display_secret(&private_key)),
        })
    }

    fn list(keyring: &Self::Keyring) -> Result<Vec<KeyringEntry>> {
        Ok(keyring
            .list()
            .iter()
            .map(|ks| KeyringEntry {
                public_key: Ext::extract_public_key(ks),
                address: ks.address.clone(),
                name: ks.name.clone(),
                secret: None,
            })
            .collect())
    }

    fn vanity(
        keyring: &mut Self::Keyring,
        name: &str,
        prefix: &str,
        password: Option<&str>,
    ) -> Result<KeyringEntry> {
        let normalized = Ext::normalize_prefix(prefix)?;
        let mut attempts = 0usize;
        let private_key = loop {
            if attempts >= MAX_VANITY_ATTEMPTS {
                anyhow::bail!("vanity search exceeded maximum attempts ({MAX_VANITY_ATTEMPTS})");
            }
            let candidate = C::random_private()?;
            if normalized.is_empty() || Ext::matches_vanity(&candidate, &normalized)? {
                break candidate;
            }
            attempts += 1;
        };
        let keystore = keyring.add(name, private_key.clone(), password)?;
        Ok(KeyringEntry {
            public_key: Ext::extract_public_key(&keystore),
            address: keystore.address.clone(),
            name: keystore.name.clone(),
            secret: Some(Ext::display_secret(&private_key)),
        })
    }
}