Skip to content

Commit d41d18a

Browse files
committed
API mods: SpendType, Descriptors, Funding return
1 parent 2a0f0ac commit d41d18a

File tree

6 files changed

+145
-76
lines changed

6 files changed

+145
-76
lines changed

contrib/remote_hsmd/NOTES.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,30 @@ Some popular tests:
7474
# sign-channel-announcement
7575
export THETEST=tests/test_closing.py::test_closing_different_fees
7676

77+
# P2SH_P2WPKH
78+
export THETEST=tests/test_closing.py::test_onchain_first_commit
79+
export THETEST=tests/test_connection.py::test_disconnect_funder
80+
export THETEST=tests/test_connection.py::test_disconnect_fundee
81+
export THETEST=tests/test_connection.py::test_reconnect_signed
82+
export THETEST=tests/test_connection.py::test_reconnect_openingd
83+
export THETEST=tests/test_connection.py::test_shutdown_awaiting_lockin
84+
7785
rust-lightning-signer
7886
----------------------------------------------------------------
7987

8088
cargo run --bin server |& tee log3
89+
90+
91+
Signing Formats
92+
----------------------------------------------------------------
93+
94+
```
95+
rust-lightning c-lightning rust-lightning-signer
96+
p2pkh P2PKH
97+
p2sh
98+
p2wpkh p2wpkh P2WPKH
99+
p2shwpkh p2sh-p2wpkh P2SH_P2WPKH
100+
p2wsh
101+
p2shwsh
102+
```
103+

contrib/remote_hsmd/dump.cc

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,15 @@ string dump_pubkey(const struct pubkey *kp)
7575
return dump_hex(kp->pubkey.data, sizeof(kp->pubkey.data));
7676
}
7777

