Skip to content

Commit c2227a4

Browse files
authored
Enable handle_sign_withdrawal_tx signing of PSBT. (#13)
* Converted debug logging to json format. * Switched status_debug to STATUS_DEBUG macro.
1 parent b318afb commit c2227a4

File tree

6 files changed

+388
-276
lines changed

6 files changed

+388
-276
lines changed

contrib/remote_hsmd/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Setup
88

99
Building
1010

11+
make distclean
1112
./configure --enable-developer
1213
make
1314

contrib/remote_hsmd/dump.cc

Lines changed: 169 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ extern "C" {
2121
using std::ostringstream;
2222
using std::string;
2323

24+
bool pubkey_is_compressed(const unsigned char pubkey[EC_PUBLIC_KEY_UNCOMPRESSED_LEN]) {
25+
return pubkey[0] == 0x02 || pubkey[0] == 0x03;
26+
}
27+
2428
/* type_to_string has issues in the C++ environment, use this to
2529
dump binary data as hex instead. */
2630
string dump_hex(const void *vptr, size_t sz)
2731
{
2832
static const char hex[] = "0123456789abcdef";
29-
string retval(sz*2, '\0');
33+
ostringstream ostrm;
34+
ostrm << '"';
3035
uint8_t const * ptr = (uint8_t const *) vptr;
3136
for (size_t ii = 0; ii < sz; ++ii) {
32-
retval[ii*2+0] = hex[(*ptr) >> 4];
33-
retval[ii*2+1] = hex[(*ptr) & 0xf];
34-
ptr++;
37+
ostrm << hex[(*ptr) >> 4]
38+
<< hex[(*ptr) & 0xf];
39+
++ptr;
3540
}
36-
return retval;
41+
ostrm << '"';
42+
return ostrm.str();
3743
}
3844

3945
string dump_bitcoin_txid(const struct bitcoin_txid *txid)
@@ -45,8 +51,9 @@ string dump_bitcoin_signature(const struct bitcoin_signature *sp)
4551
{
4652
ostringstream ostrm;
4753
ostrm << "{ "
48-
<< "sighash_type=" << int(sp->sighash_type)
49-
<< "s=" << dump_secp256k1_ecdsa_signature(&sp->s)
54+
<< "\"sighash_type\":" << int(sp->sighash_type)
55+
<< ", \"s\":"
56+
<< '"' << dump_secp256k1_ecdsa_signature(&sp->s) << '"'
5057
<< " }";
5158
return ostrm.str();
5259
}
@@ -93,11 +100,11 @@ string dump_witnesses(const u8 ***wp)
93100
ostrm << "[";
94101
for (size_t input_ndx = 0; input_ndx < tal_count(wp); ++input_ndx) {
95102
if (input_ndx != 0)
96-
ostrm << " ";
103+
ostrm << ", ";
97104
ostrm << "[";
98105
u8 const *sig = wp[input_ndx][0];
99106
ostrm << dump_hex(sig, tal_count(sig));
100-
ostrm << " ";
107+
ostrm << ", ";
101108
u8 const *pubkey = wp[input_ndx][1];
102109
ostrm << dump_hex(pubkey, tal_count(pubkey));
103110
ostrm << "]";
@@ -110,10 +117,10 @@ string dump_basepoints(const struct basepoints *bp)
110117
{
111118
ostringstream ostrm;
112119
ostrm << "{ ";
113-
ostrm << "revocation=" << dump_pubkey(&bp->revocation);
114-
ostrm << ", payment=" << dump_pubkey(&bp->payment);
115-
ostrm << ", htlc=" << dump_pubkey(&bp->htlc);
116-
ostrm << ", delayed_payment=" << dump_pubkey(&bp->delayed_payment);
120+
ostrm << "\"revocation\":" << dump_pubkey(&bp->revocation);
121+
ostrm << ", \"payment\":" << dump_pubkey(&bp->payment);
122+
ostrm << ", \"htlc\":" << dump_pubkey(&bp->htlc);
123+
ostrm << ", \"delayed_payment\":" << dump_pubkey(&bp->delayed_payment);
117124
ostrm << " }";
118125
return ostrm.str();
119126
}
@@ -122,11 +129,11 @@ string dump_unilateral_close_info(const struct unilateral_close_info *ip)
122129
{
123130
ostringstream ostrm;
124131
ostrm << "{ ";
125-
ostrm << "channel_id=" << ip->channel_id;
126-
ostrm << ", peer_id=" << dump_node_id(&ip->peer_id);
127-
ostrm << ", commitment_point=" <<
132+
ostrm << "\"channel_id\":" << ip->channel_id;
133+
ostrm << ", \"peer_id\":" << dump_node_id(&ip->peer_id);
134+
ostrm << ", \"commitment_point\":" <<
128135
(ip->commitment_point ? dump_pubkey(ip->commitment_point) :
129-
"<none>");
136+
"\"<none>\"");
130137
ostrm << " }";
131138
return ostrm.str();
132139
}
@@ -135,15 +142,15 @@ string dump_utxo(const struct utxo *in)
135142
{
136143
ostringstream ostrm;
137144
ostrm << "{ ";
138-
ostrm << "txid=" << dump_bitcoin_txid(&in->txid);
139-
ostrm << ", outnum=" << in->outnum;
140-
ostrm << ", amount=" << in->amount.satoshis;
141-
ostrm << ", keyindex=" << in->keyindex;
142-
ostrm << ", is_p2sh=" << in->is_p2sh;
143-
ostrm << ", close_info=" <<
145+
ostrm << "\"txid\":" << dump_bitcoin_txid(&in->txid);
146+
ostrm << ", \"outnum\":" << in->outnum;
147+
ostrm << ", \"amount\":" << in->amount.satoshis;
148+
ostrm << ", \"keyindex\":" << in->keyindex;
149+
ostrm << ", \"is_p2sh\":" << in->is_p2sh;
150+
ostrm << ", \"close_info\":" <<
144151
(in->close_info ?
145152
dump_unilateral_close_info(in->close_info) :
146-
"<none>");
153+
"\"<none>\"");
147154
ostrm << " }";
148155
return ostrm.str();
149156
}
@@ -165,9 +172,9 @@ string dump_bitcoin_tx_output(const struct bitcoin_tx_output *op)
165172
{
166173
ostringstream ostrm;
167174
ostrm << "{ ";
168-
ostrm << "amount=" << op->amount.satoshis;
169-
ostrm << ", script=" <<
170-
(op->script ? dump_hex(op->script, tal_count(op->script)) : "<none>");
175+
ostrm << "\"amount\":" << op->amount.satoshis;
176+
ostrm << ", \"script\":" <<
177+
(op->script ? dump_hex(op->script, tal_count(op->script)) : "\"<none>\"");
171178
ostrm << " }";
172179
return ostrm.str();
173180
}
@@ -201,20 +208,109 @@ string dump_wally_tx_witness_stack(const struct wally_tx_witness_stack *sp)
201208
return ostrm.str();
202209
}
203210

