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>
This commit is contained in:
Rusty Russell 2015-07-29 12:00:50 +09:30
parent c4d5a85b4e
commit 2255cb749d
19 changed files with 86 additions and 84 deletions

View File

@ -56,8 +56,8 @@ struct bitcoin_tx *anchor_tx_create(const tal_t *ctx,
return tal_free(tx);
/* Pubkeys both valid, right? */
if (!proto_to_pubkey(o1->anchor->pubkey, &key1)
|| !proto_to_pubkey(o2->anchor->pubkey, &key2))
if (!proto_to_pubkey(o1->commit_key, &key1)
|| !proto_to_pubkey(o2->commit_key, &key2))
return tal_free(tx);
/* Make the 2 of 2 payment for the commitment txs. */

View File

@ -29,9 +29,9 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
tx->input[0].input_amount = input_amount;
/* Outputs goes to final pubkey */
if (!proto_to_pubkey(ours->final, &ourkey))
if (!proto_to_pubkey(ours->final_key, &ourkey))
return tal_free(tx);
if (!proto_to_pubkey(theirs->final, &theirkey))
if (!proto_to_pubkey(theirs->final_key, &theirkey))
return tal_free(tx);
/* delta must make sense. */

View File

@ -32,9 +32,9 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
tx->input[0].input_amount = ours->anchor->total + theirs->anchor->total;
/* Output goes to our final pubkeys */
if (!proto_to_pubkey(ours->final, &ourkey))
if (!proto_to_pubkey(ours->final_key, &ourkey))
return tal_free(tx);
if (!proto_to_pubkey(theirs->final, &theirkey))
if (!proto_to_pubkey(theirs->final_key, &theirkey))
return tal_free(tx);
if (!proto_to_locktime(theirs, &locktime))
@ -57,7 +57,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
tx->output[0].amount += delta;
/* Second output is a P2SH payment to them. */
if (!proto_to_pubkey(theirs->final, &to_me))
if (!proto_to_pubkey(theirs->final_key, &to_me))
return tal_free(tx);
tx->output[1].script = scriptpubkey_p2sh(ctx,
bitcoin_redeem_single(ctx,

View File

@ -1153,7 +1153,7 @@ const ProtobufCMessageDescriptor change__descriptor =
(ProtobufCMessageInit) change__init,
NULL,NULL,NULL /* reserved[123] */
};
static const ProtobufCFieldDescriptor anchor__field_descriptors[6] =
static const ProtobufCFieldDescriptor anchor__field_descriptors[5] =
{
{
"inputs",
@ -1191,18 +1191,6 @@ static const ProtobufCFieldDescriptor anchor__field_descriptors[6] =
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"pubkey",
5,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_MESSAGE,
0, /* quantifier_offset */
offsetof(Anchor, pubkey),
&bitcoin_pubkey__descriptor,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"fee",
8,
@ -1230,19 +1218,18 @@ static const ProtobufCFieldDescriptor anchor__field_descriptors[6] =
};
static const unsigned anchor__field_indices_by_name[] = {
1, /* field[1] = change */
4, /* field[4] = fee */
3, /* field[3] = fee */
0, /* field[0] = inputs */
5, /* field[5] = min_confirms */
3, /* field[3] = pubkey */
4, /* field[4] = min_confirms */
2, /* field[2] = total */
};
static const ProtobufCIntRange anchor__number_ranges[4 + 1] =
{
{ 1, 0 },
{ 4, 2 },
{ 8, 4 },
{ 10, 5 },
{ 0, 6 }
{ 8, 3 },
{ 10, 4 },
{ 0, 5 }
};
const ProtobufCMessageDescriptor anchor__descriptor =
{
@ -1252,15 +1239,27 @@ const ProtobufCMessageDescriptor anchor__descriptor =
"Anchor",
"",
sizeof(Anchor),
6,
5,
anchor__field_descriptors,
anchor__field_indices_by_name,
4, anchor__number_ranges,
(ProtobufCMessageInit) anchor__init,
NULL,NULL,NULL /* reserved[123] */
};
static const ProtobufCFieldDescriptor open_channel__field_descriptors[6] =
static const ProtobufCFieldDescriptor open_channel__field_descriptors[7] =
{
{
"final_key",
1,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_MESSAGE,
0, /* quantifier_offset */
offsetof(OpenChannel, final_key),
&bitcoin_pubkey__descriptor,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"locktime_seconds",
2,
@ -1298,12 +1297,12 @@ static const ProtobufCFieldDescriptor open_channel__field_descriptors[6] =
0,NULL,NULL /* reserved1,reserved2, etc */
},
{
"final",
"commit_key",
5,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_MESSAGE,
0, /* quantifier_offset */
offsetof(OpenChannel, final),
offsetof(OpenChannel, commit_key),
&bitcoin_pubkey__descriptor,
NULL,
0, /* flags */
@ -1335,17 +1334,18 @@ static const ProtobufCFieldDescriptor open_channel__field_descriptors[6] =
},
};
static const unsigned open_channel__field_indices_by_name[] = {
5, /* field[5] = anchor */
4, /* field[4] = commitment_fee */
3, /* field[3] = final */
1, /* field[1] = locktime_blocks */
0, /* field[0] = locktime_seconds */
2, /* field[2] = revocation_hash */
6, /* field[6] = anchor */
4, /* field[4] = commit_key */
5, /* field[5] = commitment_fee */
0, /* field[0] = final_key */
2, /* field[2] = locktime_blocks */
1, /* field[1] = locktime_seconds */
3, /* field[3] = revocation_hash */
};
static const ProtobufCIntRange open_channel__number_ranges[1 + 1] =
{
{ 2, 0 },
{ 0, 6 }
{ 1, 0 },
{ 0, 7 }
};
const ProtobufCMessageDescriptor open_channel__descriptor =
{
@ -1355,7 +1355,7 @@ const ProtobufCMessageDescriptor open_channel__descriptor =
"OpenChannel",
"",
sizeof(OpenChannel),
6,
7,
open_channel__field_descriptors,
open_channel__field_indices_by_name,
1, open_channel__number_ranges,

View File

@ -142,10 +142,6 @@ struct _Anchor
*/
size_t n_inputs;
BitcoinInput **inputs;
/*
* Pubkey for anchor to pay to for commitment tx (p2sh)
*/
BitcoinPubkey *pubkey;
/*
* Any change from anchor (in case we don't want to use them all)
*/
@ -165,7 +161,7 @@ struct _Anchor
};
#define ANCHOR__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&anchor__descriptor) \
, 0,NULL, NULL, NULL, 0, 0, 0 }
, 0,NULL, NULL, 0, 0, 0 }
typedef enum {
@ -184,10 +180,14 @@ struct _OpenChannel
* Hash seed for revoking commitment transactions.
*/
Sha256Hash *revocation_hash;
/*
* Pubkey for anchor to pay to for commitment tx (p2sh)
*/
BitcoinPubkey *commit_key;
/*
* How to pay money to us from commit_tx.
*/
BitcoinPubkey *final;
BitcoinPubkey *final_key;
/*
* How much transaction fee we'll pay for commitment txs.
*/
@ -204,7 +204,7 @@ struct _OpenChannel
};
#define OPEN_CHANNEL__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&open_channel__descriptor) \
, NULL, NULL, 0, NULL, OPEN_CHANNEL__LOCKTIME__NOT_SET, {} }
, NULL, NULL, NULL, 0, NULL, OPEN_CHANNEL__LOCKTIME__NOT_SET, {} }
/*

View File

@ -52,8 +52,6 @@ message change {
message anchor {
// 0 or more unspent inputs we want to use for anchor.
repeated bitcoin_input inputs = 1;
// Pubkey for anchor to pay to for commitment tx (p2sh)
required bitcoin_pubkey pubkey = 5;
// Any change from anchor (in case we don't want to use them all)
optional change change = 2;
// How much transaction fee we'll pay in the anchor tx.
@ -77,8 +75,10 @@ message open_channel {
}
// Hash seed for revoking commitment transactions.
required sha256_hash revocation_hash = 4;
// Pubkey for anchor to pay into commitment tx.
required bitcoin_pubkey commit_key = 5;
// How to pay money to us from commit_tx.
required bitcoin_pubkey final = 5;
required bitcoin_pubkey final_key = 1;
// How much transaction fee we'll pay for commitment txs.
required uint64 commitment_fee = 6;
// The anchor transaction details.

7
pkt.c
View File

@ -33,7 +33,8 @@ static struct pkt *to_pkt(const tal_t *ctx, Pkt__PktCase type, void *msg)
struct pkt *openchannel_pkt(const tal_t *ctx,
const struct sha256 *revocation_hash,
const struct pubkey *to_me,
const struct pubkey *commit,
const struct pubkey *final,
u64 commitment_fee,
u32 rel_locktime_seconds,
Anchor *anchor)
@ -42,10 +43,10 @@ struct pkt *openchannel_pkt(const tal_t *ctx,
/* Required fields must be set: pack functions don't check! */
assert(anchor->inputs);
assert(anchor->pubkey);
o.revocation_hash = sha256_to_proto(ctx, revocation_hash);
o.final = pubkey_to_proto(ctx, to_me);
o.commit_key = pubkey_to_proto(ctx, commit);
o.final_key = pubkey_to_proto(ctx, final);
o.commitment_fee = commitment_fee;
o.anchor = anchor;
o.locktime_case = OPEN_CHANNEL__LOCKTIME_LOCKTIME_SECONDS;

