@@ -2,34 +2,79 @@ use stacks::burnchains::PrivateKey;
2
2
use stacks_common:: util:: hash:: hex_bytes;
3
3
use stacks_common:: util:: secp256k1:: { MessageSignature , Secp256k1PrivateKey , Secp256k1PublicKey } ;
4
4
5
+ /// A signer used for burnchain operations, which manages a private key and provides
6
+ /// functionality to derive public keys, sign messages, and export keys in different formats.
7
+ ///
8
+ /// The signer can be "disposed" to prevent further use of the private key (e.g., for security
9
+ /// or lifecycle management).
5
10
pub struct BurnchainOpSigner {
11
+ /// The Secp256k1 private key used for signing operations.
6
12
secret_key : Secp256k1PrivateKey ,
13
+ /// Indicates whether the signer has been disposed and can no longer be used for signing.
7
14
is_disposed : bool ,
8
15
}
9
16
10
17
impl BurnchainOpSigner {
11
- pub fn new ( secret_key : Secp256k1PrivateKey ) -> BurnchainOpSigner {
18
+ /// Creates a new `BurnchainOpSigner` from the given private key.
19
+ ///
20
+ /// # Arguments
21
+ ///
22
+ /// * `secret_key` - A Secp256k1 private key used for signing.
23
+ ///
24
+ /// # Returns
25
+ ///
26
+ /// A new instance of `BurnchainOpSigner`.
27
+ pub fn new ( secret_key : Secp256k1PrivateKey ) -> Self {
12
28
BurnchainOpSigner {
13
29
secret_key,
14
30
is_disposed : false ,
15
31
}
16
32
}
17
33
34
+ /// Returns the private key encoded as a Wallet Import Format (WIF) string.
35
+ ///
36
+ /// This format is commonly used for exporting private keys in Bitcoin-related systems.
37
+ ///
38
+ /// # Returns
39
+ ///
40
+ /// A WIF-encoded string representation of the private key.
18
41
pub fn get_sk_as_wif ( & self ) -> String {
19
42
let hex_encoded = self . secret_key . to_hex ( ) ;
20
43
let mut as_bytes = hex_bytes ( & hex_encoded) . unwrap ( ) ;
21
44
as_bytes. insert ( 0 , 0x80 ) ;
22
45
stacks_common:: address:: b58:: check_encode_slice ( & as_bytes)
23
46
}
24
47
48
+ /// Returns the private key encoded as a hexadecimal string.
49
+ ///
50
+ /// # Returns
51
+ ///
52
+ /// A hex-encoded string representation of the private key.
25
53
pub fn get_sk_as_hex ( & self ) -> String {
26
54
self . secret_key . to_hex ( )
27
55
}
28
56
57
+ /// Derives and returns the public key associated with the private key.
58
+ ///
59
+ /// # Returns
60
+ ///
61
+ /// A `Secp256k1PublicKey` corresponding to the private key.
29
62
pub fn get_public_key ( & mut self ) -> Secp256k1PublicKey {
30
63
Secp256k1PublicKey :: from_private ( & self . secret_key )
31
64
}
32
65
66
+ /// Signs the given message hash using the private key.
67
+ ///
68
+ /// If the signer has been disposed, no signature will be produced.
69
+ ///
70
+ /// # Arguments
71
+ ///
72
+ /// * `hash` - A byte slice representing the hash of the message to sign.
73
+ /// This must be exactly **32 bytes** long, as required by the Secp256k1 signing algorithm.
74
+ /// # Returns
75
+ ///
76
+ /// `Some(MessageSignature)` if signing was successful, or `None` if the signer
77
+ /// is disposed or signing failed.
33
78
pub fn sign_message ( & mut self , hash : & [ u8 ] ) -> Option < MessageSignature > {
34
79
if self . is_disposed {
35
80
debug ! ( "Signer is disposed" ) ;
@@ -47,16 +92,17 @@ impl BurnchainOpSigner {
47
92
Some ( signature)
48
93
}
49
94
95
+ /// Marks the signer as disposed, preventing any further signing operations.
96
+ ///
97
+ /// Once disposed, the private key can no longer be used to sign messages.
50
98
pub fn dispose ( & mut self ) {
51
99
self . is_disposed = true ;
52
100
}
53
101
}
54
102
55
103
#[ cfg( test) ]
56
104
mod tests {
57
- use stacks_common:: util:: secp256k1:: Secp256k1PrivateKey ;
58
-
59
- use super :: BurnchainOpSigner ;
105
+ use super :: * ;
60
106
61
107
#[ test]
62
108
fn test_get_secret_key_as_wif ( ) {
0 commit comments