Commit Graph

544 Commits

Author SHA1 Message Date
Rusty Russell 06a54606a3 check-includes: allow redundant "config.h"
We should actually be including this (as it may define _GNU_SOURCE
etc) before any system headers.  But where we include <assert.h> we
often didn't, because check-includes would complain that the headers
included it too.

Weaken that check, and include config.h in C files before assert.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-02-04 12:02:36 +10:30
Rusty Russell 84dc943cf5 common/bolt11_json: extract bolt11->json code.
Our new "decode" command will also handle bolt11.  We make a few cleanups:

1. Avoid type_to_string() in JSON, instead use format functions directly.
2. Don't need to escape description now that JSON core does that for us.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-01-09 14:34:35 +10:30
Rusty Russell f06ad0471c common/bolt12_merkle.h: experimental bolt 12 implementation.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-12-09 16:40:13 +10:30
Rusty Russell 9d656464f5 bitcoin/pubkey: add pubkey32 primitive for xonly pubkey types.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-12-09 16:40:13 +10:30
Rusty Russell 6f205896aa bitcoin/tx: fix type of outpoint (n is a u32), simplify json_to_outpoint
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-12-09 06:56:21 +10:30
Christian Decker 32000b6660 json: Add two param functions to parse string arrs and outpoint arrs
In a couple of places we accept arrays of strings and don't validate
them. If we forward them, e.g., call a JSON-RPC method from the
plugin, we end up embedding the unverified string in the JSON-RPC
call without escaping, which then leads to invalid JSON being passed
on.

This at least partially causes #4238
2020-12-09 06:56:21 +10:30
Rusty Russell e9e6f72d7c bitcoin_tx_check: don't rely on tmpctx.
We assert() this in onchaind while grinding fees; better to free newtx.

Before this we hit 530MB, after a mere 2.5MB.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: onchaind uses much less memory on unilateral closes for old channels.
2020-12-07 14:23:59 +01:00
niftynei b4aebc17a1 bitcoin: break out method to calculate input weight
We have a required minimum witness weight for dual funded opens.
2020-12-02 14:19:08 +10:30
Rusty Russell ca2bd98082 unittest: use common_setup / common_shutdown almost everywhere.
Avoids much cut & paste.  Some tests don't need any of it, but most
want at least some of this infrastructure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-12-02 12:55:09 +10:30
Jon Griffiths 5c2fc4c1cb wally: remove wally casts that are no longer needed
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-11-30 10:28:04 +10:30
Jon Griffiths 5bdad04976 tx: Remove wally workaround that is no longer needed
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-11-30 10:28:04 +10:30
Rusty Russell cce3e717d9 bitcoin/signature: wrap libwally call.
If a tx is larger than 2k, libwally will do an alloc:

```
lightning_hsmd: common/setup.c:11: wally_tal: Assertion `wally_tal_ctx' failed.
0x11c283 wally_tal
	common/setup.c:11
0x15ebd1 wally_malloc
	../../../libwally-core/src/internal.c:233
0x171e9e tx_to_bip143_bytes
	../../../libwally-core/src/transaction.c:1918
0x172cda tx_to_bytes
	../../../libwally-core/src/transaction.c:2086
0x1759df tx_get_signature_hash
	../../../libwally-core/src/transaction.c:2776
0x175afd wally_tx_get_signature_hash
	../../../libwally-core/src/transaction.c:2800
0x175b62 wally_tx_get_btc_signature_hash
	../../../libwally-core/src/transaction.c:2810
0x1297d9 bitcoin_tx_hash_for_sig
	bitcoin/signature.c:139
0x1298ca sign_tx_input
	bitcoin/signature.c:161
0x10e701 handle_sign_remote_commitment_tx
	hsmd/hsmd.c:1011
0x110f7f handle_client
	hsmd/hsmd.c:1968
0x147a71 next_plan
	ccan/ccan/io/io.c:59
0x1485ee do_plan
	ccan/ccan/io/io.c:407
0x14862c io_ready
	ccan/ccan/io/io.c:417
0x14a7f2 io_loop
	ccan/ccan/io/poll.c:445
0x111125 main
	hsmd/hsmd.c:2040
