Skip to content

Commit

Permalink
feat: made network more easier to set with config file or from json, …
Browse files Browse the repository at this point in the history
…network : [testnet|mainnet|previewnet] works now
  • Loading branch information
andrix10 authored and janaakhterov committed Jan 7, 2021
1 parent cca6064 commit ee69824
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
73 changes: 54 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ type configOperator struct {

// TODO: Implement complete spec: https://gitlab.com/launchbadge/hedera/sdk/python/-/issues/45
type clientConfig struct {
Network map[string]string `json:"network"`
MirrorNetwork string `json:"mirrorNetwork"`
Operator *configOperator `json:"operator"`
Network interface{} `json:"network"`
MirrorNetwork interface{} `json:"mirrorNetwork"`
Operator *configOperator `json:"operator"`
}

// ClientFromConfig takes in the byte slice representation of a JSON string or
Expand All @@ -147,26 +147,61 @@ func ClientFromConfig(jsonBytes []byte) (*Client, error) {

network := make(map[string]AccountID)

for url, id := range clientConfig.Network {
accountID, err := AccountIDFromString(id)
if err != nil {
return client, err
switch net := clientConfig.Network.(type) {
case map[string]interface{}:
for url, inter := range net {
switch id := inter.(type) {
case string:
accountID, err := AccountIDFromString(id)
if err != nil {
return client, err
}

network[url] = accountID
default:
return client, errors.New("network is expected to be map of string to string, or string")
}
}

network[url] = accountID
case string:
if len(net) > 0 {
switch net {
case "mainnet":
network = mainnetNodes
case "previewnet":
network = previewnetNodes
case "testnet":
network = testnetNodes
}
}
default:
return client, errors.New("network is expected to be map of string to string, or string")
}

if len(clientConfig.MirrorNetwork) > 0 {
switch mirror := clientConfig.MirrorNetwork; mirror {
case "mainnet":
client = newClient(network, mainnetMirror)
case "previewnet":
client = newClient(network, previewnetMirror)
case "testnet":
client = newClient(network, testnetMirror)
switch mirror := clientConfig.MirrorNetwork.(type) {
case []interface{}:
arr := make([]string, len(mirror))
for i, inter := range mirror {
switch str := inter.(type) {
case string:
arr[i] = str
default:
return client, errors.New("mirrorNetwork is expected to be either string or an array of strings")
}
}
} else {
return client, errors.New("mirror network wasn't provided")
client = newClient(network, arr)
case string:
if len(mirror) > 0 {
switch mirror {
case "mainnet":
client = newClient(network, mainnetMirror)
case "previewnet":
client = newClient(network, previewnetMirror)
case "testnet":
client = newClient(network, testnetMirror)
}
}
default:
return client, errors.New("mirrorNetwork is expected to be either string or an array of strings")
}

// if the operator is not provided, finish here
Expand Down
31 changes: 31 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hedera

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -42,6 +43,24 @@ const testClientJSONWithOperator string = `{
"mirrorNetwork": "testnet"
}`

const testClientJSONWrongTypeMirror string = `{
"network": "testnet",
"operator": {
"accountId": "0.0.3",
"privateKey": "302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10"
},
"mirrorNetwork": 5
}`

const testClientJSONWrongTypeNetwork string = `{
"network": 1,
"operator": {
"accountId": "0.0.3",
"privateKey": "302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10"
},
"mirrorNetwork": ["hcs.testnet.mirrornode.hedera.com:5600"]
}`

func TestClientFromConfig(t *testing.T) {
client, err := ClientFromConfig([]byte(testClientJSON))
assert.NoError(t, err)
Expand Down Expand Up @@ -84,3 +103,15 @@ func TestClientFromConfigWithOperator(t *testing.T) {
assert.Equal(t, testOperatorKey.keyData, client.operator.privateKey.keyData)
assert.Equal(t, AccountID{Account: 3}, client.operator.accountID)
}

func TestClientFromConfigWrongType(t *testing.T) {
_, err := ClientFromConfig([]byte(testClientJSONWrongTypeMirror))
if err != nil {
assert.Equal(t, fmt.Sprintf("mirrorNetwork is expected to be either string or an array of strings"), err.Error())
}

_, err = ClientFromConfig([]byte(testClientJSONWrongTypeNetwork))
if err != nil {
assert.Equal(t, fmt.Sprintf("network is expected to be map of string to string, or string"), err.Error())
}
}

0 comments on commit ee69824

Please sign in to comment.