78-
string dump_witnesses(const u8 ***wp)
78+
string dump_signatures(const u8 **sp)
7979
{
8080
ostringstream ostrm;
8181
ostrm << "[";
82-
for (size_t input_ndx = 0; input_ndx < tal_count(wp); ++input_ndx) {
82+
for (size_t input_ndx = 0; input_ndx < tal_count(sp); ++input_ndx) {
8383
if (input_ndx != 0)
8484
ostrm << " ";
85-
ostrm << "[";
86-
u8 const **stack = wp[input_ndx];
87-
for (size_t item_ndx = 0; item_ndx < tal_count(stack);
88-
++item_ndx) {
89-
if (item_ndx != 0)
90-
ostrm << " ";
91-
u8 const *item = stack[item_ndx];
92-
ostrm << dump_hex(item, tal_count(item));
93-
}
94-
ostrm << "]";
85+
u8 const *sig = sp[input_ndx];
86+
ostrm << dump_hex(sig, tal_count(sig));
9587
}
9688
ostrm << "]";
9789
return ostrm.str();

contrib/remote_hsmd/dump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ std::string dump_secp256k1_ecdsa_recoverable_signature(const secp256k1_ecdsa_rec
99
std::string dump_secret(const struct secret *sp);
1010
std::string dump_node_id(const struct node_id *pp);
1111
std::string dump_pubkey(const struct pubkey *kp);
12-
std::string dump_witnesses(const u8 ***wp);
12+
std::string dump_signatures(const u8 **sp);
1313
std::string dump_unilateral_close_info(const struct unilateral_close_info *ip);
1414
std::string dump_utxo(const struct utxo *in);
1515
std::string dump_utxos(const struct utxo **utxos);

contrib/remote_hsmd/hsmd.c

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
16381638
u32 change_keyindex;
16391639
struct utxo **utxos;
16401640
struct bitcoin_tx *tx;
1641+
struct bitcoin_tx *tx2; // REF
16411642
struct pubkey changekey;
16421643
struct bitcoin_tx_output **outputs;
16431644
u32 nlocktime;
@@ -1655,20 +1656,6 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
16551656
cast_const2(const struct utxo **, utxos), outputs,
16561657
&changekey, change_out, NULL, NULL, nlocktime);
16571658

1658-
u8 *** sigs;
1659-
proxy_stat rv = proxy_handle_sign_withdrawal_tx(
1660-
&c->id, c->dbid, &satoshi_out,
1661-
&change_out, change_keyindex,
1662-
outputs, utxos, tx, &sigs);
1663-
if (PROXY_PERMANENT(rv))
1664-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
1665-
"proxy_%s failed: %s", __FUNCTION__,
1666-
proxy_last_message());
1667-
else if (!PROXY_SUCCESS(rv))
1668-
return bad_req_fmt(conn, c, msg_in,
1669-
"proxy_%s error: %s", __FUNCTION__,
1670-
proxy_last_message());
1671-
16721659
/* FIXME - There are two things we can't do remotely yet:
16731660
* 1. Handle P2SH inputs.
16741661
* 2. Handle inputs w/ close_info.
@@ -1677,25 +1664,57 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
16771664
for (size_t ii = 0; ii < tx->wtx->num_inputs; ii++)
16781665
if (utxos[ii]->is_p2sh || utxos[ii]->close_info)
16791666
demure = true;
1667+
16801668
if (!demure) {
1669+
u8 ** sigs;
1670+
proxy_stat rv = proxy_handle_sign_withdrawal_tx(
1671+
&c->id, c->dbid, &satoshi_out,
1672+
&change_out, change_keyindex,
1673+
outputs, utxos, tx, &sigs);
1674+
if (PROXY_PERMANENT(rv))
1675+
status_failed(STATUS_FAIL_INTERNAL_ERROR,
1676+
"proxy_%s failed: %s", __FUNCTION__,
1677+
proxy_last_message());
1678+
else if (!PROXY_SUCCESS(rv))
1679+
return bad_req_fmt(conn, c, msg_in,
1680+
"proxy_%s error: %s", __FUNCTION__,
1681+
proxy_last_message());
1682+
16811683
/* Sign w/ the remote lightning-signer. */
16821684
g_proxy_impl = PROXY_IMPL_COMPLETE;
16831685
assert(tal_count(sigs) == tal_count(utxos));
16841686
for (size_t ii = 0; ii < tal_count(sigs); ++ii) {
1685-
assert(tal_count(sigs[ii]) == 2);
1686-
1687+
/* Figure out keys to spend this. */
1688+
struct pubkey inkey;
1689+
u8 der_pubkey[PUBKEY_CMPR_LEN];
1690+
const struct utxo *in = utxos[ii];
1691+
hsm_key_for_utxo(NULL, &inkey, in);
1692+
pubkey_to_der(der_pubkey, &inkey);
16871693
u8 **witness = tal_arr(tx, u8 *, 2);
1688-
witness[0] = tal_dup_arr(witness, u8, sigs[ii][0],
1689-
tal_count(sigs[ii][0]), 0);
1690-
witness[1] = tal_dup_arr(witness, u8, sigs[ii][1],
1691-
tal_count(sigs[ii][1]), 0);
1694+
witness[0] = tal_dup_arr(witness, u8,
1695+
sigs[ii],
1696+
tal_count(sigs[ii]), 0);
1697+
witness[1] = tal_dup_arr(witness, u8,
1698+
der_pubkey,
1699+
sizeof(der_pubkey), 0);
16921700
bitcoin_tx_input_set_witness(tx, ii, take(witness));
16931701
}
1702+
1703+
print_tx("RLS", tx);
16941704
} else {
16951705
/* It's P2SH, need to sign here */
16961706
g_proxy_impl = PROXY_IMPL_MARSHALED;
16971707
sign_all_inputs(tx, utxos);
16981708
}
1709+
1710+
tx2 = withdraw_tx(tmpctx, c->chainparams,
1711+
cast_const2(const struct utxo **, utxos), outputs,
1712+
&changekey, change_out, NULL, NULL, nlocktime);
1713+
1714+
sign_all_inputs(tx2, utxos);
1715+
1716+
print_tx("REF", tx2);
1717+
16991718
return req_reply(conn, c,
17001719
take(towire_hsm_sign_withdrawal_reply(NULL, tx)));
17011720
}

