好的,让我们假设 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
}
这应该让您大致了解如何进行,有几件事要记住
- 此示例要求将名称 Alice 作为参数传递给
onlyAgent
. 我这样做是出于演示目的,从技术上讲,您可能希望提取 Alice 的证书,然后直接从中查询 Alice(我可以在链代码中执行此nodejs
操作,但似乎无法在其中找到确切的 API 调用go
)