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

@@ -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,
struct bitcoin_signature *sig)
secp256k1_ecdsa_signature *sig)
{
u8 compact[64];
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)
fatal("db:bad signature blob");
sig->stype = SIGHASH_ALL;
}
static char *sig_to_sql(const tal_t *ctx,
const struct bitcoin_signature *sig)
const secp256k1_ecdsa_signature *sig)
{
u8 compact[64];
if (!sig)
return sql_hex_or_null(ctx, NULL, 0);
assert(sig->stype == SIGHASH_ALL);
secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, compact,
&sig->sig);
sig);
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)
ci->sig = NULL;
else {
ci->sig = tal(ci, struct bitcoin_signature);
ci->sig = tal(ci, secp256k1_ecdsa_signature);
sig_from_sql(stmt, 5, ci->sig);
}
@@ -878,7 +876,7 @@ static void load_peer_closing(struct peer *peer)
peer->closing.their_sig = NULL;
else {
peer->closing.their_sig = tal(peer,
struct bitcoin_signature);
secp256k1_ecdsa_signature);
sig_from_sql(stmt, 3, peer->closing.their_sig);
}
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);
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);
}
@@ -183,14 +183,14 @@ void queue_pkt_feechange(struct peer *peer, u64 feerate)
}
/* 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);
/* Now send message */
update_commit__init(u);
if (sig)
u->sig = signature_to_proto(u, &sig->sig);
u->sig = signature_to_proto(u, sig);
else
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,
struct bitcoin_signature *sig)
secp256k1_ecdsa_signature *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");
sig->stype = SIGHASH_ALL;
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,
struct bitcoin_signature *sig)
secp256k1_ecdsa_signature *sig)
{
const UpdateCommit *c = pkt->update_commit;
@@ -520,8 +518,7 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt,
if (!sig && !c->sig)
return NULL;
sig->stype = SIGHASH_ALL;
if (!proto_to_signature(c->sig, &sig->sig))
if (!proto_to_signature(c->sig, sig))
return pkt_err(peer, "Malformed signature");
return NULL;
}

View File

