bitcoin/script.h: remove struct bitcoin_signature

Technically this incudes the sighash flags, but we only handle SIGHASH_ALL
anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-01-25 10:04:23 +10:30
parent 1edce4878d
commit 8159c4458a
12 changed files with 68 additions and 99 deletions

View File

@@ -115,13 +115,13 @@ static u8 *stack_key(const tal_t *ctx, const struct pubkey *key)
} }
/* Bitcoin wants DER encoding. */ /* Bitcoin wants DER encoding. */
static u8 *stack_sig(const tal_t *ctx, const struct bitcoin_signature *sig) static u8 *stack_sig(const tal_t *ctx, const secp256k1_ecdsa_signature *sig)
{ {
u8 der[73]; u8 der[73];
size_t len = signature_to_der(der, &sig->sig); size_t len = signature_to_der(der, sig);
/* Append sighash type */ /* Append sighash type */
der[len++] = sig->stype; der[len++] = SIGHASH_ALL;
return tal_dup_arr(ctx, u8, der, len, 0); return tal_dup_arr(ctx, u8, der, len, 0);
} }
@@ -213,7 +213,7 @@ u8 *bitcoin_redeem_p2wpkh(const tal_t *ctx, const struct pubkey *key)
/* Create an input which spends the p2sh-p2wpkh. */ /* Create an input which spends the p2sh-p2wpkh. */
void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx, void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx,
struct bitcoin_tx_input *input, struct bitcoin_tx_input *input,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const struct pubkey *key) const struct pubkey *key)
{ {
u8 *redeemscript = bitcoin_redeem_p2wpkh(ctx, key); u8 *redeemscript = bitcoin_redeem_p2wpkh(ctx, key);
@@ -261,8 +261,8 @@ u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key)
/* Create a witness which spends the 2of2. */ /* Create a witness which spends the 2of2. */
u8 **bitcoin_witness_2of2(const tal_t *ctx, u8 **bitcoin_witness_2of2(const tal_t *ctx,
const struct bitcoin_signature *sig1, const secp256k1_ecdsa_signature *sig1,
const struct bitcoin_signature *sig2, const secp256k1_ecdsa_signature *sig2,
const struct pubkey *key1, const struct pubkey *key1,
const struct pubkey *key2) const struct pubkey *key2)
{ {
@@ -513,7 +513,7 @@ u8 *bitcoin_redeem_secret_or_delay(const tal_t *ctx,
u8 **bitcoin_witness_secret(const tal_t *ctx, u8 **bitcoin_witness_secret(const tal_t *ctx,
const void *secret, size_t secret_len, const void *secret, size_t secret_len,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const u8 *witnessscript) const u8 *witnessscript)
{ {
u8 **witness = tal_arr(ctx, u8 *, 3); u8 **witness = tal_arr(ctx, u8 *, 3);
@@ -528,7 +528,7 @@ u8 **bitcoin_witness_secret(const tal_t *ctx,
u8 **bitcoin_witness_htlc(const tal_t *ctx, u8 **bitcoin_witness_htlc(const tal_t *ctx,
const void *htlc_or_revocation_preimage, const void *htlc_or_revocation_preimage,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const u8 *witnessscript) const u8 *witnessscript)
{ {
static const struct sha256 no_preimage; static const struct sha256 no_preimage;

View File

@@ -13,12 +13,6 @@ struct sha256;
struct rel_locktime; struct rel_locktime;
struct abs_locktime; struct abs_locktime;
/* A bitcoin signature includes one byte for the type. */
struct bitcoin_signature {
secp256k1_ecdsa_signature sig;
enum sighash_type stype;
};
/* tal_count() gives the length of the script. */ /* tal_count() gives the length of the script. */
u8 *bitcoin_redeem_2of2(const tal_t *ctx, u8 *bitcoin_redeem_2of2(const tal_t *ctx,
const struct pubkey *key1, const struct pubkey *key1,
@@ -46,7 +40,7 @@ u8 *bitcoin_redeem_p2wpkh(const tal_t *ctx,
/* Create a witness which spends the 2of2. */ /* Create a witness which spends the 2of2. */
void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx, void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx,
struct bitcoin_tx_input *input, struct bitcoin_tx_input *input,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const struct pubkey *key); const struct pubkey *key);
/* Create scriptcode (fake witness, basically) for P2WPKH */ /* Create scriptcode (fake witness, basically) for P2WPKH */
@@ -78,21 +72,21 @@ u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key);
/* Create a witness which spends the 2of2. */ /* Create a witness which spends the 2of2. */
u8 **bitcoin_witness_2of2(const tal_t *ctx, u8 **bitcoin_witness_2of2(const tal_t *ctx,
const struct bitcoin_signature *sig1, const secp256k1_ecdsa_signature *sig1,
const struct bitcoin_signature *sig2, const secp256k1_ecdsa_signature *sig2,
const struct pubkey *key1, const struct pubkey *key1,
const struct pubkey *key2); const struct pubkey *key2);
/* Create a witness which spends a "secret_or_delay" scriptpubkey */ /* Create a witness which spends a "secret_or_delay" scriptpubkey */
u8 **bitcoin_witness_secret(const tal_t *ctx, u8 **bitcoin_witness_secret(const tal_t *ctx,
const void *secret, size_t secret_len, const void *secret, size_t secret_len,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const u8 *witnessscript); const u8 *witnessscript);
/* Create a witness which spends bitcoin_redeeem_htlc_recv/send */ /* Create a witness which spends bitcoin_redeeem_htlc_recv/send */
u8 **bitcoin_witness_htlc(const tal_t *ctx, u8 **bitcoin_witness_htlc(const tal_t *ctx,
const void *htlc_or_revocation_preimage, const void *htlc_or_revocation_preimage,
const struct bitcoin_signature *sig, const secp256k1_ecdsa_signature *sig,
const u8 *witnessscript); const u8 *witnessscript);
/* Is this a pay to pubkeu hash? */ /* Is this a pay to pubkeu hash? */

View File

@@ -104,7 +104,7 @@ static void sha256_tx_one_input(struct bitcoin_tx *tx,
tx->input[input_num].script_length = script_len; tx->input[input_num].script_length = script_len;
tx->input[input_num].script = cast_const(u8 *, script); tx->input[input_num].script = cast_const(u8 *, script);
sha256_tx_for_sig(hash, tx, input_num, SIGHASH_ALL, witness_script); sha256_tx_for_sig(hash, tx, input_num, witness_script);
/* Reset it for next time. */ /* Reset it for next time. */
tx->input[input_num].script_length = 0; tx->input[input_num].script_length = 0;
@@ -143,7 +143,7 @@ bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
const u8 *redeemscript, size_t redeemscript_len, const u8 *redeemscript, size_t redeemscript_len,
const u8 *witness_script, const u8 *witness_script,
const struct pubkey *key, const struct pubkey *key,
const struct bitcoin_signature *sig) const secp256k1_ecdsa_signature *sig)
{ {
struct sha256_double hash; struct sha256_double hash;
bool ret; bool ret;
@@ -153,11 +153,7 @@ bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
sha256_tx_one_input(tx, input_num, redeemscript, redeemscript_len, sha256_tx_one_input(tx, input_num, redeemscript, redeemscript_len,
witness_script, &hash); witness_script, &hash);
/* We only use SIGHASH_ALL for the moment. */ ret = check_signed_hash(&hash, sig, key);
if (sig->stype != SIGHASH_ALL)
return false;
ret = check_signed_hash(&hash, &sig->sig, key);
if (!ret) if (!ret)
dump_tx("Sig failed", tx, input_num, dump_tx("Sig failed", tx, input_num,
redeemscript, redeemscript_len, key, &hash); redeemscript, redeemscript_len, key, &hash);

View File

@@ -5,6 +5,12 @@
#include <secp256k1.h> #include <secp256k1.h>
#include <stdbool.h> #include <stdbool.h>
struct sha256_double;
struct bitcoin_tx;
struct pubkey;
struct privkey;
struct bitcoin_tx_output;
enum sighash_type { enum sighash_type {
SIGHASH_ALL = 1, SIGHASH_ALL = 1,
SIGHASH_NONE = 2, SIGHASH_NONE = 2,
@@ -12,13 +18,6 @@ enum sighash_type {
SIGHASH_ANYONECANPAY = 0x80 SIGHASH_ANYONECANPAY = 0x80
}; };
struct sha256_double;
struct bitcoin_tx;
struct pubkey;
struct privkey;
struct bitcoin_tx_output;
struct bitcoin_signature;
void sign_hash(const struct privkey *p, void sign_hash(const struct privkey *p,
const struct sha256_double *h, const struct sha256_double *h,
secp256k1_ecdsa_signature *s); secp256k1_ecdsa_signature *s);
@@ -40,7 +39,7 @@ bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
const u8 *redeemscript, size_t redeemscript_len, const u8 *redeemscript, size_t redeemscript_len,
const u8 *witness, const u8 *witness,
const struct pubkey *key, const struct pubkey *key,
const struct bitcoin_signature *sig); const secp256k1_ecdsa_signature *sig);
/* Signature must have low S value. */ /* Signature must have low S value. */
bool sig_valid(const secp256k1_ecdsa_signature *sig); bool sig_valid(const secp256k1_ecdsa_signature *sig);

View File

@@ -222,15 +222,12 @@ static void hash_for_segwit(struct sha256_ctx *ctx,
} }
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx, void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
unsigned int input_num, enum sighash_type stype, unsigned int input_num,
const u8 *witness_script) const u8 *witness_script)
{ {
size_t i; size_t i;
struct sha256_ctx ctx = SHA256_INIT; struct sha256_ctx ctx = SHA256_INIT;
/* We only support this. */
assert(stype == SIGHASH_ALL);
/* Caller should zero-out other scripts for signing! */ /* Caller should zero-out other scripts for signing! */
assert(input_num < tx->input_count); assert(input_num < tx->input_count);
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tx->input_count; i++)
@@ -245,7 +242,7 @@ void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
push_tx(tx, push_sha, &ctx, false); push_tx(tx, push_sha, &ctx, false);
} }
sha256_le32(&ctx, stype); sha256_le32(&ctx, SIGHASH_ALL);
sha256_double_done(&ctx, h); sha256_double_done(&ctx, h);
} }

View File

@@ -43,8 +43,7 @@ void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid);
/* Useful for signature code. */ /* Useful for signature code. */
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx, void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
unsigned int input_num, enum sighash_type stype, unsigned int input_num, const u8 *witness_script);
const u8 *witness_script);
/* Linear bytes of tx. */ /* Linear bytes of tx. */
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx); u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);

