Skip to content

Commit 5a47254

Browse files
committed
hsmd: Added output_witscripts, remote_per_commit and option_static_remotekey
to hsm_sign_remote_commitment_tx to allow complete validation.
1 parent 28080b2 commit 5a47254

File tree

13 files changed

+97
-8
lines changed

13 files changed

+97
-8
lines changed

bitcoin/tx.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
398398
tx->input_amounts = tal_arrz(tx, struct amount_sat*, input_count);
399399
tx->wtx->locktime = 0;
400400
tx->wtx->version = 2;
401+
tx->output_witscripts = tal_arrz(tx, struct witscript*, output_count);
401402
tx->chainparams = chainparams;
402403
return tx;
403404
}

bitcoin/tx.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#define BITCOIN_TX_DEFAULT_SEQUENCE 0xFFFFFFFF
1414

15+
struct witscript {
16+
u8 *ptr;
17+
};
18+
1519
struct bitcoin_txid {
1620
struct sha256_double shad;
1721
};
@@ -24,6 +28,9 @@ struct bitcoin_tx {
2428
struct amount_sat **input_amounts;
2529
struct wally_tx *wtx;
2630

31+
/* Need the output wscripts in the HSM to validate transaction */
32+
struct witscript **output_witscripts;
33+
2734
/* Keep a reference to the ruleset we have to abide by */
2835
const struct chainparams *chainparams;
2936
};

channeld/channeld.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,10 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
994994

995995
msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
996996
&peer->channel->funding_pubkey[REMOTE],
997-
*txs[0]->input_amounts[0]);
997+
*txs[0]->input_amounts[0],
998+
(const struct witscript **) txs[0]->output_witscripts,
999+
&peer->remote_per_commit,
1000+
peer->channel->option_static_remotekey);
9981001

9991002
msg = hsm_req(tmpctx, take(msg));
10001003
if (!fromwire_hsm_sign_tx_reply(msg, commit_sig))

channeld/commit_tx.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
3636

3737
static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
3838
const struct htlc *htlc,
39-
const struct keyset *keyset)
39+
const struct keyset *keyset,
40+
struct witscript * o_wscript)
4041
{
4142
struct ripemd160 ripemd;
4243
u8 *wscript, *p2wsh;
@@ -49,12 +50,15 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
4950
SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
5051
type_to_string(tmpctx, struct amount_sat, &amount),
5152
tal_hex(wscript, wscript));
53+
o_wscript->ptr = tal_dup_arr(o_wscript, u8,
54+
wscript, tal_count(wscript), 0);
5255
tal_free(wscript);
5356
}
5457

5558
static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
5659
const struct htlc *htlc,
57-
const struct keyset *keyset)
60+
const struct keyset *keyset,
61+
struct witscript * o_wscript)
5862
{
5963
struct ripemd160 ripemd;
6064
u8 *wscript, *p2wsh;
@@ -72,6 +76,8 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
7276
type_to_string(tmpctx, struct amount_sat,
7377
&amount),
7478
tal_hex(wscript, wscript));
79+
o_wscript->ptr = tal_dup_arr(o_wscript, u8,
80+
wscript, tal_count(wscript), 0);
7581
tal_free(wscript);
7682
}
7783

@@ -169,7 +175,10 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
169175
continue;
170176
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
171177
continue;
172-
add_offered_htlc_out(tx, n, htlcs[i], keyset);
178+
tx->output_witscripts[n] =
179+
tal(tx->output_witscripts, struct witscript);
180+
add_offered_htlc_out(tx, n, htlcs[i],
181+
keyset, tx->output_witscripts[n]);
173182
(*htlcmap)[n] = htlcs[i];
174183
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
175184
n++;
@@ -185,7 +194,10 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
185194
continue;
186195
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
187196
continue;
188-
add_received_htlc_out(tx, n, htlcs[i], keyset);
197+
tx->output_witscripts[n] =
198+
tal(tx->output_witscripts, struct witscript);
199+
add_received_htlc_out(tx, n, htlcs[i], keyset,
200+
tx->output_witscripts[n]);
189201
(*htlcmap)[n] = htlcs[i];
190202
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
191203
n++;
@@ -209,6 +221,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
209221
SUPERVERBOSE("# to-local amount %s wscript %s\n",
210222
type_to_string(tmpctx, struct amount_sat, &amount),
211223
tal_hex(tmpctx, wscript));
224+
tx->output_witscripts[n] =
225+
tal(tx->output_witscripts, struct witscript);
226+
tx->output_witscripts[n]->ptr =
227+
tal_dup_arr(tx->output_witscripts[n], u8,
228+
wscript, tal_count(wscript), 0);
212229
n++;
213230
}
214231

