diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index c0d9a4a09..1c7f7f009 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -13,6 +13,7 @@ #include #include #include +#include #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); +} diff --git a/daemon/bitcoind.h b/daemon/bitcoind.h index d27a7e632..fe61be878 100644 --- a/daemon/bitcoind.h +++ b/daemon/bitcoind.h @@ -1,12 +1,14 @@ #ifndef LIGHTNING_DAEMON_BITCOIND_H #define LIGHTNING_DAEMON_BITCOIND_H #include "config.h" +#include #include 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 */ diff --git a/daemon/peer.c b/daemon/peer.c index 6da480017..a18585de6 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include