Commit Graph

440 Commits

Author SHA1 Message Date
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
darosior 4b13b88f6c bitcoin/chainparams: add an utility to retrieve chainparams for all networks 2019-11-29 21:17:08 +01:00
lisa neigut 3705b5f605 devtools: add privkey+hash printing to mkcommit/mkgossip
We updated the protocol spec tests to verify a sig from a hash
and a private key; this updates mkcommit + mkgossip utilities
to print out the procotol compatible SIG() notation for all signatures.

--verbose will print a computed signature and more data as well.

Also adds --verbose flag to mkgossip.

Changelog-None
2019-11-21 01:17:33 +00:00
lisa neigut 861b12b21a dump_tx: fixup compiliation errs
When we refactored bitcoin_tx to use wally_tx, this (uncompiled)
debug statement rotted. This unrots it.
2019-11-21 01:17:33 +00:00
gorazdko a3851f2943 wallet: always create signatures with low r-value 2019-10-29 18:51:09 -05:00
Christian Decker be49a599bc elements: Do not get upset if we see a confidential asset or value
I made some assumptions that turn out not to be true, mea culpa.
2019-10-18 16:10:17 +02:00
Christian Decker d35ec902f4 elements: Work around libwally getting upset with helpful flags
libwally really is pedantic about the kind of hints it will accept.

Signed-off-by: Christian Decker <@cdecker>
2019-10-18 16:10:17 +02:00
lisa neigut cbfa045f91 tx: remove input size assertion
this is unnecessary, and actually severely limits the functionality
of `wally_tx_add_input`, which will expand the allocated input
length if there's not enough room for the additional input

```external/libwally-core/src/transaction.c
    if (tx->num_inputs >= tx->inputs_allocation_len) {
        /* Expand the inputs array */
        struct wally_tx_input *p;
        p = realloc_array(tx->inputs, tx->inputs_allocation_len,
                          tx->num_inputs + 1, sizeof(*tx->inputs));
        ...
        tx->inputs = p;
        tx->inputs_allocation_len += 1;
```
2019-10-10 05:57:45 +00:00
lisa neigut 496d2cae5f varint: Add helper function for getting varlen size
Needed to calculate somethings for building dual funding txs
2019-10-10 05:57:45 +00:00
lisa neigut cb2cad8c94 script: add helper for checking if a 'known type'
To be used for verifying remote inputs' validity
2019-10-10 05:57:45 +00:00
Christian Decker ff4a2bf38f onchaind: Annotate inputs and outputs not the transactions 2019-10-07 20:03:24 -05:00
Yash Bhutwala 30ec739c15 check for return code in the else block 2019-10-06 22:36:05 -05:00
Christian Decker 7283efa5b5 elements: Add amount_asset to support more than just plain satoshis
Currently the only source for amount_asset is the value getter on a tx output,
and we don't hand it too far around (mainly ignoring it if it isn't the
chain's main currency). Eventually we could bubble them up to the wallet, use
them to select outputs or actually support assets in the channels.

Since we don't hand them around too widely I thought it was ok for them to be
pass-by-value rather than having to allocate them and pass them around by
reference. They're just 41 bytes currently so the overhead should be ok.

Signed-off-by: Christian Decker <@cdecker>
2019-10-03 04:32:57 +00:00
Christian Decker c38afc5512 tx: Switch to amount_sat for fee computations
Suggested-by: Rusty Russell <@rustyrussell>
2019-10-03 04:32:57 +00:00
Christian Decker d5f0c08a88 elements: Remove global is_elements variable in favor of chainparams
No need to keep duplicate globals.
2019-10-03 04:32:57 +00:00
Christian Decker ef7a63d8f8 elements: Move from a global is_elements to a global chainparams
We now have a pointer to chainparams, that fails valgrind if we do anything
chain-specific before setting it.

