Skip to content

Commit

Permalink
BOLT 12: switch invoice_request/invoice to singular chain field.
Browse files Browse the repository at this point in the history
We keep the now-removed chains field, and in deprecated mode, we set it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-EXPERIMENTAL: bolt12: `chains` in invoice_request and invoice is deprecated, `chain` is used instead.
  • Loading branch information
rustyrussell authored and cdecker committed Oct 8, 2021
1 parent 45bf7a3 commit c92ce59
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 41 deletions.
23 changes: 21 additions & 2 deletions common/bolt12.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <common/bech32_util.h>
#include <common/bolt12.h>
#include <common/bolt12_merkle.h>
#include <common/configdir.h>
#include <common/features.h>
#include <secp256k1_schnorrsig.h>
#include <time.h>
Expand Down Expand Up @@ -46,6 +47,21 @@ bool bolt12_chains_match(const struct bitcoin_blkid *chains,
return false;
}

bool bolt12_chain_matches(const struct bitcoin_blkid *chain,
const struct chainparams *must_be_chain,
const struct bitcoin_blkid *deprecated_chains)
{
/* Obsolete: We used to put an array in here, but we only ever
* used a single value */
if (deprecated_apis && !chain)
chain = deprecated_chains;

if (!chain)
chain = &chainparams_for_network("bitcoin")->genesis_blockhash;

return bitcoin_blkid_eq(chain, &must_be_chain->genesis_blockhash);
}

