|
@ -125,7 +125,6 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer) |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
&peer->local.finalkey)); |
|
|
&peer->local.finalkey)); |
|
|
tx->output[0].script_length = tal_count(tx->output[0].script); |
|
|
|
|
|
|
|
|
|
|
|
/* Witness length can vary, due to DER encoding of sigs, but we
|
|
|
/* Witness length can vary, due to DER encoding of sigs, but we
|
|
|
* use 176 from an example run. */ |
|
|
* use 176 from an example run. */ |
|
@ -156,7 +155,7 @@ static void sign_commit_tx(struct peer *peer) |
|
|
secp256k1_ecdsa_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); |
|
|
assert(!peer->local.commit->tx->input[0].witness); |
|
|
assert(!peer->local.commit->tx->input[0].witness); |
|
|
|
|
|
|
|
|
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig); |
|
|
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig); |
|
@ -173,7 +172,7 @@ static u64 commit_tx_fee(const struct bitcoin_tx *commit, u64 anchor_satoshis) |
|
|
{ |
|
|
{ |
|
|
uint64_t i, total = 0; |
|
|
uint64_t i, total = 0; |
|
|
|
|
|
|
|
|
for (i = 0; i < commit->output_count; i++) |
|
|
for (i = 0; i < tal_count(commit->output); i++) |
|
|
total += commit->output[i].amount; |
|
|
total += commit->output[i].amount; |
|
|
|
|
|
|
|
|
assert(anchor_satoshis >= total); |
|
|
assert(anchor_satoshis >= total); |
|
@ -512,8 +511,6 @@ static bool bitcoin_create_anchor(struct peer *peer) |
|
|
assert(peer->local.offer_anchor); |
|
|
assert(peer->local.offer_anchor); |
|
|
|
|
|
|
|
|
tx->output[0].script = scriptpubkey_p2wsh(tx, peer->anchor.witnessscript); |
|
|
tx->output[0].script = scriptpubkey_p2wsh(tx, peer->anchor.witnessscript); |
|
|
tx->output[0].script_length = tal_count(tx->output[0].script); |
|
|
|
|
|
|
|
|
|
|
|
tx->output[0].amount = peer->anchor.input->out_amount; |
|
|
tx->output[0].amount = peer->anchor.input->out_amount; |
|
|
|
|
|
|
|
|
tx->input[0].txid = peer->anchor.input->txid; |
|
|
tx->input[0].txid = peer->anchor.input->txid; |
|
@ -533,7 +530,7 @@ static bool bitcoin_create_anchor(struct peer *peer) |
|
|
peer->anchor.satoshis = tx->output[0].amount; |
|
|
peer->anchor.satoshis = tx->output[0].amount; |
|
|
|
|
|
|
|
|
/* To avoid malleation, all inputs must be segwit! */ |
|
|
/* To avoid malleation, all inputs must be segwit! */ |
|
|
for (i = 0; i < tx->input_count; i++) |
|
|
for (i = 0; i < tal_count(tx->input); i++) |
|
|
assert(tx->input[i].witness); |
|
|
assert(tx->input[i].witness); |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
@ -625,7 +622,7 @@ static bool open_ouranchor_pkt_in(struct peer *peer, const Pkt *pkt) |
|
|
peer->local.commit->sig); |
|
|
peer->local.commit->sig); |
|
|
if (!err && |
|
|
if (!err && |
|
|
!check_tx_sig(peer->local.commit->tx, 0, |
|
|
!check_tx_sig(peer->local.commit->tx, 0, |
|
|
NULL, 0, |
|
|
NULL, |
|
|
peer->anchor.witnessscript, |
|
|
peer->anchor.witnessscript, |
|
|
&peer->remote.commitkey, |
|
|
&peer->remote.commitkey, |
|
|
peer->local.commit->sig)) |
|
|
peer->local.commit->sig)) |
|
@ -1236,7 +1233,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt) |
|
|
|
|
|
|
|
|
close_tx = peer_create_close_tx(c, peer, c->close_fee); |
|
|
close_tx = peer_create_close_tx(c, peer, c->close_fee); |
|
|
if (!check_tx_sig(close_tx, 0, |
|
|
if (!check_tx_sig(close_tx, 0, |
|
|
NULL, 0, |
|
|
NULL, |
|
|
peer->anchor.witnessscript, |
|
|
peer->anchor.witnessscript, |
|
|
&peer->remote.commitkey, &theirsig)) |
|
|
&peer->remote.commitkey, &theirsig)) |
|
|
return peer_comms_err(peer, |
|
|
return peer_comms_err(peer, |
|
@ -1389,7 +1386,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt) |
|
|
* check `sig` is valid for that transaction. |
|
|
* check `sig` is valid for that transaction. |
|
|
*/ |
|
|
*/ |
|
|
if (ci->sig && !check_tx_sig(ci->tx, 0, |
|
|
if (ci->sig && !check_tx_sig(ci->tx, 0, |
|
|
NULL, 0, |
|
|
NULL, |
|
|
peer->anchor.witnessscript, |
|
|
peer->anchor.witnessscript, |
|
|
&peer->remote.commitkey, |
|
|
&peer->remote.commitkey, |
|
|
ci->sig)) { |
|
|
ci->sig)) { |
|
@ -1963,7 +1960,6 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer, |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
&peer->local.finalkey)); |
|
|
&peer->local.finalkey)); |
|
|
tx->output[0].script_length = tal_count(tx->output[0].script); |
|
|
|
|
|
|
|
|
|
|
|
log_debug(peer->log, "Pre-witness txlen = %zu\n", |
|
|
log_debug(peer->log, "Pre-witness txlen = %zu\n", |
|
|
measure_tx_cost(tx) / 4); |
|
|
measure_tx_cost(tx) / 4); |
|
@ -3181,12 +3177,12 @@ static void json_connect(struct command *cmd, |
|
|
bitcoin_txid(tx, &connect->input->txid); |
|
|
bitcoin_txid(tx, &connect->input->txid); |
|
|
|
|
|
|
|
|
/* Find an output we know how to spend. */ |
|
|
/* Find an output we know how to spend. */ |
|
|
for (output = 0; output < tx->output_count; output++) { |
|
|
for (output = 0; output < tal_count(tx->output); output++) { |
|
|
if (wallet_can_spend(cmd->dstate, &tx->output[output], |
|
|
if (wallet_can_spend(cmd->dstate, &tx->output[output], |
|
|
&connect->input->walletkey)) |
|
|
&connect->input->walletkey)) |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
if (output == tx->output_count) { |
|
|
if (output == tal_count(tx->output)) { |
|
|
command_fail(cmd, "Tx doesn't send to wallet address"); |
|
|
command_fail(cmd, "Tx doesn't send to wallet address"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
@ -3403,9 +3399,9 @@ void peers_new_block(struct lightningd_state *dstate, unsigned int height) |
|
|
static bool outputscript_eq(const struct bitcoin_tx_output *out, |
|
|
static bool outputscript_eq(const struct bitcoin_tx_output *out, |
|
|
size_t i, const u8 *script) |
|
|
size_t i, const u8 *script) |
|
|
{ |
|
|
{ |
|
|
if (out[i].script_length != tal_count(script)) |
|
|
if (tal_count(out[i].script) != tal_count(script)) |
|
|
return false; |
|
|
return false; |
|
|
return memcmp(out[i].script, script, out[i].script_length) == 0; |
|
|
return memcmp(out[i].script, script, tal_count(script)) == 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* This tx is their commitment;
|
|
|
/* This tx is their commitment;
|
|
@ -3421,8 +3417,8 @@ static bool map_onchain_outputs(struct peer *peer, |
|
|
size_t i; |
|
|
size_t i; |
|
|
|
|
|
|
|
|
peer->onchain.to_us_idx = peer->onchain.to_them_idx = -1; |
|
|
peer->onchain.to_us_idx = peer->onchain.to_them_idx = -1; |
|
|
peer->onchain.htlcs = tal_arr(tx, struct htlc *, tx->output_count); |
|
|
peer->onchain.htlcs = tal_arr(tx, struct htlc *, tal_count(tx->output)); |
|
|
peer->onchain.wscripts = tal_arr(tx, const u8 *, tx->output_count); |
|
|
peer->onchain.wscripts = tal_arr(tx, const u8 *, tal_count(tx->output)); |
|
|
|
|
|
|
|
|
to_us = commit_output_to_us(tx, peer, rhash, side, &to_us_wscript); |
|
|
to_us = commit_output_to_us(tx, peer, rhash, side, &to_us_wscript); |
|
|
to_them = commit_output_to_them(tx, peer, rhash, side, |
|
|
to_them = commit_output_to_them(tx, peer, rhash, side, |
|
@ -3431,7 +3427,7 @@ static bool map_onchain_outputs(struct peer *peer, |
|
|
/* Now generate the wscript hashes for every possible HTLC. */ |
|
|
/* Now generate the wscript hashes for every possible HTLC. */ |
|
|
hmap = get_htlc_output_map(tx, peer, rhash, side, commit_num); |
|
|
hmap = get_htlc_output_map(tx, peer, rhash, side, commit_num); |
|
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) { |
|
|
for (i = 0; i < tal_count(tx->output); i++) { |
|
|
log_debug(peer->log, "%s: output %zi", __func__, i); |
|
|
log_debug(peer->log, "%s: output %zi", __func__, i); |
|
|
if (peer->onchain.to_us_idx == -1 |
|
|
if (peer->onchain.to_us_idx == -1 |
|
|
&& outputscript_eq(tx->output, i, to_us)) { |
|
|
&& outputscript_eq(tx->output, i, to_us)) { |
|
@ -3452,7 +3448,6 @@ static bool map_onchain_outputs(struct peer *peer, |
|
|
/* Must be an HTLC output */ |
|
|
/* Must be an HTLC output */ |
|
|
peer->onchain.htlcs[i] = txout_get_htlc(hmap, |
|
|
peer->onchain.htlcs[i] = txout_get_htlc(hmap, |
|
|
tx->output[i].script, |
|
|
tx->output[i].script, |
|
|
tx->output[i].script_length, |
|
|
|
|
|
peer->onchain.wscripts+i); |
|
|
peer->onchain.wscripts+i); |
|
|
if (!peer->onchain.htlcs[i]) { |
|
|
if (!peer->onchain.htlcs[i]) { |
|
|
log_add(peer->log, "no HTLC found"); |
|
|
log_add(peer->log, "no HTLC found"); |
|
@ -3481,20 +3476,16 @@ static bool is_mutual_close(const struct peer *peer, |
|
|
if (!ours || !theirs) |
|
|
if (!ours || !theirs) |
|
|
return false; |
|
|
return false; |
|
|
|
|
|
|
|
|
if (tx->output_count != 2) |
|
|
if (tal_count(tx->output) != 2) |
|
|
return false; |
|
|
return false; |
|
|
|
|
|
|
|
|
/* Without knowing fee amounts, can't determine order. Check both. */ |
|
|
/* Without knowing fee amounts, can't determine order. Check both. */ |
|
|
if (scripteq(tx->output[0].script, tx->output[0].script_length, |
|
|
if (scripteq(tx->output[0].script, ours) |
|
|
ours, tal_count(ours)) |
|
|
&& scripteq(tx->output[1].script, theirs)) |
|
|
&& scripteq(tx->output[1].script, tx->output[1].script_length, |
|
|
|
|
|
theirs, tal_count(theirs))) |
|
|
|
|
|
return true; |
|
|
return true; |
|
|
|
|
|
|
|
|
if (scripteq(tx->output[0].script, tx->output[0].script_length, |
|
|
if (scripteq(tx->output[0].script, theirs) |
|
|
theirs, tal_count(theirs)) |
|
|
&& scripteq(tx->output[1].script, ours)) |
|
|
&& scripteq(tx->output[1].script, tx->output[1].script_length, |
|
|
|
|
|
ours, tal_count(ours))) |
|
|
|
|
|
return true; |
|
|
return true; |
|
|
|
|
|
|
|
|
return false; |
|
|
return false; |
|
@ -3523,7 +3514,6 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer, |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
bitcoin_redeem_single(tx, |
|
|
&peer->local.finalkey)); |
|
|
&peer->local.finalkey)); |
|
|
tx->output[0].script_length = tal_count(tx->output[0].script); |
|
|
|
|
|
|
|
|
|
|
|
log_unusual(peer->log, "Pre-witness txlen = %zu\n", |
|
|
log_unusual(peer->log, "Pre-witness txlen = %zu\n", |
|
|
measure_tx_cost(tx) / 4); |
|
|
measure_tx_cost(tx) / 4); |
|
@ -3859,7 +3849,7 @@ static void resolve_our_unilateral(struct peer *peer) |
|
|
* resolved[] entries for these uncommitted HTLCs, too. */ |
|
|
* resolved[] entries for these uncommitted HTLCs, too. */ |
|
|
watch_tx(tx, peer, tx, our_unilateral_depth, NULL); |
|
|
watch_tx(tx, peer, tx, our_unilateral_depth, NULL); |
|
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) { |
|
|
for (i = 0; i < tal_count(tx->output); i++) { |
|
|
/* FIXME-OLD #onchain:
|
|
|
/* FIXME-OLD #onchain:
|
|
|
* |
|
|
* |
|
|
* 1. _A's main output_: A node SHOULD spend this output to a |
|
|
* 1. _A's main output_: A node SHOULD spend this output to a |
|
@ -3906,7 +3896,7 @@ static void resolve_their_unilateral(struct peer *peer) |
|
|
unsigned int i; |
|
|
unsigned int i; |
|
|
const struct bitcoin_tx *tx = peer->onchain.tx; |
|
|
const struct bitcoin_tx *tx = peer->onchain.tx; |
|
|
|
|
|
|
|
|
for (i = 0; i < tx->output_count; i++) { |
|
|
for (i = 0; i < tal_count(tx->output); i++) { |
|
|
/* FIXME-OLD #onchain:
|
|
|
/* FIXME-OLD #onchain:
|
|
|
* |
|
|
* |
|
|
* 1. _A's main output_: No action is required; this is a |
|
|
* 1. _A's main output_: No action is required; this is a |
|
@ -3948,13 +3938,13 @@ static void resolve_mutual_close(struct peer *peer) |
|
|
* the output, which is sent to its specified scriptpubkey (see FIXME-OLD |
|
|
* the output, which is sent to its specified scriptpubkey (see FIXME-OLD |
|
|
* #2 "4.1: Closing initiation: close_shutdown"). |
|
|
* #2 "4.1: Closing initiation: close_shutdown"). |
|
|
*/ |
|
|
*/ |
|
|
for (i = 0; i < peer->onchain.tx->output_count; i++) |
|
|
for (i = 0; i < tal_count(peer->onchain.tx->output); i++) |
|
|
peer->onchain.resolved[i] = irrevocably_resolved(peer); |
|
|
peer->onchain.resolved[i] = irrevocably_resolved(peer); |
|
|
|
|
|
|
|
|
/* No HTLCs. */ |
|
|
/* No HTLCs. */ |
|
|
peer->onchain.htlcs = tal_arrz(peer->onchain.tx, |
|
|
peer->onchain.htlcs = tal_arrz(peer->onchain.tx, |
|
|
struct htlc *, |
|
|
struct htlc *, |
|
|
peer->onchain.tx->output_count); |
|
|
tal_count(peer->onchain.tx->output)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* Called every time the tx spending the funding tx changes depth. */ |
|
|
/* Called every time the tx spending the funding tx changes depth. */ |
|
@ -4040,13 +4030,13 @@ static void resolve_their_steal(struct peer *peer, |
|
|
|
|
|
|
|
|
/* Create steal_tx: don't need to steal to_us output */ |
|
|
/* Create steal_tx: don't need to steal to_us output */ |
|
|
if (peer->onchain.to_us_idx == -1) |
|
|
if (peer->onchain.to_us_idx == -1) |
|
|
steal_tx = bitcoin_tx(tx, tx->output_count, 1); |
|
|
steal_tx = bitcoin_tx(tx, tal_count(tx->output), 1); |
|
|
else |
|
|
else |
|
|
steal_tx = bitcoin_tx(tx, tx->output_count - 1, 1); |
|
|
steal_tx = bitcoin_tx(tx, tal_count(tx->output) - 1, 1); |
|
|
n = 0; |
|
|
n = 0; |
|
|
|
|
|
|
|
|
log_debug(peer->log, "Analyzing tx to steal:"); |
|
|
log_debug(peer->log, "Analyzing tx to steal:"); |
|
|
for (i = 0; i < tx->output_count; i++) { |
|
|
for (i = 0; i < tal_count(tx->output); i++) { |
|
|
/* FIXME-OLD #onchain:
|
|
|
/* FIXME-OLD #onchain:
|
|
|
* 1. _A's main output_: No action is required; this is a |
|
|
* 1. _A's main output_: No action is required; this is a |
|
|
* simple P2WPKH output. This output is considered |
|
|
* simple P2WPKH output. This output is considered |
|
@ -4081,7 +4071,7 @@ static void resolve_their_steal(struct peer *peer, |
|
|
input_total += tx->output[i].amount; |
|
|
input_total += tx->output[i].amount; |
|
|
n++; |
|
|
n++; |
|
|
} |
|
|
} |
|
|
assert(n == steal_tx->input_count); |
|
|
assert(n == tal_count(steal_tx->input)); |
|
|
|
|
|
|
|
|
fee = get_feerate(peer->dstate) |
|
|
fee = get_feerate(peer->dstate) |
|
|
* (measure_tx_cost(steal_tx) + wsize) / 1000; |
|
|
* (measure_tx_cost(steal_tx) + wsize) / 1000; |
|
@ -4099,11 +4089,10 @@ static void resolve_their_steal(struct peer *peer, |
|
|
steal_tx->output[0].script = scriptpubkey_p2sh(steal_tx, |
|
|
steal_tx->output[0].script = scriptpubkey_p2sh(steal_tx, |
|
|
bitcoin_redeem_single(steal_tx, |
|
|
bitcoin_redeem_single(steal_tx, |
|
|
&peer->local.finalkey)); |
|
|
&peer->local.finalkey)); |
|
|
steal_tx->output[0].script_length = tal_count(steal_tx->output[0].script); |
|
|
|
|
|
|
|
|
|
|
|
/* 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 < tal_count(tx->output); i++) { |
|
|
secp256k1_ecdsa_signature sig; |
|
|
secp256k1_ecdsa_signature sig; |
|
|
|
|
|
|
|
|
/* Don't bother stealing the output already to us. */ |
|
|
/* Don't bother stealing the output already to us. */ |
|
@ -4122,7 +4111,7 @@ static void resolve_their_steal(struct peer *peer, |
|
|
peer->onchain.wscripts[i]); |
|
|
peer->onchain.wscripts[i]); |
|
|
n++; |
|
|
n++; |
|
|
} |
|
|
} |
|
|
assert(n == steal_tx->input_count); |
|
|
assert(n == tal_count(steal_tx->input)); |
|
|
|
|
|
|
|
|
broadcast_tx(peer, steal_tx, NULL); |
|
|
broadcast_tx(peer, steal_tx, NULL); |
|
|
} |
|
|
} |
|
@ -4165,7 +4154,7 @@ static enum watch_result anchor_spent(struct peer *peer, |
|
|
struct htlc *h; |
|
|
struct htlc *h; |
|
|
u64 commit_num; |
|
|
u64 commit_num; |
|
|
|
|
|
|
|
|
assert(input_num < tx->input_count); |
|
|
assert(input_num < tal_count(tx->input)); |
|
|
|
|
|
|
|
|
/* We only ever sign single-input txs. */ |
|
|
/* We only ever sign single-input txs. */ |
|
|
if (input_num != 0) { |
|
|
if (input_num != 0) { |
|
@ -4190,7 +4179,8 @@ static enum watch_result anchor_spent(struct peer *peer, |
|
|
|
|
|
|
|
|
/* We need to resolve every output. */ |
|
|
/* We need to resolve every output. */ |
|
|
peer->onchain.resolved |
|
|
peer->onchain.resolved |
|
|
= tal_arrz(tx, const struct bitcoin_tx *, tx->output_count); |
|
|
= tal_arrz(tx, const struct bitcoin_tx *, |
|
|
|
|
|
tal_count(tx->output)); |
|
|
|
|
|
|
|
|
/* A mutual close tx. */ |
|
|
/* A mutual close tx. */ |
|
|
if (is_mutual_close(peer, tx)) { |
|
|
if (is_mutual_close(peer, tx)) { |
|
|