Suggested-by: Rusty Russell <@rustyrussell>
2019-10-03 04:32:57 +00:00
Christian Decker 378745391d elements: Change function prefix to elements_ for some functions
Suggested-by: Rusty Russell <@rustyrussell>
2019-10-03 04:32:57 +00:00
Christian Decker 91a311ee0d elements: Added better handling of NULL output scripts
We used to match specifically on `is_elements && coinbase`, but we can just
hand off responsibility to libwally and then make sure we handle it correctly.
2019-10-03 04:32:57 +00:00
Christian Decker fad9a74662 tx: Use the context chainparams to determine the fee asset
This is the main reason we started weaving the chainparams everywhere: being
able to compare the asset type with the fee paying asset tag, thus determining
the value of the asset correctly (we still treat any non-fee-paying assets as
having value 0).

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker d0708a483a chainparams: Add liquid_fee_asset to determine which asset pays fees
This is required since liquid-regtest and liquidv1 have different asset tags
for L-BTC which is the fee-paying asset.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 557f6063a7 elements: Consolidate weight computation to be handled by wally
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 9e333f2416 elements: Add liquidv1 mainnet chainparams
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 99988cb0e1 elements: Fix up existing fee output instead of adding a new one
Especially when we grind fees we may end up setting the fees several times, so
instead of always adding a new fee output look for an existing one and set its
value.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker cd93a855ce elements: Give pytest some information about the test chain
We are checking against chain-dependent constants, so let's make sure we are
using the ones for the correct chain.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 92b8758753 elements: Add helpers to compute and add fee outputs.
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 5e3b27c306 elements: Mark inputs with the elements feature
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 541d8db2a6 elements: Use correct initializers for outputs and values
We use to use the non-elements ones and then patch them manually. By using the
correct ones right from the start we have less work on our side.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker f0396df449 elements: Added small helper to compute the right signature hash
Depending on the network we end up with different signature hash algorithms,
so we just collect that decision in one place.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 6b7c3c7a78 elements: Extract the real value from the transactions
If we are handling an elements transaction the value is not stored in the
satoshi field, rather it is stored in the `value` field which is prefixed with
a version (0x01) and is counted in `asset` units.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 639713b547 elements: Fix transaction handling for elements transactions
Skipping coinbase transactions and ensuring that the transaction is serialized
correctly when sending it onwards.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 436da7f231 elements: Move blkid computation into its own function
The header is not a contiguous section of memory in elements, and it is of
variable length, so the simple trick of hashing in-memory data won't work
anymore. Some of the datafields would have been wrong on big-endian machines
anyway, so this is better anyway.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker f19726b4aa elements: Pass the correct flags to libwally so it can parse txs
This was a bit of trial and error due to libwally not liking hints when it
comes to length measurements, also the parsing bumps against a masking issue
in libwally that I'd following up on their issue tracker.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker 4553b5c2f2 elements: Implement block parsing for elements block headers
Since the difference between non-elements and elements block headers is just
the middle 2 fields, I split the old parsing code so I could add the middle
part.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker a300dea7e6 elements: Global variable whether we are running on elements
Using a global variable is a bit lazy, but weaving the network type through
the entire stack is a daunting task. Maybe we can make that happen at a later
stage.

Most of the changes in `chainparams.c` are just formatting the
`genesis_blockhash` a bit nicer (`clang-format` to the rescue).

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Christian Decker b9d7731100 chainparams: Add liquid-regtest as a supported network
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-10-03 04:32:57 +00:00
Rusty Russell 45381bba33 bitcoin: implement is_scid_depth_announceable helper.
The math is a bit tricky, so encapsulate it.

Includes the extra 'e' in 'announcable' as noted by @cdecker :)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-25 04:01:56 +00:00
Rusty Russell acf3952acc JSON: remove handling of pre-Adelaide (B:T:N) short_channel_ids.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-09-06 14:19:14 +02:00
trueptolemy 6570c743c4 bitcoin: Add the interface to add multiple outputs to bitcoin transaction 2019-09-05 16:05:36 -05:00
Rusty Russell 5e3e2f4e17 run-secret_eq_consttime: try harder.
This will never be reliable under high load, without making it unable
to detect real errors.  But the test is useful because if we don't
have this test we'll never notice if we break the const-timedness of
our implementation.

