0

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

我正在尝试构建一个多组织网络,其中用户“类型”被定义为 Go 中的结构——Client Agent并且Manager

我希望Client有权访问某些事务,Agent有权访问一组不同的事务,并Manager有权访问所有Agent的和更多的事务。

如何使用 Fabric Go 链码实现这一点?感谢任何帮助!

谢谢

4

1 回答 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操作,但似乎无法在其中找到确切的 API 调用go
于 2019-02-22T08:14:32.953 回答