Skip to content

Commit

Permalink
Return JSON RPC errors for personal_unlockAccount (#171)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicko Guyer <nicko.guyer@kaleido.io>
  • Loading branch information
nguyer committed Apr 15, 2022
1 parent 17aed81 commit 020f491
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/blockchain/ethereum/geth/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2021 Kaleido, Inc.
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -28,21 +28,33 @@ type GethClient struct {
rpcUrl string
}

type RpcRequest struct {
type JSONRPCRequest struct {
JsonRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}

type JSONRPCResponse struct {
JsonRPC string `json:"jsonrpc"`
ID int `json:"id"`
Error *JSONRPCError `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
}

type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}

func NewGethClient(rpcUrl string) *GethClient {
return &GethClient{
rpcUrl: rpcUrl,
}
}

func (g *GethClient) UnlockAccount(address string, password string) error {
requestBody, err := json.Marshal(&RpcRequest{
requestBody, err := json.Marshal(&JSONRPCRequest{
JsonRPC: "2.0",
ID: 0,
Method: "personal_unlockAccount",
Expand All @@ -69,5 +81,13 @@ func (g *GethClient) UnlockAccount(address string, password string) error {
if resp.StatusCode != 200 {
return fmt.Errorf("%s [%d] %s", req.URL, resp.StatusCode, responseBody)
}
var rpcResponse *JSONRPCResponse
err = json.Unmarshal(responseBody, &rpcResponse)
if err != nil {
return err
}
if rpcResponse.Error != nil {
return fmt.Errorf(rpcResponse.Error.Message)
}
return nil
}

0 comments on commit 020f491

Please sign in to comment.