From 442f321585752f2a9b531b922e25d85e6651c6df Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 12 Jun 2015 12:09:05 +0930 Subject: [PATCH] Move protobuf<->bitcoin converters out of bitcoin/ They're lightning-specific. Signed-off-by: Rusty Russell --- Makefile | 2 +- anchor.c | 1 + bitcoin/pubkey.c | 28 ++----------- bitcoin/pubkey.h | 8 ++-- bitcoin/script.c | 1 + bitcoin/signature.c | 37 ----------------- bitcoin/signature.h | 5 --- check-commit-sig.c | 1 + close-channel.c | 1 + close_tx.c | 1 + commit_tx.c | 1 + create-close-tx.c | 1 + create-commit-spend-tx.c | 1 + create-commit-tx.c | 1 + create-steal-tx.c | 1 + open-channel.c | 1 + open-commit-sig.c | 1 + pkt.c | 24 +---------- pkt.h | 3 -- protobuf_convert.c | 83 ++++++++++++++++++++++++++++++++++++++ protobuf_convert.h | 21 ++++++++++ update-channel-accept.c | 1 + update-channel-complete.c | 1 + update-channel-signature.c | 1 + 24 files changed, 128 insertions(+), 98 deletions(-) create mode 100644 protobuf_convert.c create mode 100644 protobuf_convert.h diff --git a/Makefile b/Makefile index 236eed4a6..6f4b4c2d7 100644 --- a/Makefile +++ b/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 diff --git a/anchor.c b/anchor.c index 65703a2c4..63ed2216d 100644 --- a/anchor.c +++ b/anchor.c @@ -6,6 +6,7 @@ #include "bitcoin/script.h" #include "bitcoin/pubkey.h" #include +#include "protobuf_convert.h" struct bitcoin_tx *anchor_tx_create(const tal_t *ctx, const OpenChannel *o1, diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c index da77a30ae..bb6729d00 100644 --- a/bitcoin/pubkey.c +++ b/bitcoin/pubkey.c @@ -1,6 +1,7 @@ #include "pubkey.h" #include #include +#include /* 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; diff --git a/bitcoin/pubkey.h b/bitcoin/pubkey.h index 93288f973..50cb9aa18 100644 --- a/bitcoin/pubkey.h +++ b/bitcoin/pubkey.h @@ -2,20 +2,18 @@ #define LIGHTNING_BITCOIN_PUBKEY_H #include #include -#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 */ diff --git a/bitcoin/script.c b/bitcoin/script.c index 1e6a49028..18f5402ed 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -5,6 +5,7 @@ #include #include #include +#include /* Some standard ops */ #define OP_PUSHBYTES(val) (val) diff --git a/bitcoin/signature.c b/bitcoin/signature.c index d22e59e11..620e91a3b 100644 --- a/bitcoin/signature.c +++ b/bitcoin/signature.c @@ -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; -} - diff --git a/bitcoin/signature.h b/bitcoin/signature.h index 6e98e18f1..cc8310358 100644 --- a/bitcoin/signature.h +++ b/bitcoin/signature.h @@ -3,7 +3,6 @@ #include #include #include -#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 */ diff --git a/check-commit-sig.c b/check-commit-sig.c index 0e16c998d..198eaad77 100644 --- a/check-commit-sig.c +++ b/check-commit-sig.c @@ -18,6 +18,7 @@ #include "bitcoin/signature.h" #include "commit_tx.h" #include "bitcoin/pubkey.h" +#include "protobuf_convert.h" #include #include diff --git a/close-channel.c b/close-channel.c index c0261d00a..93cead442 100644 --- a/close-channel.c +++ b/close-channel.c @@ -19,6 +19,7 @@ #include "bitcoin/pubkey.h" #include "close_tx.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/close_tx.c b/close_tx.c index a7ed30cee..5a3441adf 100644 --- a/close_tx.c +++ b/close_tx.c @@ -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, diff --git a/commit_tx.c b/commit_tx.c index b439a9e05..780f153a0 100644 --- a/commit_tx.c +++ b/commit_tx.c @@ -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, diff --git a/create-close-tx.c b/create-close-tx.c index ea0cb0167..ab180ffe4 100644 --- a/create-close-tx.c +++ b/create-close-tx.c @@ -18,6 +18,7 @@ #include "bitcoin/pubkey.h" #include "close_tx.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/create-commit-spend-tx.c b/create-commit-spend-tx.c index d0eb3247c..197e74d0c 100644 --- a/create-commit-spend-tx.c +++ b/create-commit-spend-tx.c @@ -21,6 +21,7 @@ #include "bitcoin/address.h" #include "opt_bits.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/create-commit-tx.c b/create-commit-tx.c index 4bcb88e56..37c9f05a4 100644 --- a/create-commit-tx.c +++ b/create-commit-tx.c @@ -19,6 +19,7 @@ #include "commit_tx.h" #include "bitcoin/pubkey.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/create-steal-tx.c b/create-steal-tx.c index db26f8d35..8f3d9cd56 100644 --- a/create-steal-tx.c +++ b/create-steal-tx.c @@ -18,6 +18,7 @@ #include "bitcoin/signature.h" #include "commit_tx.h" #include "bitcoin/pubkey.h" +#include "protobuf_convert.h" #include #include diff --git a/open-channel.c b/open-channel.c index 5fa07bc1b..c99f2f5e4 100644 --- a/open-channel.c +++ b/open-channel.c @@ -17,6 +17,7 @@ #include "bitcoin/tx.h" #include "bitcoin/pubkey.h" #include "bitcoin/shadouble.h" +#include "protobuf_convert.h" #include #include #include "opt_bits.h" diff --git a/open-commit-sig.c b/open-commit-sig.c index 31067a64a..16ab9785e 100644 --- a/open-commit-sig.c +++ b/open-commit-sig.c @@ -18,6 +18,7 @@ #include "bitcoin/signature.h" #include "commit_tx.h" #include "bitcoin/pubkey.h" +#include "protobuf_convert.h" #include #include diff --git a/pkt.c b/pkt.c index b5007a0e7..dc32a7e80 100644 --- a/pkt.c +++ b/pkt.c @@ -6,8 +6,8 @@ #include "bitcoin/address.h" #include "bitcoin/pubkey.h" #include "bitcoin/signature.h" +#include "protobuf_convert.h" -#include 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, diff --git a/pkt.h b/pkt.h index 6f5607d31..bea570f13 100644 --- a/pkt.h +++ b/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 */ diff --git a/protobuf_convert.c b/protobuf_convert.c new file mode 100644 index 000000000..e173ab88b --- /dev/null +++ b/protobuf_convert.c @@ -0,0 +1,83 @@ +#include "protobuf_convert.h" +#include "bitcoin/signature.h" +#include "bitcoin/pubkey.h" +#include + +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); +} diff --git a/protobuf_convert.h b/protobuf_convert.h new file mode 100644 index 000000000..58744f991 --- /dev/null +++ b/protobuf_convert.h @@ -0,0 +1,21 @@ +#ifndef LIGHTNING_PROTOBUF_CONVERT_H +#define LIGHTNING_PROTOBUF_CONVERT_H +#include "lightning.pb-c.h" +#include +#include + +/* 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 */ diff --git a/update-channel-accept.c b/update-channel-accept.c index 6c70cffb8..6a16880c6 100644 --- a/update-channel-accept.c +++ b/update-channel-accept.c @@ -18,6 +18,7 @@ #include "commit_tx.h" #include "bitcoin/pubkey.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/update-channel-complete.c b/update-channel-complete.c index c57b63046..93f968a75 100644 --- a/update-channel-complete.c +++ b/update-channel-complete.c @@ -19,6 +19,7 @@ #include "commit_tx.h" #include "bitcoin/pubkey.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include diff --git a/update-channel-signature.c b/update-channel-signature.c index b71a31e9b..c06d28e9f 100644 --- a/update-channel-signature.c +++ b/update-channel-signature.c @@ -15,6 +15,7 @@ #include "commit_tx.h" #include "bitcoin/pubkey.h" #include "find_p2sh_out.h" +#include "protobuf_convert.h" #include #include