Skip to content

Commit

Permalink
Merge pull request #3 from knarayan/sdkrelay
Browse files Browse the repository at this point in the history
Sdkrelay
  • Loading branch information
knarayan authored Aug 10, 2021
2 parents 32851ad + 624dca6 commit be26521
Show file tree
Hide file tree
Showing 22 changed files with 1,351 additions and 81 deletions.
11 changes: 9 additions & 2 deletions samples/fabric/fabric-cli/src/helpers/fabric-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,15 @@ const generateAccessControl = async (
)
}
const updatedRules = templateJSON.rules.map(rule => {
rule.principal = keyCert.cert
rule.principalType = 'certificate'
if (rule.principalType == 'ca') {
rule.principal = mspId
} else if (rule.principalType == 'certificate') {
rule.principal = keyCert.cert
} else {
logger.error(
'Error Invalid Principal Type in template file'
)
}
return rule
})
const accessControlJSON = {
Expand Down
16 changes: 16 additions & 0 deletions sdks/fabric/go-cli/config.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"network1": {
"connProfilePath": "<PATH-TO-WEAVER>/tests/network-setups/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.yaml",
"relayEndpoint": "localhost:9080",
"mspId": "Org1MSP",
"channelName": "mychannel",
"chaincode": "simpleasset"
},
"network2": {
"connProfilePath": "<PATH-TO-WEAVER>/tests/network-setups/fabric/shared/network2/peerOrganizations/org1.network2.com/connection-org1.yaml",
"relayEndpoint": "localhost:9083",
"mspId": "Org1MSP",
"channelName": "mychannel",
"chaincode": "simpleasset"
}
}
86 changes: 86 additions & 0 deletions sdks/fabric/go-cli/configure/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package configure

import (
"errors"
"fmt"

"github.com/cloudflare/cfssl/log"
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers"
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers/interopsetup"
)

// helper functions to log and return errors
func logThenErrorf(format string, args ...interface{}) error {
errorMsg := fmt.Sprintf(format, args...)
log.Error(errorMsg)
return errors.New(errorMsg)
}

type NetworkConfig struct {
RelayEndPoint string `json:"relayEndPoint"`
ConnProfilePath string `json:"connProfilePath"`
Username string `json:"username"`
MspId string `json:"mspId"`
ChannelName string `json:"channelName"`
Chaincode string `json:"chaincode"`
}

