1
+ //! Various utilities for building scripts and deriving keys related to channels. These are
2
+ //! largely of interest for those implementing chain::keysinterface::ChannelKeys message signing
3
+ //! by hand.
4
+
1
5
use bitcoin:: blockdata:: script:: { Script , Builder } ;
2
6
use bitcoin:: blockdata:: opcodes;
3
7
use bitcoin:: blockdata:: transaction:: { TxIn , TxOut , OutPoint , Transaction } ;
@@ -14,13 +18,13 @@ use secp256k1::key::{PublicKey,SecretKey};
14
18
use secp256k1:: Secp256k1 ;
15
19
use secp256k1;
16
20
17
- pub const HTLC_SUCCESS_TX_WEIGHT : u64 = 703 ;
18
- pub const HTLC_TIMEOUT_TX_WEIGHT : u64 = 663 ;
21
+ pub ( super ) const HTLC_SUCCESS_TX_WEIGHT : u64 = 703 ;
22
+ pub ( super ) const HTLC_TIMEOUT_TX_WEIGHT : u64 = 663 ;
19
23
20
24
// Various functions for key derivation and transaction creation for use within channels. Primarily
21
25
// used in Channel and ChannelMonitor.
22
26
23
- pub fn build_commitment_secret ( commitment_seed : & [ u8 ; 32 ] , idx : u64 ) -> [ u8 ; 32 ] {
27
+ pub ( super ) fn build_commitment_secret ( commitment_seed : & [ u8 ; 32 ] , idx : u64 ) -> [ u8 ; 32 ] {
24
28
let mut res: [ u8 ; 32 ] = commitment_seed. clone ( ) ;
25
29
for i in 0 ..48 {
26
30
let bitpos = 47 - i;
@@ -32,6 +36,8 @@ pub fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32]
32
36
res
33
37
}
34
38
39
+ /// Derives a per-commitment-transaction private key (eg an htlc key or payment key) from the base
40
+ /// private key for that type of key and the per_commitment_point (available in TxCreationKeys)
35
41
pub fn derive_private_key < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , base_secret : & SecretKey ) -> Result < SecretKey , secp256k1:: Error > {
36
42
let mut sha = Sha256 :: engine ( ) ;
37
43
sha. input ( & per_commitment_point. serialize ( ) ) ;
@@ -43,7 +49,7 @@ pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_co
43
49
Ok ( key)
44
50
}
45
51
46
- pub fn derive_public_key < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , base_point : & PublicKey ) -> Result < PublicKey , secp256k1:: Error > {
52
+ pub ( super ) fn derive_public_key < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , base_point : & PublicKey ) -> Result < PublicKey , secp256k1:: Error > {
47
53
let mut sha = Sha256 :: engine ( ) ;
48
54
sha. input ( & per_commitment_point. serialize ( ) ) ;
49
55
sha. input ( & base_point. serialize ( ) ) ;
@@ -54,7 +60,7 @@ pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_com
54
60
}
55
61
56
62
/// Derives a revocation key from its constituent parts
57
- pub fn derive_private_revocation_key < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , per_commitment_secret : & SecretKey , revocation_base_secret : & SecretKey ) -> Result < SecretKey , secp256k1:: Error > {
63
+ pub ( super ) fn derive_private_revocation_key < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , per_commitment_secret : & SecretKey , revocation_base_secret : & SecretKey ) -> Result < SecretKey , secp256k1:: Error > {
58
64
let revocation_base_point = PublicKey :: from_secret_key ( & secp_ctx, & revocation_base_secret) ;
59
65
let per_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & per_commitment_secret) ;
60
66
@@ -81,7 +87,7 @@ pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1
81
87
Ok ( part_a)
82
88
}
83
89
84
- pub fn derive_public_revocation_key < T : secp256k1:: Verification > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , revocation_base_point : & PublicKey ) -> Result < PublicKey , secp256k1:: Error > {
90
+ pub ( super ) fn derive_public_revocation_key < T : secp256k1:: Verification > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , revocation_base_point : & PublicKey ) -> Result < PublicKey , secp256k1:: Error > {
85
91
let rev_append_commit_hash_key = {
86
92
let mut sha = Sha256 :: engine ( ) ;
87
93
sha. input ( & revocation_base_point. serialize ( ) ) ;
@@ -104,17 +110,26 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
104
110
part_a. combine ( & part_b)
105
111
}
106
112
113
+ /// The set of public keys which are used in the creation of one commitment transaction.
114
+ /// These are derived from the channel base keys and per-commitment data.
107
115
pub struct TxCreationKeys {
116
+ /// The per-commitment public key which was used to derive the other keys.
108
117
pub per_commitment_point : PublicKey ,
118
+ /// The revocation key which is used to allow the owner of the commitment transaction to
119
+ /// provide their counterparty the ability to punish them if they broadcast an old state.
109
120
pub revocation_key : PublicKey ,
121
+ /// A's HTLC Key
110
122
pub a_htlc_key : PublicKey ,
123
+ /// B's HTLC Key
111
124
pub b_htlc_key : PublicKey ,
125
+ /// A's Payment Key (which isn't allowed to be spent from for some delay)
112
126
pub a_delayed_payment_key : PublicKey ,
127
+ /// B's Payment Key
113
128
pub b_payment_key : PublicKey ,
114
129
}
115
130
116
131
impl TxCreationKeys {
117
- pub fn new < T : secp256k1:: Signing + secp256k1:: Verification > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , a_delayed_payment_base : & PublicKey , a_htlc_base : & PublicKey , b_revocation_base : & PublicKey , b_payment_base : & PublicKey , b_htlc_base : & PublicKey ) -> Result < TxCreationKeys , secp256k1:: Error > {
132
+ pub ( super ) fn new < T : secp256k1:: Signing + secp256k1:: Verification > ( secp_ctx : & Secp256k1 < T > , per_commitment_point : & PublicKey , a_delayed_payment_base : & PublicKey , a_htlc_base : & PublicKey , b_revocation_base : & PublicKey , b_payment_base : & PublicKey , b_htlc_base : & PublicKey ) -> Result < TxCreationKeys , secp256k1:: Error > {
118
133
Ok ( TxCreationKeys {
119
134
per_commitment_point : per_commitment_point. clone ( ) ,
120
135
revocation_key : derive_public_revocation_key ( & secp_ctx, & per_commitment_point, & b_revocation_base) ?,
@@ -128,7 +143,7 @@ impl TxCreationKeys {
128
143
129
144
/// Gets the "to_local" output redeemscript, ie the script which is time-locked or spendable by
130
145
/// the revocation key
131
- pub fn get_revokeable_redeemscript ( revocation_key : & PublicKey , to_self_delay : u16 , delayed_payment_key : & PublicKey ) -> Script {
146
+ pub ( super ) fn get_revokeable_redeemscript ( revocation_key : & PublicKey , to_self_delay : u16 , delayed_payment_key : & PublicKey ) -> Script {
132
147
Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_IF )
133
148
. push_slice ( & revocation_key. serialize ( ) )
134
149
. push_opcode ( opcodes:: all:: OP_ELSE )
@@ -142,16 +157,28 @@ pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u1
142
157
}
143
158
144
159
#[ derive( Clone , PartialEq ) ]
160
+ /// Information about an HTLC as it appears in a commitment transaction
145
161
pub struct HTLCOutputInCommitment {
162
+ /// Whether the HTLC was "offered" (ie outbound in relation to this commitment transaction).
163
+ /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
164
+ /// need to compare this value to whether the commitment transaction in question is that of
165
+ /// the remote party or our own.
146
166
pub offered : bool ,
167
+ /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
168
+ /// this divided by 1000.
147
169
pub amount_msat : u64 ,
170
+ /// The CLTV lock-time at which this HTLC expires.
148
171
pub cltv_expiry : u32 ,
172
+ /// The hash of the preimage which unlocks this HTLC.
149
173
pub payment_hash : PaymentHash ,
174
+ /// The position within the commitment transactions' outputs. This may be None if the value is
175
+ /// below the dust limit (in which case no output appears in the commitment transaction and the
176
+ /// value is spent to additional transaction fees).
150
177
pub transaction_output_index : Option < u32 > ,
151
178
}
152
179
153
180
#[ inline]
154
- pub fn get_htlc_redeemscript_with_explicit_keys ( htlc : & HTLCOutputInCommitment , a_htlc_key : & PublicKey , b_htlc_key : & PublicKey , revocation_key : & PublicKey ) -> Script {
181
+ pub ( super ) fn get_htlc_redeemscript_with_explicit_keys ( htlc : & HTLCOutputInCommitment , a_htlc_key : & PublicKey , b_htlc_key : & PublicKey , revocation_key : & PublicKey ) -> Script {
155
182
let payment_hash160 = Ripemd160 :: hash ( & htlc. payment_hash . 0 [ ..] ) . into_inner ( ) ;
156
183
if htlc. offered {
157
184
Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_DUP )
0 commit comments