Skip to content

Commit be26521

Browse files
authored
Merge pull request #3 from knarayan/sdkrelay
Sdkrelay
2 parents 32851ad + 624dca6 commit be26521

22 files changed

+1351
-81
lines changed

samples/fabric/fabric-cli/src/helpers/fabric-functions.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,15 @@ const generateAccessControl = async (
176176
)
177177
}
178178
const updatedRules = templateJSON.rules.map(rule => {
179-
rule.principal = keyCert.cert
180-
rule.principalType = 'certificate'
179+
if (rule.principalType == 'ca') {
180+
rule.principal = mspId
181+
} else if (rule.principalType == 'certificate') {
182+
rule.principal = keyCert.cert
183+
} else {
184+
logger.error(
185+
'Error Invalid Principal Type in template file'
186+
)
187+
}
181188
return rule
182189
})
183190
const accessControlJSON = {
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"network1": {
3+
"connProfilePath": "<PATH-TO-WEAVER>/tests/network-setups/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.yaml",
4+
"relayEndpoint": "localhost:9080",
5+
"mspId": "Org1MSP",
6+
"channelName": "mychannel",
7+
"chaincode": "simpleasset"
8+
},
9+
"network2": {
10+
"connProfilePath": "<PATH-TO-WEAVER>/tests/network-setups/fabric/shared/network2/peerOrganizations/org1.network2.com/connection-org1.yaml",
11+
"relayEndpoint": "localhost:9083",
12+
"mspId": "Org1MSP",
13+
"channelName": "mychannel",
14+
"chaincode": "simpleasset"
15+
}
16+
}

sdks/fabric/go-cli/configure/all.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2020 IBM All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package configure
8+
9+
import (
10+
"errors"
11+
"fmt"
12+
13+
"github.com/cloudflare/cfssl/log"
14+
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers"
15+
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers/interopsetup"
16+
)
17+
18+
// helper functions to log and return errors
19+
func logThenErrorf(format string, args ...interface{}) error {
20+
errorMsg := fmt.Sprintf(format, args...)
21+
log.Error(errorMsg)
22+
return errors.New(errorMsg)
23+
}
24+
25+
type NetworkConfig struct {
26+
RelayEndPoint string `json:"relayEndPoint"`
27+
ConnProfilePath string `json:"connProfilePath"`
28+
Username string `json:"username"`
29+
MspId string `json:"mspId"`
30+
ChannelName string `json:"channelName"`
31+
Chaincode string `json:"chaincode"`
32+
}
33+
34+
func ConfigureAll(networkId string) error {
35+
36+
networkConfig, err := helpers.GetNetworkConfig(networkId)
37+
if err != nil {
38+
return logThenErrorf(err.Error())
39+
}
40+
connProfilePath := networkConfig.ConnProfilePath
41+
42+
if connProfilePath != "" {
43+
logThenErrorf("please use a valid network, no valid environment found for %s", networkId)
44+
}
45+
46+
username := "User1@org1." + networkId + ".com"
47+
log.Infof("generating membership for network: %s", networkId)
48+
49+
// 1. Generate network configs (membership, access control and verification policy)
50+
51+
helpers.GenerateMembership("mychannel", "interop", connProfilePath, networkId, "Org1MSP", username)
52+
helpers.GenerateAccessControl("mychannel", "interop", connProfilePath, networkId, "data/interop/accessControlTemplate.json", "Org1MSP", username)
53+
helpers.GenerateVerificationPolicy("mychannel", "interop", connProfilePath, networkId, "data/interop/verificationPolicyTemplate.json", "Org1MSP", username)
54+
55+
log.Info("generated network maps for networks")
56+
57+
// 2. Add default data
58+
networkEnv, err := helpers.GetNetworkConfig(networkId)
59+
if err != nil {
60+
return logThenErrorf("failure of helpers.GetNetworkConfig for network %s with error: %s", networkId, err.Error())
61+
}
62+
connProfilePath = networkEnv.ConnProfilePath
63+
if connProfilePath == "" {
64+
logThenErrorf("please use a valid --local-network, no valid environment found for network %s", networkId)
65+
}
66+
log.Info("populating %s chaincode with data")
67+
68+
query := helpers.QueryType{
69+
ContractName: "simplestate",
70+
Channel: "mychannel",
71+
CcFunc: "Create",
72+
Args: []string{},
73+
}
74+
if networkId == "network1" {
75+
helpers.AddData("stars.json", connProfilePath, networkId, query, "Org1MSP")
76+
} else if networkId == "network2" {
77+
helpers.AddData("starSize.json", connProfilePath, networkId, query, "Org1MSP")
78+
}
79+
80+
// 3. Load configs from other networks in the credentials folder
81+
log.Infof("loading chaincode for network: %s and connection profile path: %s", networkId, connProfilePath)
82+
83+
interopsetup.ConfigureNetwork(networkId)
84+
85+
return nil
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"securityDomain": "",
3+
"rules": [
4+
{
5+
"principal": "<mspid>",
6+
"principalType": "ca",
7+
"resource": "mychannel:simplestate:Read:*",
8+
"read": true
9+
}
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"securityDomain": "<network-id>",
3+
"identifiers": [
4+
{
5+
"pattern": "mychannel:simplestate:Read:*",
6+
"policy": {
7+
"type": "Signature",
8+
"criteria": []
9+
}
10+
}]
11+
}

sdks/fabric/go-cli/data/starSize.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"Arcturus": "17.671",
3+
"Betelgeuse": "617.1",
4+
"Canopus": "49.395",
5+
"Deneb": "141.23",
6+
"Electra": "4.2159",
7+
"Furud": "2.713",
8+
"Giausar": "27.065",
9+
"Hadar": "5.983",
10+
"IZAR": "18.412",
11+
"Jabbah": "6.033",
12+
"Kuma": "1.386",
13+
"Lesath": "4.244",
14+
"Maia": "4.202",
15+
"Naos": "9.74",
16+
"Omicron1 Eridani": "2.5741",
17+
"Peacock": "3.3602",
18+
"Rotanev": "2.752",
19+
"Sadalsuud": "34.8",
20+
"Talitha": "1.288",
21+
"UNUKALHAI": "8.3484",
22+
"VEGA": "1.6432",
23+
"Wasat": "1.649",
24+
"Xamidimura": "4.774",
25+
"Yildun": "1.948",
26+
"Zosma": "1.4888"
27+
}