So, move the calloc out of the test loop (which seems to make it more
reliable), and then after we've run it, check the 1-minute load
average.  Too high, we don't complain about results.  It's not
perfect, but it's better.

Running 100 times (-O3) serially gave 100 successes with the following results:
	Constant: Within 5% 562-926(832.89+/-73)/1000 times
        Non-constant: More than 5% slower 860-990(956.35+/-26)/1000 times

More importantly, if we swap the const and non-const tests, we get
the expected 100 failures:
	Non-constant: Within 5% 14-79(41.17+/-14)/1000 times
	Constant: More than 5% slower 44-231(111.89+/-33)/1000 times

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-08-29 09:01:48 +02:00
Rusty Russell c9817b225b bitcoin/tx: pass struct amount_sat by copy.
This is the normal convention for this type; it makes using converters
a little easier.  See next patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-08-26 08:44:22 +00:00
Rusty Russell 39b34a35c8 bitcoin/tx.c: don't free witness implicitly.
This causes a crash in mkfunding, which didn't expect it:

    $ devtools/mkfunding 16835ac8c154b616baac524163f41fb0c4f82c7b972ad35d4d6f18d854f6856b 1 0.01btc 253 76edf0c303b9e692da9cb491abedef46ca5b81d32f102eb4648461b239cb0f99 0000000000000000000000000000000000000000000000000000000000000010 0000000000000000000000000000000000000000000000000000000000000020
    # funding sig: 798d96d5a057b5b7797988a855217f41af05ece3ba8278366e2f69763c72e78565d5dd7eeddc0766ddf65557c92b9c52c301f23f94d2cf681860d32153e6ae1e
    # funding witnesses: [
    Aborted (core dumped)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-08-26 08:44:22 +00:00
Karl-Johan Alm 46c184a50e signet: update genesis hash to use new POW-valid version 2019-08-19 07:09:06 +00:00
Rusty Russell f73ae3c198 test: make run-secret_eq_consttime more robust.
This is more reliable under load now: shorten the times so it is
likely to run in a single timeslice, and add a nanosleep so it's
likely to be at the start of the timeslice.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-08-19 01:38:12 +00:00
Rusty Russell 3fa375881a bigsize: make it a proper first-class type.
It doesn't belong in bitcoin, and should not be confused with varint_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-07-31 23:25:59 +00:00
Christian Decker 5dff67900e tx: Add chainparams when deserializing transactions from wire msgs
This is the other origin, besides `bitcoin_tx`, where we create `bitcoin_tx`
instances, so add the context as soon as possible. Sadly I can't weave the
chainparams into the deserialization code since that'd need to change all the
generated wire code as well.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-07-31 23:22:54 +00:00
Christian Decker d14bd286ba bitcoin: Add chainparams to transactions from blocks
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-07-31 23:22:54 +00:00
Christian Decker 9288a7906b tx: Add chainparams to struct bitcoin_tx as context
The way we build transactions, serialize them, and compute fees depends on the
chain we are working on, so let's add some context to the transactions.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-07-31 23:22:54 +00:00
trueptolemy cdbfa4229c bitcoin: Add the `cli_min_supported_version` field in `chainparams`
Set the min supported numeric version of cli as 150000.
2019-07-30 17:38:54 +08:00
Christian Decker 581694fdda devtools: Minor cleanup of the onion command line tool
Simplifying some operations, erroring in some cases and moving to global
defines for constants.

