forked from OpenBazaar/wallet-interface
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatastore.go
209 lines (155 loc) · 4.22 KB
/
datastore.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package wallet
import (
"bytes"
"github.com/phoreproject/btcd/btcec"
"github.com/phoreproject/btcd/chaincfg/chainhash"
"github.com/phoreproject/btcd/wire"
"time"
)
type Datastore interface {
Utxos() Utxos
Stxos() Stxos
Txns() Txns
Keys() Keys
WatchedScripts() WatchedScripts
}
type Utxos interface {
// Put a utxo to the database
Put(utxo Utxo) error
// Fetch all utxos from the db
GetAll() ([]Utxo, error)
// Make a utxo unspendable
SetWatchOnly(utxo Utxo) error
// Delete a utxo from the db
Delete(utxo Utxo) error
}
type Stxos interface {
// Put a stxo to the database
Put(stxo Stxo) error
// Fetch all stxos from the db
GetAll() ([]Stxo, error)
// Delete a stxo from the db
Delete(stxo Stxo) error
}
type Txns interface {
// Put a new transaction to the database
Put(txn *wire.MsgTx, value, height int, timestamp time.Time, watchOnly bool) error
// Fetch a raw tx and it's metadata given a hash
Get(txid chainhash.Hash) (*wire.MsgTx, Txn, error)
// Fetch all transactions from the db
GetAll(includeWatchOnly bool) ([]Txn, error)
// Update the height of a transaction
UpdateHeight(txid chainhash.Hash, height int) error
// Delete a transactions from the db
Delete(txid *chainhash.Hash) error
}
// Keys provides a database interface for the wallet to save key material, track
// used keys, and manage the look ahead window.
type Keys interface {
// Put a bip32 key to the database
Put(hash160 []byte, keyPath KeyPath) error
// Import a loose private key not part of the keychain
ImportKey(scriptAddress []byte, key *btcec.PrivateKey) error
// Mark the script as used
MarkKeyAsUsed(scriptAddress []byte) error
// Fetch the last index for the given key purpose
// The bool should state whether the key has been used or not
GetLastKeyIndex(purpose KeyPurpose) (int, bool, error)
// Returns the first unused path for the given purpose
GetPathForKey(scriptAddress []byte) (KeyPath, error)
// Returns an imported private key given a script address
GetKey(scriptAddress []byte) (*btcec.PrivateKey, error)
// Returns all imported keys
GetImported() ([]*btcec.PrivateKey, error)
// Get a list of unused key indexes for the given purpose
GetUnused(purpose KeyPurpose) ([]int, error)
// Fetch all key paths
GetAll() ([]KeyPath, error)
// Get the number of unused keys following the last used key
// for each key purpose.
GetLookaheadWindows() map[KeyPurpose]int
}
type WatchedScripts interface {
// Add a script to watch
Put(scriptPubKey []byte) error
// Return all watched scripts
GetAll() ([][]byte, error)
// Delete a watched script
Delete(scriptPubKey []byte) error
}
type Utxo struct {
// Previous txid and output index
Op wire.OutPoint
// Block height where this tx was confirmed, 0 for unconfirmed
AtHeight int32
// The higher the better
Value int64
// Output script
ScriptPubkey []byte
// If true this utxo will not be selected for spending. The primary
// purpose is track multisig UTXOs which must have separate handling
// to spend.
WatchOnly bool
}
func (utxo *Utxo) IsEqual(alt *Utxo) bool {
if alt == nil {
return utxo == nil
}
if !utxo.Op.Hash.IsEqual(&alt.Op.Hash) {
return false
}
if utxo.Op.Index != alt.Op.Index {
return false
}
if utxo.AtHeight != alt.AtHeight {
return false
}
if utxo.Value != alt.Value {
return false
}
if bytes.Compare(utxo.ScriptPubkey, alt.ScriptPubkey) != 0 {
return false
}
return true
}
type Stxo struct {
// When it used to be a UTXO
Utxo Utxo
// The height at which it met its demise
SpendHeight int32
// The tx that consumed it
SpendTxid chainhash.Hash
}
func (stxo *Stxo) IsEqual(alt *Stxo) bool {
if alt == nil {
return stxo == nil
}
if !stxo.Utxo.IsEqual(&alt.Utxo) {
return false
}
if stxo.SpendHeight != alt.SpendHeight {
return false
}
if !stxo.SpendTxid.IsEqual(&alt.SpendTxid) {
return false
}
return true
}
type Txn struct {
// Transaction ID
Txid string
// The value relevant to the wallet
Value int64
// The height at which it was mined
Height int32
// The time the transaction was first seen
Timestamp time.Time
// This transaction only involves a watch only address
WatchOnly bool
// Raw transaction bytes
Bytes []byte
}
type KeyPath struct {
Purpose KeyPurpose
Index int
}