BuildOnion
BuildOnion attempts to build an onion packet for the specified route.
Source: switchrpc/switch.proto
gRPC
rpc BuildOnion (BuildOnionRequest) returns (BuildOnionResponse);
REST
| HTTP Method | Path |
|---|---|
| POST | /v2/switch/build |
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', 'switchrpc/switch.proto'], loaderOptions);
const switchrpc = grpc.loadPackageDefinition(packageDefinition).switchrpc;
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 switchrpc.Switch(GRPC_HOST, creds);
let request = {
route: <Route>,
payment_hash: <bytes>,
session_key: <bytes>,
};
client.buildOnion(request, function(err, response) {
console.log(response);
});
// Console output:
// {
// "onion_blob": <bytes>,
// "session_key": <bytes>,
// "hop_pubkeys": <bytes>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the switchrpc/switch.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import switch_pb2 as switchrpc, switch_pb2_grpc as switchstub
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 = switchstub.SwitchStub(channel)
request = switchrpc.BuildOnionRequest(
route=<Route>,
payment_hash=<bytes>,
session_key=<bytes>,
)
response = stub.BuildOnion(request)
print(response)
# {
# "onion_blob": <bytes>,
# "session_key": <bytes>,
# "hop_pubkeys": <bytes>,
# }
# 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"
grpcurl \
-import-path $LND_SOURCE/lnrpc/ \
-proto switchrpc/switch.proto \
-cacert $TLS_PATH \
-H "macaroon: $(xxd -ps -u -c 1000 $MACAROON_PATH)" \
-d '{ "route": <Route>, "payment_hash": BASE64_ENCODED_VALUE, "session_key": BASE64_ENCODED_VALUE }' \
$GRPC_HOST \
switchrpc.Switch/BuildOnion
- 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 = {
route: <object>, // <Route>
payment_hash: <string>, // <bytes> (base64 encoded)
session_key: <string>, // <bytes> (base64 encoded)
};
let options = {
url: `https://${REST_HOST}/v2/switch/build`,
// 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:
// {
// "onion_blob": <string>, // <bytes>
// "session_key": <string>, // <bytes>
// "hop_pubkeys": <array>, // <bytes>
// }
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/switch/build'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
data = {
'route': <Route>,
'payment_hash': base64.b64encode(<bytes>),
'session_key': base64.b64encode(<bytes>),
}
r = requests.post(url, headers=headers, data=json.dumps(data), verify=TLS_PATH)
print(r.json())
# {
# "onion_blob": <bytes>,
# "session_key": <bytes>,
# "hop_pubkeys": <bytes>,
# }
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 '{ "route": <Route>, "payment_hash": BASE64_ENCODED_VALUE, "session_key": BASE64_ENCODED_VALUE }' \
https://$REST_HOST/v2/switch/build
Messages
switchrpc.BuildOnionRequest
Source: switchrpc/switch.proto
| Field | gRPC Type | REST Type | REST Placement |
|---|---|---|---|
route | Route | object | body |
payment_hash | bytes | string | body |
session_key | bytes | string | body |
switchrpc.BuildOnionResponse
Source: switchrpc/switch.proto
| Field | gRPC Type | REST Type |
|---|---|---|
onion_blob | bytes | string |
session_key | bytes | string |
hop_pubkeys | bytes[] | array |
Nested Messages
lnrpc.AMPRecord
| Field | gRPC Type | REST Type |
|---|---|---|
root_share | bytes | string |
set_id | bytes | string |
child_index | uint32 | integer |
lnrpc.Hop
| Field | gRPC Type | REST Type |
|---|---|---|
chan_id | uint64 | string |
chan_capacity | int64 | string |
amt_to_forward | int64 | string |
fee | int64 | string |
expiry | uint32 | integer |
amt_to_forward_msat | int64 | string |
fee_msat | int64 | string |
pub_key | string | string |
tlv_payload | bool | boolean |
mpp_record | MPPRecord | object |
amp_record | AMPRecord | object |
custom_records | CustomRecordsEntry[] | object |
metadata | bytes | string |
blinding_point | bytes | string |
encrypted_data | bytes | string |
total_amt_msat | uint64 | string |
lnrpc.Hop.CustomRecordsEntry
| Field | gRPC Type | REST Type |
|---|---|---|
key | uint64 | unknown |
value | bytes | unknown |
lnrpc.MPPRecord
| Field | gRPC Type | REST Type |
|---|---|---|
payment_addr | bytes | string |
total_amt_msat | int64 | string |
lnrpc.Route
| Field | gRPC Type | REST Type |
|---|---|---|
total_time_lock | uint32 | integer |
total_fees | int64 | string |
total_amt | int64 | string |
hops | Hop[] | array |
total_fees_msat | int64 | string |
total_amt_msat | int64 | string |
first_hop_amount_msat | int64 | string |
custom_channel_data | bytes | string |
source_pub_key | string | string |