|
|
@ -11,6 +11,8 @@ |
|
|
|
#include <ccan/crypto/sha256/sha256.h> |
|
|
|
#include <ccan/mem/mem.h> |
|
|
|
#include <ccan/str/hex/hex.h> |
|
|
|
#include <ccan/structeq/structeq.h> |
|
|
|
#include <inttypes.h> |
|
|
|
|
|
|
|
#define FIXME_STUB(peer) do { log_broken((peer)->dstate->base_log, "%s:%u: Implement %s!", __FILE__, __LINE__, __func__); abort(); } while(0) |
|
|
|
|
|
|
@ -99,7 +101,7 @@ Pkt *pkt_anchor(const tal_t *ctx, const struct peer *peer) |
|
|
|
a->amount = peer->anchor.satoshis; |
|
|
|
|
|
|
|
/* Sign their commit sig */ |
|
|
|
peer_sign_theircommit(peer, &sig); |
|
|
|
peer_sign_theircommit(peer, peer->them.commit, &sig); |
|
|
|
a->commit_sig = signature_to_proto(a, &sig); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_OPEN_ANCHOR, a); |
|
|
@ -115,7 +117,7 @@ Pkt *pkt_open_commit_sig(const tal_t *ctx, const struct peer *peer) |
|
|
|
dump_tx("Creating sig for:", peer->them.commit); |
|
|
|
dump_key("Using key:", &peer->us.commitkey); |
|
|
|
|
|
|
|
peer_sign_theircommit(peer, &sig); |
|
|
|
peer_sign_theircommit(peer, peer->them.commit, &sig); |
|
|
|
s->sig = signature_to_proto(s, &sig); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_OPEN_COMMIT_SIG, s); |
|
|
@ -132,7 +134,16 @@ Pkt *pkt_open_complete(const tal_t *ctx, const struct peer *peer) |
|
|
|
Pkt *pkt_htlc_update(const tal_t *ctx, const struct peer *peer, |
|
|
|
const struct htlc_progress *htlc_prog) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
UpdateAddHtlc *u = tal(ctx, UpdateAddHtlc); |
|
|
|
|
|
|
|
update_add_htlc__init(u); |
|
|
|
|
|
|
|
u->revocation_hash = sha256_to_proto(u, &htlc_prog->our_revocation_hash); |
|
|
|
u->amount_msat = htlc_prog->msatoshis; |
|
|
|
u->r_hash = sha256_to_proto(u, &htlc_prog->rhash); |
|
|
|
u->expiry = abs_locktime_to_proto(u, &htlc_prog->expiry); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_UPDATE_ADD_HTLC, u); |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *pkt_htlc_fulfill(const tal_t *ctx, const struct peer *peer, |
|
|
@ -155,17 +166,50 @@ Pkt *pkt_htlc_routefail(const tal_t *ctx, const struct peer *peer, |
|
|
|
|
|
|
|
Pkt *pkt_update_accept(const tal_t *ctx, const struct peer *peer) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
UpdateAccept *u = tal(ctx, UpdateAccept); |
|
|
|
const struct htlc_progress *cur = peer->current_htlc; |
|
|
|
struct signature sig; |
|
|
|
|
|
|
|
update_accept__init(u); |
|
|
|
|
|
|
|
dump_tx("Signing tx", cur->their_commit); |
|
|
|
peer_sign_theircommit(peer, cur->their_commit, &sig); |
|
|
|
u->sig = signature_to_proto(u, &sig); |
|
|
|
u->revocation_hash |
|
|
|
= sha256_to_proto(u, &cur->our_revocation_hash); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_UPDATE_ACCEPT, u); |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *pkt_update_signature(const tal_t *ctx, const struct peer *peer) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
UpdateSignature *u = tal(ctx, UpdateSignature); |
|
|
|
const struct htlc_progress *cur = peer->current_htlc; |
|
|
|
struct signature sig; |
|
|
|
struct sha256 preimage; |
|
|
|
|
|
|
|
update_signature__init(u); |
|
|
|
|
|
|
|
peer_sign_theircommit(peer, cur->their_commit, &sig); |
|
|
|
u->sig = signature_to_proto(u, &sig); |
|
|
|
assert(peer->num_htlcs > 0); |
|
|
|
peer_get_revocation_preimage(peer, peer->num_htlcs-1, &preimage); |
|
|
|
u->revocation_preimage = sha256_to_proto(u, &preimage); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_UPDATE_SIGNATURE, u); |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *pkt_update_complete(const tal_t *ctx, const struct peer *peer) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
UpdateComplete *u = tal(ctx, UpdateComplete); |
|
|
|
struct sha256 preimage; |
|
|
|
|
|
|
|
update_complete__init(u); |
|
|
|
|
|
|
|
peer_get_revocation_preimage(peer, peer->num_htlcs-1, &preimage); |
|
|
|
u->revocation_preimage = sha256_to_proto(u, &preimage); |
|
|
|
|
|
|
|
return make_pkt(ctx, PKT__PKT_UPDATE_COMPLETE, u); |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *pkt_err(const tal_t *ctx, const char *msg, ...) |
|
|
@ -317,8 +361,49 @@ Pkt *accept_pkt_htlc_update(const tal_t *ctx, |
|
|
|
struct peer *peer, const Pkt *pkt, |
|
|
|
Pkt **decline) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
const UpdateAddHtlc *u = pkt->update_add_htlc; |
|
|
|
struct htlc_progress *cur = tal(peer, struct htlc_progress); |
|
|
|
Pkt *err; |
|
|
|
|
|
|
|
cur->msatoshis = u->amount_msat; |
|
|
|
proto_to_sha256(u->r_hash, &cur->rhash); |
|
|
|
proto_to_sha256(u->revocation_hash, &cur->their_revocation_hash); |
|
|
|
if (!proto_to_abs_locktime(u->expiry, &cur->expiry)) { |
|
|
|
err = pkt_err(ctx, "Invalid HTLC expiry"); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
cur->cstate = copy_funding(cur, peer->cstate); |
|
|
|
if (!funding_delta(peer->them.offer_anchor == CMD_OPEN_WITH_ANCHOR, |
|
|
|
peer->anchor.satoshis, |
|
|
|
0, cur->msatoshis, |
|
|
|
&cur->cstate->b, &cur->cstate->a)) { |
|
|
|
err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis", |
|
|
|
cur->msatoshis); |
|
|
|
goto fail; |
|
|
|
} |
|
|
|
/* Add the htlc to their side of channel. */ |
|
|
|
funding_add_htlc(&cur->cstate->b, cur->msatoshis, |
|
|
|
&cur->expiry, &cur->rhash); |
|
|
|
|
|
|
|
peer_get_revocation_hash(peer, peer->num_htlcs+1, |
|
|
|
&cur->our_revocation_hash); |
|
|
|
|
|
|
|
/* Now we create the commit tx pair. */ |
|
|
|
make_commit_txs(cur, peer, &cur->our_revocation_hash, |
|
|
|
&cur->their_revocation_hash, |
|
|
|
cur->cstate, |
|
|
|
&cur->our_commit, &cur->their_commit); |
|
|
|
|
|
|
|
/* FIXME: Fees must be sufficient. */ |
|
|
|
*decline = NULL; |
|
|
|
assert(!peer->current_htlc); |
|
|
|
peer->current_htlc = cur; |
|
|
|
return NULL; |
|
|
|
|
|
|
|
fail: |
|
|
|
tal_free(cur); |
|
|
|
return err; |
|
|
|
}; |
|
|
|
|
|
|
|
Pkt *accept_pkt_htlc_routefail(const tal_t *ctx, |
|
|
|
struct peer *peer, const Pkt *pkt) |
|
|
@ -338,23 +423,124 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx, |
|
|
|
FIXME_STUB(peer); |
|
|
|
} |
|
|
|
|
|
|
|
static u64 total_funds(const struct channel_oneside *c) |
|
|
|
{ |
|
|
|
u64 total = (u64)c->pay_msat + c->fee_msat; |
|
|
|
size_t i, n = tal_count(c->htlcs); |
|
|
|
|
|
|
|
for (i = 0; i < n; i++) |
|
|
|
total += c->htlcs[i].msatoshis; |
|
|
|
return total; |
|
|
|
} |
|
|
|
|
|
|
|
static void update_to_new_htlcs(struct peer *peer) |
|
|
|
{ |
|
|
|
struct htlc_progress *cur = peer->current_htlc; |
|
|
|
|
|
|
|
/* FIXME: Add to shachain too. */ |
|
|
|
|
|
|
|
/* HTLCs can't change total balance in channel! */ |
|
|
|
if (total_funds(&peer->cstate->a) + total_funds(&peer->cstate->b) |
|
|
|
!= total_funds(&cur->cstate->a) + total_funds(&cur->cstate->b)) |
|
|
|
fatal("Illegal funding transition from %u/%u (total %"PRIu64")" |
|
|
|
" to %u/%u (total %"PRIu64")", |
|
|
|
peer->cstate->a.pay_msat, peer->cstate->a.fee_msat, |
|
|
|
total_funds(&peer->cstate->a), |
|
|
|
peer->cstate->b.pay_msat, peer->cstate->b.fee_msat, |
|
|
|
total_funds(&peer->cstate->b)); |
|
|
|
|
|
|
|
/* Now, we consider this channel_state current one. */ |
|
|
|
tal_free(peer->cstate); |
|
|
|
peer->cstate = tal_steal(peer, cur->cstate); |
|
|
|
|
|
|
|
tal_free(peer->us.commit); |
|
|
|
peer->us.commit = tal_steal(peer, cur->our_commit); |
|
|
|
/* FIXME: Save their old commit details, to steal funds. */ |
|
|
|
tal_free(peer->them.commit); |
|
|
|
peer->them.commit = tal_steal(peer, cur->their_commit); |
|
|
|
peer->us.revocation_hash = cur->our_revocation_hash; |
|
|
|
peer->them.revocation_hash = cur->their_revocation_hash; |
|
|
|
|
|
|
|
peer->num_htlcs++; |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *accept_pkt_update_accept(const tal_t *ctx, |
|
|
|
struct peer *peer, const Pkt *pkt) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
const UpdateAccept *a = pkt->update_accept; |
|
|
|
struct htlc_progress *cur = peer->current_htlc; |
|
|
|
|
|
|
|
proto_to_sha256(a->revocation_hash, &cur->their_revocation_hash); |
|
|
|
|
|
|
|
cur->their_sig.stype = SIGHASH_ALL; |
|
|
|
if (!proto_to_signature(a->sig, &cur->their_sig.sig)) |
|
|
|
return pkt_err(ctx, "Malformed signature"); |
|
|
|
|
|
|
|
/* Now we can make commit tx pair. */ |
|
|
|
make_commit_txs(cur, peer, &cur->our_revocation_hash, |
|
|
|
&cur->their_revocation_hash, |
|
|
|
cur->cstate, |
|
|
|
&cur->our_commit, &cur->their_commit); |
|
|
|
|
|
|
|
/* Their sig should sign our new commit tx. */ |
|
|
|
if (!check_tx_sig(peer->dstate->secpctx, |
|
|
|
cur->our_commit, 0, |
|
|
|
peer->anchor.redeemscript, |
|
|
|
tal_count(peer->anchor.redeemscript), |
|
|
|
&peer->them.commitkey, |
|
|
|
&cur->their_sig)) |
|
|
|
return pkt_err(ctx, "Bad signature"); |
|
|
|
|
|
|
|
/* Our next step will be to send the revocation preimage, so
|
|
|
|
* update to new HTLC now so we never use the old one. */ |
|
|
|
update_to_new_htlcs(peer); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
static bool check_preimage(const Sha256Hash *preimage, const struct sha256 *hash) |
|
|
|
{ |
|
|
|
struct sha256 h; |
|
|
|
|
|
|
|
proto_to_sha256(preimage, &h); |
|
|
|
sha256(&h, &h, sizeof(h)); |
|
|
|
return structeq(&h, hash); |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *accept_pkt_update_complete(const tal_t *ctx, |
|
|
|
struct peer *peer, const Pkt *pkt) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
/* FIXME: Check preimage against old tx! */ |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *accept_pkt_update_signature(const tal_t *ctx, |
|
|
|
struct peer *peer, |
|
|
|
const Pkt *pkt) |
|
|
|
{ |
|
|
|
FIXME_STUB(peer); |
|
|
|
const UpdateSignature *s = pkt->update_signature; |
|
|
|
struct htlc_progress *cur = peer->current_htlc; |
|
|
|
|
|
|
|
cur->their_sig.stype = SIGHASH_ALL; |
|
|
|
if (!proto_to_signature(s->sig, &cur->their_sig.sig)) |
|
|
|
return pkt_err(ctx, "Malformed signature"); |
|
|
|
|
|
|
|
/* Their sig should sign our new commit tx. */ |
|
|
|
if (!check_tx_sig(peer->dstate->secpctx, |
|
|
|
cur->our_commit, 0, |
|
|
|
peer->anchor.redeemscript, |
|
|
|
tal_count(peer->anchor.redeemscript), |
|
|
|
&peer->them.commitkey, |
|
|
|
&cur->their_sig)) |
|
|
|
return pkt_err(ctx, "Bad signature"); |
|
|
|
|
|
|
|
/* Check their revocation preimage. */ |
|
|
|
if (!check_preimage(s->revocation_preimage, &peer->them.revocation_hash)) |
|
|
|
return pkt_err(ctx, "Bad revocation preimage"); |
|
|
|
|
|
|
|
/* Our next step will be to send the revocation preimage, so
|
|
|
|
* update to new HTLC now so we never use the old one. */ |
|
|
|
update_to_new_htlcs(peer); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
Pkt *accept_pkt_close(const tal_t *ctx, struct peer *peer, const Pkt *pkt) |
|
|
|