2013-08-21 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-09-04 01:05:59 +02:00
|
|
|
"encoding/json"
|
2013-09-03 16:21:14 +02:00
|
|
|
"errors"
|
2013-08-21 16:37:30 +02: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"
|
2013-09-03 06:10:32 +02: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-08-21 16:37:30 +02:00
|
|
|
"github.com/conformal/seelog"
|
|
|
|
"os"
|
2013-09-03 06:10:32 +02:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2013-08-21 16:37:30 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-09-03 16:21:14 +02:00
|
|
|
var (
|
|
|
|
ErrNoWallet = errors.New("Wallet file does not exist.")
|
|
|
|
)
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
var (
|
|
|
|
log seelog.LoggerInterface = seelog.Default
|
|
|
|
cfg *config
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets = struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[string]*BtcWallet
|
|
|
|
}{
|
|
|
|
m: make(map[string]*BtcWallet),
|
|
|
|
}
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
tcfg, _, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cfg = tcfg
|
|
|
|
|
|
|
|
// Open wallet
|
2013-09-03 23:16:07 +02:00
|
|
|
w, err := OpenWallet(cfg, "")
|
2013-08-21 16:37:30 +02:00
|
|
|
if err != nil {
|
2013-09-03 16:21:14 +02:00
|
|
|
log.Info(err.Error())
|
|
|
|
} else {
|
2013-09-03 23:16:07 +02:00
|
|
|
w.Track()
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start HTTP server to listen and send messages to frontend and btcd
|
|
|
|
// backend. Try reconnection if connection failed.
|
|
|
|
for {
|
|
|
|
if err := ListenAndServe(); err == ConnRefused {
|
|
|
|
// wait and try again.
|
|
|
|
log.Info("Unable to connect to btcd. Retrying in 5 seconds.")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
} else if err != nil {
|
|
|
|
log.Info(err.Error())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
|
|
|
|
type BtcWallet struct {
|
|
|
|
*wallet.Wallet
|
2013-09-04 01:05:59 +02:00
|
|
|
mtx sync.RWMutex
|
|
|
|
dirty bool
|
|
|
|
UtxoStore struct {
|
|
|
|
sync.RWMutex
|
|
|
|
dirty bool
|
|
|
|
s tx.UtxoStore
|
|
|
|
}
|
|
|
|
TxStore struct {
|
|
|
|
sync.RWMutex
|
|
|
|
dirty bool
|
|
|
|
s tx.TxStore
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
// walletdir returns the directory path which holds the wallet, utxo,
|
|
|
|
// and tx files.
|
|
|
|
func walletdir(cfg *config, account string) string {
|
2013-09-03 06:10:32 +02:00
|
|
|
var wname string
|
|
|
|
if account == "" {
|
|
|
|
wname = "btcwallet"
|
|
|
|
} else {
|
|
|
|
wname = fmt.Sprintf("btcwallet-%s", account)
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
return filepath.Join(cfg.DataDir, wname)
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:21:14 +02:00
|
|
|
func OpenWallet(cfg *config, account string) (*BtcWallet, error) {
|
2013-09-03 15:49:16 +02:00
|
|
|
wdir := walletdir(cfg, account)
|
2013-09-03 06:10:32 +02:00
|
|
|
fi, err := os.Stat(wdir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Attempt data directory creation
|
|
|
|
if err = os.MkdirAll(wdir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, fmt.Errorf("Data directory '%s' is not a directory.", cfg.DataDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wfilepath := filepath.Join(wdir, "wallet.bin")
|
|
|
|
txfilepath := filepath.Join(wdir, "tx.bin")
|
|
|
|
utxofilepath := filepath.Join(wdir, "utxo.bin")
|
|
|
|
var wfile, txfile, utxofile *os.File
|
|
|
|
if wfile, err = os.Open(wfilepath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-09-03 16:21:14 +02:00
|
|
|
// Must create and save wallet first.
|
|
|
|
return nil, ErrNoWallet
|
2013-09-03 06:10:32 +02:00
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 23:05:22 +02:00
|
|
|
defer wfile.Close()
|
2013-09-03 06:10:32 +02:00
|
|
|
if txfile, err = os.Open(txfilepath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if txfile, err = os.Create(txfilepath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 23:05:22 +02:00
|
|
|
defer txfile.Close()
|
2013-09-03 06:10:32 +02:00
|
|
|
if utxofile, err = os.Open(utxofilepath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if utxofile, err = os.Create(utxofilepath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 23:05:22 +02:00
|
|
|
defer utxofile.Close()
|
2013-09-03 06:10:32 +02:00
|
|
|
|
|
|
|
wlt := new(wallet.Wallet)
|
|
|
|
if _, err = wlt.ReadFrom(wfile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var txs tx.TxStore
|
|
|
|
if _, err = txs.ReadFrom(txfile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var utxos tx.UtxoStore
|
|
|
|
if _, err = utxos.ReadFrom(utxofile); err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &BtcWallet{
|
2013-09-04 01:05:59 +02:00
|
|
|
Wallet: wlt,
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
w.UtxoStore.s = utxos
|
|
|
|
w.TxStore.s = txs
|
2013-09-03 06:10:32 +02:00
|
|
|
|
2013-09-03 23:16:07 +02:00
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *BtcWallet) Track() {
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.Lock()
|
2013-09-03 23:16:07 +02:00
|
|
|
name := w.Name()
|
|
|
|
if wallets.m[name] == nil {
|
|
|
|
wallets.m[name] = w
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
wallets.Unlock()
|
2013-09-04 01:05:59 +02:00
|
|
|
|
|
|
|
for _, addr := range w.GetActiveAddresses() {
|
2013-09-04 19:41:33 +02:00
|
|
|
go w.ReqNewTxsForAddress(addr)
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *BtcWallet) RescanForAddress(addr string, blocks ...int) {
|
|
|
|
seq.Lock()
|
|
|
|
n := seq.n
|
|
|
|
seq.n++
|
|
|
|
seq.Unlock()
|
|
|
|
|
|
|
|
params := []interface{}{addr}
|
|
|
|
if len(blocks) > 0 {
|
|
|
|
params = append(params, blocks[0])
|
|
|
|
}
|
|
|
|
if len(blocks) > 1 {
|
|
|
|
params = append(params, blocks[1])
|
|
|
|
}
|
|
|
|
m := &btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Id: fmt.Sprintf("btcwallet(%v)", n),
|
|
|
|
Method: "rescan",
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
msg, _ := json.Marshal(m)
|
|
|
|
|
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = func(result interface{}) bool {
|
|
|
|
// TODO(jrick)
|
|
|
|
|
|
|
|
// btcd returns a nil result when the rescan is complete.
|
|
|
|
// Returning true signals that this handler is finished
|
|
|
|
// and can be removed.
|
|
|
|
return result == nil
|
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
|
|
|
|
btcdMsgs <- msg
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:41:33 +02:00
|
|
|
func (w *BtcWallet) ReqNewTxsForAddress(addr string) {
|
2013-09-04 01:05:59 +02:00
|
|
|
seq.Lock()
|
|
|
|
n := seq.n
|
|
|
|
seq.n++
|
|
|
|
seq.Unlock()
|
|
|
|
|
|
|
|
m := &btcjson.Message{
|
|
|
|
Jsonrpc: "1.0",
|
|
|
|
Id: fmt.Sprintf("btcwallet(%d)", n),
|
2013-09-04 20:14:21 +02:00
|
|
|
Method: "notifynewtxs",
|
2013-09-04 01:05:59 +02:00
|
|
|
Params: []interface{}{addr},
|
|
|
|
}
|
|
|
|
msg, _ := json.Marshal(m)
|
|
|
|
|
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = func(result interface{}) bool {
|
2013-09-04 19:41:33 +02:00
|
|
|
// TODO(jrick): btcd also sends the block hash in the reply.
|
|
|
|
// Do we want it saved as well?
|
2013-09-04 15:54:06 +02:00
|
|
|
v, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
2013-09-04 19:41:33 +02:00
|
|
|
log.Error("Tx Handler: Unexpected result type.")
|
2013-09-04 15:54:06 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-09-04 19:41:33 +02:00
|
|
|
sender58, ok := v["sender"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified sender.")
|
2013-09-04 15:54:06 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-09-04 19:41:33 +02:00
|
|
|
receiver58, ok := v["receiver"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified receiver.")
|
2013-09-04 15:54:06 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-09-04 19:41:33 +02:00
|
|
|
height, ok := v["height"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified height.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
txhashBE, ok := v["txhash"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified transaction hash.")
|
|
|
|
return false
|
|
|
|
}
|
2013-09-04 22:36:48 +02:00
|
|
|
index, ok := v["index"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified transaction index.")
|
|
|
|
return false
|
|
|
|
}
|
2013-09-04 19:41:33 +02:00
|
|
|
amt, ok := v["amount"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified amount.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
spent, ok := v["spent"].(bool)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified spent field.")
|
|
|
|
return false
|
2013-09-04 15:54:06 +02:00
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-04 19:41:33 +02:00
|
|
|
// btcd sends the tx hashe as a BE string. Convert to a
|
|
|
|
// LE ShaHash.
|
|
|
|
txhash, err := btcwire.NewShaHashFromStr(txhashBE)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Tx Handler: Tx hash string cannot be parsed: " + err.Error())
|
|
|
|
return false
|
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-04 19:41:33 +02:00
|
|
|
sender := btcutil.Base58Decode(sender58)
|
|
|
|
receiver := btcutil.Base58Decode(receiver58)
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-04 19:41:33 +02:00
|
|
|
go func() {
|
|
|
|
t := &tx.RecvTx{
|
|
|
|
Amt: int64(amt),
|
|
|
|
}
|
|
|
|
copy(t.TxHash[:], txhash[:])
|
|
|
|
copy(t.SenderAddr[:], sender)
|
|
|
|
copy(t.ReceiverAddr[:], receiver)
|
|
|
|
|
|
|
|
w.TxStore.Lock()
|
|
|
|
txs := w.TxStore.s
|
|
|
|
w.TxStore.s = append(txs, t)
|
|
|
|
w.TxStore.dirty = true
|
|
|
|
w.TxStore.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// Do not add output to utxo store if spent.
|
|
|
|
if spent {
|
|
|
|
return
|
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-04 19:41:33 +02:00
|
|
|
u := &tx.Utxo{
|
|
|
|
Amt: int64(amt),
|
|
|
|
Height: int64(height),
|
|
|
|
}
|
2013-09-04 22:21:03 +02:00
|
|
|
copy(u.Out.Hash[:], txhash[:])
|
2013-09-04 22:36:48 +02:00
|
|
|
u.Out.Index = uint32(index)
|
2013-09-04 19:41:33 +02:00
|
|
|
copy(u.Addr[:], receiver)
|
|
|
|
|
|
|
|
w.UtxoStore.Lock()
|
|
|
|
// All newly saved utxos are first classified as unconfirmed.
|
|
|
|
utxos := w.UtxoStore.s.Unconfirmed
|
|
|
|
w.UtxoStore.s.Unconfirmed = append(utxos, u)
|
|
|
|
w.UtxoStore.dirty = true
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
}()
|
2013-09-04 01:05:59 +02:00
|
|
|
|
|
|
|
// Never remove this handler.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
|
|
|
|
btcdMsgs <- msg
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-04 15:54:06 +02:00
|
|
|
|
|
|
|
// Marshalling and unmarshalling byte arrays or slices results in ugly
|
|
|
|
// []interface{} slices where each interface{} is a float64. This
|
|
|
|
// function unmangles this to return a byte slice.
|
|
|
|
func UnmangleJsonByteSlice(mangled []interface{}) (unmangled []byte) {
|
|
|
|
unmangled = make([]byte, len(mangled))
|
|
|
|
for i, _ := range mangled[:] {
|
|
|
|
unmangled[i] = byte(mangled[i].(float64))
|
|
|
|
}
|
|
|
|
return unmangled
|
|
|
|
}
|