sdks/fabric/go-cli/data/stars.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"a": "Arcturus",
3+
"b": "Betelgeuse",
4+
"c": "Canopus",
5+
"d": "Deneb",
6+
"e": "Electra",
7+
"f": "Furud",
8+
"g": "Giausar",
9+
"h": "Hadar",
10+
"i": "IZAR",
11+
"j": "Jabbah",
12+
"k": "Kuma",
13+
"l": "Lesath",
14+
"m": "Maia",
15+
"n": "Naos",
16+
"o": "Omicron1 Eridani",
17+
"p": "Peacock",
18+
"q": "No star with letter q",
19+
"r": "Rotanev",
20+
"s": "Sadalsuud",
21+
"t": "Talitha",
22+
"u": "UNUKALHAI",
23+
"v": "VEGA",
24+
"w": "Wasat",
25+
"x": "Xamidimura",
26+
"y": "Yildun",
27+
"z": "Zosma"
28+
}

sdks/fabric/go-cli/exerciseSDK.go

+72-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111

1212
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers"
1313
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli/helpers/interopsetup"
14+
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk/interoperablehelper"
15+
"github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk/types"
1416
)
1517

1618
func connectSimpleStateWithSDK() {
1719
connProfilePath := "../../../tests/network-setups/fabric/shared/network1/peerOrganizations/org1.network1.com/connection-org1.yaml"
18-
contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "[email protected]")
20+
_, contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "[email protected]")
1921

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

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

