Browse Source

common: pull up `param_psbt`

Usable other places, not just in wallet
travis-experimental
niftynei 4 years ago
committed by Rusty Russell
parent
commit
e2a6fd7112
  1. 15
      common/json_tok.c
  2. 7
      common/json_tok.h
  3. 6
      tests/test_wallet.py
  4. 16
      wallet/walletrpc.c
  5. 6
      wallet/walletrpc.h

15
common/json_tok.c

@ -2,6 +2,7 @@
#include <bitcoin/base58.h>
#include <bitcoin/chainparams.h>
#include <bitcoin/feerate.h>
#include <bitcoin/psbt.h>
#include <bitcoin/script.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/json_escape/json_escape.h>
@ -499,3 +500,17 @@ struct command_result *param_bitcoin_address(struct command *cmd,
}
abort();
}
struct command_result *param_psbt(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct wally_psbt **psbt)
{
*psbt = psbt_from_b64(cmd, buffer + tok->start, tok->end - tok->start);
if (*psbt)
return NULL;
return command_fail_badparam(cmd, name, buffer, tok,
"Expected a PSBT");
}

7
common/json_tok.h

@ -16,6 +16,7 @@ struct command;
struct command_result;
struct json_escape;
struct sha256;
struct wally_psbt;
/* Extract json array token */
struct command_result *param_array(struct command *cmd, const char *name,
@ -165,4 +166,10 @@ struct command_result *param_bitcoin_address(struct command *cmd,
const char *buffer,
const jsmntok_t *tok,
const u8 **scriptpubkey);
struct command_result *param_psbt(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct wally_psbt **psbt);
#endif /* LIGHTNING_COMMON_JSON_TOK_H */

6
tests/test_wallet.py

@ -789,14 +789,14 @@ def test_sign_and_send_psbt(node_factory, bitcoind, chainparams):
bitcoind.rpc.sendrawtransaction(broadcast_tx['tx'])
# Try an empty PSBT
with pytest.raises(RpcError, match=r"should be a PSBT, not"):
with pytest.raises(RpcError, match=r"psbt: Expected a PSBT: invalid token"):
l1.rpc.signpsbt('')
with pytest.raises(RpcError, match=r"should be a PSBT, not"):
with pytest.raises(RpcError, match=r"psbt: Expected a PSBT: invalid token"):
l1.rpc.sendpsbt('')
# Try an invalid PSBT string
invalid_psbt = 'cHNidP8BAM0CAAAABJ9446mTRp/ml8OxSLC1hEvrcxG1L02AG7YZ4syHon2sAQAAAAD9////JFJH/NjKwjwrP9myuU68G7t8Q4VIChH0KUkZ5hSAyqcAAAAAAP3///8Uhrj0XDRhGRno8V7qEe4hHvZcmEjt3LQSIXWc+QU2tAEAAAAA/f///wstLikuBrgZJI83VPaY8aM7aPe5U6TMb06+jvGYzQLEAQAAAAD9////AcDGLQAAAAAAFgAUyQltQ/QI6lJgICYsza18hRa5KoEAAAAAAAEBH0BCDwAAAAAAFgAUqc1Qh7Q5kY1noDksmj7cJmHaIbQAAQEfQEIPAAAAAAAWABS3bdYeQbXvBSryHNoyYIiMBwu5rwABASBAQg8AAAAAABepFD1r0NuqAA+R7zDiXrlP7J+/PcNZhwEEFgAUKvGgVL/ThjWE/P1oORVXh/ObucYAAQEgQEIPAAAAAAAXqRRsrE5ugA1VJnAith5+msRMUTwl8ocBBBYAFMrfGCiLi0ZnOCY83ERKJ1sLYMY8A='
with pytest.raises(RpcError, match=r"should be a PSBT, not"):
with pytest.raises(RpcError, match=r"psbt: Expected a PSBT: invalid token"):
l1.rpc.signpsbt(invalid_psbt)
wallet_coin_mvts = [

16
wallet/walletrpc.c

@ -594,22 +594,6 @@ static const struct json_command listtransactions_command = {
};
AUTODATA(json_command, &listtransactions_command);
struct command_result *param_psbt(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct wally_psbt **psbt)
{
*psbt = psbt_from_b64(cmd, buffer + tok->start, tok->end - tok->start);
if (*psbt)
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a PSBT, not '%.*s'",
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
}
static bool in_only_inputs(const u32 *only_inputs, u32 this)
{
for (size_t i = 0; i < tal_count(only_inputs); i++)

6
wallet/walletrpc.h

@ -6,7 +6,6 @@
struct command;
struct json_stream;
struct utxo;
struct wally_psbt;
void json_add_utxos(struct json_stream *response,
struct wallet *wallet,
@ -15,9 +14,4 @@ void json_add_utxos(struct json_stream *response,
/* We evaluate reserved timeouts lazily, so use this. */
bool is_reserved(const struct utxo *utxo, u32 current_height);
struct command_result *param_psbt(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct wally_psbt **psbt);
#endif /* LIGHTNING_WALLET_WALLETRPC_H */

Loading…
Cancel
Save