github.com/MetalBlockchain/metalgo@v1.11.9/api/keystore/gkeystore/keystore_client.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package gkeystore
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/api/keystore"
    10  	"github.com/MetalBlockchain/metalgo/database"
    11  	"github.com/MetalBlockchain/metalgo/database/encdb"
    12  	"github.com/MetalBlockchain/metalgo/database/rpcdb"
    13  	"github.com/MetalBlockchain/metalgo/vms/rpcchainvm/grpcutils"
    14  
    15  	keystorepb "github.com/MetalBlockchain/metalgo/proto/pb/keystore"
    16  	rpcdbpb "github.com/MetalBlockchain/metalgo/proto/pb/rpcdb"
    17  )
    18  
    19  var _ keystore.BlockchainKeystore = (*Client)(nil)
    20  
    21  // Client is a snow.Keystore that talks over RPC.
    22  type Client struct {
    23  	client keystorepb.KeystoreClient
    24  }
    25  
    26  // NewClient returns a keystore instance connected to a remote keystore instance
    27  func NewClient(client keystorepb.KeystoreClient) *Client {
    28  	return &Client{
    29  		client: client,
    30  	}
    31  }
    32  
    33  func (c *Client) GetDatabase(username, password string) (*encdb.Database, error) {
    34  	bcDB, err := c.GetRawDatabase(username, password)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return encdb.New([]byte(password), bcDB)
    39  }
    40  
    41  func (c *Client) GetRawDatabase(username, password string) (database.Database, error) {
    42  	resp, err := c.client.GetDatabase(context.Background(), &keystorepb.GetDatabaseRequest{
    43  		Username: username,
    44  		Password: password,
    45  	})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	clientConn, err := grpcutils.Dial(resp.ServerAddr)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	dbClient := rpcdb.NewClient(rpcdbpb.NewDatabaseClient(clientConn))
    56  	return dbClient, err
    57  }