View File

@@ -160,28 +160,26 @@ static void sha256_from_sql(sqlite3_stmt *stmt, int idx, struct sha256 *sha)
} }
static void sig_from_sql(sqlite3_stmt *stmt, int idx, static void sig_from_sql(sqlite3_stmt *stmt, int idx,
struct bitcoin_signature *sig) secp256k1_ecdsa_signature *sig)
{ {
u8 compact[64]; u8 compact[64];
from_sql_blob(stmt, idx, compact, sizeof(compact)); from_sql_blob(stmt, idx, compact, sizeof(compact));
if (secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx, &sig->sig, if (secp256k1_ecdsa_signature_parse_compact(secp256k1_ctx, sig,
compact) != 1) compact) != 1)
fatal("db:bad signature blob"); fatal("db:bad signature blob");
sig->stype = SIGHASH_ALL;
} }
static char *sig_to_sql(const tal_t *ctx, static char *sig_to_sql(const tal_t *ctx,
const struct bitcoin_signature *sig) const secp256k1_ecdsa_signature *sig)
{ {
u8 compact[64]; u8 compact[64];
if (!sig) if (!sig)
return sql_hex_or_null(ctx, NULL, 0); return sql_hex_or_null(ctx, NULL, 0);
assert(sig->stype == SIGHASH_ALL);
secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, compact, secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, compact,
&sig->sig); sig);
return sql_hex_or_null(ctx, compact, sizeof(compact)); return sql_hex_or_null(ctx, compact, sizeof(compact));
} }
@@ -470,7 +468,7 @@ static void load_peer_commit_info(struct peer *peer)
if (sqlite3_column_type(stmt, 5) == SQLITE_NULL) if (sqlite3_column_type(stmt, 5) == SQLITE_NULL)
ci->sig = NULL; ci->sig = NULL;
else { else {
ci->sig = tal(ci, struct bitcoin_signature); ci->sig = tal(ci, secp256k1_ecdsa_signature);
sig_from_sql(stmt, 5, ci->sig); sig_from_sql(stmt, 5, ci->sig);
} }
@@ -878,7 +876,7 @@ static void load_peer_closing(struct peer *peer)
peer->closing.their_sig = NULL; peer->closing.their_sig = NULL;
else { else {
peer->closing.their_sig = tal(peer, peer->closing.their_sig = tal(peer,
struct bitcoin_signature); secp256k1_ecdsa_signature);
sig_from_sql(stmt, 3, peer->closing.their_sig); sig_from_sql(stmt, 3, peer->closing.their_sig);
} }
peer->closing.our_script = tal_sql_blob(peer, stmt, 4); peer->closing.our_script = tal_sql_blob(peer, stmt, 4);

