5
5
use bitcoin:: blockdata:: transaction:: { OutPoint , TxOut } ;
6
6
use bitcoin:: blockdata:: script:: Script ;
7
7
8
- use secp256k1:: key:: SecretKey ;
8
+ use secp256k1:: key:: { SecretKey , PublicKey } ;
9
+ use secp256k1:: Secp256k1 ;
10
+
11
+ use crypto:: hkdf:: { hkdf_extract, hkdf_expand} ;
12
+
13
+ use util:: sha2:: Sha256 ;
9
14
10
15
/// When on-chain outputs are created by rust-lightning an event is generated which informs the
11
16
/// user thereof. This enum descibes the format of the output and provides the OutPoint.
@@ -32,3 +37,81 @@ pub enum SpendableOutputDescriptor {
32
37
to_self_delay : u16 ,
33
38
}
34
39
}
40
+
41
+ /// A trait to describe an object which can get user secrets and key material.
42
+ pub trait KeysInterface : Send + Sync {
43
+ /// Get node secret key (aka node_id or network_key)
44
+ fn get_node_secret ( & self ) -> SecretKey ;
45
+ /// Get destination redeemScript to encumber static protocol exit points.
46
+ fn get_destination_script ( & self ) -> Script ;
47
+ /// Get shutdown_pubkey to use as PublicKey at channel closure
48
+ fn get_shutdown_pubkey ( & self ) -> PublicKey ;
49
+ /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
50
+ /// restarted with some stale data!
51
+ fn get_channel_keys ( & self , inbound : bool ) -> ChannelKeys ;
52
+ }
53
+
54
+ /// Set of lightning keys needed to operate a channel as described in BOLT 3
55
+ pub struct ChannelKeys {
56
+ /// Private key of anchor tx
57
+ pub funding_key : SecretKey ,
58
+ /// Local secret key for blinded revocation pubkey
59
+ pub revocation_base_key : SecretKey ,
60
+ /// Local secret key used in commitment tx htlc outputs
61
+ pub payment_base_key : SecretKey ,
62
+ /// Local secret key used in HTLC tx
63
+ pub delayed_payment_base_key : SecretKey ,
64
+ /// Local htlc secret key used in commitment tx htlc outputs
65
+ pub htlc_base_key : SecretKey ,
66
+ /// Local secret key used for closing tx
67
+ pub channel_close_key : SecretKey ,
68
+ /// Local secret key used in justice tx, claim tx and preimage tx outputs
69
+ pub channel_monitor_claim_key : SecretKey ,
70
+ /// Commitment seed
71
+ pub commitment_seed : [ u8 ; 32 ] ,
72
+ }
73
+
74
+ impl ChannelKeys {
75
+ /// Generate a set of lightning keys needed to operate a channel by HKDF-expanding a given
76
+ /// random 32-byte seed
77
+ pub fn new_from_seed ( seed : & [ u8 ; 32 ] ) -> ChannelKeys {
78
+ let mut prk = [ 0 ; 32 ] ;
79
+ hkdf_extract ( Sha256 :: new ( ) , b"rust-lightning key gen salt" , seed, & mut prk) ;
80
+ let secp_ctx = Secp256k1 :: without_caps ( ) ;
81
+
82
+ let mut okm = [ 0 ; 32 ] ;
83
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning funding key info" , & mut okm) ;
84
+ let funding_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
85
+
86
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning revocation base key info" , & mut okm) ;
87
+ let revocation_base_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
88
+
89
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning payment base key info" , & mut okm) ;
90
+ let payment_base_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
91
+
92
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning delayed payment base key info" , & mut okm) ;
93
+ let delayed_payment_base_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
94
+
95
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning htlc base key info" , & mut okm) ;
96
+ let htlc_base_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
97
+
98
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning channel close key info" , & mut okm) ;
99
+ let channel_close_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
100
+
101
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning channel monitor claim key info" , & mut okm) ;
102
+ let channel_monitor_claim_key = SecretKey :: from_slice ( & secp_ctx, & okm) . expect ( "Sha256 is broken" ) ;
103
+
104
+ hkdf_expand ( Sha256 :: new ( ) , & prk, b"rust-lightning local commitment seed info" , & mut okm) ;
105
+
106
+ ChannelKeys {
107
+ funding_key : funding_key,
108
+ revocation_base_key : revocation_base_key,
109
+ payment_base_key : payment_base_key,
110
+ delayed_payment_base_key : delayed_payment_base_key,
111
+ htlc_base_key : htlc_base_key,
112
+ channel_close_key : channel_close_key,
113
+ channel_monitor_claim_key : channel_monitor_claim_key,
114
+ commitment_seed : okm
115
+ }
116
+ }
117
+ }
0 commit comments