2013-08-21 16:37:30 +02:00
|
|
|
/*
|
2014-01-03 19:34:37 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-08-21 16:37:30 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-03 16:21:14 +02:00
|
|
|
"errors"
|
2014-01-30 16:14:02 +01:00
|
|
|
"fmt"
|
2013-09-04 01:05:59 +02:00
|
|
|
"github.com/conformal/btcjson"
|
2013-09-04 19:41:33 +02:00
|
|
|
"github.com/conformal/btcutil"
|
2014-01-30 16:14:02 +01:00
|
|
|
"github.com/conformal/btcwallet/tx"
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2013-09-04 15:54:06 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-11-19 18:21:54 +01:00
|
|
|
"io/ioutil"
|
2013-11-21 17:27:26 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2013-08-21 16:37:30 +02:00
|
|
|
"os"
|
2014-01-23 23:12:55 +01:00
|
|
|
"strings"
|
2013-09-03 06:10:32 +02:00
|
|
|
"sync"
|
2013-08-21 16:37:30 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-09-03 16:21:14 +02:00
|
|
|
var (
|
2013-09-09 20:14:57 +02:00
|
|
|
// ErrNoWallet describes an error where a wallet does not exist and
|
|
|
|
// must be created first.
|
2014-01-23 23:12:55 +01:00
|
|
|
ErrNoWallet = &WalletOpenError{
|
|
|
|
Err: "wallet file does not exist",
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
// ErrNoUtxos describes an error where the wallet file was successfully
|
|
|
|
// read, but the UTXO file was not. To properly handle this error,
|
|
|
|
// a rescan should be done since the wallet creation block.
|
|
|
|
ErrNoUtxos = errors.New("utxo file cannot be read")
|
|
|
|
|
|
|
|
// ErrNoTxs describes an error where the wallet and UTXO files were
|
|
|
|
// successfully read, but the TX history file was not. It is up to
|
|
|
|
// the caller whether this necessitates a rescan or not.
|
|
|
|
ErrNoTxs = errors.New("tx file cannot be read")
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
cfg *config
|
2013-09-03 16:21:14 +02:00
|
|
|
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
curBlock = struct {
|
2013-09-05 21:31:39 +02:00
|
|
|
sync.RWMutex
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
wallet.BlockStamp
|
2013-09-05 21:31:39 +02:00
|
|
|
}{
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
BlockStamp: wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
},
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
// GetCurBlock returns the blockchain height and SHA hash of the most
|
|
|
|
// recently seen block. If no blocks have been seen since btcd has
|
|
|
|
// connected, btcd is queried for the current block height and hash.
|
|
|
|
func GetCurBlock() (bs wallet.BlockStamp, err error) {
|
|
|
|
curBlock.RLock()
|
|
|
|
bs = curBlock.BlockStamp
|
|
|
|
curBlock.RUnlock()
|
|
|
|
if bs.Height != int32(btcutil.BlockHeightUnknown) {
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
bb, _ := GetBestBlock(CurrentServerConn())
|
2014-01-03 19:34:37 +01:00
|
|
|
if bb == nil {
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
return wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
}, errors.New("current block unavailable")
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-05 21:31:39 +02:00
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
hash, err := btcwire.NewShaHashFromStr(bb.Hash)
|
2013-11-06 17:23:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
2014-01-03 19:34:37 +01:00
|
|
|
}, err
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
curBlock.Lock()
|
2014-01-03 19:34:37 +01:00
|
|
|
if bb.Height > curBlock.BlockStamp.Height {
|
2013-11-12 18:01:32 +01:00
|
|
|
bs = wallet.BlockStamp{
|
2014-01-03 19:34:37 +01:00
|
|
|
Height: bb.Height,
|
|
|
|
Hash: *hash,
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
curBlock.BlockStamp = bs
|
|
|
|
}
|
|
|
|
curBlock.Unlock()
|
|
|
|
return bs, nil
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 16:40:23 +02:00
|
|
|
// NewJSONID is used to receive the next unique JSON ID for btcd
|
2013-10-15 00:45:48 +02:00
|
|
|
// requests, starting from zero and incrementing by one after each
|
|
|
|
// read.
|
2013-10-15 16:40:23 +02:00
|
|
|
var NewJSONID = make(chan uint64)
|
2013-10-15 00:45:48 +02:00
|
|
|
|
2013-10-15 16:40:23 +02:00
|
|
|
// JSONIDGenerator sends incremental integers across a channel. This
|
2013-10-15 00:45:48 +02:00
|
|
|
// is meant to provide a unique value for the JSON ID field for btcd
|
|
|
|
// messages.
|
2013-10-15 16:40:23 +02:00
|
|
|
func JSONIDGenerator(c chan uint64) {
|
2013-10-15 00:45:48 +02:00
|
|
|
var n uint64
|
|
|
|
for {
|
|
|
|
c <- n
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
func main() {
|
2013-10-29 15:38:51 +01:00
|
|
|
// Initialize logging and setup deferred flushing to ensure all
|
|
|
|
// outstanding messages are written on shutdown
|
|
|
|
loggers := setLogLevel(defaultLogLevel)
|
|
|
|
defer func() {
|
|
|
|
for _, logger := range loggers {
|
|
|
|
logger.Flush()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
tcfg, _, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cfg = tcfg
|
|
|
|
|
2013-10-29 15:38:51 +01:00
|
|
|
// Change the logging level if needed.
|
|
|
|
if cfg.DebugLevel != defaultLogLevel {
|
|
|
|
loggers = setLogLevel(cfg.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2013-11-21 17:27:26 +01:00
|
|
|
if cfg.Profile != "" {
|
|
|
|
go func() {
|
|
|
|
listenAddr := net.JoinHostPort("", cfg.Profile)
|
|
|
|
log.Infof("Profile server listening on %s", listenAddr)
|
|
|
|
profileRedirect := http.RedirectHandler("/debug/pprof",
|
|
|
|
http.StatusSeeOther)
|
|
|
|
http.Handle("/", profileRedirect)
|
|
|
|
log.Errorf("%v", http.ListenAndServe(listenAddr, nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
Introduce new account file structure.
This changes the locations that account files (wallet.bin, utxo.bin,
and tx.bin) are searched for when opening or disk syncing accounts.
Previously, files were saved in the following layout:
~/.btcwallet/
- btcwallet/
- wallet.bin
- tx.bin
- utxo.bin
- btcwallet-AccountA/
- wallet.bin
- tx.bin
- utxo.bin
This format had two issues. First, each account would require its own
directory, causing a scalability issue on unix (and perhaps other)
platforms. Second, there was no distinction between testnet and
mainnet wallets, and if mainnet support was enabled, btcwallet would
attempt to open accounts with testnet wallets.
Instead, the following file structure is now used:
~/.btcwallet/
- testnet/
- wallet.bin
- tx.bin
- utxo.bin
- AccountA-wallet.bin
- AccountA-tx.bin
- AccountA-utxo.bin
This solves both of the previously-mentioned issues by requiring only
two subdirectories (one each for the testnet and mainnet bitcoin
networks), and by separating the locations to open and save testnet
and mainnet account files.
At startup, a check for the old account file structure is performed.
If found, files are moved to the new locations, and the old account
directories are removed. Account files are moved to the testnet
directory, as only testnet support is currently enabled.
The version has been bumped to 0.1.1 to reflect this change.
Fixes #16.
2013-12-05 02:16:50 +01:00
|
|
|
// Check and update any old file locations.
|
|
|
|
updateOldFileLocations()
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
go AcctMgr.Start()
|
|
|
|
|
2014-01-23 23:12:55 +01:00
|
|
|
// Open all account saved to disk.
|
|
|
|
OpenAccounts()
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-11-19 18:21:54 +01:00
|
|
|
// Read CA file to verify a btcd TLS connection.
|
|
|
|
cafile, err := ioutil.ReadFile(cfg.CAFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot open CA file: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
go func() {
|
2013-12-05 23:20:52 +01:00
|
|
|
s, err := newServer(cfg.SvrListeners)
|
2013-11-18 19:31:58 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to create HTTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Start HTTP server to listen and send messages to frontend and btcd
|
|
|
|
// backend. Try reconnection if connection failed.
|
2013-11-18 19:31:58 +01:00
|
|
|
s.Start()
|
2013-09-09 20:14:57 +02:00
|
|
|
}()
|
|
|
|
|
2013-10-15 00:45:48 +02:00
|
|
|
// Begin generating new IDs for JSON calls.
|
2013-10-15 16:40:23 +02:00
|
|
|
go JSONIDGenerator(NewJSONID)
|
2013-10-15 00:45:48 +02:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// Begin RPC server goroutines.
|
|
|
|
go RPCGateway()
|
|
|
|
go WalletRequestProcessor()
|
|
|
|
|
2013-12-17 19:18:09 +01:00
|
|
|
// Begin maintanence goroutines.
|
|
|
|
go SendBeforeReceiveHistorySync(SendTxHistSyncChans.add,
|
|
|
|
SendTxHistSyncChans.done,
|
|
|
|
SendTxHistSyncChans.remove,
|
|
|
|
SendTxHistSyncChans.access)
|
|
|
|
go StoreNotifiedMempoolRecvTxs(NotifiedRecvTxChans.add,
|
|
|
|
NotifiedRecvTxChans.remove,
|
|
|
|
NotifiedRecvTxChans.access)
|
|
|
|
go NotifyMinedTxSender(NotifyMinedTx)
|
|
|
|
go NotifyBalanceSyncer(NotifyBalanceSyncerChans.add,
|
|
|
|
NotifyBalanceSyncerChans.remove,
|
|
|
|
NotifyBalanceSyncerChans.access)
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
updateBtcd := make(chan *BtcdRPCConn)
|
|
|
|
go func() {
|
|
|
|
// Create an RPC connection and close the closed channel.
|
|
|
|
//
|
|
|
|
// It might be a better idea to create a new concrete type
|
|
|
|
// just for an always disconnected RPC connection and begin
|
|
|
|
// with that.
|
|
|
|
btcd := NewBtcdRPCConn(nil)
|
|
|
|
close(btcd.closed)
|
|
|
|
|
|
|
|
// Maintain the current btcd connection. After reconnects,
|
|
|
|
// the current connection should be updated.
|
2013-09-09 20:14:57 +02:00
|
|
|
for {
|
|
|
|
select {
|
2014-01-03 19:34:37 +01:00
|
|
|
case conn := <-updateBtcd:
|
|
|
|
btcd = conn
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
case access := <-accessServer:
|
|
|
|
access.server <- btcd
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
btcd, err := BtcdConnect(cafile)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Retrying btcd connection in 5 seconds")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
updateBtcd <- btcd
|
|
|
|
|
|
|
|
NotifyBtcdConnection(frontendNotificationMaster)
|
|
|
|
log.Info("Established connection to btcd")
|
|
|
|
|
|
|
|
// Perform handshake.
|
|
|
|
if err := Handshake(btcd); err != nil {
|
|
|
|
var message string
|
|
|
|
if jsonErr, ok := err.(*btcjson.Error); ok {
|
|
|
|
message = jsonErr.Message
|
|
|
|
} else {
|
|
|
|
message = err.Error()
|
|
|
|
}
|
|
|
|
log.Errorf("Cannot complete handshake: %v", message)
|
|
|
|
log.Info("Retrying btcd connection in 5 seconds")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block goroutine until the connection is lost.
|
|
|
|
<-btcd.closed
|
|
|
|
NotifyBtcdConnection(frontendNotificationMaster)
|
|
|
|
log.Info("Lost btcd connection")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// WalletOpenError is a special error type so problems opening wallet
|
|
|
|
// files can be differentiated (by a type assertion) from other errors.
|
|
|
|
type WalletOpenError struct {
|
|
|
|
Err string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error satisifies the builtin error interface.
|
|
|
|
func (e *WalletOpenError) Error() string {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenSavedAccount opens a named account from disk. If the wallet does not
|
|
|
|
// exist, ErrNoWallet is returned as an error.
|
|
|
|
func OpenSavedAccount(name string, cfg *config) (*Account, error) {
|
|
|
|
netdir := networkDir(cfg.Net())
|
|
|
|
if err := checkCreateDir(netdir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wlt := new(wallet.Wallet)
|
|
|
|
a := &Account{
|
|
|
|
Wallet: wlt,
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
wfilepath := accountFilename("wallet.bin", name, netdir)
|
|
|
|
utxofilepath := accountFilename("utxo.bin", name, netdir)
|
|
|
|
txfilepath := accountFilename("tx.bin", name, netdir)
|
|
|
|
var wfile, utxofile, txfile *os.File
|
|
|
|
|
|
|
|
// Read wallet file.
|
|
|
|
wfile, err := os.Open(wfilepath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Must create and save wallet first.
|
|
|
|
return nil, ErrNoWallet
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("cannot open wallet file: %s", err)
|
|
|
|
return nil, &WalletOpenError{msg}
|
|
|
|
}
|
|
|
|
defer wfile.Close()
|
|
|
|
|
|
|
|
if _, err = wlt.ReadFrom(wfile); err != nil {
|
|
|
|
msg := fmt.Sprintf("cannot read wallet: %s", err)
|
|
|
|
return nil, &WalletOpenError{msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read tx file. If this fails, return a ErrNoTxs error and let
|
|
|
|
// the caller decide if a rescan is necessary.
|
|
|
|
var finalErr error
|
|
|
|
if txfile, err = os.Open(txfilepath); err != nil {
|
|
|
|
log.Errorf("cannot open tx file: %s", err)
|
|
|
|
// This is not a error we should immediately return with,
|
|
|
|
// but other errors can be more important, so only return
|
|
|
|
// this if none of the others are hit.
|
|
|
|
finalErr = ErrNoTxs
|
|
|
|
} else {
|
|
|
|
defer txfile.Close()
|
|
|
|
var txs tx.TxStore
|
|
|
|
if _, err = txs.ReadFrom(txfile); err != nil {
|
|
|
|
log.Errorf("cannot read tx file: %s", err)
|
|
|
|
finalErr = ErrNoTxs
|
|
|
|
} else {
|
|
|
|
a.TxStore = txs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read utxo file. If this fails, return a ErrNoUtxos error so a
|
|
|
|
// rescan can be done since the wallet creation block.
|
|
|
|
var utxos tx.UtxoStore
|
|
|
|
utxofile, err = os.Open(utxofilepath)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot open utxo file: %s", err)
|
|
|
|
finalErr = ErrNoUtxos
|
|
|
|
a.fullRescan = true
|
|
|
|
} else {
|
|
|
|
defer utxofile.Close()
|
|
|
|
if _, err = utxos.ReadFrom(utxofile); err != nil {
|
|
|
|
log.Errorf("cannot read utxo file: %s", err)
|
|
|
|
finalErr = ErrNoUtxos
|
|
|
|
} else {
|
|
|
|
a.UtxoStore = utxos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all active payment addresses as belonging to this account.
|
|
|
|
for addr := range a.ActivePaymentAddresses() {
|
|
|
|
MarkAddressForAccount(addr, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, finalErr
|
|
|
|
}
|
|
|
|
|
2014-01-23 23:12:55 +01:00
|
|
|
// OpenAccounts attempts to open all saved accounts.
|
|
|
|
func OpenAccounts() {
|
2014-01-27 15:30:42 +01:00
|
|
|
// If the network (account) directory is missing, but the temporary
|
|
|
|
// directory exists, move it. This is unlikely to happen, but possible,
|
|
|
|
// if writing out every account file at once to a tmp directory (as is
|
|
|
|
// done for changing a wallet passphrase) and btcwallet closes after
|
|
|
|
// removing the network directory but before renaming the temporary
|
|
|
|
// directory.
|
|
|
|
netDir := networkDir(cfg.Net())
|
|
|
|
tmpNetDir := tmpNetworkDir(cfg.Net())
|
|
|
|
if !fileExists(netDir) && fileExists(tmpNetDir) {
|
|
|
|
if err := Rename(tmpNetDir, netDir); err != nil {
|
|
|
|
log.Errorf("Cannot move temporary network dir: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 23:12:55 +01:00
|
|
|
// The default account must exist, or btcwallet acts as if no
|
|
|
|
// wallets/accounts have been created yet.
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := OpenSavedAccount("", cfg)
|
|
|
|
if err != nil {
|
2014-01-23 23:12:55 +01:00
|
|
|
switch err.(type) {
|
|
|
|
case *WalletOpenError:
|
|
|
|
log.Errorf("Default account wallet file unreadable: %v", err)
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Warnf("Non-critical problem opening an account file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.AddAccount(a)
|
2014-01-23 23:12:55 +01:00
|
|
|
|
|
|
|
// Read all filenames in the account directory, and look for any
|
|
|
|
// filenames matching '*-wallet.bin'. These are wallets for
|
|
|
|
// additional saved accounts.
|
2014-01-27 15:30:42 +01:00
|
|
|
accountDir, err := os.Open(netDir)
|
2014-01-23 23:12:55 +01:00
|
|
|
if err != nil {
|
|
|
|
// Can't continue.
|
|
|
|
log.Errorf("Unable to open account directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer accountDir.Close()
|
|
|
|
fileNames, err := accountDir.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
// fileNames might be partially set, so log an error and
|
|
|
|
// at least try to open some accounts.
|
|
|
|
log.Errorf("Unable to read all account files: %v", err)
|
|
|
|
}
|
|
|
|
var accounts []string
|
|
|
|
for _, file := range fileNames {
|
|
|
|
if strings.HasSuffix(file, "-wallet.bin") {
|
|
|
|
name := strings.TrimSuffix(file, "-wallet.bin")
|
|
|
|
accounts = append(accounts, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open all additional accounts.
|
|
|
|
for _, a := range accounts {
|
|
|
|
// Log txstore/utxostore errors as these will be recovered
|
|
|
|
// from with a rescan, but wallet errors must be returned
|
|
|
|
// to the caller.
|
2014-01-30 16:14:02 +01:00
|
|
|
a, err := OpenSavedAccount(a, cfg)
|
|
|
|
if err != nil {
|
2014-01-23 23:12:55 +01:00
|
|
|
switch err.(type) {
|
|
|
|
case *WalletOpenError:
|
|
|
|
log.Errorf("Error opening account's wallet: %v", err)
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Warnf("Non-critical error opening an account file: %v", err)
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
} else {
|
|
|
|
AcctMgr.AddAccount(a)
|
2014-01-23 23:12:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
var accessServer = make(chan *AccessCurrentServerConn)
|
2014-01-03 19:34:37 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// AccessCurrentServerConn is used to access the current RPC connection
|
2014-01-03 19:34:37 +01:00
|
|
|
// from the goroutine managing btcd-side RPC connections.
|
2014-01-30 16:14:02 +01:00
|
|
|
type AccessCurrentServerConn struct {
|
|
|
|
server chan ServerConn
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// CurrentServerConn returns the most recently-connected btcd-side
|
2014-01-03 19:34:37 +01:00
|
|
|
// RPC connection.
|
2014-01-30 16:14:02 +01:00
|
|
|
func CurrentServerConn() ServerConn {
|
|
|
|
access := &AccessCurrentServerConn{
|
|
|
|
server: make(chan ServerConn),
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
accessServer <- access
|
|
|
|
return <-access.server
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|