View File

@@ -111,7 +111,7 @@ void queue_pkt_open_commit_sig(struct peer *peer)
open_commit_sig__init(s); open_commit_sig__init(s);
s->sig = signature_to_proto(s, &peer->remote.commit->sig->sig); s->sig = signature_to_proto(s, peer->remote.commit->sig);
queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s); queue_pkt(peer, PKT__PKT_OPEN_COMMIT_SIG, s);
} }
@@ -183,14 +183,14 @@ void queue_pkt_feechange(struct peer *peer, u64 feerate)
} }
/* OK, we're sending a signature for their pending changes. */ /* OK, we're sending a signature for their pending changes. */
void queue_pkt_commit(struct peer *peer, const struct bitcoin_signature *sig) void queue_pkt_commit(struct peer *peer, const secp256k1_ecdsa_signature *sig)
{ {
UpdateCommit *u = tal(peer, UpdateCommit); UpdateCommit *u = tal(peer, UpdateCommit);
/* Now send message */ /* Now send message */
update_commit__init(u); update_commit__init(u);
if (sig) if (sig)
u->sig = signature_to_proto(u, &sig->sig); u->sig = signature_to_proto(u, sig);
else else
u->sig = NULL; u->sig = NULL;
@@ -368,14 +368,12 @@ Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt)
} }
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt, Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt,
struct bitcoin_signature *sig) secp256k1_ecdsa_signature *sig)
{ {
const OpenCommitSig *s = pkt->open_commit_sig; const OpenCommitSig *s = pkt->open_commit_sig;
if (!proto_to_signature(s->sig, &sig->sig)) if (!proto_to_signature(s->sig, sig))
return pkt_err(peer, "Malformed signature"); return pkt_err(peer, "Malformed signature");
sig->stype = SIGHASH_ALL;
return NULL; return NULL;
} }
@@ -507,7 +505,7 @@ Pkt *accept_pkt_update_fee(struct peer *peer, const Pkt *pkt, u64 *feerate)
} }
Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt, Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt,
struct bitcoin_signature *sig) secp256k1_ecdsa_signature *sig)
{ {
const UpdateCommit *c = pkt->update_commit; const UpdateCommit *c = pkt->update_commit;
@@ -520,8 +518,7 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt,
if (!sig && !c->sig) if (!sig && !c->sig)
return NULL; return NULL;
sig->stype = SIGHASH_ALL; if (!proto_to_signature(c->sig, sig))
if (!proto_to_signature(c->sig, &sig->sig))
return pkt_err(peer, "Malformed signature"); return pkt_err(peer, "Malformed signature");
return NULL; return NULL;
} }

