<commit_before>// Copyright (c) 2014-2018 Crown Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "base58.h"
#include "rpcprotocol.h"
#include "rpcserver.h"
#include "platform/specialtx.h"
#include "specialtx-rpc-utils.h"

#ifdef ENABLE_WALLET
#include "wallet.h"
#endif//ENABLE_WALLET

namespace Platform
{

    // Allows to specify Crown address or priv key. In case of Crown address, the priv key is taken from the wallet
    CKey ParsePrivKeyOrAddress(const std::string & strKeyOrAddress, const std::string & paramName, bool allowAddresses /* = true */)
    {
        if (allowAddresses)
        {
            CKey key = PullPrivKeyFromWallet(strKeyOrAddress, paramName);
            if (key.IsValid())
                return key;
        }

        CBitcoinSecret secret;
        if (!secret.SetString(strKeyOrAddress) || !secret.IsValid())
            throw std::runtime_error(strprintf("invalid priv-key/address %s", strKeyOrAddress));
        return secret.GetKey();
    }

    CKey GetPrivKeyFromWallet(const CKeyID & keyId)
    {
#ifdef ENABLE_WALLET
        CKey key;
        if (!keyId.IsNull() || !pwalletMain->GetKey(keyId, key))
            throw std::runtime_error(strprintf("non-wallet or invalid address %s", keyId.ToString()));
        return key;
#else//ENABLE_WALLET
        throw std::runtime_error("addresses not supported in no-wallet builds");
#endif//ENABLE_WALLET
    }

    CKey PullPrivKeyFromWallet(const std::string & strAddress, const std::string & paramName)
    {
        CBitcoinAddress address;
        if (!address.SetString(strAddress) || !address.IsValid())
            throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid P2PKH address, not %s", paramName, strAddress));

#ifdef ENABLE_WALLET
        CKeyID keyId;
        CKey key;<commit_msg>fix GetPrivKeyFromWallet bug<commit_after>// Copyright (c) 2014-2018 Crown Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "base58.h"
#include "rpcprotocol.h"
#include "rpcserver.h"
#include "platform/specialtx.h"
#include "specialtx-rpc-utils.h"

#ifdef ENABLE_WALLET
#include "wallet.h"
#endif//ENABLE_WALLET

namespace Platform
{

    // Allows to specify Crown address or priv key. In case of Crown address, the priv key is taken from the wallet
    CKey ParsePrivKeyOrAddress(const std::string & strKeyOrAddress, const std::string & paramName, bool allowAddresses /* = true */)
    {
        if (allowAddresses)
        {
            CKey key = PullPrivKeyFromWallet(strKeyOrAddress, paramName);
            if (key.IsValid())
                return key;
        }

        CBitcoinSecret secret;
        if (!secret.SetString(strKeyOrAddress) || !secret.IsValid())
            throw std::runtime_error(strprintf("invalid priv-key/address %s", strKeyOrAddress));
        return secret.GetKey();
    }

    CKey GetPrivKeyFromWallet(const CKeyID & keyId)
    {
#ifdef ENABLE_WALLET
        CKey key;
        if (keyId.IsNull() || !pwalletMain->GetKey(keyId, key))
            throw std::runtime_error(strprintf("non-wallet or invalid address %s", keyId.ToString()));
        return key;
#else//ENABLE_WALLET
        throw std::runtime_error("addresses not supported in no-wallet builds");
#endif//ENABLE_WALLET
    }

    CKey PullPrivKeyFromWallet(const std::string & strAddress, const std::string & paramName)
    {
        CBitcoinAddress address;
        if (!address.SetString(strAddress) || !address.IsValid())
            throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be a valid P2PKH address, not %s", paramName, strAddress));

#ifdef ENABLE_WALLET
        CKeyID keyId;
        CKey key;
{'fraction_non_alphanumeric': 0.10964230171073094, 'fraction_numerical': 0.00673924313115604, 'mean_word_length': 3.226725082146769, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1547364', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1172, 'n_words': 303}