2019-09-06 23:30:46 +02:00
|
|
|
#include <bitcoin/chainparams.c>
|
2019-08-28 05:39:49 +02:00
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
#include <ccan/json_out/json_out.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
2019-09-06 23:30:46 +02:00
|
|
|
#include <common/addr.h>
|
2019-08-28 05:39:49 +02:00
|
|
|
#include <common/amount.h>
|
2020-04-03 02:03:58 +02:00
|
|
|
#include <common/features.h>
|
2020-01-30 00:55:55 +01:00
|
|
|
#include <common/json_stream.h>
|
2019-08-28 05:39:49 +02:00
|
|
|
#include <common/json_tok.h>
|
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <plugins/libplugin.h>
|
|
|
|
|
2019-08-30 04:20:11 +02:00
|
|
|
const char *placeholder_script = "0020b95810f824f843934fa042acd0becba52087813e260edaeebc42b5cb9abe1464";
|
2019-09-06 23:30:46 +02:00
|
|
|
const char *placeholder_funding_addr;
|
2019-09-17 13:03:11 +02:00
|
|
|
|
|
|
|
/* Populated by libplugin */
|
|
|
|
extern const struct chainparams *chainparams;
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2019-08-28 05:39:49 +02:00
|
|
|
struct funding_req {
|
|
|
|
struct node_id *id;
|
|
|
|
const char *feerate_str;
|
2019-08-30 04:20:11 +02:00
|
|
|
const char *funding_str;
|
2019-08-28 22:52:47 +02:00
|
|
|
const char *utxo_str;
|
2019-09-03 20:51:56 +02:00
|
|
|
bool funding_all;
|
2019-12-21 06:53:12 +01:00
|
|
|
struct amount_msat *push_msat;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-04-03 02:03:58 +02:00
|
|
|
/* Features offered by this peer. */
|
2020-04-03 02:03:59 +02:00
|
|
|
const u8 *their_features;
|
2020-04-03 02:03:58 +02:00
|
|
|
|
2019-08-28 05:39:49 +02:00
|
|
|
bool *announce_channel;
|
|
|
|
u32 *minconf;
|
|
|
|
|
|
|
|
/* The prepared tx id */
|
|
|
|
struct bitcoin_txid tx_id;
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2019-08-28 05:39:49 +02:00
|
|
|
const char *chanstr;
|
|
|
|
const u8 *out_script;
|
2019-08-30 04:20:11 +02:00
|
|
|
const char *funding_addr;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
/* Failing result (NULL on success) */
|
|
|
|
/* Raw JSON from RPC output */
|
|
|
|
const char *error;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper to copy JSON object directly into a json_out */
|
|
|
|
static void json_out_add_raw_len(struct json_out *jout,
|
|
|
|
const char *fieldname,
|
|
|
|
const char *jsonstr, size_t len)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = json_out_member_direct(jout, fieldname, len);
|
|
|
|
memcpy(p, jsonstr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *send_prior(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *error,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
|
|
|
return command_err_raw(cmd, fr->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *tx_abort(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *error,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
/* We stash the error so we can return it after we've cleaned up */
|
|
|
|
fr->error = json_strdup(fr, buf, error);
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "txdiscard",
|
|
|
|
send_prior, send_prior, fr);
|
|
|
|
json_add_string(req->js, "txid",
|
2019-08-28 05:39:49 +02:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id));
|
|
|
|
|
|
|
|
/* We need to call txdiscard, and forward the actual cause for the
|
|
|
|
* error after we've cleaned up. We swallow any errors returned by
|
|
|
|
* this call, as we don't really care if it succeeds or not */
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We're basically done, we just need to format the output to match
|
|
|
|
* what the original `fundchannel` returned */
|
|
|
|
static struct command_result *finish(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-01-31 01:12:07 +01:00
|
|
|
struct json_stream *out;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-01-31 01:12:07 +01:00
|
|
|
out = jsonrpc_stream_success(cmd);
|
|
|
|
json_add_tok(out, "tx", json_get_member(buf, result, "tx"), buf);
|
|
|
|
json_add_string(out, "txid",
|
2019-08-28 05:39:49 +02:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id));
|
2020-01-31 01:12:07 +01:00
|
|
|
json_add_string(out, "channel_id", fr->chanstr);
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-01-31 01:12:07 +01:00
|
|
|
return command_finished(cmd, out);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We're ready to broadcast the transaction */
|
|
|
|
static struct command_result *send_tx(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 05:39:49 +02:00
|
|
|
const jsmntok_t *tok;
|
|
|
|
bool commitments_secured;
|
|
|
|
|
|
|
|
/* For sanity's sake, let's check that it's secured */
|
|
|
|
tok = json_get_member(buf, result, "commitments_secured");
|
|
|
|
if (!json_to_bool(buf, tok, &commitments_secured) || !commitments_secured)
|
2019-08-30 04:20:11 +02:00
|
|
|
/* TODO: better failure path? this should never fail though. */
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Commitment not secured.");
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
/* Stash the channel_id so we can return it when finalized */
|
|
|
|
tok = json_get_member(buf, result, "channel_id");
|
|
|
|
fr->chanstr = json_strdup(fr, buf, tok);
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "txsend",
|
|
|
|
finish, tx_abort, fr);
|
|
|
|
json_add_string(req->js, "txid",
|
2019-08-28 05:39:49 +02:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id));
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *tx_prepare_done(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
|
|
|
const jsmntok_t *txid_tok;
|
|
|
|
const jsmntok_t *tx_tok;
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 05:39:49 +02:00
|
|
|
const struct bitcoin_tx *tx;
|
|
|
|
const char *hex;
|
|
|
|
u32 outnum;
|
2019-08-28 22:52:47 +02:00
|
|
|
bool outnum_found;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
txid_tok = json_get_member(buf, result, "txid");
|
|
|
|
if (!txid_tok)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "txprepare missing 'txid' field");
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
tx_tok = json_get_member(buf, result, "unsigned_tx");
|
|
|
|
if (!tx_tok)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "txprepare missing 'unsigned_tx' field");
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
hex = json_strdup(tmpctx, buf, tx_tok);
|
|
|
|
tx = bitcoin_tx_from_hex(fr, hex, strlen(hex));
|
|
|
|
if (!tx)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Unable to parse tx %s", hex);
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
/* Find the txout */
|
2019-08-30 04:20:11 +02:00
|
|
|
outnum_found = false;
|
2019-08-28 05:39:49 +02:00
|
|
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
|
|
|
const u8 *output_script = bitcoin_tx_output_get_script(fr, tx, i);
|
|
|
|
if (scripteq(output_script, fr->out_script)) {
|
|
|
|
outnum = i;
|
2019-08-28 22:52:47 +02:00
|
|
|
outnum_found = true;
|
2019-08-28 05:39:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 22:52:47 +02:00
|
|
|
if (!outnum_found)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "txprepare doesn't include our funding output. "
|
2019-08-28 22:52:47 +02:00
|
|
|
"tx: %s, output: %s",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, tx),
|
|
|
|
tal_hex(tmpctx, fr->out_script));
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
hex = json_strdup(tmpctx, buf, txid_tok);
|
|
|
|
if (!bitcoin_txid_from_hex(hex, strlen(hex), &fr->tx_id))
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Unable to parse txid %s", hex);
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "fundchannel_complete",
|
|
|
|
send_tx, tx_abort, fr);
|
|
|
|
json_add_string(req->js, "id", node_id_to_hexstr(tmpctx, fr->id));
|
2019-08-28 05:39:49 +02:00
|
|
|
/* Note that hex is reused from above */
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(req->js, "txid", hex);
|
|
|
|
json_add_u32(req->js, "txout", outnum);
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 22:54:59 +02:00
|
|
|
static struct command_result *cancel_start(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *error,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 22:54:59 +02:00
|
|
|
|
|
|
|
/* We stash the error so we can return it after we've cleaned up */
|
|
|
|
fr->error = json_strdup(fr, buf, error);
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "fundchannel_cancel",
|
|
|
|
send_prior, send_prior, fr);
|
|
|
|
json_add_string(req->js, "id", node_id_to_hexstr(tmpctx, fr->id));
|
2019-08-28 22:54:59 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-28 22:54:59 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
static void txprepare(struct json_stream *js,
|
|
|
|
struct funding_req *fr,
|
|
|
|
const char *destination)
|
2019-08-30 04:20:11 +02:00
|
|
|
{
|
|
|
|
/* Add the 'outputs' */
|
2020-01-30 00:55:55 +01:00
|
|
|
json_array_start(js, "outputs");
|
|
|
|
json_object_start(js, NULL);
|
|
|
|
json_add_string(js, destination, fr->funding_str);
|
|
|
|
json_object_end(js);
|
|
|
|
json_array_end(js);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
if (fr->feerate_str)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(js, "feerate", fr->feerate_str);
|
2019-08-30 04:20:11 +02:00
|
|
|
if (fr->minconf)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_u32(js, "minconf", *fr->minconf);
|
2019-08-30 04:20:11 +02:00
|
|
|
if (fr->utxo_str)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_out_add_raw_len(js->jout, "utxos", fr->utxo_str,
|
|
|
|
strlen(fr->utxo_str));
|
2019-08-30 04:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *prepare_actual(struct command *cmd,
|
2020-01-30 00:55:55 +01:00
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
2019-08-28 05:39:49 +02:00
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "txprepare",
|
|
|
|
tx_prepare_done, cancel_start,
|
|
|
|
fr);
|
|
|
|
txprepare(req->js, fr, fr->funding_addr);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-30 04:20:11 +02:00
|
|
|
}
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2019-08-30 04:20:11 +02:00
|
|
|
static struct command_result *fundchannel_start_done(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req;
|
2019-08-28 05:39:49 +02:00
|
|
|
|
|
|
|
/* Save the outscript so we can fund the outnum later */
|
2019-08-30 04:20:11 +02:00
|
|
|
fr->out_script = json_tok_bin_from_hex(fr, buf,
|
|
|
|
json_get_member(buf, result, "scriptpubkey"));
|
|
|
|
|
|
|
|
/* Save the funding address, we'll need it later */
|
|
|
|
fr->funding_addr = json_strdup(cmd, buf,
|
|
|
|
json_get_member(buf, result, "funding_address"));
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2019-08-30 04:20:11 +02:00
|
|
|
/* Now that we're ready to go, cancel the reserved tx */
|
2020-01-30 00:55:55 +01:00
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "txdiscard",
|
|
|
|
prepare_actual, cancel_start,
|
|
|
|
fr);
|
|
|
|
json_add_string(req->js, "txid",
|
2019-08-30 04:20:11 +02:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id));
|
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-30 04:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *fundchannel_start(struct command *cmd,
|
2019-12-01 22:34:28 +01:00
|
|
|
struct funding_req *fr)
|
2019-08-30 04:20:11 +02:00
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req = jsonrpc_request_start(cmd->plugin, cmd,
|
|
|
|
"fundchannel_start",
|
|
|
|
fundchannel_start_done,
|
|
|
|
tx_abort, fr);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(req->js, "id", node_id_to_hexstr(tmpctx, fr->id));
|
2019-09-19 20:50:58 +02:00
|
|
|
|
2020-03-25 04:39:40 +01:00
|
|
|
json_add_string(req->js, "amount", fr->funding_str);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2019-08-28 22:52:47 +02:00
|
|
|
if (fr->feerate_str)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(req->js, "feerate", fr->feerate_str);
|
2019-08-30 04:20:11 +02:00
|
|
|
if (fr->announce_channel)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_bool(req->js, "announce", *fr->announce_channel);
|
2019-12-21 06:53:12 +01:00
|
|
|
if (fr->push_msat)
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(req->js, "push_msat",
|
2019-12-21 06:53:12 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, fr->push_msat));
|
2019-08-30 04:20:11 +02:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-30 04:20:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 19:50:04 +01:00
|
|
|
static struct command_result *post_dryrun(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
2019-08-30 04:20:11 +02:00
|
|
|
{
|
2019-09-17 13:03:11 +02:00
|
|
|
struct bitcoin_tx *tx;
|
2019-08-30 04:20:11 +02:00
|
|
|
const char *hex;
|
|
|
|
struct amount_sat funding;
|
|
|
|
bool funding_found;
|
|
|
|
u8 *placeholder = tal_hexdata(tmpctx, placeholder_script, strlen(placeholder_script));
|
2019-09-26 21:07:20 +02:00
|
|
|
struct amount_asset asset;
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
/* Stash the 'reserved' txid to unreserve later */
|
|
|
|
hex = json_strdup(tmpctx, buf, json_get_member(buf, result, "txid"));
|
|
|
|
if (!bitcoin_txid_from_hex(hex, strlen(hex), &fr->tx_id))
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Unable to parse reserved txid %s", hex);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
hex = json_strdup(tmpctx, buf, json_get_member(buf, result, "unsigned_tx"));
|
|
|
|
tx = bitcoin_tx_from_hex(fr, hex, strlen(hex));
|
2019-09-17 13:03:11 +02:00
|
|
|
tx->chainparams = chainparams;
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
/* Find the funding amount */
|
|
|
|
funding_found = false;
|
|
|
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
|
|
|
const u8 *output_script = bitcoin_tx_output_get_script(tmpctx, tx, i);
|
2019-09-26 21:07:20 +02:00
|
|
|
asset = bitcoin_tx_output_get_amount(tx, i);
|
|
|
|
|
|
|
|
/* We do not support funding a channel with anything but the
|
|
|
|
* main asset, for now. */
|
|
|
|
if (!amount_asset_is_main(&asset))
|
|
|
|
continue;
|
|
|
|
|
2019-08-30 04:20:11 +02:00
|
|
|
if (scripteq(output_script, placeholder)) {
|
2019-09-26 21:07:20 +02:00
|
|
|
funding = amount_asset_to_sat(&asset);
|
2019-08-30 04:20:11 +02:00
|
|
|
funding_found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!funding_found)
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_err(cmd->plugin, "Error creating placebo funding tx, funding_out not found. %s", hex);
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
/* Update funding to actual amount */
|
2020-04-03 02:03:58 +02:00
|
|
|
if (fr->funding_all
|
|
|
|
&& !feature_negotiated(plugin_feature_set(cmd->plugin),
|
2020-04-03 02:03:59 +02:00
|
|
|
fr->their_features, OPT_LARGE_CHANNELS)
|
2020-04-03 02:03:58 +02:00
|
|
|
&& amount_sat_greater(funding, chainparams->max_funding))
|
2019-09-17 13:03:11 +02:00
|
|
|
funding = chainparams->max_funding;
|
2019-08-30 04:20:11 +02:00
|
|
|
|
|
|
|
fr->funding_str = type_to_string(fr, struct amount_sat, &funding);
|
2019-12-04 19:50:04 +01:00
|
|
|
return fundchannel_start(cmd, fr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *exec_dryrun(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-04-03 02:03:58 +02:00
|
|
|
struct out_req *req;
|
|
|
|
const jsmntok_t *t;
|
|
|
|
|
|
|
|
/* Stash features so we can wumbo. */
|
|
|
|
t = json_get_member(buf, result, "features");
|
|
|
|
if (!t)
|
|
|
|
plugin_err(cmd->plugin, "No features found in connect response?");
|
2020-04-03 02:03:59 +02:00
|
|
|
fr->their_features = json_tok_bin_from_hex(fr, buf, t);
|
|
|
|
if (!fr->their_features)
|
2020-04-03 02:03:58 +02:00
|
|
|
plugin_err(cmd->plugin, "Bad features '%.*s' in connect response?",
|
|
|
|
t->end - t->start, buf + t->start);
|
|
|
|
|
|
|
|
req = jsonrpc_request_start(cmd->plugin, cmd, "txprepare",
|
|
|
|
post_dryrun, forward_error,
|
|
|
|
fr);
|
2019-12-04 19:50:04 +01:00
|
|
|
|
|
|
|
/* Now that we've tried connecting, we do a 'dry-run' of txprepare,
|
|
|
|
* so we can get an accurate idea of the funding amount */
|
2020-01-30 00:55:55 +01:00
|
|
|
txprepare(req->js, fr, placeholder_funding_addr);
|
2019-12-04 19:50:04 +01:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-12-04 19:50:04 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *connect_to_peer(struct command *cmd,
|
|
|
|
struct funding_req *fr)
|
|
|
|
{
|
2020-01-30 00:55:55 +01:00
|
|
|
struct out_req *req = jsonrpc_request_start(cmd->plugin, cmd, "connect",
|
|
|
|
exec_dryrun, forward_error,
|
|
|
|
fr);
|
2019-12-04 19:50:04 +01:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
json_add_string(req->js, "id", node_id_to_hexstr(tmpctx, fr->id));
|
2019-12-04 19:50:04 +01:00
|
|
|
|
2020-01-30 00:55:55 +01:00
|
|
|
return send_outreq(cmd->plugin, req);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 09:37:05 +02:00
|
|
|
/* We will use 'id' and 'amount' to build a output: {id: amount}.
|
|
|
|
* For array type, if we miss 'amount', next parameter will be
|
|
|
|
* mistaken for 'amount'.
|
|
|
|
* Note the check for 'output' in 'txprepare' is behind of the checks
|
|
|
|
* for other parameter, so doing a simply check for 'amount' here can
|
|
|
|
* help us locate error correctly.
|
|
|
|
*/
|
|
|
|
static struct command_result *param_string_check_sat(struct command *cmd, const char *name,
|
|
|
|
const char * buffer, const jsmntok_t *tok,
|
|
|
|
const char **str)
|
|
|
|
{
|
|
|
|
struct command_result *res;
|
|
|
|
struct amount_sat *amount;
|
|
|
|
|
|
|
|
res = param_sat_or_all(cmd, name, buffer, tok, &amount);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
return param_string(cmd, name, buffer, tok, str);
|
|
|
|
}
|
|
|
|
|
2019-08-28 05:39:49 +02:00
|
|
|
static struct command_result *json_fundchannel(struct command *cmd,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
2019-08-30 04:20:11 +02:00
|
|
|
struct funding_req *fr = tal(cmd, struct funding_req);
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2020-03-25 04:39:34 +01:00
|
|
|
if (!param(cmd, buf, params,
|
|
|
|
p_req("id", param_node_id, &fr->id),
|
|
|
|
p_req("amount", param_string_check_sat, &fr->funding_str),
|
|
|
|
p_opt("feerate", param_string, &fr->feerate_str),
|
|
|
|
p_opt_def("announce", param_bool, &fr->announce_channel, true),
|
|
|
|
p_opt_def("minconf", param_number, &fr->minconf, 1),
|
|
|
|
p_opt("utxos", param_string, &fr->utxo_str),
|
|
|
|
p_opt("push_msat", param_msat, &fr->push_msat),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
2019-08-28 05:39:49 +02:00
|
|
|
|
2019-09-03 20:51:56 +02:00
|
|
|
fr->funding_all = streq(fr->funding_str, "all");
|
|
|
|
|
2019-12-04 19:50:04 +01:00
|
|
|
return connect_to_peer(cmd, fr);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-27 00:42:00 +01:00
|
|
|
static void init(struct plugin *p,
|
2019-08-28 05:39:49 +02:00
|
|
|
const char *buf UNUSED, const jsmntok_t *config UNUSED)
|
|
|
|
{
|
2019-09-06 23:30:46 +02:00
|
|
|
/* Figure out what the 'placeholder' addr is */
|
|
|
|
const char *network_name;
|
|
|
|
u8 *placeholder = tal_hexdata(tmpctx, placeholder_script, strlen(placeholder_script));
|
|
|
|
|
2020-01-27 00:42:00 +01:00
|
|
|
network_name = rpc_delve(tmpctx, p, "listconfigs",
|
2019-09-06 23:30:46 +02:00
|
|
|
take(json_out_obj(NULL, "config",
|
|
|
|
"network")),
|
2020-01-27 00:42:00 +01:00
|
|
|
".network");
|
2019-09-06 23:30:46 +02:00
|
|
|
chainparams = chainparams_for_network(network_name);
|
2019-10-29 18:00:18 +01:00
|
|
|
placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, chainparams,
|
2019-09-06 23:30:46 +02:00
|
|
|
placeholder);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct plugin_command commands[] = { {
|
2019-08-28 23:28:26 +02:00
|
|
|
"fundchannel",
|
2019-08-28 05:39:49 +02:00
|
|
|
"channels",
|
|
|
|
"Fund channel with {id} using {satoshi} (or 'all'), at optional {feerate}. "
|
|
|
|
"Only use outputs that have {minconf} confirmations.",
|
|
|
|
"Initiaties a channel open with node 'id'. Must "
|
|
|
|
"be connected to the node and have enough funds available at the requested minimum confirmation "
|
|
|
|
"depth (minconf)",
|
|
|
|
json_fundchannel
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
setup_locale();
|
2019-12-05 11:28:32 +01:00
|
|
|
plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands),
|
|
|
|
NULL, 0, NULL, 0, NULL);
|
2019-08-28 05:39:49 +02:00
|
|
|
}
|