Skip to content

Commit 3860066

Browse files
committed
chore: add docs for 'BurnchainOpSigner', #6248
1 parent 78abf69 commit 3860066

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

stacks-node/src/operations.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,79 @@ use stacks::burnchains::PrivateKey;
22
use stacks_common::util::hash::hex_bytes;
33
use stacks_common::util::secp256k1::{MessageSignature, Secp256k1PrivateKey, Secp256k1PublicKey};
44

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).
510
pub struct BurnchainOpSigner {
11+
/// The Secp256k1 private key used for signing operations.
612
secret_key: Secp256k1PrivateKey,
13+
/// Indicates whether the signer has been disposed and can no longer be used for signing.
714
is_disposed: bool,
815
}
916

1017
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 {
1228
BurnchainOpSigner {
1329
secret_key,
1430
is_disposed: false,
1531
}
1632
}
1733

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.
1841
pub fn get_sk_as_wif(&self) -> String {
1942
let hex_encoded = self.secret_key.to_hex();
2043
let mut as_bytes = hex_bytes(&hex_encoded).unwrap();
2144
as_bytes.insert(0, 0x80);
2245
stacks_common::address::b58::check_encode_slice(&as_bytes)
2346
}
2447

48+
/// Returns the private key encoded as a hexadecimal string.
49+
///
50+
/// # Returns
51+
///
52+
/// A hex-encoded string representation of the private key.
2553
pub fn get_sk_as_hex(&self) -> String {
2654
self.secret_key.to_hex()
2755
}
2856

57+
/// Derives and returns the public key associated with the private key.
58+
///
59+
/// # Returns
60+
///
61+
/// A `Secp256k1PublicKey` corresponding to the private key.
2962
pub fn get_public_key(&mut self) -> Secp256k1PublicKey {
3063
Secp256k1PublicKey::from_private(&self.secret_key)
3164
}
3265

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.
3378
pub fn sign_message(&mut self, hash: &[u8]) -> Option<MessageSignature> {
3479
if self.is_disposed {
3580
debug!("Signer is disposed");
@@ -47,6 +92,9 @@ impl BurnchainOpSigner {
4792
Some(signature)
4893
}
4994

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.
5098
pub fn dispose(&mut self) {
5199
self.is_disposed = true;
52100
}
@@ -74,9 +122,7 @@ impl BurnchainOpSigner {
74122

75123
#[cfg(test)]
76124
mod tests {
77-
use stacks_common::util::secp256k1::Secp256k1PrivateKey;
78-
79-
use super::BurnchainOpSigner;
125+
use super::*;
80126

81127
#[test]
82128
fn test_get_secret_key_as_wif() {

0 commit comments

Comments
 (0)