Skip to content

Commit 6934e72

Browse files
committed
Added unilateral-close-info.
1 parent f37c12d commit 6934e72

File tree

6 files changed

+102
-102
lines changed

6 files changed

+102
-102
lines changed

contrib/remote_hsmd/NOTES.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ Some popular tests:
8282
export THETEST=tests/test_connection.py::test_reconnect_openingd
8383
export THETEST=tests/test_connection.py::test_shutdown_awaiting_lockin
8484

85-
# unilateral_close_info
86-
tests/test_closing.py::test_closing_negotiation_reconnect
87-
tests/test_closing.py::test_permfail
88-
tests/test_closing.py::test_option_upfront_shutdown_script
89-
tests/test_connection.py::test_fee_limits
85+
# unilateral_close_info option_static_remotekey
86+
export THETEST=tests/test_connection.py::test_fee_limits
87+
export THETEST=tests/test_closing.py::test_option_upfront_shutdown_script
88+
9089

9190
rust-lightning-signer
9291
----------------------------------------------------------------

contrib/remote_hsmd/dump.cc

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

78-
string dump_signatures(const u8 **sp)
78+
string dump_witnesses(const u8 ***wp)
7979
{
8080
ostringstream ostrm;
8181
ostrm << "[";
82-
for (size_t input_ndx = 0; input_ndx < tal_count(sp); ++input_ndx) {
82+
for (size_t input_ndx = 0; input_ndx < tal_count(wp); ++input_ndx) {
8383
if (input_ndx != 0)
8484
ostrm << " ";
85-
u8 const *sig = sp[input_ndx];
85+
ostrm << "[";
86+
u8 const *sig = wp[input_ndx][0];
8687
ostrm << dump_hex(sig, tal_count(sig));
88+
ostrm << " ";
89+
u8 const *pubkey = wp[input_ndx][1];
90+
ostrm << dump_hex(pubkey, tal_count(pubkey));
91+
ostrm << "]";
8792
}
8893
ostrm << "]";
8994
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_signatures(const u8 **sp);
12+
std::string dump_witnesses(const u8 ***wp);
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: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,6 @@ 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
16421641
struct pubkey changekey;
16431642
struct bitcoin_tx_output **outputs;
16441643
u32 nlocktime;
@@ -1652,77 +1651,60 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
16521651
return bad_req_fmt(conn, c, msg_in,
16531652
"Failed to get key %u", change_keyindex);
16541653

1654+
// FIXME - The next few lines are for debug, remove them.
1655+
struct bitcoin_tx *tx2 =
1656+
withdraw_tx(tmpctx, c->chainparams,
1657+
cast_const2(const struct utxo **, utxos), outputs,
1658+
&changekey, change_out, NULL, NULL, nlocktime);
1659+
sign_all_inputs(tx2, utxos);
1660+
print_tx("REF", tx2);
1661+
16551662
tx = withdraw_tx(tmpctx, c->chainparams,
16561663
cast_const2(const struct utxo **, utxos), outputs,
16571664
&changekey, change_out, NULL, NULL, nlocktime);
16581665

1659-
/* FIXME - There are things we can't do remotely yet:
1660-
* 2. Handle inputs w/ close_info.
1661-
*/
1662-
bool demure = false;
1663-
for (size_t ii = 0; ii < tx->wtx->num_inputs; ii++)
1664-
if (utxos[ii]->close_info)
1665-
demure = true;
1666-
1667-
if (!demure) {
1668-
u8 ** sigs;
1669-
proxy_stat rv = proxy_handle_sign_withdrawal_tx(
1670-
&c->id, c->dbid, &satoshi_out,
1671-
&change_out, change_keyindex,
1672-
outputs, utxos, tx, &sigs);
1673-
if (PROXY_PERMANENT(rv))
1674-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
1675-
"proxy_%s failed: %s", __FUNCTION__,
1676-
proxy_last_message());
1677-
else if (!PROXY_SUCCESS(rv))
1678-
return bad_req_fmt(conn, c, msg_in,
1679-
"proxy_%s error: %s", __FUNCTION__,
1680-
proxy_last_message());
1681-
1682-
/* Sign w/ the remote lightning-signer. */
1683-
g_proxy_impl = PROXY_IMPL_COMPLETE;
1684-
assert(tal_count(sigs) == tal_count(utxos));
1685-
for (size_t ii = 0; ii < tal_count(sigs); ++ii) {
1686-
/* Figure out keys to spend this. */
1687-
struct pubkey inkey;
1688-
u8 der_pubkey[PUBKEY_CMPR_LEN];
1689-
const struct utxo *in = utxos[ii];
1690-
hsm_key_for_utxo(NULL, &inkey, in);
1691-
pubkey_to_der(der_pubkey, &inkey);
1692-
1693-
if (in->is_p2sh) {
1694-
u8 *script = bitcoin_scriptsig_p2sh_p2wpkh(
1695-
tx, &inkey);
1696-
bitcoin_tx_input_set_script(tx, ii, script);
1697-
1698-
} else {
1699-
bitcoin_tx_input_set_script(tx, ii, NULL);
1700-
}
1701-
1702-
u8 **witness = tal_arr(tx, u8 *, 2);
1703-
witness[0] = tal_dup_arr(witness, u8,
1704-
sigs[ii],
1705-
tal_count(sigs[ii]), 0);
1706-
witness[1] = tal_dup_arr(witness, u8,
1707-
der_pubkey,
1708-
sizeof(der_pubkey), 0);
1709-
bitcoin_tx_input_set_witness(tx, ii, take(witness));
1710-
}
1666+
u8 *** wits;
1667+
proxy_stat rv = proxy_handle_sign_withdrawal_tx(
1668+
&c->id, c->dbid, &satoshi_out,
1669+
&change_out, change_keyindex,
1670+
outputs, utxos, tx, &wits);
1671+
if (PROXY_PERMANENT(rv))
1672+
status_failed(STATUS_FAIL_INTERNAL_ERROR,
1673+
"proxy_%s failed: %s", __FUNCTION__,
1674+
proxy_last_message());
1675+
else if (!PROXY_SUCCESS(rv))
1676+
return bad_req_fmt(conn, c, msg_in,
1677+
"proxy_%s error: %s", __FUNCTION__,
1678+
proxy_last_message());
17111679

1712-
print_tx("RLS", tx);
1713-
} else {
1714-
/* It's P2SH, need to sign here */
1715-
g_proxy_impl = PROXY_IMPL_MARSHALED;
1716-
sign_all_inputs(tx, utxos);
1717-
}
1680+
/* Sign w/ the remote lightning-signer. */
1681+
g_proxy_impl = PROXY_IMPL_COMPLETE;
1682+
assert(tal_count(wits) == tal_count(utxos));
1683+
for (size_t ii = 0; ii < tal_count(wits); ++ii) {
1684+
struct pubkey inkey;
1685+
bool ok = pubkey_from_der(
1686+
wits[ii][1], tal_count(wits[ii][1]), &inkey);
1687+
assert(ok);
17181688

1719-
tx2 = withdraw_tx(tmpctx, c->chainparams,
1720-
cast_const2(const struct utxo **, utxos), outputs,
1721-
&changekey, change_out, NULL, NULL, nlocktime);
1689+
if (utxos[ii]->is_p2sh) {
1690+
u8 *script = bitcoin_scriptsig_p2sh_p2wpkh(
1691+
tx, &inkey);
1692+
bitcoin_tx_input_set_script(tx, ii, script);
17221693

1723-
sign_all_inputs(tx2, utxos);
1694+
} else {
1695+
bitcoin_tx_input_set_script(tx, ii, NULL);
1696+
}
17241697

1725-
print_tx("REF", tx2);
1698+
u8 **witness = tal_arr(tx, u8 *, 2);
1699+
witness[0] = tal_dup_arr(witness, u8,
1700+
wits[ii][0],
1701+
tal_count(wits[ii][0]), 0);
1702+
witness[1] = tal_dup_arr(witness, u8,
1703+
wits[ii][1],
1704+
tal_count(wits[ii][1]), 0);
1705+
bitcoin_tx_input_set_witness(tx, ii, take(witness));
1706+
}
1707+
print_tx("RLS", tx);
17261708

17271709
return req_reply(conn, c,
17281710
take(towire_hsm_sign_withdrawal_reply(NULL, tx)));

contrib/remote_hsmd/proxy.cc

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ string serialized_tx(struct bitcoin_tx const *tx, bool bip144)
110110
return retval;
111111
}
112112

