2016-01-15 06:56:25 +03:00
|
|
|
package uspv
|
|
|
|
|
|
|
|
import (
|
2016-02-02 06:03:01 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2016-01-15 06:56:25 +03:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-02-05 12:16:45 +03:00
|
|
|
"sync"
|
2016-01-15 06:56:25 +03:00
|
|
|
|
2016-01-21 08:08:05 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
|
2016-01-20 07:02:18 +03:00
|
|
|
"github.com/boltdb/bolt"
|
2016-01-15 06:56:25 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcutil/bloom"
|
2016-01-21 08:08:05 +03:00
|
|
|
"github.com/btcsuite/btcutil/hdkeychain"
|
2016-01-15 06:56:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type TxStore struct {
|
2016-01-20 10:40:04 +03:00
|
|
|
OKTxids map[wire.ShaHash]int32 // known good txids and their heights
|
2016-02-05 12:16:45 +03:00
|
|
|
OKMutex sync.Mutex
|
2016-01-19 12:33:58 +03:00
|
|
|
|
2016-01-20 07:02:18 +03:00
|
|
|
Adrs []MyAdr // endeavouring to acquire capital
|
2016-02-19 11:32:22 +03:00
|
|
|
WitAdrs []MyAdr // separate segwit address slice
|
2016-01-20 07:02:18 +03:00
|
|
|
StateDB *bolt.DB // place to write all this down
|
2016-02-03 10:37:29 +03:00
|
|
|
|
|
|
|
// Params live here, not SCon
|
2016-01-21 12:04:45 +03:00
|
|
|
Param *chaincfg.Params // network parameters (testnet3, testnetL)
|
2016-01-21 08:08:05 +03:00
|
|
|
|
|
|
|
// From here, comes everything. It's a secret to everybody.
|
|
|
|
rootPrivKey *hdkeychain.ExtendedKey
|
2016-01-15 06:56:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Utxo struct { // cash money.
|
2016-01-27 12:24:16 +03:00
|
|
|
Op wire.OutPoint // where
|
|
|
|
|
2016-01-21 09:57:05 +03:00
|
|
|
// all the info needed to spend
|
|
|
|
AtHeight int32 // block height where this tx was confirmed, 0 for unconf
|
|
|
|
KeyIdx uint32 // index for private key needed to sign / spend
|
|
|
|
Value int64 // higher is better
|
|
|
|
|
2016-02-19 11:32:22 +03:00
|
|
|
// IsCoinbase bool // can't spend for a while
|
|
|
|
IsWit bool // true if p2wpkh output
|
2016-01-27 12:24:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stxo is a utxo that has moved on.
|
|
|
|
type Stxo struct {
|
|
|
|
Utxo // when it used to be a utxo
|
|
|
|
SpendHeight int32 // height at which it met its demise
|
|
|
|
SpendTxid wire.ShaHash // the tx that consumed it
|
2016-01-15 06:56:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type MyAdr struct { // an address I have the private key for
|
2016-01-21 12:04:45 +03:00
|
|
|
PkhAdr btcutil.Address
|
2016-01-15 06:56:25 +03:00
|
|
|
KeyIdx uint32 // index for private key needed to sign / spend
|
2016-01-21 12:04:45 +03:00
|
|
|
// ^^ this is kindof redundant because it'll just be their position
|
|
|
|
// inside the Adrs slice, right? leave for now
|
2016-01-15 06:56:25 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:37:29 +03:00
|
|
|
func NewTxStore(rootkey *hdkeychain.ExtendedKey, p *chaincfg.Params) TxStore {
|
2016-01-19 12:33:58 +03:00
|
|
|
var txs TxStore
|
2016-01-21 12:04:45 +03:00
|
|
|
txs.rootPrivKey = rootkey
|
2016-02-03 10:37:29 +03:00
|
|
|
txs.Param = p
|
2016-01-20 10:40:04 +03:00
|
|
|
txs.OKTxids = make(map[wire.ShaHash]int32)
|
2016-01-19 12:33:58 +03:00
|
|
|
return txs
|
|
|
|
}
|
|
|
|
|
2016-01-15 13:40:56 +03:00
|
|
|
// add txid of interest
|
2016-01-20 10:40:04 +03:00
|
|
|
func (t *TxStore) AddTxid(txid *wire.ShaHash, height int32) error {
|
2016-01-15 13:40:56 +03:00
|
|
|
if txid == nil {
|
|
|
|
return fmt.Errorf("tried to add nil txid")
|
|
|
|
}
|
2016-01-29 06:35:49 +03:00
|
|
|
log.Printf("added %s to OKTxids at height %d\n", txid.String(), height)
|
2016-02-05 12:16:45 +03:00
|
|
|
t.OKMutex.Lock()
|
2016-01-19 12:33:58 +03:00
|
|
|
t.OKTxids[*txid] = height
|
2016-02-05 12:16:45 +03:00
|
|
|
t.OKMutex.Unlock()
|
2016-01-15 13:40:56 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 10:08:37 +03:00
|
|
|
// ... or I'm gonna fade away
|
2016-01-15 06:56:25 +03:00
|
|
|
func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
|
2016-02-19 11:32:22 +03:00
|
|
|
if len(t.Adrs) == 0 && len(t.WitAdrs) == 9 {
|
|
|
|
return nil, fmt.Errorf("no address to filter for")
|
2016-01-15 06:56:25 +03:00
|
|
|
}
|
2016-02-05 12:16:45 +03:00
|
|
|
|
|
|
|
// get all utxos to add outpoints to filter
|
|
|
|
allUtxos, err := t.GetAllUtxos()
|
2016-01-27 12:24:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-19 11:32:22 +03:00
|
|
|
elem := uint32(len(t.Adrs) + len(t.WitAdrs) + len(allUtxos))
|
2016-01-29 06:35:49 +03:00
|
|
|
f := bloom.NewFilter(elem, 0, 0.000001, wire.BloomUpdateAll)
|
2016-02-19 11:32:22 +03:00
|
|
|
|
|
|
|
// note there could be false positives since we're just looking
|
|
|
|
// for the 20 byte PKH without the opcodes.
|
|
|
|
for _, a := range t.Adrs { // add 20-byte pubkeyhash
|
|
|
|
f.Add(a.PkhAdr.ScriptAddress())
|
|
|
|
}
|
|
|
|
for _, a := range t.WitAdrs { // add witness 20-byte pubkeyhash
|
2016-01-21 12:04:45 +03:00
|
|
|
f.Add(a.PkhAdr.ScriptAddress())
|
2016-01-15 06:56:25 +03:00
|
|
|
}
|
2016-01-31 12:05:31 +03:00
|
|
|
|
|
|
|
for _, u := range allUtxos {
|
2016-01-22 08:50:42 +03:00
|
|
|
f.AddOutPoint(&u.Op)
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:56:25 +03:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:59:40 +03:00
|
|
|
// GetDoubleSpends takes a transaction and compares it with
|
|
|
|
// all transactions in the db. It returns a slice of all txids in the db
|
|
|
|
// which are double spent by the received tx.
|
2016-02-07 06:15:35 +03:00
|
|
|
func CheckDoubleSpends(
|
2016-02-05 12:59:40 +03:00
|
|
|
argTx *wire.MsgTx, txs []*wire.MsgTx) ([]*wire.ShaHash, error) {
|
|
|
|
|
|
|
|
var dubs []*wire.ShaHash // slice of all double-spent txs
|
|
|
|
argTxid := argTx.TxSha()
|
|
|
|
|
|
|
|
for _, compTx := range txs {
|
|
|
|
compTxid := compTx.TxSha()
|
|
|
|
// check if entire tx is dup
|
|
|
|
if argTxid.IsEqual(&compTxid) {
|
|
|
|
return nil, fmt.Errorf("tx %s is dup", argTxid.String())
|
|
|
|
}
|
|
|
|
// not dup, iterate through inputs of argTx
|
|
|
|
for _, argIn := range argTx.TxIn {
|
|
|
|
// iterate through inputs of compTx
|
|
|
|
for _, compIn := range compTx.TxIn {
|
|
|
|
if OutPointsEqual(
|
|
|
|
argIn.PreviousOutPoint, compIn.PreviousOutPoint) {
|
|
|
|
// found double spend
|
|
|
|
dubs = append(dubs, &compTxid)
|
|
|
|
break // back to argIn loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dubs, nil
|
|
|
|
}
|
|
|
|
|
2016-01-19 22:59:01 +03:00
|
|
|
// TxToString prints out some info about a transaction. for testing / debugging
|
|
|
|
func TxToString(tx *wire.MsgTx) string {
|
2016-02-14 23:50:20 +03:00
|
|
|
str := fmt.Sprintf("\t size %d vsize %d wsize %d Tx %s\n",
|
|
|
|
tx.SerializeSize(), tx.VirtualSize(), tx.SerializeSizeWitness(),
|
|
|
|
tx.TxSha().String())
|
2016-01-19 22:59:01 +03:00
|
|
|
for i, in := range tx.TxIn {
|
|
|
|
str += fmt.Sprintf("Input %d: %s\n", i, in.PreviousOutPoint.String())
|
|
|
|
str += fmt.Sprintf("SigScript for input %d: %x\n", i, in.SignatureScript)
|
2016-02-16 09:13:17 +03:00
|
|
|
for j, wit := range in.Witness {
|
|
|
|
str += fmt.Sprintf("witness %d: %x\t", j, wit)
|
|
|
|
}
|
|
|
|
str += fmt.Sprintf("\n")
|
2016-01-19 22:59:01 +03:00
|
|
|
}
|
|
|
|
for i, out := range tx.TxOut {
|
|
|
|
if out != nil {
|
|
|
|
str += fmt.Sprintf("\toutput %d script: %x amt: %d\n",
|
|
|
|
i, out.PkScript, out.Value)
|
|
|
|
} else {
|
|
|
|
str += fmt.Sprintf("output %d nil (WARNING)\n", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2016-02-02 06:03:01 +03:00
|
|
|
|
|
|
|
// need this because before I was comparing pointers maybe?
|
|
|
|
// so they were the same outpoint but stored in 2 places so false negative?
|
|
|
|
func OutPointsEqual(a, b wire.OutPoint) bool {
|
|
|
|
if !a.Hash.IsEqual(&b.Hash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.Index == b.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----- serialization for tx outputs ------- */
|
|
|
|
|
|
|
|
// outPointToBytes turns an outpoint into 36 bytes.
|
|
|
|
func outPointToBytes(op *wire.OutPoint) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err := buf.Write(op.Hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 4 byte outpoint index within the tx to spend
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, op.Index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToBytes turns a Utxo into some bytes.
|
|
|
|
// note that the txid is the first 36 bytes and in our use cases will be stripped
|
|
|
|
// off, but is left here for other applications
|
|
|
|
func (u *Utxo) ToBytes() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
// write 32 byte txid of the utxo
|
|
|
|
_, err := buf.Write(u.Op.Hash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 4 byte outpoint index within the tx to spend
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, u.Op.Index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 4 byte height of utxo
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, u.AtHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 4 byte key index of utxo
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, u.KeyIdx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 8 byte amount of money at the utxo
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, u.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
// last byte indicates tx witness flags ( tx[5] from serialized tx)
|
|
|
|
// write a 1 at the end for p2wpkh (same as flags byte)
|
|
|
|
witByte := byte(0x00)
|
|
|
|
if u.IsWit {
|
|
|
|
witByte = 0x01
|
|
|
|
}
|
|
|
|
err = buf.WriteByte(witByte)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-02 06:03:01 +03:00
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UtxoFromBytes turns bytes into a Utxo. Note it wants the txid and outindex
|
|
|
|
// in the first 36 bytes, which isn't stored that way in the boldDB,
|
|
|
|
// but can be easily appended.
|
|
|
|
func UtxoFromBytes(b []byte) (Utxo, error) {
|
|
|
|
var u Utxo
|
|
|
|
if b == nil {
|
|
|
|
return u, fmt.Errorf("nil input slice")
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(b)
|
2016-02-19 11:32:22 +03:00
|
|
|
if buf.Len() < 53 { // utxos are 53 bytes
|
|
|
|
return u, fmt.Errorf("Got %d bytes for utxo, expect 53", buf.Len())
|
2016-02-02 06:03:01 +03:00
|
|
|
}
|
|
|
|
// read 32 byte txid
|
|
|
|
err := u.Op.Hash.SetBytes(buf.Next(32))
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
// read 4 byte outpoint index within the tx to spend
|
|
|
|
err = binary.Read(buf, binary.BigEndian, &u.Op.Index)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
// read 4 byte height of utxo
|
|
|
|
err = binary.Read(buf, binary.BigEndian, &u.AtHeight)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
// read 4 byte key index of utxo
|
|
|
|
err = binary.Read(buf, binary.BigEndian, &u.KeyIdx)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
// read 8 byte amount of money at the utxo
|
|
|
|
err = binary.Read(buf, binary.BigEndian, &u.Value)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
// read 1 byte witness flags
|
|
|
|
witByte, err := buf.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
if witByte != 0x00 {
|
|
|
|
u.IsWit = true
|
|
|
|
}
|
|
|
|
|
2016-02-02 06:03:01 +03:00
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToBytes turns an Stxo into some bytes.
|
2016-02-19 11:32:22 +03:00
|
|
|
// prevUtxo serialization, then spendheight [4], spendtxid [32]
|
2016-02-02 06:03:01 +03:00
|
|
|
func (s *Stxo) ToBytes() ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
2016-02-19 11:32:22 +03:00
|
|
|
// first serialize the utxo part
|
|
|
|
uBytes, err := s.Utxo.ToBytes()
|
2016-02-02 06:03:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
// write that into the buffer first
|
|
|
|
_, err = buf.Write(uBytes)
|
2016-02-02 06:03:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
|
2016-02-02 06:03:01 +03:00
|
|
|
// write 4 byte height where the txo was spent
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, s.SpendHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write 32 byte txid of the spending transaction
|
|
|
|
_, err = buf.Write(s.SpendTxid.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StxoFromBytes turns bytes into a Stxo.
|
2016-02-19 11:32:22 +03:00
|
|
|
// first take the first 53 bytes as a utxo, then the next 36 for how it's spent.
|
2016-02-02 06:03:01 +03:00
|
|
|
func StxoFromBytes(b []byte) (Stxo, error) {
|
|
|
|
var s Stxo
|
2016-02-19 11:32:22 +03:00
|
|
|
if len(b) < 89 {
|
|
|
|
return s, fmt.Errorf("Got %d bytes for stxo, expect 89", len(b))
|
2016-02-02 06:03:01 +03:00
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
|
|
|
|
u, err := UtxoFromBytes(b[:53])
|
2016-02-02 06:03:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
s.Utxo = u // assign the utxo
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(b[53:]) // make buffer for spend data
|
|
|
|
|
2016-02-02 06:03:01 +03:00
|
|
|
// read 4 byte spend height
|
|
|
|
err = binary.Read(buf, binary.BigEndian, &s.SpendHeight)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
// read 32 byte txid
|
|
|
|
err = s.SpendTxid.SetBytes(buf.Next(32))
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|