bitcoind: routine to send to a specific address.

We use this to create our anchor payment.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-01-22 06:41:49 +10:30
parent c0766061fa
commit c51a8d804f
3 changed files with 45 additions and 1 deletions

View File

@ -13,6 +13,7 @@
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <errno.h>
#include <inttypes.h>
#define BITCOIN_CLI "bitcoin-cli"
@ -258,3 +259,37 @@ void bitcoind_send_tx(struct lightningd_state *dstate,
"sendrawtransaction", hex, NULL);
tal_free(raw);
}
static void process_sendtoaddress(struct bitcoin_cli *bcli)
{
const char *out = (char *)bcli->output;
char *txidstr;
/* We expect a txid (followed by \n, vs hex_str_size including \0) */
if (bcli->output_bytes != hex_str_size(sizeof(struct sha256_double)))
fatal("sendtoaddress failed: %.*s",
(int)bcli->output_bytes, out);
txidstr = tal_strndup(bcli, out, bcli->output_bytes-1);
log_debug(bcli->dstate->base_log, "sendtoaddress gave %s", txidstr);
/* Now we need the raw transaction. */
start_bitcoin_cli(bcli->dstate, process_rawtx, bcli->cb, bcli->cb_arg,
"getrawtransaction", txidstr, NULL);
}
void bitcoind_create_payment(struct lightningd_state *dstate,
const char *addr,
u64 satoshis,
void (*cb)(struct lightningd_state *dstate,
const struct bitcoin_tx *tx,
struct peer *peer),
struct peer *peer)
{
char amtstr[STR_MAX_CHARS(satoshis) * 2 + 1];
sprintf(amtstr, "%"PRIu64 "." "%08"PRIu64,
satoshis / 100000000, satoshis % 100000000);
start_bitcoin_cli(dstate, process_sendtoaddress, cb, peer,
"sendtoaddress", addr, amtstr, NULL);
}

View File

@ -1,12 +1,14 @@
#ifndef LIGHTNING_DAEMON_BITCOIND_H
#define LIGHTNING_DAEMON_BITCOIND_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
struct sha256_double;
struct lightningd_state;
struct ripemd160;
struct bitcoin_tx;
struct peer;
void bitcoind_watch_addr(struct lightningd_state *dstate,
const struct ripemd160 *redeemhash);
@ -33,4 +35,12 @@ void bitcoind_txid_lookup_(struct lightningd_state *dstate,
void bitcoind_send_tx(struct lightningd_state *dstate,
const struct bitcoin_tx *tx);
void bitcoind_create_payment(struct lightningd_state *dstate,
const char *addr,
u64 satoshis,
void (*cb)(struct lightningd_state *dstate,
const struct bitcoin_tx *tx,
struct peer *peer),
struct peer *peer);
#endif /* LIGHTNING_DAEMON_BITCOIND_H */

View File

@ -7,7 +7,6 @@
#include <ccan/io/io.h>
#include <ccan/list/list.h>
#include <ccan/noerr/noerr.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <errno.h>