View File

@@ -6,7 +6,6 @@
struct peer; struct peer;
struct htlc; struct htlc;
struct sha256; struct sha256;
struct bitcoin_signature;
struct commit_info; struct commit_info;
/* Send various kinds of packets */ /* Send various kinds of packets */
@@ -18,7 +17,7 @@ void queue_pkt_htlc_add(struct peer *peer, struct htlc *htlc);
void queue_pkt_htlc_fulfill(struct peer *peer, struct htlc *htlc); void queue_pkt_htlc_fulfill(struct peer *peer, struct htlc *htlc);
void queue_pkt_htlc_fail(struct peer *peer, struct htlc *htlc); void queue_pkt_htlc_fail(struct peer *peer, struct htlc *htlc);
void queue_pkt_feechange(struct peer *peer, u64 feerate); void queue_pkt_feechange(struct peer *peer, u64 feerate);
void queue_pkt_commit(struct peer *peer, const struct bitcoin_signature *sig); void queue_pkt_commit(struct peer *peer, const secp256k1_ecdsa_signature *sig);
void queue_pkt_revocation(struct peer *peer, void queue_pkt_revocation(struct peer *peer,
const struct sha256 *preimage, const struct sha256 *preimage,
const struct sha256 *next_hash); const struct sha256 *next_hash);
@@ -39,7 +38,7 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt,
Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt);
Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt, Pkt *accept_pkt_open_commit_sig(struct peer *peer, const Pkt *pkt,
struct bitcoin_signature *sig); secp256k1_ecdsa_signature *sig);
Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_open_complete(struct peer *peer, const Pkt *pkt);
@@ -56,7 +55,7 @@ Pkt *accept_pkt_update_fee(struct peer *peer, const Pkt *pkt, u64 *feerate);
Pkt *accept_pkt_update_accept(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_update_accept(struct peer *peer, const Pkt *pkt);
Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt, Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt,
struct bitcoin_signature *sig); secp256k1_ecdsa_signature *sig);
Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt); Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt);