func ConfigureAll(networkId string) error {

networkConfig, err := helpers.GetNetworkConfig(networkId)
if err != nil {
return logThenErrorf(err.Error())
}
connProfilePath := networkConfig.ConnProfilePath

if connProfilePath != "" {
logThenErrorf("please use a valid network, no valid environment found for %s", networkId)
}

username := "User1@org1." + networkId + ".com"
log.Infof("generating membership for network: %s", networkId)

// 1. Generate network configs (membership, access control and verification policy)

helpers.GenerateMembership("mychannel", "interop", connProfilePath, networkId, "Org1MSP", username)
helpers.GenerateAccessControl("mychannel", "interop", connProfilePath, networkId, "data/interop/accessControlTemplate.json", "Org1MSP", username)
helpers.GenerateVerificationPolicy("mychannel", "interop", connProfilePath, networkId, "data/interop/verificationPolicyTemplate.json", "Org1MSP", username)

log.Info("generated network maps for networks")

// 2. Add default data
networkEnv, err := helpers.GetNetworkConfig(networkId)
if err != nil {
return logThenErrorf("failure of helpers.GetNetworkConfig for network %s with error: %s", networkId, err.Error())
}
connProfilePath = networkEnv.ConnProfilePath
if connProfilePath == "" {
logThenErrorf("please use a valid --local-network, no valid environment found for network %s", networkId)
}
log.Info("populating %s chaincode with data")

query := helpers.QueryType{
ContractName: "simplestate",
Channel: "mychannel",
CcFunc: "Create",
Args: []string{},
}
if networkId == "network1" {
helpers.AddData("stars.json", connProfilePath, networkId, query, "Org1MSP")
} else if networkId == "network2" {
helpers.AddData("starSize.json", connProfilePath, networkId, query, "Org1MSP")
}

// 3. Load configs from other networks in the credentials folder
log.Infof("loading chaincode for network: %s and connection profile path: %s", networkId, connProfilePath)

interopsetup.ConfigureNetwork(networkId)

return nil
}
11 changes: 11 additions & 0 deletions sdks/fabric/go-cli/data/interop/accessControlTemplate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"securityDomain": "",
"rules": [
{
"principal": "<mspid>",
"principalType": "ca",
"resource": "mychannel:simplestate:Read:*",
"read": true
}
]
}
11 changes: 11 additions & 0 deletions sdks/fabric/go-cli/data/interop/verificationPolicyTemplate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"securityDomain": "<network-id>",
"identifiers": [
{
"pattern": "mychannel:simplestate:Read:*",
"policy": {
"type": "Signature",
"criteria": []
}
}]
}
27 changes: 27 additions & 0 deletions sdks/fabric/go-cli/data/starSize.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Arcturus": "17.671",
"Betelgeuse": "617.1",
"Canopus": "49.395",
"Deneb": "141.23",
"Electra": "4.2159",
"Furud": "2.713",
"Giausar": "27.065",
"Hadar": "5.983",
"IZAR": "18.412",
"Jabbah": "6.033",
"Kuma": "1.386",
"Lesath": "4.244",
"Maia": "4.202",
"Naos": "9.74",
"Omicron1 Eridani": "2.5741",
"Peacock": "3.3602",
"Rotanev": "2.752",
"Sadalsuud": "34.8",
"Talitha": "1.288",
"UNUKALHAI": "8.3484",
"VEGA": "1.6432",
"Wasat": "1.649",
"Xamidimura": "4.774",
"Yildun": "1.948",
"Zosma": "1.4888"
}
28 changes: 28 additions & 0 deletions sdks/fabric/go-cli/data/stars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"a": "Arcturus",
"b": "Betelgeuse",
"c": "Canopus",
"d": "Deneb",
"e": "Electra",
"f": "Furud",
"g": "Giausar",
"h": "Hadar",
"i": "IZAR",
"j": "Jabbah",
"k": "Kuma",
"l": "Lesath",
"m": "Maia",
"n": "Naos",
"o": "Omicron1 Eridani",
"p": "Peacock",
"q": "No star with letter q",
"r": "Rotanev",
"s": "Sadalsuud",
"t": "Talitha",
"u": "UNUKALHAI",
"v": "VEGA",
"w": "Wasat",
"x": "Xamidimura",
"y": "Yildun",
"z": "Zosma"
}
82 changes: 72 additions & 10 deletions sdks/fabric/go-cli/exerciseSDK.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (

"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers"
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers/interopsetup"
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk/interoperablehelper"
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk/types"
)

func connectSimpleStateWithSDK() {
connProfilePath := "../../../tests/network-setups/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.yaml"
contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "User1@org1.network1.com")
_, contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "User1@org1.network1.com")

