Skip to content

Commit

Permalink
bitcoind: routine to send to a specific address.
Browse files Browse the repository at this point in the history
We use this to create our anchor payment.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent c076606 commit c51a8d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
35 changes: 35 additions & 0 deletions daemon/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions daemon/bitcoind.h
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 */
1 change: 0 additions & 1 deletion daemon/peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit c51a8d8

Please sign in to comment.