lbcwallet/createtx_test.go
Josh Rickmar 63686347c6 Create transactions using saved utxo data.
This is a big change that also many general fixes to problems found
when creating transactions.  In particular the Utxo and Tx formats and
serialization functions were updated with additional information that
would be necessary for rolling back old utxo and tx data data after
btcd chain switches.  This change also implements the json methods
'sendfrom' and 'sendmany' to create a new transaction based on a
frontend request.

Transactions are currently not sent to btcd since the tx relay code is
not finished yet, so a temporary error is returned back to frontends
who try to send new transactions.
2013-10-01 14:26:27 -04:00

70 lines
1.7 KiB
Go

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.
w, err := wallet.NewWallet("banana wallet", "", []byte("banana"))
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
}
copy(utxo.Addr[:], addr160)
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.
curHeight.h = 12346
// Create the transaction.
pairs := map[string]uint64{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH": 5000,
}
rawtx, err := btcw.txToPairs(pairs, 100, 0)
if err != nil {
t.Errorf("Tx creation failed: %s", err)
return
}
_ = rawtx
}