211+
string dump_wally_keypath_item(const struct wally_keypath_item *ip)
212+
{
213+
ostringstream ostrm;
214+
ostrm << "{ ";
215+
ostrm << "\"pubkey\":" << dump_hex(
216+
ip->pubkey, pubkey_is_compressed(ip->pubkey) ?
217+
EC_PUBLIC_KEY_LEN : EC_PUBLIC_KEY_UNCOMPRESSED_LEN);
218+
ostrm << ", \"origin\":{ ";
219+
ostrm << " \"fingerprint\":" << dump_hex(ip->origin.fingerprint,
220+
sizeof(ip->origin.fingerprint));
221+
ostrm << ", \"path\":[ ";
222+
for (size_t ii = 0; ii < ip->origin.path_len; ++ii) {
223+
if (ii != 0)
224+
ostrm << ",";
225+
ostrm << ip->origin.path[ii];
226+
}
227+
ostrm << " ]";
228+
ostrm << " }";
229+
ostrm << " }";
230+
return ostrm.str();
231+
}
232+
233+
string dump_wally_keypath_map(const struct wally_keypath_map *mp)
234+
{
235+
ostringstream ostrm;
236+
ostrm << "[";
237+
if (mp) {
238+
for (size_t ii = 0; ii < mp->num_items; ii++) {
239+
if (ii != 0)
240+
ostrm << ",";
241+
ostrm << dump_wally_keypath_item(&mp->items[ii]);
242+
}
243+
}
244+
ostrm << "]";
245+
return ostrm.str();
246+
}
247+
248+
string dump_wally_partial_sigs_item(const struct wally_partial_sigs_item *ip)
249+
{
250+
ostringstream ostrm;
251+
ostrm << "{ ";
252+
ostrm << "\"pubkey\":" << dump_hex(
253+
ip->pubkey, pubkey_is_compressed(ip->pubkey) ?
254+
EC_PUBLIC_KEY_LEN : EC_PUBLIC_KEY_UNCOMPRESSED_LEN);
255+
ostrm << ", \"sig\":" << dump_hex(ip->sig, ip->sig_len);
256+
ostrm << " }";
257+
return ostrm.str();
258+
}
259+
260+
string dump_wally_partial_sigs_map(const struct wally_partial_sigs_map *mp)
261+
{
262+
ostringstream ostrm;
263+
ostrm << "[";
264+
if (mp) {
265+
for (size_t ii = 0; ii < mp->num_items; ii++) {
266+
if (ii != 0)
267+
ostrm << ",";
268+
ostrm << dump_wally_partial_sigs_item(&mp->items[ii]);
269+
}
270+
}
271+
ostrm << "]";
272+
return ostrm.str();
273+
}
274+
275+
string dump_wally_unknowns_item(const struct wally_unknowns_item *ip)
276+
{
277+
ostringstream ostrm;
278+
ostrm << "{ ";
279+
ostrm << "\"key\":" << dump_hex(ip->key, ip->key_len);
280+
ostrm << ", \"value\":" << dump_hex(ip->value, ip->value_len);
281+
ostrm << " }";
282+
return ostrm.str();
283+
}
284+
285+
string dump_wally_unknowns_map(const struct wally_unknowns_map *mp)
286+
{
287+
ostringstream ostrm;
288+
ostrm << "[";
289+
if (mp) {
290+
for (size_t ii = 0; ii < mp->num_items; ii++) {
291+
if (ii != 0)
292+
ostrm << ",";
293+
ostrm << dump_wally_unknowns_item(&mp->items[ii]);
294+
}
295+
}
296+
ostrm << "]";
297+
return ostrm.str();
298+
}
299+
204300
string dump_wally_tx_input(const struct wally_tx_input *in)
205301
{
206302
ostringstream ostrm;
207303
ostrm << "{ ";
208-
ostrm << "txhash=" << dump_hex(in->txhash, sizeof(in->txhash));
209-
ostrm << ", index=" << in->index;
210-
ostrm << ", sequence=" << in->sequence;
211-
ostrm << ", script=" <<
304+
ostrm << "\"txhash\":" << dump_hex(in->txhash, sizeof(in->txhash));
305+
ostrm << ", \"index\":" << in->index;
306+
ostrm << ", \"sequence\":" << in->sequence;
307+
ostrm << ", \"script\":" <<
212308
(in->script_len ? dump_hex(in->script, in->script_len) :
213-
"<none>");
214-
ostrm << ", witness=" <<
309+
"\"<none>\"");
310+
ostrm << ", \"witness\":" <<
215311
(in->witness ? dump_wally_tx_witness_stack(in->witness) :
216-
"<none>");
217-
ostrm << ", features=" << int(in->features);
312+
"\"<none>\"");
313+
ostrm << ", \"features\":" << int(in->features);
218314
ostrm << " }";
219315
return ostrm.str();
220316
}
@@ -239,11 +335,11 @@ string dump_wally_tx_output(const struct wally_tx_output *out)
239335
return "{}";
240336
ostringstream ostrm;
241337
ostrm << "{ ";
242-
ostrm << "satoshi=" << out->satoshi;
243-
ostrm << " script=" <<
338+
ostrm << "\"satoshi\":" << out->satoshi;
339+
ostrm << ", \"script\":" <<
244340
(out->script_len ? dump_hex(out->script, out->script_len) :
245-
"<none>");
246-
ostrm << ", features=" << int(out->features);
341+
"\"<none>\"");
342+
ostrm << ", \"features\":" << int(out->features);
247343
ostrm << " }";
248344
return ostrm.str();
249345
}
@@ -268,14 +364,14 @@ string dump_wally_tx(const struct wally_tx *wtx)
268364
return "{}";
269365
ostringstream ostrm;
270366
ostrm << "{ ";
271-
ostrm << "version=" << wtx->version;
272-
ostrm << ", locktime=" << wtx->locktime;
273-
ostrm << ", inputs=" <<
367+
ostrm << "\"version\":" << wtx->version;
368+
ostrm << ", \"locktime\":" << wtx->locktime;
369+
ostrm << ", \"inputs\":" <<
274370
dump_wally_tx_inputs(wtx->inputs, wtx->num_inputs);
275-
ostrm << ", inputs_allocation_len=" << wtx->inputs_allocation_len;
276-
ostrm << ", outputs=" <<
371+
ostrm << ", \"inputs_allocation_len\":" << wtx->inputs_allocation_len;
372+
ostrm << ", \"outputs\":" <<
277373
dump_wally_tx_outputs(wtx->outputs, wtx->num_outputs);
278-
ostrm << ", outputs_allocation_len=" << wtx->outputs_allocation_len;
374+
ostrm << ", \"outputs_allocation_len\":" << wtx->outputs_allocation_len;
279375
ostrm << " }";
280376
return ostrm.str();
281377
}
@@ -284,18 +380,21 @@ string dump_wally_psbt_input(const struct wally_psbt_input *in)
284380
{
285381
ostringstream ostrm;
286382
ostrm << "{ ";
287-
ostrm << "non_witness_utxo=" << dump_wally_tx(in->non_witness_utxo);
288-
ostrm << ", witness_utxo=" << dump_wally_tx_output(in->witness_utxo);
289-
ostrm << ", redeem_script=" << dump_hex(in->redeem_script,
290-
in->redeem_script_len);
291-
ostrm << ", witness_script=" << dump_hex(in->witness_script,
292-
in->witness_script_len);
293-
ostrm << ", final_script_sig=" << dump_hex(in->final_script_sig,
294-
in->final_script_sig_len);
295-
ostrm << ", final_witness="
383+
ostrm << "\"non_witness_utxo\":" << dump_wally_tx(in->non_witness_utxo);
384+
ostrm << ", \"witness_utxo\":" << dump_wally_tx_output(in->witness_utxo);
385+
ostrm << ", \"redeem_script\":" << dump_hex(in->redeem_script,
386+
in->redeem_script_len);
387+
ostrm << ", \"witness_script\":" << dump_hex(in->witness_script,
388+
in->witness_script_len);
389+
ostrm << ", \"final_script_sig\":" << dump_hex(in->final_script_sig,
390+
in->final_script_sig_len);
391+
ostrm << ", \"final_witness\":"
296392
<< dump_wally_tx_witness_stack(in->final_witness);
297-
ostrm << ", keypaths=??, partial_sigs==??, unknown=??";
298-
ostrm << ", sighash_type=" << in->sighash_type;
393+
ostrm << ", \"keypaths\":" << dump_wally_keypath_map(in->keypaths);
394+
ostrm << ", \"partial_sigs\":"
395+
<< dump_wally_partial_sigs_map(in->partial_sigs);
396+
ostrm << ", \"unknowns\":" << dump_wally_unknowns_map(in->unknowns);
397+
ostrm << ", \"sighash_type\":" << in->sighash_type;
299398
ostrm << " }";
300399
return ostrm.str();
301400
}
@@ -318,11 +417,12 @@ string dump_wally_psbt_output(const struct wally_psbt_output *out)
318417
{
319418
ostringstream ostrm;
320419
ostrm << "{ ";
321-
ostrm << "redeem_script=" << dump_hex(out->redeem_script,
322-
out->redeem_script_len);
323-
ostrm << ", witness_script=" << dump_hex(out->witness_script,
324-
out->witness_script_len);
325-
ostrm << ", keypaths=??, unknown=??";
420+
ostrm << "\"redeem_script\":" << dump_hex(out->redeem_script,
421+
out->redeem_script_len);
422+
ostrm << ", \"witness_script\":" << dump_hex(out->witness_script,
423+
out->witness_script_len);
424+
ostrm << ", \"keypaths\":" << dump_wally_keypath_map(out->keypaths);
425+
ostrm << ", \"unknowns\":" << dump_wally_unknowns_map(out->unknowns);
326426
ostrm << " }";
327427
return ostrm.str();
328428

@@ -346,11 +446,14 @@ string dump_wally_psbt(const struct wally_psbt *psbt)
346446
{
347447
ostringstream ostrm;
348448
ostrm << "{ ";
349-
ostrm << "tx=" << dump_wally_tx(psbt->tx);
350-
ostrm << ", inputs="
449+
ostrm << "\"magic\":" << dump_hex(psbt->magic, sizeof(psbt->magic));
450+
ostrm << ", \"tx\":" << dump_wally_tx(psbt->tx);
451+
ostrm << ", \"inputs\":"
351452
<< dump_wally_psbt_inputs(psbt->inputs, psbt->num_inputs);
352-
ostrm << ", outputs="
453+
ostrm << ", \"outputs\":"
353454
<< dump_wally_psbt_outputs(psbt->outputs, psbt->num_outputs);
455+
ostrm << ", \"unknowns\":" << dump_wally_unknowns_map(psbt->unknowns);
456+
ostrm << ", \"version\":" << psbt->version;
354457
ostrm << " }";
355458
return ostrm.str();
356459
}
@@ -359,8 +462,8 @@ string dump_tx(const struct bitcoin_tx *tx)
359462
{
360463
ostringstream ostrm;
361464
ostrm << "{ ";
362-
ostrm << ", wtx=" << dump_wally_tx(tx->wtx);
363-
ostrm << ", psbt=" << dump_wally_psbt(tx->psbt);
465+
ostrm << "\"wtx\":" << dump_wally_tx(tx->wtx);
466+
ostrm << ", \"psbt\":" << dump_wally_psbt(tx->psbt);
364467
ostrm << " }";
365468
return ostrm.str();
366469
}

contrib/remote_hsmd/dump.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ std::string dump_utxos(const struct utxo **utxos);
2424
std::string dump_bitcoin_tx_output(const struct bitcoin_tx_output *op);
2525
std::string dump_bitcoin_tx_outputs(const struct bitcoin_tx_output **outputs);
2626
std::string dump_wally_tx_witness_stack(const struct wally_tx_witness_stack *sp);
27+
std::string dump_wally_keypath_map(const struct wally_keypath_map *mp);
28+
std::string dump_wally_partial_sigs_map(const struct wally_partial_sigs_map *mp);
29+
std::string dump_wally_unknowns_map(const struct wally_unknowns_map *mp);
2730
std::string dump_wally_tx_input(const struct wally_tx_input *in);
2831
std::string dump_wally_tx_inputs(const struct wally_tx_input *inputs,
2932
size_t num_inputs);

0 commit comments

Comments
 (0)