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
use std::fmt::{self, Display, Formatter};
#[cfg(feature = "keygen")]
use secp256k1::rand::thread_rng;
use secp256k1::{Secp256k1, Signing};
#[derive(Clone, PartialEq, Eq, Debug, StrictEncode, StrictDecode)]
pub struct LocalNode {
private_key: secp256k1::SecretKey,
public_key: secp256k1::PublicKey,
}
impl LocalNode {
#[cfg(feature = "keygen")]
pub fn new<C: Signing>(secp: &Secp256k1<C>) -> Self {
let mut rng = thread_rng();
let private_key = secp256k1::SecretKey::new(&mut rng);
let public_key =
secp256k1::PublicKey::from_secret_key(&secp, &private_key);
Self {
private_key,
public_key,
}
}
#[inline]
pub fn with(
private_key: secp256k1::SecretKey,
public_key: secp256k1::PublicKey,
) -> Self {
Self {
private_key,
public_key,
}
}
#[inline]
pub fn node_id(&self) -> secp256k1::PublicKey {
self.public_key
}
#[inline]
pub fn private_key(&self) -> secp256k1::SecretKey {
self.private_key
}
#[inline]
pub fn sign<C: Signing>(
&self,
secp: &Secp256k1<C>,
message: &secp256k1::Message,
) -> secp256k1::Signature {
secp.sign(message, &self.private_key)
}
}
impl Display for LocalNode {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if f.alternate() {
write!(f, "LocalNode({:#})", self.node_id())
} else {
write!(f, "{}", self.node_id())
}
}
}