Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update offers to latest spec #4849

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ $(CCAN_OBJS) $(CDUMP_OBJS): $(CCAN_HEADERS) Makefile
# Except for CCAN, we treat everything else as dependent on external/ bitcoin/ common/ wire/ and all generated headers, and Makefile
$(ALL_OBJS): $(BITCOIN_HEADERS) $(COMMON_HEADERS) $(CCAN_HEADERS) $(WIRE_HEADERS) $(ALL_GEN_HEADERS) $(EXTERNAL_HEADERS) Makefile

# Test files can literally #include generated C files.
$(ALL_TEST_PROGRAMS:=.o): $(ALL_GEN_SOURCES)

update-ccan:
mv ccan ccan.old
DIR=$$(pwd)/ccan; cd ../ccan && ./tools/create-ccan-tree -a $$DIR `cd $$DIR.old/ccan && find * -name _info | sed s,/_info,, | $(SORT)` $(CCAN_NEW)
Expand Down
14 changes: 7 additions & 7 deletions bitcoin/pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,36 @@ void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
towire(pptr, output, outputlen);
}

void fromwire_pubkey32(const u8 **cursor, size_t *max, struct pubkey32 *pubkey32)
void fromwire_point32(const u8 **cursor, size_t *max, struct point32 *point32)
{
u8 raw[32];

if (!fromwire(cursor, max, raw, sizeof(raw)))
return;

if (secp256k1_xonly_pubkey_parse(secp256k1_ctx,
&pubkey32->pubkey,
&point32->pubkey,
raw) != 1) {
SUPERVERBOSE("not a valid point");
fromwire_fail(cursor, max);
}
}

void towire_pubkey32(u8 **pptr, const struct pubkey32 *pubkey32)
void towire_point32(u8 **pptr, const struct point32 *point32)
{
u8 output[32];

secp256k1_xonly_pubkey_serialize(secp256k1_ctx, output,
&pubkey32->pubkey);
&point32->pubkey);
towire(pptr, output, sizeof(output));
}

char *pubkey32_to_hexstr(const tal_t *ctx, const struct pubkey32 *pubkey32)
char *point32_to_hexstr(const tal_t *ctx, const struct point32 *point32)
{
u8 output[32];

secp256k1_xonly_pubkey_serialize(secp256k1_ctx, output,
&pubkey32->pubkey);
&point32->pubkey);
return tal_hexstr(ctx, output, sizeof(output));
}
REGISTER_TYPE_TO_STRING(pubkey32, pubkey32_to_hexstr);
REGISTER_TYPE_TO_STRING(point32, point32_to_hexstr);
15 changes: 10 additions & 5 deletions bitcoin/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ struct pubkey {
/* Define pubkey_eq (no padding) */
STRUCTEQ_DEF(pubkey, 0, pubkey.data);

struct pubkey32 {
struct point32 {
/* Unpacked pubkey (as used by libsecp256k1 internally) */
secp256k1_xonly_pubkey pubkey;
};
/* Define pubkey_eq (no padding) */
STRUCTEQ_DEF(pubkey32, 0, pubkey.data);
STRUCTEQ_DEF(point32, 0, pubkey.data);

/* Convert from hex string of DER (scriptPubKey from validateaddress) */
bool pubkey_from_hexstr(const char *derstr, size_t derlen, struct pubkey *key);
Expand Down Expand Up @@ -66,9 +66,14 @@ void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash);
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);

/* FIXME: Old spec uses pubkey32 */
#define pubkey32 point32
#define towire_pubkey32 towire_point32
#define fromwire_pubkey32 fromwire_point32

/* marshal/unmarshal functions */
void towire_pubkey32(u8 **pptr, const struct pubkey32 *pubkey);
void fromwire_pubkey32(const u8 **cursor, size_t *max, struct pubkey32 *pubkey);
void towire_point32(u8 **pptr, const struct point32 *pubkey);
void fromwire_point32(const u8 **cursor, size_t *max, struct point32 *pubkey);

