From 5475666b7e337de1dc09f0e8d920cda24ecbac22 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 21 Feb 2017 15:19:02 +1030 Subject: [PATCH] lightningd: simple wallet support. This allows us to add funds via the P2SH-wrapped Segwit Transactions. Signed-off-by: Rusty Russell --- bitcoin/script.c | 11 ++ bitcoin/script.h | 3 + daemon/test/scripts/helpers.sh | 6 + lightningd/Makefile | 2 + lightningd/build_utxos.c | 238 +++++++++++++++++++++++++++++++++ lightningd/build_utxos.h | 14 ++ lightningd/lightningd.c | 2 + lightningd/lightningd.h | 6 +- lightningd/opening/opening.c | 57 ++++---- lightningd/test/test-basic | 8 ++ 10 files changed, 317 insertions(+), 30 deletions(-) create mode 100644 lightningd/build_utxos.c create mode 100644 lightningd/build_utxos.h diff --git a/bitcoin/script.c b/bitcoin/script.c index 8e354e5a0..1b606f107 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -322,6 +322,17 @@ u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key) return script; } +u8 *scriptpubkey_p2wpkh_derkey(const tal_t *ctx, const u8 der[33]) +{ + u8 *script = tal_arr(ctx, u8, 0); + struct ripemd160 h; + + add_op(&script, OP_0); + hash160(&h, der, PUBKEY_DER_LEN); + add_push_bytes(&script, &h, sizeof(h)); + return script; +} + /* Create a witness which spends the 2of2. */ u8 **bitcoin_witness_2of2(const tal_t *ctx, const secp256k1_ecdsa_signature *sig1, diff --git a/bitcoin/script.h b/bitcoin/script.h index 49c32846b..b6ceb79c1 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -78,6 +78,9 @@ u8 *scriptpubkey_p2wsh(const tal_t *ctx, const u8 *witnessscript); /* Create an output script for a 20-byte witness program. */ u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key); +/* Same as above, but compressed key is already DER-encoded. */ +u8 *scriptpubkey_p2wpkh_derkey(const tal_t *ctx, const u8 der[33]); + /* Create a witness which spends the 2of2. */ u8 **bitcoin_witness_2of2(const tal_t *ctx, const secp256k1_ecdsa_signature *sig1, diff --git a/daemon/test/scripts/helpers.sh b/daemon/test/scripts/helpers.sh index 155b75349..e0463c7c4 100644 --- a/daemon/test/scripts/helpers.sh +++ b/daemon/test/scripts/helpers.sh @@ -308,6 +308,12 @@ all_ok() exit 0 } +# If result is in quotes, those are stripped. Spaces in quotes not handled +get_field() +{ + tr -s '\012\011" ' ' ' | sed 's/.* '$1' : \([^, }]*\).*/\1/' +} + # If result is in quotes, those are stripped. Spaces in quotes not handled get_info_field() { diff --git a/lightningd/Makefile b/lightningd/Makefile index 88c6129c8..57143ba17 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -47,6 +47,7 @@ LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o) LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h) LIGHTNINGD_SRC := \ + lightningd/build_utxos.c \ lightningd/gossip_control.c \ lightningd/hsm_control.c \ lightningd/lightningd.c \ @@ -60,6 +61,7 @@ LIGHTNINGD_JSMN_HEADERS := daemon/jsmn/jsmn.h # We accumulate all lightningd/ headers in these three: LIGHTNINGD_HEADERS_NOGEN = \ + lightningd/build_utxos.h \ lightningd/gossip_control.h \ lightningd/hsm_control.h \ lightningd/lightningd.h \ diff --git a/lightningd/build_utxos.c b/lightningd/build_utxos.c new file mode 100644 index 000000000..04cb3fec7 --- /dev/null +++ b/lightningd/build_utxos.c @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +struct tracked_utxo { + struct list_node list; + + /* Currently being used for a connection. */ + bool reserved; + + struct utxo utxo; +}; + +static void json_newaddr(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct json_result *response = new_json_result(cmd); + struct lightningd *ld = ld_from_dstate(cmd->dstate); + struct ext_key ext; + struct sha256 h; + struct ripemd160 p2sh; + struct pubkey pubkey; + u8 *redeemscript; + + if (ld->bip32_max_index == BIP32_INITIAL_HARDENED_CHILD) { + command_fail(cmd, "Keys exhausted "); + return; + } + + if (bip32_key_from_parent(ld->bip32_base, ld->bip32_max_index, + BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { + command_fail(cmd, "Keys generation failure"); + return; + } + + if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, + ext.pub_key, sizeof(ext.pub_key))) { + command_fail(cmd, "Key parsing failure"); + return; + } + + redeemscript = bitcoin_redeem_p2wpkh(cmd, &pubkey); + sha256(&h, redeemscript, tal_count(redeemscript)); + ripemd160(&p2sh, h.u.u8, sizeof(h)); + + ld->bip32_max_index++; + + json_object_start(response, NULL); + json_add_string(response, "address", + p2sh_to_base58(cmd, cmd->dstate->testnet, &p2sh)); + json_object_end(response); + command_success(cmd, response); +} + +static const struct json_command newaddr_command = { + "newaddr", + json_newaddr, + "Get a new address to fund a channel", + "Returns {address} a p2sh address" +}; +AUTODATA(json_command, &newaddr_command); + +/* FIXME: This is very slow with lots of inputs! */ +static bool can_spend(struct lightningd *ld, const u8 *script, + u32 *index, bool *output_is_p2sh) +{ + struct ext_key ext; + u32 i; + + /* If not one of these, can't be for us. */ + if (is_p2sh(script)) + *output_is_p2sh = true; + else if (is_p2wpkh(script)) + *output_is_p2sh = false; + else + return false; + + for (i = 0; i < ld->bip32_max_index; i++) { + u8 *s; + + if (bip32_key_from_parent(ld->bip32_base, i, + BIP32_FLAG_KEY_PUBLIC, &ext) + != WALLY_OK) { + abort(); + } + s = scriptpubkey_p2wpkh_derkey(ld, ext.pub_key); + if (*output_is_p2sh) { + u8 *p2sh = scriptpubkey_p2sh(ld, s); + tal_free(s); + s = p2sh; + } + if (scripteq(s, script)) { + tal_free(s); + *index = i; + return true; + } + tal_free(s); + } + return false; +} + +static void json_addfunds(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct lightningd *ld = ld_from_dstate(cmd->dstate); + struct json_result *response = new_json_result(cmd); + jsmntok_t *txtok; + struct bitcoin_tx *tx; + int output; + size_t txhexlen, num_utxos = 0; + u64 total_satoshi = 0; + + if (!json_get_params(buffer, params, "tx", &txtok, NULL)) { + command_fail(cmd, "Need tx sending to address from newaddr"); + return; + } + + txhexlen = txtok->end - txtok->start; + tx = bitcoin_tx_from_hex(cmd, buffer + txtok->start, txhexlen); + if (!tx) { + command_fail(cmd, "'%.*s' is not a valid transaction", + txtok->end - txtok->start, + buffer + txtok->start); + return; + } + + /* Find an output we know how to spend. */ + for (output = 0; output < tal_count(tx->output); output++) { + struct tracked_utxo *utxo; + u32 index; + bool is_p2sh; + + if (!can_spend(ld, tx->output[output].script, &index, &is_p2sh)) + continue; + + utxo = tal(ld, struct tracked_utxo); + utxo->utxo.keyindex = index; + utxo->utxo.is_p2sh = is_p2sh; + utxo->utxo.amount = tx->output[output].amount; + bitcoin_txid(tx, &utxo->utxo.txid); + utxo->utxo.outnum = output; + utxo->reserved = false; + list_add_tail(&ld->utxos, &utxo->list); + total_satoshi += utxo->utxo.amount; + num_utxos++; + } + + if (!num_utxos) { + command_fail(cmd, "No usable outputs"); + return; + } + + json_object_start(response, NULL); + json_add_num(response, "outputs", num_utxos); + json_add_u64(response, "satoshis", total_satoshi); + json_object_end(response); + command_success(cmd, response); +} + +static const struct json_command addfunds_command = { + "addfunds", + json_addfunds, + "Add funds for lightningd to spend to create channels, using {tx}", + "Returns how many {outputs} it can use and total {satoshis}" +}; +AUTODATA(json_command, &addfunds_command); + +static void unreserve_utxo(struct lightningd *ld, const struct utxo *unres) +{ + struct tracked_utxo *utxo; + + list_for_each(&ld->utxos, utxo, list) { + if (unres->outnum != utxo->utxo.outnum + || !structeq(&unres->txid, &utxo->utxo.txid)) + continue; + assert(utxo->reserved); + assert(unres->amount == utxo->utxo.amount); + assert(unres->keyindex == utxo->utxo.keyindex); + assert(unres->is_p2sh == utxo->utxo.is_p2sh); + utxo->reserved = false; + return; + } + abort(); +} + +struct utxo *build_utxos(const tal_t *ctx, + struct lightningd *ld, u64 satoshi_out, + u32 feerate_per_kw, u64 dust_limit, + u64 *change_amount, u32 *change_keyindex) +{ + size_t i = 0; + struct utxo *utxos = tal_arr(ctx, struct utxo, 0); + struct tracked_utxo *utxo; + /* We assume two outputs for the weight. */ + u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4; + + list_for_each(&ld->utxos, utxo, list) { + u64 fee; + + if (utxo->reserved) + continue; + + tal_resize(&utxos, i+1); + utxos[i] = utxo->utxo; + utxo->reserved = true; + + /* Add this input's weight. */ + weight += (32 + 4 + 4) * 4; + if (utxos[i].is_p2sh) + weight += 22 * 4; + + satoshi_in += utxos[i].amount; + + fee = weight * feerate_per_kw / 1000; + if (satoshi_in >= fee + satoshi_out) { + /* We simply eliminate change if it's dust. */ + *change_amount = satoshi_in - (fee + satoshi_out); + if (*change_amount < dust_limit) + *change_amount = 0; + else + *change_keyindex = ld->bip32_max_index++; + + return utxos; + } + i++; + } + + /* Failed, unmark them all. */ + for (i = 0; i < tal_count(utxos); i++) + unreserve_utxo(ld, &utxos[i]); + + return tal_free(utxos); +} diff --git a/lightningd/build_utxos.h b/lightningd/build_utxos.h new file mode 100644 index 000000000..03bb90124 --- /dev/null +++ b/lightningd/build_utxos.h @@ -0,0 +1,14 @@ +#ifndef LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H +#define LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H +#include "config.h" +#include +#include + +/* Reserves UTXOs to build tx which pays this amount; returns NULL if + * impossible. *change_amount 0 if no change needed. */ +struct utxo *build_utxos(const tal_t *ctx, + struct lightningd *ld, u64 satoshi_out, + u32 feerate_per_kw, u64 dust_limit, + u64 *change_amount, u32 *change_keyindex); + +#endif /* LIGHTNING_LIGHTNINGD_BUILD_UTXOS_H */ diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index aafb2d085..48ee163b9 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -71,6 +71,8 @@ static struct lightningd *new_lightningd(const tal_t *ctx) struct lightningd *ld = tal(ctx, struct lightningd); list_head_init(&ld->peers); + ld->bip32_max_index = 0; + list_head_init(&ld->utxos); ld->dstate.log_book = new_log_book(&ld->dstate, 20*1024*1024,LOG_INFORM); ld->log = ld->dstate.base_log = new_log(&ld->dstate, ld->dstate.log_book, diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index a2ea7b031..d1bf74cc2 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -31,8 +31,12 @@ struct lightningd { /* All peers we're tracking. */ struct list_head peers; - /* Public base for bip32 keys. */ + /* Public base for bip32 keys, and max we've ever used. */ struct ext_key *bip32_base; + u32 bip32_max_index; + + /* UTXOs we have available to spend. */ + struct list_head utxos; }; /* FIXME */ diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 5d1fab6d3..95ca57212 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -45,7 +45,7 @@ struct secrets { }; struct state { - struct crypto_state *cs; + struct crypto_state cs; struct pubkey next_per_commit[NUM_SIDES]; /* Funding and feerate: set by opening peer. */ @@ -59,7 +59,7 @@ struct state { /* Our shaseed for generating per-commitment-secrets. */ struct sha256 shaseed; - struct channel_config *localconf, *remoteconf, *minconf, *maxconf; + struct channel_config localconf, *remoteconf, minconf, maxconf; struct channel *channel; }; @@ -242,25 +242,25 @@ static void open_channel(struct state *state, const struct points *ours) msg = towire_open_channel(state, &tmpid, state->funding_satoshis, state->push_msat, - state->localconf->dust_limit_satoshis, - state->localconf->max_htlc_value_in_flight_msat, - state->localconf->channel_reserve_satoshis, - state->localconf->htlc_minimum_msat, + state->localconf.dust_limit_satoshis, + state->localconf.max_htlc_value_in_flight_msat, + state->localconf.channel_reserve_satoshis, + state->localconf.htlc_minimum_msat, state->feerate_per_kw, - state->localconf->to_self_delay, - state->localconf->max_accepted_htlcs, + state->localconf.to_self_delay, + state->localconf.max_accepted_htlcs, &ours->funding_pubkey, &ours->revocation_basepoint, &ours->payment_basepoint, &ours->delayed_payment_basepoint, &state->next_per_commit[LOCAL]); - if (!sync_crypto_write(state->cs, PEER_FD, msg)) + if (!sync_crypto_write(&state->cs, PEER_FD, msg)) status_failed(WIRE_OPENING_PEER_WRITE_FAILED, "Writing open_channel"); state->remoteconf = tal(state, struct channel_config); - msg = sync_crypto_read(state, state->cs, PEER_FD); + msg = sync_crypto_read(state, &state->cs, PEER_FD); if (!msg) status_failed(WIRE_OPENING_PEER_READ_FAILED, "Reading accept_channel"); @@ -301,7 +301,7 @@ static void open_channel(struct state *state, const struct points *ours) type_to_string(msg, struct channel_id, &tmpid2)); check_config_bounds(state->remoteconf, - state->minconf, state->maxconf); + &state->minconf, &state->maxconf); /* Now, ask master create a transaction to pay those two addresses. */ msg = towire_opening_open_resp(state, &ours->funding_pubkey, @@ -321,7 +321,7 @@ static void open_channel(struct state *state, const struct points *ours) state->funding_satoshis, state->push_msat, state->feerate_per_kw, - state->localconf, + &state->localconf, state->remoteconf, &ours->revocation_basepoint, &theirs.revocation_basepoint, @@ -352,7 +352,7 @@ static void open_channel(struct state *state, const struct points *ours) &state->funding_txid.sha, state->funding_txout, &sig); - if (!sync_crypto_write(state->cs, PEER_FD, msg)) + if (!sync_crypto_write(&state->cs, PEER_FD, msg)) status_failed(WIRE_OPENING_PEER_WRITE_FAILED, "Writing funding_created"); @@ -364,7 +364,7 @@ static void open_channel(struct state *state, const struct points *ours) * commitment transaction, so they can broadcast it knowing they can * redeem their funds if they need to. */ - msg = sync_crypto_read(state, state->cs, PEER_FD); + msg = sync_crypto_read(state, &state->cs, PEER_FD); if (!msg) status_failed(WIRE_OPENING_PEER_READ_FAILED, "Reading funding_signed"); @@ -403,7 +403,7 @@ static void open_channel(struct state *state, const struct points *ours) msg = towire_opening_open_funding_resp(state, state->remoteconf, &sig, - state->cs, + &state->cs, &theirs.revocation_basepoint, &theirs.payment_basepoint, &theirs.delayed_payment_basepoint, @@ -471,28 +471,28 @@ static void recv_channel(struct state *state, const struct points *ours, state->push_msat, state->funding_satoshis); check_config_bounds(state->remoteconf, - state->minconf, state->maxconf); + &state->minconf, &state->maxconf); msg = towire_accept_channel(state, &tmpid, - state->localconf->dust_limit_satoshis, + state->localconf.dust_limit_satoshis, state->localconf - ->max_htlc_value_in_flight_msat, - state->localconf->channel_reserve_satoshis, - state->localconf->htlc_minimum_msat, + .max_htlc_value_in_flight_msat, + state->localconf.channel_reserve_satoshis, + state->localconf.htlc_minimum_msat, state->feerate_per_kw, - state->localconf->to_self_delay, - state->localconf->max_accepted_htlcs, + state->localconf.to_self_delay, + state->localconf.max_accepted_htlcs, &ours->funding_pubkey, &ours->revocation_basepoint, &ours->payment_basepoint, &ours->delayed_payment_basepoint, &state->next_per_commit[REMOTE]); - if (!sync_crypto_write(state->cs, PEER_FD, msg)) + if (!sync_crypto_write(&state->cs, PEER_FD, msg)) status_failed(WIRE_OPENING_PEER_WRITE_FAILED, "Writing accept_channel"); - msg = sync_crypto_read(state, state->cs, PEER_FD); + msg = sync_crypto_read(state, &state->cs, PEER_FD); if (!msg) status_failed(WIRE_OPENING_PEER_READ_FAILED, "Reading funding_created"); @@ -520,7 +520,7 @@ static void recv_channel(struct state *state, const struct points *ours, state->funding_satoshis, state->push_msat, state->feerate_per_kw, - state->localconf, + &state->localconf, state->remoteconf, &ours->revocation_basepoint, &theirs.revocation_basepoint, @@ -565,7 +565,7 @@ static void recv_channel(struct state *state, const struct points *ours, tx); msg = towire_funding_signed(state, &tmpid, &sig); - if (!sync_crypto_write(state->cs, PEER_FD, msg)) + if (!sync_crypto_write(&state->cs, PEER_FD, msg)) status_failed(WIRE_OPENING_PEER_WRITE_FAILED, "Writing funding_signed"); @@ -574,7 +574,7 @@ static void recv_channel(struct state *state, const struct points *ours, state->funding_txout, state->remoteconf, &theirsig, - state->cs, + &state->cs, &theirs.funding_pubkey, &theirs.revocation_basepoint, &theirs.payment_basepoint, @@ -609,12 +609,11 @@ int main(int argc, char *argv[]) if (!msg) status_failed(WIRE_BAD_COMMAND, "%s", strerror(errno)); - state->cs = tal(state, struct crypto_state); if (!fromwire_opening_init(msg, NULL, &state->localconf, &state->minconf, &state->maxconf, - state->cs, + &state->cs, &seed)) status_failed(WIRE_BAD_COMMAND, "%s", strerror(errno)); tal_free(msg); diff --git a/lightningd/test/test-basic b/lightningd/test/test-basic index 20c04dd00..85ed00c59 100755 --- a/lightningd/test/test-basic +++ b/lightningd/test/test-basic @@ -30,6 +30,14 @@ lcli1 getpeers info | $FGREP "Exchanging gossip" lcli1 getpeers | $FGREP '"owner" : "lightningd_gossip"' lcli2 getpeers | $FGREP '"owner" : "lightningd_gossip"' +# Add some funds. +set -x +NEWADDR=`lcli1 newaddr | get_field address` +FUND_INPUT_TXID=`$CLI sendtoaddress $NEWADDR 0.10000002` +FUND_INPUT_TX=`$CLI getrawtransaction $FUND_INPUT_TXID` + +lcli1 addfunds $FUND_INPUT_TX | $FGREP '"satoshis" : 10000002' + lcli1 stop lcli2 stop