result, err := contract.EvaluateTransaction("Read", "a")
if err != nil {
Expand All @@ -24,6 +26,8 @@ func connectSimpleStateWithSDK() {
log.Infof("result of query: %s", result)

result, err = contract.SubmitTransaction("Create", "key01", "value")
//valBytes, _ := json.Marshal([]int64{1, 2})
//result, err = contract.SubmitTransaction("CreateArr", "key01", string(valBytes))
if err != nil {
log.Fatalf("failed Invoke with error: %+v", err)
}
Expand Down Expand Up @@ -74,7 +78,7 @@ func connectSimpleAssetWithSDK(assetId string) {
}
log.Printf("%s helpers.Invoke %v", query.CcFunc, string(result))

contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "User1@org1.network1.com")
_, contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "User1@org1.network1.com")
result, err = contract.EvaluateTransaction("ReadAsset", "t1", assetId, "true")
if err != nil {
log.Fatalf("failed Query with error: %s", err)
Expand Down Expand Up @@ -545,7 +549,7 @@ func fetchAccessControlPolicy(networkId string) {
ContractName: "interop",
Channel: "mychannel",
CcFunc: "GetAccessControlPolicyBySecurityDomain",
Args: []string{"network1"},
Args: []string{networkId},
}

result, err := helpers.Query(query, connProfilePath, networkId, "Org1MSP", "")
Expand All @@ -561,7 +565,7 @@ func fetchMembership(networkId string) {
ContractName: "interop",
Channel: "mychannel",
CcFunc: "GetMembershipBySecurityDomain",
Args: []string{"network1"},
Args: []string{networkId},
}

result, err := helpers.Query(query, connProfilePath, networkId, "Org1MSP", "")
Expand All @@ -577,7 +581,7 @@ func fetchVerificationPolicy(networkId string) {
ContractName: "interop",
Channel: "mychannel",
CcFunc: "GetVerificationPolicyBySecurityDomain",
Args: []string{"network1"},
Args: []string{networkId},
}

result, err := helpers.Query(query, connProfilePath, networkId, "Org1MSP", "")
Expand All @@ -591,14 +595,72 @@ func configureNetwork(networkId string) {
interopsetup.ConfigureNetwork(networkId)
}

func interop(key string, localNetwork string, requestingOrg string, address string) {
relayEnv, err := helpers.GetNetworkConfig(localNetwork)
if err != nil {
log.Fatalf("failed helpers.GetNetworkConfig with error: %+v", err.Error())
}
log.Debugf("relayEnv: %+v", relayEnv)

if (relayEnv.RelayEndPoint == "") || (relayEnv.ConnProfilePath == "") {
log.Fatalf("please use a valid --local-network. If valid network please check if your environment variables are configured properly")
}
networkName := localNetwork
channel := "mychannel"
contractName := "interop"
connProfilePath := relayEnv.ConnProfilePath
mspId := requestingOrg
username := "User1@org1." + localNetwork + ".com"

_, contract, wallet, err := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), channel, contractName, connProfilePath,
networkName, mspId, username)
if err != nil {
log.Fatalf("failed helpers.FabricHelper with error: %s", err.Error())
}
keyUser, certUser, err := helpers.GetKeyAndCertForRemoteRequestbyUserName(wallet, username)
if err != nil {
log.Fatalf("failed helpers.GetKeyAndCertForRemoteRequestbyUserName with error: %s", err.Error())
}
log.Debugf("keyUser: %s & certUser: %s", keyUser, certUser)

applicationFunction := "Create"
args := []string{key, ""}
invokeObject := types.Query{
ContractName: "simplestate",
Channel: channel,
CcFunc: applicationFunction,
CcArgs: args,
}
log.Debugf("invokeObject: %+v", invokeObject)

interopJSON := types.InteropJSON{
Address: address,
ChaincodeFunc: "Read",
ChaincodeId: "simplestate",
ChannelId: channel,
RemoteEndPoint: "localhost:9080",
NetworkId: "network1",
Sign: true,
CcArgs: []string{"a"},
}
log.Debugf("interopJSON: %+v", interopJSON)
interopJSONs := []types.InteropJSON{interopJSON}

interopArgIndices := []int{1}
interoperablehelper.InteropFlow(contract, networkName, invokeObject, requestingOrg, relayEnv.RelayEndPoint, interopArgIndices, interopJSONs, keyUser, certUser, false)

}

func main() {

configureNetwork("network1")
fetchAccessControlPolicy("network1")
fetchMembership("network1")
fetchVerificationPolicy("network1")
interop("a", "network1", "Org1MSP", "localhost:9080/network1/mychannel:simplestate:Read:a")

//configureNetwork("network1")
//fetchAccessControlPolicy("network1")
//fetchMembership("network1")
//fetchVerificationPolicy("network1")

//connectSimpleStateWithSDK() // needs the chaincode simplestate on the channel
//connectSimpleStateWithSDK() // needs the chaincode simplestate on the channel
//connectSimpleAssetWithSDK("a001") // needs the chaincode simpleasset on the channel
//testLockAssetAndClaimAssetOfBondAsset("a020") // needs the chaincodes simpleasset and interop on the channel
//testLockAssetAndUnlockAssetOfBondAsset("a021") // needs the chaincodes simpleasset and interop on the channel
Expand Down
6 changes: 5 additions & 1 deletion sdks/fabric/go-cli/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli

go 1.15
go 1.16

replace github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk => ./../go-sdk

require (
github.com/cloudflare/cfssl v1.4.1
github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk v0.0.0-00010101000000-000000000000
github.com/hyperledger/fabric-sdk-go v1.0.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
Expand Down
Loading

0 comments on commit be26521

Please sign in to comment.