From 9463e1b63035b0ca3142f656ff42c297ecfbc20f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 9 Nov 2016 08:04:28 +1030 Subject: [PATCH] wallet: use pubkey as API, not pointer. Much easier to save/restore to/from database in coming patch. Signed-off-by: Rusty Russell --- daemon/peer.c | 11 +++++------ daemon/peer.h | 2 +- daemon/wallet.c | 36 ++++++++++++++++++++++++++++-------- daemon/wallet.h | 10 +++++----- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 993407bfe..1bfe73ac0 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -542,7 +542,8 @@ static void bitcoin_create_anchor(struct peer *peer) tx->input[0].amount = tal_dup(tx->input, u64, &peer->anchor.input->amount); - wallet_add_signed_input(peer->dstate, peer->anchor.input->w, tx, 0); + wallet_add_signed_input(peer->dstate, &peer->anchor.input->walletkey, + tx, 0); bitcoin_txid(tx, &peer->anchor.txid); peer->anchor.tx = tx; @@ -3150,14 +3151,12 @@ static void json_connect(struct command *cmd, bitcoin_txid(tx, &connect->input->txid); /* Find an output we know how to spend. */ - connect->input->w = NULL; for (output = 0; output < tx->output_count; output++) { - connect->input->w - = wallet_can_spend(cmd->dstate, &tx->output[output]); - if (connect->input->w) + if (wallet_can_spend(cmd->dstate, &tx->output[output], + &connect->input->walletkey)) break; } - if (!connect->input->w) { + if (output == tx->output_count) { command_fail(cmd, "Tx doesn't send to wallet address"); return; } diff --git a/daemon/peer.h b/daemon/peer.h index 9c68f1f23..7aa5219bc 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -25,7 +25,7 @@ struct anchor_input { /* Amount of input (satoshis) */ u64 amount; /* Wallet entry to use to spend. */ - struct wallet *w; + struct pubkey walletkey; }; /* Information we remember for their commitment txs which we signed. diff --git a/daemon/wallet.c b/daemon/wallet.c index 0f7269748..650cda713 100644 --- a/daemon/wallet.c +++ b/daemon/wallet.c @@ -50,15 +50,31 @@ static void new_keypair(struct lightningd_state *dstate, } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey)); } -void wallet_add_signed_input(struct lightningd_state *dstate, - const struct wallet *w, +static struct wallet *find_by_pubkey(struct lightningd_state *dstate, + const struct pubkey *walletkey) +{ + struct wallet *w; + + list_for_each(&dstate->wallet, w, list) { + if (pubkey_eq(walletkey, &w->pubkey)) + return w; + } + return NULL; +} + +bool wallet_add_signed_input(struct lightningd_state *dstate, + const struct pubkey *walletkey, struct bitcoin_tx *tx, unsigned int input_num) { u8 *redeemscript; struct bitcoin_signature sig; - assert(input_num < tx->input_count); + struct wallet *w = find_by_pubkey(dstate, walletkey); + assert(input_num < tx->input_count); + if (!w) + return false; + redeemscript = bitcoin_redeem_p2wpkh(tx, dstate->secpctx, &w->pubkey); sig.stype = SIGHASH_ALL; @@ -75,10 +91,12 @@ void wallet_add_signed_input(struct lightningd_state *dstate, &sig, &w->pubkey); tal_free(redeemscript); + return true; } -struct wallet *wallet_can_spend(struct lightningd_state *dstate, - const struct bitcoin_tx_output *output) +bool wallet_can_spend(struct lightningd_state *dstate, + const struct bitcoin_tx_output *output, + struct pubkey *walletkey) { struct ripemd160 h; struct wallet *w; @@ -88,10 +106,12 @@ struct wallet *wallet_can_spend(struct lightningd_state *dstate, memcpy(&h, output->script + 2, 20); list_for_each(&dstate->wallet, w, list) { - if (structeq(&h, &w->p2sh)) - return w; + if (structeq(&h, &w->p2sh)) { + *walletkey = w->pubkey; + return true; + } } - return NULL; + return false; } static void json_newaddr(struct command *cmd, diff --git a/daemon/wallet.h b/daemon/wallet.h index ebcd59f81..753542d1c 100644 --- a/daemon/wallet.h +++ b/daemon/wallet.h @@ -2,7 +2,6 @@ #define LIGHTNING_DAEMON_WALLET_H #include "config.h" -struct wallet; struct lightningd_state; struct bitcoin_tx; struct bitcoin_tx_output; @@ -10,12 +9,13 @@ struct bitcoin_tx_output; bool restore_wallet_address(struct lightningd_state *dstate, const struct privkey *privkey); -void wallet_add_signed_input(struct lightningd_state *dstate, - const struct wallet *w, +bool wallet_add_signed_input(struct lightningd_state *dstate, + const struct pubkey *walletkey, struct bitcoin_tx *tx, unsigned int input_num); -struct wallet *wallet_can_spend(struct lightningd_state *dstate, - const struct bitcoin_tx_output *output); +bool wallet_can_spend(struct lightningd_state *dstate, + const struct bitcoin_tx_output *output, + struct pubkey *walletkey); #endif /* LIGHTNING_DAEMON_WALLET_H */