|
@ -816,51 +816,76 @@ static void gossip_peer_released(struct subd *gossip, |
|
|
fds[0], fds[1]); |
|
|
fds[0], fds[1]); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* json_fund_channel - Entrypoint for funding a channel |
|
|
|
|
|
*/ |
|
|
static void json_fund_channel(struct command *cmd, |
|
|
static void json_fund_channel(struct command *cmd, |
|
|
const char *buffer, const jsmntok_t *params) |
|
|
const char *buffer, const jsmntok_t *params) |
|
|
{ |
|
|
{ |
|
|
jsmntok_t *peertok, *satoshitok; |
|
|
jsmntok_t *desttok, *sattok; |
|
|
struct funding_channel *fc = tal(cmd, struct funding_channel); |
|
|
bool all_funds = false; |
|
|
|
|
|
struct funding_channel * fc; |
|
|
|
|
|
u32 feerate_per_kw = get_feerate(cmd->ld->topology, FEERATE_NORMAL); |
|
|
|
|
|
u64 fee_estimate; |
|
|
u8 *msg; |
|
|
u8 *msg; |
|
|
|
|
|
|
|
|
if (!json_get_params(cmd, buffer, params, |
|
|
if (!json_get_params(cmd, buffer, params, |
|
|
"id", &peertok, |
|
|
"id", &desttok, |
|
|
"satoshi", &satoshitok, |
|
|
"satoshi", &sattok, |
|
|
NULL)) { |
|
|
NULL)) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fc = tal(cmd, struct funding_channel); |
|
|
fc->cmd = cmd; |
|
|
fc->cmd = cmd; |
|
|
|
|
|
fc->change_keyindex = 0; |
|
|
|
|
|
fc->funding_satoshi = 0; |
|
|
|
|
|
|
|
|
if (!pubkey_from_hexstr(buffer + peertok->start, |
|
|
if (json_tok_streq(buffer, sattok, "all")) { |
|
|
peertok->end - peertok->start, &fc->peerid)) { |
|
|
all_funds = true; |
|
|
command_fail(cmd, "Could not parse id"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!json_tok_u64(buffer, satoshitok, &fc->funding_satoshi)) { |
|
|
} else if (!json_tok_u64(buffer, sattok, &fc->funding_satoshi)) { |
|
|
command_fail(cmd, "Invalid satoshis"); |
|
|
command_fail(cmd, "Invalid satoshis"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (fc->funding_satoshi > MAX_FUNDING_SATOSHI) { |
|
|
if (!pubkey_from_hexstr(buffer + desttok->start, |
|
|
command_fail(cmd, "Funding satoshi must be <= %d", |
|
|
desttok->end - desttok->start, &fc->peerid)) { |
|
|
MAX_FUNDING_SATOSHI); |
|
|
command_fail(cmd, "Could not parse id"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* FIXME: Support push_msat? */ |
|
|
/* FIXME: Support push_msat? */ |
|
|
fc->push_msat = 0; |
|
|
fc->push_msat = 0; |
|
|
fc->channel_flags = OUR_CHANNEL_FLAGS; |
|
|
fc->channel_flags = OUR_CHANNEL_FLAGS; |
|
|
|
|
|
|
|
|
/* Try to do this now, so we know if insufficient funds. */ |
|
|
/* Try to do this now, so we know if insufficient funds. */ |
|
|
/* FIXME: dustlimit */ |
|
|
/* FIXME: dustlimit */ |
|
|
fc->utxomap = build_utxos(fc, cmd->ld, fc->funding_satoshi, |
|
|
if (all_funds) { |
|
|
get_feerate(cmd->ld->topology, FEERATE_NORMAL), |
|
|
fc->utxomap = wallet_select_all(cmd, cmd->ld->wallet, |
|
|
600, BITCOIN_SCRIPTPUBKEY_P2WSH_LEN, |
|
|
feerate_per_kw, |
|
|
&fc->change, &fc->change_keyindex); |
|
|
BITCOIN_SCRIPTPUBKEY_P2WSH_LEN, |
|
|
if (!fc->utxomap) { |
|
|
&fc->funding_satoshi, |
|
|
command_fail(cmd, "Cannot afford funding transaction"); |
|
|
&fee_estimate); |
|
|
|
|
|
if (!fc->utxomap || fc->funding_satoshi < 546) { |
|
|
|
|
|
command_fail(cmd, "Cannot afford fee %"PRIu64, |
|
|
|
|
|
fee_estimate); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
fc->change = 0; |
|
|
|
|
|
} else { |
|
|
|
|
|
fc->utxomap = build_utxos(fc, cmd->ld, fc->funding_satoshi, |
|
|
|
|
|
feerate_per_kw, |
|
|
|
|
|
600, BITCOIN_SCRIPTPUBKEY_P2WSH_LEN, |
|
|
|
|
|
&fc->change, &fc->change_keyindex); |
|
|
|
|
|
if (!fc->utxomap) { |
|
|
|
|
|
command_fail(cmd, "Cannot afford funding transaction"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fc->funding_satoshi > MAX_FUNDING_SATOSHI) { |
|
|
|
|
|
command_fail(cmd, "Funding satoshi must be <= %d", |
|
|
|
|
|
MAX_FUNDING_SATOSHI); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|