77-
contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "[email protected]")
81+
_, contract, _, _ := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), "mychannel", "simplestate", connProfilePath, "network1", "Org1MSP", "[email protected]")
7882
result, err = contract.EvaluateTransaction("ReadAsset", "t1", assetId, "true")
7983
if err != nil {
8084
log.Fatalf("failed Query with error: %s", err)
@@ -545,7 +549,7 @@ func fetchAccessControlPolicy(networkId string) {
545549
ContractName: "interop",
546550
Channel: "mychannel",
547551
CcFunc: "GetAccessControlPolicyBySecurityDomain",
548-
Args: []string{"network1"},
552+
Args: []string{networkId},
549553
}
550554

551555
result, err := helpers.Query(query, connProfilePath, networkId, "Org1MSP", "")
@@ -561,7 +565,7 @@ func fetchMembership(networkId string) {
561565
ContractName: "interop",
562566
Channel: "mychannel",
563567
CcFunc: "GetMembershipBySecurityDomain",
564-
Args: []string{"network1"},
568+
Args: []string{networkId},
565569
}
566570

567571
result, err := helpers.Query(query, connProfilePath, networkId, "Org1MSP", "")
@@ -577,7 +581,7 @@ func fetchVerificationPolicy(networkId string) {
577581
ContractName: "interop",
578582
Channel: "mychannel",
579583
CcFunc: "GetVerificationPolicyBySecurityDomain",
580-
Args: []string{"network1"},
584+
Args: []string{networkId},
581585
}
582586

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

598+
func interop(key string, localNetwork string, requestingOrg string, address string) {
599+
relayEnv, err := helpers.GetNetworkConfig(localNetwork)
600+
if err != nil {
601+
log.Fatalf("failed helpers.GetNetworkConfig with error: %+v", err.Error())
602+
}
603+
log.Debugf("relayEnv: %+v", relayEnv)
604+
605+
if (relayEnv.RelayEndPoint == "") || (relayEnv.ConnProfilePath == "") {
606+
log.Fatalf("please use a valid --local-network. If valid network please check if your environment variables are configured properly")
607+
}
608+
networkName := localNetwork
609+
channel := "mychannel"
610+
contractName := "interop"
611+
connProfilePath := relayEnv.ConnProfilePath
612+
mspId := requestingOrg
613+
username := "User1@org1." + localNetwork + ".com"
614+
615+
_, contract, wallet, err := helpers.FabricHelper(helpers.NewGatewayNetworkInterface(), channel, contractName, connProfilePath,
616+
networkName, mspId, username)
617+
if err != nil {
618+
log.Fatalf("failed helpers.FabricHelper with error: %s", err.Error())
619+
}
620+
keyUser, certUser, err := helpers.GetKeyAndCertForRemoteRequestbyUserName(wallet, username)
621+
if err != nil {
622+
log.Fatalf("failed helpers.GetKeyAndCertForRemoteRequestbyUserName with error: %s", err.Error())
623+
}
624+
log.Debugf("keyUser: %s & certUser: %s", keyUser, certUser)
625+
626+
applicationFunction := "Create"
627+
args := []string{key, ""}
628+
invokeObject := types.Query{
629+
ContractName: "simplestate",
630+
Channel: channel,
631+
CcFunc: applicationFunction,
632+
CcArgs: args,
633+
}
634+
log.Debugf("invokeObject: %+v", invokeObject)
635+
636+
interopJSON := types.InteropJSON{
637+
Address: address,
638+
ChaincodeFunc: "Read",
639+
ChaincodeId: "simplestate",
640+
ChannelId: channel,
641+
RemoteEndPoint: "localhost:9080",
642+
NetworkId: "network1",
643+
Sign: true,
644+
CcArgs: []string{"a"},
645+
}
646+
log.Debugf("interopJSON: %+v", interopJSON)
647+
interopJSONs := []types.InteropJSON{interopJSON}
648+
649+
interopArgIndices := []int{1}
650+
interoperablehelper.InteropFlow(contract, networkName, invokeObject, requestingOrg, relayEnv.RelayEndPoint, interopArgIndices, interopJSONs, keyUser, certUser, false)
651+
652+
}
653+
594654
func main() {
595655

596-
configureNetwork("network1")
597-
fetchAccessControlPolicy("network1")
598-
fetchMembership("network1")
599-
fetchVerificationPolicy("network1")
656+
interop("a", "network1", "Org1MSP", "localhost:9080/network1/mychannel:simplestate:Read:a")
657+
658+
//configureNetwork("network1")
659+
//fetchAccessControlPolicy("network1")
660+
//fetchMembership("network1")
661+
//fetchVerificationPolicy("network1")
600662

601-
//connectSimpleStateWithSDK() // needs the chaincode simplestate on the channel
663+
//connectSimpleStateWithSDK() // needs the chaincode simplestate on the channel
602664
//connectSimpleAssetWithSDK("a001") // needs the chaincode simpleasset on the channel
603665
//testLockAssetAndClaimAssetOfBondAsset("a020") // needs the chaincodes simpleasset and interop on the channel
604666
//testLockAssetAndUnlockAssetOfBondAsset("a021") // needs the chaincodes simpleasset and interop on the channel

sdks/fabric/go-cli/go.mod

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
module github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-cli
22

3-
go 1.15
3+
go 1.16
4+
5+
replace github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk => ./../go-sdk
46

57
require (
8+
github.com/cloudflare/cfssl v1.4.1
9+
github.com/hyperledger-labs/weaver-dlt-interoperability/sdks/fabric/go-sdk v0.0.0-00010101000000-000000000000
610
github.com/hyperledger/fabric-sdk-go v1.0.0
711
github.com/sirupsen/logrus v1.8.1
812
github.com/stretchr/testify v1.7.0

0 commit comments

Comments
 (0)