-
Notifications
You must be signed in to change notification settings - Fork 672
/
keystore_client.go
59 lines (48 loc) · 1.54 KB
/
keystore_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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gkeystore
import (
"context"
"github.com/hashicorp/go-plugin"
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/api/keystore/gkeystore/gkeystoreproto"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/encdb"
"github.com/ava-labs/avalanchego/database/rpcdb"
"github.com/ava-labs/avalanchego/database/rpcdb/rpcdbproto"
)
var _ keystore.BlockchainKeystore = &Client{}
// Client is a snow.Keystore that talks over RPC.
type Client struct {
client gkeystoreproto.KeystoreClient
broker *plugin.GRPCBroker
}
// NewClient returns a keystore instance connected to a remote keystore instance
func NewClient(client gkeystoreproto.KeystoreClient, broker *plugin.GRPCBroker) *Client {
return &Client{
client: client,
broker: broker,
}
}
func (c *Client) GetDatabase(username, password string) (*encdb.Database, error) {
bcDB, err := c.GetRawDatabase(username, password)
if err != nil {
return nil, err
}
return encdb.New([]byte(password), bcDB)
}
func (c *Client) GetRawDatabase(username, password string) (database.Database, error) {
resp, err := c.client.GetDatabase(context.Background(), &gkeystoreproto.GetDatabaseRequest{
Username: username,
Password: password,
})
if err != nil {
return nil, err
}
dbConn, err := c.broker.Dial(resp.DbServer)
if err != nil {
return nil, err
}
dbClient := rpcdb.NewClient(rpcdbproto.NewDatabaseClient(dbConn))
return dbClient, err
}