Suggested-by: Rusty Russell <@rustyrussell>
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-07-30 02:14:49 +00:00
Christian Decker 78c7edb889 sphinx: Switch to big-endian number encoding
See https://github.com/lightningnetwork/lightning-rfc/pull/619 and
https://github.com/lightningnetwork/lightning-rfc/pull/619 for discussion.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-07-30 02:14:49 +00:00
Karl-Johan Alm 918e130448 add signet support 2019-07-22 16:38:32 -05:00
Karl-Johan Alm ff2bfe3bdb dynamically generate string of network names 2019-07-22 16:38:32 -05:00
lisa neigut 81c40da3b5 bolt: update to lightning-rfc:636b9f, change opcode reference format
Updates BOLT reference to match spec; replaces OP_CSV ->
OP_CHECKSEQUENCEVERIFY
2019-07-16 06:10:58 +00:00
trueptolemy 285da33e9e Chainparam: Add 'bip70_name' field for blockchain
'bip70_name' is corresponding to the 'chain' field of
the API 'getblockchaininfo'.
At the beginning of lightningd, we use the 'chain' field of 'getblockchaininfo' to check if we are on right blockchain.
2019-07-04 16:13:09 +02:00
lisa neigut 7046d0220c makefiles: move all unit tests under `make check-units`
Isolate unit tests under their own make directive.
2019-06-30 16:41:30 +09:30
Rusty Russell f1b57063f7 bitcoin/tx: use fromwire_fail in pull_bitcoin_tx.
This is the correct way to mark failure: it also sets *max to 0.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-06-21 03:56:59 +00:00
Rusty Russell 711762ec67 bitcoin/tx: separate assertions.
This might help diagnose what's happening with #2759

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-06-21 03:56:59 +00:00
Rusty Russell 09d9abf12e bitcoin/tx: script arg should be const.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-06-06 04:47:44 +00:00
Rusty Russell 664916e815 bitcoin/test: fix up constant time test for secret_eq.
We check that memcmp *isn't* constant time, but that's only true under
-O2 or above: __OPTIMIZE__ doesn't distinguish.