@@ -252,6 +269,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
252269

253270
assert(n <= tx->wtx->outputs_allocation_len);
254271
tal_resize(htlcmap, n);
272+
tal_resize(&(tx->output_witscripts), n);
255273

256274
/* BOLT #3:
257275
*

common/initial_commit_tx.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
175175
int pos = bitcoin_tx_add_output(
176176
tx, scriptpubkey_p2wsh(tx, wscript), amount);
177177
assert(pos == n);
178+
tx->output_witscripts[n] =
179+
tal(tx->output_witscripts, struct witscript);
180+
tx->output_witscripts[n]->ptr =
181+
tal_dup_arr(tx->output_witscripts[n], u8,
182+
wscript, tal_count(wscript), 0);
178183
n++;
179184
}
180185

@@ -202,6 +207,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
202207

203208
assert(n <= tx->wtx->num_outputs);
204209

210+
tal_resize(&(tx->output_witscripts), n);
211+
205212
/* BOLT #3:
206213
*
207214
* 7. Sort the outputs into [BIP 69+CLTV

common/permute_tx.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,12 @@ void permute_outputs(struct bitcoin_tx *tx, u32 *cltvs, const void **map)
174174

175175
/* Swap best into first place. */
176176
swap_wally_outputs(tx->wtx->outputs, map, cltvs, i, best_pos);
177+
178+
/* If output_witscripts are present, swap them to match. */
179+
if (tx->output_witscripts) {
180+
struct witscript *tmp = tx->output_witscripts[i];
181+
tx->output_witscripts[i] = tx->output_witscripts[best_pos];
182+
tx->output_witscripts[best_pos] = tmp;
183+
}
177184
}
178185
}

hsmd/hsm_wire.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ msgtype,hsm_sign_remote_commitment_tx,19
158158
msgdata,hsm_sign_remote_commitment_tx,tx,bitcoin_tx,
159159
msgdata,hsm_sign_remote_commitment_tx,remote_funding_key,pubkey,
160160
msgdata,hsm_sign_remote_commitment_tx,funding_amount,amount_sat,
161+
msgdata,hsm_sign_remote_commitment_tx,num_witscripts,u16,
162+
msgdata,hsm_sign_remote_commitment_tx,output_witscripts,witscript,num_witscripts
163+
msgdata,hsm_sign_remote_commitment_tx,remote_per_commit,pubkey,
164+
msgdata,hsm_sign_remote_commitment_tx,option_static_remotekey,bool,
161165

162166
# channeld asks HSM to sign remote HTLC tx.
163167
msgtype,hsm_sign_remote_htlc_tx,20

hsmd/hsmd.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,17 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
996996
struct bitcoin_signature sig;
997997
struct secrets secrets;
998998
const u8 *funding_wscript;
999+
struct witscript **output_witscripts;
1000+
struct pubkey remote_per_commit;
1001+
bool option_static_remotekey;
9991002

10001003
if (!fromwire_hsm_sign_remote_commitment_tx(tmpctx, msg_in,
10011004
&tx,
10021005
&remote_funding_pubkey,
1003-
&funding))
1006+
&funding,
1007+
&output_witscripts,
1008+
&remote_per_commit,
1009+
&option_static_remotekey))
10041010
bad_req(conn, c, msg_in);
10051011
tx->chainparams = c->chainparams;
10061012

@@ -1009,6 +1015,8 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
10091015
return bad_req_fmt(conn, c, msg_in, "tx must have 1 input");
10101016
if (tx->wtx->num_outputs == 0)
10111017
return bad_req_fmt(conn, c, msg_in, "tx must have > 0 outputs");
1018+
if (tal_count(output_witscripts) != tx->wtx->num_outputs)
1019+
return bad_req_fmt(conn, c, msg_in, "tx must have matching witscripts");
10121020