contrib/remote_hsmd/proxy.cc

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -140,36 +140,38 @@ void marshal_single_input_tx(struct bitcoin_tx const *tx,
140140
if (output_witscript) {
141141
/* Called with a single witscript. */
142142
assert(tx->wtx->num_outputs == 1);
143-
o_tp->add_output_witscripts((const char *) output_witscript,
144-
tal_count(output_witscript));
145143
} else if (output_witscripts) {
146144
/* Called with an array of witscripts. */
147-
size_t nwitscripts = tal_count(output_witscripts);
148-
assert(nwitscripts == tx->wtx->num_outputs);
149-
for (size_t ii = 0; ii < tx->wtx->num_outputs; ii++)
150-
if (output_witscripts[ii])
151-
o_tp->add_output_witscripts(
152-
(const char *)
153-
output_witscripts[ii]->ptr,
154-
tal_count(output_witscripts[ii]->ptr));
155-
else
156-
o_tp->add_output_witscripts("");
157-
} else {
158-
/* Called with no witscrtipts. */
159-
for (size_t ii = 0; ii < tx->wtx->num_outputs; ii++)
160-
o_tp->add_output_witscripts("");
145+
assert(tal_count(output_witscripts) == tx->wtx->num_outputs);
161146
}
162147

163148
o_tp->set_raw_tx_bytes(serialized_tx(tx, true));
164149

165150
assert(tx->wtx->num_inputs == 1);
166-
SignDescriptor *desc = o_tp->add_input_descs();
167-
desc->mutable_output()->set_value(tx->input_amounts[0]->satoshis);
151+
InputDescriptor *idesc = o_tp->add_input_descs();
152+
idesc->mutable_prev_output()->set_value(tx->input_amounts[0]->satoshis);
168153
/* FIXME - What else needs to be set? */
169154

170155
for (size_t ii = 0; ii < tx->wtx->num_outputs; ii++) {
171-
SignDescriptor *desc = o_tp->add_output_descs();
172-
/* FIXME - We don't need to set *anything* here? */
156+
OutputDescriptor *odesc = o_tp->add_output_descs();
157+
if (output_witscript) {
158+
/* We have a single witscript. */
159+
odesc->set_witscript((const char *) output_witscript,
160+
tal_count(output_witscript));
161+
} else if (output_witscripts) {
162+
/* We have an array of witscripts. */
163+
if (output_witscripts[ii])
164+
odesc->set_witscript(
165+
(const char *)
166+
output_witscripts[ii]->ptr,
167+
tal_count(output_witscripts[ii]->ptr));
168+
else
169+
odesc->set_witscript("");
170+
} else {
171+
/* Called w/ no witscripts. */
172+
odesc->set_witscript("");
173+
}
174+
173175
}
174176
}
175177

@@ -227,23 +229,17 @@ void unmarshal_ecdsa_recoverable_signature(ECDSARecoverableSignature const &es,
227229
assert(ok);
228230
}
229231

