2222#include < util/fees.h>
2323#include < util/system.h>
2424#include < util/moneystr.h>
25+ #include < util/ref.h>
2526#include < util/string.h>
2627#include < util/translation.h>
2728#include < util/url.h>
2829#include < util/validation.h>
2930#include < util/vector.h>
3031#include < validation.h>
3132#include < wallet/coincontrol.h>
33+ #include < wallet/context.h>
3234#include < wallet/psbtwallet.h>
3335#include < wallet/rpcwallet.h>
3436#include < wallet/wallet.h>
@@ -94,6 +96,14 @@ void EnsureWalletIsUnlocked(CWallet * const pwallet)
9496 }
9597}
9698
99+ WalletContext& EnsureWalletContext (const util::Ref& context)
100+ {
101+ if (!context.Has <WalletContext>()) {
102+ throw JSONRPCError (RPC_INTERNAL_ERROR, " Wallet context not found" );
103+ }
104+ return context.Get <WalletContext>();
105+ }
106+
97107static void WalletTxToJSON (interfaces::Chain& chain, interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx, UniValue& entry)
98108{
99109 AssertLockHeld (cs_main); // for mapBlockIndex
@@ -2715,6 +2725,7 @@ static UniValue loadwallet(const JSONRPCRequest& request)
27152725 },
27162726 }.ToString ());
27172727
2728+ WalletContext& context = EnsureWalletContext (request.context );
27182729 WalletLocation location (request.params [0 ].get_str ());
27192730
27202731 if (!location.Exists ()) {
@@ -2729,7 +2740,7 @@ static UniValue loadwallet(const JSONRPCRequest& request)
27292740
27302741 bilingual_str error;
27312742 std::vector<bilingual_str> warnings;
2732- std::shared_ptr<CWallet> const wallet = LoadWallet (*g_rpc_chain , location, error, warnings);
2743+ std::shared_ptr<CWallet> const wallet = LoadWallet (*context. chain , location, error, warnings);
27332744 if (!wallet) throw JSONRPCError (RPC_WALLET_ERROR, error.original );
27342745
27352746 UniValue obj (UniValue::VOBJ);
@@ -2767,6 +2778,7 @@ static UniValue createwallet(const JSONRPCRequest& request)
27672778 throw std::runtime_error (help.ToString ());
27682779 }
27692780
2781+ WalletContext& context = EnsureWalletContext (request.context );
27702782 uint64_t flags = 0 ;
27712783 if (!request.params [1 ].isNull () && request.params [1 ].get_bool ()) {
27722784 flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
@@ -2788,7 +2800,7 @@ static UniValue createwallet(const JSONRPCRequest& request)
27882800
27892801 bilingual_str error;
27902802 std::shared_ptr<CWallet> wallet;
2791- WalletCreationStatus status = CreateWallet (*g_rpc_chain , passphrase, flags, request.params [0 ].get_str (), error, warnings, wallet);
2803+ WalletCreationStatus status = CreateWallet (*context. chain , passphrase, flags, request.params [0 ].get_str (), error, warnings, wallet);
27922804 switch (status) {
27932805 case WalletCreationStatus::CREATION_FAILED:
27942806 throw JSONRPCError (RPC_WALLET_ERROR, error.original );
@@ -4051,10 +4063,7 @@ static const CRPCCommand commands[] =
40514063};
40524064// clang-format on
40534065
4054- void RegisterWalletRPCCommands (interfaces::Chain& chain, std::vector<std::unique_ptr<interfaces::Handler>>& handlers )
4066+ Span< const CRPCCommand> GetWalletRPCCommands ( )
40554067{
4056- for (unsigned int vcidx = 0 ; vcidx < ARRAYLEN (commands); vcidx++)
4057- handlers.emplace_back (chain.handleRpc (commands[vcidx]));
4068+ return MakeSpan (commands);
40584069}
4059-
4060- interfaces::Chain* g_rpc_chain = nullptr ;
0 commit comments