Skip to content

Commit

Permalink
common/utxo: make commitment_point optional in close_info.
Browse files Browse the repository at this point in the history
We don't rotate key for option_static_remotekey, so we don't need
this point for such channels.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell authored and niftynei committed Sep 10, 2019
1 parent 87f0ee6 commit 160f270
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
12 changes: 10 additions & 2 deletions common/utxo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo)
if (is_unilateral_close) {
towire_u64(pptr, utxo->close_info->channel_id);
towire_node_id(pptr, &utxo->close_info->peer_id);
towire_pubkey(pptr, &utxo->close_info->commitment_point);
towire_bool(pptr, utxo->close_info->commitment_point != NULL);
if (utxo->close_info->commitment_point)
towire_pubkey(pptr, utxo->close_info->commitment_point);
}
}

Expand All @@ -42,7 +44,13 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
utxo->close_info = tal(utxo, struct unilateral_close_info);
utxo->close_info->channel_id = fromwire_u64(ptr, max);
fromwire_node_id(ptr, max, &utxo->close_info->peer_id);
fromwire_pubkey(ptr, max, &utxo->close_info->commitment_point);
if (fromwire_bool(ptr, max)) {
utxo->close_info->commitment_point = tal(utxo,
struct pubkey);
fromwire_pubkey(ptr, max,
utxo->close_info->commitment_point);
} else
utxo->close_info->commitment_point = NULL;
} else {
utxo->close_info = NULL;
}
Expand Down
3 changes: 2 additions & 1 deletion common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct ext_key;
struct unilateral_close_info {
u64 channel_id;
struct node_id peer_id;
struct pubkey commitment_point;
/* NULL if this is an option_static_remotekey commitment */
struct pubkey *commitment_point;
};

struct utxo {
Expand Down
2 changes: 1 addition & 1 deletion hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ static void hsm_unilateral_close_privkey(struct privkey *dst,
derive_basepoints(&channel_seed, NULL, &basepoints, &secrets, NULL);

if (!derive_simple_privkey(&secrets.payment_basepoint_secret,
&basepoints.payment, &info->commitment_point,
&basepoints.payment, info->commitment_point,
dst)) {
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Deriving unilateral_close_privkey");
Expand Down
2 changes: 1 addition & 1 deletion onchaind/onchain_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ msgtype,onchain_all_irrevocably_resolved,5011
msgtype,onchain_add_utxo,5012
msgdata,onchain_add_utxo,prev_out_tx,bitcoin_txid,
msgdata,onchain_add_utxo,prev_out_index,u32,
msgdata,onchain_add_utxo,per_commit_point,pubkey,
msgdata,onchain_add_utxo,per_commit_point,?pubkey,
msgdata,onchain_add_utxo,value,amount_sat,
msgdata,onchain_add_utxo,blockheight,u32,
msgdata,onchain_add_utxo,len,u16,
Expand Down
27 changes: 25 additions & 2 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
u.close_info = tal(w, struct unilateral_close_info);
u.close_info->channel_id = 42;
u.close_info->peer_id = id;
u.close_info->commitment_point = pk;
u.close_info->commitment_point = &pk;
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh),
"wallet_add_utxo with close_info");

Expand All @@ -796,7 +796,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)

u = *utxos[1];
CHECK(u.close_info->channel_id == 42 &&
pubkey_eq(&u.close_info->commitment_point, &pk) &&
pubkey_eq(u.close_info->commitment_point, &pk) &&
node_id_eq(&u.close_info->peer_id, &id));
/* Now un-reserve them for the tests below */
tal_free(utxos);
Expand Down Expand Up @@ -826,6 +826,29 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
output_state_spent),
"could not change output state ignoring oldstate");

/* Attempt to save an UTXO with close_info set, no commitment_point */
memset(&u.txid, 2, sizeof(u.txid));
u.amount = AMOUNT_SAT(5);
u.close_info = tal(w, struct unilateral_close_info);
u.close_info->channel_id = 42;
u.close_info->peer_id = id;
u.close_info->commitment_point = NULL;
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh),
"wallet_add_utxo with close_info no commitment_point");

/* Now select it */
utxos = wallet_select_coins(w, w, AMOUNT_SAT(5), 0, 21,
0 /* no confirmations required */,
&fee_estimate, &change_satoshis);
CHECK(utxos && tal_count(utxos) == 2);

u = *utxos[1];
CHECK(u.close_info->channel_id == 42 &&
u.close_info->commitment_point == NULL &&
node_id_eq(&u.close_info->peer_id, &id));
/* Now un-reserve them */
tal_free(utxos);

db_commit_transaction(w->db);
return true;
}
Expand Down
13 changes: 11 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
if (utxo->close_info) {
db_bind_u64(stmt, 6, utxo->close_info->channel_id);
db_bind_node_id(stmt, 7, &utxo->close_info->peer_id);
db_bind_pubkey(stmt, 8, &utxo->close_info->commitment_point);
if (utxo->close_info->commitment_point)
db_bind_pubkey(stmt, 8, utxo->close_info->commitment_point);
else
db_bind_null(stmt, 8);
} else {
db_bind_null(stmt, 6);
db_bind_null(stmt, 7);
Expand Down Expand Up @@ -155,7 +158,13 @@ static struct utxo *wallet_stmt2output(const tal_t *ctx, struct db_stmt *stmt)
utxo->close_info = tal(utxo, struct unilateral_close_info);
utxo->close_info->channel_id = db_column_u64(stmt, 6);
db_column_node_id(stmt, 7, &utxo->close_info->peer_id);
db_column_pubkey(stmt, 8, &utxo->close_info->commitment_point);
if (!db_column_is_null(stmt, 8)) {
utxo->close_info->commitment_point
= tal(utxo->close_info, struct pubkey);
db_column_pubkey(stmt, 8,
utxo->close_info->commitment_point);
} else
utxo->close_info->commitment_point = NULL;
} else {
utxo->close_info = NULL;
}
Expand Down

0 comments on commit 160f270

Please sign in to comment.