如何限制Fabric中某些“类型”用户的函数调用?

问题描述 投票:0回答:1

我最近从使用Composer转向在Go中编写ChainCode。在Composer中,使用ACL,我可以将某些事务限制为特定的participant类型。

我正在尝试构建一个多组织网络,其中用户'类型'被定义为Go中的结构 - Client AgentManager

我希望Client能够访问某些交易,Agent可以访问不同的交易,Manager可以访问所有Agent和更多交易。

如何使用Fabric Go链代码实现此功能?感谢任何帮助!

谢谢

hyperledger-fabric hyperledger-fabric-sdk-go
1个回答
1
投票

好吧让我们假设Alice是一个代理。有一个函数onlyAgent(),你想确保只能由Alice调用。它会是这样的

func (t *SimpleChaincode) createParticipant (stub shim.ChaincodeStubInterface, args []string) pb.Response {
    username := args[0]; // This would be Alice
    type := args[1]; // This should be Agent
    user := &marble{type , username }
    userAsJsonBues, err := json.Marshal(user )
    err = stub.PutState(marbleName, userAsJsonBues);
    return shim.Success(nil);
}

func (t *SimpleChaincode) onlyAgent(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    username := args[0]; // Expects to be Alice
    aliceAsBytes, err := stub.GetState(username)
    alice:= User{}
    err = json.Unmarshal(aliceAsBytes, &alice)
    // alice.user should return Agent. Perform whatever checks you want
}

这应该让你大致了解如何继续,要记住几件事

  1. 此示例需要将名称Alice作为onlyAgent中的参数传递。我是出于演示目的而做的,从技术上讲你会想要提取Alice的证书,然后直接从中查询Alice(我可以在nodejs链码中这样做,但似乎无法在go中找到确切的API调用)
© www.soinside.com 2019 - 2024. All rights reserved.