230-
void unmarshal_witnesses(RepeatedPtrField<WitnessStack> const &wits,
231-
u8 ****o_sigs)
232+
void unmarshal_signatures(RepeatedPtrField<BitcoinSignature> const &sigs,
233+
u8 ***o_sigs)
232234
{
233-
u8 ***osigs = NULL;
234-
int nsigs = wits.size();
235+
u8 **osigs = NULL;
236+
int nsigs = sigs.size();
235237
if (nsigs > 0) {
236-
osigs = tal_arrz(tmpctx, u8**, nsigs);
238+
osigs = tal_arrz(tmpctx, u8*, nsigs);
237239
for (size_t ii = 0; ii < nsigs; ++ii) {
238-
WitnessStack const &sig = wits[ii];
239-
int nelem = sig.item_size();
240-
osigs[ii] = tal_arrz(osigs, u8*, nelem);
241-
for (size_t jj = 0; jj < nelem; ++jj) {
242-
string const &elem = sig.item(jj);
243-
size_t elen = elem.size();
244-
osigs[ii][jj] = tal_arr(osigs[ii], u8, elen);
245-
memcpy(osigs[ii][jj], &elem[0], elen);
246-
}
240+
BitcoinSignature const &bs = sigs[ii];
241+
osigs[ii] = tal_arr(osigs, u8, bs.data().size());
242+
memcpy(osigs[ii], bs.data().data(), bs.data().size());
247243
}
248244
}
249245
*o_sigs = osigs;
@@ -397,8 +393,25 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
397393
struct bitcoin_tx_output **outputs,
398394
struct utxo **utxos,
399395
struct bitcoin_tx *tx,
400-
u8 ****o_sigs)
396+
u8 ***o_sigs)
401397
{
398+
fprintf(stderr,
399+
"%s:%d %s self_id=%s peer_id=%s dbid=%" PRIu64 " "
400+
"satoshi_out=%" PRIu64 " change_out=%" PRIu64 " "
401+
"change_keyindex=%u utxos=%s outputs=%s tx=%s\n",
402+
__FILE__, __LINE__, __FUNCTION__,
403+
dump_node_id(&self_id).c_str(),
404+
dump_node_id(peer_id).c_str(),
405+
dbid,
406+
satoshi_out->satoshis,
407+
change_out->satoshis,
408+
change_keyindex,
409+
dump_utxos((const struct utxo **)utxos).c_str(),
410+
dump_bitcoin_tx_outputs(
411+
(const struct bitcoin_tx_output **)outputs).c_str(),
412+
dump_tx(tx).c_str()
413+
);
414+
402415
status_debug(
403416
"%s:%d %s self_id=%s peer_id=%s dbid=%" PRIu64 " "
404417
"satoshi_out=%" PRIu64 " change_out=%" PRIu64 " "
@@ -427,9 +440,12 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
427440
const struct utxo *in = utxos[ii];
428441
/* Fails in tests/test_closing.py::test_onchain_first_commit */
429442
/* assert(!in->is_p2sh); */
430-
SignDescriptor *desc = req.mutable_tx()->add_input_descs();
431-
desc->mutable_key_loc()->set_key_index(in->keyindex);
432-
desc->mutable_output()->set_value(in->amount.satoshis);
443+
InputDescriptor *idesc = req.mutable_tx()->add_input_descs();
444+
idesc->mutable_key_loc()->set_key_index(in->keyindex);
445+
idesc->mutable_prev_output()->set_value(in->amount.satoshis);
446+
idesc->set_spend_type(in->is_p2sh
447+
? SpendType::P2SH_P2WPKH
448+
: SpendType::P2WPKH);
433449
}
434450

435451
/* We expect exactly two total ouputs, with one non-change. */
@@ -440,7 +456,7 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
440456
assert(tal_count(outputs) == 1);
441457
for (size_t ii = 0; ii < tx->wtx->num_outputs; ii++) {
442458
const struct wally_tx_output *out = &tx->wtx->outputs[ii];
443-
SignDescriptor *desc = req.mutable_tx()->add_output_descs();
459+
OutputDescriptor *odesc = req.mutable_tx()->add_output_descs();
444460
/* Does this output match the funding output? */
445461
if (memeq(out->script, out->script_len,
446462
outputs[0]->script, tal_count(outputs[0]->script))) {
@@ -449,22 +465,31 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
449465
} else {
450466
/* Nope, this must be the change output. */
451467
assert(out->satoshi == change_out->satoshis);
452-
desc->mutable_key_loc()->set_key_index(change_keyindex);
468+
odesc->mutable_key_loc()->
469+
set_key_index(change_keyindex);
453470
}
454471
}
455472

456473
ClientContext context;
457474
SignFundingTxReply rsp;
458475
Status status = stub->SignFundingTx(&context, req, &rsp);
459476
if (status.ok()) {
460-
unmarshal_witnesses(rsp.witnesses(), o_sigs);
477+
unmarshal_signatures(rsp.signatures(), o_sigs);
478+
fprintf(stderr, "%s:%d %s self_id=%s witnesses=%s\n",
479+
__FILE__, __LINE__, __FUNCTION__,
480+
dump_node_id(&self_id).c_str(),
481+
dump_signatures((u8 const **) *o_sigs).c_str());
461482
status_debug("%s:%d %s self_id=%s witnesses=%s",
462483
__FILE__, __LINE__, __FUNCTION__,
463484
dump_node_id(&self_id).c_str(),
464-
dump_witnesses((u8 const ***) *o_sigs).c_str());
485+
dump_signatures((u8 const **) *o_sigs).c_str());
465486
last_message = "success";
466487
return PROXY_OK;
467488
} else {
489+
fprintf(stderr, "%s:%d %s: self_id=%s %s\n",
490+
__FILE__, __LINE__, __FUNCTION__,
491+
dump_node_id(&self_id).c_str(),
492+
status.error_message().c_str());
468493
status_unusual("%s:%d %s: self_id=%s %s",
469494
__FILE__, __LINE__, __FUNCTION__,
470495
dump_node_id(&self_id).c_str(),
@@ -1236,4 +1261,11 @@ proxy_stat proxy_handle_sign_node_announcement(
12361261
}
12371262
}
12381263

1264+
// FIXME - This routine allows us to pretty print the tx to stderr
1265+
// from C code. Probably should remove it in production ...
1266+
void print_tx(char const *tag, struct bitcoin_tx const *tx)
1267+
{
1268+
fprintf(stderr, "%s: tx=%s\n", tag, dump_tx(tx).c_str());
1269+
}
1270+
12391271
} /* extern "C" */

contrib/remote_hsmd/proxy.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
5555
struct bitcoin_tx_output **outputs,
5656
struct utxo **utxos,
5757
struct bitcoin_tx *tx,
58-
u8 ****o_sigs);
58+
u8 ***o_sigs);
5959

6060
proxy_stat proxy_handle_sign_remote_commitment_tx(
6161
struct bitcoin_tx *tx,
@@ -166,6 +166,9 @@ proxy_stat proxy_handle_sign_node_announcement(
166166
u8 *node_announcement,
167167
secp256k1_ecdsa_signature *o_sig);
168168

169+
// FIXME - For debugging, remove for production.
170+
void print_tx(char const *tag, struct bitcoin_tx const *tx);
171+
169172
#ifdef __cplusplus
170173
} /* extern C */
171174
#endif

0 commit comments

Comments
 (0)