mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
josn: Move json_tok_address_scriptpubkey
from lightningd/jsonrpc.c to lightningd/json.c
Its declaration is in lightningd/json.h, so we should move it to lightningd/json.c.
This commit is contained in:
parent
7fd2f6db6d
commit
8346d5c353
@ -1,8 +1,12 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <bitcoin/address.h>
|
||||
#include <bitcoin/base58.h>
|
||||
#include <bitcoin/script.h>
|
||||
#include <ccan/json_escape/json_escape.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/bech32.h>
|
||||
#include <common/json.h>
|
||||
#include <common/json_command.h>
|
||||
#include <common/json_helpers.h>
|
||||
@ -14,6 +18,7 @@
|
||||
#include <common/wallet_tx.h>
|
||||
#include <common/wireaddr.h>
|
||||
#include <gossipd/routing.h>
|
||||
#include <lightningd/chaintopology.h>
|
||||
#include <lightningd/json.h>
|
||||
#include <lightningd/json_stream.h>
|
||||
#include <lightningd/jsonrpc.h>
|
||||
@ -460,3 +465,110 @@ void json_add_sha256(struct json_stream *result, const char *fieldname,
|
||||
{
|
||||
json_add_hex(result, fieldname, hash, sizeof(*hash));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
@ -15,10 +15,6 @@
|
||||
/* eg: { "jsonrpc":"2.0", "method" : "dev-echo", "params" : [ "hello", "Arabella!" ], "id" : "1" } */
|
||||
#include "ccan/config.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <bitcoin/address.h>
|
||||
#include <bitcoin/base58.h>
|
||||
#include <bitcoin/script.h>
|
||||
#include <ccan/array_size/array_size.h>
|
||||
#include <ccan/asort/asort.h>
|
||||
#include <ccan/err/err.h>
|
||||
#include <ccan/io/io.h>
|
||||
@ -27,7 +23,6 @@
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <ccan/strmap/strmap.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/bech32.h>
|
||||
#include <common/configdir.h>
|
||||
#include <common/json_command.h>
|
||||
#include <common/jsonrpc_errors.h>
|
||||
@ -966,113 +961,6 @@ void jsonrpc_listen(struct jsonrpc *jsonrpc, struct lightningd *ld)
|
||||
log_debug(jsonrpc->log, "Listening on '%s'", ld->rpc_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
static struct command_result *param_command(struct command *cmd,
|
||||
const char *name,
|
||||
const char *buffer,
|
||||
|
Loading…
Reference in New Issue
Block a user