HtlcModifier
HtlcModifier is a bidirectional streaming RPC that allows a client to intercept and modify the HTLCs that attempt to settle the given invoice. The server will send HTLCs of invoices to the client and the client can modify some aspects of the HTLC in order to pass the invoice acceptance tests.
Source: invoicesrpc/invoices.proto
gRPC
info
This is a bidirectional-streaming RPC
rpc HtlcModifier (stream HtlcModifyResponse) returns (stream HtlcModifyRequest);
REST
| HTTP Method | Path |
|---|---|
| POST | /v2/invoices/htlcmodifier |
Code Samples
- gRPC
- REST
- Javascript
- Python
- grpcurl
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:10009'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
const TLS_PATH = 'LND_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync(['lightning.proto', 'invoicesrpc/invoices.proto'], loaderOptions);
const invoicesrpc = grpc.loadPackageDefinition(packageDefinition).invoicesrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new invoicesrpc.Invoices(GRPC_HOST, creds);
let request = {
circuit_key: <CircuitKey>,
amt_paid: <uint64>,
cancel_set: <bool>,
};
let call = client.htlcModifier({});
call.on('data', function(response) {
// A response was received from the server.
console.log(response);
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
call.write(request);
// Console output:
// {
// "invoice": <Invoice>,
// "exit_htlc_circuit_key": <CircuitKey>,
// "exit_htlc_amt": <uint64>,
// "exit_htlc_expiry": <uint32>,
// "current_height": <uint32>,
// "exit_htlc_wire_custom_records": <ExitHtlcWireCustomRecordsEntry>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the invoicesrpc/invoices.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import invoices_pb2 as invoicesrpc, invoices_pb2_grpc as invoicesstub
GRPC_HOST = 'localhost:10009'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
# create macaroon credentials
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = invoicesstub.InvoicesStub(channel)
# Define a generator that returns an Iterable of HtlcModifyResponse objects.
def request_generator():
# Initialization code here.
while True:
# Parameters here can be set as arguments to the generator.
request = invoicesrpc.HtlcModifyResponse(
circuit_key=<CircuitKey>,
amt_paid=<uint64>,
cancel_set=<bool>,
)
yield request
# Do things between iterations here.
request_iterable = request_generator()
for response in stub.HtlcModifier(request_iterable):
print(response)
# {
# "invoice": <Invoice>,
# "exit_htlc_circuit_key": <CircuitKey>,
# "exit_htlc_amt": <uint64>,
# "exit_htlc_expiry": <uint32>,
# "current_height": <uint32>,
# "exit_htlc_wire_custom_records": <ExitHtlcWireCustomRecordsEntry>,
# }
# grpcurl docs: https://github.com/fullstorydev/grpcurl
# Proto source: https://github.com/lightningnetwork/lnd
GRPC_HOST=localhost:10009
LND_DIR=~/.lnd
LND_SOURCE=path/to/lnd
NETWORK=mainnet
MACAROON_PATH="$LND_DIR/data/chain/bitcoin/$NETWORK/admin.macaroon"
TLS_PATH="$LND_DIR/tls.cert"
# Client streaming or bidirectional streaming RPCs are not easily supported
# by grpcurl. Please use a programming language client for this RPC.
- Javascript
- Python
- curl
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8080'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
let requestBody = {
circuit_key: <object>, // <CircuitKey>
amt_paid: <string>, // <uint64>
cancel_set: <boolean>, // <bool>
};
let options = {
url: `https://${REST_HOST}/v2/invoices/htlcmodifier`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
form: JSON.stringify(requestBody),
}
request.post(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "invoice": <object>, // <Invoice>
// "exit_htlc_circuit_key": <object>, // <CircuitKey>
// "exit_htlc_amt": <string>, // <uint64>
// "exit_htlc_expiry": <integer>, // <uint32>
// "current_height": <integer>, // <uint32>
// "exit_htlc_wire_custom_records": <object>, // <ExitHtlcWireCustomRecordsEntry>
// }
// --------------------------
// Example with websockets:
// --------------------------
const WebSocket = require('ws');
const fs = require('fs');
const REST_HOST = 'localhost:8080'
const MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
let ws = new WebSocket(`wss://${REST_HOST}/v2/invoices/htlcmodifier?method=POST`, {
// Work-around for self-signed certificates.
rejectUnauthorized: false,
headers: {
'Grpc-Metadata-Macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
});
let requestBody = {
circuit_key: <CircuitKey>, // <CircuitKey>
amt_paid: <uint64>, // <uint64>
cancel_set: <bool>, // <bool>
};
ws.on('open', function() {
ws.send(JSON.stringify(requestBody));
});
ws.on('error', function(err) {
console.log('Error: ' + err);
});
ws.on('message', function(body) {
console.log(body);
});
// Console output:
// {
// "invoice": <object>, // <Invoice>
// "exit_htlc_circuit_key": <object>, // <CircuitKey>
// "exit_htlc_amt": <string>, // <uint64>
// "exit_htlc_expiry": <integer>, // <uint32>
// "current_height": <integer>, // <uint32>
// "exit_htlc_wire_custom_records": <object>, // <ExitHtlcWireCustomRecordsEntry>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8080'
MACAROON_PATH = 'LND_DIR/data/chain/bitcoin/regtest/admin.macaroon'
TLS_PATH = 'LND_DIR/tls.cert'
url = f'https://{REST_HOST}/v2/invoices/htlcmodifier'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
data = {
'circuit_key': <CircuitKey>,
'amt_paid': <uint64>,
'cancel_set': <bool>,
}
r = requests.post(url, headers=headers, stream=True, data=json.dumps(data), verify=TLS_PATH)
for raw_response in r.iter_lines():
json_response = json.loads(raw_response)
print(json_response)
# {
# "invoice": <Invoice>,
# "exit_htlc_circuit_key": <CircuitKey>,
# "exit_htlc_amt": <uint64>,
# "exit_htlc_expiry": <uint32>,
# "current_height": <uint32>,
# "exit_htlc_wire_custom_records": <ExitHtlcWireCustomRecordsEntry>,
# }
REST_HOST=localhost:8080
LND_DIR=~/.lnd
NETWORK=mainnet
MACAROON_PATH="$LND_DIR/data/chain/bitcoin/$NETWORK/admin.macaroon"
TLS_PATH="$LND_DIR/tls.cert"
curl -X POST \
--cacert $TLS_PATH \
-H "Grpc-Metadata-macaroon: $(xxd -ps -u -c 1000 $MACAROON_PATH)" \
-d '{ "circuit_key": <CircuitKey>, "amt_paid": <uint64>, "cancel_set": <bool> }' \
https://$REST_HOST/v2/invoices/htlcmodifier
Messages
invoicesrpc.HtlcModifyResponse
Source: invoicesrpc/invoices.proto
| Field | gRPC Type | REST Type | REST Placement |
|---|---|---|---|
circuit_key | CircuitKey | object | body |
amt_paid | uint64 | string | body |
cancel_set | bool | boolean | body |
invoicesrpc.HtlcModifyRequest
Source: invoicesrpc/invoices.proto
| Field | gRPC Type | REST Type |
|---|---|---|
invoice | Invoice | object |
exit_htlc_circuit_key | CircuitKey | object |
exit_htlc_amt | uint64 | string |
exit_htlc_expiry | uint32 | integer |
current_height | uint32 | integer |
exit_htlc_wire_custom_records | ExitHtlcWireCustomRecordsEntry[] | object |
Nested Messages
invoicesrpc.CircuitKey
| Field | gRPC Type | REST Type |
|---|---|---|
chan_id | uint64 | string |
htlc_id | uint64 | string |
invoicesrpc.HtlcModifyRequest.ExitHtlcWireCustomRecordsEntry
| Field | gRPC Type | REST Type |
|---|---|---|
key | uint64 | unknown |
value | bytes | unknown |
lnrpc.AMP
| Field | gRPC Type | REST Type |
|---|---|---|
root_share | bytes | string |
set_id | bytes | string |
child_index | uint32 | integer |
hash | bytes | string |
preimage | bytes | string |
lnrpc.AMPInvoiceState
| Field | gRPC Type | REST Type |
|---|---|---|
state | InvoiceHTLCState | string |
settle_index | uint64 | string |
settle_time | int64 | string |
amt_paid_msat | int64 | string |
lnrpc.BlindedPathConfig
| Field | gRPC Type | REST Type |
|---|---|---|
min_num_real_hops | uint32 | integer |
num_hops | uint32 | integer |
max_num_paths | uint32 | integer |
node_omission_list | bytes[] | array |
incoming_channel_list | uint64[] | array |
lnrpc.Feature
| Field | gRPC Type | REST Type |
|---|---|---|
name | string | string |
is_required | bool | boolean |
is_known | bool | boolean |
lnrpc.HopHint
| Field | gRPC Type | REST Type |
|---|---|---|
node_id | string | string |
chan_id | uint64 | string |
fee_base_msat | uint32 | integer |
fee_proportional_millionths | uint32 | integer |
cltv_expiry_delta | uint32 | integer |
lnrpc.Invoice
| Field | gRPC Type | REST Type |
|---|---|---|
memo | string | string |
r_preimage | bytes | string |
r_hash | bytes | string |
value | int64 | string |
value_msat | int64 | string |
settleddeprecated | bool | boolean |
creation_date | int64 | string |
settle_date | int64 | string |
payment_request | string | string |
description_hash | bytes | string |
expiry | int64 | string |
fallback_addr | string | string |
cltv_expiry | uint64 | string |
route_hints | RouteHint[] | array |
private | bool | boolean |
add_index | uint64 | string |
settle_index | uint64 | string |
amt_paiddeprecated | int64 | string |
amt_paid_sat | int64 | string |
amt_paid_msat | int64 | string |
state | InvoiceState | string |
htlcs | InvoiceHTLC[] | array |
features | FeaturesEntry[] | object |
is_keysend | bool | boolean |
payment_addr | bytes | string |
is_amp | bool | boolean |
amp_invoice_state | AmpInvoiceStateEntry[] | object |
is_blinded | bool | boolean |
blinded_path_config | BlindedPathConfig | object |
lnrpc.Invoice.AmpInvoiceStateEntry
| Field | gRPC Type | REST Type |
|---|---|---|
key | string | unknown |
value | AMPInvoiceState | unknown |
lnrpc.Invoice.FeaturesEntry
| Field | gRPC Type | REST Type |
|---|---|---|
key | uint32 | unknown |
value | Feature | unknown |
lnrpc.InvoiceHTLC
| Field | gRPC Type | REST Type |
|---|---|---|
chan_id | uint64 | string |
htlc_index | uint64 | string |
amt_msat | uint64 | string |
accept_height | int32 | integer |
accept_time | int64 | string |
resolve_time | int64 | string |
expiry_height | int32 | integer |
state | InvoiceHTLCState | string |
custom_records | CustomRecordsEntry[] | object |
mpp_total_amt_msat | uint64 | string |
amp | AMP | object |
custom_channel_data | bytes | string |
lnrpc.InvoiceHTLC.CustomRecordsEntry
| Field | gRPC Type | REST Type |
|---|---|---|
key | uint64 | unknown |
value | bytes | unknown |
lnrpc.RouteHint
| Field | gRPC Type | REST Type |
|---|---|---|
hop_hints | HopHint[] | array |
Enums
lnrpc.Invoice.InvoiceState
| Name | Number |
|---|---|
OPEN | 0 |
SETTLED | 1 |
CANCELED | 2 |
ACCEPTED | 3 |
lnrpc.InvoiceHTLCState
| Name | Number |
|---|---|
ACCEPTED | 0 |
SETTLED | 1 |
CANCELED | 2 |