github.com/MetalBlockchain/metalgo@v1.11.9/api/keystore/service.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 "fmt" 8 "net/http" 9 10 "go.uber.org/zap" 11 12 "github.com/MetalBlockchain/metalgo/api" 13 "github.com/MetalBlockchain/metalgo/utils/formatting" 14 "github.com/MetalBlockchain/metalgo/utils/logging" 15 ) 16 17 type service struct { 18 ks *keystore 19 } 20 21 func (s *service) CreateUser(_ *http.Request, args *api.UserPass, _ *api.EmptyReply) error { 22 s.ks.log.Warn("deprecated API called", 23 zap.String("service", "keystore"), 24 zap.String("method", "createUser"), 25 logging.UserString("username", args.Username), 26 ) 27 28 return s.ks.CreateUser(args.Username, args.Password) 29 } 30 31 func (s *service) DeleteUser(_ *http.Request, args *api.UserPass, _ *api.EmptyReply) error { 32 s.ks.log.Warn("deprecated API called", 33 zap.String("service", "keystore"), 34 zap.String("method", "deleteUser"), 35 logging.UserString("username", args.Username), 36 ) 37 38 return s.ks.DeleteUser(args.Username, args.Password) 39 } 40 41 type ListUsersReply struct { 42 Users []string `json:"users"` 43 } 44 45 func (s *service) ListUsers(_ *http.Request, _ *struct{}, reply *ListUsersReply) error { 46 s.ks.log.Warn("deprecated API called", 47 zap.String("service", "keystore"), 48 zap.String("method", "listUsers"), 49 ) 50 51 var err error 52 reply.Users, err = s.ks.ListUsers() 53 return err 54 } 55 56 type ImportUserArgs struct { 57 // The username and password of the user being imported 58 api.UserPass 59 // The string representation of the user 60 User string `json:"user"` 61 // The encoding of [User] ("hex") 62 Encoding formatting.Encoding `json:"encoding"` 63 } 64 65 func (s *service) ImportUser(_ *http.Request, args *ImportUserArgs, _ *api.EmptyReply) error { 66 s.ks.log.Warn("deprecated API called", 67 zap.String("service", "keystore"), 68 zap.String("method", "importUser"), 69 logging.UserString("username", args.Username), 70 ) 71 72 // Decode the user from string to bytes 73 user, err := formatting.Decode(args.Encoding, args.User) 74 if err != nil { 75 return fmt.Errorf("couldn't decode 'user' to bytes: %w", err) 76 } 77 78 return s.ks.ImportUser(args.Username, args.Password, user) 79 } 80 81 type ExportUserArgs struct { 82 // The username and password 83 api.UserPass 84 // The encoding for the exported user ("hex") 85 Encoding formatting.Encoding `json:"encoding"` 86 } 87 88 type ExportUserReply struct { 89 // String representation of the user 90 User string `json:"user"` 91 // The encoding for the exported user ("hex") 92 Encoding formatting.Encoding `json:"encoding"` 93 } 94 95 func (s *service) ExportUser(_ *http.Request, args *ExportUserArgs, reply *ExportUserReply) error { 96 s.ks.log.Warn("deprecated API called", 97 zap.String("service", "keystore"), 98 zap.String("method", "exportUser"), 99 logging.UserString("username", args.Username), 100 ) 101 102 userBytes, err := s.ks.ExportUser(args.Username, args.Password) 103 if err != nil { 104 return err 105 } 106 107 // Encode the user from bytes to string 108 reply.User, err = formatting.Encode(args.Encoding, userBytes) 109 if err != nil { 110 return fmt.Errorf("couldn't encode user to string: %w", err) 111 } 112 reply.Encoding = args.Encoding 113 return nil 114 }