-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from partior-3p/feature/set516_support_quorum…
…_tessera Support quorum tessera blockchain node
- Loading branch information
Showing
21 changed files
with
2,361 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright © 2024 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package quorum | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type QuorumClient struct { | ||
rpcURL string | ||
} | ||
|
||
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 NewQuorumClient(rpcURL string) *QuorumClient { | ||
return &QuorumClient{ | ||
rpcURL: rpcURL, | ||
} | ||
} | ||
|
||
func (g *QuorumClient) UnlockAccount(address string, password string) error { | ||
requestBody, err := json.Marshal(&JSONRPCRequest{ | ||
JSONRPC: "2.0", | ||
ID: 0, | ||
Method: "personal_unlockAccount", | ||
Params: []interface{}{address, password, 0}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest("POST", g.rpcURL, bytes.NewBuffer(requestBody)) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package quorum | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hyperledger/firefly-cli/internal/utils" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnlockAccount(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
RPCUrl string | ||
Address string | ||
Password string | ||
StatusCode int | ||
ApiResponse *JSONRPCResponse | ||
}{ | ||
{ | ||
Name: "TestUnlockAccount-1", | ||
RPCUrl: "http://127.0.0.1:8545", | ||
Address: "user-1", | ||
Password: "POST", | ||
StatusCode: 200, | ||
ApiResponse: &JSONRPCResponse{ | ||
JSONRPC: "2.0", | ||
ID: 0, | ||
Error: nil, | ||
Result: "mock result", | ||
}, | ||
}, | ||
{ | ||
Name: "TestUnlockAccountError-2", | ||
RPCUrl: "http://127.0.0.1:8545", | ||
Address: "user-1", | ||
Password: "POST", | ||
StatusCode: 200, | ||
ApiResponse: &JSONRPCResponse{ | ||
JSONRPC: "2.0", | ||
ID: 0, | ||
Error: &JSONRPCError{500, "invalid account"}, | ||
Result: "mock result", | ||
}, | ||
}, | ||
{ | ||
Name: "TestUnlockAccountHTTPError-3", | ||
RPCUrl: "http://localhost:8545", | ||
Address: "user-1", | ||
Password: "POST", | ||
StatusCode: 500, | ||
ApiResponse: &JSONRPCResponse{ | ||
JSONRPC: "2.0", | ||
ID: 0, | ||
Error: nil, | ||
Result: "mock result", | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
apiResponse, _ := json.Marshal(tc.ApiResponse) | ||
// mockResponse | ||
httpmock.RegisterResponder("POST", tc.RPCUrl, | ||
httpmock.NewStringResponder(tc.StatusCode, string(apiResponse))) | ||
client := NewQuorumClient(tc.RPCUrl) | ||
utils.StartMockServer(t) | ||
err := client.UnlockAccount(tc.Address, tc.Password) | ||
utils.StopMockServer(t) | ||
|
||
// expect errors when returned status code != 200 or ApiResponse comes back with non nil error | ||
if tc.StatusCode != 200 || tc.ApiResponse.Error != nil { | ||
assert.NotNil(t, err, "expects error to be returned when either quorum returns an application error or non 200 http response") | ||
} else { | ||
assert.NoError(t, err, fmt.Sprintf("unable to unlock account: %v", err)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.