@@ -6,7 +6,6 @@
struct peer;
struct htlc;
struct sha256;
struct bitcoin_signature;
struct commit_info;
/* 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_fail(struct peer *peer, struct htlc *htlc);
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,
const struct sha256 *preimage,
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_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);
@@ -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_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);

View File

@@ -80,12 +80,11 @@ static const struct bitcoin_tx *mk_bitcoin_close(const tal_t *ctx,
struct peer *peer)
{
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);
our_close_sig.stype = SIGHASH_ALL;
peer_sign_mutual_close(peer, close_tx, &our_close_sig.sig);
peer_sign_mutual_close(peer, close_tx, &our_close_sig);
close_tx->input[0].witness
= bitcoin_witness_2of2(close_tx->input,
@@ -102,7 +101,7 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
{
u8 *witnessscript;
const struct bitcoin_tx *commit = peer->local.commit->tx;
struct bitcoin_signature sig;
secp256k1_ecdsa_signature sig;
struct bitcoin_tx *tx;
unsigned int p2wsh_out;
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;
sig.stype = SIGHASH_ALL;
peer_sign_spend(peer, tx, witnessscript, &sig.sig);
peer_sign_spend(peer, tx, witnessscript, &sig);
tx->input[0].witness = bitcoin_witness_secret(tx,
NULL, 0, &sig,
@@ -155,14 +153,13 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
/* Sign and local commit tx */
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! */
assert(peer->local.commit->tx->input[0].script_length == 0);
assert(!peer->local.commit->tx->input[0].witness);
sig.stype = SIGHASH_ALL;
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig.sig);
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig);
peer->local.commit->tx->input[0].witness
= 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__);
peer->local.commit->sig = tal(peer->local.commit,
struct bitcoin_signature);
secp256k1_ecdsa_signature);
err = accept_pkt_open_commit_sig(peer, pkt,
peer->local.commit->sig);
if (!err &&
@@ -684,10 +681,9 @@ static bool open_theiranchor_pkt_in(struct peer *peer, const Pkt *pkt)
struct pubkey, &peer->local.commitkey);
peer->remote.commit->sig = tal(peer->remote.commit,
struct bitcoin_signature);
peer->remote.commit->sig->stype = SIGHASH_ALL;
secp256k1_ecdsa_signature);
peer_sign_theircommit(peer, peer->remote.commit->tx,
&peer->remote.commit->sig->sig);
peer->remote.commit->sig);
peer->remote.commit->order = peer->order_counter++;
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;
struct bitcoin_tx *close_tx;
struct bitcoin_signature theirsig;
secp256k1_ecdsa_signature theirsig;
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
* transaction with the given `close_fee`, and MUST fail the
* connection if it is not. */
theirsig.stype = SIGHASH_ALL;
if (!proto_to_signature(c->sig, &theirsig.sig))
if (!proto_to_signature(c->sig, &theirsig))
return peer_comms_err(peer,
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);
peer->closing.their_sig = tal_dup(peer,
struct bitcoin_signature, &theirsig);
secp256k1_ecdsa_signature, &theirsig);
peer->closing.their_fee = c->close_fee;
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`.
*/
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);
if (err)
@@ -1779,9 +1774,8 @@ static bool do_commit(struct peer *peer, struct command *jsoncmd)
log_add_struct(peer->log, " (txid %s)",
struct sha256_double, &ci->txid);
ci->sig = tal(ci, struct bitcoin_signature);
ci->sig->stype = SIGHASH_ALL;
peer_sign_theircommit(peer, ci->tx, &ci->sig->sig);
ci->sig = tal(ci, secp256k1_ecdsa_signature);
peer_sign_theircommit(peer, ci->tx, ci->sig);
}
/* 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);
const struct htlc *htlc = peer->onchain.htlcs[out_num];
const u8 *wscript = peer->onchain.wscripts[out_num];
struct bitcoin_signature sig;
secp256k1_ecdsa_signature sig;
u64 fee, satoshis;
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;
sig.stype = SIGHASH_ALL;
peer_sign_htlc_fulfill(peer, tx, wscript, &sig.sig);
peer_sign_htlc_fulfill(peer, tx, wscript, &sig);
tx->input[0].witness = bitcoin_witness_htlc(tx,
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 u8 *wscript = peer->onchain.wscripts[out_num];
struct bitcoin_tx *tx = bitcoin_tx(peer, 1, 1);
struct bitcoin_signature sig;
secp256k1_ecdsa_signature sig;
u64 fee, satoshis;
/* 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;
sig.stype = SIGHASH_ALL;
peer_sign_htlc_refund(peer, tx, wscript, &sig.sig);
peer_sign_htlc_refund(peer, tx, wscript, &sig);
tx->input[0].witness = bitcoin_witness_htlc(tx,
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). */
n = 0;
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. */
if (i == peer->onchain.to_us_idx)
continue;
sig.stype = SIGHASH_ALL;
peer_sign_steal_input(peer, steal_tx, n,
peer->onchain.wscripts[i],
&sig.sig);
&sig);
steal_tx->input[n].witness
= bitcoin_witness_secret(steal_tx,

View File

@@ -52,7 +52,7 @@ struct commit_info {
/* Channel state for this tx. */
struct channel_state *cstate;
/* 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) */
s64 order;
};
@@ -151,7 +151,7 @@ struct peer {
struct {
/* Their signature for our current commit sig. */
struct bitcoin_signature theirsig;
secp256k1_ecdsa_signature theirsig;
/* The watch we have on a live commit tx. */
struct txwatch *watch;
} cur_commit;
@@ -164,7 +164,7 @@ struct peer {
/* Our last suggested closing fee. */
u64 our_fee;
/* 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. */
u64 their_fee;
/* 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)
{
u8 *redeemscript;
struct bitcoin_signature sig;
secp256k1_ecdsa_signature sig;
struct wallet *w = find_by_pubkey(dstate, walletkey);
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);
sig.stype = SIGHASH_ALL;
sign_tx_input(tx, input_num,
redeemscript, tal_count(redeemscript),
p2wpkh_scriptcode(redeemscript, &w->pubkey),
&w->privkey,
&w->pubkey,
&sig.sig);
&sig);
bitcoin_witness_p2sh_p2wpkh(tx->input,
&tx->input[input_num],