6
pkt.h
View File

@ -32,14 +32,16 @@ struct pubkey;
* openchannel_pkt - create an openchannel message
* @ctx: tal context to allocate off.
* @revocation_hash: first hash value generated from seed.
* @to_me: the pubkey for the commit transactions' P2SH output.
* @commit: the pubkey for the anchor transactions' P2SH output.
* @final: the pubkey for the commit transactions' P2SH output.
* @commitment_fee: the fee to use for commitment tx.
* @rel_locktime_seconds: relative seconds for commitment locktime.
* @anchor: the anchor transaction details.
*/
struct pkt *openchannel_pkt(const tal_t *ctx,
const struct sha256 *revocation_hash,
const struct pubkey *to_me,
const struct pubkey *commit,
const struct pubkey *final,
u64 commitment_fee,
u32 rel_locktime_seconds,
Anchor *anchor);

View File

@ -54,8 +54,8 @@ int main(int argc, char *argv[])
errx(1, "Private key '%s' not on testnet!", argv[4]);
/* Pubkey well-formed? */
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid anchor-2 key");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit_key");
/* Get the transaction ID of the anchor. */
anchor = anchor_tx_create(ctx, o1, o2, &inmap, &outmap);

View File

@ -68,13 +68,13 @@ int main(int argc, char *argv[])
}
/* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
if (!proto_to_pubkey(o1->commit_key, &pubkey2))
errx(1, "Invalid o1 commit pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid o2 final pubkey");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit pubkey");
/* This is what the anchor pays to; figure out whick output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);

View File

@ -53,10 +53,10 @@ int main(int argc, char *argv[])
bitcoin_txid(anchor, &anchor_txid);
/* Pubkeys well-formed? */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey1))
errx(1, "Invalid anchor-1 key");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid anchor-2 key");
if (!proto_to_pubkey(o1->commit_key, &pubkey1))
errx(1, "Invalid o1 commit_key");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit_key");
/* Get delta by accumulting all the updates. */
delta = 0;

