2017-10-24 04:24:43 +02:00
|
|
|
#include <assert.h>
|
2017-06-14 10:29:10 +02:00
|
|
|
#include <bitcoin/address.h>
|
2017-02-07 02:44:21 +01:00
|
|
|
#include <bitcoin/base58.h>
|
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/pubkey.h>
|
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
2017-02-07 02:44:21 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
2019-02-21 04:45:55 +01:00
|
|
|
#include "../amount.c"
|
2017-02-07 02:44:21 +01:00
|
|
|
#define SUPERVERBOSE printf
|
2019-02-21 04:45:55 +01:00
|
|
|
#include "../funding_tx.c"
|
2017-03-07 02:01:55 +01:00
|
|
|
#undef SUPERVERBOSE
|
2019-02-21 04:45:55 +01:00
|
|
|
#include "../key_derive.c"
|
|
|
|
#include "../type_to_string.c"
|
|
|
|
#include "../permute_tx.c"
|
|
|
|
#include "../utxo.c"
|
2018-12-03 00:02:11 +01:00
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
2019-02-21 03:39:51 +01:00
|
|
|
/* Generated stub for fromwire_amount_sat */
|
|
|
|
struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_amount_sat called!\n"); abort(); }
|
2018-12-03 00:02:11 +01:00
|
|
|
/* Generated stub for fromwire_bitcoin_txid */
|
|
|
|
void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
|
|
|
struct bitcoin_txid *txid UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_bitcoin_txid called!\n"); abort(); }
|
|
|
|
/* Generated stub for fromwire_bool */
|
|
|
|
bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_bool called!\n"); abort(); }
|
2019-04-08 11:58:32 +02:00
|
|
|
/* Generated stub for fromwire_node_id */
|
|
|
|
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
|
2018-12-03 00:02:11 +01:00
|
|
|
/* Generated stub for fromwire_pubkey */
|
|
|
|
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
|
|
|
|
/* Generated stub for fromwire_u32 */
|
|
|
|
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); }
|
|
|
|
/* Generated stub for fromwire_u64 */
|
|
|
|
u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); }
|
2019-02-21 03:39:51 +01:00
|
|
|
/* Generated stub for towire_amount_sat */
|
|
|
|
void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
|
2018-12-03 00:02:11 +01:00
|
|
|
/* Generated stub for towire_bitcoin_txid */
|
|
|
|
void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); }
|
|
|
|
/* Generated stub for towire_bool */
|
|
|
|
void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_bool called!\n"); abort(); }
|
2019-04-08 11:58:32 +02:00
|
|
|
/* Generated stub for towire_node_id */
|
|
|
|
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
|
2018-12-03 00:02:11 +01:00
|
|
|
/* Generated stub for towire_pubkey */
|
|
|
|
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
|
|
|
|
/* Generated stub for towire_u32 */
|
|
|
|
void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_u32 called!\n"); abort(); }
|
|
|
|
/* Generated stub for towire_u64 */
|
|
|
|
void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_u64 called!\n"); abort(); }
|
|
|
|
/* AUTOGENERATED MOCKS END */
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
static struct sha256 sha256_from_hex(const char *hex)
|
|
|
|
{
|
|
|
|
struct sha256 sha256;
|
|
|
|
if (strstarts(hex, "0x"))
|
|
|
|
hex += 2;
|
|
|
|
if (!hex_decode(hex, strlen(hex), &sha256, sizeof(sha256)))
|
|
|
|
abort();
|
|
|
|
return sha256;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct privkey privkey_from_hex(const char *hex)
|
|
|
|
{
|
|
|
|
struct privkey pk;
|
|
|
|
size_t len;
|
|
|
|
if (strstarts(hex, "0x"))
|
|
|
|
hex += 2;
|
|
|
|
len = strlen(hex);
|
|
|
|
if (len == 66 && strends(hex, "01"))
|
|
|
|
len -= 2;
|
|
|
|
if (!hex_decode(hex, len, &pk, sizeof(pk)))
|
|
|
|
abort();
|
|
|
|
return pk;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-02-07 02:44:21 +01:00
|
|
|
struct bitcoin_tx *input, *funding;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat fee, change;
|
2017-02-07 02:44:21 +01:00
|
|
|
struct pubkey local_funding_pubkey, remote_funding_pubkey;
|
|
|
|
struct privkey input_privkey;
|
|
|
|
struct pubkey inputkey;
|
|
|
|
bool testnet;
|
2017-03-07 01:58:09 +01:00
|
|
|
struct utxo utxo;
|
2018-03-15 07:10:20 +01:00
|
|
|
const struct utxo **utxomap;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding_sat;
|
2017-03-07 02:03:24 +01:00
|
|
|
u16 funding_outnum;
|
2019-03-20 16:45:21 +01:00
|
|
|
u8 *subscript, *script;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature sig;
|
2017-06-14 10:29:10 +02:00
|
|
|
struct bitcoin_address addr;
|
2019-03-25 11:35:56 +01:00
|
|
|
struct amount_sat tmpamt;
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
|
|
|
| SECP256K1_CONTEXT_SIGN);
|
2018-03-15 07:10:20 +01:00
|
|
|
setup_tmpctx();
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* Block 1 coinbase transaction: 01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0100f2052a010000001976a9143ca33c2e4446f4a305f23c80df8ad1afdcf652f988ac00000000
|
|
|
|
*/
|
|
|
|
input = bitcoin_tx_from_hex(tmpctx,
|
|
|
|
"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0100f2052a010000001976a9143ca33c2e4446f4a305f23c80df8ad1afdcf652f988ac00000000",
|
|
|
|
strlen("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0100f2052a010000001976a9143ca33c2e4446f4a305f23c80df8ad1afdcf652f988ac00000000"));
|
|
|
|
assert(input);
|
|
|
|
|
|
|
|
/* BOLT #3:
|
2017-10-09 03:22:13 +02:00
|
|
|
* Block 1 coinbase privkey: 6bd078650fcee8444e4e09825227b801a1ca928debb750eb36e6d56124bb20e801
|
2017-02-07 02:44:22 +01:00
|
|
|
* # privkey in base58: cRCH7YNcarfvaiY1GWUKQrRGmoezvfAiqHtdRvxe16shzbd7LDMz
|
2017-02-07 02:44:21 +01:00
|
|
|
*/
|
|
|
|
if (!key_from_base58("cRCH7YNcarfvaiY1GWUKQrRGmoezvfAiqHtdRvxe16shzbd7LDMz", strlen("cRCH7YNcarfvaiY1GWUKQrRGmoezvfAiqHtdRvxe16shzbd7LDMz"),
|
|
|
|
&testnet, &input_privkey, &inputkey))
|
|
|
|
abort();
|
|
|
|
assert(testnet);
|
2017-10-09 03:22:13 +02:00
|
|
|
printf("* Block 1 coinbase privkey: %s\n",
|
2017-02-07 02:44:21 +01:00
|
|
|
type_to_string(tmpctx, struct privkey, &input_privkey));
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The funding transaction is paid to the following pubkeys:
|
2017-02-21 05:45:28 +01:00
|
|
|
*
|
2017-02-07 02:44:21 +01:00
|
|
|
* local_funding_pubkey: 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb
|
|
|
|
* remote_funding_pubkey: 030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1
|
|
|
|
*/
|
|
|
|
if (!pubkey_from_hexstr("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb",
|
|
|
|
strlen("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb"),
|
|
|
|
&local_funding_pubkey))
|
|
|
|
abort();
|
|
|
|
if (!pubkey_from_hexstr("030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1",
|
|
|
|
strlen("030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c1"),
|
|
|
|
&remote_funding_pubkey))
|
|
|
|
abort();
|
|
|
|
|
2017-03-07 01:58:09 +01:00
|
|
|
bitcoin_txid(input, &utxo.txid);
|
|
|
|
utxo.outnum = 0;
|
2019-02-21 03:39:51 +01:00
|
|
|
utxo.amount = AMOUNT_SAT(5000000000);
|
2017-03-07 02:01:55 +01:00
|
|
|
utxo.is_p2sh = false;
|
2017-12-20 12:44:00 +01:00
|
|
|
utxo.close_info = NULL;
|
2019-02-21 04:45:55 +01:00
|
|
|
funding_sat = AMOUNT_SAT(10000000);
|
|
|
|
fee = AMOUNT_SAT(13920);
|
2017-02-07 02:44:21 +01:00
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
printf("input[0] txid: %s\n",
|
2017-03-07 01:58:09 +01:00
|
|
|
tal_hexstr(tmpctx, &utxo.txid, sizeof(utxo.txid)));
|
|
|
|
printf("input[0] input: %u\n", utxo.outnum);
|
2019-02-21 03:39:51 +01:00
|
|
|
printf("input[0] satoshis: %s\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &utxo.amount));
|
2019-02-21 04:45:55 +01:00
|
|
|
printf("funding: %s\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &funding_sat));
|
2017-02-07 02:44:21 +01:00
|
|
|
|
2018-03-15 07:10:20 +01:00
|
|
|
utxomap = tal_arr(tmpctx, const struct utxo *, 1);
|
2017-03-07 01:58:09 +01:00
|
|
|
utxomap[0] = &utxo;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_sub(&change, utxo.amount, funding_sat)
|
|
|
|
|| !amount_sat_sub(&change, change, fee))
|
|
|
|
abort();
|
2017-03-07 01:58:09 +01:00
|
|
|
funding = funding_tx(tmpctx, &funding_outnum, utxomap,
|
2019-02-21 04:45:55 +01:00
|
|
|
funding_sat,
|
2017-02-07 02:44:21 +01:00
|
|
|
&local_funding_pubkey,
|
|
|
|
&remote_funding_pubkey,
|
2019-02-21 04:45:55 +01:00
|
|
|
change,
|
2017-03-07 02:01:55 +01:00
|
|
|
&inputkey, NULL);
|
2019-02-21 04:45:55 +01:00
|
|
|
printf("# fee: %s\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat, &fee));
|
2019-03-25 11:35:56 +01:00
|
|
|
|
|
|
|
tmpamt = bitcoin_tx_output_get_amount(funding, !funding_outnum);
|
2019-02-21 04:45:55 +01:00
|
|
|
printf("change: %s\n",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-03-25 11:35:56 +01:00
|
|
|
&tmpamt));
|
2017-02-07 02:44:21 +01:00
|
|
|
|
|
|
|
printf("funding output: %u\n", funding_outnum);
|
|
|
|
|
2017-06-14 10:29:10 +02:00
|
|
|
pubkey_to_hash160(&inputkey, &addr.addr);
|
|
|
|
subscript = scriptpubkey_p2pkh(funding, &addr);
|
2017-02-24 06:52:56 +01:00
|
|
|
sign_tx_input(funding, 0, subscript, NULL, &input_privkey, &inputkey,
|
2018-12-03 00:15:06 +01:00
|
|
|
SIGHASH_ALL, &sig);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2019-03-20 16:45:21 +01:00
|
|
|
script = bitcoin_redeem_p2pkh(funding, &inputkey, &sig);
|
|
|
|
bitcoin_tx_input_set_script(funding, 0, script);
|
2017-02-07 02:44:21 +01:00
|
|
|
printf("funding tx: %s\n",
|
|
|
|
tal_hex(tmpctx, linearize_tx(tmpctx, funding)));
|
|
|
|
|
|
|
|
/* No memory leaks please */
|
|
|
|
secp256k1_context_destroy(secp256k1_ctx);
|
|
|
|
tal_free(tmpctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|