|
9 | 9 | use bitcoin::blockdata::transaction::OutPoint;
|
10 | 10 | use bitcoin::blockdata::script::Script;
|
11 | 11 |
|
12 |
| -use secp256k1::key::SecretKey; |
| 12 | +use secp256k1::key::{SecretKey, PublicKey}; |
| 13 | +use secp256k1::Secp256k1; |
| 14 | +use secp256k1; |
| 15 | + |
| 16 | +use crypto::hkdf::{hkdf_extract,hkdf_expand}; |
13 | 17 |
|
14 | 18 | use util::events;
|
| 19 | +use util::sha2::Sha256; |
15 | 20 |
|
16 | 21 | /// A trait to describe a wallet which sould receive data to be able to spend onchain outputs
|
17 | 22 | /// fron a lightning channel
|
@@ -66,3 +71,87 @@ impl CustomOutputScriptDescriptor {
|
66 | 71 | }
|
67 | 72 | }
|
68 | 73 | }
|
| 74 | + |
| 75 | +/// A trait to describe an object which should get secrets from user wallet and apply derivation |
| 76 | +/// to provide keys materials downstream |
| 77 | +/// node_id /0' |
| 78 | +/// destination_pubkey /1' |
| 79 | +/// shutdown_pubkey /2' |
| 80 | +/// channel_master_pubkey /3/N' |
| 81 | +pub trait KeysInterface: Send + Sync { |
| 82 | + /// Get node secret key to derive node_id |
| 83 | + fn get_node_secret(&self) -> SecretKey; |
| 84 | + /// Get destination redeemScript to encumber static protocol exit points. For now |
| 85 | + /// redeemScript is a pay-2-public-key-hash. |
| 86 | + fn get_destination_script(&self) -> Script; |
| 87 | + /// Get shutdown_pubkey to use as PublicKey at channel closure |
| 88 | + fn get_shutdown_pubkey(&self) -> PublicKey; |
| 89 | + /// Get a new set of ChannelKeys from per-channel random key /3/N' |
| 90 | + /// For Channel N, keys correspond to ChannelKeys::new_from_seed(/3/N') |
| 91 | + fn get_channel_keys(&self) -> Option<ChannelKeys>; |
| 92 | +} |
| 93 | + |
| 94 | +/// Set of lightning keys needed to operate a channel as described in BOLT 3 |
| 95 | +pub struct ChannelKeys { |
| 96 | + /// Private key of anchor tx |
| 97 | + pub funding_key: SecretKey, |
| 98 | + /// Local secret key for blinded revocation pubkey |
| 99 | + pub revocation_base_key: SecretKey, |
| 100 | + /// Local secret key used in commitment tx htlc outputs |
| 101 | + pub payment_base_key: SecretKey, |
| 102 | + /// Local secret key used in HTLC tx |
| 103 | + pub delayed_payment_base_key: SecretKey, |
| 104 | + /// Local htlc secret key used in commitment tx htlc outputs |
| 105 | + pub htlc_base_key: SecretKey, |
| 106 | + /// Local secret key used for closing tx |
| 107 | + pub channel_close_key: SecretKey, |
| 108 | + /// Local secret key used in justice tx, claim tx and preimage tx outputs |
| 109 | + pub channel_monitor_claim_key: SecretKey, |
| 110 | + /// Commitment seed |
| 111 | + pub commitment_seed: [u8; 32], |
| 112 | +} |
| 113 | + |
| 114 | +impl ChannelKeys { |
| 115 | + /// Generate a set of lightning keys needed to operate a channel as described in BOLT 3 from |
| 116 | + /// used-provided seed |
| 117 | + pub fn new_from_seed(seed: &[u8; 32]) -> Result<ChannelKeys, secp256k1::Error> { |
| 118 | + let mut prk = [0; 32]; |
| 119 | + hkdf_extract(Sha256::new(), b"rust-lightning key gen salt", seed, &mut prk); |
| 120 | + let secp_ctx = Secp256k1::without_caps(); |
| 121 | + |
| 122 | + let mut okm = [0; 32]; |
| 123 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning funding key info", &mut okm); |
| 124 | + let funding_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 125 | + |
| 126 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning revocation base key info", &mut okm); |
| 127 | + let revocation_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 128 | + |
| 129 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning payment base key info", &mut okm); |
| 130 | + let payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 131 | + |
| 132 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning delayed payment base key info", &mut okm); |
| 133 | + let delayed_payment_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 134 | + |
| 135 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning htlc base key info", &mut okm); |
| 136 | + let htlc_base_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 137 | + |
| 138 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel close key info", &mut okm); |
| 139 | + let channel_close_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 140 | + |
| 141 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning channel monitor claim key info", &mut okm); |
| 142 | + let channel_monitor_claim_key = SecretKey::from_slice(&secp_ctx, &okm)?; |
| 143 | + |
| 144 | + hkdf_expand(Sha256::new(), &prk, b"rust-lightning local commitment seed info", &mut okm); |
| 145 | + |
| 146 | + Ok(ChannelKeys { |
| 147 | + funding_key: funding_key, |
| 148 | + revocation_base_key: revocation_base_key, |
| 149 | + payment_base_key: payment_base_key, |
| 150 | + delayed_payment_base_key: delayed_payment_base_key, |
| 151 | + htlc_base_key: htlc_base_key, |
| 152 | + channel_close_key: channel_close_key, |
| 153 | + channel_monitor_claim_key: channel_monitor_claim_key, |
| 154 | + commitment_seed: okm |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments