2013-11-14 18:15:16 +01:00
|
|
|
/*
|
2014-01-03 19:34:37 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-11-14 18:15:16 +01: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 (
|
|
|
|
"bytes"
|
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
|
|
|
"encoding/base64"
|
2013-11-14 18:15:16 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
|
|
|
"github.com/conformal/btcwire"
|
2013-12-02 20:56:06 +01:00
|
|
|
"path/filepath"
|
2013-11-14 18:15:16 +01:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2013-12-10 22:15:25 +01:00
|
|
|
// addressAccountMap holds a map of addresses to names of the
|
|
|
|
// accounts that hold each address.
|
2014-01-30 16:14:02 +01:00
|
|
|
//
|
|
|
|
// TODO: move this to AccountManager
|
2013-12-10 22:15:25 +01:00
|
|
|
var addressAccountMap = struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string]string
|
|
|
|
}{
|
|
|
|
m: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkAddressForAccount marks an address as belonging to an account.
|
|
|
|
func MarkAddressForAccount(address, account string) {
|
|
|
|
addressAccountMap.Lock()
|
|
|
|
addressAccountMap.m[address] = account
|
|
|
|
addressAccountMap.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupAccountByAddress returns the account name for address. error
|
|
|
|
// will be set to ErrNotFound if the address has not been marked as
|
|
|
|
// associated with any account.
|
|
|
|
func LookupAccountByAddress(address string) (string, error) {
|
|
|
|
addressAccountMap.RLock()
|
|
|
|
defer addressAccountMap.RUnlock()
|
|
|
|
account, ok := addressAccountMap.m[address]
|
|
|
|
if !ok {
|
|
|
|
return "", ErrNotFound
|
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2013-11-14 18:15:16 +01:00
|
|
|
// Account is a structure containing all the components for a
|
|
|
|
// complete wallet. It contains the Armory-style wallet (to store
|
2014-01-30 16:14:02 +01:00
|
|
|
// addresses and keys), and tx and utxo stores, and a mutex to prevent
|
|
|
|
// incorrect multiple access.
|
2013-11-14 18:15:16 +01:00
|
|
|
type Account struct {
|
2014-01-03 19:34:37 +01:00
|
|
|
name string
|
|
|
|
fullRescan bool
|
2014-01-30 16:14:02 +01:00
|
|
|
*wallet.Wallet
|
|
|
|
tx.UtxoStore
|
|
|
|
tx.TxStore
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Lock locks the underlying wallet for an account.
|
|
|
|
func (a *Account) Lock() error {
|
2014-01-27 21:48:12 +01:00
|
|
|
switch err := a.Wallet.Lock(); err {
|
|
|
|
case nil:
|
2013-12-13 17:00:31 +01:00
|
|
|
NotifyWalletLockStateChange(a.Name(), true)
|
2014-01-27 21:48:12 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
case wallet.ErrWalletLocked:
|
|
|
|
// Do not pass wallet already locked errors to the caller.
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return err
|
2013-12-13 17:00:31 +01:00
|
|
|
}
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Unlock unlocks the underlying wallet for an account.
|
2014-01-27 15:30:42 +01:00
|
|
|
func (a *Account) Unlock(passphrase []byte) error {
|
2014-01-27 21:48:12 +01:00
|
|
|
if err := a.Wallet.Unlock(passphrase); err != nil {
|
|
|
|
return err
|
2013-12-13 17:00:31 +01:00
|
|
|
}
|
2014-01-27 21:48:12 +01:00
|
|
|
|
|
|
|
NotifyWalletLockStateChange(a.Name(), false)
|
|
|
|
return nil
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-31 19:11:47 +01:00
|
|
|
// AddressUsed returns whether there are any recorded transactions spending to
|
|
|
|
// a given address. Assumming correct TxStore usage, this will return true iff
|
|
|
|
// there are any transactions with outputs to this address in the blockchain or
|
|
|
|
// the btcd mempool.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) AddressUsed(addr btcutil.Address) bool {
|
2013-12-31 19:11:47 +01:00
|
|
|
// This can be optimized by recording this data as it is read when
|
|
|
|
// opening an account, and keeping it up to date each time a new
|
|
|
|
// received tx arrives.
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
pkHash := addr.ScriptAddress()
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
for i := range a.TxStore {
|
|
|
|
rtx, ok := a.TxStore[i].(*tx.RecvTx)
|
2013-12-31 19:11:47 +01:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(rtx.ReceiverHash, pkHash) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-11-14 18:15:16 +01:00
|
|
|
// CalculateBalance sums the amounts of all unspent transaction
|
|
|
|
// outputs to addresses of a wallet and returns the balance as a
|
|
|
|
// float64.
|
|
|
|
//
|
|
|
|
// If confirmations is 0, all UTXOs, even those not present in a
|
|
|
|
// block (height -1), will be used to get the balance. Otherwise,
|
|
|
|
// a UTXO must be in a block. If confirmations is 1 or greater,
|
|
|
|
// the balance will be calculated based on how many how many blocks
|
|
|
|
// include a UTXO.
|
2013-11-15 17:44:24 +01:00
|
|
|
func (a *Account) CalculateBalance(confirms int) float64 {
|
2013-11-14 18:15:16 +01:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
|
|
|
|
return 0.
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
var bal uint64 // Measured in satoshi
|
|
|
|
for _, u := range a.UtxoStore {
|
2013-11-14 18:15:16 +01:00
|
|
|
// Utxos not yet in blocks (height -1) should only be
|
|
|
|
// added if confirmations is 0.
|
2014-02-03 19:00:28 +01:00
|
|
|
if confirmed(confirms, u.Height, bs.Height) {
|
2013-11-14 18:15:16 +01:00
|
|
|
bal += u.Amt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(bal) / float64(btcutil.SatoshiPerBitcoin)
|
|
|
|
}
|
|
|
|
|
2013-12-10 22:15:25 +01:00
|
|
|
// CalculateAddressBalance sums the amounts of all unspent transaction
|
|
|
|
// outputs to a single address's pubkey hash and returns the balance
|
|
|
|
// as a float64.
|
|
|
|
//
|
|
|
|
// If confirmations is 0, all UTXOs, even those not present in a
|
|
|
|
// block (height -1), will be used to get the balance. Otherwise,
|
|
|
|
// a UTXO must be in a block. If confirmations is 1 or greater,
|
|
|
|
// the balance will be calculated based on how many how many blocks
|
|
|
|
// include a UTXO.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) CalculateAddressBalance(addr *btcutil.AddressPubKeyHash, confirms int) float64 {
|
2013-12-10 22:15:25 +01:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
|
|
|
|
return 0.
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
var bal uint64 // Measured in satoshi
|
|
|
|
for _, u := range a.UtxoStore {
|
2013-12-10 22:15:25 +01:00
|
|
|
// Utxos not yet in blocks (height -1) should only be
|
|
|
|
// added if confirmations is 0.
|
2014-02-03 19:00:28 +01:00
|
|
|
if confirmed(confirms, u.Height, bs.Height) {
|
2014-01-06 18:24:29 +01:00
|
|
|
if bytes.Equal(addr.ScriptAddress(), u.AddrHash[:]) {
|
2013-12-10 22:15:25 +01:00
|
|
|
bal += u.Amt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return float64(bal) / float64(btcutil.SatoshiPerBitcoin)
|
|
|
|
}
|
|
|
|
|
2013-12-31 19:11:47 +01:00
|
|
|
// CurrentAddress gets the most recently requested Bitcoin payment address
|
|
|
|
// from an account. If the address has already been used (there is at least
|
|
|
|
// one transaction spending to it in the blockchain or btcd mempool), the next
|
|
|
|
// chained address is returned.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) CurrentAddress() (btcutil.Address, error) {
|
|
|
|
addr := a.Wallet.LastChainedAddress()
|
2013-12-31 19:11:47 +01:00
|
|
|
|
|
|
|
// Get next chained address if the last one has already been used.
|
2014-01-06 18:24:29 +01:00
|
|
|
if a.AddressUsed(addr) {
|
|
|
|
return a.NewAddress()
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
return addr, nil
|
2013-12-31 19:11:47 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 18:53:32 +01:00
|
|
|
// ListSinceBlock returns a slice of maps with details about transactions since
|
|
|
|
// the given block. If the block is -1 then all transactions are included.
|
|
|
|
// transaction. This is intended to be used for listsinceblock RPC
|
|
|
|
// replies.
|
|
|
|
func (a *Account) ListSinceBlock(since, curBlockHeight int32, minconf int) ([]map[string]interface{}, error) {
|
|
|
|
var txInfoList []map[string]interface{}
|
2014-01-30 16:14:02 +01:00
|
|
|
for _, tx := range a.TxStore {
|
2014-01-27 18:53:32 +01:00
|
|
|
// check block number.
|
|
|
|
if since != -1 && tx.Height() <= since {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txInfoList = append(txInfoList,
|
|
|
|
tx.TxInfo(a.name, curBlockHeight, a.Net())...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txInfoList, nil
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// ListTransactions returns a slice of maps with details about a recorded
|
|
|
|
// transaction. This is intended to be used for listtransactions RPC
|
|
|
|
// replies.
|
|
|
|
func (a *Account) ListTransactions(from, count int) ([]map[string]interface{}, error) {
|
|
|
|
// Get current block. The block height used for calculating
|
|
|
|
// the number of tx confirmations.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txInfoList []map[string]interface{}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
lastLookupIdx := len(a.TxStore) - count
|
2013-12-02 20:56:06 +01:00
|
|
|
// Search in reverse order: lookup most recently-added first.
|
2014-01-30 16:14:02 +01:00
|
|
|
for i := len(a.TxStore) - 1; i >= from && i >= lastLookupIdx; i-- {
|
2014-01-23 01:00:10 +01:00
|
|
|
txInfoList = append(txInfoList,
|
2014-01-30 16:14:02 +01:00
|
|
|
a.TxStore[i].TxInfo(a.name, bs.Height, a.Net())...)
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return txInfoList, nil
|
|
|
|
}
|
|
|
|
|
2013-12-30 17:10:06 +01:00
|
|
|
// ListAddressTransactions returns a slice of maps with details about a
|
|
|
|
// recorded transactions to or from any address belonging to a set. This is
|
|
|
|
// intended to be used for listaddresstransactions RPC replies.
|
|
|
|
func (a *Account) ListAddressTransactions(pkHashes map[string]struct{}) (
|
|
|
|
[]map[string]interface{}, error) {
|
|
|
|
|
|
|
|
// Get current block. The block height used for calculating
|
|
|
|
// the number of tx confirmations.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txInfoList []map[string]interface{}
|
2014-01-30 16:14:02 +01:00
|
|
|
for i := range a.TxStore {
|
|
|
|
rtx, ok := a.TxStore[i].(*tx.RecvTx)
|
2013-12-30 17:10:06 +01:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := pkHashes[string(rtx.ReceiverHash[:])]; ok {
|
|
|
|
info := rtx.TxInfo(a.name, bs.Height, a.Net())
|
2014-01-23 01:00:10 +01:00
|
|
|
txInfoList = append(txInfoList, info...)
|
2013-12-30 17:10:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return txInfoList, nil
|
|
|
|
}
|
|
|
|
|
2013-12-02 23:34:36 +01:00
|
|
|
// ListAllTransactions returns a slice of maps with details about a recorded
|
|
|
|
// transaction. This is intended to be used for listalltransactions RPC
|
|
|
|
// replies.
|
|
|
|
func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) {
|
|
|
|
// Get current block. The block height used for calculating
|
|
|
|
// the number of tx confirmations.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search in reverse order: lookup most recently-added first.
|
2014-01-30 16:14:02 +01:00
|
|
|
var txInfoList []map[string]interface{}
|
|
|
|
for i := len(a.TxStore) - 1; i >= 0; i-- {
|
2014-01-23 01:00:10 +01:00
|
|
|
txInfoList = append(txInfoList,
|
2014-01-30 16:14:02 +01:00
|
|
|
a.TxStore[i].TxInfo(a.name, bs.Height, a.Net())...)
|
2013-12-02 23:34:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return txInfoList, nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
// DumpPrivKeys returns the WIF-encoded private keys for all addresses with
|
|
|
|
// private keys in a wallet.
|
2013-11-20 02:18:11 +01:00
|
|
|
func (a *Account) DumpPrivKeys() ([]string, error) {
|
|
|
|
// Iterate over each active address, appending the private
|
|
|
|
// key to privkeys.
|
|
|
|
var privkeys []string
|
2014-01-28 20:43:55 +01:00
|
|
|
for addr, info := range a.Wallet.ActiveAddresses() {
|
|
|
|
key, err := a.Wallet.AddressKey(addr)
|
2013-11-20 02:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(),
|
2014-01-28 20:43:55 +01:00
|
|
|
a.Wallet.Net(), info.Compressed)
|
2013-11-20 02:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
privkeys = append(privkeys, encKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return privkeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpWIFPrivateKey returns the WIF encoded private key for a
|
|
|
|
// single wallet address.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Get private key from wallet if it exists.
|
2014-01-28 20:43:55 +01:00
|
|
|
key, err := a.Wallet.AddressKey(addr)
|
2013-11-20 02:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get address info. This is needed to determine whether
|
|
|
|
// the pubkey is compressed or not.
|
2014-01-28 20:43:55 +01:00
|
|
|
info, err := a.Wallet.AddressInfo(addr)
|
2013-11-20 02:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return WIF-encoding of the private key.
|
|
|
|
return btcutil.EncodePrivateKey(key.D.Bytes(), a.Net(), info.Compressed)
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// ImportPrivateKey imports a private key to the account's wallet and
|
|
|
|
// writes the new wallet to disk.
|
|
|
|
func (a *Account) ImportPrivateKey(pk []byte, compressed bool, bs *wallet.BlockStamp) (string, error) {
|
2013-11-20 02:18:11 +01:00
|
|
|
// Attempt to import private key into wallet.
|
2014-01-30 16:14:02 +01:00
|
|
|
addr, err := a.Wallet.ImportPrivateKey(pk, compressed, bs)
|
2013-11-20 02:18:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-01-20 18:56:27 +01:00
|
|
|
addrStr := addr.String()
|
2013-11-20 02:18:11 +01:00
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
// Immediately write wallet to disk.
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.ds.ScheduleWalletWrite(a)
|
|
|
|
if err := AcctMgr.ds.FlushAccount(a); err != nil {
|
2014-01-29 05:04:10 +01:00
|
|
|
return "", fmt.Errorf("cannot write account: %v", err)
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2013-12-10 22:15:25 +01:00
|
|
|
// Associate the imported address with this account.
|
2014-01-20 18:56:27 +01:00
|
|
|
MarkAddressForAccount(addrStr, a.Name())
|
2013-12-10 22:15:25 +01:00
|
|
|
|
2014-01-20 18:56:27 +01:00
|
|
|
log.Infof("Imported payment address %v", addrStr)
|
2013-11-20 02:18:11 +01:00
|
|
|
|
|
|
|
// Return the payment address string of the imported private key.
|
2014-01-20 18:56:27 +01:00
|
|
|
return addrStr, nil
|
2013-11-20 02:18:11 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// ExportToDirectory writes an account to a special export directory. Any
|
|
|
|
// previous files are overwritten.
|
|
|
|
func (a *Account) ExportToDirectory(dirBaseName string) error {
|
|
|
|
dir := filepath.Join(networkDir(cfg.Net()), dirBaseName)
|
|
|
|
if err := checkCreateDir(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return AcctMgr.ds.ExportAccount(a, dir)
|
|
|
|
}
|
|
|
|
|
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 returns a new account with a watching wallet
|
|
|
|
// exported by this a's wallet. Both wallets share the same tx and utxo
|
|
|
|
// stores, so locking one will lock the other as well. The returned account
|
|
|
|
// should be exported quickly, either to file or to an rpc caller, and then
|
|
|
|
// dropped from scope.
|
|
|
|
func (a *Account) ExportWatchingWallet() (*Account, error) {
|
|
|
|
ww, err := a.Wallet.ExportWatchingWallet()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wa := *a
|
|
|
|
wa.Wallet = ww
|
|
|
|
return &wa, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportBase64 exports an account's serialized wallet, tx, and utxo
|
|
|
|
// stores as base64-encoded values in a map.
|
|
|
|
func (a *Account) exportBase64() (map[string]string, error) {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
m := make(map[string]string)
|
|
|
|
|
2014-01-28 20:43:55 +01:00
|
|
|
_, err := a.Wallet.WriteTo(buf)
|
|
|
|
if err != nil {
|
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
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
if _, err = a.TxStore.WriteTo(buf); err != nil {
|
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
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
_, err = a.UtxoStore.WriteTo(buf)
|
2014-01-28 20:43:55 +01:00
|
|
|
if err != nil {
|
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
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["utxo"] = base64.StdEncoding.EncodeToString(buf.Bytes())
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2013-11-14 18:15:16 +01:00
|
|
|
// Track requests btcd to send notifications of new transactions for
|
2014-01-03 19:34:37 +01:00
|
|
|
// each address stored in a wallet.
|
2013-11-15 17:44:24 +01:00
|
|
|
func (a *Account) Track() {
|
2014-01-03 19:34:37 +01:00
|
|
|
// Request notifications for transactions sending to all wallet
|
|
|
|
// addresses.
|
|
|
|
addrs := a.ActiveAddresses()
|
|
|
|
addrstrs := make([]string, len(addrs))
|
|
|
|
i := 0
|
|
|
|
for addr := range addrs {
|
|
|
|
addrstrs[i] = addr.EncodeAddress()
|
|
|
|
i++
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
err := NotifyNewTXs(CurrentServerConn(), addrstrs)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to request transaction updates for address.")
|
|
|
|
}
|
2013-11-14 18:15:16 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
for _, utxo := range a.UtxoStore {
|
2014-01-03 19:34:37 +01:00
|
|
|
ReqSpentUtxoNtfn(utxo)
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 02:46:16 +01:00
|
|
|
// RescanActiveAddresses requests btcd to rescan the blockchain for new
|
2013-11-20 02:18:11 +01:00
|
|
|
// transactions to all active wallet addresses. This is needed for
|
|
|
|
// catching btcwallet up to a long-running btcd process, as otherwise
|
2013-11-14 18:15:16 +01:00
|
|
|
// it would have missed notifications as blocks are attached to the
|
|
|
|
// main chain.
|
2013-11-20 02:18:11 +01:00
|
|
|
func (a *Account) RescanActiveAddresses() {
|
|
|
|
// Determine the block to begin the rescan from.
|
2013-11-14 18:15:16 +01:00
|
|
|
beginBlock := int32(0)
|
2013-11-15 17:44:24 +01:00
|
|
|
if a.fullRescan {
|
2013-11-14 18:15:16 +01:00
|
|
|
// Need to perform a complete rescan since the wallet creation
|
|
|
|
// block.
|
2013-11-20 02:18:11 +01:00
|
|
|
beginBlock = a.EarliestBlockHeight()
|
2013-11-14 18:15:16 +01:00
|
|
|
log.Debugf("Rescanning account '%v' for new transactions since block height %v",
|
2013-11-15 17:44:24 +01:00
|
|
|
a.name, beginBlock)
|
2013-11-14 18:15:16 +01:00
|
|
|
} else {
|
|
|
|
// The last synced block height should be used the starting
|
|
|
|
// point for block rescanning. Grab the block stamp here.
|
2013-11-15 17:44:24 +01:00
|
|
|
bs := a.SyncedWith()
|
2013-11-14 18:15:16 +01:00
|
|
|
|
2013-12-06 21:37:07 +01:00
|
|
|
log.Debugf("Rescanning account '%v' for new transactions after block height %v hash %v",
|
2013-11-15 17:44:24 +01:00
|
|
|
a.name, bs.Height, bs.Hash)
|
2013-11-14 18:15:16 +01:00
|
|
|
|
|
|
|
// If we're synced with block x, must scan the blocks x+1 to best block.
|
|
|
|
beginBlock = bs.Height + 1
|
|
|
|
}
|
|
|
|
|
2013-11-20 02:18:11 +01:00
|
|
|
// Rescan active addresses starting at the determined block height.
|
2014-01-30 16:14:02 +01:00
|
|
|
Rescan(CurrentServerConn(), beginBlock, a.ActivePaymentAddresses())
|
|
|
|
AcctMgr.ds.FlushAccount(a)
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SortedActivePaymentAddresses returns a slice of all active payment
|
|
|
|
// addresses in an account.
|
2013-11-15 17:44:24 +01:00
|
|
|
func (a *Account) SortedActivePaymentAddresses() []string {
|
2014-01-28 20:43:55 +01:00
|
|
|
infos := a.Wallet.SortedActiveAddresses()
|
2013-11-14 18:15:16 +01:00
|
|
|
|
|
|
|
addrs := make([]string, len(infos))
|
2014-01-06 18:24:29 +01:00
|
|
|
for i, info := range infos {
|
|
|
|
addrs[i] = info.Address.EncodeAddress()
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivePaymentAddresses returns a set of all active pubkey hashes
|
|
|
|
// in an account.
|
2013-11-15 17:44:24 +01:00
|
|
|
func (a *Account) ActivePaymentAddresses() map[string]struct{} {
|
2013-12-02 20:56:06 +01:00
|
|
|
infos := a.ActiveAddresses()
|
2013-11-14 18:15:16 +01:00
|
|
|
|
2014-01-28 20:43:55 +01:00
|
|
|
addrs := make(map[string]struct{}, len(infos))
|
2013-11-14 18:15:16 +01:00
|
|
|
for _, info := range infos {
|
2014-01-06 18:24:29 +01:00
|
|
|
addrs[info.Address.EncodeAddress()] = struct{}{}
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// NewAddress returns a new payment address for an account.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) NewAddress() (btcutil.Address, error) {
|
2013-12-02 20:56:06 +01:00
|
|
|
// Get current block's height and hash.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
2014-01-06 18:24:29 +01:00
|
|
|
return nil, err
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get next address from wallet.
|
2014-01-28 20:43:55 +01:00
|
|
|
addr, err := a.Wallet.NextChainedAddress(&bs, cfg.KeypoolSize)
|
2013-12-02 20:56:06 +01:00
|
|
|
if err != nil {
|
2014-01-06 18:24:29 +01:00
|
|
|
return nil, err
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 18:24:29 +01:00
|
|
|
// Immediately write updated wallet to disk.
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.ds.ScheduleWalletWrite(a)
|
|
|
|
if err := AcctMgr.ds.FlushAccount(a); err != nil {
|
2014-01-29 05:04:10 +01:00
|
|
|
return nil, fmt.Errorf("account write failed: %v", err)
|
2013-12-02 20:56:06 +01:00
|
|
|
}
|
|
|
|
|
2013-12-10 22:15:25 +01:00
|
|
|
// Mark this new address as belonging to this account.
|
2014-01-06 18:24:29 +01:00
|
|
|
MarkAddressForAccount(addr.EncodeAddress(), a.Name())
|
2013-12-10 22:15:25 +01:00
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
// Request updates from btcd for new transactions sent to this address.
|
|
|
|
a.ReqNewTxsForAddress(addr)
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2014-02-03 16:52:02 +01:00
|
|
|
// NewChangeAddress returns a new change address for an account.
|
|
|
|
func (a *Account) NewChangeAddress() (btcutil.Address, error) {
|
|
|
|
// Get current block's height and hash.
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get next chained change address from wallet.
|
|
|
|
addr, err := a.Wallet.ChangeAddress(&bs, cfg.KeypoolSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Immediately write updated wallet to disk.
|
|
|
|
AcctMgr.ds.ScheduleWalletWrite(a)
|
|
|
|
if err := AcctMgr.ds.FlushAccount(a); err != nil {
|
|
|
|
return nil, fmt.Errorf("account write failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark this new address as belonging to this account.
|
|
|
|
MarkAddressForAccount(addr.EncodeAddress(), a.Name())
|
|
|
|
|
|
|
|
// Request updates from btcd for new transactions sent to this address.
|
|
|
|
a.ReqNewTxsForAddress(addr)
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
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 recovers the next n chained addresses of a wallet.
|
|
|
|
func (a *Account) RecoverAddresses(n int) error {
|
|
|
|
// Get info on the last chained address. The rescan starts at the
|
|
|
|
// earliest block height the last chained address might appear at.
|
|
|
|
last := a.Wallet.LastChainedAddress()
|
|
|
|
lastInfo, err := a.Wallet.AddressInfo(last)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := a.Wallet.ExtendActiveAddresses(n, cfg.KeypoolSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a goroutine to rescan blockchain for recovered addresses.
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
for i := range addrs {
|
|
|
|
m[addrs[i].EncodeAddress()] = struct{}{}
|
|
|
|
}
|
|
|
|
go func(addrs map[string]struct{}) {
|
2014-01-30 16:14:02 +01:00
|
|
|
jsonErr := Rescan(CurrentServerConn(), lastInfo.FirstBlock, addrs)
|
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 jsonErr != nil {
|
|
|
|
log.Errorf("Rescanning for recovered addresses failed: %v",
|
|
|
|
jsonErr.Message)
|
|
|
|
}
|
|
|
|
}(m)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-14 18:15:16 +01:00
|
|
|
// ReqNewTxsForAddress sends a message to btcd to request tx updates
|
|
|
|
// for addr for each new block that is added to the blockchain.
|
2014-01-06 18:24:29 +01:00
|
|
|
func (a *Account) ReqNewTxsForAddress(addr btcutil.Address) {
|
|
|
|
// Only support P2PKH addresses currently.
|
|
|
|
apkh, ok := addr.(*btcutil.AddressPubKeyHash)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Requesting notifications of TXs sending to address %v", apkh)
|
2013-11-14 18:15:16 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
err := NotifyNewTXs(CurrentServerConn(), []string{apkh.EncodeAddress()})
|
2013-11-14 18:15:16 +01:00
|
|
|
if err != nil {
|
2014-01-03 19:34:37 +01:00
|
|
|
log.Error("Unable to request transaction updates for address.")
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReqSpentUtxoNtfn sends a message to btcd to request updates for when
|
|
|
|
// a stored UTXO has been spent.
|
2014-01-03 19:34:37 +01:00
|
|
|
func ReqSpentUtxoNtfn(u *tx.Utxo) {
|
2013-11-14 18:15:16 +01:00
|
|
|
log.Debugf("Requesting spent UTXO notifications for Outpoint hash %s index %d",
|
|
|
|
u.Out.Hash, u.Out.Index)
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
NotifySpent(CurrentServerConn(), (*btcwire.OutPoint)(&u.Out))
|
2013-11-14 18:15:16 +01:00
|
|
|
}
|
2014-02-03 19:00:28 +01:00
|
|
|
|
|
|
|
// TotalReceived iterates through an account's transaction history, returning the
|
|
|
|
// total amount of bitcoins received for any account address. Amounts received
|
|
|
|
// through multisig transactions are ignored.
|
|
|
|
func (a *Account) TotalReceived(confirms int) (float64, error) {
|
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalSatoshis int64
|
|
|
|
for _, e := range a.TxStore {
|
|
|
|
recvtx, ok := e.(*tx.RecvTx)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore change.
|
|
|
|
addr, err := btcutil.NewAddressPubKeyHash(recvtx.ReceiverHash, cfg.Net())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info, err := a.Wallet.AddressInfo(addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if info.Change {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tally if the appropiate number of block confirmations have passed.
|
|
|
|
if confirmed(confirms, recvtx.Height(), bs.Height) {
|
|
|
|
totalSatoshis += recvtx.Amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return float64(totalSatoshis) / float64(btcutil.SatoshiPerBitcoin), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// confirmed checks whether a transaction at height txHeight has met
|
|
|
|
// minconf confirmations for a blockchain at height curHeight.
|
|
|
|
func confirmed(minconf int, txHeight, curHeight int32) bool {
|
|
|
|
if minconf == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if txHeight != -1 && int(curHeight-txHeight+1) >= minconf {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|