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 (
|
2014-02-24 20:35:30 +01:00
|
|
|
"bytes"
|
2014-04-18 00:13:53 +02:00
|
|
|
"crypto/ecdsa"
|
2014-06-12 18:39:26 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/subtle"
|
|
|
|
"crypto/tls"
|
2014-02-12 03:14:07 +01:00
|
|
|
"encoding/base64"
|
2013-10-04 21:02:17 +02:00
|
|
|
"encoding/hex"
|
2014-04-09 18:07:09 +02:00
|
|
|
"encoding/json"
|
2014-04-18 00:13:53 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-06-12 18:39:26 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-02-12 03:14:07 +01:00
|
|
|
"github.com/conformal/btcec"
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/conformal/btcjson"
|
2014-06-12 18:39:26 +02:00
|
|
|
"github.com/conformal/btcrpcclient"
|
2014-02-24 20:35:30 +01:00
|
|
|
"github.com/conformal/btcscript"
|
2013-12-10 22:15:25 +01:00
|
|
|
"github.com/conformal/btcutil"
|
2014-05-08 21:48:42 +02:00
|
|
|
"github.com/conformal/btcwallet/txstore"
|
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"
|
2014-06-12 18:39:26 +02:00
|
|
|
"github.com/conformal/websocket"
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
// Error types to simplify the reporting of specific categories of
|
|
|
|
// errors, and their btcjson.Error creation.
|
|
|
|
type (
|
|
|
|
// DeserializationError describes a failed deserializaion due to bad
|
|
|
|
// user input. It cooresponds to btcjson.ErrDeserialization.
|
|
|
|
DeserializationError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidParameterError describes an invalid parameter passed by
|
|
|
|
// the user. It cooresponds to btcjson.ErrInvalidParameter.
|
|
|
|
InvalidParameterError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseError describes a failed parse due to bad user input. It
|
|
|
|
// cooresponds to btcjson.ErrParse.
|
|
|
|
ParseError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidAddressOrKeyError describes a parse, network mismatch, or
|
|
|
|
// missing address error when decoding or validating an address or
|
|
|
|
// key. It cooresponds to btcjson.ErrInvalidAddressOrKey.
|
|
|
|
InvalidAddressOrKeyError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Errors variables that are defined once here to avoid duplication below.
|
|
|
|
var (
|
|
|
|
ErrNeedPositiveAmount = InvalidParameterError{
|
|
|
|
errors.New("amount must be positive"),
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrNeedPositiveMinconf = InvalidParameterError{
|
|
|
|
errors.New("minconf must be positive"),
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrAddressNotInWallet = InvalidAddressOrKeyError{
|
|
|
|
errors.New("address not found in wallet"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
type websocketClient struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
authenticated bool
|
|
|
|
remoteAddr string
|
|
|
|
allRequests chan []byte
|
|
|
|
unauthedRequests chan unauthedRequest
|
|
|
|
responses chan []byte
|
|
|
|
quit chan struct{} // closed on disconnect
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWebsocketClient(c *websocket.Conn, authenticated bool, remoteAddr string) *websocketClient {
|
|
|
|
return &websocketClient{
|
|
|
|
conn: c,
|
|
|
|
authenticated: authenticated,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
allRequests: make(chan []byte),
|
|
|
|
unauthedRequests: make(chan unauthedRequest, maxConcurrentClientRequests),
|
|
|
|
responses: make(chan []byte),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errDisconnected = errors.New("websocket client disconnected")
|
|
|
|
|
|
|
|
func (c *websocketClient) send(b []byte) error {
|
|
|
|
select {
|
|
|
|
case c.responses <- b:
|
|
|
|
return nil
|
|
|
|
case <-c.quit:
|
|
|
|
return errDisconnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseListeners splits the list of listen addresses passed in addrs into
|
|
|
|
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
|
|
|
|
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
|
|
|
// detects addresses which apply to "all interfaces" and adds the address to
|
|
|
|
// both slices.
|
|
|
|
func parseListeners(addrs []string) ([]string, []string, error) {
|
|
|
|
ipv4ListenAddrs := make([]string, 0, len(addrs)*2)
|
|
|
|
ipv6ListenAddrs := make([]string, 0, len(addrs)*2)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen due to already being normalized.
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty host or host of * on plan9 is both IPv4 and IPv6.
|
|
|
|
if host == "" || (host == "*" && runtime.GOOS == "plan9") {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the IP.
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, nil, fmt.Errorf("'%s' is not a valid IP "+
|
|
|
|
"address", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// To4 returns nil when the IP is not an IPv4 address, so use
|
|
|
|
// this determine the address type.
|
|
|
|
if ip.To4() == nil {
|
|
|
|
ipv6ListenAddrs = append(ipv6ListenAddrs, addr)
|
|
|
|
} else {
|
|
|
|
ipv4ListenAddrs = append(ipv4ListenAddrs, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipv4ListenAddrs, ipv6ListenAddrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genCertPair generates a key/cert pair to the paths provided.
|
|
|
|
func genCertPair(certFile, keyFile string) error {
|
|
|
|
log.Infof("Generating TLS certificates...")
|
|
|
|
|
|
|
|
// Create directories for cert and key files if they do not yet exist.
|
|
|
|
certDir, _ := filepath.Split(certFile)
|
|
|
|
keyDir, _ := filepath.Split(keyFile)
|
|
|
|
if err := os.MkdirAll(certDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(keyDir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate cert pair.
|
|
|
|
org := "btcwallet autogenerated cert"
|
|
|
|
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
|
|
|
|
cert, key, err := btcutil.NewTLSCertPair(org, validUntil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write cert and key files.
|
|
|
|
if err = ioutil.WriteFile(certFile, cert, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(keyFile, key, 0600); err != nil {
|
|
|
|
if rmErr := os.Remove(certFile); rmErr != nil {
|
|
|
|
log.Warnf("Cannot remove written certificates: %v", rmErr)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Done generating TLS certificates")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// rpcServer holds the items the RPC server may need to access (auth,
|
|
|
|
// config, shutdown, etc.)
|
|
|
|
type rpcServer struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
listeners []net.Listener
|
|
|
|
authsha [sha256.Size]byte
|
|
|
|
wsClients map[*websocketClient]struct{}
|
|
|
|
|
|
|
|
upgrader websocket.Upgrader
|
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
requests chan handlerJob
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
addWSClient chan *websocketClient
|
|
|
|
removeWSClient chan *websocketClient
|
|
|
|
broadcasts chan []byte
|
2014-06-24 23:00:27 +02:00
|
|
|
|
|
|
|
quit chan struct{}
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRPCServer creates a new server for serving RPC client connections, both
|
|
|
|
// HTTP POST and websocket.
|
|
|
|
func newRPCServer(listenAddrs []string) (*rpcServer, error) {
|
|
|
|
login := cfg.Username + ":" + cfg.Password
|
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
|
|
|
|
s := rpcServer{
|
|
|
|
authsha: sha256.Sum256([]byte(auth)),
|
|
|
|
wsClients: map[*websocketClient]struct{}{},
|
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
// Allow all origins.
|
|
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
|
|
|
},
|
2014-06-24 23:00:27 +02:00
|
|
|
requests: make(chan handlerJob),
|
2014-06-12 18:39:26 +02:00
|
|
|
addWSClient: make(chan *websocketClient),
|
|
|
|
removeWSClient: make(chan *websocketClient),
|
|
|
|
broadcasts: make(chan []byte),
|
2014-06-24 23:00:27 +02:00
|
|
|
quit: make(chan struct{}),
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for existence of cert file and key file
|
|
|
|
if !fileExists(cfg.RPCKey) && !fileExists(cfg.RPCCert) {
|
|
|
|
// if both files do not exist, we generate them.
|
|
|
|
err := genCertPair(cfg.RPCCert, cfg.RPCKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keypair, err := tls.LoadX509KeyPair(cfg.RPCCert, cfg.RPCKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := tls.Config{
|
|
|
|
Certificates: []tls.Certificate{keypair},
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4ListenAddrs, ipv6ListenAddrs, err := parseListeners(listenAddrs)
|
|
|
|
listeners := make([]net.Listener, 0,
|
|
|
|
len(ipv6ListenAddrs)+len(ipv4ListenAddrs))
|
|
|
|
for _, addr := range ipv4ListenAddrs {
|
|
|
|
listener, err := tls.Listen("tcp4", addr, &tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("RPCS: Can't listen on %s: %v", addr,
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range ipv6ListenAddrs {
|
|
|
|
listener, err := tls.Listen("tcp6", addr, &tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("RPCS: Can't listen on %s: %v", addr,
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
}
|
|
|
|
if len(listeners) == 0 {
|
|
|
|
return nil, errors.New("no valid listen address")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.listeners = listeners
|
|
|
|
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts a HTTP server to provide standard RPC and extension
|
|
|
|
// websocket connections for any number of btcwallet clients.
|
|
|
|
func (s *rpcServer) Start() {
|
|
|
|
// A duplicator for notifications intended for all clients runs
|
|
|
|
// in another goroutines. Any such notifications are sent to
|
|
|
|
// the allClients channel and then sent to each connected client.
|
2014-06-24 23:00:27 +02:00
|
|
|
s.wg.Add(2)
|
2014-06-12 18:39:26 +02:00
|
|
|
go s.NotificationHandler()
|
2014-06-24 23:00:27 +02:00
|
|
|
go s.RequestHandler()
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
log.Trace("Starting RPC server")
|
|
|
|
|
|
|
|
serveMux := http.NewServeMux()
|
|
|
|
const rpcAuthTimeoutSeconds = 10
|
|
|
|
httpServer := &http.Server{
|
|
|
|
Handler: serveMux,
|
|
|
|
|
|
|
|
// Timeout connections which don't complete the initial
|
|
|
|
// handshake within the allowed timeframe.
|
|
|
|
ReadTimeout: time.Second * rpcAuthTimeoutSeconds,
|
|
|
|
}
|
|
|
|
serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
r.Close = true
|
|
|
|
|
|
|
|
// TODO: Limit number of active connections.
|
|
|
|
|
|
|
|
if err := s.checkAuthHeader(r); err != nil {
|
|
|
|
log.Warnf("Unauthorized client connection attempt")
|
|
|
|
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.PostClientRPC(w, r)
|
|
|
|
})
|
2014-06-12 19:54:58 +02:00
|
|
|
serveMux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
2014-06-12 18:39:26 +02:00
|
|
|
authenticated := false
|
|
|
|
switch s.checkAuthHeader(r) {
|
|
|
|
case nil:
|
|
|
|
authenticated = true
|
|
|
|
case ErrNoAuth:
|
|
|
|
// nothing
|
|
|
|
default:
|
|
|
|
// If auth was supplied but incorrect, rather than simply
|
|
|
|
// being missing, immediately terminate the connection.
|
|
|
|
log.Warnf("Disconnecting improperly authorized " +
|
|
|
|
"websocket client")
|
|
|
|
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := s.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot websocket upgrade client %s: %v",
|
|
|
|
r.RemoteAddr, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wsc := newWebsocketClient(conn, authenticated, r.RemoteAddr)
|
|
|
|
s.WebsocketClientRPC(wsc)
|
|
|
|
})
|
|
|
|
for _, listener := range s.listeners {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go func(listener net.Listener) {
|
|
|
|
log.Infof("RPCS: RPC server listening on %s", listener.Addr())
|
2014-06-24 23:00:27 +02:00
|
|
|
_ = httpServer.Serve(listener)
|
2014-06-12 18:39:26 +02:00
|
|
|
log.Tracef("RPCS: RPC listener done for %s", listener.Addr())
|
|
|
|
s.wg.Done()
|
|
|
|
}(listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
// Stop gracefully shuts down the rpc server by stopping and disconnecting all
|
|
|
|
// clients, disconnecting the chain server connection, and closing the wallet's
|
|
|
|
// account files.
|
|
|
|
func (s *rpcServer) Stop() {
|
|
|
|
// If the server is changed to run more than one rpc handler at a time,
|
|
|
|
// to prevent a double channel close, this should be replaced with an
|
|
|
|
// atomic test-and-set.
|
|
|
|
select {
|
|
|
|
case <-s.quit:
|
|
|
|
log.Warnf("Server already shutting down")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("Server shutting down")
|
|
|
|
|
|
|
|
// Stop all the listeners. There will not be any listeners if
|
|
|
|
// listening is disabled.
|
|
|
|
for _, listener := range s.listeners {
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot close listener %s: %v",
|
|
|
|
listener.Addr(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the connected chain server, if any.
|
|
|
|
client, err := accessClient()
|
|
|
|
if err == nil {
|
|
|
|
client.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the account manager and finish all pending account file writes.
|
|
|
|
AcctMgr.Stop()
|
|
|
|
|
|
|
|
// Signal the remaining goroutines to stop.
|
|
|
|
close(s.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// ErrNoAuth represents an error where authentication could not succeed
|
|
|
|
// due to a missing Authorization HTTP header.
|
|
|
|
var ErrNoAuth = errors.New("no auth")
|
|
|
|
|
|
|
|
// checkAuthHeader checks the HTTP Basic authentication supplied by a client
|
|
|
|
// in the HTTP request r. It errors with ErrNoAuth if the request does not
|
|
|
|
// contain the Authorization header, or another non-nil error if the
|
|
|
|
// authentication was provided but incorrect.
|
|
|
|
//
|
|
|
|
// This check is time-constant.
|
|
|
|
func (s *rpcServer) checkAuthHeader(r *http.Request) error {
|
|
|
|
authhdr := r.Header["Authorization"]
|
|
|
|
if len(authhdr) == 0 {
|
|
|
|
return ErrNoAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
authsha := sha256.Sum256([]byte(authhdr[0]))
|
|
|
|
cmp := subtle.ConstantTimeCompare(authsha[:], s.authsha[:])
|
|
|
|
if cmp != 1 {
|
|
|
|
return errors.New("bad auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) WebsocketClientRead(wsc *websocketClient) {
|
|
|
|
for {
|
|
|
|
_, request, err := wsc.conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF && err != io.ErrUnexpectedEOF {
|
|
|
|
log.Warnf("Websocket receive failed from client %s: %v",
|
|
|
|
wsc.remoteAddr, err)
|
|
|
|
}
|
|
|
|
close(wsc.allRequests)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
wsc.allRequests <- request
|
|
|
|
}
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawRequest struct {
|
|
|
|
// "jsonrpc" value isn't checked so we exclude it.
|
|
|
|
ID interface{} `json:"id"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []json.RawMessage `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// idPointer returns a pointer to the passed ID, or nil if the interface is nil.
|
|
|
|
// Interface pointers are usually a red flag of doing something incorrectly,
|
|
|
|
// but this is only implemented here to work around an oddity with btcjson,
|
|
|
|
// which uses empty interface pointers for request and response IDs.
|
|
|
|
func idPointer(id interface{}) (p *interface{}) {
|
|
|
|
if id != nil {
|
|
|
|
p = &id
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalError(id *interface{}) []byte {
|
|
|
|
response := btcjson.Reply{
|
|
|
|
Id: id,
|
|
|
|
Error: &btcjson.ErrInvalidRequest,
|
|
|
|
}
|
|
|
|
mresponse, err := json.Marshal(response)
|
|
|
|
// We expect the marshal to succeed. If it doesn't, it indicates some
|
|
|
|
// non-marshalable type in the response.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return mresponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// websocketPassthrough pass a websocket client's raw request to the connected
|
|
|
|
// chain server.
|
|
|
|
func (s *rpcServer) websocketPassthrough(wsc *websocketClient, request rawRequest) {
|
|
|
|
resp := passthrough(request)
|
|
|
|
_ = wsc.send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// postPassthrough pass a websocket client's raw request to the connected
|
|
|
|
// chain server.
|
|
|
|
func (s *rpcServer) postPassthrough(w http.ResponseWriter, request rawRequest) {
|
|
|
|
resp := passthrough(request)
|
|
|
|
if _, err := w.Write(resp); err != nil {
|
|
|
|
log.Warnf("Unable to respond to client with passthrough "+
|
|
|
|
"response: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// passthrough is a helper function for websocketPassthrough and postPassthrough
|
|
|
|
// to request and receive the chain server's marshaled response to an
|
|
|
|
// unhandled-by-wallet request. The marshaled response includes the original
|
|
|
|
// request's ID.
|
|
|
|
func passthrough(request rawRequest) []byte {
|
|
|
|
var res json.RawMessage
|
|
|
|
client, err := accessClient()
|
|
|
|
if err == nil {
|
|
|
|
res, err = client.RawRequest(request.Method, request.Params)
|
|
|
|
}
|
|
|
|
var jsonErr *btcjson.Error
|
|
|
|
if err != nil {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case *btcjson.Error:
|
|
|
|
jsonErr = e
|
|
|
|
case btcjson.Error:
|
|
|
|
jsonErr = &e
|
|
|
|
default:
|
|
|
|
jsonErr = &btcjson.Error{
|
|
|
|
Code: btcjson.ErrWallet.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The raw result will only marshal correctly if called with the
|
|
|
|
// MarshalJSON method, and that method requires a pointer receiver.
|
|
|
|
var pres *json.RawMessage
|
|
|
|
if res != nil {
|
|
|
|
pres = &res
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := btcjson.Reply{
|
|
|
|
Id: idPointer(request.ID),
|
|
|
|
Result: pres,
|
|
|
|
Error: jsonErr,
|
|
|
|
}
|
|
|
|
mresp, err := json.Marshal(resp)
|
|
|
|
// The chain server response was successfully unmarshaled or we created
|
|
|
|
// our own error, so a marshal can never error.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return mresp
|
|
|
|
}
|
|
|
|
|
|
|
|
type unauthedRequest struct {
|
|
|
|
marshaledRequest []byte
|
|
|
|
handler requestHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) WebsocketClientGateway(wsc *websocketClient) {
|
|
|
|
out:
|
|
|
|
for request := range wsc.allRequests {
|
|
|
|
// Get the method of the request and check whether it should be
|
|
|
|
// handled by wallet or passed down to btcd. If the latter,
|
|
|
|
// handle in a new goroutine (to not block or be blocked by
|
|
|
|
// the handling of actual wallet requests).
|
|
|
|
//
|
|
|
|
// This is done by unmarshaling the JSON bytes into a rawRequest
|
|
|
|
// to avoid the mangling of unmarshaling and re-marshaling of
|
|
|
|
// large JSON numbers, as well as the overhead of unneeded
|
|
|
|
// unmarshals and marshals.
|
|
|
|
var raw rawRequest
|
|
|
|
if err := json.Unmarshal(request, &raw); err != nil {
|
|
|
|
if !wsc.authenticated {
|
|
|
|
// Disconnect immediately.
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
err = wsc.send(marshalError(idPointer(raw.ID)))
|
|
|
|
if err != nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f, ok := handlerFunc(raw.Method, true)
|
|
|
|
if ok || raw.Method == "authenticate" {
|
|
|
|
// unauthedRequests is buffered to the max number of
|
|
|
|
// concurrent websocket client requests so as to not
|
|
|
|
// block the passthrough of later btcd requests.
|
|
|
|
wsc.unauthedRequests <- unauthedRequest{request, f}
|
|
|
|
} else {
|
|
|
|
// websocketPassthrough is run as a goroutine to
|
|
|
|
// send an unhandled request to the chain server without
|
|
|
|
// blocking the handling of later wallet requests.
|
|
|
|
go s.websocketPassthrough(wsc, raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(wsc.unauthedRequests)
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalidAuth checks whether a websocket request is allowed for the current
|
|
|
|
// authentication state. If an unauthenticated client submitted an
|
|
|
|
// authenticate request, the authentication is verified and the client's
|
|
|
|
// authentication state is modified.
|
|
|
|
func (s *rpcServer) invalidAuth(wsc *websocketClient, request btcjson.Cmd) (invalid, checked bool) {
|
|
|
|
if authCmd, ok := request.(*btcws.AuthenticateCmd); ok {
|
|
|
|
// Duplication authentication is not allowed.
|
|
|
|
if wsc.authenticated {
|
|
|
|
return true, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check credentials.
|
|
|
|
login := authCmd.Username + ":" + authCmd.Passphrase
|
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
|
|
|
|
authSha := sha256.Sum256([]byte(auth))
|
|
|
|
cmp := subtle.ConstantTimeCompare(authSha[:], s.authsha[:])
|
|
|
|
wsc.authenticated = cmp == 1
|
|
|
|
return cmp != 1, true
|
|
|
|
}
|
|
|
|
// Unauthorized clients must first issue an authenticate request. If
|
|
|
|
// not already authenticated, the auth is invalid.
|
|
|
|
return !wsc.authenticated, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) WebsocketClientRespond(wsc *websocketClient) {
|
|
|
|
out:
|
|
|
|
for r := range wsc.unauthedRequests {
|
|
|
|
cmd, parseErr := btcjson.ParseMarshaledCmd(r.marshaledRequest)
|
|
|
|
var id interface{}
|
|
|
|
if cmd != nil {
|
|
|
|
id = cmd.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the websocket is authenticated and not send an
|
|
|
|
// unnecessary authentication request, or perform the check
|
|
|
|
// if unauthenticated and this is an authentication request.
|
|
|
|
// Disconnect the client immediately if the authentication is
|
|
|
|
// invalid or disallowed.
|
|
|
|
switch invalid, checked := s.invalidAuth(wsc, cmd); {
|
|
|
|
case invalid:
|
|
|
|
log.Warnf("Disconnecting improperly authenticated "+
|
|
|
|
"websocket client %s", wsc.remoteAddr)
|
|
|
|
break out
|
|
|
|
case checked:
|
|
|
|
// Marshal and send a successful auth response. The
|
|
|
|
// marshal is expected to never fail.
|
|
|
|
response := btcjson.Reply{Id: idPointer(id)}
|
|
|
|
mresponse, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := wsc.send(mresponse); err != nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parse error is checked after the authentication check
|
|
|
|
// so we don't respond back for invalid requests sent by
|
|
|
|
// unauthenticated clients.
|
|
|
|
if parseErr != nil {
|
|
|
|
if wsc.send(marshalError(idPointer(id))) != nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send request and the handler func (already looked up) to the
|
|
|
|
// server's global request handler. This serializes the
|
|
|
|
// execution of all handlers from all connections (both
|
|
|
|
// websocket and HTTP POST), and runs the handler with exclusive
|
|
|
|
// access of the account manager.
|
|
|
|
responseChan := make(chan handlerResponse)
|
|
|
|
s.requests <- handlerJob{
|
|
|
|
request: cmd,
|
|
|
|
handler: r.handler,
|
|
|
|
response: responseChan,
|
|
|
|
}
|
|
|
|
response := <-responseChan
|
|
|
|
resp := btcjson.Reply{
|
|
|
|
Id: idPointer(id),
|
|
|
|
Result: response.result,
|
|
|
|
Error: response.jsonErr,
|
|
|
|
}
|
|
|
|
mresp, err := json.Marshal(resp)
|
|
|
|
// All responses originating from us must be marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Send marshaled response to client.
|
|
|
|
if err := wsc.send(mresp); err != nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(wsc.responses)
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) WebsocketClientSend(wsc *websocketClient) {
|
|
|
|
const deadline time.Duration = 2 * time.Second
|
|
|
|
for response := range wsc.responses {
|
|
|
|
err := wsc.conn.SetWriteDeadline(time.Now().Add(deadline))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot set write deadline on client %s: %v",
|
|
|
|
wsc.remoteAddr, err)
|
|
|
|
}
|
|
|
|
err = wsc.conn.WriteMessage(websocket.TextMessage, response)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed websocket send to client %s: %v",
|
|
|
|
wsc.remoteAddr, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(wsc.quit)
|
|
|
|
log.Infof("Disconnected websocket client %s", wsc.remoteAddr)
|
|
|
|
s.removeWSClient <- wsc
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebsocketClientRPC starts the goroutines to serve JSON-RPC requests and
|
|
|
|
// notifications over a websocket connection for a single client.
|
|
|
|
func (s *rpcServer) WebsocketClientRPC(wsc *websocketClient) {
|
|
|
|
log.Infof("New websocket client %s", wsc.remoteAddr)
|
|
|
|
|
|
|
|
// Clear the read deadline set before the websocket hijacked
|
|
|
|
// the connection.
|
|
|
|
if err := wsc.conn.SetReadDeadline(time.Time{}); err != nil {
|
|
|
|
log.Warnf("Cannot remove read deadline: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add client context so notifications duplicated to each
|
|
|
|
// client are received by this client.
|
|
|
|
s.addWSClient <- wsc
|
|
|
|
|
|
|
|
s.wg.Add(4)
|
|
|
|
go s.WebsocketClientRead(wsc)
|
|
|
|
go s.WebsocketClientGateway(wsc)
|
|
|
|
go s.WebsocketClientRespond(wsc)
|
|
|
|
go s.WebsocketClientSend(wsc)
|
|
|
|
|
|
|
|
// Send initial unsolicited notifications.
|
|
|
|
// TODO: these should be requested by the client first.
|
|
|
|
s.NotifyConnectionStatus(wsc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxRequestSize specifies the maximum number of bytes in the request body
|
|
|
|
// that may be read from a client. This is currently limited to 4MB.
|
|
|
|
const maxRequestSize = 1024 * 1024 * 4
|
|
|
|
|
|
|
|
// PostClientRPC processes and replies to a JSON-RPC client request.
|
|
|
|
func (s *rpcServer) PostClientRPC(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body := http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
rpcRequest, err := ioutil.ReadAll(body)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: what if the underlying reader errored?
|
|
|
|
http.Error(w, "413 Request Too Large.",
|
|
|
|
http.StatusRequestEntityTooLarge)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check whether wallet has a handler for this request's method.
|
|
|
|
// If unfound, the request is sent to the chain server for further
|
|
|
|
// processing. While checking the methods, disallow authenticate
|
|
|
|
// requests, as they are invalid for HTTP POST clients.
|
|
|
|
var raw rawRequest
|
|
|
|
err = json.Unmarshal(rpcRequest, &raw)
|
|
|
|
if err != nil || raw.Method == "authenticate" {
|
|
|
|
_, err := w.Write(marshalError(idPointer(raw.ID)))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot write invalid request request to "+
|
|
|
|
"client: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, ok := handlerFunc(raw.Method, false)
|
|
|
|
if !ok {
|
|
|
|
s.postPassthrough(w, raw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the full request since it must be handled by wallet.
|
|
|
|
cmd, err := btcjson.ParseMarshaledCmd(rpcRequest)
|
|
|
|
var id interface{}
|
|
|
|
if cmd != nil {
|
|
|
|
id = cmd.Id()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-06-23 23:59:57 +02:00
|
|
|
fmt.Printf("%s\n", rpcRequest)
|
2014-06-12 18:39:26 +02:00
|
|
|
_, err := w.Write(marshalError(idPointer(cmd.Id())))
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Client sent invalid request but unable "+
|
|
|
|
"to respond with error: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send request and the handler func (already looked up) to the
|
|
|
|
// server's global request handler. This serializes the
|
|
|
|
// execution of all handlers from all connections (both
|
|
|
|
// websocket and HTTP POST), and runs the handler with exclusive
|
|
|
|
// access of the account manager.
|
|
|
|
responseChan := make(chan handlerResponse)
|
|
|
|
s.requests <- handlerJob{
|
|
|
|
request: cmd,
|
|
|
|
handler: f,
|
|
|
|
response: responseChan,
|
|
|
|
}
|
|
|
|
response := <-responseChan
|
|
|
|
resp := btcjson.Reply{
|
|
|
|
Id: idPointer(id),
|
|
|
|
Result: response.result,
|
|
|
|
Error: response.jsonErr,
|
|
|
|
}
|
|
|
|
mresp, err := json.Marshal(resp)
|
|
|
|
// All responses originating from us must be marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Send marshaled response to client.
|
|
|
|
if _, err := w.Write(mresp); err != nil {
|
|
|
|
log.Warnf("Unable to respond to client: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyConnectionStatus notifies all connected websocket clients of the
|
|
|
|
// current connection status of btcwallet to btcd.
|
|
|
|
func (s *rpcServer) NotifyConnectionStatus(wsc *websocketClient) {
|
|
|
|
connected := false
|
|
|
|
client, err := accessClient()
|
|
|
|
if err == nil {
|
|
|
|
connected = !client.Disconnected()
|
|
|
|
}
|
|
|
|
ntfn := btcws.NewBtcdConnectedNtfn(connected)
|
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// btcws notifications must always marshal without error.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if wsc == nil {
|
|
|
|
s.broadcasts <- mntfn
|
|
|
|
} else {
|
|
|
|
// Don't care whether the client disconnected at this
|
|
|
|
// point, so discard error.
|
|
|
|
_ = wsc.send(mntfn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) NotificationHandler() {
|
2014-06-24 23:00:27 +02:00
|
|
|
out:
|
2014-06-12 18:39:26 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-s.addWSClient:
|
|
|
|
s.wsClients[c] = struct{}{}
|
|
|
|
case c := <-s.removeWSClient:
|
|
|
|
delete(s.wsClients, c)
|
|
|
|
case b := <-s.broadcasts:
|
|
|
|
for wsc := range s.wsClients {
|
|
|
|
if err := wsc.send(b); err != nil {
|
|
|
|
delete(s.wsClients, wsc)
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 23:00:27 +02:00
|
|
|
case <-s.quit:
|
|
|
|
break out
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-24 23:00:27 +02:00
|
|
|
s.wg.Done()
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// requestHandler is a handler function to handle an unmarshaled and parsed
|
2014-06-04 02:52:04 +02:00
|
|
|
// request into a marshalable response. If the error is a btcjson.Error
|
|
|
|
// or any of the above special error classes, the server will respond with
|
|
|
|
// the JSON-RPC appropiate error code. All other errors use the wallet
|
|
|
|
// catch-all error code, btcjson.ErrWallet.Code.
|
2014-06-12 18:39:26 +02:00
|
|
|
type requestHandler func(btcjson.Cmd) (interface{}, error)
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
var rpcHandlers = map[string]requestHandler{
|
2013-12-30 18:44:51 +01:00
|
|
|
// Standard bitcoind methods (implemented)
|
2014-03-20 12:12:13 +01:00
|
|
|
"addmultisigaddress": AddMultiSigAddress,
|
|
|
|
"createmultisig": CreateMultiSig,
|
2014-01-27 15:30:42 +01:00
|
|
|
"dumpprivkey": DumpPrivKey,
|
|
|
|
"getaccount": GetAccount,
|
|
|
|
"getaccountaddress": GetAccountAddress,
|
|
|
|
"getaddressesbyaccount": GetAddressesByAccount,
|
|
|
|
"getbalance": GetBalance,
|
2014-01-29 18:14:24 +01:00
|
|
|
"getinfo": GetInfo,
|
2014-01-27 15:30:42 +01:00
|
|
|
"getnewaddress": GetNewAddress,
|
2014-02-03 16:52:02 +01:00
|
|
|
"getrawchangeaddress": GetRawChangeAddress,
|
2014-02-03 19:00:28 +01:00
|
|
|
"getreceivedbyaccount": GetReceivedByAccount,
|
2014-02-03 19:29:25 +01:00
|
|
|
"gettransaction": GetTransaction,
|
2014-01-27 15:30:42 +01:00
|
|
|
"importprivkey": ImportPrivKey,
|
|
|
|
"keypoolrefill": KeypoolRefill,
|
|
|
|
"listaccounts": ListAccounts,
|
2014-06-23 23:59:57 +02:00
|
|
|
"listlockunspent": ListLockUnspent,
|
2014-06-06 18:10:29 +02:00
|
|
|
"listreceivedbyaddress": ListReceivedByAddress,
|
2014-01-27 18:53:32 +01:00
|
|
|
"listsinceblock": ListSinceBlock,
|
2014-01-27 15:30:42 +01:00
|
|
|
"listtransactions": ListTransactions,
|
2014-02-13 19:43:52 +01:00
|
|
|
"listunspent": ListUnspent,
|
2014-06-23 23:59:57 +02:00
|
|
|
"lockunspent": LockUnspent,
|
2014-01-27 15:30:42 +01:00
|
|
|
"sendfrom": SendFrom,
|
|
|
|
"sendmany": SendMany,
|
2014-02-04 17:33:29 +01:00
|
|
|
"sendtoaddress": SendToAddress,
|
2014-01-27 15:30:42 +01:00
|
|
|
"settxfee": SetTxFee,
|
2014-02-12 03:14:07 +01:00
|
|
|
"signmessage": SignMessage,
|
2014-04-18 00:13:53 +02:00
|
|
|
"signrawtransaction": SignRawTransaction,
|
2014-06-24 23:00:27 +02:00
|
|
|
"stop": Stop,
|
2014-02-14 00:16:21 +01:00
|
|
|
"validateaddress": ValidateAddress,
|
2014-02-12 03:14:07 +01:00
|
|
|
"verifymessage": VerifyMessage,
|
2014-01-27 15:30:42 +01:00
|
|
|
"walletlock": WalletLock,
|
|
|
|
"walletpassphrase": WalletPassphrase,
|
|
|
|
"walletpassphrasechange": WalletPassphraseChange,
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2013-12-30 18:44:51 +01:00
|
|
|
// Standard bitcoind methods (currently unimplemented)
|
2014-01-27 15:30:42 +01:00
|
|
|
"backupwallet": Unimplemented,
|
|
|
|
"dumpwallet": Unimplemented,
|
|
|
|
"getreceivedbyaddress": Unimplemented,
|
|
|
|
"gettxout": Unimplemented,
|
|
|
|
"gettxoutsetinfo": Unimplemented,
|
2014-06-12 18:39:26 +02:00
|
|
|
"getwalletinfo": Unimplemented,
|
2014-01-27 15:30:42 +01:00
|
|
|
"importwallet": Unimplemented,
|
|
|
|
"listaddressgroupings": Unimplemented,
|
|
|
|
"listreceivedbyaccount": Unimplemented,
|
|
|
|
"move": Unimplemented,
|
|
|
|
"setaccount": Unimplemented,
|
2013-12-30 18:44:51 +01:00
|
|
|
|
|
|
|
// 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.
|
2014-06-12 18:39:26 +02:00
|
|
|
var wsHandlers = map[string]requestHandler{
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
"exportwatchingwallet": ExportWatchingWallet,
|
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,
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
"recoveraddresses": RecoverAddresses,
|
2013-12-30 17:10:06 +01:00
|
|
|
"walletislocked": WalletIsLocked,
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// handlerFunc looks up a request handler func for the passed method from
|
|
|
|
// the http post and (if the request is from a websocket connection) websocket
|
|
|
|
// handler maps. If a suitable handler could not be found, ok is false.
|
|
|
|
func handlerFunc(method string, ws bool) (f requestHandler, ok bool) {
|
|
|
|
f, ok = rpcHandlers[method]
|
|
|
|
if !ok && ws {
|
|
|
|
f, ok = wsHandlers[method]
|
2013-08-21 17:14:21 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
return f, ok
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
type handlerResponse struct {
|
|
|
|
result interface{}
|
|
|
|
jsonErr *btcjson.Error
|
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
type handlerJob struct {
|
|
|
|
request btcjson.Cmd
|
|
|
|
handler requestHandler
|
|
|
|
response chan<- handlerResponse
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
// RequestHandler reads and processes client requests from the request channel.
|
|
|
|
// Each request is run with exclusive access to the account manager.
|
|
|
|
func (s *rpcServer) RequestHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-s.requests:
|
|
|
|
AcctMgr.Grab()
|
|
|
|
result, err := r.handler(r.request)
|
|
|
|
AcctMgr.Release()
|
2014-03-28 04:18:23 +01:00
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
var jsonErr *btcjson.Error
|
|
|
|
if err != nil {
|
|
|
|
jsonErr = &btcjson.Error{Message: err.Error()}
|
|
|
|
switch e := err.(type) {
|
|
|
|
case btcjson.Error:
|
|
|
|
*jsonErr = e
|
|
|
|
case DeserializationError:
|
|
|
|
jsonErr.Code = btcjson.ErrDeserialization.Code
|
|
|
|
case InvalidParameterError:
|
|
|
|
jsonErr.Code = btcjson.ErrInvalidParameter.Code
|
|
|
|
case ParseError:
|
|
|
|
jsonErr.Code = btcjson.ErrParse.Code
|
|
|
|
case InvalidAddressOrKeyError:
|
|
|
|
jsonErr.Code = btcjson.ErrInvalidAddressOrKey.Code
|
|
|
|
default: // All other errors get the wallet error code.
|
|
|
|
jsonErr.Code = btcjson.ErrWallet.Code
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-24 23:00:27 +02:00
|
|
|
r.response <- handlerResponse{result, jsonErr}
|
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-24 23:00:27 +02:00
|
|
|
s.wg.Done()
|
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-06-12 18:39:26 +02:00
|
|
|
func Unimplemented(btcjson.Cmd) (interface{}, error) {
|
2014-06-04 02:52:04 +02:00
|
|
|
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-06-12 18:39:26 +02:00
|
|
|
func Unsupported(btcjson.Cmd) (interface{}, error) {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, 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-06-12 18:39:26 +02:00
|
|
|
// makeMultiSigScript is a helper function to combine common logic for
|
2014-03-20 12:12:13 +01:00
|
|
|
// AddMultiSig and CreateMultiSig.
|
|
|
|
// all error codes are rpc parse error here to match bitcoind which just throws
|
|
|
|
// a runtime exception. *sigh*.
|
2014-06-04 02:52:04 +02:00
|
|
|
func makeMultiSigScript(keys []string, nRequired int) ([]byte, error) {
|
2014-03-20 12:12:13 +01:00
|
|
|
keysesPrecious := make([]*btcutil.AddressPubKey, len(keys))
|
|
|
|
|
|
|
|
// The address list will made up either of addreseses (pubkey hash), for
|
|
|
|
// which we need to look up the keys in wallet, straight pubkeys, or a
|
|
|
|
// mixture of the two.
|
|
|
|
for i, a := range keys {
|
|
|
|
// try to parse as pubkey address
|
2014-05-27 19:50:51 +02:00
|
|
|
a, err := btcutil.DecodeAddress(a, activeNet.Params)
|
2014-03-20 12:12:13 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch addr := a.(type) {
|
|
|
|
case *btcutil.AddressPubKey:
|
|
|
|
keysesPrecious[i] = addr
|
|
|
|
case *btcutil.AddressPubKeyHash:
|
2014-04-18 00:00:52 +02:00
|
|
|
ainfo, err := AcctMgr.Address(addr)
|
2014-03-20 12:12:13 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
apkinfo := ainfo.(wallet.PubKeyAddress)
|
2014-03-20 12:12:13 +01:00
|
|
|
|
|
|
|
// This will be an addresspubkey
|
2014-04-09 02:18:52 +02:00
|
|
|
a, err := btcutil.DecodeAddress(apkinfo.ExportPubKey(),
|
2014-05-27 19:50:51 +02:00
|
|
|
activeNet.Params)
|
2014-03-20 12:12:13 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
apk := a.(*btcutil.AddressPubKey)
|
|
|
|
keysesPrecious[i] = apk
|
|
|
|
default:
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return btcscript.MultiSigScript(keysesPrecious, nRequired)
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddMultiSigAddress handles an addmultisigaddress request by adding a
|
|
|
|
// multisig address to the given wallet.
|
2014-06-04 02:52:04 +02:00
|
|
|
func AddMultiSigAddress(icmd btcjson.Cmd) (interface{}, error) {
|
2014-03-20 12:12:13 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.AddMultisigAddressCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
acct, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
script, err := makeMultiSigScript(cmd.Keys, cmd.NRequired)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ParseError{err}
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(oga) blockstamp current block?
|
|
|
|
address, err := acct.ImportScript(script, &wallet.BlockStamp{})
|
2014-04-18 00:13:53 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
2014-03-20 12:12:13 +01:00
|
|
|
|
|
|
|
return address.EncodeAddress(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMultiSig handles an createmultisig request by returning a
|
|
|
|
// multisig address for the given inputs.
|
2014-06-04 02:52:04 +02:00
|
|
|
func CreateMultiSig(icmd btcjson.Cmd) (interface{}, error) {
|
2014-03-20 12:12:13 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.CreateMultisigCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
script, err := makeMultiSigScript(cmd.Keys, cmd.NRequired)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ParseError{err}
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
address, err := btcutil.NewAddressScriptHash(script, activeNet.Params)
|
2014-03-20 12:12:13 +01:00
|
|
|
if err != nil {
|
|
|
|
// above is a valid script, shouldn't happen.
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-03-20 12:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:57:28 +02:00
|
|
|
return btcjson.CreateMultiSigResult{
|
|
|
|
Address: address.EncodeAddress(),
|
|
|
|
RedeemScript: hex.EncodeToString(script),
|
2014-03-20 12:12:13 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-06-04 02:52:04 +02:00
|
|
|
func DumpPrivKey(icmd btcjson.Cmd) (interface{}, error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.DumpPrivKeyCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-01-06 18:24:29 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
key, err := AcctMgr.DumpWIFPrivateKey(addr)
|
|
|
|
if err == wallet.ErrWalletLocked {
|
2013-12-02 20:56:06 +01:00
|
|
|
// Address was found, but the private key isn't
|
|
|
|
// accessible.
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrWalletUnlockNeeded
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return key, err
|
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-06-04 02:52:04 +02:00
|
|
|
func DumpWallet(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
keys, err := AcctMgr.DumpKeys()
|
|
|
|
if err == wallet.ErrWalletLocked {
|
|
|
|
// Address was found, but the private key isn't
|
|
|
|
// accessible.
|
|
|
|
return nil, btcjson.ErrWalletUnlockNeeded
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return keys, err
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
// ExportWatchingWallet handles an exportwatchingwallet request by exporting
|
|
|
|
// the current account wallet as a watching wallet (with no private keys), and
|
|
|
|
// either writing the exported wallet to disk, or base64-encoding serialized
|
|
|
|
// account files and sending them back in the response.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ExportWatchingWallet(icmd btcjson.Cmd) (interface{}, error) {
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.ExportWatchingWalletCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wa, err := a.ExportWatchingWallet()
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Download {
|
2014-06-04 02:52:04 +02:00
|
|
|
return wa.exportBase64()
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create export directory, write files there.
|
2014-06-04 02:52:04 +02:00
|
|
|
err = wa.ExportToDirectory("watchingwallet")
|
|
|
|
return nil, err
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +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-06-04 02:52:04 +02:00
|
|
|
func GetAddressesByAccount(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-10-08 04:17:27 +02:00
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.SortedActivePaymentAddresses(), nil
|
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-06-04 02:52:04 +02:00
|
|
|
func GetBalance(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-03 17:00:01 +02:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
balance, err := AcctMgr.CalculateBalance(cmd.Account, cmd.MinConf)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return balance, err
|
2013-09-03 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:14:24 +01:00
|
|
|
// GetInfo handles a getinfo request by returning the a structure containing
|
|
|
|
// information about the current state of btcwallet.
|
|
|
|
// exist.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetInfo(icmd btcjson.Cmd) (interface{}, error) {
|
2014-01-29 18:14:24 +01:00
|
|
|
// Call down to btcd for all of the information in this command known
|
2014-06-12 18:39:26 +02:00
|
|
|
// by them.
|
|
|
|
client, err := accessClient()
|
2014-05-28 06:54:50 +02:00
|
|
|
if err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
return nil, err
|
2014-05-28 06:54:50 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
info, err := client.GetInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-01-29 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
balance := float64(0.0)
|
2014-01-30 16:14:02 +01:00
|
|
|
accounts := AcctMgr.ListAccounts(1)
|
2014-01-29 18:14:24 +01:00
|
|
|
for _, v := range accounts {
|
|
|
|
balance += v
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
info.WalletVersion = int(wallet.VersCurrent.Uint32())
|
|
|
|
info.Balance = balance
|
2014-01-29 18:14:24 +01:00
|
|
|
// Keypool times are not tracked. set to current time.
|
2014-04-09 18:07:09 +02:00
|
|
|
info.KeypoolOldest = time.Now().Unix()
|
|
|
|
info.KeypoolSize = int(cfg.KeypoolSize)
|
2014-01-29 18:14:24 +01:00
|
|
|
TxFeeIncrement.Lock()
|
2014-04-09 18:07:09 +02:00
|
|
|
info.PaytxFee = float64(TxFeeIncrement.i) / float64(btcutil.SatoshiPerBitcoin)
|
2014-01-29 18:14:24 +01:00
|
|
|
TxFeeIncrement.Unlock()
|
2014-06-12 18:39:26 +02:00
|
|
|
// We don't set the following since they don't make much sense in the
|
|
|
|
// wallet architecture:
|
|
|
|
// - unlocked_until
|
|
|
|
// - errors
|
2014-01-29 18:14:24 +01:00
|
|
|
|
2014-04-09 18:07:09 +02:00
|
|
|
return info, nil
|
2014-01-29 18:14:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// GetAccount handles a getaccount request by returning the account name
|
|
|
|
// associated with a single address.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetAccount(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-30 22:31:33 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetAccountCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is address valid?
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil || !addr.IsForNet(activeNet.Params) {
|
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look up account which holds this address.
|
2014-04-03 17:00:46 +02:00
|
|
|
acct, err := AcctMgr.AccountByAddress(addr)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, ErrAddressNotInWallet
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-30 22:31:33 +01:00
|
|
|
}
|
2014-04-03 17:00:46 +02:00
|
|
|
return acct.Name(), 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-06-04 02:52:04 +02:00
|
|
|
func GetAccountAddress(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-31 19:11:47 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetAccountAddressCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup account for this request.
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
addr, err := a.CurrentAddress()
|
|
|
|
if err != nil {
|
|
|
|
if err == wallet.ErrWalletLocked {
|
|
|
|
return nil, btcjson.ErrWalletKeypoolRanOut
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return addr.EncodeAddress(), err
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetAddressBalance(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-10 22:15:25 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.GetAddressBalanceCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is address valid?
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-01-06 18:24:29 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
2013-12-10 22:15:25 +01:00
|
|
|
|
2014-04-03 17:00:46 +02:00
|
|
|
// Get the account which holds the address in the request.
|
|
|
|
a, err := AcctMgr.AccountByAddress(addr)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ErrAddressNotInWallet
|
2013-12-10 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:22:39 +02:00
|
|
|
return a.CalculateAddressBalance(addr, int(cmd.Minconf)), 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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetUnconfirmedBalance(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-30 21:11:41 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.GetUnconfirmedBalanceCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the account included in the request.
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-30 21:11:41 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.CalculateBalance(0) - a.CalculateBalance(1), 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-06-04 02:52:04 +02:00
|
|
|
func ImportPrivKey(icmd btcjson.Cmd) (interface{}, error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ImportPrivKeyCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02: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...
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Label)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-05-22 00:50:47 +02:00
|
|
|
wif, err := btcutil.DecodeWIF(cmd.PrivKey)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil || !wif.IsForNet(activeNet.Params) {
|
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Import the private key, handling any errors.
|
2014-06-04 02:52:04 +02:00
|
|
|
bs := wallet.BlockStamp{}
|
|
|
|
if _, err := a.ImportPrivateKey(wif, &bs, cmd.Rescan); err != nil {
|
|
|
|
switch err {
|
|
|
|
case wallet.ErrDuplicate:
|
|
|
|
// Do not return duplicate key errors to the client.
|
|
|
|
return nil, nil
|
|
|
|
case wallet.ErrWalletLocked:
|
|
|
|
return nil, btcjson.ErrWalletUnlockNeeded
|
|
|
|
default:
|
|
|
|
return nil, err
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
|
|
|
|
// If the import was successful, reply with nil.
|
|
|
|
return nil, nil
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:28:09 +01:00
|
|
|
// KeypoolRefill handles the keypoolrefill command. Since we handle the keypool
|
|
|
|
// automatically this does nothing since refilling is never manually required.
|
2014-06-04 02:52:04 +02:00
|
|
|
func KeypoolRefill(icmd btcjson.Cmd) (interface{}, error) {
|
2014-01-22 18:28:09 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// NotifyNewBlockChainHeight notifies all websocket clients of a new
|
2014-06-12 18:39:26 +02:00
|
|
|
// blockchain height. This sends the same notification as
|
|
|
|
// btcd, so this can probably be removed.
|
|
|
|
func (s *rpcServer) NotifyNewBlockChainHeight(bs *wallet.BlockStamp) {
|
|
|
|
ntfn := btcws.NewBlockConnectedNtfn(bs.Hash.String(), bs.Height)
|
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// btcws notifications must always marshal without error.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s.broadcasts <- mntfn
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// NotifyBalances notifies an attached websocket clients of the current
|
|
|
|
// confirmed and unconfirmed account balances.
|
2013-10-14 22:39:15 +02:00
|
|
|
//
|
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.
|
2014-06-18 23:46:18 +02:00
|
|
|
func (s *rpcServer) NotifyBalances() {
|
2014-06-12 18:39:26 +02:00
|
|
|
for _, a := range AcctMgr.AllAccounts() {
|
|
|
|
balance := a.CalculateBalance(1)
|
|
|
|
unconfirmed := a.CalculateBalance(0) - balance
|
|
|
|
s.NotifyWalletBalance(a.name, balance)
|
|
|
|
s.NotifyWalletBalanceUnconfirmed(a.name, unconfirmed)
|
|
|
|
}
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetNewAddress(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-11-21 17:57:28 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
addr, err := a.NewAddress()
|
2014-02-03 16:52:02 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-03 16:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the new payment address string.
|
|
|
|
return addr.EncodeAddress(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawChangeAddress handles a getrawchangeaddress request by creating
|
|
|
|
// and returning a new change address for an account.
|
|
|
|
//
|
|
|
|
// Note: bitcoind allows specifying the account as an optional parameter,
|
|
|
|
// but ignores the parameter.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetRawChangeAddress(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-03 16:52:02 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.GetRawChangeAddressCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-03 16:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-11-21 17:57:28 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2014-02-03 16:52:02 +01:00
|
|
|
|
|
|
|
addr, err := a.NewChangeAddress()
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-03 16:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the new payment address string.
|
|
|
|
return addr.EncodeAddress(), nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-02-03 19:00:28 +01:00
|
|
|
// GetReceivedByAccount handles a getreceivedbyaccount request by returning
|
|
|
|
// the total amount received by addresses of an account.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetReceivedByAccount(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-03 19:00:28 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.GetReceivedByAccountCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-03 19:00:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2014-02-03 19:00:28 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-03 19:00:28 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.TotalReceived(cmd.MinConf)
|
2014-02-03 19:00:28 +01:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:49:59 +02:00
|
|
|
// GetTransaction handles a gettransaction request by returning details about
|
|
|
|
// a single transaction saved by wallet.
|
2014-06-04 02:52:04 +02:00
|
|
|
func GetTransaction(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-03 19:29:25 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.GetTransactionCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:49:59 +02:00
|
|
|
txSha, err := btcwire.NewShaHashFromStr(cmd.Txid)
|
2014-02-24 20:35:30 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrDecodeHexString
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:49:59 +02:00
|
|
|
accumulatedTxen := AcctMgr.GetTransaction(txSha)
|
2014-02-03 19:29:25 +01:00
|
|
|
if len(accumulatedTxen) == 0 {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrNoTxInfo
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
|
|
|
|
2014-06-02 18:49:59 +02:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-06-02 18:49:59 +02:00
|
|
|
}
|
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
received := btcutil.Amount(0)
|
2014-06-18 07:16:08 +02:00
|
|
|
var debits *txstore.Debits
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
var debitAccount string
|
2014-06-02 18:49:59 +02:00
|
|
|
var targetAddr string
|
2014-04-11 20:10:27 +02:00
|
|
|
|
|
|
|
ret := btcjson.GetTransactionResult{
|
2014-04-11 20:51:41 +02:00
|
|
|
Details: []btcjson.GetTransactionDetailsResult{},
|
|
|
|
WalletConflicts: []string{},
|
2014-04-11 20:10:27 +02:00
|
|
|
}
|
2014-04-11 20:58:04 +02:00
|
|
|
details := []btcjson.GetTransactionDetailsResult{}
|
2014-02-03 19:29:25 +01:00
|
|
|
for _, e := range accumulatedTxen {
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
for _, cred := range e.Tx.Credits() {
|
|
|
|
// Change is ignored.
|
|
|
|
if cred.Change() {
|
2014-02-24 20:35:30 +01:00
|
|
|
continue
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
received += cred.Amount()
|
2014-04-11 20:10:27 +02:00
|
|
|
|
|
|
|
var addr string
|
2014-05-28 06:54:50 +02:00
|
|
|
// Errors don't matter here, as we only consider the
|
|
|
|
// case where len(addrs) == 1.
|
2014-05-23 04:16:50 +02:00
|
|
|
_, addrs, _, _ := cred.Addresses(activeNet.Params)
|
2014-04-11 20:10:27 +02:00
|
|
|
if len(addrs) == 1 {
|
|
|
|
addr = addrs[0].EncodeAddress()
|
2014-06-02 18:49:59 +02:00
|
|
|
// The first non-change output address is considered the
|
|
|
|
// target for sent transactions.
|
|
|
|
if targetAddr == "" {
|
|
|
|
targetAddr = addr
|
|
|
|
}
|
2014-04-11 20:10:27 +02:00
|
|
|
}
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
|
2014-04-11 20:10:27 +02:00
|
|
|
details = append(details, btcjson.GetTransactionDetailsResult{
|
2014-06-02 18:49:59 +02:00
|
|
|
Account: e.Account,
|
|
|
|
Category: cred.Category(bs.Height).String(),
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
Amount: cred.Amount().ToUnit(btcutil.AmountBTC),
|
2014-04-11 20:10:27 +02:00
|
|
|
Address: addr,
|
2014-02-03 19:29:25 +01:00
|
|
|
})
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 07:16:08 +02:00
|
|
|
if d, err := e.Tx.Debits(); err == nil {
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
// There should only be a single debits record for any
|
|
|
|
// of the account's transaction records.
|
2014-06-18 07:16:08 +02:00
|
|
|
debits = &d
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
debitAccount = e.Account
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
totalAmount := received
|
2014-06-18 07:16:08 +02:00
|
|
|
if debits != nil {
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
totalAmount -= debits.InputAmount()
|
2014-04-11 20:10:27 +02:00
|
|
|
info := btcjson.GetTransactionDetailsResult{
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
Account: debitAccount,
|
2014-06-02 18:49:59 +02:00
|
|
|
Address: targetAddr,
|
2014-04-11 20:10:27 +02:00
|
|
|
Category: "send",
|
2014-02-24 20:35:30 +01:00
|
|
|
// negative since it is a send
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
Amount: (-debits.OutputAmount(true)).ToUnit(btcutil.AmountBTC),
|
|
|
|
Fee: debits.Fee().ToUnit(btcutil.AmountBTC),
|
2014-04-11 20:10:27 +02:00
|
|
|
}
|
|
|
|
ret.Fee += info.Fee
|
2014-02-24 20:35:30 +01:00
|
|
|
// Add sent information to front.
|
2014-04-11 20:10:27 +02:00
|
|
|
ret.Details = append(ret.Details, info)
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
2014-04-11 20:10:27 +02:00
|
|
|
ret.Details = append(ret.Details, details...)
|
2014-02-24 20:35:30 +01:00
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
ret.Amount = totalAmount.ToUnit(btcutil.AmountBTC)
|
|
|
|
|
2014-02-03 19:29:25 +01:00
|
|
|
// Generic information should be the same, so just use the first one.
|
|
|
|
first := accumulatedTxen[0]
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
ret.TxID = first.Tx.Tx().Sha().String()
|
2014-04-11 20:10:27 +02:00
|
|
|
|
2014-04-16 23:22:39 +02:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
buf.Grow(first.Tx.Tx().MsgTx().SerializeSize())
|
2014-04-11 20:10:27 +02:00
|
|
|
err = first.Tx.Tx().MsgTx().Serialize(buf)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
2014-04-11 20:10:27 +02:00
|
|
|
ret.Hex = hex.EncodeToString(buf.Bytes())
|
|
|
|
|
|
|
|
// TODO(oga) technically we have different time and
|
|
|
|
// timereceived depending on if a transaction was send or
|
|
|
|
// receive. We ideally should provide the correct numbers for
|
|
|
|
// both. Right now they will always be the same
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
ret.Time = first.Tx.Received().Unix()
|
|
|
|
ret.TimeReceived = first.Tx.Received().Unix()
|
|
|
|
if txr := first.Tx; txr.BlockHeight != -1 {
|
|
|
|
txBlock, err := txr.Block()
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
}
|
2014-06-02 18:49:59 +02:00
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
ret.BlockIndex = int64(first.Tx.Tx().Index())
|
|
|
|
ret.BlockHash = txBlock.Hash.String()
|
|
|
|
ret.BlockTime = txBlock.Time.Unix()
|
2014-05-07 05:48:12 +02:00
|
|
|
ret.Confirmations = int64(txr.Confirmations(bs.Height))
|
2014-02-03 19:29:25 +01:00
|
|
|
}
|
|
|
|
// TODO(oga) if the tx is a coinbase we should set "generated" to true.
|
|
|
|
// Since we do not mine this currently is never the case.
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
// ListAccounts handles a listaccounts request by returning a map of account
|
|
|
|
// names to their balances.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListAccounts(icmd btcjson.Cmd) (interface{}, error) {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ListAccountsCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02: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.
|
2014-01-30 16:14:02 +01:00
|
|
|
return AcctMgr.ListAccounts(cmd.MinConf), nil
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2014-06-23 23:59:57 +02:00
|
|
|
// ListLockUnspent handles a listlockunspent request by returning an array of
|
|
|
|
// all locked outpoints.
|
|
|
|
func ListLockUnspent(icmd btcjson.Cmd) (interface{}, error) {
|
|
|
|
// Due to our poor account support, this assumes only the default
|
|
|
|
// account is available. When the keystore and account heirarchies are
|
|
|
|
// reversed, the locked outpoints mapping will cover all accounts.
|
|
|
|
a, err := AcctMgr.Account("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.LockedOutpoints(), nil
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:10:29 +02:00
|
|
|
// ListReceivedByAddress handles a listreceivedbyaddress request by returning
|
|
|
|
// a slice of objects, each one containing:
|
|
|
|
// "account": the account of the receiving address;
|
|
|
|
// "address": the receiving address;
|
|
|
|
// "amount": total amount received by the address;
|
|
|
|
// "confirmations": number of confirmations of the most recent transaction.
|
|
|
|
// It takes two parameters:
|
|
|
|
// "minconf": minimum number of confirmations to consider a transaction -
|
|
|
|
// default: one;
|
|
|
|
// "includeempty": whether or not to include addresses that have no transactions -
|
|
|
|
// default: false.
|
|
|
|
func ListReceivedByAddress(icmd btcjson.Cmd) (interface{}, error) {
|
|
|
|
cmd, ok := icmd.(*btcjson.ListReceivedByAddressCmd)
|
|
|
|
if !ok {
|
|
|
|
return nil, btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intermediate data for each address.
|
|
|
|
type AddrData struct {
|
|
|
|
// Associated account.
|
|
|
|
account *Account
|
|
|
|
// Total amount received.
|
|
|
|
amount btcutil.Amount
|
|
|
|
// Number of confirmations of the last transaction.
|
|
|
|
confirmations int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intermediate data for all addresses.
|
|
|
|
allAddrData := make(map[string]AddrData)
|
|
|
|
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, account := range AcctMgr.AllAccounts() {
|
|
|
|
if cmd.IncludeEmpty {
|
|
|
|
// Create an AddrData entry for each active address in the account.
|
|
|
|
// Otherwise we'll just get addresses from transactions later.
|
|
|
|
for _, address := range account.SortedActivePaymentAddresses() {
|
|
|
|
// There might be duplicates, just overwrite them.
|
|
|
|
allAddrData[address] = AddrData{account: account}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, record := range account.TxStore.Records() {
|
|
|
|
for _, credit := range record.Credits() {
|
|
|
|
confirmations := credit.Confirmations(bs.Height)
|
|
|
|
if !credit.Confirmed(cmd.MinConf, bs.Height) {
|
|
|
|
// Not enough confirmations, skip the current block.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, addresses, _, err := credit.Addresses(activeNet.Params)
|
|
|
|
if err != nil {
|
|
|
|
// Unusable address, skip it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, address := range addresses {
|
|
|
|
addrStr := address.EncodeAddress()
|
|
|
|
addrData, ok := allAddrData[addrStr]
|
|
|
|
if ok {
|
|
|
|
// Address already present, check account consistency.
|
|
|
|
if addrData.account != account {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Address %v in both account %v and account %v",
|
|
|
|
addrStr, addrData.account.name, account.name)
|
|
|
|
}
|
|
|
|
addrData.amount += credit.Amount()
|
|
|
|
// Always overwrite confirmations with newer ones.
|
|
|
|
addrData.confirmations = confirmations
|
|
|
|
} else {
|
|
|
|
addrData = AddrData{
|
|
|
|
account: account,
|
|
|
|
amount: credit.Amount(),
|
|
|
|
confirmations: confirmations,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
allAddrData[addrStr] = addrData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Massage address data into output format.
|
|
|
|
numAddresses := len(allAddrData)
|
|
|
|
ret := make([]btcjson.ListReceivedByAddressResult, numAddresses, numAddresses)
|
|
|
|
idx := 0
|
|
|
|
for address, addrData := range allAddrData {
|
|
|
|
ret[idx] = btcjson.ListReceivedByAddressResult{
|
|
|
|
Account: addrData.account.name,
|
|
|
|
Address: address,
|
|
|
|
Amount: addrData.amount.ToUnit(btcutil.AmountBTC),
|
|
|
|
Confirmations: uint64(addrData.confirmations),
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-01-27 18:53:32 +01:00
|
|
|
// ListSinceBlock handles a listsinceblock request by returning an array of maps
|
|
|
|
// with details of sent and received wallet transactions since the given block.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListSinceBlock(icmd btcjson.Cmd) (interface{}, error) {
|
2014-01-27 18:53:32 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.ListSinceBlockCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
client, err := accessClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-27 18:53:32 +01:00
|
|
|
height := int32(-1)
|
|
|
|
if cmd.BlockHash != "" {
|
2014-06-12 18:39:26 +02:00
|
|
|
hash, err := btcwire.NewShaHashFromStr(cmd.BlockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, DeserializationError{err}
|
|
|
|
}
|
|
|
|
block, err := client.GetBlock(hash)
|
2014-01-27 18:53:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
height = int32(block.Height())
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// For the result we need the block hash for the last block counted
|
|
|
|
// in the blockchain due to confirmations. We send this off now so that
|
|
|
|
// it can arrive asynchronously while we figure out the rest.
|
2014-06-12 18:39:26 +02:00
|
|
|
gbh := client.GetBlockHashAsync(int64(bs.Height) + 1 - int64(cmd.TargetConfirmations))
|
2014-01-27 18:53:32 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
txInfoList, err := AcctMgr.ListSinceBlock(height, bs.Height,
|
2014-01-27 18:53:32 +01:00
|
|
|
cmd.TargetConfirmations)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done with work, get the response.
|
2014-06-12 18:39:26 +02:00
|
|
|
blockHash, err := gbh.Receive()
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-01-27 18:53:32 +01:00
|
|
|
}
|
|
|
|
|
2014-04-12 02:33:15 +02:00
|
|
|
res := btcjson.ListSinceBlockResult{
|
|
|
|
Transactions: txInfoList,
|
2014-06-12 18:39:26 +02:00
|
|
|
LastBlock: blockHash.String(),
|
2014-04-12 02:33:15 +02:00
|
|
|
}
|
2014-01-27 18:53:32 +01:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListTransactions(icmd btcjson.Cmd) (interface{}, error) {
|
2013-11-22 19:42:25 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.ListTransactionsCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.ListTransactions(cmd.From, cmd.Count)
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListAddressTransactions(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-30 17:10:06 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.ListAddressTransactionsCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
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 {
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(addrStr, activeNet.Params)
|
2014-01-06 18:24:29 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-01-06 18:24:29 +01:00
|
|
|
}
|
|
|
|
apkh, ok := addr.(*btcutil.AddressPubKeyHash)
|
2014-05-27 19:50:51 +02:00
|
|
|
if !ok || !apkh.IsForNet(activeNet.Params) {
|
2014-06-04 02:52:04 +02: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
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.ListAddressTransactions(pkHashMap)
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListAllTransactions(icmd btcjson.Cmd) (interface{}, error) {
|
2013-12-02 23:34:36 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcws.ListAllTransactionsCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return a.ListAllTransactions()
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 19:43:52 +01:00
|
|
|
// ListUnspent handles the listunspent command.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ListUnspent(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-13 19:43:52 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.ListUnspentCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-13 19:43:52 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
|
2014-02-13 19:43:52 +01:00
|
|
|
addresses := make(map[string]bool)
|
|
|
|
if len(cmd.Addresses) != 0 {
|
|
|
|
// confirm that all of them are good:
|
|
|
|
for _, as := range cmd.Addresses {
|
2014-05-27 19:50:51 +02:00
|
|
|
a, err := btcutil.DecodeAddress(as, activeNet.Params)
|
2014-02-13 19:43:52 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-02-13 19:43:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := addresses[a.EncodeAddress()]; ok {
|
|
|
|
// duplicate
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidParameter
|
2014-02-13 19:43:52 +01:00
|
|
|
}
|
|
|
|
addresses[a.EncodeAddress()] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
return AcctMgr.ListUnspent(cmd.MinConf, cmd.MaxConf, addresses)
|
2014-02-13 19:43:52 +01:00
|
|
|
}
|
|
|
|
|
2014-06-23 23:59:57 +02:00
|
|
|
// LockUnspent handles the lockunspent command.
|
|
|
|
func LockUnspent(icmd btcjson.Cmd) (interface{}, error) {
|
|
|
|
cmd, ok := icmd.(*btcjson.LockUnspentCmd)
|
|
|
|
if !ok {
|
|
|
|
return nil, btcjson.ErrInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Due to our poor account support, this assumes only the default
|
|
|
|
// account is available. When the keystore and account heirarchies are
|
|
|
|
// reversed, the locked outpoints mapping will cover all accounts.
|
|
|
|
a, err := AcctMgr.Account("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case cmd.Unlock && len(cmd.Transactions) == 0:
|
|
|
|
a.ResetLockedOutpoints()
|
|
|
|
default:
|
|
|
|
for _, input := range cmd.Transactions {
|
|
|
|
txSha, err := btcwire.NewShaHashFromStr(input.Txid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ParseError{err}
|
|
|
|
}
|
|
|
|
op := btcwire.OutPoint{Hash: *txSha, Index: input.Vout}
|
|
|
|
if cmd.Unlock {
|
|
|
|
a.UnlockOutpoint(op)
|
|
|
|
} else {
|
|
|
|
a.LockOutpoint(op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-02-04 17:33:29 +01:00
|
|
|
// sendPairs is a helper routine to reduce duplicated code when creating and
|
|
|
|
// sending payment transactions.
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]btcutil.Amount,
|
2014-06-04 02:52:04 +02:00
|
|
|
minconf int) (interface{}, error) {
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
client, err := accessClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// Check that the account specified in the request exists.
|
2014-02-04 17:33:29 +01:00
|
|
|
a, err := AcctMgr.Account(account)
|
2013-12-02 20:56:06 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-10-29 07:19:40 +01:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
|
|
|
// Create transaction, replying with an error if the creation
|
|
|
|
// was not successful.
|
2014-02-04 17:33:29 +01:00
|
|
|
createdTx, err := a.txToPairs(amounts, minconf)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
switch err {
|
|
|
|
case ErrNonPositiveAmount:
|
|
|
|
return nil, ErrNeedPositiveAmount
|
|
|
|
case wallet.ErrWalletLocked:
|
|
|
|
return nil, btcjson.ErrWalletUnlockNeeded
|
|
|
|
default:
|
|
|
|
return nil, err
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
// If a change address was added, sync wallet to disk and request
|
|
|
|
// transaction notifications to the change address.
|
2014-04-09 00:49:02 +02:00
|
|
|
if createdTx.changeAddr != nil {
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.ds.ScheduleWalletWrite(a)
|
|
|
|
if err := AcctMgr.ds.FlushAccount(a); err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, fmt.Errorf("Cannot write account: %v", err)
|
2013-11-06 18:37:46 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
err := client.NotifyReceived([]btcutil.Address{createdTx.changeAddr})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-06 18:37:46 +01:00
|
|
|
}
|
2013-10-28 19:13:20 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
txSha, err := client.SendRawTransaction(createdTx.tx.MsgTx(), false)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-10-15 15:42:10 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
if err := handleSendRawTxReply(icmd, txSha, a, createdTx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return txSha.String(), nil
|
2014-02-04 17:33:29 +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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func SendFrom(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-04 17:33:29 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SendFromCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-04 17:33:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that signed integer parameters are positive.
|
|
|
|
if cmd.Amount < 0 {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ErrNeedPositiveAmount
|
2014-02-04 17:33:29 +01:00
|
|
|
}
|
|
|
|
if cmd.MinConf < 0 {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ErrNeedPositiveMinconf
|
2014-02-04 17:33:29 +01:00
|
|
|
}
|
|
|
|
// Create map of address and amount pairs.
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
pairs := map[string]btcutil.Amount{
|
|
|
|
cmd.ToAddress: btcutil.Amount(cmd.Amount),
|
2014-02-04 17:33:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendPairs(cmd, cmd.FromAccount, pairs, cmd.MinConf)
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func SendMany(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02: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-06-04 02:52:04 +02:00
|
|
|
return nil, ErrNeedPositiveMinconf
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
// Recreate address/amount pairs, using btcutil.Amount.
|
|
|
|
pairs := make(map[string]btcutil.Amount, len(cmd.Amounts))
|
|
|
|
for k, v := range cmd.Amounts {
|
|
|
|
pairs[k] = btcutil.Amount(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendPairs(cmd, cmd.FromAccount, pairs, cmd.MinConf)
|
2014-02-04 17:33:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendToAddress handles a sendtoaddress 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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func SendToAddress(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-04 17:33:29 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SendToAddressCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 17:33:29 +01:00
|
|
|
// Check that signed integer parameters are positive.
|
|
|
|
if cmd.Amount < 0 {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ErrNeedPositiveAmount
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 17:33:29 +01:00
|
|
|
// Mock up map of address and amount pairs.
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
pairs := map[string]btcutil.Amount{
|
|
|
|
cmd.Address: btcutil.Amount(cmd.Amount),
|
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-02-04 17:33:29 +01:00
|
|
|
return sendPairs(cmd, "", pairs, 1)
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
2013-10-15 16:27:54 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func handleSendRawTxReply(icmd btcjson.Cmd, txSha *btcwire.ShaHash, a *Account, txInfo *CreatedTx) error {
|
2013-11-22 19:42:25 +01:00
|
|
|
// Add to transaction store.
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
txr, err := a.TxStore.InsertTx(txInfo.tx, nil)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
log.Errorf("Error adding sent tx history: %v", err)
|
2014-06-12 18:39:26 +02:00
|
|
|
return btcjson.ErrInternal
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
}
|
|
|
|
debits, err := txr.AddDebits(txInfo.inputs)
|
2014-02-28 19:03:23 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
log.Errorf("Error adding sent tx history: %v", err)
|
2014-06-12 18:39:26 +02:00
|
|
|
return btcjson.ErrInternal
|
2014-02-28 19:03:23 +01:00
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.ds.ScheduleTxStoreWrite(a)
|
2013-11-22 19:42:25 +01:00
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Notify websocket clients of the transaction.
|
2013-12-02 23:34:36 +01:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err == nil {
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
ltr, err := debits.ToJSON(a.Name(), bs.Height, a.Net())
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
log.Errorf("Error adding sent tx history: %v", err)
|
2014-06-12 18:39:26 +02:00
|
|
|
return btcjson.ErrInternal
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
}
|
|
|
|
for _, details := range ltr {
|
2014-06-12 18:39:26 +02:00
|
|
|
server.NotifyNewTxDetails(a.Name(), details)
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// Disk sync tx and utxo stores.
|
2014-01-30 16:14:02 +01:00
|
|
|
if err := AcctMgr.ds.FlushAccount(a); err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
log.Errorf("Cannot write account: %v", err)
|
2014-06-12 18:39:26 +02:00
|
|
|
return err
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Notify websocket clients of account's new unconfirmed and
|
2013-11-22 19:42:25 +01:00
|
|
|
// confirmed balance.
|
|
|
|
confirmed := a.CalculateBalance(1)
|
|
|
|
unconfirmed := a.CalculateBalance(0) - confirmed
|
2014-06-12 18:39:26 +02:00
|
|
|
server.NotifyWalletBalance(a.name, confirmed)
|
|
|
|
server.NotifyWalletBalanceUnconfirmed(a.name, unconfirmed)
|
2013-11-22 19:42:25 +01:00
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// 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-02-04 17:33:29 +01:00
|
|
|
case *btcjson.SendToAddressCmd:
|
|
|
|
_ = cmd.Comment
|
|
|
|
_ = cmd.CommentTo
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
log.Infof("Successfully sent transaction %v", txSha)
|
|
|
|
return 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-06-04 02:52:04 +02:00
|
|
|
func SetTxFee(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02: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-06-04 02:52:04 +02:00
|
|
|
return nil, ErrNeedPositiveAmount
|
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()
|
Another day, another tx store implementation.
The last transaction store was a great example of how not to write
scalable software. For a variety of reasons, it was very slow at
processing transaction inserts. Among them:
1) Every single transaction record being saved in a linked list
(container/list), and inserting into this list would be an O(n)
operation so that records could be ordered by receive date.
2) Every single transaction in the above mentioned list was iterated
over in order to find double spends which must be removed. It is
silly to do this check for mined transactions, which already have
been checked for this by btcd. Worse yet, if double spends were
found, the list would be iterated a second (or third, or fourth)
time for each removed transaction.
3) All spend tracking for signed-by-wallet transactions was found on
each transaction insert, even if the now spent previous transaction
outputs were known by the caller.
This list could keep going on, but you get the idea. It was bad.
To resolve these issues a new transaction store had to be implemented.
The new implementation:
1) Tracks mined and unmined transactions in different data structures.
Mined transactions are cheap to track because the required double
spend checks have already been performed by the chain server, and
double spend checks are only required to be performed on
newly-inserted mined transactions which may conflict with previous
unmined transactions.
2) Saves mined transactions grouped by block first, and then by their
transaction index. Lookup keys for mined transactions are simply
the block height (in the best chain, that's all we save) and index
of the transaction in the block. This makes looking up any
arbitrary transaction almost an O(1) operation (almost, because
block height and block indexes are mapped to their slice indexes
with a Go map).
3) Saves records in each transaction for whether the outputs are
wallet credits (spendable by wallet) and for whether inputs debit
from previous credits. Both structures point back to the source
or spender (credits point to the transaction that spends them, or
nil for unspent credits, and debits include keys to lookup the
transaction credits they spent. While complicated to keep track
of, this greatly simplifies the spent tracking for transactions
across rollbacks and transaction removals.
4) Implements double spend checking as an almost O(1) operation. A
Go map is used to map each previous outpoint for all unconfirmed
transactions to the unconfirmed tx record itself. Checking for
double spends on confirmed transaction inserts only involves
looking up each previous outpoint of the inserted tx in this map.
If a double spend is found, removal is simplified by only
removing the transaction and its spend chain from store maps,
rather than iterating a linked list several times over to remove
each dead transaction in the spend chain.
5) Allows the caller to specify the previous credits which are spent
by a debiting transaction. When a transaction is created by
wallet, the previous outputs are already known, and by passing
their record types to the AddDebits method, lookups for each
previously unspent credit are omitted.
6) Bookkeeps all blocks with transactions with unspent credits, and
bookkeeps the transaction indexes of all transactions with unspent
outputs for a single block. For the case where the caller adding a
debit record does not know what credits a transaction debits from,
these bookkeeping structures allow the store to only consider known
unspent transactions, rather than searching through both spent and
unspents.
7) Saves amount deltas for the entire balance as a result of each
block, due to transactions within that block. This improves the
performance of calculating the full balance by not needing to
iterate over every transaction, and then every credit, to determine
if a credit is spent or unspent. When transactions are moved from
unconfirmed to a block structure, the amount deltas are incremented
by the amount of all transaction credits (both spent and unspent)
and debited by the total amount the transaction spends from
previous wallet credits. For the common case of calculating a
balance with just one confirmation, the only involves iterating
over each block structure and adding the (possibly negative)
amount delta. Coinbase rewards are saved similarly, but with a
different amount variable so they can be seperatly included or
excluded.
Due to all of the changes in how the store internally works, the
serialization format has changed. To simplify the serialization
logic, support for reading the last store file version has been
removed. Past this change, a rescan (run automatically) will be
required to rebuild the transaction history.
2014-05-05 23:12:05 +02:00
|
|
|
TxFeeIncrement.i = btcutil.Amount(cmd.Amount)
|
2013-12-04 01:22:47 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-12 03:14:07 +01:00
|
|
|
// SignMessage signs the given message with the private key for the given
|
|
|
|
// address
|
2014-06-04 02:52:04 +02:00
|
|
|
func SignMessage(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-12 03:14:07 +01:00
|
|
|
// Type assert icmd to access parameters.
|
|
|
|
cmd, ok := icmd.(*btcjson.SignMessageCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ParseError{err}
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-04-18 00:00:52 +02:00
|
|
|
ainfo, err := AcctMgr.Address(addr)
|
2014-04-03 17:00:46 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-04-03 17:00:46 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
pka := ainfo.(wallet.PubKeyAddress)
|
|
|
|
privkey, err := pka.PrivKey()
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fullmsg := "Bitcoin Signed Message:\n" + cmd.Message
|
|
|
|
sigbytes, err := btcec.SignCompact(btcec.S256(), privkey,
|
2014-03-06 01:34:44 +01:00
|
|
|
btcwire.DoubleSha256([]byte(fullmsg)), ainfo.Compressed())
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(sigbytes), nil
|
|
|
|
}
|
|
|
|
|
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-06-04 02:52:04 +02:00
|
|
|
func CreateEncryptedWallet(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-09-03 15:49:16 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
err := AcctMgr.CreateEncryptedWallet([]byte(cmd.Passphrase))
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrWalletExists {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2014-05-30 16:31:42 +02:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-10-15 22:55:28 +02:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
|
|
|
|
// A nil reply is sent upon successful wallet creation.
|
|
|
|
return nil, nil
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// RecoverAddresses recovers the next n addresses from an account's wallet.
|
2014-06-04 02:52:04 +02:00
|
|
|
func RecoverAddresses(icmd btcjson.Cmd) (interface{}, error) {
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
cmd, ok := icmd.(*btcws.RecoverAddressesCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
2014-06-04 02:52:04 +02:00
|
|
|
err = a.RecoverAddresses(cmd.N)
|
|
|
|
return nil, err
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
}
|
|
|
|
|
2014-04-18 00:13:53 +02:00
|
|
|
// pendingTx is used for async fetching of transaction dependancies in
|
|
|
|
// SignRawTransaction.
|
|
|
|
type pendingTx struct {
|
2014-06-12 18:39:26 +02:00
|
|
|
resp btcrpcclient.FutureGetRawTransactionResult
|
2014-04-18 00:13:53 +02:00
|
|
|
inputs []uint32 // list of inputs that care about this tx.
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignRawTransaction handles the signrawtransaction command.
|
2014-06-04 02:52:04 +02:00
|
|
|
func SignRawTransaction(icmd btcjson.Cmd) (interface{}, error) {
|
2014-04-18 00:13:53 +02:00
|
|
|
cmd, ok := icmd.(*btcjson.SignRawTransactionCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
serializedTx, err := hex.DecodeString(cmd.RawTx)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrDecodeHexString
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
msgTx := btcwire.NewMsgTx()
|
|
|
|
err = msgTx.Deserialize(bytes.NewBuffer(serializedTx))
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
e := errors.New("TX decode failed")
|
|
|
|
return nil, DeserializationError{e}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// First we add the stuff we have been given.
|
|
|
|
// TODO(oga) really we probably should look these up with btcd anyway
|
|
|
|
// to make sure that they match the blockchain if present.
|
|
|
|
inputs := make(map[btcwire.OutPoint][]byte)
|
|
|
|
scripts := make(map[string][]byte)
|
|
|
|
for _, rti := range cmd.Inputs {
|
|
|
|
inputSha, err := btcwire.NewShaHashFromStr(rti.Txid)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
script, err := hex.DecodeString(rti.ScriptPubKey)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// redeemScript is only actually used iff the user provided
|
|
|
|
// private keys. In which case, it is used to get the scripts
|
|
|
|
// for signing. If the user did not provide keys then we always
|
|
|
|
// get scripts from the wallet.
|
|
|
|
// Empty strings are ok for this one and hex.DecodeString will
|
|
|
|
// DTRT.
|
|
|
|
if len(cmd.PrivKeys) != 0 {
|
|
|
|
redeemScript, err := hex.DecodeString(rti.RedeemScript)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := btcutil.NewAddressScriptHash(redeemScript,
|
2014-05-27 19:50:51 +02:00
|
|
|
activeNet.Params)
|
2014-04-18 00:13:53 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
scripts[addr.String()] = redeemScript
|
|
|
|
}
|
|
|
|
inputs[btcwire.OutPoint{
|
|
|
|
Hash: *inputSha,
|
2014-06-16 23:25:04 +02:00
|
|
|
Index: rti.Vout,
|
2014-04-18 00:13:53 +02:00
|
|
|
}] = script
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
var client *rpcClient
|
|
|
|
|
2014-04-18 00:13:53 +02:00
|
|
|
// Now we go and look for any inputs that we were not provided by
|
|
|
|
// querying btcd with getrawtransaction. We queue up a bunch of async
|
|
|
|
// requests and will wait for replies after we have checked the rest of
|
|
|
|
// the arguments.
|
|
|
|
requested := make(map[btcwire.ShaHash]*pendingTx)
|
|
|
|
for _, txIn := range msgTx.TxIn {
|
|
|
|
// Did we get this txin from the arguments?
|
|
|
|
if _, ok := inputs[txIn.PreviousOutpoint]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we already fetching this tx? If so mark us as interested
|
|
|
|
// in this outpoint. (N.B. that any *sane* tx will only
|
|
|
|
// reference each outpoint once, since anything else is a double
|
|
|
|
// spend. We don't check this ourselves to save having to scan
|
|
|
|
// the array, it will fail later if so).
|
|
|
|
if ptx, ok := requested[txIn.PreviousOutpoint.Hash]; ok {
|
|
|
|
ptx.inputs = append(ptx.inputs,
|
|
|
|
txIn.PreviousOutpoint.Index)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never heard of this one before, request it.
|
2014-06-12 18:39:26 +02:00
|
|
|
if client == nil {
|
|
|
|
client, err = accessClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevHash := &txIn.PreviousOutpoint.Hash
|
2014-04-18 00:13:53 +02:00
|
|
|
requested[txIn.PreviousOutpoint.Hash] = &pendingTx{
|
2014-06-12 18:39:26 +02:00
|
|
|
resp: client.GetRawTransactionAsync(prevHash),
|
2014-04-18 00:13:53 +02:00
|
|
|
inputs: []uint32{txIn.PreviousOutpoint.Index},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse list of private keys, if present. If there are any keys here
|
|
|
|
// they are the keys that we may use for signing. If empty we will
|
|
|
|
// use any keys known to us already.
|
2014-05-22 00:50:47 +02:00
|
|
|
var keys map[string]*btcutil.WIF
|
2014-04-18 00:13:53 +02:00
|
|
|
if len(cmd.PrivKeys) != 0 {
|
2014-05-22 00:50:47 +02:00
|
|
|
keys = make(map[string]*btcutil.WIF)
|
2014-04-18 00:13:53 +02:00
|
|
|
|
|
|
|
for _, key := range cmd.PrivKeys {
|
2014-05-22 00:50:47 +02:00
|
|
|
wif, err := btcutil.DecodeWIF(key)
|
2014-04-18 00:13:53 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
if !wif.IsForNet(activeNet.Params) {
|
2014-06-04 02:52:04 +02:00
|
|
|
s := "key network doesn't match wallet's"
|
|
|
|
return nil, DeserializationError{errors.New(s)}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 00:50:47 +02:00
|
|
|
addr, err := btcutil.NewAddressPubKey(wif.SerializePubKey(),
|
2014-05-27 19:50:51 +02:00
|
|
|
activeNet.Params)
|
2014-05-22 00:50:47 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, DeserializationError{err}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
2014-05-22 00:50:47 +02:00
|
|
|
keys[addr.EncodeAddress()] = wif
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hashType := btcscript.SigHashAll
|
|
|
|
if cmd.Flags != "" {
|
|
|
|
switch cmd.Flags {
|
|
|
|
case "ALL":
|
|
|
|
hashType = btcscript.SigHashAll
|
|
|
|
case "NONE":
|
|
|
|
hashType = btcscript.SigHashNone
|
|
|
|
case "SINGLE":
|
|
|
|
hashType = btcscript.SigHashSingle
|
|
|
|
case "ALL|ANYONECANPAY":
|
|
|
|
hashType = btcscript.SigHashAll |
|
|
|
|
btcscript.SigHashAnyOneCanPay
|
|
|
|
case "NONE|ANYONECANPAY":
|
|
|
|
hashType = btcscript.SigHashNone |
|
|
|
|
btcscript.SigHashAnyOneCanPay
|
|
|
|
case "SINGLE|ANYONECANPAY":
|
|
|
|
hashType = btcscript.SigHashSingle |
|
|
|
|
btcscript.SigHashAnyOneCanPay
|
|
|
|
default:
|
2014-06-04 02:52:04 +02:00
|
|
|
e := errors.New("Invalid sighash parameter")
|
|
|
|
return nil, InvalidParameterError{e}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have checked the rest of the args. now we can collect the async
|
|
|
|
// txs. TODO(oga) If we don't mind the possibility of wasting work we
|
|
|
|
// could move waiting to the following loop and be slightly more
|
|
|
|
// asynchronous.
|
|
|
|
for txid, ptx := range requested {
|
2014-06-12 18:39:26 +02:00
|
|
|
tx, err := ptx.resp.Receive()
|
2014-04-18 00:13:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, input := range ptx.inputs {
|
|
|
|
if input >= uint32(len(tx.MsgTx().TxOut)) {
|
2014-06-04 02:52:04 +02:00
|
|
|
e := fmt.Errorf("input %s:%d is not in tx",
|
|
|
|
txid.String(), input)
|
|
|
|
return nil, InvalidParameterError{e}
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inputs[btcwire.OutPoint{
|
|
|
|
Hash: txid,
|
|
|
|
Index: input,
|
|
|
|
}] = tx.MsgTx().TxOut[input].PkScript
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All args collected. Now we can sign all the inputs that we can.
|
|
|
|
// `complete' denotes that we successfully signed all outputs and that
|
|
|
|
// all scripts will run to completion. This is returned as part of the
|
|
|
|
// reply.
|
|
|
|
complete := true
|
|
|
|
for i, txIn := range msgTx.TxIn {
|
|
|
|
input, ok := inputs[txIn.PreviousOutpoint]
|
|
|
|
if !ok {
|
|
|
|
// failure to find previous is actually an error since
|
|
|
|
// we failed above if we don't have all the inputs.
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, fmt.Errorf("%s:%d not found",
|
|
|
|
txIn.PreviousOutpoint.Hash,
|
|
|
|
txIn.PreviousOutpoint.Index)
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up our callbacks that we pass to btcscript so it can
|
|
|
|
// look up the appropriate keys and scripts by address.
|
|
|
|
getKey := btcscript.KeyClosure(func(addr btcutil.Address) (
|
|
|
|
*ecdsa.PrivateKey, bool, error) {
|
|
|
|
if len(keys) != 0 {
|
2014-05-22 00:50:47 +02:00
|
|
|
wif, ok := keys[addr.EncodeAddress()]
|
2014-04-18 00:13:53 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false,
|
|
|
|
errors.New("no key for address")
|
|
|
|
}
|
2014-05-22 00:50:47 +02:00
|
|
|
return wif.PrivKey.ToECDSA(), wif.CompressPubKey, nil
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|
|
|
|
address, err := AcctMgr.Address(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pka, ok := address.(wallet.PubKeyAddress)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, errors.New("address is not " +
|
|
|
|
"a pubkey address")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := pka.PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, pka.Compressed(), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
getScript := btcscript.ScriptClosure(func(
|
|
|
|
addr btcutil.Address) ([]byte, error) {
|
|
|
|
// If keys were provided then we can only use the
|
|
|
|
// scripts provided with our inputs, too.
|
|
|
|
if len(keys) != 0 {
|
|
|
|
script, ok := scripts[addr.EncodeAddress()]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no script for " +
|
|
|
|
"address")
|
|
|
|
}
|
|
|
|
return script, nil
|
|
|
|
}
|
|
|
|
address, err := AcctMgr.Address(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sa, ok := address.(wallet.ScriptAddress)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, errors.New("address is not a script" +
|
2014-04-18 00:13:53 +02:00
|
|
|
" address")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(oga) we could possible speed things up further
|
|
|
|
// by returning the addresses, class and nrequired here
|
|
|
|
// thus avoiding recomputing them.
|
|
|
|
return sa.Script(), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// SigHashSingle inputs can only be signed if there's a
|
|
|
|
// corresponding output. However this could be already signed,
|
|
|
|
// so we always verify the output.
|
2014-05-28 00:46:21 +02:00
|
|
|
if (hashType&btcscript.SigHashSingle) !=
|
|
|
|
btcscript.SigHashSingle || i < len(msgTx.TxOut) {
|
2014-04-18 00:13:53 +02:00
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
script, err := btcscript.SignTxOutput(activeNet.Params,
|
2014-04-18 00:13:53 +02:00
|
|
|
msgTx, i, input, byte(hashType), getKey,
|
|
|
|
getScript, txIn.SignatureScript)
|
|
|
|
// Failure to sign isn't an error, it just means that
|
|
|
|
// the tx isn't complete.
|
|
|
|
if err != nil {
|
|
|
|
complete = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txIn.SignatureScript = script
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either it was already signed or we just signed it.
|
|
|
|
// Find out if it is completely satisfied or still needs more.
|
|
|
|
engine, err := btcscript.NewScript(txIn.SignatureScript, input,
|
|
|
|
i, msgTx, btcscript.ScriptBip16|
|
|
|
|
btcscript.ScriptCanonicalSignatures)
|
|
|
|
if err != nil || engine.Execute() != nil {
|
|
|
|
complete = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
buf.Grow(msgTx.SerializeSize())
|
|
|
|
|
2014-05-28 06:54:50 +02:00
|
|
|
// All returned errors (not OOM, which panics) encounted during
|
|
|
|
// bytes.Buffer writes are unexpected.
|
|
|
|
if err = msgTx.Serialize(buf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-04-18 00:13:53 +02:00
|
|
|
|
|
|
|
return btcjson.SignRawTransactionResult{
|
|
|
|
Hex: hex.EncodeToString(buf.Bytes()),
|
|
|
|
Complete: complete,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
// Stop handles the stop command by shutting down the process after the request
|
|
|
|
// is handled.
|
|
|
|
func Stop(icmd btcjson.Cmd) (interface{}, error) {
|
|
|
|
server.Stop()
|
|
|
|
return "btcwallet stopping.", nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 00:16:21 +01:00
|
|
|
// ValidateAddress handles the validateaddress command.
|
2014-06-04 02:52:04 +02:00
|
|
|
func ValidateAddress(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-14 00:16:21 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.ValidateAddressCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-14 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 20:21:20 +02:00
|
|
|
result := btcjson.ValidateAddressResult{}
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-02-14 00:16:21 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
// Use zero value (false) for IsValid.
|
2014-04-07 20:21:20 +02:00
|
|
|
return result, nil
|
2014-02-14 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
2014-04-07 20:21:20 +02:00
|
|
|
// We could put whether or not the address is a script here,
|
|
|
|
// by checking the type of "addr", however, the reference
|
|
|
|
// implementation only puts that information if the script is
|
|
|
|
// "ismine", and we follow that behaviour.
|
|
|
|
result.Address = addr.EncodeAddress()
|
|
|
|
result.IsValid = true
|
|
|
|
|
2014-04-18 00:00:52 +02:00
|
|
|
// We can't use AcctMgr.Address() here since we also need the account
|
|
|
|
// name.
|
|
|
|
if account, err := AcctMgr.AccountByAddress(addr); err == nil {
|
2014-05-28 06:54:50 +02:00
|
|
|
// The address must be handled by this account, so we expect
|
|
|
|
// this call to succeed without error.
|
|
|
|
ainfo, err := account.Address(addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-02-14 00:16:21 +01:00
|
|
|
|
2014-04-07 20:21:20 +02:00
|
|
|
result.IsMine = true
|
|
|
|
result.Account = account.name
|
2014-02-14 00:16:21 +01:00
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
if pka, ok := ainfo.(wallet.PubKeyAddress); ok {
|
|
|
|
result.IsCompressed = pka.Compressed()
|
|
|
|
result.PubKey = pka.ExportPubKey()
|
2014-03-13 20:13:39 +01:00
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
} else if sa, ok := ainfo.(wallet.ScriptAddress); ok {
|
2014-04-07 20:21:20 +02:00
|
|
|
result.IsScript = true
|
2014-04-09 02:18:52 +02:00
|
|
|
addresses := sa.Addresses()
|
|
|
|
addrStrings := make([]string, len(addresses))
|
|
|
|
for i, a := range addresses {
|
2014-03-13 20:13:39 +01:00
|
|
|
addrStrings[i] = a.EncodeAddress()
|
|
|
|
}
|
2014-04-07 20:21:20 +02:00
|
|
|
result.Addresses = addrStrings
|
2014-04-09 02:18:52 +02:00
|
|
|
result.Hex = hex.EncodeToString(sa.Script())
|
2014-03-13 20:13:39 +01:00
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
class := sa.ScriptClass()
|
2014-03-13 20:13:39 +01:00
|
|
|
// script type
|
2014-04-07 20:21:20 +02:00
|
|
|
result.Script = class.String()
|
2014-03-13 20:13:39 +01:00
|
|
|
if class == btcscript.MultiSigTy {
|
2014-04-09 02:18:52 +02:00
|
|
|
result.SigsRequired = sa.RequiredSigs()
|
2014-03-13 20:13:39 +01:00
|
|
|
}
|
2014-03-06 01:34:44 +01:00
|
|
|
}
|
2014-02-14 00:16:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-02-12 03:14:07 +01:00
|
|
|
// VerifyMessage handles the verifymessage command by verifying the provided
|
|
|
|
// compact signature for the given address and message.
|
2014-06-04 02:52:04 +02:00
|
|
|
func VerifyMessage(icmd btcjson.Cmd) (interface{}, error) {
|
2014-02-12 03:14:07 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.VerifyMessageCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
addr, err := btcutil.DecodeAddress(cmd.Address, activeNet.Params)
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, ParseError{err}
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 17:00:46 +02:00
|
|
|
// First check we know about the address and get the keys.
|
2014-04-18 00:00:52 +02:00
|
|
|
ainfo, err := AcctMgr.Address(addr)
|
2014-04-03 17:00:46 +02:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInvalidAddressOrKey
|
2014-04-03 17:00:46 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
pka := ainfo.(wallet.PubKeyAddress)
|
|
|
|
privkey, err := pka.PrivKey()
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode base64 signature
|
2014-02-13 19:42:22 +01:00
|
|
|
sig, err := base64.StdEncoding.DecodeString(cmd.Signature)
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the signature - this just shows that it was valid at all.
|
|
|
|
// we will compare it with the key next.
|
|
|
|
pk, wasCompressed, err := btcec.RecoverCompact(btcec.S256(), sig,
|
2014-02-13 19:42:22 +01:00
|
|
|
btcwire.DoubleSha256([]byte("Bitcoin Signed Message:\n"+
|
|
|
|
cmd.Message)))
|
2014-02-12 03:14:07 +01:00
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-02-12 03:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return boolean if keys match.
|
|
|
|
return (pk.X.Cmp(privkey.X) == 0 && pk.Y.Cmp(privkey.Y) == 0 &&
|
2014-03-06 01:34:44 +01:00
|
|
|
ainfo.Compressed() == wasCompressed), nil
|
2014-02-12 03:14:07 +01: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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func WalletIsLocked(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
2013-10-29 07:19:40 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := AcctMgr.Account(cmd.Account)
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotFound {
|
|
|
|
return nil, btcjson.ErrWalletInvalidAccountName
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
2013-11-12 18:01:32 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
return a.Wallet.IsLocked(), nil
|
2013-08-21 20:46:20 +02:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:48:12 +01:00
|
|
|
// WalletLock handles a walletlock request by locking the all account
|
|
|
|
// wallets, returning an error if any wallet is not encrypted (for example,
|
|
|
|
// a watching-only wallet).
|
2014-06-04 02:52:04 +02:00
|
|
|
func WalletLock(icmd btcjson.Cmd) (interface{}, error) {
|
|
|
|
err := AcctMgr.LockWallets()
|
|
|
|
return nil, err
|
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.
|
2014-06-04 02:52:04 +02:00
|
|
|
func WalletPassphrase(icmd btcjson.Cmd) (interface{}, 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-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
if err := AcctMgr.UnlockWallets(cmd.Passphrase); err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:48:12 +01:00
|
|
|
go func(timeout int64) {
|
|
|
|
time.Sleep(time.Second * time.Duration(timeout))
|
2014-03-20 17:21:52 +01:00
|
|
|
AcctMgr.Grab()
|
2014-05-28 06:54:50 +02:00
|
|
|
defer AcctMgr.Release()
|
|
|
|
err := AcctMgr.LockWallets()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Cannot lock account wallets: %v", err)
|
|
|
|
}
|
2014-01-27 21:48:12 +01:00
|
|
|
}(cmd.Timeout)
|
2013-12-02 20:56:06 +01:00
|
|
|
|
2014-01-27 21:48:12 +01:00
|
|
|
return nil, nil
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
2013-08-22 18:00:37 +02:00
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// WalletPassphraseChange responds to the walletpassphrasechange request
|
|
|
|
// by unlocking all accounts with the provided old passphrase, and
|
|
|
|
// re-encrypting each private key with an AES key derived from the new
|
|
|
|
// passphrase.
|
|
|
|
//
|
|
|
|
// If the old passphrase is correct and the passphrase is changed, all
|
|
|
|
// wallets will be immediately locked.
|
2014-06-04 02:52:04 +02:00
|
|
|
func WalletPassphraseChange(icmd btcjson.Cmd) (interface{}, error) {
|
2014-01-27 15:30:42 +01:00
|
|
|
cmd, ok := icmd.(*btcjson.WalletPassphraseChangeCmd)
|
|
|
|
if !ok {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, btcjson.ErrInternal
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
err := AcctMgr.ChangePassphrase([]byte(cmd.OldPassphrase),
|
2014-01-27 15:30:42 +01:00
|
|
|
[]byte(cmd.NewPassphrase))
|
2014-06-04 02:52:04 +02:00
|
|
|
if err == wallet.ErrWrongPassphrase {
|
|
|
|
return nil, btcjson.ErrWalletPassphraseIncorrect
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// AccountNtfn is a struct for marshalling any generic notification
|
2014-06-12 19:54:58 +02:00
|
|
|
// about a account for a websocket client.
|
2013-10-14 22:39:15 +02:00
|
|
|
//
|
2014-06-12 19:54:58 +02:00
|
|
|
// TODO(jrick): move to btcjson so it can be shared with clients?
|
2013-10-09 17:23:54 +02:00
|
|
|
type AccountNtfn struct {
|
|
|
|
Account string `json:"account"`
|
|
|
|
Notification interface{} `json:"notification"`
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// NotifyWalletLockStateChange sends a notification to all websocket clients
|
2013-08-22 18:00:37 +02:00
|
|
|
// that the wallet has just been locked or unlocked.
|
2014-06-12 18:39:26 +02:00
|
|
|
func (s *rpcServer) NotifyWalletLockStateChange(account string, locked bool) {
|
2013-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewWalletLockStateNtfn(account, locked)
|
2014-05-28 06:54:50 +02:00
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// If the marshal failed, it indicates that the btcws notification
|
|
|
|
// struct contains a field with a type that is not marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
s.broadcasts <- 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
|
2014-06-12 19:54:58 +02:00
|
|
|
// to all websocket clients.
|
2014-06-12 18:39:26 +02:00
|
|
|
func (s *rpcServer) NotifyWalletBalance(account string, balance float64) {
|
2013-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewAccountBalanceNtfn(account, balance, true)
|
2014-05-28 06:54:50 +02:00
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// If the marshal failed, it indicates that the btcws notification
|
|
|
|
// struct contains a field with a type that is not marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
s.broadcasts <- mntfn
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 23:34:36 +01:00
|
|
|
// NotifyWalletBalanceUnconfirmed sends a confirmed account balance
|
2014-06-12 19:54:58 +02:00
|
|
|
// notification to all websocket clients.
|
2014-06-12 18:39:26 +02:00
|
|
|
func (s *rpcServer) NotifyWalletBalanceUnconfirmed(account string, balance float64) {
|
2013-12-13 17:00:31 +01:00
|
|
|
ntfn := btcws.NewAccountBalanceNtfn(account, balance, false)
|
2014-05-28 06:54:50 +02:00
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// If the marshal failed, it indicates that the btcws notification
|
|
|
|
// struct contains a field with a type that is not marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
s.broadcasts <- mntfn
|
2013-10-09 17:23:54 +02:00
|
|
|
}
|
2013-12-02 23:34:36 +01:00
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// NotifyNewTxDetails sends details of a new transaction to all websocket
|
|
|
|
// clients.
|
2014-06-12 18:39:26 +02:00
|
|
|
func (s *rpcServer) NotifyNewTxDetails(account string, details btcjson.ListTransactionsResult) {
|
2014-04-09 05:04:10 +02:00
|
|
|
ntfn := btcws.NewTxNtfn(account, &details)
|
2014-05-28 06:54:50 +02:00
|
|
|
mntfn, err := ntfn.MarshalJSON()
|
|
|
|
// If the marshal failed, it indicates that the btcws notification
|
|
|
|
// struct contains a field with a type that is not marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
s.broadcasts <- mntfn
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|