mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
Move protobuf<->bitcoin converters out of bitcoin/
They're lightning-specific. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
612d713470
commit
442f321585
2
Makefile
2
Makefile
@ -7,7 +7,7 @@ PROGRAMS := open-channel open-anchor-scriptsigs leak-anchor-sigs open-commit-sig
|
||||
|
||||
BITCOIN_OBJS := bitcoin/address.o bitcoin/base58.o bitcoin/pubkey.o bitcoin/script.o bitcoin/shadouble.o bitcoin/signature.o bitcoin/tx.o
|
||||
|
||||
HELPER_OBJS := lightning.pb-c.o pkt.o permute_tx.o anchor.o commit_tx.o opt_bits.o close_tx.o find_p2sh_out.o
|
||||
HELPER_OBJS := lightning.pb-c.o pkt.o permute_tx.o anchor.o commit_tx.o opt_bits.o close_tx.o find_p2sh_out.o protobuf_convert.o
|
||||
|
||||
CCAN_OBJS := ccan-crypto-sha256.o ccan-crypto-shachain.o ccan-err.o ccan-tal.o ccan-tal-str.o ccan-take.o ccan-list.o ccan-str.o ccan-opt-helpers.o ccan-opt.o ccan-opt-parse.o ccan-opt-usage.o ccan-read_write_all.o ccan-str-hex.o ccan-tal-grab_file.o ccan-noerr.o
|
||||
|
||||
|
1
anchor.c
1
anchor.c
@ -6,6 +6,7 @@
|
||||
#include "bitcoin/script.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include <ccan/err/err.h>
|
||||
#include "protobuf_convert.h"
|
||||
|
||||
struct bitcoin_tx *anchor_tx_create(const tal_t *ctx,
|
||||
const OpenChannel *o1,
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "pubkey.h"
|
||||
#include <openssl/ecdsa.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* Must agree on key validity with bitcoin! Stolen from bitcoin/src/pubkey.h's
|
||||
* GetLen:
|
||||
@ -18,11 +19,11 @@ static unsigned int GetLen(unsigned char chHeader)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool valid_pubkey(const BitcoinPubkey *key)
|
||||
bool pubkey_valid(const u8 *first_char, size_t len)
|
||||
{
|
||||
if (key->key.len < 1)
|
||||
if (len < 1)
|
||||
return false;
|
||||
return (key->key.len == GetLen(key->key.data[0]));
|
||||
return (len == GetLen(*first_char));
|
||||
}
|
||||
|
||||
size_t pubkey_len(const struct pubkey *key)
|
||||
@ -33,27 +34,6 @@ size_t pubkey_len(const struct pubkey *key)
|
||||
return len;
|
||||
}
|
||||
|
||||
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key)
|
||||
{
|
||||
BitcoinPubkey *p = tal(ctx, BitcoinPubkey);
|
||||
|
||||
bitcoin_pubkey__init(p);
|
||||
p->key.len = pubkey_len(key);
|
||||
p->key.data = tal_dup_arr(p, u8, key->key, p->key.len, 0);
|
||||
|
||||
assert(valid_pubkey(p));
|
||||
return p;
|
||||
}
|
||||
|
||||
bool proto_to_pubkey(const BitcoinPubkey *pb, struct pubkey *key)
|
||||
{
|
||||
if (!valid_pubkey(pb))
|
||||
return false;
|
||||
|
||||
memcpy(key->key, pb->key.data, pb->key.len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool pubkey_from_hexstr(const char *str, struct pubkey *key)
|
||||
{
|
||||
size_t slen = strlen(str), dlen;
|
||||
|
@ -2,20 +2,18 @@
|
||||
#define LIGHTNING_BITCOIN_PUBKEY_H
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
#include "../lightning.pb-c.h"
|
||||
|
||||
struct pubkey {
|
||||
u8 key[65];
|
||||
};
|
||||
|
||||
/* Convert to-from protobuf to internal representation. */
|
||||
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key);
|
||||
bool proto_to_pubkey(const BitcoinPubkey *pb, struct pubkey *key);
|
||||
|
||||
/* 33 or 65 bytes? */
|
||||
size_t pubkey_len(const struct pubkey *key);
|
||||
|
||||
/* Convert from hex string (scriptPubKey from validateaddress) */
|
||||
bool pubkey_from_hexstr(const char *str, struct pubkey *key);
|
||||
|
||||
/* For conversion routines in protobuf_convert.c */
|
||||
bool pubkey_valid(const u8 *first_char, size_t len);
|
||||
|
||||
#endif /* LIGHTNING_PUBKEY_H */
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <openssl/ripemd.h>
|
||||
#include <ccan/endian/endian.h>
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* Some standard ops */
|
||||
#define OP_PUSHBYTES(val) (val)
|
||||
|
@ -237,40 +237,3 @@ bool check_2of2_sig(struct bitcoin_tx *tx, size_t input_num,
|
||||
return check_signed_hash(&hash, &sig1->sig, key1)
|
||||
&& check_signed_hash(&hash, &sig2->sig, key2);
|
||||
}
|
||||
|
||||
Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig)
|
||||
{
|
||||
Signature *pb = tal(ctx, Signature);
|
||||
signature__init(pb);
|
||||
|
||||
assert((sig->s[31] & 1) == 0);
|
||||
|
||||
/* Kill me now... */
|
||||
memcpy(&pb->r1, sig->r, 8);
|
||||
memcpy(&pb->r2, sig->r + 8, 8);
|
||||
memcpy(&pb->r3, sig->r + 16, 8);
|
||||
memcpy(&pb->r4, sig->r + 24, 8);
|
||||
memcpy(&pb->s1, sig->s, 8);
|
||||
memcpy(&pb->s2, sig->s + 8, 8);
|
||||
memcpy(&pb->s3, sig->s + 16, 8);
|
||||
memcpy(&pb->s4, sig->s + 24, 8);
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
bool proto_to_signature(const Signature *pb, struct signature *sig)
|
||||
{
|
||||
/* Kill me again. */
|
||||
memcpy(sig->r, &pb->r1, 8);
|
||||
memcpy(sig->r + 8, &pb->r2, 8);
|
||||
memcpy(sig->r + 16, &pb->r3, 8);
|
||||
memcpy(sig->r + 24, &pb->r4, 8);
|
||||
memcpy(sig->s, &pb->s1, 8);
|
||||
memcpy(sig->s + 8, &pb->s2, 8);
|
||||
memcpy(sig->s + 16, &pb->s3, 8);
|
||||
memcpy(sig->s + 24, &pb->s4, 8);
|
||||
|
||||
/* S must be even */
|
||||
return (sig->s[31] & 1) == 0;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <openssl/ecdsa.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
#include "../lightning.pb-c.h"
|
||||
|
||||
enum sighash_type {
|
||||
SIGHASH_ALL = 1,
|
||||
@ -47,8 +46,4 @@ bool check_2of2_sig(struct bitcoin_tx *tx, size_t input_num,
|
||||
const struct bitcoin_signature *sig1,
|
||||
const struct bitcoin_signature *sig2);
|
||||
|
||||
/* Convert to-from protobuf to internal representation. */
|
||||
Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig);
|
||||
bool proto_to_signature(const Signature *pb, struct signature *sig);
|
||||
|
||||
#endif /* LIGHTNING_BITCOIN_SIGNATURE_H */
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "bitcoin/signature.h"
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "close_tx.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "permute_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "pkt.h"
|
||||
#include "protobuf_convert.h"
|
||||
|
||||
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
||||
OpenChannel *ours,
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "permute_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "pkt.h"
|
||||
#include "protobuf_convert.h"
|
||||
|
||||
struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
|
||||
OpenChannel *ours,
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "close_tx.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include "bitcoin/address.h"
|
||||
#include "opt_bits.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "bitcoin/signature.h"
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "bitcoin/tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "bitcoin/shadouble.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
#include "opt_bits.h"
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "bitcoin/signature.h"
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
24
pkt.c
24
pkt.c
@ -6,8 +6,8 @@
|
||||
#include "bitcoin/address.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "protobuf_convert.h"
|
||||
|
||||
#include <stdio.h>
|
||||
static struct pkt *to_pkt(const tal_t *ctx, Pkt__PktCase type, void *msg)
|
||||
{
|
||||
struct pkt *ret;
|
||||
@ -26,28 +26,6 @@ static struct pkt *to_pkt(const tal_t *ctx, Pkt__PktCase type, void *msg)
|
||||
return ret;
|
||||
}
|
||||
|
||||
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash)
|
||||
{
|
||||
Sha256Hash *h = tal(ctx, Sha256Hash);
|
||||
sha256_hash__init(h);
|
||||
|
||||
/* Kill me now... */
|
||||
memcpy(&h->a, hash->u.u8, 8);
|
||||
memcpy(&h->b, hash->u.u8 + 8, 8);
|
||||
memcpy(&h->c, hash->u.u8 + 16, 8);
|
||||
memcpy(&h->d, hash->u.u8 + 24, 8);
|
||||
return h;
|
||||
}
|
||||
|
||||
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash)
|
||||
{
|
||||
/* Kill me again. */
|
||||
memcpy(hash->u.u8, &pb->a, 8);
|
||||
memcpy(hash->u.u8 + 8, &pb->b, 8);
|
||||
memcpy(hash->u.u8 + 16, &pb->c, 8);
|
||||
memcpy(hash->u.u8 + 24, &pb->d, 8);
|
||||
}
|
||||
|
||||
struct pkt *openchannel_pkt(const tal_t *ctx,
|
||||
u64 seed,
|
||||
const struct sha256 *revocation_hash,
|
||||
|
3
pkt.h
3
pkt.h
@ -119,7 +119,4 @@ struct pkt *update_signature_pkt(const tal_t *ctx,
|
||||
struct pkt *update_complete_pkt(const tal_t *ctx,
|
||||
const struct sha256 *revocation_preimage);
|
||||
|
||||
/* Useful helper for allocating & populating a protobuf Sha256Hash */
|
||||
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash);
|
||||
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash);
|
||||
#endif /* LIGHTNING_PKT_H */
|
||||
|
83
protobuf_convert.c
Normal file
83
protobuf_convert.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include "protobuf_convert.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
|
||||
Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig)
|
||||
{
|
||||
Signature *pb = tal(ctx, Signature);
|
||||
signature__init(pb);
|
||||
|
||||
assert((sig->s[31] & 1) == 0);
|
||||
|
||||
/* Kill me now... */
|
||||
memcpy(&pb->r1, sig->r, 8);
|
||||
memcpy(&pb->r2, sig->r + 8, 8);
|
||||
memcpy(&pb->r3, sig->r + 16, 8);
|
||||
memcpy(&pb->r4, sig->r + 24, 8);
|
||||
memcpy(&pb->s1, sig->s, 8);
|
||||
memcpy(&pb->s2, sig->s + 8, 8);
|
||||
memcpy(&pb->s3, sig->s + 16, 8);
|
||||
memcpy(&pb->s4, sig->s + 24, 8);
|
||||
|
||||
return pb;
|
||||
}
|
||||
|
||||
bool proto_to_signature(const Signature *pb, struct signature *sig)
|
||||
{
|
||||
/* Kill me again. */
|
||||
memcpy(sig->r, &pb->r1, 8);
|
||||
memcpy(sig->r + 8, &pb->r2, 8);
|
||||
memcpy(sig->r + 16, &pb->r3, 8);
|
||||
memcpy(sig->r + 24, &pb->r4, 8);
|
||||
memcpy(sig->s, &pb->s1, 8);
|
||||
memcpy(sig->s + 8, &pb->s2, 8);
|
||||
memcpy(sig->s + 16, &pb->s3, 8);
|
||||
memcpy(sig->s + 24, &pb->s4, 8);
|
||||
|
||||
/* S must be even */
|
||||
return (sig->s[31] & 1) == 0;
|
||||
}
|
||||
|
||||
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key)
|
||||
{
|
||||
BitcoinPubkey *p = tal(ctx, BitcoinPubkey);
|
||||
|
||||
bitcoin_pubkey__init(p);
|
||||
p->key.len = pubkey_len(key);
|
||||
p->key.data = tal_dup_arr(p, u8, key->key, p->key.len, 0);
|
||||
|
||||
assert(pubkey_valid(p->key.data, p->key.len));
|
||||
return p;
|
||||
}
|
||||
|
||||
bool proto_to_pubkey(const BitcoinPubkey *pb, struct pubkey *key)
|
||||
{
|
||||
if (!pubkey_valid(pb->key.data, pb->key.len))
|
||||
return false;
|
||||
|
||||
memcpy(key->key, pb->key.data, pb->key.len);
|
||||
return true;
|
||||
}
|
||||
|
||||
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash)
|
||||
{
|
||||
Sha256Hash *h = tal(ctx, Sha256Hash);
|
||||
sha256_hash__init(h);
|
||||
|
||||
/* Kill me now... */
|
||||
memcpy(&h->a, hash->u.u8, 8);
|
||||
memcpy(&h->b, hash->u.u8 + 8, 8);
|
||||
memcpy(&h->c, hash->u.u8 + 16, 8);
|
||||
memcpy(&h->d, hash->u.u8 + 24, 8);
|
||||
return h;
|
||||
}
|
||||
|
||||
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash)
|
||||
{
|
||||
/* Kill me again. */
|
||||
memcpy(hash->u.u8, &pb->a, 8);
|
||||
memcpy(hash->u.u8 + 8, &pb->b, 8);
|
||||
memcpy(hash->u.u8 + 16, &pb->c, 8);
|
||||
memcpy(hash->u.u8 + 24, &pb->d, 8);
|
||||
}
|
21
protobuf_convert.h
Normal file
21
protobuf_convert.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef LIGHTNING_PROTOBUF_CONVERT_H
|
||||
#define LIGHTNING_PROTOBUF_CONVERT_H
|
||||
#include "lightning.pb-c.h"
|
||||
#include <ccan/tal/tal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Convert to-from protobuf to internal representation. */
|
||||
struct signature;
|
||||
Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig);
|
||||
bool proto_to_signature(const Signature *pb, struct signature *sig);
|
||||
|
||||
/* Convert to-from protobuf to internal representation. */
|
||||
struct pubkey;
|
||||
BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key);
|
||||
bool proto_to_pubkey(const BitcoinPubkey *pb, struct pubkey *key);
|
||||
|
||||
/* Useful helper for allocating & populating a protobuf Sha256Hash */
|
||||
struct sha256;
|
||||
Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash);
|
||||
void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash);
|
||||
#endif /* LIGHTNING_PROTOBUF_CONVERT_H */
|
@ -18,6 +18,7 @@
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include "commit_tx.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include "find_p2sh_out.h"
|
||||
#include "protobuf_convert.h"
|
||||
#include <openssl/ec.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user