|
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 describes the format of the output and provides the OutPoint.
|
@@ -34,3 +39,81 @@ pub enum SpendableOutputDescriptor {
|
34 | 39 | to_self_delay: u16,
|
35 | 40 | }
|
36 | 41 | }
|
| 42 | + |
| 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; |
| 54 | +} |
| 55 | + |
| 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, |
| 72 | + /// Commitment seed |
| 73 | + pub commitment_seed: [u8; 32], |
| 74 | +} |
| 75 | + |
| 76 | +impl ChannelKeys { |
| 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(); |
| 83 | + |
| 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"); |
| 87 | + |
| 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"); |
| 90 | + |
| 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"); |
| 93 | + |
| 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"); |
| 96 | + |
| 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"); |
| 99 | + |
| 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"); |
| 102 | + |
| 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"); |
| 105 | + |
| 106 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm); |
| 107 | + |
| 108 | + ChannelKeys { |
| 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, |
| 116 | + commitment_seed: okm |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments