Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return JSON RPC errors for personal_unlockAccount #171

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}