mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
common: withdraw_tx()
now use the array of struct bitcoin_tx_output
as parameter
This commit is contained in:
parent
6570c743c4
commit
b660531216
7 changed files with 36 additions and 16 deletions
|
@ -26,7 +26,7 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx,
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
bool has_change = !amount_sat_eq(change, AMOUNT_SAT(0));
|
bool has_change = !amount_sat_eq(change, AMOUNT_SAT(0));
|
||||||
|
|
||||||
tx = tx_spending_utxos(ctx, chainparams, utxomap, bip32_base, has_change);
|
tx = tx_spending_utxos(ctx, chainparams, utxomap, bip32_base, has_change, 1);
|
||||||
|
|
||||||
|
|
||||||
wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey);
|
wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <assert.h>
|
||||||
#include <bitcoin/script.h>
|
#include <bitcoin/script.h>
|
||||||
#include <common/key_derive.h>
|
#include <common/key_derive.h>
|
||||||
#include <common/utils.h>
|
#include <common/utils.h>
|
||||||
|
@ -52,11 +53,14 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
|
||||||
const struct chainparams *chainparams,
|
const struct chainparams *chainparams,
|
||||||
const struct utxo **utxos,
|
const struct utxo **utxos,
|
||||||
const struct ext_key *bip32_base,
|
const struct ext_key *bip32_base,
|
||||||
bool add_change_output)
|
bool add_change_output,
|
||||||
|
size_t num_output)
|
||||||
{
|
{
|
||||||
struct pubkey key;
|
struct pubkey key;
|
||||||
u8 *script;
|
u8 *script;
|
||||||
size_t outcount = add_change_output ? 2 : 1;
|
|
||||||
|
assert(num_output);
|
||||||
|
size_t outcount = add_change_output ? 1 + num_output : num_output;
|
||||||
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, tal_count(utxos), outcount);
|
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, tal_count(utxos), outcount);
|
||||||
|
|
||||||
for (size_t i = 0; i < tal_count(utxos); i++) {
|
for (size_t i = 0; i < tal_count(utxos); i++) {
|
||||||
|
|
|
@ -50,6 +50,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
|
||||||
const struct chainparams *chainparams,
|
const struct chainparams *chainparams,
|
||||||
const struct utxo **utxos,
|
const struct utxo **utxos,
|
||||||
const struct ext_key *bip32_base,
|
const struct ext_key *bip32_base,
|
||||||
bool add_change_output);
|
bool add_change_output,
|
||||||
|
size_t num_output);
|
||||||
|
|
||||||
#endif /* LIGHTNING_COMMON_UTXO_H */
|
#endif /* LIGHTNING_COMMON_UTXO_H */
|
||||||
|
|
|
@ -11,8 +11,7 @@
|
||||||
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
||||||
const struct chainparams *chainparams,
|
const struct chainparams *chainparams,
|
||||||
const struct utxo **utxos,
|
const struct utxo **utxos,
|
||||||
const u8 *destination,
|
struct bitcoin_tx_output **outputs,
|
||||||
struct amount_sat withdraw_amount,
|
|
||||||
const struct pubkey *changekey,
|
const struct pubkey *changekey,
|
||||||
struct amount_sat change,
|
struct amount_sat change,
|
||||||
const struct ext_key *bip32_base,
|
const struct ext_key *bip32_base,
|
||||||
|
@ -21,9 +20,10 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
|
|
||||||
tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
|
tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
|
||||||
!amount_sat_eq(change, AMOUNT_SAT(0)));
|
!amount_sat_eq(change, AMOUNT_SAT(0)),
|
||||||
|
tal_count(outputs));
|
||||||
|
|
||||||
bitcoin_tx_add_output(tx, destination, withdraw_amount);
|
bitcoin_tx_add_multi_outputs(tx, outputs);
|
||||||
|
|
||||||
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
|
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
|
||||||
const void *map[2];
|
const void *map[2];
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define LIGHTNING_COMMON_WITHDRAW_TX_H
|
#define LIGHTNING_COMMON_WITHDRAW_TX_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <bitcoin/chainparams.h>
|
#include <bitcoin/chainparams.h>
|
||||||
|
#include <bitcoin/tx.h>
|
||||||
#include <ccan/short_types/short_types.h>
|
#include <ccan/short_types/short_types.h>
|
||||||
#include <ccan/tal/tal.h>
|
#include <ccan/tal/tal.h>
|
||||||
#include <common/amount.h>
|
#include <common/amount.h>
|
||||||
|
@ -19,8 +20,7 @@ struct utxo;
|
||||||
* @ctx: context to tal from.
|
* @ctx: context to tal from.
|
||||||
* @chainparams: (in) the params for the created transaction.
|
* @chainparams: (in) the params for the created transaction.
|
||||||
* @utxos: (in/out) tal_arr of UTXO pointers to spend (permuted to match)
|
* @utxos: (in/out) tal_arr of UTXO pointers to spend (permuted to match)
|
||||||
* @destination: (in) tal_arr of u8, scriptPubKey to send to.
|
* @outputs: (in) tal_arr of bitcoin_tx_output, scriptPubKeys with amount to send to.
|
||||||
* @amount: (in) satoshis to send to the destination
|
|
||||||
* @changekey: (in) key to send change to (only used if change_satoshis != 0).
|
* @changekey: (in) key to send change to (only used if change_satoshis != 0).
|
||||||
* @change: (in) amount to send as change.
|
* @change: (in) amount to send as change.
|
||||||
* @bip32_base: (in) bip32 base for key derivation, or NULL.
|
* @bip32_base: (in) bip32 base for key derivation, or NULL.
|
||||||
|
@ -29,8 +29,7 @@ struct utxo;
|
||||||
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
||||||
const struct chainparams *chainparams,
|
const struct chainparams *chainparams,
|
||||||
const struct utxo **utxos,
|
const struct utxo **utxos,
|
||||||
const u8 *destination,
|
struct bitcoin_tx_output **outputs,
|
||||||
struct amount_sat withdraw_amount,
|
|
||||||
const struct pubkey *changekey,
|
const struct pubkey *changekey,
|
||||||
struct amount_sat change,
|
struct amount_sat change,
|
||||||
const struct ext_key *bip32_base,
|
const struct ext_key *bip32_base,
|
||||||
|
|
11
hsmd/hsmd.c
11
hsmd/hsmd.c
|
@ -1515,7 +1515,9 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
struct pubkey changekey;
|
struct pubkey changekey;
|
||||||
u8 *scriptpubkey;
|
u8 *scriptpubkey;
|
||||||
|
struct bitcoin_tx_output **outputs;
|
||||||
|
|
||||||
|
outputs = tal_arr(tmpctx, struct bitcoin_tx_output *, 0);
|
||||||
if (!fromwire_hsm_sign_withdrawal(tmpctx, msg_in, &satoshi_out,
|
if (!fromwire_hsm_sign_withdrawal(tmpctx, msg_in, &satoshi_out,
|
||||||
&change_out, &change_keyindex,
|
&change_out, &change_keyindex,
|
||||||
&scriptpubkey, &utxos))
|
&scriptpubkey, &utxos))
|
||||||
|
@ -1525,9 +1527,14 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
|
||||||
return bad_req_fmt(conn, c, msg_in,
|
return bad_req_fmt(conn, c, msg_in,
|
||||||
"Failed to get key %u", change_keyindex);
|
"Failed to get key %u", change_keyindex);
|
||||||
|
|
||||||
|
struct bitcoin_tx_output *output = tal(outputs,
|
||||||
|
struct bitcoin_tx_output);
|
||||||
|
output->script = tal_steal(output, scriptpubkey);
|
||||||
|
output->amount = satoshi_out;
|
||||||
|
tal_arr_expand(&outputs, output);
|
||||||
tx = withdraw_tx(tmpctx, c->chainparams,
|
tx = withdraw_tx(tmpctx, c->chainparams,
|
||||||
cast_const2(const struct utxo **, utxos), scriptpubkey,
|
cast_const2(const struct utxo **, utxos), outputs,
|
||||||
satoshi_out, &changekey, change_out, NULL, NULL);
|
&changekey, change_out, NULL, NULL);
|
||||||
|
|
||||||
sign_all_inputs(tx, utxos);
|
sign_all_inputs(tx, utxos);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include <bitcoin/address.h>
|
#include <bitcoin/address.h>
|
||||||
#include <bitcoin/base58.h>
|
#include <bitcoin/base58.h>
|
||||||
#include <bitcoin/script.h>
|
#include <bitcoin/script.h>
|
||||||
|
#include <ccan/cast/cast.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
#include <common/addr.h>
|
#include <common/addr.h>
|
||||||
#include <common/bech32.h>
|
#include <common/bech32.h>
|
||||||
|
@ -163,10 +164,12 @@ static struct command_result *json_prepare_tx(struct command *cmd,
|
||||||
struct command_result *res;
|
struct command_result *res;
|
||||||
u32 *minconf, maxheight;
|
u32 *minconf, maxheight;
|
||||||
struct pubkey *changekey;
|
struct pubkey *changekey;
|
||||||
|
struct bitcoin_tx_output **outputs;
|
||||||
|
|
||||||
*utx = tal(cmd, struct unreleased_tx);
|
*utx = tal(cmd, struct unreleased_tx);
|
||||||
(*utx)->wtx = tal(*utx, struct wallet_tx);
|
(*utx)->wtx = tal(*utx, struct wallet_tx);
|
||||||
wtx_init(cmd, (*utx)->wtx, AMOUNT_SAT(-1ULL));
|
wtx_init(cmd, (*utx)->wtx, AMOUNT_SAT(-1ULL));
|
||||||
|
outputs = tal_arr(tmpctx, struct bitcoin_tx_output *, 0);
|
||||||
|
|
||||||
if (!param(cmd, buffer, params,
|
if (!param(cmd, buffer, params,
|
||||||
p_req("destination", param_bitcoin_address,
|
p_req("destination", param_bitcoin_address,
|
||||||
|
@ -201,8 +204,14 @@ static struct command_result *json_prepare_tx(struct command *cmd,
|
||||||
} else
|
} else
|
||||||
changekey = NULL;
|
changekey = NULL;
|
||||||
|
|
||||||
(*utx)->tx = withdraw_tx(*utx, get_chainparams(cmd->ld), (*utx)->wtx->utxos,
|
struct bitcoin_tx_output *output = tal(outputs,
|
||||||
(*utx)->destination, (*utx)->wtx->amount,
|
struct bitcoin_tx_output);
|
||||||
|
output->script = tal_dup_arr(output, u8, (*utx)->destination,
|
||||||
|
tal_count((*utx)->destination), 0);
|
||||||
|
output->amount = (*utx)->wtx->amount;
|
||||||
|
tal_arr_expand(&outputs, output);
|
||||||
|
(*utx)->tx = withdraw_tx(*utx, get_chainparams(cmd->ld),
|
||||||
|
(*utx)->wtx->utxos, outputs,
|
||||||
changekey, (*utx)->wtx->change,
|
changekey, (*utx)->wtx->change,
|
||||||
cmd->ld->wallet->bip32_base,
|
cmd->ld->wallet->bip32_base,
|
||||||
&(*utx)->change_outnum);
|
&(*utx)->change_outnum);
|
||||||
|
|
Loading…
Add table
Reference in a new issue