mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
tx: centralize transaction writing, append amounts for alpha.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
5dc9cd16f9
commit
ae31431168
61
bitcoin/tx.c
61
bitcoin/tx.c
@ -1,6 +1,7 @@
|
|||||||
#include <ccan/crypto/sha256/sha256.h>
|
#include <ccan/crypto/sha256/sha256.h>
|
||||||
#include <ccan/endian/endian.h>
|
#include <ccan/endian/endian.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
|
#include <ccan/read_write_all/read_write_all.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/tal/grab_file/grab_file.h>
|
#include <ccan/tal/grab_file/grab_file.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@ -416,7 +417,7 @@ static struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
|
|||||||
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
||||||
const char *filename)
|
const char *filename)
|
||||||
{
|
{
|
||||||
char *hex;
|
char *hex, *end;
|
||||||
u8 *linear_tx;
|
u8 *linear_tx;
|
||||||
const u8 *p;
|
const u8 *p;
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
@ -429,14 +430,36 @@ struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
|||||||
|
|
||||||
if (strends(hex, "\n"))
|
if (strends(hex, "\n"))
|
||||||
hex[strlen(hex)-1] = '\0';
|
hex[strlen(hex)-1] = '\0';
|
||||||
len = hex_data_size(strlen(hex));
|
|
||||||
|
end = strchr(hex, ':');
|
||||||
|
if (!end)
|
||||||
|
end = hex + strlen(hex);
|
||||||
|
|
||||||
|
len = hex_data_size(end - hex);
|
||||||
p = linear_tx = tal_arr(hex, u8, len);
|
p = linear_tx = tal_arr(hex, u8, len);
|
||||||
if (!hex_decode(hex, strlen(hex), linear_tx, len))
|
if (!hex_decode(hex, end - hex, linear_tx, len))
|
||||||
errx(1, "Bad hex string in %s", filename);
|
errx(1, "Bad hex string in %s", filename);
|
||||||
|
|
||||||
tx = pull_bitcoin_tx(ctx, &p, &len);
|
tx = pull_bitcoin_tx(ctx, &p, &len);
|
||||||
if (!tx)
|
if (!tx)
|
||||||
errx(1, "Bad transaction in %s", filename);
|
errx(1, "Bad transaction in %s", filename);
|
||||||
|
|
||||||
|
/* Optional appended [:input-amount]* */
|
||||||
|
for (len = 0; len < tx->input_count; len++) {
|
||||||
|
if (*end != ':')
|
||||||
|
break;
|
||||||
|
tx->input[len].input_amount = strtoull(end + 1, &end, 10);
|
||||||
|
}
|
||||||
|
if (len == tx->input_count) {
|
||||||
|
if (*end != '\0')
|
||||||
|
errx(1, "Additional input amounts appended to %s",
|
||||||
|
filename);
|
||||||
|
} else {
|
||||||
|
/* Input amounts are compulsory for alpha, to generate sigs */
|
||||||
|
#ifdef ALPHA_TXSTYLE
|
||||||
|
errx(1, "No input amount #%zu in %s", len, filename);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
tal_free(hex);
|
tal_free(hex);
|
||||||
|
|
||||||
return tx;
|
return tx;
|
||||||
@ -465,3 +488,35 @@ bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool write_input_amounts(int fd, const struct bitcoin_tx *tx)
|
||||||
|
{
|
||||||
|
/* Alpha required input amounts, so append them */
|
||||||
|
#ifdef ALPHA_TXSTYLE
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
for (i = 0; i < tx->input_count; i++) {
|
||||||
|
char str[1 + STR_MAX_CHARS(tx->input[i].input_amount)];
|
||||||
|
sprintf(str, ":%llu",
|
||||||
|
(unsigned long long)tx->input[i].input_amount);
|
||||||
|
if (!write_all(fd, str, strlen(str)))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx)
|
||||||
|
{
|
||||||
|
u8 *tx_arr;
|
||||||
|
char *tx_hex;
|
||||||
|
bool ok;
|
||||||
|
|
||||||
|
tx_arr = linearize_tx(NULL, tx);
|
||||||
|
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
||||||
|
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
||||||
|
|
||||||
|
ok = write_all(fd, tx_hex, strlen(tx_hex))
|
||||||
|
&& write_input_amounts(fd, tx);
|
||||||
|
tal_free(tx_arr);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
@ -57,6 +57,8 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
|
|||||||
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
struct bitcoin_tx *bitcoin_tx_from_file(const tal_t *ctx,
|
||||||
const char *filename);
|
const char *filename);
|
||||||
|
|
||||||
|
bool bitcoin_tx_write(int fd, const struct bitcoin_tx *tx);
|
||||||
|
|
||||||
/* Parse hex string to get txid (reversed, a-la bitcoind). */
|
/* Parse hex string to get txid (reversed, a-la bitcoind). */
|
||||||
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
|
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
|
||||||
struct sha256_double *txid);
|
struct sha256_double *txid);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include <ccan/structeq/structeq.h>
|
#include <ccan/structeq/structeq.h>
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
@ -24,9 +23,7 @@ int main(int argc, char *argv[])
|
|||||||
OpenAnchorScriptsigs *ss1, *ss2;
|
OpenAnchorScriptsigs *ss1, *ss2;
|
||||||
struct bitcoin_tx *anchor;
|
struct bitcoin_tx *anchor;
|
||||||
struct sha256_double txid;
|
struct sha256_double txid;
|
||||||
u8 *tx_arr;
|
|
||||||
size_t *inmap, *outmap;
|
size_t *inmap, *outmap;
|
||||||
char *tx_hex;
|
|
||||||
|
|
||||||
err_set_progname(argv[0]);
|
err_set_progname(argv[0]);
|
||||||
|
|
||||||
@ -55,12 +52,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
bitcoin_txid(anchor, &txid);
|
bitcoin_txid(anchor, &txid);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
if (!bitcoin_tx_write(STDOUT_FILENO, anchor))
|
||||||
tx_arr = linearize_tx(ctx, anchor);
|
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out anchor transaction");
|
err(1, "Writing out anchor transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
#include "bitcoin/base58.h"
|
#include "bitcoin/base58.h"
|
||||||
@ -25,11 +24,10 @@ int main(int argc, char *argv[])
|
|||||||
OpenCommitSig *cs2;
|
OpenCommitSig *cs2;
|
||||||
struct bitcoin_tx *anchor, *commit;
|
struct bitcoin_tx *anchor, *commit;
|
||||||
struct sha256_double txid;
|
struct sha256_double txid;
|
||||||
u8 *tx_arr, *subscript;
|
u8 *subscript;
|
||||||
size_t *inmap, *outmap;
|
size_t *inmap, *outmap;
|
||||||
struct pubkey pubkey1, pubkey2;
|
struct pubkey pubkey1, pubkey2;
|
||||||
struct bitcoin_signature sig1, sig2;
|
struct bitcoin_signature sig1, sig2;
|
||||||
char *tx_hex;
|
|
||||||
struct privkey privkey;
|
struct privkey privkey;
|
||||||
bool testnet;
|
bool testnet;
|
||||||
struct sha256 rhash;
|
struct sha256 rhash;
|
||||||
@ -101,11 +99,7 @@ int main(int argc, char *argv[])
|
|||||||
commit->input[0].script_length = tal_count(commit->input[0].script);
|
commit->input[0].script_length = tal_count(commit->input[0].script);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
/* Print it out in hex. */
|
||||||
tx_arr = linearize_tx(ctx, commit);
|
if (!bitcoin_tx_write(STDOUT_FILENO, commit))
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out transaction");
|
err(1, "Writing out transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
#include "bitcoin/base58.h"
|
#include "bitcoin/base58.h"
|
||||||
@ -26,8 +25,7 @@ int main(int argc, char *argv[])
|
|||||||
struct sha256_double anchor_txid;
|
struct sha256_double anchor_txid;
|
||||||
struct bitcoin_signature sig1, sig2;
|
struct bitcoin_signature sig1, sig2;
|
||||||
struct pubkey pubkey1, pubkey2;
|
struct pubkey pubkey1, pubkey2;
|
||||||
u8 *redeemscript, *tx_arr;
|
u8 *redeemscript;
|
||||||
char *tx_hex;
|
|
||||||
CloseChannel *close;
|
CloseChannel *close;
|
||||||
CloseChannelComplete *closecomplete;
|
CloseChannelComplete *closecomplete;
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -92,11 +90,7 @@ int main(int argc, char *argv[])
|
|||||||
close_tx->input[0].script_length = tal_count(close_tx->input[0].script);
|
close_tx->input[0].script_length = tal_count(close_tx->input[0].script);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
/* Print it out in hex. */
|
||||||
tx_arr = linearize_tx(ctx, close_tx);
|
if (!bitcoin_tx_write(STDOUT_FILENO, close_tx))
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out transaction");
|
err(1, "Writing out transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include <ccan/structeq/structeq.h>
|
#include <ccan/structeq/structeq.h>
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
@ -31,8 +30,7 @@ int main(int argc, char *argv[])
|
|||||||
struct privkey privkey;
|
struct privkey privkey;
|
||||||
bool testnet;
|
bool testnet;
|
||||||
struct pubkey pubkey1, pubkey2, outpubkey;
|
struct pubkey pubkey1, pubkey2, outpubkey;
|
||||||
u8 *redeemscript, *tx_arr;
|
u8 *redeemscript;
|
||||||
char *tx_hex;
|
|
||||||
struct sha256 rhash;
|
struct sha256 rhash;
|
||||||
size_t i, p2sh_out;
|
size_t i, p2sh_out;
|
||||||
u64 fee = 10000;
|
u64 fee = 10000;
|
||||||
@ -120,11 +118,7 @@ int main(int argc, char *argv[])
|
|||||||
tx->input[0].script_length = tal_count(tx->input[0].script);
|
tx->input[0].script_length = tal_count(tx->input[0].script);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
/* Print it out in hex. */
|
||||||
tx_arr = linearize_tx(ctx, tx);
|
if (!bitcoin_tx_write(STDOUT_FILENO, tx))
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out transaction");
|
err(1, "Writing out transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
#include "bitcoin/base58.h"
|
#include "bitcoin/base58.h"
|
||||||
@ -32,8 +31,7 @@ int main(int argc, char *argv[])
|
|||||||
struct bitcoin_signature sig1, sig2;
|
struct bitcoin_signature sig1, sig2;
|
||||||
size_t i;
|
size_t i;
|
||||||
struct pubkey pubkey1, pubkey2;
|
struct pubkey pubkey1, pubkey2;
|
||||||
u8 *redeemscript, *tx_arr;
|
u8 *redeemscript;
|
||||||
char *tx_hex;
|
|
||||||
int64_t delta;
|
int64_t delta;
|
||||||
struct sha256 rhash;
|
struct sha256 rhash;
|
||||||
|
|
||||||
@ -121,11 +119,7 @@ int main(int argc, char *argv[])
|
|||||||
commit->input[0].script_length = tal_count(commit->input[0].script);
|
commit->input[0].script_length = tal_count(commit->input[0].script);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
/* Print it out in hex. */
|
||||||
tx_arr = linearize_tx(ctx, commit);
|
if (!bitcoin_tx_write(STDOUT_FILENO, commit))
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out transaction");
|
err(1, "Writing out transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
#include "bitcoin/base58.h"
|
#include "bitcoin/base58.h"
|
||||||
@ -25,11 +24,10 @@ int main(int argc, char *argv[])
|
|||||||
OpenChannel *o1, *o2;
|
OpenChannel *o1, *o2;
|
||||||
Pkt *pkt;
|
Pkt *pkt;
|
||||||
struct bitcoin_tx *commit, *tx;
|
struct bitcoin_tx *commit, *tx;
|
||||||
u8 *tx_arr, *redeemscript, *p2sh;
|
u8 *redeemscript, *p2sh;
|
||||||
size_t i;
|
size_t i;
|
||||||
struct pubkey pubkey1, pubkey2, outpubkey;
|
struct pubkey pubkey1, pubkey2, outpubkey;
|
||||||
struct bitcoin_signature sig;
|
struct bitcoin_signature sig;
|
||||||
char *tx_hex;
|
|
||||||
struct privkey privkey;
|
struct privkey privkey;
|
||||||
bool testnet;
|
bool testnet;
|
||||||
u32 locktime_seconds;
|
u32 locktime_seconds;
|
||||||
@ -123,11 +121,7 @@ int main(int argc, char *argv[])
|
|||||||
tx->input[0].script_length = tal_count(tx->input[0].script);
|
tx->input[0].script_length = tal_count(tx->input[0].script);
|
||||||
|
|
||||||
/* Print it out in hex. */
|
/* Print it out in hex. */
|
||||||
tx_arr = linearize_tx(ctx, tx);
|
if (!bitcoin_tx_write(STDOUT_FILENO, tx))
|
||||||
tx_hex = tal_arr(tx_arr, char, hex_str_size(tal_count(tx_arr)));
|
|
||||||
hex_encode(tx_arr, tal_count(tx_arr), tx_hex, tal_count(tx_hex));
|
|
||||||
|
|
||||||
if (!write_all(STDOUT_FILENO, tx_hex, strlen(tx_hex)))
|
|
||||||
err(1, "Writing out transaction");
|
err(1, "Writing out transaction");
|
||||||
|
|
||||||
tal_free(ctx);
|
tal_free(ctx);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <ccan/opt/opt.h>
|
#include <ccan/opt/opt.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
#include <ccan/err/err.h>
|
#include <ccan/err/err.h>
|
||||||
#include <ccan/read_write_all/read_write_all.h>
|
|
||||||
#include "lightning.pb-c.h"
|
#include "lightning.pb-c.h"
|
||||||
#include "anchor.h"
|
#include "anchor.h"
|
||||||
#include "bitcoin/base58.h"
|
#include "bitcoin/base58.h"
|
||||||
|
Loading…
Reference in New Issue
Block a user