10131021
get_channel_seed(&c->id, c->dbid, &channel_seed);
10141022
derive_basepoints(&channel_seed,

openingd/openingd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,10 @@ static bool funder_finalize_channel_setup(struct state *state,
718718
msg = towire_hsm_sign_remote_commitment_tx(NULL,
719719
*tx,
720720
&state->channel->funding_pubkey[REMOTE],
721-
state->channel->funding);
721+
state->channel->funding,
722+
(const struct witscript **) (*tx)->output_witscripts,
723+
&state->first_per_commitment_point[REMOTE],
724+
state->channel->option_static_remotekey);
722725

723726
wire_sync_write(HSM_FD, take(msg));
724727
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -1233,7 +1236,10 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
12331236
msg = towire_hsm_sign_remote_commitment_tx(NULL,
12341237
remote_commit,
12351238
&state->channel->funding_pubkey[REMOTE],
1236-
state->channel->funding);
1239+
state->channel->funding,
1240+
(const struct witscript **) remote_commit->output_witscripts,
1241+
&state->first_per_commitment_point[REMOTE],
1242+
state->channel->option_static_remotekey);
12371243

12381244
wire_sync_write(HSM_FD, take(msg));
12391245
msg = wire_sync_read(tmpctx, HSM_FD);

tools/generate-wire.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class Type(FieldSet):
225225
'exclude_entry',
226226
'fee_states',
227227
'onionreply',
228+
'witscript',
228229
]
229230

230231
# Some BOLT types are re-typed based on their field name

wire/fromwire.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,19 @@ struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
401401
return output;
402402
}
403403

404+
struct witscript *fromwire_witscript(const tal_t *ctx, const u8 **cursor, size_t *max)
405+
{
406+
struct witscript *retval = tal(ctx, struct witscript);
407+
u16 len = fromwire_u16(cursor, max);
408+
if (len == 0) {
409+
retval->ptr = NULL;
410+
} else {
411+
retval->ptr = tal_arr(retval, u8, len);
412+
fromwire_u8_array(cursor, max, retval->ptr, len);
413+
}
414+
return retval;
415+
}
416+
404417
void fromwire_chainparams(const u8 **cursor, size_t *max,
405418
const struct chainparams **chainparams)
406419
{

wire/towire.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output)
262262
towire_u8_array(pptr, output->script, tal_count(output->script));
263263
}
264264

265+
void towire_witscript(u8 **pptr, const struct witscript *script)
266+
{
267+
if (script == NULL || script->ptr == NULL) {
268+
towire_u16(pptr, 0);
269+
} else {
270+
towire_u16(pptr, tal_count(script->ptr));
271+
towire_u8_array(pptr, script->ptr, tal_count(script->ptr));
272+
}
273+
}
274+
265275
void towire_chainparams(u8 **cursor, const struct chainparams *chainparams)
266276
{
267277
towire_bitcoin_blkid(cursor, &chainparams->genesis_blockhash);

wire/wire.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct bitcoin_txid;
2929
struct preimage;
3030
struct ripemd160;
3131
struct siphash_seed;
32+
struct witscript;
3233

3334
/* Makes generate-wire.py work */
3435
typedef char wirestring;
@@ -90,6 +91,7 @@ void towire_siphash_seed(u8 **cursor, const struct siphash_seed *seed);
9091

9192
void towire_bip32_key_version(u8 **cursor, const struct bip32_key_version *version);
9293
void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output);
94+
void towire_witscript(u8 **pptr, const struct witscript *script);
9395
void towire_chainparams(u8 **cursor, const struct chainparams *chainparams);
9496

9597
const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n);
@@ -144,6 +146,8 @@ void fromwire_bip32_key_version(const u8 **cursor, size_t *max,
144146
struct bip32_key_version *version);
145147
struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
146148
const u8 **cursor, size_t *max);
149+
struct witscript *fromwire_witscript(const tal_t *ctx,
150+
const u8 **cursor, size_t *max);
147151

148152
void fromwire_chainparams(const u8 **cursor, size_t *max,
149153
const struct chainparams **chainparams);

0 commit comments

Comments
 (0)