-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
108 lines (101 loc) · 2.7 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
package db
import (
"database/sql"
"path"
"sync"
"github.com/dev-warrior777/go-electrum-client/wallet"
_ "github.com/mattn/go-sqlite3"
)
// This database is an SqLite3 implementation of Datastore.
// A different database could be plugged in .. bbolt maybe
type SQLiteDatastore struct {
cfg wallet.Cfg
enc wallet.Enc
keys wallet.Keys
utxos wallet.Utxos
stxos wallet.Stxos
txns wallet.Txns
subscriptions wallet.Subscriptions
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{
cfg: &CfgDB{
db: conn,
lock: l,
},
enc: &EncDB{
db: conn,
lock: l,
},
keys: &KeysDB{
db: conn,
lock: l,
},
utxos: &UtxoDB{
db: conn,
lock: l,
},
stxos: &StxoDB{
db: conn,
lock: l,
},
txns: &TxnsDB{
db: conn,
lock: l,
},
subscriptions: &SubscriptionsDB{
db: conn,
lock: l,
},
db: conn,
lock: l,
}
initDatabaseTables(conn)
return sqliteDB, nil
}
func (db *SQLiteDatastore) Cfg() wallet.Cfg {
return db.cfg
}
func (db *SQLiteDatastore) Enc() wallet.Enc {
return db.enc
}
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) Subscriptions() wallet.Subscriptions {
return db.subscriptions
}
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);
create table if not exists utxos (outpoint text primary key not null, value integer, height integer, scriptPubKey text, watchOnly integer, frozen 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 subscriptions (scriptPubKey text primary key not null, electrumScripthash text, address text);
create table if not exists config(key text primary key not null, value blob);
create table if not exists enc(key text primary key not null, value blob);
`
_, err := db.Exec(sqlStmt)
if err != nil {
return err
}
return nil
}