Commit Graph

38 Commits

Author SHA1 Message Date
Rusty Russell b423b33236 protocol: implement close as per BOLT #2.
We don't actually implement closing when we have HTLCs (we should
allow it, as that's what the clearing phase is for), since soon we'll
rewrite HTLC to match the async HTLC protocol of BOLT #2.

Note that this folds the close paths, using a simple check if we have
a close transaction.  That's a slight state layer violation, but
reduces code duplication.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:09:41 +10:30
Rusty Russell b017ca1240 protocol: include next revocation hash in open packet.
This means we send the first two revocation hashes; this is important
once we move to a commit model as we need to send (unsolicited) the
signature for the *next* commit tx so we need its commit hash.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:08:35 +10:30
Rusty Russell cb0cc80984 patch remove-timeout.patch 2016-03-15 17:07:12 +10:30
Rusty Russell 4f67b59c26 protocol: rename routefail to fail.
It's a generic "I couldn't complete this" failure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:41:15 +10:30
Rusty Russell 3494cbfb7c protocol: remove update message
We never do unilateral transfers, except via an HTLC.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-08 10:40:15 +10:30
Rusty Russell 74f294e36c daemon: encrypted communication (version 3)
After useful feedback from Anthony Towns and Mats Jerratsch (of
thunder.network fame), this is the third version of inter-node crypto.

1) First, each side sends a 33-byte session pubkey.  This is a
   bitcoin-style compressed EC key, unique for each session.
  
2) ECDH is used to derive a shared secret.  From this we generate
   the following transmission encoding parameters for each side:
   Session AES-128 key: SHA256(shared-secret || my-sessionpubkey || 0)
   Session HMAC key: SHA256(shared-secret || my-sessionpubkey || 1)
   IV for AES: SHA256(shared-secret || my-sessionpubkey || 2)

3) All packets from then on are encrypted of form:
	/* HMAC, covering totlen and data */
	struct sha256 hmac;
	/* Total data transmitted (including this). */
	le64 totlen;
	/* Encrypted contents, rounded up to 16 byte boundary. */
	u8 data[];

4) The first packet is an Authenticate protobuf, containing this node's
   pubkey, and a bitcoin-style EC signature of the other side's session
   pubkey.

5) Unknown protobuf fields are handled in the protocol as follows
   (including in the initial Authenticate packet):

   1) Odd numbered fields are optional, and backwards compatible.
   2) Even numbered fields are required; abort if you get one.

Currently both sides just send an error packet "hello" after the
handshake, and make sure they receive the same.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 31459d6cd2 protocol: rename update_complete_htlc to update_fulfill_htlc.
Complete was an overloaded word.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-25 11:51:18 +09:30
Rusty Russell 02a563ba82 protocol: renumber.
Use lower numbers and clean up.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-24 15:02:54 +09:30
Rusty Russell fefe2deea2 protocol: add a third leg to the close operation.
This gives us a neat way to know the other end has received our sig.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-24 15:02:52 +09:30
Rusty Russell d00eeded9f protocol: split message update_remove_htlc into update_timedout_htlc and update_routefail_htlc, remove update_remove_htlc_delay.
For the moment, there's no way to remove an in-progress HTLC before
it's timed out.  The other side can remove it with a routefail, but
you can't push for it to be removed.

We may add that later, but by definition it's only a polited request, and
normally we should rely on timeouts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-24 15:02:49 +09:30
Rusty Russell 807ac38308 protocol: add decline response for HTLC.
We don't have to accept an HTLC they offer; we might not be able to
route it, or it might offer insufficient payment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-24 15:02:46 +09:30
Rusty Russell 5bc22f0368 protocol: increase HTLC precision to 1/1000 satoshi.
This gets truncated for on-chain transactions (thus, rounding may
contribute to fees).

This also means we currently have an upper bound of 0.04 BTC per HTLC;
this can be increased later if required.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-09-24 15:00:47 +09:30
Rusty Russell 7f21695a63 protocol: rename locktime fields to "delay" and "expiry"
For open transactions, locktime is a delay we require on the other
side's to-self commit transaction outputs to ensure we can cut them
off if necessary.

For HTLCs, it's an absolute expiry time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-08-07 12:45:30 +09:30
Rusty Russell 3325c7320d protocol: add HTLC
Proposing an HTLC follows the same sequence as proposing a normal
update.  There's also requests to complete and remove HTLCs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-08-07 12:45:30 +09:30
Rusty Russell 81bced330a protocol: move locktime into its own message type.
We're going to want this for HTLC times, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-08-07 12:45:30 +09:30
Rusty Russell de051f0df9 protocol: add close fee logic.
The closer proposes the fee.  This is simple, at least.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 16:17:08 +09:30
Rusty Russell 3260fb2ed1 protocol: add commitment fee logic.
Both sides elect a commitment fee, and the lowest is chosen.  That means
you can't game the other side (but if you offer too low, then can error
out of course).

