Skip to content

Commit

Permalink
tests and fixes for client from json/file
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Jan 15, 2020
1 parent 87df336 commit 5103ce0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
15 changes: 11 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
60 changes: 60 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5103ce0

Please sign in to comment.