mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-23 17:14:22 +01:00
wire: Removing global secpctx
Changed the generation of messages so that we pass in a reference to the secp256k1_context if it is required by the underlying primitive function. This gets rid of the global `secp256k1_ctx` variable and adheres closer to how we've been handing in the context so far.
This commit is contained in:
committed by
Rusty Russell
parent
e40509c6c3
commit
ca9fb0376a
@@ -64,25 +64,25 @@ u64 fromwire_u64(const u8 **cursor, size_t *max)
|
||||
return be64_to_cpu(ret);
|
||||
}
|
||||
|
||||
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
|
||||
void fromwire_pubkey(secp256k1_context *secpctx, 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(secp256k1_ctx, der, sizeof(der), pubkey))
|
||||
if (!pubkey_from_der(secpctx, der, sizeof(der), pubkey))
|
||||
fail_pull(cursor, max);
|
||||
}
|
||||
|
||||
void fromwire_signature(const u8 **cursor, size_t *max, struct signature *sig)
|
||||
void fromwire_signature(secp256k1_context *secpctx, 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(secp256k1_ctx,
|
||||
if (secp256k1_ecdsa_signature_parse_compact(secpctx,
|
||||
&sig->sig, compact)
|
||||
!= 1)
|
||||
fail_pull(cursor, max);
|
||||
@@ -122,11 +122,12 @@ void fromwire_pad_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
|
||||
fromwire(cursor, max, arr, num);
|
||||
}
|
||||
|
||||
void fromwire_signature_array(const u8 **cursor, size_t *max,
|
||||
void fromwire_signature_array(secp256k1_context *secpctx,
|
||||
const u8 **cursor, size_t *max,
|
||||
struct signature *arr, size_t num)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
fromwire_signature(cursor, max, arr + i);
|
||||
fromwire_signature(secpctx, cursor, max, arr + i);
|
||||
}
|
||||
|
||||
@@ -8,12 +8,20 @@ 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))
|
||||
@@ -102,6 +110,7 @@ class Message(object):
|
||||
self.name = name
|
||||
self.enum = enum
|
||||
self.fields = []
|
||||
self.is_crypto = False
|
||||
|
||||
def checkLenField(self,field):
|
||||
for f in self.fields:
|
||||
@@ -123,6 +132,7 @@ 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));
|
||||
@@ -139,7 +149,9 @@ class Message(object):
|
||||
print('};')
|
||||
|
||||
def print_fromwire(self,is_header):
|
||||
print('struct msg_{} *fromwire_{}(const tal_t *ctx, const void *p, size_t *len)'.format(self.name,self.name), end='')
|
||||
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='')
|
||||
|
||||
if is_header:
|
||||
print(';')
|
||||
@@ -156,20 +168,25 @@ 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('\tfromwire_{}_array(&cursor, len, in->{}, {});'
|
||||
.format(basetype, f.name, f.num_elems))
|
||||
print("\t//1th case", f.name)
|
||||
print('\tfromwire_{}_array({}&cursor, len, in->{}, {});'
|
||||
.format(basetype, crypto_param, 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, f.name, f.lenvar))
|
||||
print('\tfromwire_{}_array({}&cursor, len, in->{}, in->{});'
|
||||
.format(basetype, crypto_param, 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('\tfromwire_{}(&cursor, len, &in->{});'
|
||||
.format(basetype, f.name))
|
||||
print("\t//4th case", f.name)
|
||||
print('\tfromwire_{}({}&cursor, len, &in->{});'
|
||||
.format(basetype, crypto_param, f.name))
|
||||
|
||||
print('\n'
|
||||
'\tif (!cursor)\n'
|
||||
@@ -178,7 +195,8 @@ class Message(object):
|
||||
'}\n')
|
||||
|
||||
def print_towire(self,is_header):
|
||||
print('u8 *towire_{}(const tal_t *ctx, const struct msg_{} *out)'.format(self.name,self.name), end='')
|
||||
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='')
|
||||
|
||||
if is_header:
|
||||
print(';')
|
||||
@@ -194,18 +212,19 @@ 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, f.name, f.num_elems))
|
||||
print('\ttowire_{}_array({}&p, out->{}, {});'
|
||||
.format(basetype, crypto_param, f.name, f.num_elems))
|
||||
elif f.is_variable_size():
|
||||
print('\ttowire_{}_array(&p, out->{}, out->{});'
|
||||
.format(basetype, f.name, f.lenvar))
|
||||
print('\ttowire_{}_array({}&p, out->{}, out->{});'
|
||||
.format(basetype, crypto_param, f.name, f.lenvar))
|
||||
elif f.is_assignable():
|
||||
print('\ttowire_{}(&p, out->{});'
|
||||
.format(basetype, f.name))
|
||||
print('\ttowire_{}({}&p, out->{});'
|
||||
.format(basetype, crypto_param, f.name))
|
||||
else:
|
||||
print('\ttowire_{}(&p, &out->{});'
|
||||
.format(basetype, f.name))
|
||||
print('\ttowire_{}({}&p, &out->{});'
|
||||
.format(basetype, crypto_param, f.name))
|
||||
|
||||
print('\n'
|
||||
'\treturn p;\n'
|
||||
|
||||
@@ -34,22 +34,22 @@ void towire_u64(u8 **pptr, u64 v)
|
||||
towire(pptr, &l, sizeof(l));
|
||||
}
|
||||
|
||||
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
|
||||
void towire_pubkey(secp256k1_context *secpctx, u8 **pptr, const struct pubkey *pubkey)
|
||||
{
|
||||
u8 output[PUBKEY_DER_LEN];
|
||||
size_t outputlen = sizeof(output);
|
||||
|
||||
secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen,
|
||||
secp256k1_ec_pubkey_serialize(secpctx, output, &outputlen,
|
||||
&pubkey->pubkey,
|
||||
SECP256K1_EC_COMPRESSED);
|
||||
towire(pptr, output, outputlen);
|
||||
}
|
||||
|
||||
void towire_signature(u8 **pptr, const struct signature *sig)
|
||||
void towire_signature(secp256k1_context *secpctx, u8 **pptr, const struct signature *sig)
|
||||
{
|
||||
u8 compact[64];
|
||||
|
||||
secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
|
||||
secp256k1_ecdsa_signature_serialize_compact(secpctx,
|
||||
compact, &sig->sig);
|
||||
towire(pptr, compact, sizeof(compact));
|
||||
}
|
||||
@@ -88,10 +88,10 @@ void towire_pad_array(u8 **pptr, const u8 *arr, size_t num)
|
||||
memset(*pptr + oldsize, 0, num);
|
||||
}
|
||||
|
||||
void towire_signature_array(u8 **pptr, const struct signature *arr, size_t num)
|
||||
void towire_signature_array(secp256k1_context *secpctx, u8 **pptr, const struct signature *arr, size_t num)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
towire_signature(pptr, arr+i);
|
||||
towire_signature(secpctx, pptr, arr+i);
|
||||
}
|
||||
|
||||
12
wire/wire.h
12
wire/wire.h
@@ -22,8 +22,8 @@ struct ipv6 {
|
||||
};
|
||||
|
||||
void towire(u8 **pptr, const void *data, size_t len);
|
||||
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
|
||||
void towire_signature(u8 **pptr, const struct signature *signature);
|
||||
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_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 +34,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(u8 **pptr, const struct signature *arr, size_t num);
|
||||
void towire_signature_array(secp256k1_context *secpctx, 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 +42,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(const u8 **cursor, size_t *max, struct pubkey *pubkey);
|
||||
void fromwire_signature(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,
|
||||
struct signature *signature);
|
||||
void fromwire_channel_id(const u8 **cursor, size_t *max,
|
||||
struct channel_id *channel_id);
|
||||
@@ -54,7 +54,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(const u8 **cursor, size_t *max,
|
||||
void fromwire_signature_array(secp256k1_context *secpctx, const u8 **cursor, size_t *max,
|
||||
struct signature *arr, size_t num);
|
||||
|
||||
#endif /* LIGHTNING_WIRE_WIRE_H */
|
||||
|
||||
Reference in New Issue
Block a user