2013-08-21 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-10-04 21:02:17 +02:00
|
|
|
"encoding/hex"
|
2013-08-21 16:37:30 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcjson"
|
2013-09-03 15:49:16 +02:00
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2013-10-07 18:35:32 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-08-21 19:25:22 +02:00
|
|
|
"time"
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Standard JSON-RPC 2.0 errors
|
2013-08-21 17:14:21 +02:00
|
|
|
var (
|
|
|
|
InvalidRequest = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -32600,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Invalid request",
|
|
|
|
}
|
|
|
|
MethodNotFound = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -32601,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Method not found",
|
|
|
|
}
|
|
|
|
InvalidParams = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -32602,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Invalid paramaters",
|
|
|
|
}
|
|
|
|
InternalError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -32603,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Internal error",
|
|
|
|
}
|
|
|
|
ParseError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -32700,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Parse error",
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
)
|
2013-08-21 17:14:21 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// General application defined JSON errors
|
|
|
|
var (
|
2013-08-21 17:14:21 +02:00
|
|
|
MiscError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -1,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Miscellaneous error",
|
|
|
|
}
|
|
|
|
ForbiddenBySafeMode = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -2,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Server is in safe mode, and command is not allowed in safe mode",
|
|
|
|
}
|
|
|
|
TypeError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -3,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Unexpected type was passed as parameter",
|
|
|
|
}
|
|
|
|
InvalidAddressOrKey = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -5,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Invalid address or key",
|
|
|
|
}
|
|
|
|
OutOfMemory = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -7,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Ran out of memory during operation",
|
|
|
|
}
|
|
|
|
InvalidParameter = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -8,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Invalid, missing or duplicate parameter",
|
|
|
|
}
|
|
|
|
DatabaseError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -20,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Database error",
|
|
|
|
}
|
|
|
|
DeserializationError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -22,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Error parsing or validating structure in raw format",
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
)
|
2013-08-21 17:14:21 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Wallet JSON errors
|
|
|
|
var (
|
2013-08-21 17:14:21 +02:00
|
|
|
WalletError = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -4,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Unspecified problem with wallet",
|
|
|
|
}
|
|
|
|
WalletInsufficientFunds = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -6,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Not enough funds in wallet or account",
|
|
|
|
}
|
|
|
|
WalletInvalidAccountName = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -11,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Invalid account name",
|
|
|
|
}
|
|
|
|
WalletKeypoolRanOut = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -12,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Keypool ran out, call keypoolrefill first",
|
|
|
|
}
|
|
|
|
WalletUnlockNeeded = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -13,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Enter the wallet passphrase with walletpassphrase first",
|
|
|
|
}
|
|
|
|
WalletPassphraseIncorrect = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -14,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "The wallet passphrase entered was incorrect",
|
|
|
|
}
|
|
|
|
WalletWrongEncState = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -15,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Command given in wrong wallet encryption state",
|
|
|
|
}
|
|
|
|
WalletEncryptionFailed = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -16,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Failed to encrypt the wallet",
|
|
|
|
}
|
|
|
|
WalletAlreadyUnlocked = btcjson.Error{
|
2013-08-21 19:25:22 +02:00
|
|
|
Code: -17,
|
2013-08-21 17:14:21 +02:00
|
|
|
Message: "Wallet is already unlocked",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
// ProcessFrontendMsg checks the message sent from a frontend. If the
|
|
|
|
// message method is one that must be handled by btcwallet, the request
|
|
|
|
// is processed here. Otherwise, the message is sent to btcd.
|
|
|
|
func ProcessFrontendMsg(reply chan []byte, msg []byte) {
|
2013-09-09 20:14:57 +02:00
|
|
|
var jsonMsg btcjson.Message
|
|
|
|
if err := json.Unmarshal(msg, &jsonMsg); err != nil {
|
|
|
|
log.Errorf("ProcessFrontendMsg: Cannot unmarshal message: %v",
|
|
|
|
err)
|
2013-08-21 16:37:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
switch jsonMsg.Method {
|
2013-08-21 20:46:20 +02:00
|
|
|
// Standard bitcoind methods
|
2013-08-21 16:37:30 +02:00
|
|
|
case "getaddressesbyaccount":
|
2013-09-09 20:14:57 +02:00
|
|
|
GetAddressesByAccount(reply, &jsonMsg)
|
2013-09-03 17:00:01 +02:00
|
|
|
case "getbalance":
|
2013-09-09 20:14:57 +02:00
|
|
|
GetBalance(reply, &jsonMsg)
|
2013-08-21 16:37:30 +02:00
|
|
|
case "getnewaddress":
|
2013-09-09 20:14:57 +02:00
|
|
|
GetNewAddress(reply, &jsonMsg)
|
2013-10-09 17:23:54 +02:00
|
|
|
case "listaccounts":
|
|
|
|
ListAccounts(reply, &jsonMsg)
|
2013-09-09 20:14:57 +02:00
|
|
|
case "sendfrom":
|
|
|
|
SendFrom(reply, &jsonMsg)
|
|
|
|
case "sendmany":
|
|
|
|
SendMany(reply, &jsonMsg)
|
2013-10-07 21:14:39 +02:00
|
|
|
case "settxfee":
|
|
|
|
SetTxFee(reply, &jsonMsg)
|
2013-08-21 16:37:30 +02:00
|
|
|
case "walletlock":
|
2013-09-09 20:14:57 +02:00
|
|
|
WalletLock(reply, &jsonMsg)
|
2013-08-21 16:37:30 +02:00
|
|
|
case "walletpassphrase":
|
2013-09-09 20:14:57 +02:00
|
|
|
WalletPassphrase(reply, &jsonMsg)
|
2013-08-21 20:46:20 +02:00
|
|
|
|
|
|
|
// btcwallet extensions
|
2013-09-03 15:49:16 +02:00
|
|
|
case "createencryptedwallet":
|
2013-09-09 20:14:57 +02:00
|
|
|
CreateEncryptedWallet(reply, &jsonMsg)
|
2013-10-09 17:23:54 +02:00
|
|
|
case "getbalances":
|
|
|
|
GetBalances(reply, &jsonMsg)
|
2013-08-21 20:46:20 +02:00
|
|
|
case "walletislocked":
|
2013-09-09 20:14:57 +02:00
|
|
|
WalletIsLocked(reply, &jsonMsg)
|
|
|
|
case "btcdconnected":
|
|
|
|
BtcdConnected(reply, &jsonMsg)
|
2013-08-21 20:46:20 +02:00
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
default:
|
|
|
|
// btcwallet does not understand method. Pass to btcd.
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-09-09 20:14:57 +02:00
|
|
|
var id interface{} = fmt.Sprintf("btcwallet(%v)-%v", n,
|
|
|
|
jsonMsg.Id)
|
|
|
|
jsonMsg.Id = &id
|
|
|
|
newMsg, err := json.Marshal(jsonMsg)
|
2013-08-21 16:37:30 +02:00
|
|
|
if err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
log.Errorf("ProcessFrontendMsg: Cannot marshal message: %v",
|
|
|
|
err)
|
|
|
|
return
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
replyRouter.Lock()
|
|
|
|
replyRouter.m[n] = reply
|
|
|
|
replyRouter.Unlock()
|
|
|
|
btcdMsgs <- newMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 17:14:21 +02:00
|
|
|
// ReplyError creates and marshalls a btcjson.Reply with the error e,
|
|
|
|
// sending the reply to a reply channel.
|
|
|
|
func ReplyError(reply chan []byte, id interface{}, e *btcjson.Error) {
|
|
|
|
r := btcjson.Reply{
|
|
|
|
Error: e,
|
2013-08-21 19:25:22 +02:00
|
|
|
Id: &id,
|
|
|
|
}
|
2013-08-21 21:28:15 +02:00
|
|
|
if mr, err := json.Marshal(r); err == nil {
|
2013-08-21 19:25:22 +02:00
|
|
|
reply <- mr
|
2013-09-09 20:14:57 +02:00
|
|
|
} else {
|
|
|
|
log.Errorf("Cannot marshal json reply: %v", err)
|
2013-08-21 19:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplySuccess creates and marshalls a btcjson.Reply with the result r,
|
|
|
|
// sending the reply to a reply channel.
|
|
|
|
func ReplySuccess(reply chan []byte, id interface{}, result interface{}) {
|
|
|
|
r := btcjson.Reply{
|
|
|
|
Result: result,
|
2013-08-21 19:43:05 +02:00
|
|
|
Id: &id,
|
2013-08-21 17:14:21 +02:00
|
|
|
}
|
2013-08-21 21:26:00 +02:00
|
|
|
if mr, err := json.Marshal(r); err == nil {
|
2013-08-21 17:14:21 +02:00
|
|
|
reply <- mr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 17:00:01 +02:00
|
|
|
// GetAddressesByAccount replies with all addresses for an account.
|
2013-09-09 20:14:57 +02:00
|
|
|
func GetAddressesByAccount(reply chan []byte, msg *btcjson.Message) {
|
2013-10-08 04:17:27 +02:00
|
|
|
e := InvalidParams
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// TODO(jrick): check if we can make btcjson.Message.Params
|
|
|
|
// a []interface{} to avoid this.
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
2013-10-08 04:17:27 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
account, ok := params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "account is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
|
|
|
}
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2013-08-21 19:43:05 +02:00
|
|
|
var result interface{}
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.RLock()
|
2013-10-08 04:17:27 +02:00
|
|
|
w := wallets.m[account]
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.RUnlock()
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2013-09-03 06:10:32 +02:00
|
|
|
if w != nil {
|
|
|
|
result = w.Wallet.GetActiveAddresses()
|
2013-08-21 16:37:30 +02:00
|
|
|
} else {
|
2013-10-08 04:17:27 +02:00
|
|
|
ReplyError(reply, msg.Id, &WalletInvalidAccountName)
|
|
|
|
return
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, result)
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 17:00:01 +02:00
|
|
|
// GetBalance replies with the balance for an account (wallet). If
|
|
|
|
// the requested wallet does not exist, a JSON error will be returned to
|
|
|
|
// the client.
|
2013-09-09 20:14:57 +02:00
|
|
|
func GetBalance(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
log.Error("GetBalance: Cannot parse parameters.")
|
|
|
|
return
|
|
|
|
}
|
2013-09-03 17:00:01 +02:00
|
|
|
var wname string
|
2013-09-05 21:31:39 +02:00
|
|
|
conf := 1
|
2013-09-03 17:00:01 +02:00
|
|
|
if len(params) > 0 {
|
|
|
|
if s, ok := params[0].(string); ok {
|
|
|
|
wname = s
|
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InvalidParams)
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-05 21:31:39 +02:00
|
|
|
if len(params) > 1 {
|
|
|
|
if f, ok := params[1].(float64); ok {
|
|
|
|
conf = int(f)
|
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InvalidParams)
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 17:00:01 +02:00
|
|
|
|
|
|
|
wallets.RLock()
|
|
|
|
w := wallets.m[wname]
|
|
|
|
wallets.RUnlock()
|
|
|
|
var result interface{}
|
|
|
|
if w != nil {
|
2013-09-05 21:31:39 +02:00
|
|
|
result = w.CalculateBalance(conf)
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, result)
|
2013-09-03 17:00:01 +02:00
|
|
|
} else {
|
|
|
|
e := WalletInvalidAccountName
|
|
|
|
e.Message = fmt.Sprintf("Wallet for account '%s' does not exist.", wname)
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// GetBalances notifies each attached wallet of the current confirmed
|
|
|
|
// and unconfirmed account balances.
|
|
|
|
//
|
|
|
|
// TODO(jrick): Switch this to return a JSON object (map) of all accounts
|
|
|
|
// and their balances, instead of separate notifications for each account.
|
2013-10-09 17:23:54 +02:00
|
|
|
func GetBalances(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
wallets.RLock()
|
|
|
|
for _, w := range wallets.m {
|
|
|
|
balance := w.CalculateBalance(6)
|
|
|
|
unconfirmed := w.CalculateBalance(0) - balance
|
|
|
|
NotifyWalletBalance(reply, w.name, balance)
|
|
|
|
NotifyWalletBalanceUnconfirmed(reply, w.name, unconfirmed)
|
|
|
|
}
|
|
|
|
wallets.RUnlock()
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
// GetNewAddress gets or generates a new address for an account. If
|
|
|
|
// the requested wallet does not exist, a JSON error will be returned to
|
|
|
|
// the client.
|
2013-09-09 20:14:57 +02:00
|
|
|
func GetNewAddress(reply chan []byte, msg *btcjson.Message) {
|
2013-10-09 01:25:33 +02:00
|
|
|
e := InvalidParams
|
2013-09-09 20:14:57 +02:00
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
2013-10-09 01:25:33 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
|
|
|
}
|
2013-09-03 15:49:16 +02:00
|
|
|
var wname string
|
2013-10-09 01:25:33 +02:00
|
|
|
if len(params) > 0 {
|
|
|
|
var ok bool
|
|
|
|
if wname, ok = params[0].(string); !ok {
|
|
|
|
e.Message = "account is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wallets.RLock()
|
|
|
|
w := wallets.m[wname]
|
|
|
|
wallets.RUnlock()
|
|
|
|
if w != nil {
|
|
|
|
// TODO(jrick): generate new addresses if the address pool is empty.
|
2013-09-09 20:14:57 +02:00
|
|
|
addr, err := w.NextUnusedAddress()
|
|
|
|
if err != nil {
|
|
|
|
e := InternalError
|
|
|
|
e.Message = fmt.Sprintf("New address generation not implemented yet")
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
w.dirty = true
|
|
|
|
if err = w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-10-09 01:30:43 +02:00
|
|
|
w.ReqNewTxsForAddress(addr)
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, addr)
|
2013-09-03 15:49:16 +02:00
|
|
|
} else {
|
2013-09-03 16:21:14 +02:00
|
|
|
e := WalletInvalidAccountName
|
|
|
|
e.Message = fmt.Sprintf("Wallet for account '%s' does not exist.", wname)
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 17:23:54 +02:00
|
|
|
// ListAccounts returns a JSON object filled with account names as
|
|
|
|
// keys and their balances as values.
|
|
|
|
func ListAccounts(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
minconf := 1
|
|
|
|
e := InvalidParams
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if ok && len(params) != 0 {
|
|
|
|
fnum, ok := params[0].(float64)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "minconf is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
minconf = int(fnum)
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs := make(map[string]float64)
|
|
|
|
|
|
|
|
wallets.RLock()
|
|
|
|
for account, w := range wallets.m {
|
|
|
|
pairs[account] = w.CalculateBalance(minconf)
|
|
|
|
}
|
|
|
|
wallets.RUnlock()
|
|
|
|
|
|
|
|
ReplySuccess(reply, msg.Id, pairs)
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// SendFrom creates a new transaction spending unspent transaction
|
|
|
|
// outputs for a wallet to another payment address. Leftover inputs
|
|
|
|
// not sent to the payment address or a fee for the miner are sent
|
|
|
|
// back to a new address in the wallet.
|
|
|
|
func SendFrom(reply chan []byte, msg *btcjson.Message) {
|
2013-10-05 00:34:53 +02:00
|
|
|
e := InvalidParams
|
2013-09-09 20:14:57 +02:00
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
2013-10-05 00:34:53 +02:00
|
|
|
e.Message = "Cannot parse parameters."
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var fromaccount, toaddr58, comment, commentto string
|
|
|
|
var famt, minconf float64
|
|
|
|
if len(params) < 3 {
|
|
|
|
e.Message = "Too few parameters."
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fromaccount, ok = params[0].(string); !ok {
|
|
|
|
e.Message = "fromaccount is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if toaddr58, ok = params[1].(string); !ok {
|
|
|
|
e.Message = "tobitcoinaddress is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if famt, ok = params[2].(float64); !ok {
|
|
|
|
e.Message = "amount is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if famt < 0 {
|
|
|
|
e.Message = "amount cannot be negative"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
amt, err := btcjson.JSONToAmount(famt)
|
|
|
|
if err != nil {
|
|
|
|
e.Message = "amount cannot be converted to integer"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(params) > 3 {
|
|
|
|
if minconf, ok = params[3].(float64); !ok {
|
|
|
|
e.Message = "minconf is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if minconf < 0 {
|
|
|
|
e.Message = "minconf cannot be negative"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(params) > 4 {
|
|
|
|
if comment, ok = params[4].(string); !ok {
|
|
|
|
e.Message = "comment is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(params) > 5 {
|
|
|
|
if commentto, ok = params[5].(string); !ok {
|
|
|
|
e.Message = "comment-to is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is wallet for this account unlocked?
|
|
|
|
wallets.Lock()
|
|
|
|
w := wallets.m[fromaccount]
|
|
|
|
wallets.Unlock()
|
|
|
|
if w.IsLocked() {
|
|
|
|
ReplyError(reply, msg.Id, &WalletUnlockNeeded)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-07 21:14:39 +02:00
|
|
|
TxFee.Lock()
|
|
|
|
fee := TxFee.i
|
|
|
|
TxFee.Unlock()
|
2013-09-09 20:14:57 +02:00
|
|
|
pairs := map[string]uint64{
|
|
|
|
toaddr58: uint64(amt),
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
2013-10-15 16:05:51 +02:00
|
|
|
rawtx, inputs, err := w.txToPairs(pairs, uint64(fee), int(minconf))
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
e := InternalError
|
|
|
|
e.Message = err.Error()
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:02:17 +02:00
|
|
|
// Send rawtx off to btcd
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-10-15 15:42:10 +02:00
|
|
|
var id interface{} = fmt.Sprintf("btcwallet(%v)", n)
|
2013-10-04 21:02:17 +02:00
|
|
|
m, err := btcjson.CreateMessageWithId("sendrawtransaction", id,
|
|
|
|
hex.EncodeToString(rawtx))
|
|
|
|
if err != nil {
|
|
|
|
e := InternalError
|
|
|
|
e.Message = err.Error()
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
2013-10-15 15:42:10 +02:00
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = func(result interface{}, err *btcjson.Error) bool {
|
|
|
|
if err != nil {
|
|
|
|
ReplyError(reply, msg.Id, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-10-15 16:27:54 +02:00
|
|
|
// TODO(jrick): btcd cannot be trusted to successfully relay the
|
|
|
|
// tx to the Bitcoin network. Even if this succeeds, the rawtx
|
|
|
|
// must be saved and checked for if it exists in a later block.
|
|
|
|
// btcd will make a best try effort, but ultimately it's
|
|
|
|
// btcwallet's responsibility.
|
|
|
|
|
|
|
|
// Remove previous unspent outputs now spent by the tx.
|
|
|
|
w.UtxoStore.Lock()
|
|
|
|
modified := w.UtxoStore.s.Remove(inputs)
|
|
|
|
if modified {
|
|
|
|
w.UtxoStore.dirty = true
|
|
|
|
w.UtxoStore.Unlock()
|
2013-10-15 22:55:28 +02:00
|
|
|
if err := w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-10-15 16:27:54 +02:00
|
|
|
|
|
|
|
// Notify all frontends of new account balances.
|
|
|
|
confirmed := w.CalculateBalance(6)
|
|
|
|
unconfirmed := w.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
|
|
|
|
} else {
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplySuccess(reply, msg.Id, result)
|
|
|
|
|
2013-10-15 15:42:10 +02:00
|
|
|
// TODO(jrick): If message succeeded in being sent, save the
|
|
|
|
// transaction details with comments.
|
|
|
|
_, _ = comment, commentto
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-10-15 15:42:10 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
btcdMsgs <- m
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendMany creates a new transaction spending unspent transaction
|
|
|
|
// outputs for a wallet to any number of payment addresses. Leftover
|
|
|
|
// inputs not sent to the payment address or a fee for the miner are
|
|
|
|
// sent back to a new address in the wallet.
|
|
|
|
func SendMany(reply chan []byte, msg *btcjson.Message) {
|
2013-10-05 00:34:53 +02:00
|
|
|
e := InvalidParams
|
2013-09-09 20:14:57 +02:00
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
2013-10-05 00:34:53 +02:00
|
|
|
e.Message = "Cannot parse parameters."
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var fromaccount, comment string
|
|
|
|
var minconf float64
|
|
|
|
var jsonPairs map[string]interface{}
|
2013-10-04 21:02:17 +02:00
|
|
|
if len(params) < 2 {
|
2013-09-09 20:14:57 +02:00
|
|
|
e.Message = "Too few parameters."
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fromaccount, ok = params[0].(string); !ok {
|
|
|
|
e.Message = "fromaccount is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if jsonPairs, ok = params[1].(map[string]interface{}); !ok {
|
|
|
|
e.Message = "address and amount pairs is not a JSON object"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pairs := make(map[string]uint64)
|
|
|
|
for toaddr58, iamt := range jsonPairs {
|
|
|
|
famt, ok := iamt.(float64)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "amount is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if famt < 0 {
|
|
|
|
e.Message = "amount cannot be negative"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
amt, err := btcjson.JSONToAmount(famt)
|
|
|
|
if err != nil {
|
|
|
|
e.Message = "amount cannot be converted to integer"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pairs[toaddr58] = uint64(amt)
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:02:17 +02:00
|
|
|
if len(params) > 2 {
|
2013-09-09 20:14:57 +02:00
|
|
|
if minconf, ok = params[2].(float64); !ok {
|
|
|
|
e.Message = "minconf is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if minconf < 0 {
|
|
|
|
e.Message = "minconf cannot be negative"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-10-04 21:02:17 +02:00
|
|
|
return
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-04 21:02:17 +02:00
|
|
|
if len(params) > 3 {
|
2013-09-09 20:14:57 +02:00
|
|
|
if comment, ok = params[3].(string); !ok {
|
|
|
|
e.Message = "comment is not a string"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is wallet for this account unlocked?
|
|
|
|
wallets.Lock()
|
|
|
|
w := wallets.m[fromaccount]
|
|
|
|
wallets.Unlock()
|
|
|
|
if w.IsLocked() {
|
|
|
|
ReplyError(reply, msg.Id, &WalletUnlockNeeded)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-07 21:14:39 +02:00
|
|
|
TxFee.Lock()
|
|
|
|
fee := TxFee.i
|
|
|
|
TxFee.Unlock()
|
2013-10-15 16:05:51 +02:00
|
|
|
rawtx, inputs, err := w.txToPairs(pairs, uint64(fee), int(minconf))
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
e := InternalError
|
|
|
|
e.Message = err.Error()
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-04 21:02:17 +02:00
|
|
|
// Send rawtx off to btcd
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-10-15 15:42:10 +02:00
|
|
|
var id interface{} = fmt.Sprintf("btcwallet(%v)", n)
|
2013-10-04 21:02:17 +02:00
|
|
|
m, err := btcjson.CreateMessageWithId("sendrawtransaction", id,
|
|
|
|
hex.EncodeToString(rawtx))
|
|
|
|
if err != nil {
|
|
|
|
e := InternalError
|
|
|
|
e.Message = err.Error()
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
2013-10-15 15:42:10 +02:00
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = func(result interface{}, err *btcjson.Error) bool {
|
|
|
|
if err != nil {
|
|
|
|
ReplyError(reply, msg.Id, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-10-15 16:27:54 +02:00
|
|
|
// TODO(jrick): btcd cannot be trusted to successfully relay the
|
|
|
|
// tx to the Bitcoin network. Even if this succeeds, the rawtx
|
|
|
|
// must be saved and checked for if it exists in a later block.
|
|
|
|
// btcd will make a best try effort, but ultimately it's
|
|
|
|
// btcwallet's responsibility.
|
|
|
|
|
|
|
|
// Remove previous unspent outputs now spent by the tx.
|
|
|
|
w.UtxoStore.Lock()
|
|
|
|
modified := w.UtxoStore.s.Remove(inputs)
|
|
|
|
if modified {
|
|
|
|
w.UtxoStore.dirty = true
|
|
|
|
w.UtxoStore.Unlock()
|
2013-10-15 22:55:28 +02:00
|
|
|
if err := w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-10-15 16:27:54 +02:00
|
|
|
|
|
|
|
// Notify all frontends of new account balances.
|
|
|
|
confirmed := w.CalculateBalance(6)
|
|
|
|
unconfirmed := w.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
|
|
|
|
} else {
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplySuccess(reply, msg.Id, result)
|
|
|
|
|
2013-10-15 15:42:10 +02:00
|
|
|
// TODO(jrick): If message succeeded in being sent, save the
|
|
|
|
// transaction details with comments.
|
|
|
|
_ = comment
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-10-15 15:42:10 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
btcdMsgs <- m
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-07 21:14:39 +02:00
|
|
|
// SetTxFee sets the global transaction fee added to transactions.
|
|
|
|
func SetTxFee(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
e := InvalidParams
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(params) != 1 {
|
|
|
|
e.Message = "Incorrect number of parameters"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonFee, ok := params[0].(float64)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "Amount is not a number"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if jsonFee < 0 {
|
|
|
|
e.Message = "Amount cannot be negative"
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fee, err := btcjson.JSONToAmount(jsonFee)
|
|
|
|
if err != nil {
|
|
|
|
e.Message = fmt.Sprintf("Cannot convert JSON number to int64: %v", err)
|
|
|
|
ReplyError(reply, msg.Id, &e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(jrick): need to notify all frontends of new tx fee.
|
|
|
|
TxFee.Lock()
|
|
|
|
TxFee.i = fee
|
|
|
|
TxFee.Unlock()
|
|
|
|
|
|
|
|
ReplySuccess(reply, msg.Id, true)
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
// CreateEncryptedWallet creates a new encrypted wallet. The form of the command is:
|
|
|
|
//
|
|
|
|
// createencryptedwallet [account] [description] [passphrase]
|
|
|
|
//
|
|
|
|
// All three parameters are required, and must be of type string. If
|
|
|
|
// the wallet specified by account already exists, an invalid account
|
|
|
|
// name error is returned to the client.
|
2013-10-07 18:35:32 +02:00
|
|
|
//
|
2013-10-09 01:33:22 +02:00
|
|
|
// Wallets will be created on TestNet3, or MainNet if btcwallet is run with
|
|
|
|
// the --mainnet option.
|
2013-09-09 20:14:57 +02:00
|
|
|
func CreateEncryptedWallet(reply chan []byte, msg *btcjson.Message) {
|
2013-10-07 21:14:39 +02:00
|
|
|
e := InvalidParams
|
2013-09-09 20:14:57 +02:00
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
2013-10-07 21:14:39 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-09 20:14:57 +02:00
|
|
|
return
|
|
|
|
}
|
2013-09-03 15:49:16 +02:00
|
|
|
if len(params) != 3 {
|
2013-10-07 18:35:32 +02:00
|
|
|
e.Message = "Incorrect number of parameters"
|
2013-10-07 21:14:39 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-10-07 18:35:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
wname, ok := params[0].(string)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "Account is not a string"
|
2013-10-07 21:14:39 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-03 15:49:16 +02:00
|
|
|
return
|
|
|
|
}
|
2013-10-07 18:35:32 +02:00
|
|
|
desc, ok := params[1].(string)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "Description is not a string"
|
2013-10-07 21:14:39 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-10-07 18:35:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pass, ok := params[2].(string)
|
|
|
|
if !ok {
|
|
|
|
e.Message = "Passphrase is not a string"
|
2013-10-07 21:14:39 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-03 15:49:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does this wallet already exist?
|
2013-10-09 01:36:44 +02:00
|
|
|
wallets.Lock()
|
2013-09-03 15:49:16 +02:00
|
|
|
if w := wallets.m[wname]; w != nil {
|
|
|
|
e := WalletInvalidAccountName
|
|
|
|
e.Message = "Wallet already exists."
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &e)
|
2013-09-03 15:49:16 +02:00
|
|
|
return
|
|
|
|
}
|
2013-10-09 01:36:44 +02:00
|
|
|
defer wallets.Unlock()
|
2013-09-03 15:49:16 +02:00
|
|
|
|
2013-10-07 18:35:32 +02:00
|
|
|
var net btcwire.BitcoinNet
|
2013-10-07 19:25:05 +02:00
|
|
|
if cfg.MainNet {
|
2013-10-07 18:35:32 +02:00
|
|
|
net = btcwire.MainNet
|
2013-10-07 19:25:05 +02:00
|
|
|
} else {
|
|
|
|
net = btcwire.TestNet3
|
2013-10-07 18:35:32 +02:00
|
|
|
}
|
|
|
|
w, err := wallet.NewWallet(wname, desc, []byte(pass), net)
|
2013-09-03 15:49:16 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error creating wallet: " + err.Error())
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InternalError)
|
2013-09-03 15:49:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-05 18:50:39 +02:00
|
|
|
// Grab a new unique sequence number for tx notifications in new blocks.
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-09-05 18:50:39 +02:00
|
|
|
bw := &BtcWallet{
|
|
|
|
Wallet: w,
|
2013-10-09 17:23:54 +02:00
|
|
|
name: wname,
|
2013-10-15 21:53:49 +02:00
|
|
|
dirty: true,
|
2013-09-05 18:50:39 +02:00
|
|
|
NewBlockTxSeqN: n,
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
2013-09-05 18:50:39 +02:00
|
|
|
// TODO(jrick): only begin tracking wallet if btcwallet is already
|
|
|
|
// connected to btcd.
|
|
|
|
bw.Track()
|
|
|
|
|
|
|
|
wallets.m[wname] = bw
|
2013-10-15 22:55:28 +02:00
|
|
|
if err := bw.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, nil)
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:46:20 +02:00
|
|
|
// WalletIsLocked returns whether the wallet used by the specified
|
|
|
|
// account, or default account, is locked.
|
2013-09-09 20:14:57 +02:00
|
|
|
func WalletIsLocked(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
log.Error("WalletIsLocked: Cannot parse parameters.")
|
|
|
|
}
|
2013-08-21 20:46:20 +02:00
|
|
|
account := ""
|
|
|
|
if len(params) > 0 {
|
|
|
|
if acct, ok := params[0].(string); ok {
|
|
|
|
account = acct
|
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InvalidParams)
|
2013-08-21 20:46:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.RLock()
|
|
|
|
w := wallets.m[account]
|
|
|
|
wallets.RUnlock()
|
|
|
|
if w != nil {
|
2013-08-21 20:46:20 +02:00
|
|
|
result := w.IsLocked()
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, result)
|
2013-08-21 20:46:20 +02:00
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &WalletInvalidAccountName)
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
// WalletLock locks the wallet.
|
|
|
|
//
|
|
|
|
// TODO(jrick): figure out how multiple wallets/accounts will work
|
2013-09-09 20:14:57 +02:00
|
|
|
// with this. Lock all the wallets, like if all accounts are locked
|
|
|
|
// for one bitcoind wallet?
|
|
|
|
func WalletLock(reply chan []byte, msg *btcjson.Message) {
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.RLock()
|
|
|
|
w := wallets.m[""]
|
|
|
|
wallets.RUnlock()
|
|
|
|
if w != nil {
|
2013-08-21 19:25:22 +02:00
|
|
|
if err := w.Lock(); err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &WalletWrongEncState)
|
2013-08-21 19:25:22 +02:00
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, nil)
|
2013-10-09 17:23:54 +02:00
|
|
|
NotifyWalletLockStateChange("", true)
|
2013-08-21 19:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WalletPassphrase stores the decryption key for the default account,
|
|
|
|
// unlocking the wallet.
|
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// TODO(jrick): figure out how to do this for non-default accounts.
|
|
|
|
func WalletPassphrase(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
params, ok := msg.Params.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
log.Error("WalletPassphrase: Cannot parse parameters.")
|
|
|
|
return
|
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
if len(params) != 2 {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InvalidParams)
|
2013-08-21 16:37:30 +02:00
|
|
|
return
|
|
|
|
}
|
2013-08-21 18:07:57 +02:00
|
|
|
passphrase, ok1 := params[0].(string)
|
|
|
|
timeout, ok2 := params[1].(float64)
|
|
|
|
if !ok1 || !ok2 {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &InvalidParams)
|
2013-08-21 16:37:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.RLock()
|
|
|
|
w := wallets.m[""]
|
|
|
|
wallets.RUnlock()
|
|
|
|
if w != nil {
|
2013-08-21 18:07:57 +02:00
|
|
|
if err := w.Unlock([]byte(passphrase)); err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplyError(reply, msg.Id, &WalletPassphraseIncorrect)
|
2013-08-21 18:07:57 +02:00
|
|
|
return
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
ReplySuccess(reply, msg.Id, nil)
|
2013-10-09 17:23:54 +02:00
|
|
|
NotifyWalletLockStateChange("", false)
|
2013-08-21 16:37:30 +02:00
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Second * time.Duration(int64(timeout)))
|
|
|
|
w.Lock()
|
2013-10-09 17:23:54 +02:00
|
|
|
NotifyWalletLockStateChange("", true)
|
2013-08-21 16:37:30 +02:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2013-08-22 18:00:37 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// BtcdConnected is the wallet handler for the frontend
|
|
|
|
// 'btcdconnected' method. It returns to the frontend whether btcwallet
|
|
|
|
// is currently connected to btcd or not.
|
|
|
|
func BtcdConnected(reply chan []byte, msg *btcjson.Message) {
|
|
|
|
ReplySuccess(reply, msg.Id, btcdConnected.b)
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// AccountNtfn is a struct for marshalling any generic notification
|
|
|
|
// about a account for a wallet frontend.
|
|
|
|
//
|
|
|
|
// TODO(jrick): move to btcjson so it can be shared with frontends?
|
2013-10-09 17:23:54 +02:00
|
|
|
type AccountNtfn struct {
|
|
|
|
Account string `json:"account"`
|
|
|
|
Notification interface{} `json:"notification"`
|
|
|
|
}
|
|
|
|
|
2013-08-22 18:00:37 +02:00
|
|
|
// NotifyWalletLockStateChange sends a notification to all frontends
|
|
|
|
// that the wallet has just been locked or unlocked.
|
2013-10-09 17:23:54 +02:00
|
|
|
func NotifyWalletLockStateChange(account string, locked bool) {
|
2013-08-22 18:00:37 +02:00
|
|
|
var id interface{} = "btcwallet:newwalletlockstate"
|
|
|
|
m := btcjson.Reply{
|
2013-10-09 17:23:54 +02:00
|
|
|
Result: &AccountNtfn{
|
|
|
|
Account: account,
|
|
|
|
Notification: locked,
|
|
|
|
},
|
|
|
|
Id: &id,
|
2013-08-22 18:00:37 +02:00
|
|
|
}
|
|
|
|
msg, _ := json.Marshal(&m)
|
|
|
|
frontendNotificationMaster <- msg
|
|
|
|
}
|
2013-10-09 17:23:54 +02:00
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// NotifyWalletBalance sends a confirmed account balance notification
|
|
|
|
// to a frontend.
|
2013-10-09 17:23:54 +02:00
|
|
|
func NotifyWalletBalance(frontend chan []byte, account string, balance float64) {
|
|
|
|
var id interface{} = "btcwallet:accountbalance"
|
|
|
|
m := btcjson.Reply{
|
|
|
|
Result: &AccountNtfn{
|
|
|
|
Account: account,
|
|
|
|
Notification: balance,
|
|
|
|
},
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
msg, _ := json.Marshal(&m)
|
|
|
|
frontend <- msg
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// NotifyWalletBalanceUnconfirmed sends a confirmed account balance
|
|
|
|
// notification to a frontend.
|
2013-10-09 17:23:54 +02:00
|
|
|
func NotifyWalletBalanceUnconfirmed(frontend chan []byte, account string, balance float64) {
|
|
|
|
var id interface{} = "btcwallet:accountbalanceunconfirmed"
|
|
|
|
m := btcjson.Reply{
|
|
|
|
Result: &AccountNtfn{
|
|
|
|
Account: account,
|
|
|
|
Notification: balance,
|
|
|
|
},
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
msg, _ := json.Marshal(&m)
|
|
|
|
frontend <- msg
|
|
|
|
}
|