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 wallet
|
|
|
|
|
|
|
|
import (
|
2013-09-03 06:10:32 +02:00
|
|
|
"crypto/rand"
|
2013-10-07 18:35:32 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"os"
|
2013-09-03 06:10:32 +02:00
|
|
|
"reflect"
|
2013-08-21 16:37:30 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-09-03 06:10:32 +02:00
|
|
|
var _ = spew.Dump
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
func TestBtcAddressSerializer(t *testing.T) {
|
2013-09-03 06:10:32 +02:00
|
|
|
kdfp := &kdfParameters{
|
|
|
|
mem: 1024,
|
|
|
|
nIter: 5,
|
|
|
|
}
|
2013-11-15 18:03:52 +01:00
|
|
|
if _, err := rand.Read(kdfp.salt[:]); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
key := Key([]byte("banana"), kdfp)
|
|
|
|
privKey := make([]byte, 32)
|
2013-11-15 18:03:52 +01:00
|
|
|
if _, err := rand.Read(privKey); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2013-11-22 17:40:24 +01:00
|
|
|
addr, err := newBtcAddress(privKey, nil, &BlockStamp{}, true)
|
2013-09-03 06:10:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = addr.encrypt(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create("btcaddress.bin")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err := addr.WriteTo(file); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Seek(0, 0)
|
|
|
|
|
|
|
|
var readAddr btcAddress
|
|
|
|
_, err = readAddr.ReadFrom(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-11 21:30:50 +01:00
|
|
|
if _, err = readAddr.unlock(key); err != nil {
|
2013-09-03 06:10:32 +02:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(addr, &readAddr) {
|
2013-08-21 16:37:30 +02:00
|
|
|
t.Error("Original and read btcAddress differ.")
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
|
|
|
|
func TestWalletCreationSerialization(t *testing.T) {
|
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
|
|
|
createdAt := &BlockStamp{}
|
|
|
|
w1, err := NewWallet("banana wallet", "A wallet for testing.",
|
|
|
|
[]byte("banana"), btcwire.MainNet, createdAt)
|
2013-09-03 06:10:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
2014-01-06 18:24:29 +01:00
|
|
|
return
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create("newwallet.bin")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err := w1.WriteTo(file); err != nil {
|
|
|
|
t.Error("Error writing new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Seek(0, 0)
|
|
|
|
|
|
|
|
w2 := new(Wallet)
|
|
|
|
_, err = w2.ReadFrom(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error reading newly written wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w1.Lock()
|
|
|
|
w2.Lock()
|
|
|
|
|
|
|
|
if err = w1.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Error("Decrypting original wallet failed: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w2.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Error("Decrypting newly read wallet failed: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(w1, w2) {
|
|
|
|
t.Error("Created and read-in wallets do not match.")
|
|
|
|
spew.Dump(w1, w2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|