-
Notifications
You must be signed in to change notification settings - Fork 35
/
intercept_htlcs.rs
70 lines (64 loc) · 2.52 KB
/
intercept_htlcs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This example connects to LND and uses router rpc to intercept htlcs, inspect the details, then resume forwarding them
//
// The program accepts four arguments: host, port, cert file, macaroon file
// Example run: `cargo run --features=routerrpc --example intercept_htlcs <host> <port> <tls.cert> <file.macaroon>`
#[tokio::main]
#[cfg(feature = "routerrpc")]
async fn main() {
let mut args = std::env::args_os();
args.next().expect("not even zeroth arg given");
let host = args
.next()
.expect("missing arguments: host, port, cert file, macaroon file")
.into_string()
.expect("host is not UTF-8");
let port: u32 = args
.next()
.expect("missing arguments: port, cert file, macaroon file")
.into_string()
.expect("port is not UTF-8")
.parse()
.expect("port is not u32");
let cert_file: String = args
.next()
.expect("missing arguments: cert file, macaroon file")
.into_string()
.expect("cert_file is not UTF-8");
let macaroon_file: String = args
.next()
.expect("missing argument: macaroon file")
.into_string()
.expect("macaroon_file is not UTF-8");
// Connecting to LND requires only address, cert file, and macaroon file
let mut client = tonic_lnd::connect(host, port, cert_file, macaroon_file)
.await
.expect("failed to connect");
let (tx, rx) =
tokio::sync::mpsc::channel::<tonic_lnd::routerrpc::ForwardHtlcInterceptResponse>(1024);
let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
let mut htlc_stream = client
.router()
.htlc_interceptor(stream)
.await
.expect("Failed to call subscribe_invoices")
.into_inner();
while let Some(htlc) = htlc_stream
.message()
.await
.expect("Failed to receive invoices")
{
println!("HTLC Intercepted------------");
println!(
"incoming_circuit_key: {:?}\nincoming_amount_msat: {}\noutgoing_amount_msat: {}\npayment_hash: {:?}\n",
htlc.incoming_circuit_key, htlc.incoming_amount_msat, htlc.outgoing_amount_msat, htlc.payment_hash
);
let response = tonic_lnd::routerrpc::ForwardHtlcInterceptResponse {
incoming_circuit_key: htlc.incoming_circuit_key,
action: 2, // Resume fordwarding of intercepted HTLC
preimage: vec![],
failure_message: vec![],
failure_code: 0,
};
tx.send(response).await.unwrap();
}
}