forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stxo.go
115 lines (109 loc) · 2.95 KB
/
stxo.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package db
import (
"database/sql"
"encoding/hex"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"strconv"
"strings"
"sync"
)
type StxoDB struct {
modelStore
coinType wallet.CoinType
}
func NewSpentTransactionStore(db *sql.DB, lock *sync.Mutex, coinType wallet.CoinType) repo.SpentTransactionOutputStore {
return &StxoDB{modelStore{db, lock}, coinType}
}
func (s *StxoDB) Put(stxo wallet.Stxo) error {
s.lock.Lock()
defer s.lock.Unlock()
tx, _ := s.db.Begin()
stmt, err := tx.Prepare("insert or replace into stxos(coin, outpoint, value, height, scriptPubKey, watchOnly, spendHeight, spendTxid) values(?,?,?,?,?,?,?,?)")
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
watchOnly := 0
if stxo.Utxo.WatchOnly {
watchOnly = 1
}
outpoint := stxo.Utxo.Op.Hash.String() + ":" + strconv.Itoa(int(stxo.Utxo.Op.Index))
_, err = stmt.Exec(s.coinType.CurrencyCode(), outpoint, int(stxo.Utxo.Value), int(stxo.Utxo.AtHeight), hex.EncodeToString(stxo.Utxo.ScriptPubkey), watchOnly, int(stxo.SpendHeight), stxo.SpendTxid.String())
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (s *StxoDB) GetAll() ([]wallet.Stxo, error) {
s.lock.Lock()
defer s.lock.Unlock()
var ret []wallet.Stxo
stm := "select outpoint, value, height, scriptPubKey, watchOnly, spendHeight, spendTxid from stxos where coin=?"
rows, err := s.db.Query(stm, s.coinType.CurrencyCode())
if err != nil {
return ret, err
}
defer rows.Close()
for rows.Next() {
var outpoint string
var value int
var height int
var scriptPubKey string
var spendHeight int
var spendTxid string
var watchOnlyInt int
if err := rows.Scan(&outpoint, &value, &height, &scriptPubKey, &watchOnlyInt, &spendHeight, &spendTxid); err != nil {
continue
}
s := strings.Split(outpoint, ":")
shaHash, err := chainhash.NewHashFromStr(s[0])
if err != nil {
continue
}
index, err := strconv.Atoi(s[1])
if err != nil {
continue
}
scriptBytes, err := hex.DecodeString(scriptPubKey)
if err != nil {
continue
}
spentHash, err := chainhash.NewHashFromStr(spendTxid)
if err != nil {
continue
}
watchOnly := false
if watchOnlyInt > 0 {
watchOnly = true
}
utxo := wallet.Utxo{
Op: *wire.NewOutPoint(shaHash, uint32(index)),
AtHeight: int32(height),
Value: int64(value),
ScriptPubkey: scriptBytes,
WatchOnly: watchOnly,
}
ret = append(ret, wallet.Stxo{
Utxo: utxo,
SpendHeight: int32(spendHeight),
SpendTxid: *spentHash,
})
}
return ret, nil
}
func (s *StxoDB) Delete(stxo wallet.Stxo) error {
s.lock.Lock()
defer s.lock.Unlock()
outpoint := stxo.Utxo.Op.Hash.String() + ":" + strconv.Itoa(int(stxo.Utxo.Op.Index))
_, err := s.db.Exec("delete from stxos where outpoint=? and coin=?", outpoint, s.coinType.CurrencyCode())
if err != nil {
return err
}
return nil
}