1 //! keysinterface provides keys into rust-lightning and defines some useful enums which describe
2 //! spendable on-chain outputs which the user owns and is responsible for using just as any other
3 //! on-chain output which is theirs.
5 use bitcoin::blockdata::transaction::{OutPoint, TxOut};
6 use bitcoin::blockdata::script::Script;
8 use secp256k1::key::{SecretKey, PublicKey};
9 use secp256k1::Secp256k1;
11 use crypto::hkdf::{hkdf_extract,hkdf_expand};
13 use util::sha2::Sha256;
15 /// When on-chain outputs are created by rust-lightning an event is generated which informs the
16 /// user thereof. This enum describes the format of the output and provides the OutPoint.
17 pub enum SpendableOutputDescriptor {
18 /// Outpoint with an output to a script which was provided via KeysInterface, thus you should
19 /// have stored somewhere how to spend script_pubkey!
20 /// Outputs from a justice tx, claim tx or preimage tx
22 /// The outpoint spendable by user wallet
24 /// The output which is referenced by the given outpoint
27 /// Outpoint commits to a P2WSH, should be spend by the following witness :
28 /// <local_delayedsig> 0 <witnessScript>
29 /// With input nSequence set to_self_delay.
30 /// Outputs from a HTLC-Success/Timeout tx
32 /// Outpoint spendable by user wallet
34 /// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint
35 local_delayedkey: SecretKey,
36 /// witness redeemScript encumbering output
37 witness_script: Script,
38 /// nSequence input must commit to self_delay to satisfy script's OP_CSV
43 /// A trait to describe an object which can get user secrets and key material.
44 pub trait KeysInterface: Send + Sync {
45 /// Get node secret key (aka node_id or network_key)
46 fn get_node_secret(&self) -> SecretKey;
47 /// Get destination redeemScript to encumber static protocol exit points.
48 fn get_destination_script(&self) -> Script;
49 /// Get shutdown_pubkey to use as PublicKey at channel closure
50 fn get_shutdown_pubkey(&self) -> PublicKey;
51 /// Get a new set of ChannelKeys for per-channel secrets. These MUST be unique even if you
52 /// restarted with some stale data!
53 fn get_channel_keys(&self, inbound: bool) -> ChannelKeys;
56 /// Set of lightning keys needed to operate a channel as described in BOLT 3
57 pub struct ChannelKeys {
58 /// Private key of anchor tx
59 pub funding_key: SecretKey,
60 /// Local secret key for blinded revocation pubkey
61 pub revocation_base_key: SecretKey,
62 /// Local secret key used in commitment tx htlc outputs
63 pub payment_base_key: SecretKey,
64 /// Local secret key used in HTLC tx
65 pub delayed_payment_base_key: SecretKey,
66 /// Local htlc secret key used in commitment tx htlc outputs
67 pub htlc_base_key: SecretKey,
68 /// Local secret key used for closing tx
69 pub channel_close_key: SecretKey,
70 /// Local secret key used in justice tx, claim tx and preimage tx outputs
71 pub channel_monitor_claim_key: SecretKey,
73 pub commitment_seed: [u8; 32],
77 /// Generate a set of lightning keys needed to operate a channel by HKDF-expanding a given
78 /// random 32-byte seed
79 pub fn new_from_seed(seed: &[u8; 32]) -> ChannelKeys {
80 let mut prk = [0; 32];
81 hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk);
82 let secp_ctx = Secp256k1::without_caps();
84 let mut okm = [0; 32];
85 hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm);
86 let funding_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
88 hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm);
89 let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
91 hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm);
92 let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
94 hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm);
95 let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
97 hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm);
98 let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
100 hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm);
101 let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
103 hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm);
104 let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm).expect("Sha256 is broken");
106 hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm);
109 funding_key: funding_key,
110 revocation_base_key: revocation_base_key,
111 payment_base_key: payment_base_key,
112 delayed_payment_base_key: delayed_payment_base_key,
113 htlc_base_key: htlc_base_key,
114 channel_close_key: channel_close_key,
115 channel_monitor_claim_key: channel_monitor_claim_key,