So we need a finer-grained test.  Also reduce verbosity by default.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 15:39:56 -04:00
Christian Decker 0d19d04def wallet: Pass chainparams to address serialization
The chainparams are needed to know the prefixes, so instead of passing down
the testnet, we pass the entire params struct.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-05-10 23:07:49 +00:00
Christian Decker aa9284eaa3 base58: Simplified the address parsing
We were deciding whether an address is a testnet address or not in the parser,
and then checking whether it matches our expectation outside as well. This
just returns the address version instead, and still checks it against our
expectation, but without having the parser need to know about address types.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-05-01 12:37:30 +02:00
Christian Decker 5d185f43a8 base58: Add function to just parse a ripemd160 hash
This is an intermediate step since the only difference between p2pkh and p2sh
is the argument that the parsing functions take, and parsing twice for that
reason alone is quite useless.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-05-01 12:37:30 +02:00
Christian Decker 6d618511fc chainparams: Add p2pkh and p2sh versions to chinparams
Signed-off-by: Christian Decker <decker.christian@gmail.com>
Co-authored-by: bisoge <bisoge@gmx.de>
2019-05-01 12:37:30 +02:00
GreenAddress fb07265663 remove libbase58, use base58 from libwally (#2594)
* remove libbase58, use base58 from libwally

This removes libbase58 and uses libwally instead.

It allocates and then frees some memory, we may want to
add a function in wally that doesn't or override
wally_operations to use tal.

Signed-off-by: Lawrence Nahum lawrence@greenaddress.it
2019-04-30 23:07:31 +02:00
Rusty Russell e0ec9ac521 libwally: update to 0.6.8.
This fixes block parsing on testnet; specifically, non-standard tx versions.

We hit a type bug in libwally (wallt_get_secp_context()) which I had to
work around for the moment, and the updated libsecp adds an optional hash
function arg to the ECDH function.

Fixes: #2563
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-13 18:55:15 +02:00
Rusty Russell 837a095d68 pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.

Results from 5 runs, min-max(mean +/- stddev):
	store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
	38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-09 12:37:16 -07:00
Christian Decker 21a0dad016 wally: Post-migration cleanups
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 509bb2c7ae wally: Remove tx->input and tx->output, wally all the way!
This is what all of this has been working towards: ripping out the handwoven
transaction handling. By removing the custom parsing we can finally switch
over to using `wally_tx` as sole representation of transactions in
memory. The commit is a bit larger but it's mostly removing setters and old
references to the input and output fields.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker d651ce6f3b wally: Migrate main daemon to use wally transactions
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 8d0500228e wally: Add accessor methods for script and amount
These are handled internally in the `wally_tx` and do not conform to our usual
tallocated strings that can by inspected using `tal_bytelen`, and we don't
really want to litter our code with whitelisting comments for the
`amount_sat.satoshis` access, so these just do read-only on the fly conversions.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 30ed2e1a8f wally: Remove unused sha256_tx_for_sig function
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 3ae19479a8 wally: Use libwally only to serialize transactions
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 9fe481b967 wally: Move input amounts into a separate array
The `wally_tx_input`s do not keep track of their input value, which means we
need to track them ourselves if we try to sign these transactions at a later
point in time.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 48006cb674 wally: Switch signatures over to using the wally_tx hash
First step towards decomissioning the handrolled bitcoin_tx operations.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker cc31efd0e8 wally: Migrate run-tx-encode to directly access the wally_tx
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker ce58cba9fe wally: Making extra sure that pulling a TX results in the correct read offset. 2019-04-08 00:00:00 +00:00
Christian Decker 03329a61da wally: Add setters for output amounts, input witnesses and scripts
These are used when grinding the feerate and signing. These are just simple
facades that keep both wally and old style transactions in sync.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker bd2573eafb wally: Build wally transactions in parallel with the old ones
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 3937b17e2b wally: Add a consistency check for old and new style txs
During the migration to `libwally` we want to make absolutely sure that both
transactions are generated identical, and can eventually be switched over.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 60fbf1f7d0 wally: Add shims to generate both transaction versions in parallel
We are slowly migrating towards a wally-transactions only world, but to make
this reviewable we start building both old and new style transactions in
parallel. In a second pass we'll then start removing the old ones and use
libwally only.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 16f72cb160 wally: Migrate version and locktime to libwally tx
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
Christian Decker 9609d762c8 wally: Add wally_tx in bitcoin_tx
Allows us to slowly migrate individual parts.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-04-08 00:00:00 +00:00
trueptolemy 92c08cd861 Fix the version of bip32 private_key generation
We set the version BIP32_VER_TEST_PRIVATE for testnet/regtest
BIP32 privkey generation with libwally-core, and set
BIP32_VER_MAIN_PRIVATE for mainnet.
For litecoin, we also set it like bitcoin else.
2019-03-18 02:47:04 +00:00
Rusty Russell 02faadfb93 amount: make it work with gcc-4.8.
```
In file included from bitcoin/chainparams.h:7:0,from bitcoin/chainparams.c:1:
./common/amount.h:36:11: error: initializer element is not constant
((struct amount_sat){(constant) + AMOUNT_MUST_BE_CONST(constant)})
^
bitcoin/chainparams.c:20:21: note: in expansion of macro ‘AMOUNT_SAT’
.max_funding = AMOUNT_SAT((1 << 24) - 1),
^
./common/amount.h:36:11: error: (near initialization for ‘networks[0].max_funding’)
((struct amount_sat){(constant) + AMOUNT_MUST_BE_CONST(constant)})
^
bitcoin/chainparams.c:20:21: note: in expansion of macro ‘AMOUNT_SAT’
.max_funding = AMOUNT_SAT((1 << 24) - 1),
```

Fixes: #2404
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-27 23:12:50 +00:00
Rusty Russell 38e7d19dd5 Makefile: check for direct amount_sat/amount_msat access.
We need to do it in various places, but we shouldn't do it lightly:
the primitives are there to help us get overflow handling correct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-21 08:01:37 +00:00
Rusty Russell 948ca470ad bitcoin: use amount_sat/amount_msat.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-21 08:01:37 +00:00
Rusty Russell 85b8b25749 bitcoin/chainparams: use amount_sat / amount_msat
Simple changes, but ripples through the code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-21 08:01:37 +00:00
Rusty Russell 269dbe585c wire: move short_channel formatting functions into bitcoin/short_channel_id
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-21 00:44:57 +00:00
Rusty Russell b99293fbb6 short_channel_id: don't accept :-separated in JSON if --allow-deprecated-apis=false
We need to still accept it when parsing the database, but this flag
should allow upgrade testing for devs building on top

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-02-08 16:52:30 -08:00
Rusty Russell 018a3f1d58 short_channel_id: make mk_short_channel_id return a failure.
We had a bug 0ba547ee10 caused by
short_channel_id overflow.  If we'd caught this, we'd have terminated
the peer instead of crashing, so add appropriate checks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-01-21 12:31:06 +01:00
Rusty Russell d69680934e short_channel_id: catch parsing errors.
I upgraded my node with --disable-compat, and a heap of channels closed like:

	CHANNELD_NORMAL:We disagree on short_channel_ids: I have 557653x0x1351, you say 557653x2373x1", 

This is because the scids are strings in the databases, and it failed to parse
them properly.

Now we'll not start if that happens.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-01-21 12:31:06 +01:00
Rusty Russell 9f1f79587e short_channel_id_dir: new primitive for one direction of short_channel_id
Currently only used by gossipd for channel elimination.

Also print them in canonical form (/[01]), so tests need to be
changed.

Suggested-by: @cdecker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-01-15 12:01:38 +01:00
Rusty Russell be64dd84ca waitsendpay: indicate which channel direction the error was.
You can figure this yourself by knowing the route, but it's better to report
it directly here.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-01-15 12:01:38 +01:00
Christian Decker a5ed98a2ea misc: Add a bolt quote to the short_channel_id
[ Formatting fixed for make check-source to find, punctutation fixed --RR ]
Reported-by: Rusty Russell <@rustyrussell>
2019-01-15 03:50:27 +00:00
Christian Decker 659a26ea5a misc: Update short_channel_id representation to use 'x' separators
Reported-by: Alex Bosworth <@alexbosworth>
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2019-01-15 03:50:27 +00:00
Rusty Russell b7da41e674 bitcoin/signature: implement SIGHASH_SINGLE|SIGHASH_ANYONECANPAY for segwit.
option_simplified_commitment wants this for HTLC txs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-12-06 23:11:51 +01:00
Rusty Russell dffe2f516a signature: wrap almost all signatures in struct bitcoin_signature.
This is prep work for when we sign htlc txs with
SIGHASH_SINGLE|SIGHASH_ANYONECANPAY.

We still deal with raw signatures for the htlc txs at the moment, since
we send them like that across the wire, and changing that was simply too
painful (for the moment?).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-12-06 23:11:51 +01:00
Rusty Russell bbc4a599dc bitcoin/script.h: assert the constants in the header are correct.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-12-06 23:11:51 +01:00
Rusty Russell a83e45fec2 bitcoin/signature: create/check tx sigs without mangling the tx.
We currently make sure that all the bitcoin_tx input scripts are NULL
and set the input script of the input we're signing, so we can easily
reuse the tx hashing code for signature checks.  This means that we
sometimes jump through hoops to make sure input scripts are NULL, and
also means that the tx can't be const.

Put more logic inside bitcoin/tx so it can simply ignore things we
don't want to hash.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-12-06 23:11:51 +01:00
Christian Decker e10cde3516 chainparams: Remove index from chainparams
We no longer use it to reference chainparams, so we can remove it completely.
2018-09-14 21:18:11 +02:00
Christian Decker f417dfa0e1 chainparams: Always retrieve chainparams by the chain_hash 2018-09-14 21:18:11 +02:00
Christian Decker 2d7e603ac1 chainparams: Move the BOLT2 quote to the chainparams where we set it 2018-09-14 21:18:11 +02:00
Christian Decker 8d95917e7c chainparams: Add max_funding_satoshi and max_payment_msat to chainparams 2018-09-14 21:18:11 +02:00
Rusty Russell 8340d8c070 secret_eq: remove in favor of constant time variant.
To be safe, we should never memcmp secrets.  We don't do this
currently outside tests, but we're about to.

The tests to prove this as constant time are the tricky bit.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-23 14:46:22 +02:00
Rusty Russell 5cf34d6618 Remove tal_len, use tal_count() or tal_bytelen().
tal_count() is used where there's a type, even if it's char or u8, and
tal_bytelen() is going to replace tal_len() for clarity: it's only needed
where a pointer is void.

We shim tal_bytelen() for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-07-30 11:31:17 +02:00