| 2726 | |
| 2727 | |
| 2728 | static UniValue encryptwallet(const JSONRPCRequest& request) |
| 2729 | { |
| 2730 | std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); |
| 2731 | CWallet* const pwallet = wallet.get(); |
| 2732 | |
| 2733 | if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { |
| 2734 | return NullUniValue; |
| 2735 | } |
| 2736 | |
| 2737 | if (request.fHelp || request.params.size() != 1) { |
| 2738 | throw std::runtime_error( |
| 2739 | "encryptwallet \"passphrase\"\n" |
| 2740 | "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n" |
| 2741 | "After this, any calls that interact with private keys such as sending or signing \n" |
| 2742 | "will require the passphrase to be set prior the making these calls.\n" |
| 2743 | "Use the walletpassphrase call for this, and then walletlock call.\n" |
| 2744 | "If the wallet is already encrypted, use the walletpassphrasechange call.\n" |
| 2745 | "Note that this will shutdown the server.\n" |
| 2746 | "\nArguments:\n" |
| 2747 | "1. \"passphrase\" (string) The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long.\n" |
| 2748 | "\nExamples:\n" |
| 2749 | "\nEncrypt your wallet\n" |
| 2750 | + HelpExampleCli("encryptwallet", "\"my pass phrase\"") + |
| 2751 | "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n" |
| 2752 | + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") + |
| 2753 | "\nNow we can do something like sign\n" |
| 2754 | + HelpExampleCli("signmessage", "\"address\" \"test message\"") + |
| 2755 | "\nNow lock the wallet again by removing the passphrase\n" |
| 2756 | + HelpExampleCli("walletlock", "") + |
| 2757 | "\nAs a json rpc call\n" |
| 2758 | + HelpExampleRpc("encryptwallet", "\"my pass phrase\"") |
| 2759 | ); |
| 2760 | } |
| 2761 | |
| 2762 | LOCK2(cs_main, pwallet->cs_wallet); |
| 2763 | |
| 2764 | if (pwallet->IsCrypted()) { |
| 2765 | throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called."); |
| 2766 | } |
| 2767 | |
| 2768 | // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string) |
| 2769 | // Alternately, find a way to make request.params[0] mlock()'d to begin with. |
| 2770 | SecureString strWalletPass; |
| 2771 | strWalletPass.reserve(100); |
| 2772 | strWalletPass = request.params[0].get_str().c_str(); |
| 2773 | |
| 2774 | if (strWalletPass.length() < 1) |
| 2775 | throw std::runtime_error( |
| 2776 | "encryptwallet <passphrase>\n" |
| 2777 | "Encrypts the wallet with <passphrase>."); |
| 2778 | |
| 2779 | if (!pwallet->EncryptWallet(strWalletPass)) { |
| 2780 | throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet."); |
| 2781 | } |
| 2782 | |
| 2783 | // BDB seems to have a bad habit of writing old data into |
| 2784 | // slack space in .dat files; that is bad if the old data is |
| 2785 | // unencrypted private keys. So: |
nothing calls this directly
no test coverage detected