113-
void marshal_channel_nonce(struct node_id *peer_id, u64 dbid,
114-
ChannelNonce *o_np)
113+
void marshal_channel_nonce(struct node_id const *peer_id, u64 dbid,
114+
ChannelNonce *o_np)
115115
{
116116
o_np->set_data(string((char const *)peer_id->k, sizeof(peer_id->k)) +
117117
string((char const *)&dbid, sizeof(dbid)));
@@ -132,6 +132,25 @@ void marshal_pubkey(struct pubkey const *pp, PubKey *o_pp)
132132
o_pp->set_data(pp->pubkey.data, sizeof(pp->pubkey.data));
133133
}
134134

135+
void marshal_utxo(struct utxo const *up, InputDescriptor *idesc)
136+
{
137+
idesc->mutable_key_loc()->set_key_index(up->keyindex);
138+
idesc->mutable_prev_output()->set_value(up->amount.satoshis);
139+
/* FIXME - where does pk_script come from? */
140+
idesc->set_spend_type(up->is_p2sh
141+
? SpendType::P2SH_P2WPKH
142+
: SpendType::P2WPKH);
143+
if (up->close_info) {
144+
UnilateralCloseInfo *cinfo = idesc->mutable_close_info();
145+
marshal_channel_nonce(&up->close_info->peer_id,
146+
up->close_info->channel_id,
147+
cinfo->mutable_channel_nonce());
148+
if (up->close_info->commitment_point)
149+
marshal_pubkey(up->close_info->commitment_point,
150+
cinfo->mutable_commitment_point());
151+
}
152+
}
153+
135154
void marshal_single_input_tx(struct bitcoin_tx const *tx,
136155
u8 const *output_witscript,
137156
struct witscript const **output_witscripts,
@@ -229,20 +248,24 @@ void unmarshal_ecdsa_recoverable_signature(ECDSARecoverableSignature const &es,
229248
assert(ok);
230249
}
231250

232-
void unmarshal_signatures(RepeatedPtrField<BitcoinSignature> const &sigs,
233-
u8 ***o_sigs)
251+
void unmarshal_witnesses(RepeatedPtrField<Witness> const &wits, u8 ****o_wits)
234252
{
235-
u8 **osigs = NULL;
236-
int nsigs = sigs.size();
237-
if (nsigs > 0) {
238-
osigs = tal_arrz(tmpctx, u8*, nsigs);
239-
for (size_t ii = 0; ii < nsigs; ++ii) {
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());
253+
u8 ***owits = NULL;
254+
int nwits = wits.size();
255+
if (nwits > 0) {
256+
owits = tal_arrz(tmpctx, u8**, nwits);
257+
for (size_t ii = 0; ii < nwits; ++ii) {
258+
owits[ii] = tal_arrz(owits, u8*, 2);
259+
Witness const &wit = wits[ii];
260+
const string &sig = wit.signature().data();
261+
const string &pubkey = wit.pubkey().data();
262+
owits[ii][0] = tal_arr(owits[ii], u8, sig.size());
263+
memcpy(owits[ii][0], sig.data(), sig.size());
264+
owits[ii][1] = tal_arr(owits[ii], u8, pubkey.size());
265+
memcpy(owits[ii][1], pubkey.data(), pubkey.size());
243266
}
244267
}
245-
*o_sigs = osigs;
268+
*o_wits = owits;
246269
}
247270

248271
/* Copied from ccan/mem/mem.h which the c++ compiler doesn't like */
@@ -393,7 +416,7 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
393416
struct bitcoin_tx_output **outputs,
394417
struct utxo **utxos,
395418
struct bitcoin_tx *tx,
396-
u8 ***o_sigs)
419+
u8 ****o_wits)
397420
{
398421
fprintf(stderr,
399422
"%s:%d %s self_id=%s peer_id=%s dbid=%" PRIu64 " "
@@ -436,17 +459,8 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
436459

437460
req.mutable_tx()->set_raw_tx_bytes(serialized_tx(tx, true));
438461
assert(tx->wtx->num_inputs == tal_count(utxos));
439-
for (size_t ii = 0; ii < tx->wtx->num_inputs; ii++) {
440-
const struct utxo *in = utxos[ii];
441-
/* Fails in tests/test_closing.py::test_onchain_first_commit */
442-
/* assert(!in->is_p2sh); */
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);
449-
}
462+
for (size_t ii = 0; ii < tx->wtx->num_inputs; ii++)
463+
marshal_utxo(utxos[ii], req.mutable_tx()->add_input_descs());
450464

451465
/* We expect exactly two total ouputs, with one non-change. */
452466
/* FIXME - next assert fails in
@@ -474,15 +488,15 @@ proxy_stat proxy_handle_sign_withdrawal_tx(
474488
SignFundingTxReply rsp;
475489
Status status = stub->SignFundingTx(&context, req, &rsp);
476490
if (status.ok()) {
477-
unmarshal_signatures(rsp.signatures(), o_sigs);
491+
unmarshal_witnesses(rsp.witnesses(), o_wits);
478492
fprintf(stderr, "%s:%d %s self_id=%s witnesses=%s\n",
479493
__FILE__, __LINE__, __FUNCTION__,
480494
dump_node_id(&self_id).c_str(),
481-
dump_signatures((u8 const **) *o_sigs).c_str());
495+
dump_witnesses((u8 const ***) *o_wits).c_str());
482496
status_debug("%s:%d %s self_id=%s witnesses=%s",
483497
__FILE__, __LINE__, __FUNCTION__,
484498
dump_node_id(&self_id).c_str(),
485-
dump_signatures((u8 const **) *o_sigs).c_str());
499+
dump_witnesses((u8 const ***) *o_wits).c_str());
486500
last_message = "success";
487501
return PROXY_OK;
488502
} else {

contrib/remote_hsmd/proxy.h

Lines changed: 1 addition & 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_wits);
5959

6060
proxy_stat proxy_handle_sign_remote_commitment_tx(
6161
struct bitcoin_tx *tx,

0 commit comments

Comments
 (0)