wallet: use pubkey as API, not pointer.

Much easier to save/restore to/from database in coming patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-11-09 08:04:28 +10:30
parent 7f0a56f674
commit 9463e1b630
4 changed files with 39 additions and 20 deletions

View File

@ -542,7 +542,8 @@ static void bitcoin_create_anchor(struct peer *peer)
tx->input[0].amount = tal_dup(tx->input, u64, tx->input[0].amount = tal_dup(tx->input, u64,
&peer->anchor.input->amount); &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); bitcoin_txid(tx, &peer->anchor.txid);
peer->anchor.tx = tx; peer->anchor.tx = tx;
@ -3150,14 +3151,12 @@ static void json_connect(struct command *cmd,
bitcoin_txid(tx, &connect->input->txid); bitcoin_txid(tx, &connect->input->txid);
/* Find an output we know how to spend. */ /* Find an output we know how to spend. */
connect->input->w = NULL;
for (output = 0; output < tx->output_count; output++) { for (output = 0; output < tx->output_count; output++) {
connect->input->w if (wallet_can_spend(cmd->dstate, &tx->output[output],
= wallet_can_spend(cmd->dstate, &tx->output[output]); &connect->input->walletkey))
if (connect->input->w)
break; break;
} }
if (!connect->input->w) { if (output == tx->output_count) {
command_fail(cmd, "Tx doesn't send to wallet address"); command_fail(cmd, "Tx doesn't send to wallet address");
return; return;
} }

View File

@ -25,7 +25,7 @@ struct anchor_input {
/* Amount of input (satoshis) */ /* Amount of input (satoshis) */
u64 amount; u64 amount;
/* Wallet entry to use to spend. */ /* Wallet entry to use to spend. */
struct wallet *w; struct pubkey walletkey;
}; };
/* Information we remember for their commitment txs which we signed. /* Information we remember for their commitment txs which we signed.

View File

@ -50,15 +50,31 @@ static void new_keypair(struct lightningd_state *dstate,
} while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey)); } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey));
} }
void wallet_add_signed_input(struct lightningd_state *dstate, static struct wallet *find_by_pubkey(struct lightningd_state *dstate,
const struct wallet *w, 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, struct bitcoin_tx *tx,
unsigned int input_num) unsigned int input_num)
{ {
u8 *redeemscript; u8 *redeemscript;
struct bitcoin_signature sig; 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); redeemscript = bitcoin_redeem_p2wpkh(tx, dstate->secpctx, &w->pubkey);
sig.stype = SIGHASH_ALL; sig.stype = SIGHASH_ALL;
@ -75,10 +91,12 @@ void wallet_add_signed_input(struct lightningd_state *dstate,
&sig, &sig,
&w->pubkey); &w->pubkey);
tal_free(redeemscript); tal_free(redeemscript);
return true;
} }
struct wallet *wallet_can_spend(struct lightningd_state *dstate, bool wallet_can_spend(struct lightningd_state *dstate,
const struct bitcoin_tx_output *output) const struct bitcoin_tx_output *output,
struct pubkey *walletkey)
{ {
struct ripemd160 h; struct ripemd160 h;
struct wallet *w; struct wallet *w;
@ -88,10 +106,12 @@ struct wallet *wallet_can_spend(struct lightningd_state *dstate,
memcpy(&h, output->script + 2, 20); memcpy(&h, output->script + 2, 20);
list_for_each(&dstate->wallet, w, list) { list_for_each(&dstate->wallet, w, list) {
if (structeq(&h, &w->p2sh)) if (structeq(&h, &w->p2sh)) {
return w; *walletkey = w->pubkey;
return true;
}
} }
return NULL; return false;
} }
static void json_newaddr(struct command *cmd, static void json_newaddr(struct command *cmd,

View File

@ -2,7 +2,6 @@
#define LIGHTNING_DAEMON_WALLET_H #define LIGHTNING_DAEMON_WALLET_H
#include "config.h" #include "config.h"
struct wallet;
struct lightningd_state; struct lightningd_state;
struct bitcoin_tx; struct bitcoin_tx;
struct bitcoin_tx_output; struct bitcoin_tx_output;
@ -10,12 +9,13 @@ struct bitcoin_tx_output;
bool restore_wallet_address(struct lightningd_state *dstate, bool restore_wallet_address(struct lightningd_state *dstate,
const struct privkey *privkey); const struct privkey *privkey);
void wallet_add_signed_input(struct lightningd_state *dstate, bool wallet_add_signed_input(struct lightningd_state *dstate,
const struct wallet *w, const struct pubkey *walletkey,
struct bitcoin_tx *tx, struct bitcoin_tx *tx,
unsigned int input_num); unsigned int input_num);
struct wallet *wallet_can_spend(struct lightningd_state *dstate, bool wallet_can_spend(struct lightningd_state *dstate,
const struct bitcoin_tx_output *output); const struct bitcoin_tx_output *output,
struct pubkey *walletkey);
#endif /* LIGHTNING_DAEMON_WALLET_H */ #endif /* LIGHTNING_DAEMON_WALLET_H */