static char *check_features_and_chain(const tal_t *ctx,
const struct feature_set *our_features,
const struct chainparams *must_be_chain,
Expand Down Expand Up @@ -231,7 +247,9 @@ struct tlv_invoice_request *invrequest_decode(const tal_t *ctx,
*fail = check_features_and_chain(ctx,
our_features, must_be_chain,
invrequest->features,
invrequest->chains);
invrequest->chain
? invrequest->chain
: invrequest->chains);
if (*fail)
return tal_free(invrequest);

Expand Down Expand Up @@ -270,7 +288,8 @@ struct tlv_invoice *invoice_decode_nosig(const tal_t *ctx,
*fail = check_features_and_chain(ctx,
our_features, must_be_chain,
invoice->features,
invoice->chains);
invoice->chain
? invoice->chain : invoice->chains);
if (*fail)
return tal_free(invoice);

Expand Down
5 changes: 5 additions & 0 deletions common/bolt12.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ bool bolt12_check_signature(const struct tlv_field *fields,
bool bolt12_chains_match(const struct bitcoin_blkid *chains,
const struct chainparams *must_be_chain);

/* Given a single bolt12 chain, does it match? (NULL == bitcoin) */
bool bolt12_chain_matches(const struct bitcoin_blkid *chain,
const struct chainparams *must_be_chain,
const struct bitcoin_blkid *deprecated_chains);

/* Given a basetime, when does period N start? */
u64 offer_period_start(u64 basetime, size_t n,
const struct tlv_offer_recurrence *recurrence);
Expand Down
2 changes: 2 additions & 0 deletions common/test/run-bolt12_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>

bool deprecated_apis = false;

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
Expand Down
2 changes: 2 additions & 0 deletions common/test/run-bolt12_merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
/* Definition of n1 from the spec */
#include <wire/peer_wire.h>

bool deprecated_apis = false;

/* AUTOGENERATED MOCKS START */
/* Generated stub for features_unsupported */
int features_unsupported(const struct feature_set *our_features UNNEEDED,
Expand Down
2 changes: 2 additions & 0 deletions common/test/run-bolt12_period.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <ccan/tal/path/path.h>
#include <common/setup.h>

bool deprecated_apis = false;

/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_asset_is_main */
bool amount_asset_is_main(struct amount_asset *asset UNNEEDED)
Expand Down
19 changes: 13 additions & 6 deletions devtools/bolt12-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define ERROR_USAGE 3

static bool well_formed = true;
bool deprecated_apis = true;

/* Tal wrappers for opt. */
static void *opt_allocfn(size_t size)
Expand Down Expand Up @@ -72,6 +73,12 @@ static void print_chains(const struct bitcoin_blkid *chains)
printf("\n");
}

static void print_chain(const struct bitcoin_blkid *chain)
{
printf("chain: %s\n",
type_to_string(tmpctx, struct bitcoin_blkid, chain));
}

static bool print_amount(const struct bitcoin_blkid *chains,
const char *iso4217, u64 amount)
{
Expand Down Expand Up @@ -532,16 +539,16 @@ int main(int argc, char *argv[])
if (!invreq)
errx(ERROR_BAD_DECODE, "Bad invoice_request: %s", fail);

if (invreq->chains)
print_chains(invreq->chains);
if (invreq->chain)
print_chain(invreq->chain);
if (must_have(invreq, payer_key))
print_payer_key(invreq->payer_key, invreq->payer_info);
if (invreq->payer_note)
print_payer_note(invreq->payer_note);
if (must_have(invreq, offer_id))
print_offer_id(invreq->offer_id);
if (must_have(invreq, amount))
well_formed &= print_amount(invreq->chains,
well_formed &= print_amount(invreq->chain,
NULL,
*invreq->amount);
if (invreq->features)
Expand Down Expand Up @@ -569,14 +576,14 @@ int main(int argc, char *argv[])
if (!invoice)
errx(ERROR_BAD_DECODE, "Bad invoice: %s", fail);

if (invoice->chains)
print_chains(invoice->chains);
if (invoice->chain)
print_chain(invoice->chain);

if (invoice->offer_id) {
print_offer_id(invoice->offer_id);
}
if (must_have(invoice, amount))
well_formed &= print_amount(invoice->chains,
well_formed &= print_amount(invoice->chain,
NULL,
*invoice->amount);
if (must_have(invoice, description))
Expand Down
34 changes: 13 additions & 21 deletions doc/schemas/decode.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,11 @@
"type": "bip340sig",
"description": "BIP-340 signature of the *node_id* on this offer"
},
"chains": {
"type": "array",
"description": "which blockchains this offer is for (missing implies bitcoin mainnet only)",
"items": {
"type": "hex",
"description": "the genesis blockhash",
"maxLength": 64,
"minLength": 64
}
"chain": {
"type": "hex",
"description": "which blockchain this invoice is for (missing implies bitcoin mainnet only)",
"maxLength": 64,
"minLength": 64
},
"amount_msat": {
"type": "msat",
Expand Down Expand Up @@ -450,7 +446,7 @@
"offer_id": { },
"node_id": { },
"signature": { },
"chains": { },
"chain": { },
"amount_msat": { },
"send_invoice": { },
"refund_for": { },
Expand Down Expand Up @@ -551,19 +547,15 @@
"valid": { },
"offer_id": {
"type": "hex",
"description": "the id of this offer (merkle hash of non-signature fields)",
"description": "the id of the offer this is requesting (merkle hash of non-signature fields)",
"maxLength": 64,
"minLength": 64
},
"chains": {
"type": "array",
"description": "which blockchains this offer is for (missing implies bitcoin mainnet only)",
"items": {
"type": "hex",
"description": "the genesis blockhash",
"maxLength": 64,
"minLength": 64
}
"chain": {
"type": "hex",
"description": "which blockchain this invoice_request is for (missing implies bitcoin mainnet only)",
"maxLength": 64,
"minLength": 64
},
"amount_msat": {
"type": "msat",
Expand Down Expand Up @@ -620,7 +612,7 @@
"type": { },
"valid": { },
"offer_id": { },
"chains": { },
"chain": { },
"amount_msat": { },
"features": { },
"quantity": { },
Expand Down
16 changes: 12 additions & 4 deletions plugins/fetchinvoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,12 @@ static struct command_result *json_fetchinvoice(struct command *cmd,
* - the bitcoin chain is implied as the first and only entry.
*/
if (!streq(chainparams->network_name, "bitcoin")) {
invreq->chains = tal_arr(invreq, struct bitcoin_blkid, 1);
invreq->chains[0] = chainparams->genesis_blockhash;
if (deprecated_apis) {
invreq->chains = tal_arr(invreq, struct bitcoin_blkid, 1);
invreq->chains[0] = chainparams->genesis_blockhash;
}
invreq->chain = tal_dup(invreq, struct bitcoin_blkid,
&chainparams->genesis_blockhash);
}

invreq->features
Expand Down Expand Up @@ -1790,8 +1794,12 @@ static struct command_result *json_sendinvoice(struct command *cmd,
* - the bitcoin chain is implied as the first and only entry.
*/
if (!streq(chainparams->network_name, "bitcoin")) {
sent->inv->chains = tal_arr(sent->inv, struct bitcoin_blkid, 1);
sent->inv->chains[0] = chainparams->genesis_blockhash;
if (deprecated_apis) {
sent->inv->chains = tal_arr(sent->inv, struct bitcoin_blkid, 1);
sent->inv->chains[0] = chainparams->genesis_blockhash;
}
sent->inv->chain = tal_dup(sent->inv, struct bitcoin_blkid,
&chainparams->genesis_blockhash);
}

sent->inv->features
Expand Down
8 changes: 7 additions & 1 deletion plugins/offers.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ static void json_add_b12_invoice(struct json_stream *js,

if (invoice->chains)
json_add_chains(js, invoice->chains);
if (invoice->chain)
json_add_sha256(js, "chain", &invoice->chain->shad.sha);
if (invoice->offer_id)
json_add_sha256(js, "offer_id", invoice->offer_id);

Expand Down Expand Up @@ -700,7 +702,8 @@ static void json_add_b12_invoice(struct json_stream *js,
json_add_u32(js, "min_final_cltv_expiry", 18);

if (invoice->fallbacks)
valid &= json_add_fallbacks(js, invoice->chains,
valid &= json_add_fallbacks(js,
invoice->chain ? invoice->chain : invoice->chains,
invoice->fallbacks->fallbacks);

/* BOLT-offers #12:
Expand Down Expand Up @@ -749,6 +752,9 @@ static void json_add_invoice_request(struct json_stream *js,

if (invreq->chains)
json_add_chains(js, invreq->chains);
if (invreq->chain)
json_add_sha256(js, "chain", &invreq->chain->shad.sha);

/* BOLT-offers #12:
* - MUST fail the request if `payer_key` is not present.
* - MUST fail the request if `chains` does not include (or imply) a supported chain.
Expand Down
4 changes: 2 additions & 2 deletions plugins/offers_inv_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ struct command_result *handle_invoice(struct command *cmd,
* - MUST fail the request if `chains` does not include (or imply) a
* supported chain.
*/
if (!bolt12_chains_match(inv->inv->chains, chainparams)) {
if (!bolt12_chain_matches(inv->inv->chain, chainparams, inv->inv->chains)) {
return fail_inv(cmd, inv,
"Wrong chains %s",
tal_hex(tmpctx, inv->inv->chains));
tal_hex(tmpctx, inv->inv->chain));
}

/* BOLT-offers #12:
Expand Down
16 changes: 11 additions & 5 deletions plugins/offers_invreq_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,14 @@ static struct command_result *listoffers_done(struct command *cmd,
* - MUST specify `chains` the offer is valid for.
*/
if (!streq(chainparams->network_name, "bitcoin")) {
ir->inv->chains = tal_arr(ir->inv, struct bitcoin_blkid, 1);
ir->inv->chains[0] = chainparams->genesis_blockhash;
if (deprecated_apis) {
ir->inv->chains = tal_arr(ir->inv, struct bitcoin_blkid, 1);
ir->inv->chains[0] = chainparams->genesis_blockhash;
}
ir->inv->chain = tal_dup(ir->inv, struct bitcoin_blkid,
&chainparams->genesis_blockhash);
}

/* BOLT-offers #12:
* - MUST set `offer_id` to the id of the offer.
*/
Expand Down Expand Up @@ -883,10 +888,11 @@ struct command_result *handle_invoice_request(struct command *cmd,
* - MUST fail the request if `chains` does not include (or imply) a
* supported chain.
*/
if (!bolt12_chains_match(ir->invreq->chains, chainparams)) {
if (!bolt12_chain_matches(ir->invreq->chain, chainparams,
ir->invreq->chains)) {
return fail_invreq(cmd, ir,
"Wrong chains %s",
tal_hex(tmpctx, ir->invreq->chains));
"Wrong chain %s",
tal_hex(tmpctx, ir->invreq->chain));
}

/* BOLT-offers #12:
Expand Down
4 changes: 4 additions & 0 deletions wire/bolt12_exp_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ subtypedata,blinded_path,num_hops,byte,
subtypedata,blinded_path,path,onionmsg_path,num_hops
tlvtype,invoice_request,chains,2
tlvdata,invoice_request,chains,chains,chain_hash,...
tlvtype,invoice_request,chain,3
tlvdata,invoice_request,chain,chain,chain_hash,
tlvtype,invoice_request,offer_id,4
tlvdata,invoice_request,offer_id,offer_id,sha256,
tlvtype,invoice_request,amount,8
Expand All @@ -68,6 +70,8 @@ tlvtype,invoice_request,payer_signature,240
tlvdata,invoice_request,payer_signature,sig,bip340sig,
tlvtype,invoice,chains,2
tlvdata,invoice,chains,chains,chain_hash,...
tlvtype,invoice,chain,3
tlvdata,invoice,chain,chain,chain_hash,
tlvtype,invoice,offer_id,4
tlvdata,invoice,offer_id,offer_id,sha256,
tlvtype,invoice,amount,8
Expand Down
4 changes: 4 additions & 0 deletions wire/bolt12_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ subtypedata,blinded_path,num_hops,byte,
subtypedata,blinded_path,path,onionmsg_path,num_hops
tlvtype,invoice_request,chains,2
tlvdata,invoice_request,chains,chains,chain_hash,...
tlvtype,invoice_request,chain,3
tlvdata,invoice_request,chain,chain,chain_hash,
tlvtype,invoice_request,offer_id,4
tlvdata,invoice_request,offer_id,offer_id,sha256,
tlvtype,invoice_request,amount,8
Expand All @@ -68,6 +70,8 @@ tlvtype,invoice_request,payer_signature,240
tlvdata,invoice_request,payer_signature,sig,bip340sig,
tlvtype,invoice,chains,2
tlvdata,invoice,chains,chains,chain_hash,...
tlvtype,invoice,chain,3
tlvdata,invoice,chain,chain,chain_hash,
tlvtype,invoice,offer_id,4
tlvdata,invoice,offer_id,offer_id,sha256,
tlvtype,invoice,amount,8
Expand Down

0 comments on commit c92ce59

Please sign in to comment.