我最近从使用Composer转向在Go中编写ChainCode。在Composer中,使用ACL,我可以将某些事务限制为特定的participant
类型。
我正在尝试构建一个多组织网络,其中用户'类型'被定义为Go中的结构 - Client
Agent
和Manager
我希望Client
能够访问某些交易,Agent
可以访问不同的交易,Manager
可以访问所有Agent
和更多交易。
如何使用Fabric Go链代码实现此功能?感谢任何帮助!
谢谢
好吧让我们假设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
}
这应该让你大致了解如何继续,要记住几件事
onlyAgent
中的参数传递。我是出于演示目的而做的,从技术上讲你会想要提取Alice的证书,然后直接从中查询Alice(我可以在nodejs
链码中这样做,但似乎无法在go
中找到确切的API调用)