forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
101 lines (89 loc) · 2.43 KB
/
client.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
package cardano
import (
"fmt"
"github.com/tclairet/cardano-go/crypto"
"github.com/tyler-smith/go-bip39"
)
// Client provides a clean interface for creating, saving and deleting Wallets.
type Client struct {
db DB
node cardanoNode
socketPath string
}
// NewClient builds a new Client using cardano-cli as the default connection
// to the Blockhain.
//
// It uses BadgerDB as the default Wallet storage.
func NewClient(opts ...Options) *Client {
client := &Client{node: newCli()}
for _, opt := range opts {
opt.apply(client)
}
if client.db == nil {
client.db = newBadgerDB()
}
return client
}
// Close closes all the resources used by the Client.
func (c *Client) Close() {
c.db.Close()
}
// CreateWallet creates a new Wallet using a secure entropy and password,
// returning a Wallet with its corresponding 24 word mnemonic
func (c *Client) CreateWallet(name, password string) (*Wallet, string, error) {
entropy := newEntropy(entropySizeInBits)
mnemonic := crypto.NewMnemonic(entropy)
wallet := newWallet(name, password, entropy)
wallet.node = c.node
err := c.db.SaveWallet(wallet)
if err != nil {
return nil, "", err
}
return wallet, mnemonic, nil
}
// RestoreWallet restores a Wallet from a mnemonic and password.
func (c *Client) RestoreWallet(name, password, mnemonic string) (*Wallet, error) {
entropy, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
wallet := newWallet(name, password, entropy)
wallet.node = c.node
err = c.db.SaveWallet(wallet)
if err != nil {
return nil, err
}
return wallet, nil
}
// SaveWallet saves a Wallet in the Client's storage.
func (c *Client) SaveWallet(w *Wallet) error {
return c.db.SaveWallet(w)
}
// Wallets returns the list of Wallets currently saved in the Client's storage.
func (c *Client) Wallets() ([]*Wallet, error) {
wallets, err := c.db.GetWallets()
if err != nil {
return nil, err
}
for i := range wallets {
wallets[i].node = c.node
}
return wallets, nil
}
// Wallet returns a Wallet with the given id from the Client's storage.
func (c *Client) Wallet(id string) (*Wallet, error) {
wallets, err := c.Wallets()
if err != nil {
return nil, err
}
for _, w := range wallets {
if w.ID == id {
return w, nil
}
}
return nil, fmt.Errorf("wallet %v not found", id)
}
// DeleteWallet removes a Wallet with the given id from the Client's storage.
func (c *Client) DeleteWallet(id string) error {
return c.db.DeleteWallet(id)
}