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,
&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;
}

View File

@ -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.

View File

@ -50,14 +50,30 @@ 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;
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);
@ -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,

View File

@ -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 */