forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
keystore_server.go
68 lines (53 loc) · 1.62 KB
/
keystore_server.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gkeystore
import (
"context"
"github.com/MetalBlockchain/metalgo/api/keystore"
"github.com/MetalBlockchain/metalgo/database"
"github.com/MetalBlockchain/metalgo/database/rpcdb"
"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/grpcutils"
keystorepb "github.com/MetalBlockchain/metalgo/proto/pb/keystore"
rpcdbpb "github.com/MetalBlockchain/metalgo/proto/pb/rpcdb"
)
var _ keystorepb.KeystoreServer = (*Server)(nil)
// Server is a snow.Keystore that is managed over RPC.
type Server struct {
keystorepb.UnsafeKeystoreServer
ks keystore.BlockchainKeystore
}
// NewServer returns a keystore connected to a remote keystore
func NewServer(ks keystore.BlockchainKeystore) *Server {
return &Server{
ks: ks,
}
}
func (s *Server) GetDatabase(
_ context.Context,
req *keystorepb.GetDatabaseRequest,
) (*keystorepb.GetDatabaseResponse, error) {
db, err := s.ks.GetRawDatabase(req.Username, req.Password)
if err != nil {
return nil, err
}
closer := dbCloser{Database: db}
serverListener, err := grpcutils.NewListener()
if err != nil {
return nil, err
}
server := grpcutils.NewServer()
closer.closer.Add(server)
rpcdbpb.RegisterDatabaseServer(server, rpcdb.NewServer(&closer))
// start the db server
go grpcutils.Serve(serverListener, server)
return &keystorepb.GetDatabaseResponse{ServerAddr: serverListener.Addr().String()}, nil
}
type dbCloser struct {
database.Database
closer grpcutils.ServerCloser
}
func (db *dbCloser) Close() error {
err := db.Database.Close()
db.closer.Stop()
return err
}