-
Notifications
You must be signed in to change notification settings - Fork 8
/
wallet.go
214 lines (181 loc) · 4.92 KB
/
wallet.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
210
211
212
213
214
package wallet
import (
"errors"
"os"
"path/filepath"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // blank import for bolt db driver
"github.com/p2pderivatives/dlc/internal/rpc"
"github.com/p2pderivatives/dlc/pkg/wallet"
)
// Namespace bucket keys.
var (
waddrmgrNamespaceKey = []byte("waddrmgr")
waddrmgrKeyScope = waddrmgr.KeyScopeBIP0084
)
const accountName = "dlc"
// Wallet is hierarchical deterministic Wallet
type Wallet struct {
params *chaincfg.Params
publicPassphrase []byte
rpc rpc.Client
db walletdb.DB
manager *waddrmgr.Manager
account uint32
}
// wallet should satisfy Wallet interface
var _ wallet.Wallet = (*Wallet)(nil)
// CreateWallet returns a new Wallet, also creates db where wallet resides
func CreateWallet(
params *chaincfg.Params,
seed, pubPass, privPass []byte,
walletDir, walletName string) (wallet.Wallet, error) {
db, err := createDB(walletDir, walletName+".db")
if err != nil {
return nil, err
}
return create(db, params, seed, pubPass, privPass)
}
// createDB creates a new db at specified path
func createDB(dbDirPath, dbname string) (walletdb.DB, error) {
dbPath := filepath.Join(dbDirPath, dbname)
err := os.MkdirAll(dbDirPath, 0700)
if err != nil {
return nil, err
}
db, err := walletdb.Create("bdb", dbPath)
if err != nil {
return nil, err
}
return db, err
}
// Create creates an new wallet, writing it to the passed in db.
func create(
db walletdb.DB,
params *chaincfg.Params,
seed, pubPass, privPass []byte) (*Wallet, error) {
err := createManagers(db, seed, pubPass, privPass, params)
if err != nil {
return nil, err
}
err = createAccount(db, privPass, pubPass, params)
if err != nil {
return nil, err
}
w, err := open(db, pubPass, params, nil)
if err != nil {
return nil, err
}
return w, nil
}
// createManagers create address manager and tx manager
func createManagers(
db walletdb.DB,
seed, pubPass, privPass []byte,
params *chaincfg.Params,
) error {
return walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
addrmgrNs, e := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
if e != nil {
return e
}
birthday := time.Now()
e = waddrmgr.Create(
addrmgrNs, seed, pubPass, privPass, params, nil,
birthday,
)
return e
})
}
// createAccount creates a new account in ScopedKeyManagar of scope
func createAccount(
db walletdb.DB, privPass, pubPass []byte, params *chaincfg.Params) error {
return walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
addrMgr, e := waddrmgr.Open(ns, pubPass, params)
if e != nil {
return e
}
e = addrMgr.Unlock(ns, privPass)
if e != nil {
return e
}
scopedMgr, e := addrMgr.FetchScopedKeyManager(waddrmgrKeyScope)
if e != nil {
return e
}
_, e = scopedMgr.NewAccount(ns, accountName)
return e
})
}
// Open loads a wallet from the passed db and public pass phrase.
func Open(
db walletdb.DB, pubPass []byte, params *chaincfg.Params, rpcclient rpc.Client,
) (wallet.Wallet, error) {
return open(db, pubPass, params, rpcclient)
}
// open is an implementation of Open
func open(
db walletdb.DB, pubPass []byte, params *chaincfg.Params, rpcclient rpc.Client,
) (*Wallet, error) {
// TODO: Perform wallet upgrades/updates if necessary?
// Open database abstraction instances
var (
addrMgr *waddrmgr.Manager
account uint32
)
err := walletdb.View(db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
if addrmgrNs == nil {
return errors.New("missing address manager namespace")
}
var e error
addrMgr, e = waddrmgr.Open(addrmgrNs, pubPass, params)
if e != nil {
return e
}
scopedMgr, e := addrMgr.FetchScopedKeyManager(waddrmgrKeyScope)
if e != nil {
return e
}
account, e = scopedMgr.LookupAccount(addrmgrNs, accountName)
return e
})
if err != nil {
return nil, err
}
w := &Wallet{
params: params,
publicPassphrase: pubPass,
rpc: rpcclient,
db: db,
manager: addrMgr,
account: account,
}
return w, nil
}
// SetRPCClient sets rpc client
func (w *Wallet) SetRPCClient(rpc rpc.Client) {
w.rpc = rpc
}
// SendRawTransaction delegates to RPC client
func (w *Wallet) SendRawTransaction(tx *wire.MsgTx) (*chainhash.Hash, error) {
return w.rpc.SendRawTransaction(tx, false)
}
// Unlock unlocks address manager with a given private pass phrase
func (w *Wallet) Unlock(privPass []byte) error {
return walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return w.manager.Unlock(ns, privPass)
})
}
// Close closes managers
func (w *Wallet) Close() error {
w.manager.Close()
return w.db.Close()
}