2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-12-05 11:06:28 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ccan/array_size/array_size.h>
|
2020-04-11 05:23:09 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2022-10-17 02:44:39 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2022-10-17 02:44:38 +02:00
|
|
|
#include <common/blindedpath.h>
|
2020-04-11 04:52:51 +02:00
|
|
|
#include <common/ecdh.h>
|
2022-11-09 02:30:10 +01:00
|
|
|
#include <common/onion_decode.h>
|
2019-12-05 11:06:28 +01:00
|
|
|
#include <common/sphinx.h>
|
2020-04-11 04:52:51 +02:00
|
|
|
#include <sodium/crypto_aead_chacha20poly1305.h>
|
2019-12-05 11:06:28 +01:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2023-01-12 04:55:38 +01:00
|
|
|
* - If `encrypted_recipient_data` is present:
|
|
|
|
*...
|
|
|
|
* - If it is not the final node:
|
|
|
|
* - MUST return an error if the payload contains other tlv fields than
|
|
|
|
* `encrypted_recipient_data` and `current_blinding_point`.
|
|
|
|
*/
|
2023-04-07 08:36:14 +02:00
|
|
|
static bool check_nonfinal_tlv(const struct tlv_payload *tlv,
|
2023-01-12 04:55:38 +01:00
|
|
|
u64 *failtlvtype)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(tlv->fields); i++) {
|
|
|
|
switch (tlv->fields[i].numtype) {
|
2023-04-07 08:36:14 +02:00
|
|
|
case TLV_PAYLOAD_BLINDING_POINT:
|
|
|
|
case TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA:
|
2023-01-12 04:55:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*failtlvtype = tlv->fields[i].numtype;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2023-01-12 04:55:38 +01:00
|
|
|
* - If `encrypted_recipient_data` is present:
|
|
|
|
*...
|
|
|
|
* - If it is the final node:
|
|
|
|
* - MUST return an error if the payload contains other tlv fields than
|
|
|
|
* `encrypted_recipient_data`, `current_blinding_point`, `amt_to_forward`,
|
|
|
|
* `outgoing_cltv_value` and `total_amount_msat`.
|
|
|
|
*/
|
2023-04-07 08:36:14 +02:00
|
|
|
static bool check_final_tlv(const struct tlv_payload *tlv,
|
2023-01-12 04:55:38 +01:00
|
|
|
u64 *failtlvtype)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(tlv->fields); i++) {
|
|
|
|
switch (tlv->fields[i].numtype) {
|
2023-04-07 08:36:14 +02:00
|
|
|
case TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA:
|
|
|
|
case TLV_PAYLOAD_BLINDING_POINT:
|
|
|
|
case TLV_PAYLOAD_AMT_TO_FORWARD:
|
|
|
|
case TLV_PAYLOAD_OUTGOING_CLTV_VALUE:
|
|
|
|
case TLV_PAYLOAD_TOTAL_AMOUNT_MSAT:
|
2023-01-12 04:55:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*failtlvtype = tlv->fields[i].numtype;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-17 02:44:38 +02:00
|
|
|
static u64 ceil_div(u64 a, u64 b)
|
|
|
|
{
|
|
|
|
return (a + b - 1) / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool handle_blinded_forward(struct onion_payload *p,
|
|
|
|
struct amount_msat amount_in,
|
|
|
|
u32 cltv_expiry,
|
2023-04-07 08:36:14 +02:00
|
|
|
const struct tlv_payload *tlv,
|
2022-10-17 02:44:38 +02:00
|
|
|
const struct tlv_encrypted_data_tlv *enc,
|
|
|
|
u64 *failtlvtype)
|
|
|
|
{
|
|
|
|
u64 amt = amount_in.millisatoshis; /* Raw: allowed to wrap */
|
|
|
|
|
2023-01-12 04:55:38 +01:00
|
|
|
if (!check_nonfinal_tlv(tlv, failtlvtype))
|
|
|
|
return false;
|
2022-10-17 02:44:38 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - If it is not the final node:
|
2022-10-17 02:44:38 +02:00
|
|
|
*...
|
|
|
|
* - MUST return an error if `encrypted_recipient_data` does not
|
2022-11-09 02:30:10 +01:00
|
|
|
* contain either `short_channel_id` or `next_node_id`.
|
2022-10-17 02:44:38 +02:00
|
|
|
*/
|
|
|
|
if (!enc->short_channel_id && !enc->next_node_id) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-09 02:30:10 +01:00
|
|
|
if (enc->short_channel_id) {
|
|
|
|
p->forward_channel = tal_dup(p, struct short_channel_id,
|
|
|
|
enc->short_channel_id);
|
|
|
|
p->forward_node_id = NULL;
|
|
|
|
} else {
|
|
|
|
p->forward_channel = NULL;
|
|
|
|
p->forward_node_id = tal_dup(p, struct pubkey,
|
|
|
|
enc->next_node_id);
|
2022-10-17 02:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p->total_msat = NULL;
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - If it is not the final node:
|
2022-10-17 02:44:38 +02:00
|
|
|
*...
|
|
|
|
* - MUST return an error if `encrypted_recipient_data` does not
|
|
|
|
* contain `payment_relay`.
|
|
|
|
*/
|
|
|
|
if (!enc->payment_relay) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Put these formulae in BOLT 4! */
|
|
|
|
/* amt_to_forward = ceil((amount_msat - fee_base_msat) * 1000000 / (1000000 + fee_proportional_millionths)) */
|
|
|
|
/* If these values are crap, that's OK: the HTLC will fail. */
|
|
|
|
p->amt_to_forward = amount_msat(ceil_div((amt - enc->payment_relay->fee_base_msat) * 1000000,
|
|
|
|
1000000 + enc->payment_relay->fee_proportional_millionths));
|
|
|
|
p->outgoing_cltv = cltv_expiry - enc->payment_relay->cltv_expiry_delta;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool handle_blinded_terminal(struct onion_payload *p,
|
2023-04-07 08:36:14 +02:00
|
|
|
const struct tlv_payload *tlv,
|
2022-10-17 02:44:38 +02:00
|
|
|
const struct tlv_encrypted_data_tlv *enc,
|
|
|
|
u64 *failtlvtype)
|
|
|
|
{
|
2023-01-12 04:55:38 +01:00
|
|
|
if (!check_final_tlv(tlv, failtlvtype))
|
|
|
|
return false;
|
2022-10-17 02:44:38 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2023-01-12 04:53:38 +01:00
|
|
|
* - MUST return an error if `amt_to_forward`, `outgoing_cltv_value`
|
|
|
|
* or `total_amount_msat` are not present.
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST return an error if `amt_to_forward` is below what it expects
|
|
|
|
* for the payment.
|
|
|
|
*/
|
2022-10-17 02:44:38 +02:00
|
|
|
if (!tlv->amt_to_forward) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_AMT_TO_FORWARD;
|
2022-10-17 02:44:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tlv->outgoing_cltv_value) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_OUTGOING_CLTV_VALUE;
|
2022-10-17 02:44:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:53:38 +01:00
|
|
|
if (!tlv->total_amount_msat) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_TOTAL_AMOUNT_MSAT;
|
2023-01-12 04:53:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-17 02:44:38 +02:00
|
|
|
p->amt_to_forward = amount_msat(*tlv->amt_to_forward);
|
|
|
|
p->outgoing_cltv = *tlv->outgoing_cltv_value;
|
|
|
|
|
|
|
|
p->forward_channel = NULL;
|
2022-11-09 02:30:10 +01:00
|
|
|
p->forward_node_id = NULL;
|
|
|
|
|
2022-11-09 02:30:10 +01:00
|
|
|
if (tlv->total_amount_msat) {
|
|
|
|
p->total_msat = tal(p, struct amount_msat);
|
|
|
|
*p->total_msat = amount_msat(*tlv->total_amount_msat);
|
|
|
|
} else {
|
|
|
|
/* BOLT #4:
|
2023-04-07 08:55:33 +02:00
|
|
|
* - If it is the final node:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST treat `total_msat` as if it were equal to
|
|
|
|
* `amt_to_forward` if it is not present. */
|
|
|
|
p->total_msat = tal_dup(p, struct amount_msat,
|
|
|
|
&p->amt_to_forward);
|
|
|
|
}
|
2022-10-17 02:44:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-05 11:06:28 +01:00
|
|
|
struct onion_payload *onion_decode(const tal_t *ctx,
|
2022-10-17 02:44:38 +02:00
|
|
|
bool blinding_support,
|
2020-03-04 14:20:59 +01:00
|
|
|
const struct route_step *rs,
|
2020-04-11 04:52:51 +02:00
|
|
|
const struct pubkey *blinding,
|
2022-03-23 03:44:38 +01:00
|
|
|
const u64 *accepted_extra_tlvs,
|
2022-10-17 02:44:38 +02:00
|
|
|
struct amount_msat amount_in,
|
|
|
|
u32 cltv_expiry,
|
2020-03-04 14:20:59 +01:00
|
|
|
u64 *failtlvtype,
|
|
|
|
size_t *failtlvpos)
|
2019-12-05 11:06:28 +01:00
|
|
|
{
|
|
|
|
struct onion_payload *p = tal(ctx, struct onion_payload);
|
|
|
|
const u8 *cursor = rs->raw_payload;
|
|
|
|
size_t max = tal_bytelen(cursor), len;
|
|
|
|
|
2023-01-12 04:52:38 +01:00
|
|
|
p->final = (rs->nextcase == ONION_END);
|
|
|
|
|
2023-04-07 08:36:14 +02:00
|
|
|
/* BOLT #4:
|
2022-09-28 06:49:35 +02:00
|
|
|
* 1. type: `hop_payloads`
|
|
|
|
* 2. data:
|
|
|
|
* * [`bigsize`:`length`]
|
|
|
|
* * [`length*byte`:`payload`]
|
|
|
|
*/
|
|
|
|
len = fromwire_bigsize(&cursor, &max);
|
|
|
|
if (!cursor || len > max) {
|
2022-03-23 03:44:36 +01:00
|
|
|
*failtlvtype = 0;
|
|
|
|
*failtlvpos = tal_bytelen(rs->raw_payload);
|
2022-10-17 02:44:39 +02:00
|
|
|
return tal_free(p);
|
2022-03-22 09:48:13 +01:00
|
|
|
}
|
2022-03-08 21:22:22 +01:00
|
|
|
|
2022-03-23 03:44:36 +01:00
|
|
|
/* We do this manually so we can accept extra types, and get
|
|
|
|
* error off and type. */
|
2023-04-07 08:36:14 +02:00
|
|
|
p->tlv = tlv_payload_new(p);
|
|
|
|
if (!fromwire_tlv(&cursor, &max, tlvs_tlv_payload,
|
|
|
|
TLVS_ARRAY_SIZE_tlv_payload,
|
2022-10-17 02:44:39 +02:00
|
|
|
p->tlv, &p->tlv->fields, accepted_extra_tlvs,
|
2022-03-23 03:44:36 +01:00
|
|
|
failtlvpos, failtlvtype)) {
|
2022-10-17 02:44:39 +02:00
|
|
|
return tal_free(p);
|
2022-03-08 21:22:22 +01:00
|
|
|
}
|
2019-12-05 11:06:28 +01:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
*
|
|
|
|
* The reader:
|
|
|
|
*
|
|
|
|
* - If `encrypted_recipient_data` is present:
|
|
|
|
*/
|
|
|
|
if (p->tlv->encrypted_recipient_data) {
|
2022-10-17 02:44:38 +02:00
|
|
|
struct tlv_encrypted_data_tlv *enc;
|
|
|
|
|
|
|
|
/* Only supported with --experimental-onion-messages! */
|
|
|
|
if (!blinding_support) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:38 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
*
|
|
|
|
* - If `blinding_point` is set in the incoming `update_add_htlc`:
|
|
|
|
* - MUST return an error if `current_blinding_point` is present.
|
|
|
|
* - MUST use that `blinding_point` as the blinding point for decryption.
|
|
|
|
* - Otherwise:
|
|
|
|
* - MUST return an error if `current_blinding_point` is not present.
|
|
|
|
* - MUST use that `current_blinding_point` as the blinding point for decryption.
|
2022-10-17 02:44:38 +02:00
|
|
|
*/
|
2022-11-09 02:30:10 +01:00
|
|
|
if (blinding) {
|
|
|
|
if (p->tlv->blinding_point) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_BLINDING_POINT;
|
2022-11-09 02:30:10 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
p->blinding = tal_dup(p, struct pubkey, blinding);
|
|
|
|
} else {
|
|
|
|
if (!p->tlv->blinding_point) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_BLINDING_POINT;
|
2022-11-09 02:30:10 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
2022-10-17 02:44:38 +02:00
|
|
|
p->blinding = tal_dup(p, struct pubkey,
|
2022-10-17 02:44:39 +02:00
|
|
|
p->tlv->blinding_point);
|
2022-11-09 02:30:10 +01:00
|
|
|
}
|
2022-10-17 02:44:38 +02:00
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-10-17 02:44:38 +02:00
|
|
|
* The reader:
|
|
|
|
*...
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST return an error if `encrypted_recipient_data` does
|
|
|
|
* not decrypt using the blinding point as described in
|
|
|
|
* [Route Blinding](#route-blinding).
|
2022-10-17 02:44:38 +02:00
|
|
|
*/
|
|
|
|
ecdh(p->blinding, &p->blinding_ss);
|
|
|
|
enc = decrypt_encrypted_data(tmpctx, p->blinding, &p->blinding_ss,
|
2022-10-17 02:44:39 +02:00
|
|
|
p->tlv->encrypted_recipient_data);
|
2022-10-17 02:44:38 +02:00
|
|
|
if (!enc) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:38 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2022-10-17 02:44:39 +02:00
|
|
|
if (enc->payment_constraints) {
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST return an error if:
|
|
|
|
* - the expiry is greater than
|
|
|
|
* `encrypted_recipient_data.payment_constraints.max_cltv_expiry`.
|
2022-10-17 02:44:39 +02:00
|
|
|
*/
|
|
|
|
if (cltv_expiry > enc->payment_constraints->max_cltv_expiry) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:39 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST return an error if:
|
|
|
|
*...
|
|
|
|
* - the amount is below
|
|
|
|
* `encrypted_recipient_data.payment_constraints.htlc_minimum_msat`.
|
2022-10-17 02:44:39 +02:00
|
|
|
*/
|
|
|
|
if (amount_msat_less(amount_in,
|
|
|
|
amount_msat(enc->payment_constraints->htlc_minimum_msat))) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:39 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - MUST return an error if:
|
|
|
|
*...
|
|
|
|
* - the payment uses a feature not included in
|
|
|
|
* `encrypted_recipient_data.allowed_features.features`
|
2022-10-17 02:44:39 +02:00
|
|
|
*/
|
|
|
|
/* We don't have any features yet... */
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
|
|
|
* - If `allowed_features` is missing:
|
|
|
|
* - MUST process the message as if it were present and contained an
|
|
|
|
* empty array.
|
2022-10-17 02:44:39 +02:00
|
|
|
* - MUST return an error if:
|
|
|
|
* - `encrypted_recipient_data.allowed_features.features`
|
|
|
|
* contains an unknown feature bit (even if it is odd).
|
|
|
|
* - the payment uses a feature not included in
|
|
|
|
* `encrypted_recipient_data.allowed_features.features`.
|
|
|
|
*/
|
|
|
|
/* No features, this is easy */
|
|
|
|
if (!memeqzero(enc->allowed_features,
|
|
|
|
tal_bytelen(enc->allowed_features))) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-10-17 02:44:39 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:52:38 +01:00
|
|
|
if (!p->final) {
|
2022-10-17 02:44:38 +02:00
|
|
|
if (!handle_blinded_forward(p, amount_in, cltv_expiry,
|
2022-10-17 02:44:39 +02:00
|
|
|
p->tlv, enc, failtlvtype))
|
2022-10-17 02:44:38 +02:00
|
|
|
goto field_bad;
|
|
|
|
} else {
|
2022-10-17 02:44:39 +02:00
|
|
|
if (!handle_blinded_terminal(p, p->tlv, enc, failtlvtype))
|
2022-10-17 02:44:38 +02:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:31:52 +01:00
|
|
|
/* We stash path_id (if present and valid!) in payment_secret */
|
|
|
|
if (tal_bytelen(enc->path_id) == sizeof(*p->payment_secret)) {
|
|
|
|
p->payment_secret = tal_steal(p,
|
|
|
|
(struct secret *)enc->path_id);
|
|
|
|
} else
|
|
|
|
p->payment_secret = NULL;
|
|
|
|
|
|
|
|
/* FIXME: if we supported metadata, it would also be in path_id */
|
2022-10-17 02:44:38 +02:00
|
|
|
p->payment_metadata = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-04-07 08:55:33 +02:00
|
|
|
/* BOLT #4:
|
2022-11-09 02:30:10 +01:00
|
|
|
* - Otherwise (it is not part of a blinded route):
|
|
|
|
* - MUST return an error if `blinding_point` is set in the
|
|
|
|
* incoming `update_add_htlc` or `current_blinding_point`
|
|
|
|
* is present.
|
|
|
|
*/
|
|
|
|
if (blinding || p->tlv->blinding_point) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_ENCRYPTED_RECIPIENT_DATA;
|
2022-11-09 02:30:10 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:22:22 +01:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2023-04-07 08:55:33 +02:00
|
|
|
* - Otherwise (it is not part of a blinded route):
|
|
|
|
*...
|
2022-03-08 21:22:22 +01:00
|
|
|
* - MUST return an error if `amt_to_forward` or
|
|
|
|
* `outgoing_cltv_value` are not present.
|
|
|
|
*/
|
2022-10-17 02:44:39 +02:00
|
|
|
if (!p->tlv->amt_to_forward) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_AMT_TO_FORWARD;
|
2022-03-22 09:48:13 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
2022-10-17 02:44:39 +02:00
|
|
|
if (!p->tlv->outgoing_cltv_value) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_OUTGOING_CLTV_VALUE;
|
2022-03-22 09:48:13 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
2019-12-05 11:06:28 +01:00
|
|
|
|
2022-10-17 02:44:39 +02:00
|
|
|
p->amt_to_forward = amount_msat(*p->tlv->amt_to_forward);
|
|
|
|
p->outgoing_cltv = *p->tlv->outgoing_cltv_value;
|
2019-12-05 11:06:28 +01:00
|
|
|
|
2022-03-08 21:22:22 +01:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2023-04-07 08:55:33 +02:00
|
|
|
* - if it is not the final node:
|
|
|
|
* - MUST return an error if:
|
|
|
|
* - `short_channel_id` is not present,
|
2022-03-08 21:22:22 +01:00
|
|
|
*/
|
2023-01-12 04:52:38 +01:00
|
|
|
if (!p->final) {
|
2022-10-17 02:44:39 +02:00
|
|
|
if (!p->tlv->short_channel_id) {
|
2023-04-07 08:36:14 +02:00
|
|
|
*failtlvtype = TLV_PAYLOAD_SHORT_CHANNEL_ID;
|
2022-03-22 09:48:13 +01:00
|
|
|
goto field_bad;
|
|
|
|
}
|
2022-03-08 21:22:22 +01:00
|
|
|
p->forward_channel = tal_dup(p, struct short_channel_id,
|
2022-10-17 02:44:39 +02:00
|
|
|
p->tlv->short_channel_id);
|
2022-03-08 21:22:22 +01:00
|
|
|
p->total_msat = NULL;
|
|
|
|
} else {
|
|
|
|
p->forward_channel = NULL;
|
|
|
|
/* BOLT #4:
|
2023-04-07 08:55:33 +02:00
|
|
|
* - If it is the final node:
|
2022-03-08 21:22:22 +01:00
|
|
|
* - MUST treat `total_msat` as if it were equal to
|
|
|
|
* `amt_to_forward` if it is not present. */
|
|
|
|
p->total_msat = tal_dup(p, struct amount_msat,
|
|
|
|
&p->amt_to_forward);
|
|
|
|
}
|
2020-03-04 14:19:23 +01:00
|
|
|
|
2022-11-09 02:30:10 +01:00
|
|
|
/* Non-blinded is (currently) always by scid */
|
|
|
|
p->forward_node_id = NULL;
|
|
|
|
|
2022-03-08 21:22:22 +01:00
|
|
|
p->payment_secret = NULL;
|
2022-10-17 02:44:39 +02:00
|
|
|
if (p->tlv->payment_data) {
|
2022-03-08 21:22:22 +01:00
|
|
|
p->payment_secret = tal_dup(p, struct secret,
|
2022-10-17 02:44:39 +02:00
|
|
|
&p->tlv->payment_data->payment_secret);
|
2022-03-08 21:22:22 +01:00
|
|
|
tal_free(p->total_msat);
|
|
|
|
p->total_msat = tal(p, struct amount_msat);
|
|
|
|
*p->total_msat
|
2022-10-17 02:44:39 +02:00
|
|
|
= amount_msat(p->tlv->payment_data->total_msat);
|
2019-12-05 11:06:28 +01:00
|
|
|
}
|
2022-10-17 02:44:39 +02:00
|
|
|
if (p->tlv->payment_metadata)
|
2022-03-31 11:10:50 +02:00
|
|
|
p->payment_metadata
|
2022-10-17 02:44:39 +02:00
|
|
|
= tal_dup_talarr(p, u8, p->tlv->payment_metadata);
|
2022-03-31 11:10:50 +02:00
|
|
|
else
|
|
|
|
p->payment_metadata = NULL;
|
|
|
|
|
2022-10-17 02:44:38 +02:00
|
|
|
p->blinding = NULL;
|
|
|
|
|
2022-03-08 21:22:22 +01:00
|
|
|
return p;
|
2019-12-05 11:06:28 +01:00
|
|
|
|
2022-03-22 09:48:13 +01:00
|
|
|
field_bad:
|
|
|
|
*failtlvpos = tlv_field_offset(rs->raw_payload, tal_bytelen(rs->raw_payload),
|
|
|
|
*failtlvtype);
|
2022-10-17 02:44:39 +02:00
|
|
|
return tal_free(p);
|
2019-12-05 11:06:28 +01:00
|
|
|
}
|