forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utxos.go
115 lines (108 loc) · 2.89 KB
/
utxos.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 UtxoDB struct {
modelStore
coinType wallet.CoinType
}
func NewUnspentTransactionStore(db *sql.DB, lock *sync.Mutex, coinType wallet.CoinType) repo.UnspentTransactionOutputStore {
return &UtxoDB{modelStore{db, lock}, coinType}
}
func (u *UtxoDB) Put(utxo wallet.Utxo) error {
u.lock.Lock()
defer u.lock.Unlock()
tx, _ := u.db.Begin()
stmt, err := tx.Prepare("insert or replace into utxos(coin, outpoint, value, height, scriptPubKey, watchOnly) values(?,?,?,?,?,?)")
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
watchOnlyInt := 0
if utxo.WatchOnly {
watchOnlyInt = 1
}
outpoint := utxo.Op.Hash.String() + ":" + strconv.Itoa(int(utxo.Op.Index))
_, err = stmt.Exec(u.coinType.CurrencyCode(), outpoint, int(utxo.Value), int(utxo.AtHeight), hex.EncodeToString(utxo.ScriptPubkey), watchOnlyInt)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (u *UtxoDB) GetAll() ([]wallet.Utxo, error) {
u.lock.Lock()
defer u.lock.Unlock()
var ret []wallet.Utxo
stm := "select outpoint, value, height, scriptPubKey, watchOnly from utxos where coin=?"
rows, err := u.db.Query(stm, u.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 watchOnlyInt int
if err := rows.Scan(&outpoint, &value, &height, &scriptPubKey, &watchOnlyInt); 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
}
watchOnly := false
if watchOnlyInt == 1 {
watchOnly = true
}
ret = append(ret, wallet.Utxo{
Op: *wire.NewOutPoint(shaHash, uint32(index)),
AtHeight: int32(height),
Value: int64(value),
ScriptPubkey: scriptBytes,
WatchOnly: watchOnly,
})
}
return ret, nil
}
func (u *UtxoDB) SetWatchOnly(utxo wallet.Utxo) error {
u.lock.Lock()
defer u.lock.Unlock()
outpoint := utxo.Op.Hash.String() + ":" + strconv.Itoa(int(utxo.Op.Index))
_, err := u.db.Exec("update utxos set watchOnly=? where outpoint=? and coin=?", 1, outpoint, u.coinType.CurrencyCode())
if err != nil {
return err
}
return nil
}
func (u *UtxoDB) Delete(utxo wallet.Utxo) error {
u.lock.Lock()
defer u.lock.Unlock()
outpoint := utxo.Op.Hash.String() + ":" + strconv.Itoa(int(utxo.Op.Index))
_, err := u.db.Exec("delete from utxos where outpoint=? and coin=?", outpoint, u.coinType.CurrencyCode())
if err != nil {
return err
}
return nil
}