2018-03-16 04:45:08 +01:00
|
|
|
#include <arpa/inet.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <bitcoin/address.h>
|
|
|
|
#include <bitcoin/base58.h>
|
2020-06-16 14:07:22 +02:00
|
|
|
#include <bitcoin/feerate.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <bitcoin/script.h>
|
2019-06-12 02:38:54 +02:00
|
|
|
#include <ccan/json_escape/json_escape.h>
|
2018-08-21 15:58:55 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2018-08-30 15:48:36 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2018-05-07 06:29:21 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <common/bech32.h>
|
2020-05-15 12:29:53 +02:00
|
|
|
#include <common/channel_id.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/json.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
2019-01-15 04:54:27 +01:00
|
|
|
#include <common/json_helpers.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
#include <common/memleak.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/type_to_string.h>
|
2019-10-15 12:58:30 +02:00
|
|
|
#include <common/utils.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <gossipd/routing.h>
|
2019-09-29 15:16:40 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
2018-08-10 22:16:22 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/options.h>
|
|
|
|
#include <sys/socket.h>
|
2018-04-30 14:54:39 +02:00
|
|
|
#include <wire/wire.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_pubkey(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey **pubkey)
|
2018-08-14 23:19:31 +02:00
|
|
|
{
|
|
|
|
*pubkey = tal(cmd, struct pubkey);
|
|
|
|
if (json_to_pubkey(buffer, tok, *pubkey))
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-14 23:19:31 +02:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a pubkey, not '%.*s'",
|
2019-06-05 07:28:53 +02:00
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct command_result *param_txid(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct bitcoin_txid **txid)
|
|
|
|
{
|
|
|
|
*txid = tal(cmd, struct bitcoin_txid);
|
|
|
|
if (json_to_txid(buffer, tok, *txid))
|
|
|
|
return NULL;
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be txid, not '%.*s'",
|
2018-12-16 05:50:06 +01:00
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
2018-08-14 23:19:31 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_short_channel_id(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
struct short_channel_id **scid)
|
2018-08-15 16:20:40 +02:00
|
|
|
{
|
|
|
|
*scid = tal(cmd, struct short_channel_id);
|
2019-09-06 08:41:41 +02:00
|
|
|
if (json_to_short_channel_id(buffer, tok, *scid))
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-15 16:20:40 +02:00
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a short channel id, not '%.*s'",
|
|
|
|
name, json_tok_full_len(tok),
|
|
|
|
json_tok_full(buffer, tok));
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:11:39 +02:00
|
|
|
const char *json_feerate_style_name(enum feerate_style style)
|
|
|
|
{
|
|
|
|
switch (style) {
|
|
|
|
case FEERATE_PER_KBYTE:
|
|
|
|
return "perkb";
|
|
|
|
case FEERATE_PER_KSIPA:
|
|
|
|
return "perkw";
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_feerate_style(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
enum feerate_style **style)
|
2018-08-27 07:11:39 +02:00
|
|
|
{
|
|
|
|
*style = tal(cmd, enum feerate_style);
|
|
|
|
if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
**style = FEERATE_PER_KSIPA;
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-27 07:11:39 +02:00
|
|
|
} else if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
**style = FEERATE_PER_KBYTE;
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-27 07:11:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
|
|
|
json_tok_full_len(tok), json_tok_full(buffer, tok));
|
2018-08-27 07:11:39 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *param_feerate(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
u32 **feerate)
|
2018-08-28 22:46:32 +02:00
|
|
|
{
|
|
|
|
jsmntok_t base = *tok, suffix = *tok;
|
|
|
|
enum feerate_style style;
|
|
|
|
unsigned int num;
|
|
|
|
|
2018-08-28 22:46:34 +02:00
|
|
|
for (size_t i = 0; i < NUM_FEERATES; i++) {
|
|
|
|
if (json_tok_streq(buffer, tok, feerate_name(i)))
|
2018-12-16 05:50:06 +01:00
|
|
|
return param_feerate_estimate(cmd, feerate, i);
|
2018-08-28 22:46:34 +02:00
|
|
|
}
|
chaintopology: better feerate targets differentiation
We kept track of an URGENT, a NORMAL, and a SLOW feerate. They were used
for opening (NORMAL), mutual (NORMAL), UNILATERAL (URGENT) transactions
as well as minimum and maximum estimations, and onchain resolution.
We now keep track of more fine-grained feerates:
- `opening` used for funding and also misc transactions
- `mutual_close` used for the mutual close transaction
- `unilateral_close` used for unilateral close (commitment transactions)
- `delayed_to_us` used for resolving our output from our unilateral close
- `htlc_resolution` used for resolving onchain HTLCs
- `penalty` used for resolving revoked transactions
We don't modify our requests to our Bitcoin backend, as the next commit
will batch them !
Changelog-deprecated: The "urgent", "slow", and "normal" field of the `feerates` command are now deprecated.
Changelog-added: The fields "opening", "mutual_close", "unilateral_close", "delayed_to_us", "htlc_resolution" and "penalty" have been added to the `feerates` command.
2020-03-10 17:52:13 +01:00
|
|
|
/* We used SLOW, NORMAL, and URGENT as feerate targets previously,
|
|
|
|
* and many commands rely on this syntax now.
|
|
|
|
* It's also really more natural for an user interface. */
|
|
|
|
if (json_tok_streq(buffer, tok, "slow"))
|
|
|
|
return param_feerate_estimate(cmd, feerate, FEERATE_MIN);
|
|
|
|
else if (json_tok_streq(buffer, tok, "normal"))
|
|
|
|
return param_feerate_estimate(cmd, feerate, FEERATE_OPENING);
|
|
|
|
else if (json_tok_streq(buffer, tok, "urgent"))
|
|
|
|
return param_feerate_estimate(cmd, feerate, FEERATE_UNILATERAL_CLOSE);
|
2018-08-28 22:46:34 +02:00
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
/* We have to split the number and suffix. */
|
|
|
|
suffix.start = suffix.end;
|
|
|
|
while (suffix.start > base.start && !isdigit(buffer[suffix.start-1])) {
|
|
|
|
suffix.start--;
|
|
|
|
base.end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_to_number(buffer, &base, &num)) {
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' prefix should be an integer, not '%.*s'",
|
|
|
|
name, base.end - base.start,
|
|
|
|
buffer + base.start);
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, &suffix, "")
|
|
|
|
|| json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
style = FEERATE_PER_KBYTE;
|
|
|
|
} else if (json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
style = FEERATE_PER_KSIPA;
|
|
|
|
} else {
|
2018-12-16 05:50:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' suffix should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
|
|
|
suffix.end - suffix.start,
|
|
|
|
buffer + suffix.start);
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*feerate = tal(cmd, u32);
|
|
|
|
**feerate = feerate_from_style(num, style);
|
2020-06-16 14:07:22 +02:00
|
|
|
if (**feerate < FEERATE_FLOOR)
|
|
|
|
**feerate = FEERATE_FLOOR;
|
2018-12-16 05:50:06 +01:00
|
|
|
return NULL;
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 14:54:39 +02:00
|
|
|
bool
|
|
|
|
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct channel_id *cid)
|
|
|
|
{
|
|
|
|
return hex_decode(buffer + tok->start, tok->end - tok->start,
|
|
|
|
cid, sizeof(*cid));
|
|
|
|
}
|
|
|
|
|
2019-09-29 15:16:40 +02:00
|
|
|
/**
|
|
|
|
* segwit_addr_net_decode - Try to decode a Bech32 address and detect
|
|
|
|
* testnet/mainnet/regtest/signet
|
|
|
|
*
|
|
|
|
* This processes the address and returns a string if it is a Bech32
|
|
|
|
* address specified by BIP173. The string is set whether it is
|
|
|
|
* testnet ("tb"), mainnet ("bc"), regtest ("bcrt"), or signet ("sb")
|
|
|
|
* It does not check, witness version and program size restrictions.
|
|
|
|
*
|
|
|
|
* Out: witness_version: Pointer to an int that will be updated to contain
|
|
|
|
* the witness program version (between 0 and 16 inclusive).
|
|
|
|
* witness_program: Pointer to a buffer of size 40 that will be updated
|
|
|
|
* to contain the witness program bytes.
|
|
|
|
* witness_program_len: Pointer to a size_t that will be updated to
|
|
|
|
* contain the length of bytes in witness_program.
|
|
|
|
* In: addrz: Pointer to the null-terminated address.
|
|
|
|
* Returns string containing the human readable segment of bech32 address
|
|
|
|
*/
|
|
|
|
static const char *segwit_addr_net_decode(int *witness_version,
|
|
|
|
uint8_t *witness_program,
|
|
|
|
size_t *witness_program_len,
|
|
|
|
const char *addrz,
|
|
|
|
const struct chainparams *chainparams)
|
|
|
|
{
|
|
|
|
if (segwit_addr_decode(witness_version, witness_program,
|
|
|
|
witness_program_len, chainparams->bip173_name,
|
|
|
|
addrz))
|
|
|
|
return chainparams->bip173_name;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum address_parse_result
|
|
|
|
json_to_address_scriptpubkey(const tal_t *ctx,
|
|
|
|
const struct chainparams *chainparams,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok, const u8 **scriptpubkey)
|
|
|
|
{
|
|
|
|
struct bitcoin_address destination;
|
|
|
|
int witness_version;
|
|
|
|
/* segwit_addr_net_decode requires a buffer of size 40, and will
|
|
|
|
* not write to the buffer if the address is too long, so a buffer
|
|
|
|
* of fixed size 40 will not overflow. */
|
|
|
|
uint8_t witness_program[40];
|
|
|
|
size_t witness_program_len;
|
|
|
|
|
|
|
|
char *addrz;
|
|
|
|
const char *bip173;
|
|
|
|
|
|
|
|
bool parsed;
|
|
|
|
bool right_network;
|
|
|
|
u8 addr_version;
|
|
|
|
|
|
|
|
parsed =
|
|
|
|
ripemd160_from_base58(&addr_version, &destination.addr,
|
|
|
|
buffer + tok->start, tok->end - tok->start);
|
|
|
|
|
|
|
|
if (parsed) {
|
|
|
|
if (addr_version == chainparams->p2pkh_version) {
|
|
|
|
*scriptpubkey = scriptpubkey_p2pkh(ctx, &destination);
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
} else if (addr_version == chainparams->p2sh_version) {
|
|
|
|
*scriptpubkey =
|
|
|
|
scriptpubkey_p2sh_hash(ctx, &destination.addr);
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return ADDRESS_PARSE_WRONG_NETWORK;
|
|
|
|
}
|
|
|
|
/* Insert other parsers that accept pointer+len here. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate null-terminated address. */
|
|
|
|
addrz = tal_dup_arr(ctx, char, buffer + tok->start, tok->end - tok->start, 1);
|
|
|
|
addrz[tok->end - tok->start] = '\0';
|
|
|
|
|
|
|
|
bip173 = segwit_addr_net_decode(&witness_version, witness_program,
|
|
|
|
&witness_program_len, addrz, chainparams);
|
|
|
|
|
|
|
|
if (bip173) {
|
|
|
|
bool witness_ok = false;
|
|
|
|
if (witness_version == 0 && (witness_program_len == 20 ||
|
|
|
|
witness_program_len == 32)) {
|
|
|
|
witness_ok = true;
|
|
|
|
}
|
|
|
|
/* Insert other witness versions here. */
|
|
|
|
|
|
|
|
if (witness_ok) {
|
|
|
|
*scriptpubkey = scriptpubkey_witness_raw(ctx, witness_version,
|
|
|
|
witness_program, witness_program_len);
|
|
|
|
parsed = true;
|
|
|
|
right_network = streq(bip173, chainparams->bip173_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Insert other parsers that accept null-terminated string here. */
|
|
|
|
|
|
|
|
tal_free(addrz);
|
|
|
|
|
|
|
|
if (parsed) {
|
|
|
|
if (right_network)
|
|
|
|
return ADDRESS_PARSE_SUCCESS;
|
|
|
|
else
|
|
|
|
return ADDRESS_PARSE_WRONG_NETWORK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ADDRESS_PARSE_UNRECOGNIZED;
|
|
|
|
}
|
2019-09-29 15:19:42 +02:00
|
|
|
|
|
|
|
struct command_result *param_bitcoin_address(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const u8 **scriptpubkey)
|
|
|
|
{
|
|
|
|
/* Parse address. */
|
|
|
|
switch (json_to_address_scriptpubkey(cmd,
|
2019-10-15 12:58:30 +02:00
|
|
|
chainparams,
|
2019-09-29 15:19:42 +02:00
|
|
|
buffer, tok,
|
|
|
|
scriptpubkey)) {
|
|
|
|
case ADDRESS_PARSE_UNRECOGNIZED:
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Could not parse destination address, "
|
|
|
|
"%s should be a valid address",
|
|
|
|
name ? name : "address field");
|
|
|
|
case ADDRESS_PARSE_WRONG_NETWORK:
|
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Destination address is not on network %s",
|
2019-10-15 12:58:30 +02:00
|
|
|
chainparams->network_name);
|
2019-09-29 15:19:42 +02:00
|
|
|
case ADDRESS_PARSE_SUCCESS:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|