forked from OpenBazaar/spvwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
181 lines (171 loc) · 4.51 KB
/
database.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
package db
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/scryinfo/wallet-interface"
"path"
"sync"
"time"
)
// This database is mostly just an example implementation used for testing.
// End users are free to user their own database.
type SQLiteDatastore struct {
keys wallet.Keys
utxos wallet.Utxos
stxos wallet.Stxos
txns wallet.Txns
watchedScripts wallet.WatchedScripts
scanBlocks wallet.ScanBlocks
noticeTxs wallet.NoticeTxs
db *sql.DB
lock *sync.RWMutex
}
func Create(repoPath string) (*SQLiteDatastore, error) {
dbPath := path.Join(repoPath, "wallet.db")
conn, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
l := new(sync.RWMutex)
sqliteDB := &SQLiteDatastore{
keys: &KeysDB{
db: conn,
lock: l,
},
utxos: &UtxoDB{
db: conn,
lock: l,
},
stxos: &StxoDB{
db: conn,
lock: l,
},
txns: &TxnsDB{
db: conn,
lock: l,
},
watchedScripts: &WatchedScriptsDB{
db: conn,
lock: l,
},
scanBlocks: &ScanBlocksDB{
db: conn,
lock: l,
},
noticeTxs: &NoticeTxsDB{
db: conn,
lock: l,
},
db: conn,
lock: l,
}
initDatabaseTables(conn)
return sqliteDB, nil
}
func (db *SQLiteDatastore) Keys() wallet.Keys {
return db.keys
}
func (db *SQLiteDatastore) Utxos() wallet.Utxos {
return db.utxos
}
func (db *SQLiteDatastore) Stxos() wallet.Stxos {
return db.stxos
}
func (db *SQLiteDatastore) Txns() wallet.Txns {
return db.txns
}
func (db *SQLiteDatastore) WatchedScripts() wallet.WatchedScripts {
return db.watchedScripts
}
func (db *SQLiteDatastore) ScanBlocks() wallet.ScanBlocks {
return db.scanBlocks
}
func (db *SQLiteDatastore) NoticeTxs() wallet.NoticeTxs {
return db.noticeTxs
}
func initDatabaseTables(db *sql.DB) error {
var sqlStmt string
sqlStmt = sqlStmt + `
create table if not exists keys (scriptAddress text primary key not null, purpose integer, keyIndex integer, used integer, key text);
create table if not exists utxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, watchOnly integer);
create table if not exists stxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, watchOnly integer, spendHeight integer, spendTxid text);
create table if not exists txns (txid text primary key not null, value integer, height integer, timestamp integer, watchOnly integer, tx blob);
create table if not exists watchedScripts (scriptPubKey text primary key not null);
create table if not exists scanBlocks (blockHash text primary key not null, blockHeight integer,isFixScan integer);
create table if not exists noticeTx (txHash text primary key not null, value integer, wechatTxId text,targetAddress text, isNotice integer, noticedCount integer);
create table if not exists config(key text primary key not null, value blob);
`
_, err := db.Exec(sqlStmt)
if err != nil {
return err
}
return nil
}
func (s *SQLiteDatastore) GetMnemonic() (string, error) {
s.lock.RLock()
defer s.lock.RUnlock()
stmt, err := s.db.Prepare("select value from config where key=?")
defer stmt.Close()
var mnemonic string
err = stmt.QueryRow("mnemonic").Scan(&mnemonic)
if err != nil {
return "", err
}
return mnemonic, nil
}
func (s *SQLiteDatastore) SetMnemonic(mnemonic string) error {
s.lock.RLock()
defer s.lock.RUnlock()
tx, err := s.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("insert or replace into config(key, value) values(?,?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec("mnemonic", mnemonic)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (s *SQLiteDatastore) GetCreationDate() (time.Time, error) {
s.lock.RLock()
defer s.lock.RUnlock()
var t time.Time
stmt, err := s.db.Prepare("select value from config where key=?")
if err != nil {
return t, err
}
defer stmt.Close()
var creationDate []byte
err = stmt.QueryRow("creationDate").Scan(&creationDate)
if err != nil {
return t, err
}
return time.Parse(time.RFC3339, string(creationDate))
}
func (s *SQLiteDatastore) SetCreationDate(creationDate time.Time) error {
s.lock.RLock()
defer s.lock.RUnlock()
tx, err := s.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("insert or replace into config(key, value) values(?,?)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec("creationDate", creationDate.Format(time.RFC3339))
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}