Skip to content

Commit 5c8f881

Browse files
authored
hsmd: Added fields to hsm_sign_remote_commitment_tx to allow complete validation.
Changelog-Added: hsmd: Added fields to hsm_sign_remote_commitment_tx to allow complete validation by signing daemon.
1 parent 149620e commit 5c8f881

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
@@ -399,6 +399,7 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
399399
tx->input_amounts = tal_arrz(tx, struct amount_sat*, input_count);
400400
tx->wtx->locktime = nlocktime;
401401
tx->wtx->version = 2;
402+
tx->output_witscripts = tal_arrz(tx, struct witscript*, output_count);
402403
tx->chainparams = chainparams;
403404
return tx;
404405
}

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
@@ -995,7 +995,10 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
995995

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

10001003
msg = hsm_req(tmpctx, take(msg));
10011004
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, wscript,
54+
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
@@ -159,6 +159,10 @@ msgtype,hsm_sign_remote_commitment_tx,19
159159
msgdata,hsm_sign_remote_commitment_tx,tx,bitcoin_tx,
160160
msgdata,hsm_sign_remote_commitment_tx,remote_funding_key,pubkey,
161161
msgdata,hsm_sign_remote_commitment_tx,funding_amount,amount_sat,
162+
msgdata,hsm_sign_remote_commitment_tx,num_witscripts,u16,
163+
msgdata,hsm_sign_remote_commitment_tx,output_witscripts,witscript,num_witscripts
164+
msgdata,hsm_sign_remote_commitment_tx,remote_per_commit,pubkey,
165+
msgdata,hsm_sign_remote_commitment_tx,option_static_remotekey,bool,
162166

163167
# channeld asks HSM to sign remote HTLC tx.
164168
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
@@ -719,7 +719,10 @@ static bool funder_finalize_channel_setup(struct state *state,
719719
msg = towire_hsm_sign_remote_commitment_tx(NULL,
720720
*tx,
721721
&state->channel->funding_pubkey[REMOTE],
722-
state->channel->funding);
722+
state->channel->funding,
723+
(const struct witscript **) (*tx)->output_witscripts,
724+
&state->first_per_commitment_point[REMOTE],
725+
state->channel->option_static_remotekey);
723726

724727
wire_sync_write(HSM_FD, take(msg));
725728
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -1234,7 +1237,10 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
12341237
msg = towire_hsm_sign_remote_commitment_tx(NULL,
12351238
remote_commit,
12361239
&state->channel->funding_pubkey[REMOTE],
1237-
state->channel->funding);
1240+
state->channel->funding,
1241+
(const struct witscript **) remote_commit->output_witscripts,
1242+
&state->first_per_commitment_point[REMOTE],
1243+
state->channel->option_static_remotekey);
12381244

12391245
wire_sync_write(HSM_FD, take(msg));
12401246
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
403403
return output;
404404
}
405405

406+
struct witscript *fromwire_witscript(const tal_t *ctx, const u8 **cursor, size_t *max)
407+
{
408+
struct witscript *retval;
409+
u16 len = fromwire_u16(cursor, max);
410+
if (!len)
411+
return NULL;
412+
retval = tal(ctx, struct witscript);
413+
retval->ptr = tal_arr(retval, u8, len);
414+
fromwire_u8_array(cursor, max, retval->ptr, len);
415+
return retval;
416+
}
417+
406418
void fromwire_chainparams(const u8 **cursor, size_t *max,
407419
const struct chainparams **chainparams)
408420
{

wire/towire.c

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

266+
void towire_witscript(u8 **pptr, const struct witscript *script)
267+
{
268+
if (script == NULL) {
269+
towire_u16(pptr, 0);
270+
} else {
271+
assert(script->ptr != NULL);
272+
towire_u16(pptr, tal_count(script->ptr));
273+
towire_u8_array(pptr, script->ptr, tal_count(script->ptr));
274+
}
275+
}
276+
266277
void towire_chainparams(u8 **cursor, const struct chainparams *chainparams)
267278
{
268279
towire_bitcoin_blkid(cursor, &chainparams->genesis_blockhash);

wire/wire.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct bitcoin_txid;
3030
struct preimage;
3131
struct ripemd160;
3232
struct siphash_seed;
33+
struct witscript;
3334

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

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

9698
const u8 *fromwire(const u8 **cursor, size_t *max, void *copy, size_t n);
@@ -145,6 +147,8 @@ void fromwire_bip32_key_version(const u8 **cursor, size_t *max,
145147
struct bip32_key_version *version);
146148
struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
147149
const u8 **cursor, size_t *max);
150+
struct witscript *fromwire_witscript(const tal_t *ctx,
151+
const u8 **cursor, size_t *max);
148152

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

0 commit comments

Comments
 (0)