diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 276111a6c..0203900c4 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -11,6 +11,7 @@ #include "routing.h" #include "secrets.h" #include "timeout.h" +#include "utils.h" #include #include #include @@ -355,7 +356,7 @@ static struct lightningd_state *lightningd_state(void) timers_init(&dstate->timers, time_mono()); txwatch_hash_init(&dstate->txwatches); txowatch_hash_init(&dstate->txowatches); - dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY + secp256k1_ctx = dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->wallet); diff --git a/utils.c b/utils.c index 4058a2bbf..1770fcbbe 100644 --- a/utils.c +++ b/utils.c @@ -1,6 +1,8 @@ #include "utils.h" #include +secp256k1_context *secp256k1_ctx; + char *tal_hexstr(const tal_t *ctx, const void *data, size_t len) { char *str = tal_arr(ctx, char, hex_str_size(len)); diff --git a/utils.h b/utils.h index 963bcbfb1..90f02ff6d 100644 --- a/utils.h +++ b/utils.h @@ -3,6 +3,9 @@ #include "config.h" #include #include +#include + +extern secp256k1_context *secp256k1_ctx; /* Allocate and fill in a hex-encoded string of this data. */ char *tal_hexstr(const tal_t *ctx, const void *data, size_t len); diff --git a/wire/fromwire.c b/wire/fromwire.c index 8e23c9666..ca3f9360a 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -1,3 +1,4 @@ +#include "utils.h" #include "wire.h" #include #include @@ -64,25 +65,25 @@ u64 fromwire_u64(const u8 **cursor, size_t *max) return be64_to_cpu(ret); } -void fromwire_pubkey(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct pubkey *pubkey) +void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey) { u8 der[PUBKEY_DER_LEN]; if (!fromwire(cursor, max, der, sizeof(der))) return; - if (!pubkey_from_der(secpctx, der, sizeof(der), pubkey)) + if (!pubkey_from_der(secp256k1_ctx, der, sizeof(der), pubkey)) fail_pull(cursor, max); } -void fromwire_signature(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct signature *sig) +void fromwire_signature(const u8 **cursor, size_t *max, struct signature *sig) { u8 compact[64]; if (!fromwire(cursor, max, compact, sizeof(compact))) return; - if (secp256k1_ecdsa_signature_parse_compact(secpctx, + if (secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx, &sig->sig, compact) != 1) fail_pull(cursor, max); @@ -122,12 +123,11 @@ void fromwire_pad_array(const u8 **cursor, size_t *max, u8 *arr, size_t num) fromwire(cursor, max, arr, num); } -void fromwire_signature_array(secp256k1_context *secpctx, - const u8 **cursor, size_t *max, +void fromwire_signature_array(const u8 **cursor, size_t *max, struct signature *arr, size_t num) { size_t i; for (i = 0; i < num; i++) - fromwire_signature(secpctx, cursor, max, arr + i); + fromwire_signature(cursor, max, arr + i); } diff --git a/wire/tools/generate-wire.py b/wire/tools/generate-wire.py index 8fc0b618c..9dd8c747f 100755 --- a/wire/tools/generate-wire.py +++ b/wire/tools/generate-wire.py @@ -8,20 +8,12 @@ import re Enumtype = namedtuple('Enumtype', ['name', 'value']) -# Field types that require a crypto context -crypto_types = [ - 'struct pubkey', - 'struct signature' -] - class Field(object): def __init__(self,message,name,size): self.message = message self.name = name.replace('-', '_') (self.typename, self.basesize) = Field._guess_type(message,self.name,size) - self.is_crypto = self.typename in crypto_types - try: if int(size) % self.basesize != 0: raise ValueError('Invalid size {} for {}.{} not a multiple of {}'.format(size,self.message,self.name,self.basesize)) @@ -110,7 +102,6 @@ class Message(object): self.name = name self.enum = enum self.fields = [] - self.is_crypto = False def checkLenField(self,field): for f in self.fields: @@ -132,7 +123,6 @@ class Message(object): if field.is_variable_size(): self.checkLenField(field) self.fields.append(field) - self.is_crypto |= field.is_crypto def print_structure(self): print('struct msg_{} {{'.format(self.name)); @@ -149,9 +139,7 @@ class Message(object): print('};') def print_fromwire(self,is_header): - crypto_arg = "secp256k1_context *secpctx, " if self.is_crypto else "" - - print('struct msg_{0} *fromwire_{0}({1}const tal_t *ctx, const void *p, size_t *len)'.format(self.name, crypto_arg), end='') + print('struct msg_{0} *fromwire_{0}(const tal_t *ctx, const void *p, size_t *len)'.format(self.name), end='') if is_header: print(';') @@ -168,25 +156,24 @@ class Message(object): if f.typename.startswith('struct '): basetype=f.typename[7:] - crypto_param = "secpctx, " if f.is_crypto else "" if f.is_array(): print("\t//1th case", f.name) - print('\tfromwire_{}_array({}&cursor, len, in->{}, {});' - .format(basetype, crypto_param, f.name, f.num_elems)) + print('\tfromwire_{}_array(&cursor, len, in->{}, {});' + .format(basetype, f.name, f.num_elems)) elif f.is_variable_size(): print("\t//2th case", f.name) print('\tin->{} = tal_arr(in, {}, in->{});' .format(f.name, f.typename, f.lenvar)) - print('\tfromwire_{}_array({}&cursor, len, in->{}, in->{});' - .format(basetype, crypto_param, f.name, f.lenvar)) + print('\tfromwire_{}_array(&cursor, len, in->{}, in->{});' + .format(basetype, f.name, f.lenvar)) elif f.is_assignable(): print("\t//3th case", f.name) print('\tin->{} = fromwire_{}(&cursor, len);' .format(f.name, basetype)) else: print("\t//4th case", f.name) - print('\tfromwire_{}({}&cursor, len, &in->{});' - .format(basetype, crypto_param, f.name)) + print('\tfromwire_{}(&cursor, len, &in->{});' + .format(basetype, f.name)) print('\n' '\tif (!cursor)\n' @@ -195,8 +182,7 @@ class Message(object): '}\n') def print_towire(self,is_header): - crypto_arg = "secp256k1_context *secpctx, " if self.is_crypto else "" - print('u8 *towire_{0}({1}const tal_t *ctx, const struct msg_{0} *out)'.format(self.name, crypto_arg), end='') + print('u8 *towire_{0}(const tal_t *ctx, const struct msg_{0} *out)'.format(self.name), end='') if is_header: print(';') @@ -212,19 +198,18 @@ class Message(object): if f.typename.startswith('struct '): basetype=f.typename[7:] - crypto_param = "secpctx, " if f.is_crypto else "" if f.is_array(): - print('\ttowire_{}_array({}&p, out->{}, {});' - .format(basetype, crypto_param, f.name, f.num_elems)) + print('\ttowire_{}_array(&p, out->{}, {});' + .format(basetype, f.name, f.num_elems)) elif f.is_variable_size(): - print('\ttowire_{}_array({}&p, out->{}, out->{});' - .format(basetype, crypto_param, f.name, f.lenvar)) + print('\ttowire_{}_array(&p, out->{}, out->{});' + .format(basetype, f.name, f.lenvar)) elif f.is_assignable(): - print('\ttowire_{}({}&p, out->{});' - .format(basetype, crypto_param, f.name)) + print('\ttowire_{}(&p, out->{});' + .format(basetype, f.name)) else: - print('\ttowire_{}({}&p, &out->{});' - .format(basetype, crypto_param, f.name)) + print('\ttowire_{}(&p, &out->{});' + .format(basetype, f.name)) print('\n' '\treturn p;\n' diff --git a/wire/towire.c b/wire/towire.c index 3beccaee7..abad65de2 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -1,3 +1,4 @@ +#include "utils.h" #include "wire.h" #include #include @@ -34,22 +35,22 @@ void towire_u64(u8 **pptr, u64 v) towire(pptr, &l, sizeof(l)); } -void towire_pubkey(secp256k1_context *secpctx, u8 **pptr, const struct pubkey *pubkey) +void towire_pubkey(u8 **pptr, const struct pubkey *pubkey) { u8 output[PUBKEY_DER_LEN]; size_t outputlen = sizeof(output); - secp256k1_ec_pubkey_serialize(secpctx, output, &outputlen, + secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen, &pubkey->pubkey, SECP256K1_EC_COMPRESSED); towire(pptr, output, outputlen); } -void towire_signature(secp256k1_context *secpctx, u8 **pptr, const struct signature *sig) +void towire_signature(u8 **pptr, const struct signature *sig) { u8 compact[64]; - secp256k1_ecdsa_signature_serialize_compact(secpctx, + secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, compact, &sig->sig); towire(pptr, compact, sizeof(compact)); } @@ -88,10 +89,10 @@ void towire_pad_array(u8 **pptr, const u8 *arr, size_t num) memset(*pptr + oldsize, 0, num); } -void towire_signature_array(secp256k1_context *secpctx, u8 **pptr, const struct signature *arr, size_t num) +void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num) { size_t i; for (i = 0; i < num; i++) - towire_signature(secpctx, pptr, arr+i); + towire_signature(pptr, arr+i); } diff --git a/wire/wire.h b/wire/wire.h index e6dd7e466..f803d761b 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -7,9 +7,6 @@ #include #include -/* FIXME: Move this declaration! */ -extern secp256k1_context *secp256k1_ctx; - struct pubkey; struct sha256; struct channel_id { @@ -22,8 +19,8 @@ struct ipv6 { }; void towire(u8 **pptr, const void *data, size_t len); -void towire_pubkey(secp256k1_context *secpctx, u8 **pptr, const struct pubkey *pubkey); -void towire_signature(secp256k1_context *secpctx, u8 **pptr, const struct signature *signature); +void towire_pubkey(u8 **pptr, const struct pubkey *pubkey); +void towire_signature(u8 **pptr, const struct signature *signature); void towire_channel_id(u8 **pptr, const struct channel_id *channel_id); void towire_sha256(u8 **pptr, const struct sha256 *sha256); void towire_ipv6(u8 **pptr, const struct ipv6 *ipv6); @@ -34,7 +31,7 @@ void towire_u64(u8 **pptr, u64 v); void towire_u8_array(u8 **pptr, const u8 *arr, size_t num); void towire_pad_array(u8 **pptr, const u8 *arr, size_t num); -void towire_signature_array(secp256k1_context *secpctx, u8 **pptr, const struct signature *arr, size_t num); +void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num); const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n); @@ -42,8 +39,8 @@ u8 fromwire_u8(const u8 **cursor, size_t *max); u16 fromwire_u16(const u8 **cursor, size_t *max); u32 fromwire_u32(const u8 **cursor, size_t *max); u64 fromwire_u64(const u8 **cursor, size_t *max); -void fromwire_pubkey(secp256k1_context *secpctx, const u8 **cursor, size_t *max, struct pubkey *pubkey); -void fromwire_signature(secp256k1_context *secpctx, const u8 **cursor, size_t *max, +void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey); +void fromwire_signature(const u8 **cursor, size_t *max, struct signature *signature); void fromwire_channel_id(const u8 **cursor, size_t *max, struct channel_id *channel_id); @@ -54,7 +51,7 @@ void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num); void fromwire_pad_array(const u8 **cursor, size_t *max, u8 *arr, size_t num); -void fromwire_signature_array(secp256k1_context *secpctx, const u8 **cursor, size_t *max, +void fromwire_signature_array(const u8 **cursor, size_t *max, struct signature *arr, size_t num); #endif /* LIGHTNING_WIRE_WIRE_H */