Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txprepare fixes #3384

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions common/withdraw_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,37 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
int *change_outnum)
{
struct bitcoin_tx *tx;
int output_count;

tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
!amount_sat_eq(change, AMOUNT_SAT(0)),
tal_count(outputs));

bitcoin_tx_add_multi_outputs(tx, outputs);
output_count = bitcoin_tx_add_multi_outputs(tx, outputs);
assert(output_count == tal_count(outputs));

if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
const void *map[2];
map[0] = int2ptr(0);
map[1] = int2ptr(1);
/* Add one to the output_count, for the change */
output_count++;

const void *map[output_count];
for (size_t i = 0; i < output_count; i++)
map[i] = int2ptr(i);

bitcoin_tx_add_output(tx, scriptpubkey_p2wpkh(tmpctx, changekey),
change);

assert(tx->wtx->num_outputs == output_count);
permute_outputs(tx, NULL, map);

/* The change is the last output added, so the last position
* in the map */
if (change_outnum)
*change_outnum = ptr2int(map[1]);
*change_outnum = ptr2int(map[output_count - 1]);

} else if (change_outnum)
*change_outnum = -1;

permute_inputs(tx, (const void **)utxos);
elements_tx_add_fee_output(tx);
assert(bitcoin_tx_check(tx));
Expand Down
2 changes: 1 addition & 1 deletion contrib/pyln-client/pyln/client/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def txprepare(self, *args, **kwargs):
if 'destination' in kwargs or 'satoshi' in kwargs:
return self._deprecated_txprepare(*args, **kwargs)

if not isinstance(args[0], list):
if len(args) and not isinstance(args[0], list):
return self._deprecated_txprepare(*args, **kwargs)

def _txprepare(outputs, feerate=None, minconf=None, utxos=None):
Expand Down
17 changes: 16 additions & 1 deletion tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,21 @@ def test_deprecated_txprepare(node_factory, bitcoind):
l1.rpc.call('txprepare', {'destination': addr, 'satoshi': Millisatoshi(amount * 100)})


def test_txprepare_multi(node_factory, bitcoind):
amount = 10000000
l1 = node_factory.get_node(random_hsm=True)

bitcoind.rpc.sendtoaddress(l1.rpc.newaddr()['bech32'], amount / 10**8)
bitcoind.generate_block(1)
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)

outputs = []
for i in range(9):
outputs.append({l1.rpc.newaddr()['bech32']: Millisatoshi(amount * 100)})
prep = l1.rpc.txprepare(outputs=outputs)
l1.rpc.txdiscard(prep['txid'])


def test_txprepare(node_factory, bitcoind, chainparams):
amount = 1000000
l1 = node_factory.get_node(random_hsm=True)
Expand All @@ -277,7 +292,7 @@ def test_txprepare(node_factory, bitcoind, chainparams):
bitcoind.generate_block(1)
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10)

prep = l1.rpc.txprepare([{addr: Millisatoshi(amount * 3 * 1000)}])
prep = l1.rpc.txprepare(outputs=[{addr: Millisatoshi(amount * 3 * 1000)}])
decode = bitcoind.rpc.decoderawtransaction(prep['unsigned_tx'])
assert decode['txid'] == prep['txid']
# 4 inputs, 2 outputs (3 if we have a fee output).
Expand Down