github.com/MetalBlockchain/metalgo@v1.11.9/api/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 keystore
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/api"
    10  	"github.com/MetalBlockchain/metalgo/utils/formatting"
    11  	"github.com/MetalBlockchain/metalgo/utils/rpc"
    12  )
    13  
    14  var _ Client = (*client)(nil)
    15  
    16  // Client interface for Avalanche Keystore API Endpoint
    17  //
    18  // Deprecated: The Keystore API is deprecated. Dedicated wallets should be used
    19  // instead.
    20  type Client interface {
    21  	CreateUser(context.Context, api.UserPass, ...rpc.Option) error
    22  	// Returns the usernames of all keystore users
    23  	ListUsers(context.Context, ...rpc.Option) ([]string, error)
    24  	// Returns the byte representation of the given user
    25  	ExportUser(context.Context, api.UserPass, ...rpc.Option) ([]byte, error)
    26  	// Import [exportedUser] to [importTo]
    27  	ImportUser(ctx context.Context, importTo api.UserPass, exportedUser []byte, options ...rpc.Option) error
    28  	// Delete the given user
    29  	DeleteUser(context.Context, api.UserPass, ...rpc.Option) error
    30  }
    31  
    32  // Client implementation for Avalanche Keystore API Endpoint
    33  type client struct {
    34  	requester rpc.EndpointRequester
    35  }
    36  
    37  // Deprecated: The Keystore API is deprecated. Dedicated wallets should be used
    38  // instead.
    39  func NewClient(uri string) Client {
    40  	return &client{requester: rpc.NewEndpointRequester(
    41  		uri + "/ext/keystore",
    42  	)}
    43  }
    44  
    45  func (c *client) CreateUser(ctx context.Context, user api.UserPass, options ...rpc.Option) error {
    46  	return c.requester.SendRequest(ctx, "keystore.createUser", &user, &api.EmptyReply{}, options...)
    47  }
    48  
    49  func (c *client) ListUsers(ctx context.Context, options ...rpc.Option) ([]string, error) {
    50  	res := &ListUsersReply{}
    51  	err := c.requester.SendRequest(ctx, "keystore.listUsers", struct{}{}, res, options...)
    52  	return res.Users, err
    53  }
    54  
    55  func (c *client) ExportUser(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]byte, error) {
    56  	res := &ExportUserReply{
    57  		Encoding: formatting.Hex,
    58  	}
    59  	err := c.requester.SendRequest(ctx, "keystore.exportUser", &user, res, options...)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return formatting.Decode(res.Encoding, res.User)
    64  }
    65  
    66  func (c *client) ImportUser(ctx context.Context, user api.UserPass, account []byte, options ...rpc.Option) error {
    67  	accountStr, err := formatting.Encode(formatting.Hex, account)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	return c.requester.SendRequest(ctx, "keystore.importUser", &ImportUserArgs{
    73  		UserPass: user,
    74  		User:     accountStr,
    75  		Encoding: formatting.Hex,
    76  	}, &api.EmptyReply{}, options...)
    77  }
    78  
    79  func (c *client) DeleteUser(ctx context.Context, user api.UserPass, options ...rpc.Option) error {
    80  	return c.requester.SendRequest(ctx, "keystore.deleteUser", &user, &api.EmptyReply{}, options...)
    81  }