Skip to content

Commit ba56c95

Browse files
author
Antoine Riard
committed
Add WalletInterface trait to handle keys management, first part: when ChannelMonitor
detects a spendable outputs onchain it will send back an Event to user wallet with needed data to be at disposal Add CustomOutputScriptDescriptor to ease user wallet spending of onchain lightning outputs Extend KeyStorage with delayed_payment_base_key and per_commitment_point to derive local_delayed private key
1 parent 3bcd911 commit ba56c95

File tree

6 files changed

+261
-56
lines changed

6 files changed

+261
-56
lines changed

src/chain/keysinterface.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! WalletInterface is *not* a wallet, only an interface to bridge between
2+
//! user wallet and ChannelMonitor. If this last one discover on-chain outputs they will
3+
//! be send with associate data as events::Event::SpendableOutputs to be at the disposal of wallet.
4+
//!
5+
//! KeysInterface is no more a wallet, just an entity to get secret from user wallet and derive
6+
//! appropriate keyring materials to others lightning components, as such node_id, destination_script.
7+
//!
8+
9+
use bitcoin::blockdata::transaction::OutPoint;
10+
use bitcoin::blockdata::script::Script;
11+
12+
use secp256k1::key::SecretKey;
13+
14+
use util::events;
15+
16+
/// A trait to describe a wallet which sould receive data to be able to spend onchain outputs
17+
/// fron a lightning channel
18+
pub trait WalletInterface: Send + Sync {
19+
/// Handle an incoming SpendableOutputs event from SimpleManyChannelMonitor containing a
20+
/// CustomOutputScriptDesctitpor. Follow doc of the latter to know how to spend the output.
21+
fn handle_spendable_output(&self, event: events::Event);
22+
}
23+
24+
/// Hacky custom output script descriptors to ease spending of onchain outputs by user wallet
25+
/// Maybe should be changed by real ones when merged into rust-bitcoin.
26+
/// StaticOutputs commit to a static pubkey, i.e one derived once for node operation lifetime.
27+
/// DynamicOutputs commit to a dynamic local_delayedpubkey, i.e one which change for each per_commitment_point
28+
pub enum CustomOutputScriptDescriptor {
29+
/// Outpoint commits to a P2PWKH, should be spend by the following witness :
30+
/// <signature> <pubkey>
31+
/// With pubkey being bip32 /1' from HMAC-Sha512 of user-provided seed as master private key
32+
StaticOutput {
33+
/// Outpoint spendable by user wallet
34+
outpoint: OutPoint,
35+
},
36+
/// Outpoint commits to a P2WSH, should be spend by the following witness :
37+
/// <local_delayedsig> 0 <witnessScript>
38+
/// With input nSequence set to_self_delay.
39+
DynamicOutput {
40+
/// Outpoint spendable by user wallet
41+
outpoint: OutPoint,
42+
/// local_delayedkey = delayed_payment_basepoint_secret + SHA256(per_commitment_point || delayed_payment_basepoint
43+
local_delayedkey: SecretKey,
44+
/// witness redeemScript encumbering output
45+
witness_script: Script,
46+
/// nSequence input must commit to self_delay to satisfy script's OP_CSV
47+
to_self_delay: u16,
48+
}
49+
}
50+
51+
impl CustomOutputScriptDescriptor {
52+
/// Build a StaticOuput descriptor
53+
pub fn static_key(outpoint: OutPoint) -> Self {
54+
CustomOutputScriptDescriptor::StaticOutput {
55+
outpoint,
56+
}
57+
}
58+
59+
/// Build a DynamicOuput descriptor
60+
pub fn dynamic_key(outpoint: OutPoint, local_delayedkey: SecretKey, witness_script: Script, to_self_delay: u16) -> Self {
61+
CustomOutputScriptDescriptor::DynamicOutput {
62+
outpoint,
63+
local_delayedkey,
64+
witness_script,
65+
to_self_delay,
66+
}
67+
}
68+
}

src/chain/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
33
pub mod chaininterface;
44
pub mod transaction;
5+
pub mod keysinterface;

src/ln/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl Channel {
491491
let our_channel_monitor_claim_script = Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script();
492492
let channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key,
493493
&PublicKey::from_secret_key(&secp_ctx, &chan_keys.delayed_payment_base_key),
494-
&chan_keys.htlc_base_key,
494+
&chan_keys.htlc_base_key, &chan_keys.delayed_payment_base_key,
495495
BREAKDOWN_TIMEOUT, our_channel_monitor_claim_script);
496496

497497
Ok(Channel {
@@ -653,7 +653,7 @@ impl Channel {
653653
let our_channel_monitor_claim_script = Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0).push_slice(&our_channel_monitor_claim_key_hash[..]).into_script();
654654
let mut channel_monitor = ChannelMonitor::new(&chan_keys.revocation_base_key,
655655
&PublicKey::from_secret_key(&secp_ctx, &chan_keys.delayed_payment_base_key),
656-
&chan_keys.htlc_base_key,
656+
&chan_keys.htlc_base_key, &chan_keys.delayed_payment_base_key,
657657
BREAKDOWN_TIMEOUT, our_channel_monitor_claim_script);
658658
channel_monitor.set_their_base_keys(&msg.htlc_basepoint, &msg.delayed_payment_basepoint);
659659
channel_monitor.set_their_to_self_delay(msg.to_self_delay);

0 commit comments

Comments
 (0)