2013-09-09 20:14:57 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/conformal/btcscript"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFakeTxs(t *testing.T) {
|
|
|
|
// First we need a wallet.
|
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
|
|
|
w, err := wallet.NewWallet("banana wallet", "", []byte("banana"),
|
|
|
|
btcwire.MainNet, &wallet.BlockStamp{})
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can not create encrypted wallet: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
btcw := &BtcWallet{
|
|
|
|
Wallet: w,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Unlock([]byte("banana"))
|
|
|
|
|
|
|
|
// Create and add a fake Utxo so we have some funds to spend.
|
|
|
|
//
|
|
|
|
// This will pass validation because btcscript is unaware of invalid
|
|
|
|
// tx inputs, however, this example would fail in btcd.
|
|
|
|
utxo := &tx.Utxo{}
|
|
|
|
addr, err := w.NextUnusedAddress()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get next address: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addr160, _, err := btcutil.DecodeAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot decode address: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2013-10-14 01:25:47 +02:00
|
|
|
copy(utxo.AddrHash[:], addr160)
|
2013-09-09 20:14:57 +02:00
|
|
|
ophash := (btcwire.ShaHash)([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
|
|
|
|
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
|
|
|
|
28, 29, 30, 31, 32})
|
|
|
|
out := btcwire.NewOutPoint(&ophash, 0)
|
|
|
|
utxo.Out = tx.OutPoint(*out)
|
|
|
|
ss, err := btcscript.PayToPubKeyHashScript(addr160)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not create utxo PkScript: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
utxo.Subscript = tx.PkScript(ss)
|
|
|
|
utxo.Amt = 10000
|
|
|
|
utxo.Height = 12345
|
|
|
|
btcw.UtxoStore.s = append(btcw.UtxoStore.s, utxo)
|
|
|
|
|
|
|
|
// Fake our current block height so btcd doesn't need to be queried.
|
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.BlockStamp.Height = 12346
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
// Create the transaction.
|
|
|
|
pairs := map[string]uint64{
|
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH": 5000,
|
|
|
|
}
|
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
|
|
|
_, err = btcw.txToPairs(pairs, 100, 0)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Tx creation failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|