2013-08-21 16:37:30 +02:00
|
|
|
/*
|
2014-01-03 19:34:37 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-08-21 16:37:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
"github.com/conformal/btcjson"
|
2013-12-10 22:15:25 +01:00
|
|
|
"github.com/conformal/btcutil"
|
2013-11-22 19:42:25 +01:00
|
|
|
"github.com/conformal/btcwallet/tx"
|
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-11-12 18:01:32 +01:00
|
|
|
"github.com/conformal/btcws"
|
2013-12-17 19:18:09 +01:00
|
|
|
"sync"
|
2013-08-21 19:25:22 +02:00
|
|
|
"time"
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
type cmdHandler func(btcjson.Cmd) (interface{}, *btcjson.Error)
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2013-11-18 19:31:58 +01:00
|
|
|
var rpcHandlers = map[string]cmdHandler{
|
2013-12-30 18:44:51 +01:00
|
|
|
// Standard bitcoind methods (implemented)
|
2013-11-20 02:18:11 +01:00
|
|
|
"dumpprivkey": DumpPrivKey,
|
2013-12-30 22:31:33 +01:00
|
|
|
"getaccount": GetAccount,
|
2013-12-31 19:11:47 +01:00
|
|
|
"getaccountaddress": GetAccountAddress,
|
2013-11-12 18:01:32 +01:00
|
|
|
"getaddressesbyaccount": GetAddressesByAccount,
|
|
|
|
"getbalance": GetBalance,
|
|
|
|
"getnewaddress": GetNewAddress,
|
2013-11-20 02:18:11 +01:00
|
|
|
"importprivkey": ImportPrivKey,
|
2013-11-12 18:01:32 +01:00
|
|
|
"listaccounts": ListAccounts,
|
2013-11-22 19:42:25 +01:00
|
|
|
"listtransactions": ListTransactions,
|
2013-11-12 18:01:32 +01:00
|
|
|
"sendfrom": SendFrom,
|
|
|
|
"sendmany": SendMany,
|
|
|
|
"settxfee": SetTxFee,
|
|
|
|
"walletlock": WalletLock,
|
|
|
|
"walletpassphrase": WalletPassphrase,
|
|
|
|
|
2013-12-30 18:44:51 +01:00
|
|
|
// Standard bitcoind methods (currently unimplemented)
|
|
|
|
"addmultisigaddress": Unimplemented,
|
|
|
|
"backupwallet": Unimplemented,
|
|
|
|
"createmultisig": Unimplemented,
|
|
|
|
"dumpwallet": Unimplemented,
|
2014-01-15 20:53:09 +01:00
|
|
|
"getblocktemplate": Unimplemented,
|
2013-12-30 18:44:51 +01:00
|
|
|
"getrawchangeaddress": Unimplemented,
|
|
|
|
"getreceivedbyaccount": Unimplemented,
|
|
|
|
"getreceivedbyaddress": Unimplemented,
|
|
|
|
"gettransaction": Unimplemented,
|
|
|
|
"gettxout": Unimplemented,
|
|
|
|
"gettxoutsetinfo": Unimplemented,
|
2014-01-15 20:53:09 +01:00
|
|
|
"getwork": Unimplemented,
|
2013-12-30 18:44:51 +01:00
|
|
|
"importwallet": Unimplemented,
|
|
|
|
"keypoolrefill": Unimplemented,
|
|
|
|
"listaddressgroupings": Unimplemented,
|
|
|
|
"listlockunspent": Unimplemented,
|
|
|
|
"listreceivedbyaccount": Unimplemented,
|
|
|
|
"listsinceblock": Unimplemented,
|
2014-01-03 19:34:37 +01:00
|
|
|
"listreceivedbyaddress": Unimplemented,
|
2013-12-30 18:44:51 +01:00
|
|
|
"listunspent": Unimplemented,
|
|
|
|
"lockunspent": Unimplemented,
|
|
|
|
"move": Unimplemented,
|
|
|
|
"sendtoaddress": Unimplemented,
|
|
|
|
"setaccount": Unimplemented,
|
|
|
|
"signmessage": Unimplemented,
|
|
|
|
"signrawtransaction": Unimplemented,
|
|
|
|
"validateaddress": Unimplemented,
|
|
|
|
"verifymessage": Unimplemented,
|
|
|
|
"walletpassphrasechange": Unimplemented,
|
|
|
|
|
|
|
|
// Standard bitcoind methods which won't be implemented by btcwallet.
|
|
|
|
"encryptwallet": Unsupported,
|
|
|
|
|
2013-11-18 19:31:58 +01:00
|
|
|
// Extensions not exclusive to websocket connections.
|
2013-11-12 18:01:32 +01:00
|
|
|
"createencryptedwallet": CreateEncryptedWallet,
|
2013-11-18 19:31:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extensions exclusive to websocket connections.
|
|
|
|
var wsHandlers = map[string]cmdHandler{
|
2013-12-30 17:10:06 +01:00
|
|
|
"getaddressbalance": GetAddressBalance,
|
2013-12-30 21:11:41 +01:00
|
|
|
"getunconfirmedbalance": GetUnconfirmedBalance,
|
2013-12-30 17:10:06 +01:00
|
|
|
"listaddresstransactions": ListAddressTransactions,
|
|
|
|
"listalltransactions": ListAllTransactions,
|
|
|
|
"walletislocked": WalletIsLocked,
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ProcessFrontendRequest checks the requests sent from a frontend. If the
|
2013-11-20 02:18:11 +01:00
|
|
|
// request method is one that must be handled by btcwallet, the
|
|
|
|
// request is processed here. Otherwise, the request is sent to btcd
|
|
|
|
// and btcd's reply is routed back to the frontend.
|
2014-01-03 19:34:37 +01:00
|
|
|
func ProcessFrontendRequest(msg []byte, ws bool) *btcjson.Reply {
|
|
|
|
// Parse marshaled command.
|
2013-11-12 18:01:32 +01:00
|
|
|
cmd, err := btcjson.ParseMarshaledCmd(msg)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil || cmd.Id() == nil {
|
|
|
|
// Invalid JSON-RPC request.
|
|
|
|
response := &btcjson.Reply{
|
|
|
|
Error: &btcjson.ErrInvalidRequest,
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return response
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
id := cmd.Id()
|
|
|
|
var result interface{}
|
|
|
|
var jsonErr *btcjson.Error
|
|
|
|
|
|
|
|
// Check for a handler to reply to cmd. If none exist, defer handlng
|
|
|
|
// to btcd.
|
2013-11-18 19:31:58 +01:00
|
|
|
if f, ok := rpcHandlers[cmd.Method()]; ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
result, jsonErr = f(cmd)
|
2013-11-18 19:31:58 +01:00
|
|
|
} else if f, ok := wsHandlers[cmd.Method()]; ws && ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
result, jsonErr = f(cmd)
|
2013-11-12 18:01:32 +01:00
|
|
|
} else {
|
2014-01-03 19:34:37 +01:00
|
|
|
// btcwallet does not have a handler for the command, so ask
|
|
|
|
// btcd.
|
|
|
|
result, jsonErr = DeferToBtcd(cmd)
|
2013-08-21 19:25:22 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Create and return response.
|
|
|
|
response := &btcjson.Reply{
|
2013-08-21 19:43:05 +02:00
|
|
|
Id: &id,
|
2014-01-03 19:34:37 +01:00
|
|
|
Result: result,
|
|
|
|
Error: jsonErr,
|
2013-08-21 17:14:21 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return response
|
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// DeferToBtcd sends a marshaled JSON-RPC request to btcd and returns
|
|
|
|
// the reply.
|
|
|
|
func DeferToBtcd(cmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
|
|
|
// Update cmd with a new ID so replies can be handled without frontend
|
|
|
|
// IDs clashing with requests originating in btcwallet. The original
|
|
|
|
// request ID is always used in the frontend's response.
|
|
|
|
cmd.SetId(<-NewJSONID)
|
|
|
|
|
|
|
|
request := NewRPCRequest(cmd, nil)
|
|
|
|
response := <-CurrentRPCConn().SendRequest(request)
|
|
|
|
return response.Result, response.Err
|
2013-08-21 17:14:21 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Unimplemented handles an unimplemented RPC request with the
|
2013-12-30 18:44:51 +01:00
|
|
|
// appropiate error.
|
2014-01-03 19:34:37 +01:00
|
|
|
func Unimplemented(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
|
|
|
return nil, &btcjson.ErrUnimplemented
|
2013-12-30 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Unsupported handles a standard bitcoind RPC request which is
|
2013-12-30 18:44:51 +01:00
|
|
|
// unsupported by btcwallet due to design differences.
|
2014-01-03 19:34:37 +01:00
|
|
|
func Unsupported(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
|
|
|
e := btcjson.Error{
|
2013-12-30 18:44:51 +01:00
|
|
|
Code: -1,
|
2014-01-03 19:34:37 +01:00
|
|
|
Message: "Request unsupported by btcwallet",
|
2013-12-30 18:44:51 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-30 18:44:51 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// DumpPrivKey handles a dumpprivkey request with the private key
|
|
|
|
// for a single address, or an appropiate error if the wallet
|
2013-11-20 02:18:11 +01:00
|
|
|
// is locked.
|
2014-01-03 19:34:37 +01:00
|
|
|
func DumpPrivKey(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.DumpPrivKeyCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
addr, err := btcutil.DecodeAddr(cmd.Address)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch key, err := accountstore.DumpWIFPrivateKey(addr); err {
|
2013-12-02 20:56:06 +01:00
|
|
|
case nil:
|
|
|
|
// Key was found.
|
2014-01-03 19:34:37 +01:00
|
|
|
return key, nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
case wallet.ErrWalletLocked:
|
|
|
|
// Address was found, but the private key isn't
|
|
|
|
// accessible.
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletUnlockNeeded
|
2013-11-20 02:18:11 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// DumpWallet handles a dumpwallet request by returning all private
|
|
|
|
// keys in a wallet, or an appropiate error if the wallet is locked.
|
2013-11-20 02:44:37 +01:00
|
|
|
// TODO: finish this to match bitcoind by writing the dump to a file.
|
2014-01-03 19:34:37 +01:00
|
|
|
func DumpWallet(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Type assert icmd to access parameters.
|
2014-01-03 19:34:37 +01:00
|
|
|
_, ok := icmd.(*btcjson.DumpWalletCmd)
|
2013-11-20 02:18:11 +01:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
switch keys, err := accountstore.DumpKeys(); err {
|
|
|
|
case nil:
|
|
|
|
// Reply with sorted WIF encoded private keys
|
2014-01-03 19:34:37 +01:00
|
|
|
return keys, nil
|
2013-11-20 02:18:11 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
case wallet.ErrWalletLocked:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletUnlockNeeded
|
2013-11-20 02:18:11 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default: // any other non-nil error
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetAddressesByAccount handles a getaddressesbyaccount request by returning
|
2013-11-12 18:01:32 +01:00
|
|
|
// all addresses for an account, or an error if the requested account does
|
|
|
|
// not exist.
|
2014-01-03 19:34:37 +01:00
|
|
|
func GetAddressesByAccount(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetAddressesByAccountCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
switch a, err := accountstore.Account(cmd.Account); err {
|
|
|
|
case nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return sorted active payment addresses.
|
|
|
|
return a.SortedActivePaymentAddresses(), nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetBalance handles a getbalance request by returning the balance for an
|
2013-11-12 18:01:32 +01:00
|
|
|
// account (wallet), or an error if the requested account does not
|
|
|
|
// exist.
|
2014-01-03 19:34:37 +01:00
|
|
|
func GetBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetBalanceCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-03 17:00:01 +02:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
balance, err := accountstore.CalculateBalance(cmd.Account, cmd.MinConf)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return calculated balance.
|
|
|
|
return balance, nil
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetAccount handles a getaccount request by returning the account name
|
|
|
|
// associated with a single address.
|
|
|
|
func GetAccount(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-30 22:31:33 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetAccountCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is address valid?
|
2014-01-06 18:24:29 +01:00
|
|
|
addr, err := btcutil.DecodeAddr(cmd.Address)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
var net btcwire.BitcoinNet
|
|
|
|
switch a := addr.(type) {
|
|
|
|
case *btcutil.AddressPubKeyHash:
|
|
|
|
net = a.Net()
|
|
|
|
|
|
|
|
case *btcutil.AddressScriptHash:
|
|
|
|
net = a.Net()
|
|
|
|
|
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
if net != cfg.Net() {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up account which holds this address.
|
|
|
|
aname, err := LookupAccountByAddress(cmd.Address)
|
|
|
|
if err == ErrNotFound {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-30 22:31:33 +01:00
|
|
|
Code: btcjson.ErrInvalidAddressOrKey.Code,
|
|
|
|
Message: "Address not found in wallet",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
return aname, nil
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetAccountAddress handles a getaccountaddress by returning the most
|
2013-12-31 19:11:47 +01:00
|
|
|
// recently-created chained address that has not yet been used (does not yet
|
|
|
|
// appear in the blockchain, or any tx that has arrived in the btcd mempool).
|
|
|
|
// If the most recently-requested address has been used, a new address (the
|
|
|
|
// next chained address in the keypool) is used. This can fail if the keypool
|
|
|
|
// runs out (and will return btcjson.ErrWalletKeypoolRanOut if that happens).
|
2014-01-03 19:34:37 +01:00
|
|
|
func GetAccountAddress(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-31 19:11:47 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetAccountAddressCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup account for this request.
|
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-31 19:11:47 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-31 19:11:47 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch addr, err := a.CurrentAddress(); err {
|
|
|
|
case nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
return addr.EncodeAddress(), nil
|
2013-12-31 19:11:47 +01:00
|
|
|
|
|
|
|
case wallet.ErrWalletLocked:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletKeypoolRanOut
|
2013-12-31 19:11:47 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-31 19:11:47 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetAddressBalance handles a getaddressbalance extension request by
|
|
|
|
// returning the current balance (sum of unspent transaction output amounts)
|
|
|
|
// for a single address.
|
|
|
|
func GetAddressBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-10 22:15:25 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.GetAddressBalanceCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is address valid?
|
2014-01-06 18:24:29 +01:00
|
|
|
addr, err := btcutil.DecodeAddr(cmd.Address)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
apkh, ok := addr.(*btcutil.AddressPubKeyHash)
|
|
|
|
if !ok || apkh.Net() != cfg.Net() {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up account which holds this address.
|
|
|
|
aname, err := LookupAccountByAddress(cmd.Address)
|
|
|
|
if err == ErrNotFound {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-10 22:15:25 +01:00
|
|
|
Code: btcjson.ErrInvalidAddressOrKey.Code,
|
|
|
|
Message: "Address not found in wallet",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the account which holds the address in the request.
|
|
|
|
// This should not fail, so if it does, return an internal
|
|
|
|
// error to the frontend.
|
|
|
|
a, err := accountstore.Account(aname)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
bal := a.CalculateAddressBalance(apkh, int(cmd.Minconf))
|
2014-01-03 19:34:37 +01:00
|
|
|
return bal, nil
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetUnconfirmedBalance handles a getunconfirmedbalance extension request
|
|
|
|
// by returning the current unconfirmed balance of an account.
|
|
|
|
func GetUnconfirmedBalance(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-30 21:11:41 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.GetUnconfirmedBalanceCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the account included in the request.
|
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-30 21:11:41 +01:00
|
|
|
|
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-30 21:11:41 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
confirmed := a.CalculateBalance(1)
|
|
|
|
unconfirmed := a.CalculateBalance(0) - confirmed
|
2014-01-03 19:34:37 +01:00
|
|
|
return unconfirmed, nil
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ImportPrivKey handles an importprivkey request by parsing
|
2013-11-20 02:18:11 +01:00
|
|
|
// a WIF-encoded private key and adding it to an account.
|
2014-01-03 19:34:37 +01:00
|
|
|
func ImportPrivKey(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ImportPrivKeyCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Get the acount included in the request. Yes, Label is the
|
|
|
|
// account name...
|
|
|
|
a, err := accountstore.Account(cmd.Label)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-11-20 02:18:11 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-20 02:18:11 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Import the private key, handling any errors.
|
|
|
|
switch err := a.ImportPrivKey(cmd.PrivKey, cmd.Rescan); err {
|
|
|
|
case nil:
|
|
|
|
// If the import was successful, reply with nil.
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
2014-01-15 20:07:08 +01:00
|
|
|
case wallet.ErrDuplicate:
|
|
|
|
// Do not return duplicate key errors to the client.
|
|
|
|
return nil, nil
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
case wallet.ErrWalletLocked:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletUnlockNeeded
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:44:24 +01:00
|
|
|
// NotifyBalances notifies an attached frontend of the current confirmed
|
2013-10-14 22:39:15 +02:00
|
|
|
// and unconfirmed account balances.
|
|
|
|
//
|
2013-12-02 20:56:06 +01:00
|
|
|
// TODO(jrick): Switch this to return a single JSON object
|
|
|
|
// (map[string]interface{}) of all accounts and their balances, instead of
|
|
|
|
// separate notifications for each account.
|
2013-11-15 17:44:24 +01:00
|
|
|
func NotifyBalances(frontend chan []byte) {
|
2013-12-02 20:56:06 +01:00
|
|
|
accountstore.NotifyBalances(frontend)
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetNewAddress handlesa getnewaddress request by returning a new
|
|
|
|
// address for an account. If the account does not exist or the keypool
|
|
|
|
// ran out with a locked wallet, an appropiate error is returned.
|
|
|
|
func GetNewAddress(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
2013-11-12 18:40:20 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.GetNewAddressCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-09-03 15:49:16 +02:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
case ErrBtcdDisconnected:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-21 17:57:28 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: "btcd disconnected",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-21 17:57:28 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
addr, err := a.NewAddress()
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return the new payment address string.
|
|
|
|
return addr.EncodeAddress(), nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
case wallet.ErrWalletLocked:
|
2013-11-21 17:57:28 +01:00
|
|
|
// The wallet is locked error may be sent if the keypool needs
|
|
|
|
// to be refilled, but the wallet is currently in a locked
|
|
|
|
// state. Notify the frontend that an unlock is needed to
|
|
|
|
// refill the keypool.
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletKeypoolRanOut
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-21 17:57:28 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ListAccounts handles a listaccounts request by returning a map of account
|
|
|
|
// names to their balances.
|
|
|
|
func ListAccounts(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ListAccountsCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return the map. This will be marshaled into a JSON object.
|
|
|
|
return accountstore.ListAccounts(cmd.MinConf), nil
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ListTransactions handles a listtransactions request by returning an
|
|
|
|
// array of maps with details of sent and recevied wallet transactions.
|
|
|
|
func ListTransactions(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-22 19:42:25 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ListTransactionsCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-11-22 19:42:25 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
2013-11-22 19:42:25 +01:00
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
switch txList, err := a.ListTransactions(cmd.From, cmd.Count); err {
|
|
|
|
case nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return the list of tx information.
|
|
|
|
return txList, nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
case ErrBtcdDisconnected:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: "btcd disconnected",
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-22 19:42:25 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ListAddressTransactions handles a listaddresstransactions request by
|
|
|
|
// returning an array of maps with details of spent and received wallet
|
|
|
|
// transactions. The form of the reply is identical to listtransactions,
|
|
|
|
// but the array elements are limited to transaction details which are
|
|
|
|
// about the addresess included in the request.
|
|
|
|
func ListAddressTransactions(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-30 17:10:06 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.ListAddressTransactionsCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-30 17:10:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-30 17:10:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
// Decode addresses.
|
2013-12-30 17:10:06 +01:00
|
|
|
pkHashMap := make(map[string]struct{})
|
2014-01-06 18:24:29 +01:00
|
|
|
for _, addrStr := range cmd.Addresses {
|
|
|
|
addr, err := btcutil.DecodeAddr(addrStr)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
apkh, ok := addr.(*btcutil.AddressPubKeyHash)
|
|
|
|
if !ok || apkh.Net() != cfg.Net() {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInvalidAddressOrKey
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
2014-01-06 18:24:29 +01:00
|
|
|
pkHashMap[string(addr.ScriptAddress())] = struct{}{}
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
txList, err := a.ListAddressTransactions(pkHashMap)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-30 17:10:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return txList, nil
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ListAllTransactions handles a listalltransactions request by returning
|
|
|
|
// a map with details of sent and recevied wallet transactions. This is
|
|
|
|
// similar to ListTransactions, except it takes only a single optional
|
|
|
|
// argument for the account name and replies with all transactions.
|
|
|
|
func ListAllTransactions(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-02 23:34:36 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.ListAllTransactionsCmd)
|
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-02 23:34:36 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 23:34:36 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch txList, err := a.ListAllTransactions(); err {
|
|
|
|
case nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
// Return the list of tx information.
|
|
|
|
return txList, nil
|
2013-12-02 23:34:36 +01:00
|
|
|
|
|
|
|
case ErrBtcdDisconnected:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 23:34:36 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: "btcd disconnected",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 23:34:36 +01:00
|
|
|
|
|
|
|
default:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 23:34:36 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// SendFrom handles a sendfrom RPC request by creating 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. Upon success,
|
|
|
|
// the TxID for the created transaction is returned.
|
|
|
|
func SendFrom(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SendFromCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
// Check that signed integer parameters are positive.
|
|
|
|
if cmd.Amount < 0 {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParameter.Code,
|
|
|
|
Message: "amount must be positive",
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
if cmd.MinConf < 0 {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParameter.Code,
|
|
|
|
Message: "minconf must be positive",
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Check that the account specified in the request exists.
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account(cmd.FromAccount)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-10-29 07:19:40 +01:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
// Create map of address and amount pairs.
|
|
|
|
pairs := map[string]int64{
|
|
|
|
cmd.ToAddress: cmd.Amount,
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Create transaction, replying with an error if the creation
|
|
|
|
// was not successful.
|
2013-12-04 01:22:47 +01:00
|
|
|
createdTx, err := a.txToPairs(pairs, cmd.MinConf)
|
2013-11-12 18:01:32 +01:00
|
|
|
switch {
|
|
|
|
case err == ErrNonPositiveAmount:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParameter.Code,
|
|
|
|
Message: "amount must be positive",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
case err == wallet.ErrWalletLocked:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletUnlockNeeded
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
case err != nil:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Mark txid as having send history so handlers adding receive history
|
|
|
|
// wait until all send history has been written.
|
|
|
|
SendTxHistSyncChans.add <- createdTx.txid
|
|
|
|
|
2013-11-06 18:37:46 +01:00
|
|
|
// If a change address was added, mark wallet as dirty, sync to disk,
|
2013-12-17 19:18:09 +01:00
|
|
|
// and request updates for change address.
|
2014-01-06 18:24:29 +01:00
|
|
|
if createdTx.changeAddr != nil {
|
2013-11-15 17:44:24 +01:00
|
|
|
a.dirty = true
|
|
|
|
if err := a.writeDirtyToDisk(); err != nil {
|
2013-11-06 18:37:46 +01:00
|
|
|
log.Errorf("cannot write dirty wallet: %v", err)
|
|
|
|
}
|
2013-11-15 17:44:24 +01:00
|
|
|
a.ReqNewTxsForAddress(createdTx.changeAddr)
|
2013-11-06 18:37:46 +01:00
|
|
|
}
|
2013-10-28 19:13:20 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
hextx := hex.EncodeToString(createdTx.rawTx)
|
|
|
|
// NewSendRawTransactionCmd will never fail so don't check error.
|
|
|
|
sendtx, _ := btcjson.NewSendRawTransactionCmd(<-NewJSONID, hextx)
|
|
|
|
var txid string
|
|
|
|
request := NewRPCRequest(sendtx, txid)
|
|
|
|
response := <-CurrentRPCConn().SendRequest(request)
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
if response.Err != nil {
|
|
|
|
SendTxHistSyncChans.remove <- createdTx.txid
|
|
|
|
return nil, response.Err
|
2013-10-15 15:42:10 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
return handleSendRawTxReply(cmd, txid, a, createdTx)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// SendMany handles a sendmany RPC request by creating 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.
|
|
|
|
// Upon success, the TxID for the created transaction is returned.
|
|
|
|
func SendMany(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SendManyCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Check that minconf is positive.
|
|
|
|
if cmd.MinConf < 0 {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParameter.Code,
|
|
|
|
Message: "minconf must be positive",
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Check that the account specified in the request exists.
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account(cmd.FromAccount)
|
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Create transaction, replying with an error if the creation
|
|
|
|
// was not successful.
|
2013-12-04 01:22:47 +01:00
|
|
|
createdTx, err := a.txToPairs(cmd.Amounts, cmd.MinConf)
|
2013-11-12 18:01:32 +01:00
|
|
|
switch {
|
|
|
|
case err == ErrNonPositiveAmount:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParameter.Code,
|
|
|
|
Message: "amount must be positive",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
case err == wallet.ErrWalletLocked:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletUnlockNeeded
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
case err != nil: // any other non-nil error
|
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// Mark txid as having send history so handlers adding receive history
|
|
|
|
// wait until all send history has been written.
|
|
|
|
SendTxHistSyncChans.add <- createdTx.txid
|
|
|
|
|
2013-11-06 18:37:46 +01:00
|
|
|
// If a change address was added, mark wallet as dirty, sync to disk,
|
2013-11-12 18:01:32 +01:00
|
|
|
// and request updates for change address.
|
2014-01-06 18:24:29 +01:00
|
|
|
if createdTx.changeAddr != nil {
|
2013-11-15 17:44:24 +01:00
|
|
|
a.dirty = true
|
|
|
|
if err := a.writeDirtyToDisk(); err != nil {
|
2013-11-06 18:37:46 +01:00
|
|
|
log.Errorf("cannot write dirty wallet: %v", err)
|
|
|
|
}
|
2013-11-15 17:44:24 +01:00
|
|
|
a.ReqNewTxsForAddress(createdTx.changeAddr)
|
2013-11-06 18:37:46 +01:00
|
|
|
}
|
2013-10-28 19:13:20 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
hextx := hex.EncodeToString(createdTx.rawTx)
|
|
|
|
// NewSendRawTransactionCmd will never fail so don't check error.
|
|
|
|
sendtx, _ := btcjson.NewSendRawTransactionCmd(<-NewJSONID, hextx)
|
|
|
|
var txid string
|
|
|
|
request := NewRPCRequest(sendtx, txid)
|
|
|
|
response := <-CurrentRPCConn().SendRequest(request)
|
|
|
|
txid = response.Result.(string)
|
2013-12-17 19:18:09 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
if response.Err != nil {
|
|
|
|
SendTxHistSyncChans.remove <- createdTx.txid
|
|
|
|
return nil, response.Err
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
return handleSendRawTxReply(cmd, txid, a, createdTx)
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2013-10-15 16:27:54 +02:00
|
|
|
|
2013-12-17 19:18:09 +01:00
|
|
|
// Channels to manage SendBeforeReceiveHistorySync.
|
|
|
|
var SendTxHistSyncChans = struct {
|
|
|
|
add, done, remove chan btcwire.ShaHash
|
|
|
|
access chan SendTxHistSyncRequest
|
|
|
|
}{
|
|
|
|
add: make(chan btcwire.ShaHash),
|
|
|
|
remove: make(chan btcwire.ShaHash),
|
|
|
|
done: make(chan btcwire.ShaHash),
|
|
|
|
access: make(chan SendTxHistSyncRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendTxHistSyncRequest requests a SendTxHistSyncResponse from
|
|
|
|
// SendBeforeReceiveHistorySync.
|
|
|
|
type SendTxHistSyncRequest struct {
|
|
|
|
txid btcwire.ShaHash
|
|
|
|
response chan SendTxHistSyncResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendTxHistSyncResponse is the response
|
|
|
|
type SendTxHistSyncResponse struct {
|
|
|
|
c chan struct{}
|
|
|
|
ok bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendBeforeReceiveHistorySync manages a set of transaction hashes
|
|
|
|
// created by this wallet. For each newly added txid, a channel is
|
|
|
|
// created. Once the send history has been recorded, the txid should
|
|
|
|
// be messaged across done, causing the internal channel to be closed.
|
|
|
|
// Before receive history is recorded, access should be used to check
|
|
|
|
// if there are or were any goroutines writing send history, and if
|
|
|
|
// so, wait until the channel is closed after a done message.
|
|
|
|
func SendBeforeReceiveHistorySync(add, done, remove chan btcwire.ShaHash,
|
|
|
|
access chan SendTxHistSyncRequest) {
|
|
|
|
|
|
|
|
m := make(map[btcwire.ShaHash]chan struct{})
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case txid := <-add:
|
|
|
|
m[txid] = make(chan struct{})
|
|
|
|
|
|
|
|
case txid := <-remove:
|
|
|
|
delete(m, txid)
|
|
|
|
|
|
|
|
case txid := <-done:
|
|
|
|
if c, ok := m[txid]; ok {
|
|
|
|
close(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
case req := <-access:
|
|
|
|
c, ok := m[req.txid]
|
|
|
|
req.response <- SendTxHistSyncResponse{c: c, ok: ok}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
func handleSendRawTxReply(icmd btcjson.Cmd, txIDStr string, a *Account, txInfo *CreatedTx) (interface{}, *btcjson.Error) {
|
2013-11-22 19:42:25 +01:00
|
|
|
txID, err := btcwire.NewShaHashFromStr(txIDStr)
|
2013-11-12 18:01:32 +01:00
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-22 19:42:25 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: "Invalid hash string from btcd reply",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2013-10-15 16:27:54 +02:00
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// Add to transaction store.
|
|
|
|
sendtx := &tx.SendTx{
|
|
|
|
TxID: *txID,
|
|
|
|
Time: txInfo.time.Unix(),
|
|
|
|
BlockHeight: -1,
|
|
|
|
Fee: txInfo.fee,
|
|
|
|
Receivers: txInfo.outputs,
|
|
|
|
}
|
|
|
|
a.TxStore.Lock()
|
|
|
|
a.TxStore.s = append(a.TxStore.s, sendtx)
|
|
|
|
a.TxStore.dirty = true
|
|
|
|
a.TxStore.Unlock()
|
|
|
|
|
2013-12-02 23:34:36 +01:00
|
|
|
// Notify frontends of new SendTx.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err == nil {
|
|
|
|
for _, details := range sendtx.TxInfo(a.Name(), bs.Height, a.Net()) {
|
|
|
|
NotifyNewTxDetails(frontendNotificationMaster, a.Name(),
|
|
|
|
details)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 19:18:09 +01:00
|
|
|
// Signal that received notifiations are ok to add now.
|
|
|
|
SendTxHistSyncChans.done <- txInfo.txid
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Remove previous unspent outputs now spent by the tx.
|
2013-11-15 17:44:24 +01:00
|
|
|
a.UtxoStore.Lock()
|
|
|
|
modified := a.UtxoStore.s.Remove(txInfo.inputs)
|
2013-11-22 19:42:25 +01:00
|
|
|
a.UtxoStore.dirty = a.UtxoStore.dirty || modified
|
|
|
|
a.UtxoStore.Unlock()
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// Disk sync tx and utxo stores.
|
|
|
|
if err := a.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// Notify all frontends of account's new unconfirmed and
|
|
|
|
// confirmed balance.
|
|
|
|
confirmed := a.CalculateBalance(1)
|
|
|
|
unconfirmed := a.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(frontendNotificationMaster, a.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, a.name, unconfirmed)
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// 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 an appearence in a later block. btcd
|
|
|
|
// will make a best try effort, but ultimately it's btcwallet's
|
|
|
|
// responsibility.
|
|
|
|
//
|
|
|
|
// Add hex string of raw tx to sent tx pool. If btcd disconnects
|
|
|
|
// and is reconnected, these txs are resent.
|
|
|
|
UnminedTxs.Lock()
|
2013-11-22 19:42:25 +01:00
|
|
|
UnminedTxs.m[TXID(*txID)] = txInfo
|
2013-11-12 18:01:32 +01:00
|
|
|
UnminedTxs.Unlock()
|
|
|
|
|
|
|
|
// The comments to be saved differ based on the underlying type
|
|
|
|
// of the cmd, so switch on the type to check whether it is a
|
|
|
|
// SendFromCmd or SendManyCmd.
|
|
|
|
//
|
|
|
|
// TODO(jrick): If message succeeded in being sent, save the
|
|
|
|
// transaction details with comments.
|
|
|
|
switch cmd := icmd.(type) {
|
|
|
|
case *btcjson.SendFromCmd:
|
|
|
|
_ = cmd.Comment
|
|
|
|
_ = cmd.CommentTo
|
|
|
|
|
|
|
|
case *btcjson.SendManyCmd:
|
|
|
|
_ = cmd.Comment
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
log.Infof("Successfully sent transaction %v", txIDStr)
|
|
|
|
return txIDStr, nil
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
// SetTxFee sets the transaction fee per kilobyte added to transactions.
|
2014-01-03 19:34:37 +01:00
|
|
|
func SetTxFee(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SetTxFeeCmd)
|
2013-10-07 21:14:39 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-10-07 21:14:39 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
// Check that amount is not negative.
|
|
|
|
if cmd.Amount < 0 {
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInvalidParams.Code,
|
|
|
|
Message: "amount cannot be negative",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-10-07 21:14:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Set global tx fee.
|
2013-12-04 01:22:47 +01:00
|
|
|
TxFeeIncrement.Lock()
|
|
|
|
TxFeeIncrement.i = cmd.Amount
|
|
|
|
TxFeeIncrement.Unlock()
|
2013-10-07 21:14:39 +02:00
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// A boolean true result is returned upon success.
|
2014-01-03 19:34:37 +01:00
|
|
|
return true, nil
|
2013-10-07 21:14:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// CreateEncryptedWallet creates a new account with an encrypted
|
|
|
|
// wallet. If an account with the same name as the requested account
|
|
|
|
// name 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.
|
2014-01-03 19:34:37 +01:00
|
|
|
func CreateEncryptedWallet(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.CreateEncryptedWalletCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
err := accountstore.CreateEncryptedWallet(cmd.Account, cmd.Description,
|
|
|
|
[]byte(cmd.Passphrase))
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// A nil reply is sent upon successful wallet creation.
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, nil
|
2013-10-29 07:19:40 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-09-03 15:49:16 +02:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
case ErrBtcdDisconnected:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-11-12 18:01:32 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: "btcd disconnected",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
default: // all other non-nil errors
|
|
|
|
return nil, &btcjson.ErrInternal
|
2013-10-15 22:55:28 +02:00
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// WalletIsLocked handles the walletislocked extension request by
|
|
|
|
// returning the current lock state (false for unlocked, true for locked)
|
|
|
|
// of an account. An error is returned if the requested account does not
|
|
|
|
// exist.
|
|
|
|
func WalletIsLocked(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.WalletIsLockedCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
2013-10-29 07:19:40 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account(cmd.Account)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
a.mtx.RLock()
|
|
|
|
locked := a.Wallet.IsLocked()
|
|
|
|
a.mtx.RUnlock()
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Reply with true for a locked wallet, and false for unlocked.
|
2014-01-03 19:34:37 +01:00
|
|
|
return locked, nil
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// WalletLock handles a walletlock request by locking the wallet,
|
|
|
|
// returning an error if the wallet is already locked.
|
2013-08-21 16:37:30 +02:00
|
|
|
//
|
|
|
|
// 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?
|
2014-01-03 19:34:37 +01:00
|
|
|
func WalletLock(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account("")
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: "default account does not exist",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
2013-08-21 19:25:22 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
if err := a.Lock(); err != nil {
|
|
|
|
return nil, &btcjson.ErrWalletWrongEncState
|
2013-08-21 19:25:22 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, nil
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// WalletPassphrase responds to the walletpassphrase request by unlocking
|
|
|
|
// the wallet. The decryption key is saved in the wallet until timeout
|
|
|
|
// seconds expires, after which the wallet is locked.
|
2013-08-21 16:37:30 +02:00
|
|
|
//
|
2013-09-09 20:14:57 +02:00
|
|
|
// TODO(jrick): figure out how to do this for non-default accounts.
|
2014-01-03 19:34:37 +01:00
|
|
|
func WalletPassphrase(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.WalletPassphraseCmd)
|
2013-09-09 20:14:57 +02:00
|
|
|
if !ok {
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrInternal
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
a, err := accountstore.Account("")
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
break
|
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: "default account does not exist",
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
default: // all other non-nil errors
|
2014-01-03 19:34:37 +01:00
|
|
|
e := btcjson.Error{
|
2013-12-02 20:56:06 +01:00
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
2013-08-21 18:07:57 +02:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &e
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch err := a.Unlock([]byte(cmd.Passphrase), cmd.Timeout); err {
|
|
|
|
case nil:
|
|
|
|
go func(timeout int64) {
|
|
|
|
time.Sleep(time.Second * time.Duration(timeout))
|
|
|
|
_ = a.Lock()
|
|
|
|
}(cmd.Timeout)
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, nil
|
2013-12-02 20:56:06 +01:00
|
|
|
|
|
|
|
case ErrAcctNotExist:
|
2014-01-03 19:34:37 +01:00
|
|
|
return nil, &btcjson.ErrWalletInvalidAccountName
|
2013-12-02 20:56:06 +01:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
default: // all other non-nil errors
|
|
|
|
return nil, &btcjson.ErrWalletPassphraseIncorrect
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 18:00:37 +02:00
|
|
|
|
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-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewWalletLockStateNtfn(account, locked)
|
|
|
|
mntfn, _ := ntfn.MarshalJSON()
|
|
|
|
frontendNotificationMaster <- mntfn
|
2013-08-22 18:00:37 +02:00
|
|
|
}
|
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) {
|
2013-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewAccountBalanceNtfn(account, balance, true)
|
|
|
|
mntfn, _ := ntfn.MarshalJSON()
|
|
|
|
frontend <- mntfn
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 23:34:36 +01:00
|
|
|
// NotifyWalletBalanceUnconfirmed sends a confirmed account balance
|
2013-10-14 22:39:15 +02:00
|
|
|
// notification to a frontend.
|
2013-10-09 17:23:54 +02:00
|
|
|
func NotifyWalletBalanceUnconfirmed(frontend chan []byte, account string, balance float64) {
|
2013-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewAccountBalanceNtfn(account, balance, false)
|
|
|
|
mntfn, _ := ntfn.MarshalJSON()
|
|
|
|
frontend <- mntfn
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
2013-12-02 23:34:36 +01:00
|
|
|
|
|
|
|
// NotifyNewTxDetails sends details of a new transaction to a frontend.
|
|
|
|
func NotifyNewTxDetails(frontend chan []byte, account string,
|
|
|
|
details map[string]interface{}) {
|
|
|
|
|
|
|
|
ntfn := btcws.NewTxNtfn(account, details)
|
|
|
|
mntfn, _ := ntfn.MarshalJSON()
|
|
|
|
frontend <- mntfn
|
|
|
|
}
|
2013-12-17 19:18:09 +01:00
|
|
|
|
|
|
|
// NotifiedRecvTxRequest is used to check whether the outpoint of
|
|
|
|
// a received transaction has already been notified due to
|
|
|
|
// arriving first in the btcd mempool.
|
|
|
|
type NotifiedRecvTxRequest struct {
|
|
|
|
op btcwire.OutPoint
|
|
|
|
response chan NotifiedRecvTxResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifiedRecvTxResponse is the response of a NotifiedRecvTxRequest
|
|
|
|
// request.
|
|
|
|
type NotifiedRecvTxResponse bool
|
|
|
|
|
|
|
|
// NotifiedRecvTxChans holds the channels to manage
|
|
|
|
// StoreNotifiedMempoolTxs.
|
|
|
|
var NotifiedRecvTxChans = struct {
|
|
|
|
add, remove chan btcwire.OutPoint
|
|
|
|
access chan NotifiedRecvTxRequest
|
|
|
|
}{
|
|
|
|
add: make(chan btcwire.OutPoint),
|
|
|
|
remove: make(chan btcwire.OutPoint),
|
|
|
|
access: make(chan NotifiedRecvTxRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreNotifiedMempoolRecvTxs maintains a set of previously-sent
|
|
|
|
// received transaction notifications originating from the btcd
|
|
|
|
// mempool. This is used to prevent duplicate frontend transaction
|
|
|
|
// notifications once a mempool tx is mined into a block.
|
|
|
|
func StoreNotifiedMempoolRecvTxs(add, remove chan btcwire.OutPoint,
|
|
|
|
access chan NotifiedRecvTxRequest) {
|
|
|
|
|
|
|
|
m := make(map[btcwire.OutPoint]struct{})
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case op := <-add:
|
|
|
|
m[op] = struct{}{}
|
|
|
|
|
|
|
|
case op := <-remove:
|
|
|
|
if _, ok := m[op]; ok {
|
|
|
|
delete(m, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
case req := <-access:
|
|
|
|
_, ok := m[req.op]
|
|
|
|
req.response <- NotifiedRecvTxResponse(ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channel to send received transactions that were previously
|
|
|
|
// notified to frontends by the mempool. A TxMined notification
|
|
|
|
// is sent to all connected frontends detailing the block information
|
|
|
|
// about the now confirmed transaction.
|
|
|
|
var NotifyMinedTx = make(chan *tx.RecvTx)
|
|
|
|
|
|
|
|
// NotifyMinedTxSender reads received transactions from in, notifying
|
|
|
|
// frontends that the tx has now been confirmed in a block. Duplicates
|
|
|
|
// are filtered out.
|
|
|
|
func NotifyMinedTxSender(in chan *tx.RecvTx) {
|
|
|
|
// Create a map to hold a set of already notified
|
|
|
|
// txids. Do not send duplicates.
|
|
|
|
m := make(map[btcwire.ShaHash]struct{})
|
|
|
|
|
|
|
|
for recv := range in {
|
|
|
|
if _, ok := m[recv.TxID]; !ok {
|
|
|
|
ntfn := btcws.NewTxMinedNtfn(recv.TxID.String(),
|
|
|
|
recv.BlockHash.String(), recv.BlockHeight,
|
|
|
|
recv.BlockTime, int(recv.BlockIndex))
|
|
|
|
mntfn, _ := ntfn.MarshalJSON()
|
|
|
|
frontendNotificationMaster <- mntfn
|
|
|
|
|
|
|
|
// Mark as sent.
|
|
|
|
m[recv.TxID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBalanceSyncerChans holds channels for accessing
|
|
|
|
// the NotifyBalanceSyncer goroutine.
|
|
|
|
var NotifyBalanceSyncerChans = struct {
|
|
|
|
add chan NotifyBalanceWorker
|
|
|
|
remove chan btcwire.ShaHash
|
|
|
|
access chan NotifyBalanceRequest
|
|
|
|
}{
|
|
|
|
add: make(chan NotifyBalanceWorker),
|
|
|
|
remove: make(chan btcwire.ShaHash),
|
|
|
|
access: make(chan NotifyBalanceRequest),
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBalanceWorker holds a block hash to add a worker to
|
|
|
|
// NotifyBalanceSyncer and uses a chan to returns the WaitGroup
|
|
|
|
// which should be decremented with Done after the worker is finished.
|
|
|
|
type NotifyBalanceWorker struct {
|
|
|
|
block btcwire.ShaHash
|
|
|
|
wg chan *sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBalanceRequest is used by the blockconnected notification handler
|
|
|
|
// to access and wait on the the WaitGroup for workers currently processing
|
|
|
|
// transactions for a block. If no handlers have been added, a nil
|
|
|
|
// WaitGroup is returned.
|
|
|
|
type NotifyBalanceRequest struct {
|
|
|
|
block btcwire.ShaHash
|
|
|
|
wg chan *sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBalanceSyncer maintains a map of block hashes to WaitGroups
|
|
|
|
// for worker goroutines that must finish before it is safe to notify
|
|
|
|
// frontends of a new balance in the blockconnected notification handler.
|
|
|
|
func NotifyBalanceSyncer(add chan NotifyBalanceWorker,
|
|
|
|
remove chan btcwire.ShaHash,
|
|
|
|
access chan NotifyBalanceRequest) {
|
|
|
|
|
|
|
|
m := make(map[btcwire.ShaHash]*sync.WaitGroup)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case worker := <-add:
|
|
|
|
wg, ok := m[worker.block]
|
|
|
|
if !ok {
|
|
|
|
wg = &sync.WaitGroup{}
|
|
|
|
m[worker.block] = wg
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
m[worker.block] = wg
|
|
|
|
worker.wg <- wg
|
|
|
|
|
|
|
|
case block := <-remove:
|
|
|
|
if _, ok := m[block]; ok {
|
|
|
|
delete(m, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
case req := <-access:
|
|
|
|
req.wg <- m[req.block]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|