diff --git a/pkt.c b/pkt.c index 8c90dabd1..0e992fdb3 100644 --- a/pkt.c +++ b/pkt.c @@ -47,37 +47,6 @@ void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash) memcpy(hash->u.u8 + 24, &pb->d, 8); } -Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig) -{ - Signature *pb = tal(ctx, Signature); - signature__init(pb); - - /* 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; -} - -void 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); -} - BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct bitcoin_compressed_pubkey *key) { diff --git a/pkt.h b/pkt.h index 295e32aca..1be0fa25b 100644 --- a/pkt.h +++ b/pkt.h @@ -70,9 +70,7 @@ struct pkt *open_commit_sig_pkt(const tal_t *ctx, const struct signature *sig); Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash); void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash); -Signature *signature_to_proto(const tal_t *ctx, const struct signature *sig); -void proto_to_signature(const Signature *pb, struct signature *sig); - BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct bitcoin_compressed_pubkey *key); + #endif /* LIGHTNING_PKT_H */ diff --git a/signature.c b/signature.c index 28c6ae4a8..3d771acde 100644 --- a/signature.c +++ b/signature.c @@ -71,3 +71,40 @@ struct signature *sign_tx_input(const tal_t *ctx, struct bitcoin_tx *tx, return sign_hash(ctx, privkey, &hash); } + +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/signature.h b/signature.h index 55989d474..bdf2bfa9a 100644 --- a/signature.h +++ b/signature.h @@ -3,6 +3,7 @@ #include #include #include +#include "lightning.pb-c.h" enum sighash_type { SIGHASH_ALL = 1, @@ -29,4 +30,8 @@ struct signature *sign_tx_input(const tal_t *ctx, const u8 *subscript, size_t subscript_len, EC_KEY *privkey); +/* 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_SIGNATURE_H */