Fees are split 50-50 if possible: originally the whole fee has to be
paid by the (single) funder.  Neither side can withdraw funds which
would make them unable to pay fees.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 16:16:24 +09:30
Rusty Russell eac3af06f1 protocol: switch to single-funder anchor model.
Most complex change was gather_updates(), which handles all the "what
is the current state of the channel" logic for our dumb test utils.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 16:14:28 +09:30
Rusty Russell 2255cb749d protocol: move commitment key to open_channel message.
And rename final to final_key to be clearer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 12:00:50 +09:30
Rusty Russell c4d5a85b4e protocol: remove tx_version field.
It's trivial to add later as an optional field.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 12:00:49 +09:30
Rusty Russell 1a46334bd7 protocol: remove new anchor ops.
We didn't implement them yet, and they'll need revision anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-24 16:00:10 +09:30
Rusty Russell 8a4246cb36 permute: use BIP69 order.
It's a canonical ordering, rather than a random shuffle.  Far simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-20 14:07:44 +09:30
Rusty Russell a663dc322c Remove signature leaking.
Alpha has segregated witnesses, so txid doesn't include the inputs.
That means we can create the first commit transaction before we sign
the anchor tx.

Bitcoin will need to do this differently: presumbly via new sipops
(a OP_CHECKSIG2VERIFY I imagine).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-03 14:00:54 +09:30
Rusty Russell 6389dbd455 protocol: restore locktime in blocks option.
This could be used by an always-on node to agressively shorten their timeouts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-12 13:06:33 +09:30
Rusty Russell ed3f0115d6 protocol: fix horribly thinko, keep own secrets.
I had each side using the other side's hash secret.  That's a very
dumb idea, since it means you can steal from a unilateral close!

A's secret applies to A's commit transaction: it needs the
secret and B's final signature to steal funds, and that should
never happen (since A doesn't have the B's final signature, and
once A has given B the secret, they never broadcast the commit tx).

This makes the update a 4 step dance, since you need the new
revocation hash to make the other side's TX to sign.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-10 21:39:45 +09:30
Rusty Russell 38f7a23064 close-channel: create message to mutually close channel.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-08 14:46:41 +09:30
Rusty Russell 4dd6b8e385 lightning.proto: Rename 'to_me' to 'final', use for commit_tx.
This is where the commit tx outputs should pay to.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-08 06:29:15 +09:30
Rusty Russell d30c470c7d anchor: change is not an arbitrary output, but a pubkey we p2sh to.
Gets rid of the last pubkey user; they're generally deprecated.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-05 17:13:26 +09:30
Rusty Russell 29d0cdc3cd open-channel: use pubkey, not arbitrary script for final outout address.
We need to pay to this from two places: on their side, it's a simple
payment, on our side, it's a complex timeout-or-mutual-or-hval script,
which doesn't lend itself to arbitrary scripts.

Use P2SH, of course.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-05 11:37:27 +09:30
Rusty Russell a6b08dc393 protocol: add revocation hash to update_accept for other commit tx.
And clarify that we're going to be closing the anchor tx directly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-05 11:04:04 +09:30
Rusty Russell 67f98fe6b6 Rename "anchor sig" to "anchor scriptsigs" to be clearer.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-04 12:10:01 +09:30
Rusty Russell eec612af9f Use raw 64-byte signatures in protocol.
DER encoding introduces problems for non-canonical encodings; we should
do that only at the lightning<->bitcoin interface.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-06-01 13:04:00 +09:30
Rusty Russell bf38ca052e open-commit-sig: create signature for commit tx.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-30 20:12:14 +09:30
Rusty Russell 8d31902785 Remove locktime-in-blocks option.
Needless complexity, AFAICT.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-30 20:12:14 +09:30
Rusty Russell 10133575b9 leak-anchor-sigs: the horrible hack until we have normalized txs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-30 20:12:14 +09:30
Rusty Russell 670670f138 open-anchor-sig, many fixes for open-channel.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-29 10:51:16 +09:30
Rusty Russell 94254e7c3f open_channel: say what version txs we'll create.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-27 11:42:05 +09:30
Rusty Russell 1c4fdce514 Initial silly cmdline util to create an openchannel packet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-05-26 14:08:38 +09:30