char *pubkey32_to_hexstr(const tal_t *ctx, const struct pubkey32 *pubkey32);
char *point32_to_hexstr(const tal_t *ctx, const struct point32 *point32);
#endif /* LIGHTNING_BITCOIN_PUBKEY_H */
27 changes: 23 additions & 4 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 All @@ -70,7 +86,7 @@ static char *check_features_and_chain(const tal_t *ctx,
bool bolt12_check_signature(const struct tlv_field *fields,
const char *messagename,
const char *fieldname,
const struct pubkey32 *key,
const struct point32 *key,
const struct bip340sig *sig)
{
struct sha256 m, shash;
Expand All @@ -87,7 +103,7 @@ static char *check_signature(const tal_t *ctx,
const struct tlv_field *fields,
const char *messagename,
const char *fieldname,
const struct pubkey32 *node_id,
const struct point32 *node_id,
const struct bip340sig *sig)
{
if (!node_id)
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
7 changes: 6 additions & 1 deletion common/bolt12.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ struct tlv_invoice *invoice_decode_nosig(const tal_t *ctx,
bool bolt12_check_signature(const struct tlv_field *fields,
const char *messagename,
const char *fieldname,
const struct pubkey32 *key,
const struct point32 *key,
const struct bip340sig *sig);

/* Given a tal_arr of chains, does it contain this chain? */
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: 1 addition & 1 deletion common/bolt12_merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void sighash_from_merkle(const char *messagename,

/* We use the SHA(pubkey | publictweak); so reader cannot figure out the
* tweak and derive the base key */
void payer_key_tweak(const struct pubkey32 *bolt12,
void payer_key_tweak(const struct point32 *bolt12,
const u8 *publictweak, size_t publictweaklen,
struct sha256 *tweak)
{
Expand Down
2 changes: 1 addition & 1 deletion common/bolt12_merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void sighash_from_merkle(const char *messagename,
/**
* payer_key_tweak - get the actual tweak to use for a payer_key
*/
void payer_key_tweak(const struct pubkey32 *bolt12,
void payer_key_tweak(const struct point32 *bolt12,
const u8 *publictweak, size_t publictweaklen,
struct sha256 *tweak);

Expand Down
4 changes: 2 additions & 2 deletions common/gossmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1255,13 +1255,13 @@ int gossmap_node_get_feature(const struct gossmap *map,
/* There are two 33-byte pubkeys possible: choose the one which appears
* in the graph (otherwise payment will fail anyway). */
void gossmap_guess_node_id(const struct gossmap *map,
const struct pubkey32 *pubkey32,
const struct point32 *point32,
struct node_id *id)
{
id->k[0] = SECP256K1_TAG_PUBKEY_EVEN;
secp256k1_xonly_pubkey_serialize(secp256k1_ctx,
id->k + 1,
&pubkey32->pubkey);
&point32->pubkey);

/* If we don't find this, let's assume it's odd. */
if (!gossmap_find_node(map, id))
Expand Down
4 changes: 2 additions & 2 deletions common/gossmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <common/fp16.h>

struct node_id;
struct pubkey32;
struct point32;

struct gossmap_node {
/* Offset in memory map for node_announce, or 0. */
Expand Down Expand Up @@ -202,7 +202,7 @@ struct gossmap_chan *gossmap_next_chan(const struct gossmap *map,
/* Each x-only pubkey has two possible values: we can figure out which by
* examining the gossmap. */
void gossmap_guess_node_id(const struct gossmap *map,
const struct pubkey32 *pubkey32,
const struct point32 *point32,
struct node_id *id);

#endif /* LIGHTNING_COMMON_GOSSMAP_H */
6 changes: 3 additions & 3 deletions common/json_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ void json_add_pubkey(struct json_stream *response,
json_add_hex(response, fieldname, der, sizeof(der));
}

void json_add_pubkey32(struct json_stream *response,
const char *fieldname,
const struct pubkey32 *key)
void json_add_point32(struct json_stream *response,
const char *fieldname,
const struct point32 *key)
{
u8 output[32];

Expand Down
6 changes: 3 additions & 3 deletions common/json_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct lease_rates;
struct node_id;
struct preimage;
struct pubkey;
struct pubkey32;
struct point32;
struct secret;
struct short_channel_id;
struct short_channel_id_dir;
Expand Down Expand Up @@ -91,9 +91,9 @@ void json_add_pubkey(struct json_stream *response,
const struct pubkey *key);

/* '"fieldname" : "89abcdef..."' or "89abcdef..." if fieldname is NULL */
void json_add_pubkey32(struct json_stream *response,
void json_add_point32(struct json_stream *response,
const char *fieldname,
const struct pubkey32 *key);
const struct point32 *key);

/* '"fieldname" : "89abcdef..."' or "89abcdef..." if fieldname is NULL */
void json_add_bip340sig(struct json_stream *response,
Expand Down
2 changes: 1 addition & 1 deletion common/node_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool pubkey_from_node_id(struct pubkey *key, const struct node_id *id)
}

WARN_UNUSED_RESULT
bool pubkey32_from_node_id(struct pubkey32 *key, const struct node_id *id)
bool point32_from_node_id(struct point32 *key, const struct node_id *id)
{
struct pubkey k;
if (!pubkey_from_node_id(&k, id))
Expand Down
2 changes: 1 addition & 1 deletion common/node_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool pubkey_from_node_id(struct pubkey *key, const struct node_id *id);

/* Returns false if not a valid pubkey: relatively expensive */
WARN_UNUSED_RESULT
bool pubkey32_from_node_id(struct pubkey32 *key, const struct node_id *id);
bool point32_from_node_id(struct point32 *key, const struct node_id *id);

/* Convert to hex string of SEC1 encoding. */
char *node_id_to_hexstr(const tal_t *ctx, const struct node_id *id);
Expand Down
4 changes: 3 additions & 1 deletion 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 Expand Up @@ -208,7 +210,7 @@ int main(int argc, char *argv[])
strtok->end - strtok->start);
str = json_escape_unescape(tmpctx, esc);
actual = (string_to_data(tmpctx, str, strlen(str),
"lni", &dlen, &fail) != NULL);
"lno", &dlen, &fail) != NULL);
assert(actual == valid);
}
tal_free(tmpctx);
Expand Down
8 changes: 5 additions & 3 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 Expand Up @@ -330,14 +332,14 @@ int main(int argc, char *argv[])
fieldwires[1] = tlv(8, "\x03\xe8", 2);
/* description: 10USD every day */
fieldwires[2] = tlv(10, "10USD every day", strlen("10USD every day"));
/* vendor: rusty.ozlabs.org */
/* issuer: rusty.ozlabs.org */
fieldwires[3] = tlv(20, "rusty.ozlabs.org", strlen("rusty.ozlabs.org"));
/* recurrence: time_unit = 1, period = 1 */
fieldwires[4] = tlv(26, "\x01\x01", 2);
/* node_id: 4b9a1fa8e006f1e3937f65f66c408e6da8e1ca728ea43222a7381df1cc449605 */
fieldwires[5] = tlv(30, "\x4b\x9a\x1f\xa8\xe0\x06\xf1\xe3\x93\x7f\x65\xf6\x6c\x40\x8e\x6d\xa8\xe1\xca\x72\x8e\xa4\x32\x22\xa7\x38\x1d\xf1\xcc\x44\x96\x05", 32);

json_out("{\"comment\": \"offer test, currency = USD, amount = 1000, description = 10USD every day, vendor = rusty.ozlabs.org, recurrence = time_unit = 1, period = 1, node_id = 4b9a1fa8e006f1e3937f65f66c408e6da8e1ca728ea43222a7381df1cc449605\",");
json_out("{\"comment\": \"offer test, currency = USD, amount = 1000, description = 10USD every day, issuer = rusty.ozlabs.org, recurrence = time_unit = 1, period = 1, node_id = 4b9a1fa8e006f1e3937f65f66c408e6da8e1ca728ea43222a7381df1cc449605\",");
json_out("\"tlv\": \"offer\",");

all = concat(fieldwires[0], fieldwires[1], fieldwires[2],
Expand All @@ -364,7 +366,7 @@ int main(int argc, char *argv[])
json_out("{ \"desc\": \"1: currency+nonce and amount+nonce\", \"H(`LnBranch`,%s)\": \"%s\" },",
tal_hex(tmpctx, ordered(leaf[0], leaf[1])),
tal_hex(tmpctx, H(LnBranch, ordered(leaf[0], leaf[1]))));
json_out("{ \"desc\": \"2: description+nonce and vendor+nonce\", \"H(`LnBranch`,%s)\": \"%s\"},",
json_out("{ \"desc\": \"2: description+nonce and issuer+nonce\", \"H(`LnBranch`,%s)\": \"%s\"},",
tal_hex(tmpctx, ordered(leaf[2], leaf[3])),
tal_hex(tmpctx, H(LnBranch, ordered(leaf[2], leaf[3]))));
struct sha256 *b12 = H(LnBranch,
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
2 changes: 1 addition & 1 deletion common/type_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* This must match the type_to_string_ cases. */
union printable_types {
const struct pubkey *pubkey;
const struct pubkey32 *pubkey32;
const struct point32 *point32;
const struct node_id *node_id;
const struct bitcoin_txid *bitcoin_txid;
const struct bitcoin_blkid *bitcoin_blkid;
Expand Down
4 changes: 2 additions & 2 deletions contrib/pyln-testing/pyln/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def is_pubkey(checker, instance):
return False
return instance[0:2] == "02" or instance[0:2] == "03"

def is_pubkey32(checker, instance):
def is_point32(checker, instance):
"""x-only BIP-340 public key"""
if not checker.is_type(instance, "hex"):
return False
Expand Down Expand Up @@ -319,7 +319,7 @@ def is_txid(checker, instance):
"txid": is_txid,
"signature": is_signature,
"bip340sig": is_bip340sig,
"pubkey32": is_pubkey32,
"point32": is_point32,
"short_channel_id": is_short_channel_id,
})

Expand Down
Loading