github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/routes/account_keys.go (about) 1 package routes 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/flow-go/access" 7 "github.com/onflow/flow-go/engine/access/rest/models" 8 "github.com/onflow/flow-go/engine/access/rest/request" 9 ) 10 11 // GetAccountKeyByIndex handler retrieves an account key by address and index and returns the response 12 func GetAccountKeyByIndex(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) { 13 req, err := r.GetAccountKeyRequest() 14 if err != nil { 15 return nil, models.NewBadRequestError(err) 16 } 17 18 // In case we receive special height values 'final' and 'sealed', 19 // fetch that height and overwrite request with it. 20 if req.Height == request.FinalHeight || req.Height == request.SealedHeight { 21 isSealed := req.Height == request.SealedHeight 22 header, _, err := backend.GetLatestBlockHeader(r.Context(), isSealed) 23 if err != nil { 24 err := fmt.Errorf("block with height: %d does not exist", req.Height) 25 return nil, models.NewNotFoundError(err.Error(), err) 26 } 27 req.Height = header.Height 28 } 29 30 account, err := backend.GetAccountAtBlockHeight(r.Context(), req.Address, req.Height) 31 if err != nil { 32 err := fmt.Errorf("account with address: %s does not exist", req.Address) 33 return nil, models.NewNotFoundError(err.Error(), err) 34 } 35 36 var response models.AccountPublicKey 37 for _, key := range account.Keys { 38 if key.Index == int(req.Index) { 39 response.Build(key) 40 return response, nil 41 } 42 } 43 44 err = fmt.Errorf("account key with index: %d does not exist", req.Index) 45 return nil, models.NewNotFoundError(err.Error(), err) 46 }