View File

@ -70,12 +70,12 @@ int main(int argc, char *argv[])
errx(1, "Invalid bitcoin pubkey '%s'", argv[5]);
/* Get pubkeys */
if (!proto_to_pubkey(o1->final, &pubkey2))
if (!proto_to_pubkey(o1->final_key, &pubkey2))
errx(1, "Invalid o1 final pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->final, &pubkey2))
if (!proto_to_pubkey(o2->final_key, &pubkey2))
errx(1, "Invalid o2 final pubkey");
/* o1 gives us the revocation hash */

View File

@ -58,13 +58,13 @@ int main(int argc, char *argv[])
errx(1, "Private key '%s' not on testnet!", argv[4]);
/* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
errx(1, "Invalid o1 anchor pubkey");
if (!proto_to_pubkey(o1->commit_key, &pubkey2))
errx(1, "Invalid o1 commit pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid o2 anchor pubkey");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit pubkey");
/* Their signature comes from open-commit or from update-accept. */
sig2.stype = SIGHASH_ALL;

View File

@ -74,12 +74,12 @@ int main(int argc, char *argv[])
errx(1, "Invalid bitcoin pubkey '%s'", argv[6]);
/* Get pubkeys */
if (!proto_to_pubkey(o1->final, &pubkey2))
if (!proto_to_pubkey(o1->final_key, &pubkey2))
errx(1, "Invalid o1 final pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->final, &pubkey2))
if (!proto_to_pubkey(o2->final_key, &pubkey2))
errx(1, "Invalid o2 final pubkey");
/* Now, which commit output? Match redeem script. */

View File

@ -132,7 +132,6 @@ int main(int argc, char *argv[])
anchor.n_inputs = (argc - 6);
anchor.inputs = tal_arr(ctx, BitcoinInput *, anchor.n_inputs);
anchor.pubkey = pubkey_to_proto(ctx, &commitkey);
total_in = 0;
for (i = 0; i < anchor.n_inputs; i++) {
@ -160,7 +159,7 @@ int main(int argc, char *argv[])
sha256(&revocation_hash,
revocation_hash.u.u8, sizeof(revocation_hash.u.u8));
pkt = openchannel_pkt(ctx, &revocation_hash, &outkey,
pkt = openchannel_pkt(ctx, &revocation_hash, &commitkey, &outkey,
commit_tx_fee, locktime_seconds, &anchor);
if (!write_all(STDOUT_FILENO, pkt, pkt_totlen(pkt)))

View File

@ -74,7 +74,7 @@ int main(int argc, char *argv[])
(long long)o2->commitment_fee);
/* Their pubkey must be valid */
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid public open-channel-file2");
/* Sign it for them. */

View File

@ -76,13 +76,13 @@ int main(int argc, char *argv[])
revocation_hash.u.u8, sizeof(revocation_hash.u.u8));
/* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
if (!proto_to_pubkey(o1->commit_key, &pubkey2))
errx(1, "Invalid o1 commit pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid o2 final pubkey");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit pubkey");
/* This is what the anchor pays to; figure out whick output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);

View File

@ -89,10 +89,10 @@ int main(int argc, char *argv[])
errx(1, "Their preimage was incorrect");
/* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey1))
if (!proto_to_pubkey(o1->commit_key, &pubkey1))
errx(1, "Invalid o1 commit pubkey");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid o2 final pubkey");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit pubkey");
/* This is what the anchor pays to; figure out whick output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);

View File

@ -80,13 +80,13 @@ int main(int argc, char *argv[])
shachain_from_seed(&seed, argc - 7 - 1, &preimage);
/* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
if (!proto_to_pubkey(o1->commit_key, &pubkey2))
errx(1, "Invalid o1 commit pubkey");
if (pubkey_len(&pubkey1) != pubkey_len(&pubkey2)
|| memcmp(pubkey1.key, pubkey2.key, pubkey_len(&pubkey2)) != 0)
errx(1, "o1 pubkey != this privkey");
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid o2 final pubkey");
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit pubkey");
/* This is what the anchor pays to; figure out whick output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
@ -115,7 +115,7 @@ int main(int argc, char *argv[])
errx(1, "Delta too large");
/* Their pubkey must be valid */
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid public open-channel-file2");
/* Sign it for them. */