View File

@@ -80,12 +80,11 @@ static const struct bitcoin_tx *mk_bitcoin_close(const tal_t *ctx,
struct peer *peer) struct peer *peer)
{ {
struct bitcoin_tx *close_tx; struct bitcoin_tx *close_tx;
struct bitcoin_signature our_close_sig; secp256k1_ecdsa_signature our_close_sig;
close_tx = peer_create_close_tx(ctx, peer, peer->closing.their_fee); close_tx = peer_create_close_tx(ctx, peer, peer->closing.their_fee);
our_close_sig.stype = SIGHASH_ALL; peer_sign_mutual_close(peer, close_tx, &our_close_sig);
peer_sign_mutual_close(peer, close_tx, &our_close_sig.sig);
close_tx->input[0].witness close_tx->input[0].witness
= bitcoin_witness_2of2(close_tx->input, = bitcoin_witness_2of2(close_tx->input,
@@ -102,7 +101,7 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
{ {
u8 *witnessscript; u8 *witnessscript;
const struct bitcoin_tx *commit = peer->local.commit->tx; const struct bitcoin_tx *commit = peer->local.commit->tx;
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
unsigned int p2wsh_out; unsigned int p2wsh_out;
uint64_t fee; uint64_t fee;
@@ -142,8 +141,7 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
tx->output[0].amount = commit->output[p2wsh_out].amount - fee; tx->output[0].amount = commit->output[p2wsh_out].amount - fee;
sig.stype = SIGHASH_ALL; peer_sign_spend(peer, tx, witnessscript, &sig);
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
tx->input[0].witness = bitcoin_witness_secret(tx, tx->input[0].witness = bitcoin_witness_secret(tx,
NULL, 0, &sig, NULL, 0, &sig,
@@ -155,14 +153,13 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
/* Sign and local commit tx */ /* Sign and local commit tx */
static void sign_commit_tx(struct peer *peer) static void sign_commit_tx(struct peer *peer)
{ {
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
/* Can't be signed already, and can't have scriptsig! */ /* Can't be signed already, and can't have scriptsig! */
assert(peer->local.commit->tx->input[0].script_length == 0); assert(peer->local.commit->tx->input[0].script_length == 0);
assert(!peer->local.commit->tx->input[0].witness); assert(!peer->local.commit->tx->input[0].witness);
sig.stype = SIGHASH_ALL; peer_sign_ourcommit(peer, peer->local.commit->tx, &sig);
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig.sig);
peer->local.commit->tx->input[0].witness peer->local.commit->tx->input[0].witness
= bitcoin_witness_2of2(peer->local.commit->tx->input, = bitcoin_witness_2of2(peer->local.commit->tx->input,
@@ -623,7 +620,7 @@ static bool open_ouranchor_pkt_in(struct peer *peer, const Pkt *pkt)
return peer_received_unexpected_pkt(peer, pkt, __func__); return peer_received_unexpected_pkt(peer, pkt, __func__);
peer->local.commit->sig = tal(peer->local.commit, peer->local.commit->sig = tal(peer->local.commit,
struct bitcoin_signature); secp256k1_ecdsa_signature);
err = accept_pkt_open_commit_sig(peer, pkt, err = accept_pkt_open_commit_sig(peer, pkt,
peer->local.commit->sig); peer->local.commit->sig);
if (!err && if (!err &&
@@ -684,10 +681,9 @@ static bool open_theiranchor_pkt_in(struct peer *peer, const Pkt *pkt)
struct pubkey, &peer->local.commitkey); struct pubkey, &peer->local.commitkey);
peer->remote.commit->sig = tal(peer->remote.commit, peer->remote.commit->sig = tal(peer->remote.commit,
struct bitcoin_signature); secp256k1_ecdsa_signature);
peer->remote.commit->sig->stype = SIGHASH_ALL;
peer_sign_theircommit(peer, peer->remote.commit->tx, peer_sign_theircommit(peer, peer->remote.commit->tx,
&peer->remote.commit->sig->sig); peer->remote.commit->sig);
peer->remote.commit->order = peer->order_counter++; peer->remote.commit->order = peer->order_counter++;
db_start_transaction(peer); db_start_transaction(peer);
@@ -1187,7 +1183,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
{ {
const CloseSignature *c = pkt->close_signature; const CloseSignature *c = pkt->close_signature;
struct bitcoin_tx *close_tx; struct bitcoin_tx *close_tx;
struct bitcoin_signature theirsig; secp256k1_ecdsa_signature theirsig;
assert(peer->state == STATE_MUTUAL_CLOSING); assert(peer->state == STATE_MUTUAL_CLOSING);
@@ -1234,8 +1230,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
* The receiver MUST check `sig` is valid for the close * The receiver MUST check `sig` is valid for the close
* transaction with the given `close_fee`, and MUST fail the * transaction with the given `close_fee`, and MUST fail the
* connection if it is not. */ * connection if it is not. */
theirsig.stype = SIGHASH_ALL; if (!proto_to_signature(c->sig, &theirsig))
if (!proto_to_signature(c->sig, &theirsig.sig))
return peer_comms_err(peer, return peer_comms_err(peer,
pkt_err(peer, "Invalid signature format")); pkt_err(peer, "Invalid signature format"));
@@ -1249,7 +1244,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
tal_free(peer->closing.their_sig); tal_free(peer->closing.their_sig);
peer->closing.their_sig = tal_dup(peer, peer->closing.their_sig = tal_dup(peer,
struct bitcoin_signature, &theirsig); secp256k1_ecdsa_signature, &theirsig);
peer->closing.their_fee = c->close_fee; peer->closing.their_fee = c->close_fee;
peer->closing.sigs_in++; peer->closing.sigs_in++;
@@ -1381,7 +1376,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
* changes to the remote commitment before generating `sig`. * changes to the remote commitment before generating `sig`.
*/ */
if (!to_them_only) if (!to_them_only)
ci->sig = tal(ci, struct bitcoin_signature); ci->sig = tal(ci, secp256k1_ecdsa_signature);
err = accept_pkt_commit(peer, pkt, ci->sig); err = accept_pkt_commit(peer, pkt, ci->sig);
if (err) if (err)
@@ -1779,9 +1774,8 @@ static bool do_commit(struct peer *peer, struct command *jsoncmd)
log_add_struct(peer->log, " (txid %s)", log_add_struct(peer->log, " (txid %s)",
struct sha256_double, &ci->txid); struct sha256_double, &ci->txid);
ci->sig = tal(ci, struct bitcoin_signature); ci->sig = tal(ci, secp256k1_ecdsa_signature);
ci->sig->stype = SIGHASH_ALL; peer_sign_theircommit(peer, ci->tx, ci->sig);
peer_sign_theircommit(peer, ci->tx, &ci->sig->sig);
} }
/* Switch to the new commitment. */ /* Switch to the new commitment. */
@@ -1953,7 +1947,7 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1); struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
const struct htlc *htlc = peer->onchain.htlcs[out_num]; const struct htlc *htlc = peer->onchain.htlcs[out_num];
const u8 *wscript = peer->onchain.wscripts[out_num]; const u8 *wscript = peer->onchain.wscripts[out_num];
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
u64 fee, satoshis; u64 fee, satoshis;
assert(htlc->r); assert(htlc->r);
@@ -1987,8 +1981,7 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
tx->output[0].amount = satoshis - fee; tx->output[0].amount = satoshis - fee;
sig.stype = SIGHASH_ALL; peer_sign_htlc_fulfill(peer, tx, wscript, &sig);
peer_sign_htlc_fulfill(peer, tx, wscript, &sig.sig);
tx->input[0].witness = bitcoin_witness_htlc(tx, tx->input[0].witness = bitcoin_witness_htlc(tx,
htlc->r, &sig, wscript); htlc->r, &sig, wscript);
@@ -3514,7 +3507,7 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
const struct htlc *htlc = peer->onchain.htlcs[out_num]; const struct htlc *htlc = peer->onchain.htlcs[out_num];
const u8 *wscript = peer->onchain.wscripts[out_num]; const u8 *wscript = peer->onchain.wscripts[out_num];
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1); struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
u64 fee, satoshis; u64 fee, satoshis;
/* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */ /* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */
@@ -3548,8 +3541,7 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
tx->output[0].amount = satoshis - fee; tx->output[0].amount = satoshis - fee;
sig.stype = SIGHASH_ALL; peer_sign_htlc_refund(peer, tx, wscript, &sig);
peer_sign_htlc_refund(peer, tx, wscript, &sig.sig);
tx->input[0].witness = bitcoin_witness_htlc(tx, tx->input[0].witness = bitcoin_witness_htlc(tx,
NULL, &sig, wscript); NULL, &sig, wscript);
@@ -4112,16 +4104,15 @@ static void resolve_their_steal(struct peer *peer,
/* Now, we can sign them all (they're all of same form). */ /* Now, we can sign them all (they're all of same form). */
n = 0; n = 0;
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tx->output_count; i++) {
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
/* Don't bother stealing the output already to us. */ /* Don't bother stealing the output already to us. */
if (i == peer->onchain.to_us_idx) if (i == peer->onchain.to_us_idx)
continue; continue;
sig.stype = SIGHASH_ALL;
peer_sign_steal_input(peer, steal_tx, n, peer_sign_steal_input(peer, steal_tx, n,
peer->onchain.wscripts[i], peer->onchain.wscripts[i],
&sig.sig); &sig);
steal_tx->input[n].witness steal_tx->input[n].witness
= bitcoin_witness_secret(steal_tx, = bitcoin_witness_secret(steal_tx,

View File

@@ -52,7 +52,7 @@ struct commit_info {
/* Channel state for this tx. */ /* Channel state for this tx. */
struct channel_state *cstate; struct channel_state *cstate;
/* Other side's signature for last commit tx (if known) */ /* Other side's signature for last commit tx (if known) */
struct bitcoin_signature *sig; secp256k1_ecdsa_signature *sig;
/* Order which commit was sent (theirs) / revocation was sent (ours) */ /* Order which commit was sent (theirs) / revocation was sent (ours) */
s64 order; s64 order;
}; };
@@ -151,7 +151,7 @@ struct peer {
struct { struct {
/* Their signature for our current commit sig. */ /* Their signature for our current commit sig. */
struct bitcoin_signature theirsig; secp256k1_ecdsa_signature theirsig;
/* The watch we have on a live commit tx. */ /* The watch we have on a live commit tx. */
struct txwatch *watch; struct txwatch *watch;
} cur_commit; } cur_commit;
@@ -164,7 +164,7 @@ struct peer {
/* Our last suggested closing fee. */ /* Our last suggested closing fee. */
u64 our_fee; u64 our_fee;
/* If they've offered a signature, these are set: */ /* If they've offered a signature, these are set: */
struct bitcoin_signature *their_sig; secp256k1_ecdsa_signature *their_sig;
/* If their_sig is non-NULL, this is the fee. */ /* If their_sig is non-NULL, this is the fee. */
u64 their_fee; u64 their_fee;
/* scriptPubKey we/they want for closing. */ /* scriptPubKey we/they want for closing. */

View File

@@ -67,7 +67,7 @@ bool wallet_add_signed_input(struct lightningd_state *dstate,
unsigned int input_num) unsigned int input_num)
{ {
u8 *redeemscript; u8 *redeemscript;
struct bitcoin_signature sig; secp256k1_ecdsa_signature sig;
struct wallet *w = find_by_pubkey(dstate, walletkey); struct wallet *w = find_by_pubkey(dstate, walletkey);
assert(input_num < tx->input_count); assert(input_num < tx->input_count);
@@ -76,13 +76,12 @@ bool wallet_add_signed_input(struct lightningd_state *dstate,
redeemscript = bitcoin_redeem_p2wpkh(tx, &w->pubkey); redeemscript = bitcoin_redeem_p2wpkh(tx, &w->pubkey);
sig.stype = SIGHASH_ALL;
sign_tx_input(tx, input_num, sign_tx_input(tx, input_num,
redeemscript, tal_count(redeemscript), redeemscript, tal_count(redeemscript),
p2wpkh_scriptcode(redeemscript, &w->pubkey), p2wpkh_scriptcode(redeemscript, &w->pubkey),
&w->privkey, &w->privkey,
&w->pubkey, &w->pubkey,
&sig.sig); &sig);
bitcoin_witness_p2sh_p2wpkh(tx->input, bitcoin_witness_p2sh_p2wpkh(tx->input,
&tx->input[input_num], &tx->input[input_num],