diff --git a/lightningd/dev_newhtlc.c b/lightningd/dev_newhtlc.c index 29bf7402b..17ad3adeb 100644 --- a/lightningd/dev_newhtlc.c +++ b/lightningd/dev_newhtlc.c @@ -62,6 +62,7 @@ static void json_dev_newhtlc(struct command *cmd, u8 *onion; struct htlc_end *hend; struct pubkey *path = tal_arrz(cmd, struct pubkey, 1); + struct sha256 *shared_secrets; if (!json_get_params(buffer, params, "peerid", &peeridtok, @@ -126,7 +127,7 @@ static void json_dev_newhtlc(struct command *cmd, path[0] = *peer->id; randombytes_buf(&sessionkey, sizeof(sessionkey)); packet = create_onionpacket(cmd, path, hopsdata, sessionkey, rhash.u.u8, - sizeof(rhash)); + sizeof(rhash), &shared_secrets); onion = serialize_onionpacket(cmd, packet); log_debug(peer->log, "JSON command to add new HTLC"); @@ -137,6 +138,7 @@ static void json_dev_newhtlc(struct command *cmd, hend->msatoshis = msatoshi; hend->other_end = NULL; hend->pay_command = (void *)cmd; + hend->path_secrets = tal_steal(hend, shared_secrets); /* FIXME: If subdaemon dies? */ msg = towire_channel_offer_htlc(cmd, msatoshi, expiry, &rhash, onion); diff --git a/lightningd/htlc_end.h b/lightningd/htlc_end.h index 47fc11c41..2d0948eba 100644 --- a/lightningd/htlc_end.h +++ b/lightningd/htlc_end.h @@ -28,7 +28,14 @@ struct htlc_end { u32 outgoing_cltv_value; u32 cltv_expiry; struct sha256 payment_hash; + + /* If we are forwarding, remember the shared secret for an + * eventual reply */ struct sha256 shared_secret; + + /* If we are the origin, remember all shared secrets, so we + * can unwrap an eventual reply */ + struct sha256 *path_secrets; }; static inline const struct htlc_end *keyof_htlc_end(const struct htlc_end *e) diff --git a/lightningd/pay.c b/lightningd/pay.c index ff125e325..3573aa528 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -163,6 +163,7 @@ static void json_sendpay(struct command *cmd, u64 amount, lastamount; struct onionpacket *packet; u8 *msg; + struct sha256 *path_secrets; if (!json_get_params(buffer, params, "route", &routetok, @@ -321,8 +322,8 @@ static void json_sendpay(struct command *cmd, randombytes_buf(&sessionkey, sizeof(sessionkey)); /* Onion will carry us from first peer onwards. */ - packet = create_onionpacket(cmd, ids, hop_data, sessionkey, - rhash.u.u8, sizeof(struct sha256)); + packet = create_onionpacket(cmd, ids, hop_data, sessionkey, rhash.u.u8, + sizeof(struct sha256), &path_secrets); onion = serialize_onionpacket(cmd, packet); if (pc) @@ -344,6 +345,7 @@ static void json_sendpay(struct command *cmd, pc->out->msatoshis = amount; pc->out->other_end = NULL; pc->out->pay_command = pc; + pc->out->path_secrets = tal_steal(pc->out, path_secrets); log_info(ld->log, "Sending %"PRIu64" over %zu hops to deliver %"PRIu64, amount, n_hops, lastamount); diff --git a/lightningd/sphinx.c b/lightningd/sphinx.c index faeaed4fc..b7cedfd72 100644 --- a/lightningd/sphinx.c +++ b/lightningd/sphinx.c @@ -362,7 +362,8 @@ struct onionpacket *create_onionpacket( struct hop_data hops_data[], const u8 *sessionkey, const u8 *assocdata, - const size_t assocdatalen + const size_t assocdatalen, + struct sha256 **path_secrets ) { struct onionpacket *packet = talz(ctx, struct onionpacket); @@ -403,6 +404,11 @@ struct onionpacket *create_onionpacket( } memcpy(packet->mac, nexthmac, sizeof(nexthmac)); memcpy(&packet->ephemeralkey, ¶ms[0].ephemeralkey, sizeof(secp256k1_pubkey)); + + *path_secrets = tal_arr(ctx, struct sha256, num_hops); + for (i=0; i