```

I reduced that constant in libwally to 200, and ran the entire
test suite, and found no other places.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-11-04 14:28:06 -06:00
niftynei 0ca6c3cc84 script: break out redeemscript->scriptsig function
We're going to finalze some redeemscripts here shortly, so break out the
ability to render an arbitrary redeemscript -> scriptsig
2020-10-26 13:04:42 +10:30
Rusty Russell a8177e9013 Makefile: make check-includes check all the non-generated files.
Note that check-whitespace and check-bolt already do this, so we
can eliminate redundant lines in common/Makefile and bitcoin/Makefile.

We also include the plugin headers in ALL_C_HEADERS so they get
checked.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-10-22 12:14:34 +10:30
niftynei 26bc4f5239 tx,bugfix: correct signature length estimate
71-bytes for a signature already includes the sighash byte.

 2-bytes	30 44 (DER- prefix thing)
34-bytes	02 20 6e29c8df67fffdda1613cef1413eb1a9ef3627f1fc5e4d910837274eafcc7b2a (r)
34-bytes	02 20 4b8563d79b92fdd830a546862439f80b24132d09318af2c7220c791067067e29 (s)
 1-byte		01 (sighash)
==
71-bytes
2020-10-21 11:24:16 +10:30
niftynei 8317957db2 feerate: remove duplicate method
is dupe of `bitcoin_tx_core_weight`
2020-10-20 14:27:19 +10:30
niftynei 4034d0c306 psbt: have the unknown map 'add' be a 'set' instead 2020-10-20 14:27:19 +10:30
niftynei 93045945be psbt: add helper to set the non-witness utxo for an input
We need this info for every dual-funded open
2020-10-20 12:50:31 +10:30
niftynei 82c0b48215 wires: towire/fromwire for wally_tx
We're eventually moving away from 'bitcoin_tx
2020-10-20 12:50:31 +10:30
Dr. Maxim Orlovsky 0b01a5d7c4 Updating signet chainparams for the latest PoW fix
Changelog-Fixed: Protocol: `signet` is now compatible with the final bitcoin-core version
2020-09-24 09:24:14 +09:30
Rusty Russell 1356700e2b bitcoin/chainparams: signet uses the same P2PKH and P2SH version bytes as testnet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-24 09:24:14 +09:30
Rusty Russell d5cb0d85b5 utils: use a cleaner pattern to capture wally allocations.
We force use of tal_wally_start/tal_wally_end around every wally
allocation, and with "end" make the caller choose where to reparent
everything.

This is particularly powerful where we allocate a tx or a psbt: we
want that tx or psbt to be the parent of the other allocations, so
this way we can reparent the tx or psbt, then reparent everything
else onto it.

Implementing psbt_finalize (which uses a behavior flag antipattern)
was tricky, so I ended up splitting that into 'psbt_finalize' and
'psbt_final_tx', which I think also makes the callers clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 14:45:53 +02:00
Rusty Russell 66ffd7a73f bitcoin/: neaten use of libwally base58 and bas64 routines.
Now libwally uses tal, we don't need to make copies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 14:45:53 +02:00
niftynei 1f165c00ae psbt_txid: it's possible a psbt may already have the finalized scriptsig
If we've already got a scriptSig field filled out for a PSBT input, we
use that instead of 'deriving' the scriptSig from the redeemscript
(finalizing a PSBT removes the redeemscript field)
2020-09-23 15:51:13 +09:30
Rusty Russell c23001d508 bitcoin: use tal_gather_wally() so we don't leave unattached allocations.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 3c8049f32c bitcoin/psbt: psbt_input_add_unknown/psbt_output_add_unknown needs a tal ctx.
Since it allocates something, it needs a context (used in the next patch!).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 77b62d9e42 bitcoin/psbt: psbt_finalize needs a tal ctx.
Since it returns a wally_tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 607075a3d4 bitcoin/psbt: wallt_tx_output needs a tal ctx.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 480f671e91 bitcoin/psbt: psbt_txid needs a tal ctx.
It returns a wally_tx; it's an anti-pattern not to hand in a tal context.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 67fbb349b6 bitcoin/tx: trivial cleanups.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
Rusty Russell 11ebb84e7d bitcoin/psbt: attach destructors to wally allocations to avoid leaks.
This covers the obvious ones, but the later patches fix this more
seriously.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Fixed: Some memory leaks in transaction and PSBT manipulate closed.
2020-09-23 13:52:49 +09:30
Rusty Russell 80c705d528 bitcoin/psbt: more const pointers.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-09-23 13:52:49 +09:30
niftynei 165d039f6c elements: consolidate fee outputs into a single fee output
In the case that you've got more than one fee output already on a
psbt, we consolidate them into a single fee output (the first)
2020-09-10 12:31:36 +09:30
niftynei 4c28e7b362 txprepare: elements requires inclusion of an accurate fee output
so we add an accurate one
2020-09-10 12:31:36 +09:30
niftynei 65f01d8180 bitcoin: pull up elements_fee calc to allow wtx passed in
We're moving away from bitcoin_tx, slowly
2020-09-10 12:31:36 +09:30
niftynei 24578c7fea elementsd: use the elements version of a 'witness utxo'
Elements requires the witness utxo to include the asset and value info,
in order for the signing hash to be constructed correctly

Changelog-Fixed: elementsd: PSBTs include correct witness_utxo struct for elements transactions
2020-09-10 12:31:36 +09:30
ZmnSCPxj jxPCSnmZ 5d7178d488 bitcoin/psbt.c: Add psbt_insert_output function. 2020-09-09 20:36:08 +09:30
niftynei 303263d381 psbt: clean up interface for setting metadata on PSBT inputs
it's just neater if it's not all wrapped up together, simplifies the
interface a smidge
2020-09-09 19:54:20 +09:30
niftynei b2170cf3f4 feerate: add a 'common_weight' calculation for a tx
The bits of a transaction that are paid by the opener!
2020-09-09 19:54:20 +09:30
ZmnSCPxj jxPCSnmZ 0d3a3b6d48 plugins/multifundchannel.c: Implementation of `multifundchannel`.
Changelog-Added: We now have `multifundchannel` as a builtin plugin command to fund multiple channels to different peers all in a single onchain transaction.


Header from folded patch 'fixup-use-json_add_psbt.patch':

fixup!


Header from folded patch 'use-goto-no-ok-chain.patch':

fixup!


Header from folded patch 'destinations-at-parse-time.patch':

fixup!


Header from folded patch 'multifundchannel__use_jsmntoks_to_pass_through_json_string,_not_strings.patch':

multifundchannel: use jsmntoks to pass through json string, not strings

Passing in "" for utxos would crash lightningd on the command-line
otherwise. Now returns an error.


Header from folded patch 'update_plugins-multifundchannel.c.patch':

Update plugins/multifundchannel.c

Co-authored-by: Darosior <darosior@protonmail.com>
2020-09-09 16:45:56 +09:30
ZmnSCPxj jxPCSnmZ 6c13e9b300 bitcoin/script.c: Add scriptpubkey_opreturn_padded, which creates an OP_RETURN with a pointless random 20-byte padding.
In the case of `donateutxo`, this is needed since a simple spend of a P2WPKH to an `OP_RETURN` would be below the minimum transaction size.
Sizes below 20 are not plausible as commitments.
2020-09-09 12:38:19 +09:30
ZmnSCPxj jxPCSnmZ 12c9b27838 bitcoin/tx.c: `bitcoin_tx_output_get_amount_sat` now accepts `const` transactions.
It is a getter, so, does not change the transaction, so should accept `const`.
2020-09-09 12:38:19 +09:30
Christian Decker f2797f303a block: Compute the txids only once
We're not going to mutate transactions in a block, so computing the txids
every time we need them is a waste, let's compute them upfront and use them
afterwards.
2020-09-02 13:21:32 +02:00
Rusty Russell 1746406e41 Makefile: normalize all the Makefiles
We create ALL_PROGRAMS, ALL_TEST_PROGRAMS, ALL_C_SOURCES and
ALL_C_HEADERS.  Then the toplevel Makefile knows which are
autogenerated (by wildcard), so it can have all the rules to clean
them or check the source as necessary.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-31 21:33:26 -05:00
niftynei 0305c0a57e psbt: if psbt is null, return null for get_bytes 2020-08-25 12:34:56 +09:30
niftynei 10237ccb8e psbt: handle 'NULL' or empty byte PSBTs
Don't crash if the PSBT is null / zero-byte len.
2020-08-25 12:34:56 +09:30
Rusty Russell 6e4e267f2c doc: update BOLT now option_anchor_outputs is merged.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-24 20:55:47 +09:30
Rusty Russell 85a90b9e32 bitcoin: bitcoin_tx_add_input use psbt_append_input.
Most of what it does was actually a function of adding the input metadata
to the PSBT, so call that and simply copy out the tx input it creates.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-20 21:14:12 +09:30
Rusty Russell a0ede40743 bitcoin: make psbt_append_input more powerful.
It can now handle all the metadata as well as the base tx input.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-20 21:14:12 +09:30
Rusty Russell 172b9d7ae3 bitcoin: add nlocktime arg to create_psbt.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-20 21:14:12 +09:30
niftynei d1d96a9952 tx: use elements_input_init_alloc 2020-08-18 11:08:44 +09:30
niftynei c6656dcbe8 psbt: use elements input init alloc 2020-08-18 11:08:44 +09:30
niftynei 2f9a37de4a psbt: rename append_out -> append_output 2020-08-18 11:08:44 +09:30
niftynei eef762cc09 psbt: re-use init function for create_psbt
Less code is more code!
2020-08-18 11:08:44 +09:30
niftynei 0651b0ed8a pubkey: update comment to reflect location 2020-08-18 11:08:44 +09:30
niftynei 3923e0f0fd psbt: utilities for adding unknown info / proprietary c-lightning keydata 2020-08-18 11:08:44 +09:30
niftynei 251cde3514 psbt: add helpers for creating + adding input/outputs 2020-08-18 11:08:44 +09:30
Rusty Russell 591e8f9663 psbt: finalize to-remote with option_anchor_outputs.
Until it gains miniscript support, wally doesn't know how to
finalize P2WSH.  This happens with `option_anchor_outputs` for
to-remote, which requires a 1 block delay to force use of
anchor outputs for CPFP.

So we finalize this ourselves.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-14 11:51:14 +09:30
Rusty Russell 3ff8311b40 channeld: change to_remote for option_anchor_outputs.
It's now a P2WSH to incorporate a CSV 1 delay.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-14 11:51:14 +09:30
Rusty Russell e7423888ba initial_commit_tx, commit_tx: add anchor outputs if needed.
This also means we subtract 660 satoshis more everywhere we subtract
the base fee (except for mutual close, where the base fee is still
used).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-14 11:51:14 +09:30
Rusty Russell 05c3a9bf12 script: change htlc output scripts if option_anchor_outputs.
For the moment, everyone passes "false".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-14 11:51:14 +09:30
Rusty Russell a6a8a40594 psbt: implement psbt_txid.
You can't just take the txid of the global tx, if some are p2sh-wrapped.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-11 08:39:24 +09:30
Rusty Russell 09f065fc45 script: expose script_push_bytes().
We'll need it for creating redeemscripts for PSBTs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-11 08:39:24 +09:30
Rusty Russell 6966cf99e1 bitcoin: add wally_tx_output helper to create standalone output.
In preparation for when we don't have a tx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-11 08:39:24 +09:30
Rusty Russell 600d0a4a1d psbt: make psbt_from_b64 more conventional.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-11 08:39:24 +09:30
Jon Griffiths 908a8399e2 psbt: Allocate correctly sized buffer for psbt_to_bytes
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths fef155a9e2 psbt: Use wallys function to check PST finalization status
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths 1fe53880a7 psbt: use new wally functions to add PSBT inputs/outputs
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths 68ffecba63 psbt: Use the newly exposed wally function to clone PSBTs
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths a51c6550ec psbt: avoid allocations when adding psbt outputs
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths 499395636c psbt: remove now-unneeded const casts
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths 21ee2c3a9c psbt: Remove workaround for now-fixed wally tx flag behaviour
Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Jon Griffiths 95d3d65c62 wally: update to the latest wally version
Includes:
psbt: Use renamed functions for new wally version
psbt: Set the transaction directly to avoid script workarounds
psbt: Use low-S grinding when computing signatures
tx: Use wally_tx_clone from libwally now that its exported

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
2020-08-07 10:28:01 +09:30
Rusty Russell ffbb409b47 amount: use initializers everywhere.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-08-06 09:36:47 +09:30
Rusty Russell bd19ec2292 fundpsbt: new JSON API to gather UTXOs.
Technically, they could do this themselves, but it's much nicer to have one
place to do it (and it makes sure we get the required information into the
PSBT, which is actually not entirely accessible through listfunds, as that
doesn't want to consult with the HSM for close outputs).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: JSON RPC: new low-level coin selection `fundpsbt` routine.
2020-07-15 18:49:02 +09:30
niftynei f5f85b389d elements,pset: populate elements specific data for PSBTs
PSETs have a bit different requirements. The witness_utxo needs
the asset tag + values, and these should also be added to the PSET
struct separately as well. To do this, we create a new 'init' method for
elements inputs, which takes care of the elements specific things.
2020-07-13 11:37:24 +09:30
niftynei bc5a817100 elements: convenience methods for dealing with assets
We don't preserve detailed asset information at the moment, so provide a
way to convert from a sat to an amount_asset struct.

We also need a way to convert from an 'amount_asset' to a 'value' for
elements, which for explicit (i.e. non-blinded) asssets is a 0x01 prefix
plus the big-endian encoded value.
2020-07-13 11:37:24 +09:30
niftynei 14de198bd1 wally-tx: add type-to-string for a wally-tx
and then use it to print out things
2020-07-13 11:37:24 +09:30
niftynei 4ecbd5f1a7 psbt: use pset initializer if needed
we're moving over to elements!
2020-07-13 11:37:24 +09:30
Rusty Russell 731e037b36 change_amount: routine to determine if change output is worthwhile.
This can be used by plugins which create their own txs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-07-08 21:07:20 +02:00
Rusty Russell a9427f1a8d bitcoin/feerate: new exposure for feerate parsing outside lightningd.
This exposes the numeric part of param_feerate() as param_feerate_val().

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-07-08 21:07:20 +02:00
Rusty Russell 7aa8ffa2a0 bitcoin: add weight calculation helpers.
These are pulled from wallet/wallet.c, with the fix now that we grind sigs.

This reduces the fees we pay slightly, as you can see in the coinmoves changes.

I now print out all the coin moves in suitable format before we match:
you only see this if the test fails, but it's really helpful.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-07-06 19:25:05 +02:00
niftynei 2ab41af8e2 rm pullpush: cleans up unused methods for push/pull
most likely unused since the switch to libwally for internal blockchain
things.

these method names were clashing with ones that are to be introduced
with some libwally cleanups, so getting rid of them pre-emptively keeps
us libwally compatible
2020-07-01 19:50:02 -05:00
niftynei ad6ca610b8 bugfix: tx_parts omits two byte counts in serialization 2020-07-01 19:50:02 -05:00
niftynei 2e9c387f45 coin_moves: update withdrawal logic to account for 'variable owner' txs
Our existing coin_moves tracking logic assumed that any tx we had an
input in belonged to *all* of our wallet (not a bad assumption as long
as there was no way to update a tx that spends our wallets)

Now that we've got `signpsbt` implemented, however, we need to be
careful about how we account for withdrawals. For now we do a best guess
at what the feerate is, and lump all of our spent outputs as a
'withdrawal' when it's impossible to disambiguate
2020-06-29 16:10:05 +02:00
niftynei ee549a2af9 tx: fix case where input amounts are less than total outputs
when attempting to calculate the fees for a tx where we don't own all of
the outputs, we can overshoot the feerate
2020-06-29 16:10:05 +02:00
niftynei 0388fe6db4 tx: add helper for extracting script from a wally_tx
the bitcoin_tx version is basically a wrapper for the wally_tx script
extraction -- here we pull it apart so we can easily get a tal'd script
for a wally_tx_output
2020-06-29 16:10:05 +02:00
Rusty Russell 93d04d08d0 wire: update to latest version of the spec.
The main change here is that the previously-optional open/accept
fields and reestablish fields are now compulsory (everyone was
including them anyway).  In fact, the open/accept is a TLV
because it was actually the same format.

For more details, see lightning-rfc/f068dd0d8dfa5ae75feedd99f269e23be4777381

Changelog-Removed: protocol: support for optioned form of reestablish messages now compulsory.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-06-23 18:49:15 +02:00
niftynei 85dca84327 psbt-json: remove reliance on bitcoin_tx, use straight wally_psbt struct 2020-06-23 14:49:32 +02:00
niftynei 175fcf381a psbt: have wally_tx serialization methods be legible for gen'd code
our code generators expect the serialization name to match the struct
type
2020-06-23 14:49:32 +02:00
niftynei b63abef542 psbt: add method to finalize + extract a psbt
will either use a temporary psbt (and not munge the passed in psbt)
or will finalize in place -- finalization erases most of the signature
metadata from the psbt struct
2020-06-23 14:49:32 +02:00
niftynei 0d1f1bc66e psbt: add helper method for setting a psbt input's redeemscript 2020-06-23 14:49:32 +02:00
niftynei c3ae44e296 psbt: don't crash if we can't add a partial sig
instead return a boolean indicating the success/failure of a sig set
2020-06-23 14:49:32 +02:00
niftynei 80072b389e psbt: remove script sig info from inputs before adding them to global
PSBT's dont' serialize / unserialize if there's any sig info set on the
global transaction
2020-06-23 14:49:32 +02:00
lisa neigut 16656a85cf withdraw: refactor change output handling
We're not using the change_outnum for withdraw tx's (and the way
we were calculating it was broken as of the addition of 'multiple
outputs'). This removes the change output knowhow from withdraw_tx
entirely, and pushes the responsibility up to the caller to
include the change output in the output set if desired.

Consequently, we also remove the change output knowhow from hsmd.
2020-06-23 14:49:32 +02:00
niftynei 09815c7e7f psbt: return NULL instead of aborting on wally-lib problems
This lets us parse invalid/bad psbt data from user input without
crashing
2020-06-23 14:49:32 +02:00
niftynei 5ecacf3dd0 psbt: add type-to-string that prints b64 string
Re-uses code from what was the bitcoin_tx_to_psbt_b64
2020-06-23 14:49:32 +02:00
niftynei 9e0ef45048 psbt: handle 'unsetting' final witness stack
Prior to this commit, passing a NULL stack to `bitcoin_tx_input_set_witness`
unsets the witness stack on the bitcoin_tx's wally_tx but leaves the
final witness on the PSBT unchanged.

at the moment, libwally's `wally_psbt_input_set_final_witness` will blow
up if you attempt to set a NULL witness -- instead we manually remove it
if the passed in stack is NULL. previously we would leave the PSBT's
witness unchanged.
2020-06-11 13:13:13 +02:00
niftynei 58282819a9 psbt: if a transaction has witnesses/scriptSig set, add it to psbt
For any transaction that's got 'finalized' signature data for an input,
we should add this information to the psbt also
2020-06-11 13:13:13 +02:00
niftynei 8fa04a710a psbt: move `channels.last_tx` field to be a psbt
note: missing migration at the moment lol
2020-06-11 13:13:13 +02:00
niftynei 052d40ae98 psbt: add method to confirm 'finalized' status of psbt
calling `wally_psbt_finalize` doesn't return a status indicator; instead
you must call `psbt_is_finalized` to check that it's eligible for
'extraction' -- extraction will fail if the psbt is not in a finalized
state.
2020-06-11 13:13:13 +02:00
niftynei 000ef2079c psbt: helpers for adding a pubkey or signature to a psbt
we'll use these for the commitment txs!
2020-06-11 13:13:13 +02:00
niftynei 3c6e3eecee psbt: add 'wally_psbt_clone' function, to clone a psbt
We'll need this for settng the tx correctly, for reasons
2020-06-11 13:13:13 +02:00
Rusty Russell d737b54625 bitcoin/tx: implement wally_tx_clone (badly) for now.
When libwally exposes it, we can use theirs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-06-11 13:13:13 +02:00
niftynei f9300e8480 tx: add setter for tx locktime
We need to update the psbt's global transaction simultaneously, so we
wrap access to the locktime in a method which will handle both
2020-06-11 13:13:13 +02:00
niftynei db8ef922ed psbt: add to/from byte helpers
We'll need these for the database methods we're going to add shortly
2020-06-11 13:13:13 +02:00
niftynei 7969f61ab3 tx: update comment
Make comment a bit more descriptive / usefu
2020-06-11 13:13:13 +02:00
niftynei dc868630a8 tx-psbt: pass in the witness script (if known) when adding an input
Update the `bitcoin_tx_add_input` interface to accept a witness script
and or scriptPubkey.

We save the amount + witness script + witness program (if known) to
the PSBT object for a transaction when creating an input.
2020-06-11 13:13:13 +02:00
niftynei a04f0fe250 psbt: remove input_amounts from bitcoin tx
Instead we will stash them into the PSBT as a utxo/witness record (which
includes the amount)
2020-06-11 13:13:13 +02:00
niftynei cc6eba1d72 psbt: try one big alloc and fail instead of incremental buffer increases
was running into buffer overrun errors? something about the iteration
method was broken
2020-06-11 13:13:13 +02:00
niftynei 6197bd2b82 psbt: check return value on init for wally input 2020-06-11 13:13:13 +02:00
Rusty Russell 0b3040b9a6 bitcoin/tx: insist input amounts all be populated across the wire.
If you need to send a tx where you don't know this info, send a tx_parts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-27 10:12:03 +09:30
Rusty Russell dafaf854c5 bitcoin/tx_parts: infrastructure for partial bitcoin txs.
`struct tx_parts` is just a txid and a bunch of inputs and outputs,
some of which may be NULL.

This is both a nod towards a future where we (or our peer) can combine
HTLCs or (in an eltoo world) commitments, although for the moment all
our tx_parts will be complete.

It also matches our plan to split `bitcoin_tx` into two types: this
`struct tx_parts` where we don't know input amounts etc, and `psbt`
where we do.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-27 10:12:03 +09:30
Rusty Russell 7e48f77d43 bitcoin/tx: expose functions for raw wally_txs.
These are useful for tx_parts:

1. wally_txid.
2. linearize_wtx.
3. wally_tx_input_spends
4. wally_tx_output_get_amount
5. wally_tx_input_get_txid

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-27 10:12:03 +09:30
lisa neigut 1fb9a078b6 txprepare: return psbt serialized version of tx as well
Changelog-Added: JSON-API: `txprepare` returns a psbt version of the created transaction
2020-05-21 18:45:07 +09:30
niftynei bf4cac7fb8 tx: strip out witscript
now that witness script data is saved into the tx/psbt which is
serialized across the wire, there's no reason to use witscript to do
this. good bye witscript!
2020-05-21 18:45:07 +09:30
niftynei 559f88faa1 psbt: add serialize to/from wire for psbts 2020-05-21 18:45:07 +09:30
niftynei 5d0fc176e8 psbt: create new_psbt method
Move all psbt creation into single method, new_psbt

note that if a psbt is init'd for a transaction that's
deserialized with scripts etc already attached, then set_global_tx
will fail. instead, we empty all of this out first.

if the tx is being re-init'd from a tx source that had a psbt attached
(e.g. fromwire_) then the script/witness data will get populated
appropriatel from there.
2020-05-21 18:45:07 +09:30
niftynei a848df67f1 psbt: populate scriptsigs + witnesses
Pass scriptSig + witness info down to the PSBT struct
2020-05-21 18:45:07 +09:30
niftynei 2055b53425 psbt: update global tx output amount also 2020-05-21 18:45:07 +09:30
niftynei cf9de86dba psbt: add transaction inputs to the psbt struct
Make sure that we permute them also!

Fixes weird spacing also
2020-05-21 18:45:07 +09:30
niftynei a1e1073752 tx: move to tal_dup_arr 2020-05-21 18:45:07 +09:30
niftynei b076f40cf3 psbt: move witness script storage into the psbt
we can now keep associated witness data with the output in the psbt
struct, so we do that.
2020-05-21 18:45:07 +09:30
niftynei 2d5c61dfc1 psbt: methods to mutate psbt in place
add the missing psbt helpers for adding and removing an input and output
2020-05-21 18:45:07 +09:30
niftynei 7a0624797e psbt: add psbt to bitcoin tx struct 2020-05-21 18:45:07 +09:30
Rusty Russell f1426bad8e bitcoin/tx: fix bug in fromwire_bitcoin_tx.
If we fail to unmarshal the tx (shouldn't happen, but...) we must
not dereference NULL.

Also tighten the check: towire_ must send 0 or all inputs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-20 11:36:13 +02:00
Rusty Russell b920d4d21b wire: make fromwire_fail return non-const, use it more
It returns NULL, so you can simply `return fromwire_fail(...)`
if you want to return NULL in this case.  Use that more.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-20 11:36:13 +02:00
niftynei fbe50e087a setup: create a common setup which will handle the wally-context
Since we now over-write the wally malloc/free functions, we need to do
so for tests as well. Here we pull up all of the common setup/teardown
logic into a separate place, and update the tests that use libwally to
use the new common_setup core

Changelog-None
2020-05-19 13:35:42 +09:30
Rusty Russell cfb320c972 wire: move remaining bitcoin functions out to bitcoin/ files.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-18 14:51:12 +02:00
Rusty Russell 197d1bcef2 wire: move towire/fromwire_short_channel_id out to bitcoin/short_channel_id.c
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-18 14:51:12 +02:00
Rusty Russell 4dbfce6057 wire: move towire/fromwire_preimage out to bitcoin/preimage.c.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-18 14:51:12 +02:00
Rusty Russell 27220646c3 common/wire: move bitcoin-specific marshalling functions into bitcoin files.
We did this originally because these types are referred to in the bolts, and we
had no way of injecting the correct include lines into those.  Now we do, so
there's less excuse for this.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-18 14:51:12 +02:00
Rusty Russell 4bcac3032c bitcoin: remove unused struct bitcoin_tx_input.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-05-15 09:55:59 +09:30
lisa neigut 824e8fa72b tx: split 'compute fee' into two, with one that takes an input value
Transactions that we 'get' from bitciond don't have the input values
available on the transaction; for these cases we'll sum up the inputs
amounts using a different data source than the transaction's
`input_amounts`. So we need to expose it here.
2020-05-12 15:46:58 +09:30
lisa neigut 3d241bc261 tx: expose elements 'is fee' calculation
we'll use it when figuring out what outputs to account for during
a withdrawal.
2020-05-12 15:46:58 +09:30
lisa neigut c110f3662c bitcoin_tx: add helper for extracting output amount 2020-05-07 08:43:00 +09:30
Rusty Russell d881a4bd66 BOLT: update to latest version.
This is all typo/clarity fixes, no substantive changes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-03-31 13:36:02 +02:00
Christian Decker c236bf0fbd tx: Make elements_add_fee_output private 2020-03-24 09:52:33 +10:30
Christian Decker 42a63e4416 tx: Strengthen transaction construction checks
We roll the `elements_add_fee_output` function and the cropping of
overallocated arrays into the `bitcoin_tx_finalize` function. This is supposed
to be the final cleanup and compaction step before a tx can be sent to bitcoin
or passed off to other daemons.

This is the cleanup promised in #3491
2020-03-24 09:52:33 +10:30
Rusty Russell 2aad3ffcf8 common: tal_dup_talarr() helper.
This is a common thing to do, so create a macro.

Unfortunately, it still needs the type arg, because the paramter may
be const, and the return cannot be, and C doesn't have a general
"(-const)" cast.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2020-02-27 14:16:16 +10:30
Christian Decker 9521549c49 elements: Fix missing witness script entry for fee outputs
Fixes #3487
2020-02-08 14:26:12 +01:00
Ken Sedgwick 5c8f881a75
hsmd: Added fields to hsm_sign_remote_commitment_tx to allow complete validation.
Changelog-Added: hsmd: Added fields to hsm_sign_remote_commitment_tx to allow complete validation by signing daemon.
2020-02-04 10:40:43 +10:30
darosior 273029f244 wallet: set nLockTime to the tip for withdrawal transactions
This sets the nLockTime to the tip (and accordingly each input's nSequence to
0xfffffffe) for withdrawal transactions.

Even if the anti fee-sniping argument might not be valid until some time yet,
this makes our regular wallet transactions far less distinguishable from
bitcoind's ones since it now defaults to using native Segwit transactions
(like us). Moreover other wallets are likely to implement this (if they
haven't already).

Changelog-Added: wallet: withdrawal transactions now sets nlocktime to the current tip.
2020-02-03 00:45:27 +00:00
Christian Decker 234252016d cleanup: Remove block header fields we don't use 2020-01-28 20:03:55 +01:00
Christian Decker a61b044056 elements: Implement dynafed block format support
Changelog-Added: elements: Added support for the dynafed block header format and elementsd >=0.18.1
2020-01-28 20:03:55 +01:00
Christian Decker c99786e720 bitcoin: Use the block hash we computed while deserializing it
Since we now compute the hash while deserializing the block header we can now
just use it, no reason to serialize the header just to hash it again. This
also allows us to throw away all the added dynafed fields in the next commit
instead of having to carry them around.
2020-01-28 20:03:55 +01:00
Christian Decker fce05d74e1 elements: Construct the block header hash while we parse it 2020-01-28 20:03:55 +01:00
Christian Decker 8839be641a sha256: Add primitive to add a varint-encoded int to a hash
We're about to push a couple of varints as part of the dynafed blockparsing,
so we might as well make it easier for us.
2020-01-28 20:03:55 +01:00
Christian Decker 4de0d1ca22 bitcoin: Compute block hash while parsing
This avoids having to re-serialize the block header just to compute the
hash. It also frees us from having to carry around all the details in the
header and we can hand around a minimal version.
2020-01-28 20:03:55 +01:00
Christian Decker c2434ad4fb bitcoin: Compute block hash while parsing
This avoids having to re-serialize the block header just to compute the
hash. It also frees us from having to carry around all the details in the
header and we can hand around a minimal version.
2020-01-27 22:48:42 +01:00