From 5103ce069aa7eee5ec19a716db8a967fc1b6b6c2 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 14 Jan 2020 18:58:58 -0800 Subject: [PATCH] tests and fixes for client from json/file --- client.go | 15 +++++++++---- client_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index d42b87e70..b8f5a805d 100644 --- a/client.go +++ b/client.go @@ -81,8 +81,8 @@ func NewClient(network map[string]AccountID) Client { } type configOperator struct { - AccountID AccountID `json:"accountId"` - PrivateKey []byte `json:"privateKey"` + AccountID string `json:"accountId"` + PrivateKey string `json:"privateKey"` } type clientConfig struct { @@ -102,19 +102,26 @@ func ClientFromJSON(jsonBytes []byte) (Client, error) { return client, nil } - operatorKey, err := Ed25519PrivateKeyFromBytes(clientConfig.Operator.PrivateKey) + operatorId, err := AccountIDFromString(clientConfig.Operator.AccountID) + if err != nil { + return Client{}, err + } + + operatorKey, err := Ed25519PrivateKeyFromString(clientConfig.Operator.PrivateKey) if err != nil { return Client{}, err } operator := operator{ - accountID: clientConfig.Operator.AccountID, + accountID: operatorId, privateKey: &operatorKey, publicKey: operatorKey.PublicKey(), signer: operatorKey.Sign, } client.operator = &operator + + return client, nil } func ClientFromFile(filename string) (Client, error) { diff --git a/client_test.go b/client_test.go index 881785c7f..a131766e6 100644 --- a/client_test.go +++ b/client_test.go @@ -1 +1,61 @@ package hedera + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +const testClientJson string = `{ + "network": { + "35.237.200.180:50211": "0.0.3", + "35.186.191.247:50211": "0.0.4", + "35.192.2.25:50211": "0.0.5", + "35.199.161.108:50211": "0.0.6", + "35.203.82.240:50211": "0.0.7", + "35.236.5.219:50211": "0.0.8", + "35.197.192.225:50211": "0.0.9", + "35.242.233.154:50211": "0.0.10", + "35.240.118.96:50211": "0.0.11", + "35.204.86.32:50211": "0.0.12" + } +}` + +const testClientJsonWithOperator string = `{ + "network": { + "35.237.200.180:50211": "0.0.3", + "35.186.191.247:50211": "0.0.4", + "35.192.2.25:50211": "0.0.5", + "35.199.161.108:50211": "0.0.6", + "35.203.82.240:50211": "0.0.7", + "35.236.5.219:50211": "0.0.8", + "35.197.192.225:50211": "0.0.9", + "35.242.233.154:50211": "0.0.10", + "35.240.118.96:50211": "0.0.11", + "35.204.86.32:50211": "0.0.12" + }, + "operator": { + "accountId": "0.0.3", + "privateKey": "302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10" + } +}` + +func TestClientFromJSON(t *testing.T) { + client, err := ClientFromJSON([]byte(testClientJson)) + assert.NoError(t, err) + + assert.Equal(t, 10, len(client.networkNodeIds)) + assert.Nil(t, client.operator) +} + +func TestClientFromJSONWithOperator(t *testing.T) { + client, err := ClientFromJSON([]byte(testClientJsonWithOperator)) + assert.NoError(t, err) + + testOperatorKey, err := Ed25519PrivateKeyFromString("302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10") + assert.NoError(t, err) + + assert.Equal(t, 10, len(client.networkNodeIds)) + assert.NotNil(t, client.operator) + assert.Equal(t, testOperatorKey.keyData, client.operator.privateKey.keyData) + assert.Equal(t, AccountID{Account: 3}, client.operator.accountID) +}