common/onion: add blinding and enctlv encoding.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2020-04-11 12:53:09 +09:30
parent 490a819402
commit a85d40fc5e
4 changed files with 52 additions and 6 deletions

View File

@ -1,6 +1,7 @@
#include "common/onion.h"
#include <assert.h>
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <common/ecdh.h>
#include <common/sphinx.h>
#include <sodium/crypto_aead_chacha20poly1305.h>
@ -60,7 +61,9 @@ u8 *onion_nonfinal_hop(const tal_t *ctx,
bool use_tlv,
const struct short_channel_id *scid,
struct amount_msat forward,
u32 outgoing_cltv)
u32 outgoing_cltv,
const struct pubkey *blinding,
const u8 *enctlv)
{
if (use_tlv) {
struct tlv_tlv_payload *tlv = tlv_tlv_payload_new(tmpctx);
@ -84,9 +87,24 @@ u8 *onion_nonfinal_hop(const tal_t *ctx,
tlv->amt_to_forward = &tlv_amt;
tlv->outgoing_cltv_value = &tlv_cltv;
tlv->short_channel_id = &tlv_scid;
#if EXPERIMENTAL_FEATURES
struct tlv_tlv_payload_blinding_seed tlv_blinding;
struct tlv_tlv_payload_enctlv tlv_enctlv;
if (blinding) {
tlv_blinding.blinding_seed = *blinding;
tlv->blinding_seed = &tlv_blinding;
}
if (enctlv) {
tlv_enctlv.enctlv = cast_const(u8 *, enctlv);
tlv->enctlv = &tlv_enctlv;
}
#endif
return make_tlv_hop(ctx, tlv);
} else {
#if EXPERIMENTAL_FEATURES
if (blinding || enctlv)
return NULL;
#endif
return make_v0_hop(ctx, scid, forward, outgoing_cltv);
}
}
@ -96,6 +114,8 @@ u8 *onion_final_hop(const tal_t *ctx,
struct amount_msat forward,
u32 outgoing_cltv,
struct amount_msat total_msat,
const struct pubkey *blinding,
const u8 *enctlv,
const struct secret *payment_secret)
{
/* These go together! */
@ -132,12 +152,28 @@ u8 *onion_final_hop(const tal_t *ctx,
tlv_pdata.total_msat = total_msat.millisatoshis; /* Raw: TLV convert */
tlv->payment_data = &tlv_pdata;
}
#if EXPERIMENTAL_FEATURES
struct tlv_tlv_payload_blinding_seed tlv_blinding;
struct tlv_tlv_payload_enctlv tlv_enctlv;
if (blinding) {
tlv_blinding.blinding_seed = *blinding;
tlv->blinding_seed = &tlv_blinding;
}
if (enctlv) {
tlv_enctlv.enctlv = cast_const(u8 *, enctlv);
tlv->enctlv = &tlv_enctlv;
}
#endif
return make_tlv_hop(ctx, tlv);
} else {
static struct short_channel_id all_zero_scid;
/* No payment secrets in legacy format. */
if (payment_secret)
return NULL;
#if EXPERIMENTAL_FEATURES
if (blinding || enctlv)
return NULL;
#endif
return make_v0_hop(ctx, &all_zero_scid, forward, outgoing_cltv);
}
}

View File

@ -30,7 +30,9 @@ u8 *onion_nonfinal_hop(const tal_t *ctx,
bool use_tlv,
const struct short_channel_id *scid,
struct amount_msat forward,
u32 outgoing_cltv);
u32 outgoing_cltv,
const struct pubkey *blinding,
const u8 *enctlv);
/* Note that this can fail if we supply payment_secret and !use_tlv! */
u8 *onion_final_hop(const tal_t *ctx,
@ -38,6 +40,8 @@ u8 *onion_final_hop(const tal_t *ctx,
struct amount_msat forward,
u32 outgoing_cltv,
struct amount_msat total_msat,
const struct pubkey *blinding,
const u8 *enctlv,
const struct secret *payment_secret);
/**

View File

@ -89,13 +89,16 @@ static void do_generate(int argc, char **argv,
take(onion_final_hop(NULL,
use_tlv,
amt, i, amt,
NULL, NULL,
NULL)));
else
sphinx_add_hop(sp, &path[i],
take(onion_nonfinal_hop(NULL,
use_tlv,
&scid,
amt, i)));
amt, i,
NULL,
NULL)));
}
}

View File

@ -996,7 +996,9 @@ send_payment(struct lightningd *ld,
should_use_tlv(route[i].style),
&route[i + 1].channel_id,
route[i + 1].amount,
base_expiry + route[i + 1].delay)));
base_expiry + route[i + 1].delay,
route[i].blinding,
route[i].enctlv)));
}
/* And finally set the final hop to the special values in
@ -1025,7 +1027,8 @@ send_payment(struct lightningd *ld,
final_tlv,
route[i].amount,
base_expiry + route[i].delay,
total_msat, payment_secret);
total_msat, route[i].blinding, route[i].enctlv,
payment_secret);
if (!onion) {
return command_fail(cmd, PAY_DESTINATION_PERM_FAIL,
"Destination does not support"