@ -10,50 +10,18 @@
# include <common/json.h>
# include <common/json_command.h>
# include <common/json_helpers.h>
# include <common/json_stream.h>
# include <common/jsonrpc_errors.h>
# include <common/memleak.h>
# include <common/node_id.h>
# include <common/param.h>
# include <common/type_to_string.h>
# include <common/utils.h>
# include <common/wallet_tx.h>
# include <common/wireaddr.h>
# include <gossipd/routing.h>
# include <lightningd/chaintopology.h>
# include <lightningd/json.h>
# include <lightningd/jsonrpc.h>
# include <lightningd/options.h>
# include <sys/socket.h>
# include <wallet/wallet.h>
# include <wire/wire.h>
void json_add_node_id ( struct json_stream * response ,
const char * fieldname ,
const struct node_id * id )
{
json_add_hex ( response , fieldname , id - > k , sizeof ( id - > k ) ) ;
}
void json_add_pubkey ( struct json_stream * response ,
const char * fieldname ,
const struct pubkey * key )
{
u8 der [ PUBKEY_CMPR_LEN ] ;
pubkey_to_der ( der , key ) ;
json_add_hex ( response , fieldname , der , sizeof ( der ) ) ;
}
void json_add_txid ( struct json_stream * result , const char * fieldname ,
const struct bitcoin_txid * txid )
{
char hex [ hex_str_size ( sizeof ( * txid ) ) ] ;
bitcoin_txid_to_hex ( txid , hex , sizeof ( hex ) ) ;
json_add_string ( result , fieldname , hex ) ;
}
struct command_result * param_pubkey ( struct command * cmd , const char * name ,
const char * buffer , const jsmntok_t * tok ,
struct pubkey * * pubkey )
@ -83,16 +51,6 @@ struct command_result *param_txid(struct command *cmd,
json_tok_full ( buffer , tok ) ) ;
}
void json_add_short_channel_id ( struct json_stream * response ,
const char * fieldname ,
const struct short_channel_id * scid )
{
json_add_member ( response , fieldname , true , " %dx%dx%d " ,
short_channel_id_blocknum ( scid ) ,
short_channel_id_txnum ( scid ) ,
short_channel_id_outnum ( scid ) ) ;
}
struct command_result * param_short_channel_id ( struct command * cmd ,
const char * name ,
const char * buffer ,
@ -202,254 +160,6 @@ json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
cid , sizeof ( * cid ) ) ;
}
void json_add_address ( struct json_stream * response , const char * fieldname ,
const struct wireaddr * addr )
{
json_object_start ( response , fieldname ) ;
char * addrstr = tal_arr ( response , char , INET6_ADDRSTRLEN ) ;
if ( addr - > type = = ADDR_TYPE_IPV4 ) {
inet_ntop ( AF_INET , addr - > addr , addrstr , INET_ADDRSTRLEN ) ;
json_add_string ( response , " type " , " ipv4 " ) ;
json_add_string ( response , " address " , addrstr ) ;
json_add_num ( response , " port " , addr - > port ) ;
} else if ( addr - > type = = ADDR_TYPE_IPV6 ) {
inet_ntop ( AF_INET6 , addr - > addr , addrstr , INET6_ADDRSTRLEN ) ;
json_add_string ( response , " type " , " ipv6 " ) ;
json_add_string ( response , " address " , addrstr ) ;
json_add_num ( response , " port " , addr - > port ) ;
} else if ( addr - > type = = ADDR_TYPE_TOR_V2 ) {
json_add_string ( response , " type " , " torv2 " ) ;
json_add_string ( response , " address " , fmt_wireaddr_without_port ( tmpctx , addr ) ) ;
json_add_num ( response , " port " , addr - > port ) ;
} else if ( addr - > type = = ADDR_TYPE_TOR_V3 ) {
json_add_string ( response , " type " , " torv3 " ) ;
json_add_string ( response , " address " , fmt_wireaddr_without_port ( tmpctx , addr ) ) ;
json_add_num ( response , " port " , addr - > port ) ;
}
json_object_end ( response ) ;
}
void json_add_address_internal ( struct json_stream * response ,
const char * fieldname ,
const struct wireaddr_internal * addr )
{
switch ( addr - > itype ) {
case ADDR_INTERNAL_SOCKNAME :
json_object_start ( response , fieldname ) ;
json_add_string ( response , " type " , " local socket " ) ;
json_add_string ( response , " socket " , addr - > u . sockname ) ;
json_object_end ( response ) ;
return ;
case ADDR_INTERNAL_ALLPROTO :
json_object_start ( response , fieldname ) ;
json_add_string ( response , " type " , " any protocol " ) ;
json_add_num ( response , " port " , addr - > u . port ) ;
json_object_end ( response ) ;
return ;
case ADDR_INTERNAL_AUTOTOR :
json_object_start ( response , fieldname ) ;
json_add_string ( response , " type " , " Tor generated address " ) ;
json_add_address ( response , " service " , & addr - > u . torservice . address ) ;
json_object_end ( response ) ;
return ;
case ADDR_INTERNAL_STATICTOR :
json_object_start ( response , fieldname ) ;
json_add_string ( response , " type " , " Tor from blob generated static address " ) ;
json_add_address ( response , " service " , & addr - > u . torservice . address ) ;
json_object_end ( response ) ;
return ;
case ADDR_INTERNAL_FORPROXY :
json_object_start ( response , fieldname ) ;
json_add_string ( response , " type " , " unresolved " ) ;
json_add_string ( response , " name " , addr - > u . unresolved . name ) ;
json_add_num ( response , " port " , addr - > u . unresolved . port ) ;
json_object_end ( response ) ;
return ;
case ADDR_INTERNAL_WIREADDR :
json_add_address ( response , fieldname , & addr - > u . wireaddr ) ;
return ;
}
abort ( ) ;
}
void json_add_num ( struct json_stream * result , const char * fieldname , unsigned int value )
{
json_add_member ( result , fieldname , false , " %u " , value ) ;
}
void json_add_double ( struct json_stream * result , const char * fieldname , double value )
{
json_add_member ( result , fieldname , false , " %f " , value ) ;
}
void json_add_u64 ( struct json_stream * result , const char * fieldname ,
uint64_t value )
{
json_add_member ( result , fieldname , false , " % " PRIu64 , value ) ;
}
void json_add_s64 ( struct json_stream * result , const char * fieldname ,
int64_t value )
{
json_add_member ( result , fieldname , false , " % " PRIi64 , value ) ;
}
void json_add_u32 ( struct json_stream * result , const char * fieldname ,
uint32_t value )
{
json_add_member ( result , fieldname , false , " %u " , value ) ;
}
void json_add_s32 ( struct json_stream * result , const char * fieldname ,
int32_t value )
{
json_add_member ( result , fieldname , false , " %d " , value ) ;
}
void json_add_literal ( struct json_stream * result , const char * fieldname ,
const char * literal , int len )
{
/* Literal may contain quotes, so bypass normal checks */
char * dest = json_member_direct ( result , fieldname , strlen ( literal ) ) ;
if ( dest )
memcpy ( dest , literal , strlen ( literal ) ) ;
}
void json_add_string ( struct json_stream * result , const char * fieldname , const char * value TAKES )
{
json_add_member ( result , fieldname , true , " %s " , value ) ;
if ( taken ( value ) )
tal_free ( value ) ;
}
void json_add_bool ( struct json_stream * result , const char * fieldname , bool value )
{
json_add_member ( result , fieldname , false , value ? " true " : " false " ) ;
}
void json_add_null ( struct json_stream * stream , const char * fieldname )
{
json_add_member ( stream , fieldname , false , " null " ) ;
}
void json_add_hex ( struct json_stream * js , const char * fieldname ,
const void * data , size_t len )
{
/* Size without NUL term */
size_t hexlen = hex_str_size ( len ) - 1 ;
char * dest ;
dest = json_member_direct ( js , fieldname , 1 + hexlen + 1 ) ;
if ( dest ) {
dest [ 0 ] = ' " ' ;
if ( ! hex_encode ( data , len , dest + 1 , hexlen + 1 ) )
abort ( ) ;
dest [ 1 + hexlen ] = ' " ' ;
}
}
void json_add_hex_talarr ( struct json_stream * result ,
const char * fieldname ,
const tal_t * data )
{
json_add_hex ( result , fieldname , data , tal_bytelen ( data ) ) ;
}
void json_add_tx ( struct json_stream * result ,
const char * fieldname ,
const struct bitcoin_tx * tx )
{
json_add_hex_talarr ( result , fieldname , linearize_tx ( tmpctx , tx ) ) ;
}
void json_add_escaped_string ( struct json_stream * result , const char * fieldname ,
const struct json_escape * esc TAKES )
{
/* Already escaped, don't re-escape! */
char * dest = json_member_direct ( result , fieldname ,
1 + strlen ( esc - > s ) + 1 ) ;
if ( dest ) {
dest [ 0 ] = ' " ' ;
memcpy ( dest + 1 , esc - > s , strlen ( esc - > s ) ) ;
dest [ 1 + strlen ( esc - > s ) ] = ' " ' ;
}
if ( taken ( esc ) )
tal_free ( esc ) ;
}
void json_add_amount_msat_compat ( struct json_stream * result ,
struct amount_msat msat ,
const char * rawfieldname ,
const char * msatfieldname )
{
json_add_u64 ( result , rawfieldname , msat . millisatoshis ) ; /* Raw: low-level helper */
json_add_amount_msat_only ( result , msatfieldname , msat ) ;
}
void json_add_amount_msat_only ( struct json_stream * result ,
const char * msatfieldname ,
struct amount_msat msat )
{
json_add_string ( result , msatfieldname ,
type_to_string ( tmpctx , struct amount_msat , & msat ) ) ;
}
void json_add_amount_sat_compat ( struct json_stream * result ,
struct amount_sat sat ,
const char * rawfieldname ,
const char * msatfieldname )
{
json_add_u64 ( result , rawfieldname , sat . satoshis ) ; /* Raw: low-level helper */
json_add_amount_sat_only ( result , msatfieldname , sat ) ;
}
void json_add_amount_sat_only ( struct json_stream * result ,
const char * msatfieldname ,
struct amount_sat sat )
{
struct amount_msat msat ;
if ( amount_sat_to_msat ( & msat , sat ) )
json_add_string ( result , msatfieldname ,
type_to_string ( tmpctx , struct amount_msat , & msat ) ) ;
}
void json_add_timeabs ( struct json_stream * result , const char * fieldname ,
struct timeabs t )
{
json_add_member ( result , fieldname , false , " % " PRIu64 " .%03 " PRIu64 ,
( u64 ) t . ts . tv_sec , ( u64 ) t . ts . tv_nsec / 1000000 ) ;
}
void json_add_time ( struct json_stream * result , const char * fieldname ,
struct timespec ts )
{
char timebuf [ 100 ] ;
snprintf ( timebuf , sizeof ( timebuf ) , " %lu.%09u " ,
( unsigned long ) ts . tv_sec ,
( unsigned ) ts . tv_nsec ) ;
json_add_string ( result , fieldname , timebuf ) ;
}
void json_add_secret ( struct json_stream * response , const char * fieldname ,
const struct secret * secret )
{
json_add_hex ( response , fieldname , secret , sizeof ( struct secret ) ) ;
}
void json_add_sha256 ( struct json_stream * result , const char * fieldname ,
const struct sha256 * hash )
{
json_add_hex ( result , fieldname , hash , sizeof ( * hash ) ) ;
}
void json_add_preimage ( struct json_stream * result , const char * fieldname ,
const struct preimage * preimage )
{
json_add_hex ( result , fieldname , preimage , sizeof ( * preimage ) ) ;
}
/**
* segwit_addr_net_decode - Try to decode a Bech32 address and detect
* testnet / mainnet / regtest / signet
@ -582,46 +292,3 @@ struct command_result *param_bitcoin_address(struct command *cmd,
}
abort ( ) ;
}
void json_add_tok ( struct json_stream * result , const char * fieldname ,
const jsmntok_t * tok , const char * buffer )
{
int i = 0 ;
const jsmntok_t * t ;
switch ( tok - > type ) {
case JSMN_PRIMITIVE :
if ( json_tok_is_num ( buffer , tok ) ) {
json_to_int ( buffer , tok , & i ) ;
json_add_num ( result , fieldname , i ) ;
}
return ;
case JSMN_STRING :
if ( json_tok_streq ( buffer , tok , " true " ) )
json_add_bool ( result , fieldname , true ) ;
else if ( json_tok_streq ( buffer , tok , " false " ) )
json_add_bool ( result , fieldname , false ) ;
else
json_add_string ( result , fieldname , json_strdup ( tmpctx , buffer , tok ) ) ;
return ;
case JSMN_ARRAY :
json_array_start ( result , fieldname ) ;
json_for_each_arr ( i , t , tok )
json_add_tok ( result , NULL , t , buffer ) ;
json_array_end ( result ) ;
return ;
case JSMN_OBJECT :
json_object_start ( result , fieldname ) ;
json_for_each_obj ( i , t , tok )
json_add_tok ( result , json_strdup ( tmpctx , buffer , t ) , t + 1 , buffer ) ;
json_object_end ( result ) ;
return ;
case JSMN_UNDEFINED :
break ;
}
abort ( ) ;
}