如何在超级账本fabric中使用fabric-client sdk创建通道

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

我一直致力于为超级账本结构网络构建一个 API,该 API 将在我们的一个项目中使用。主要问题是,对于相同的内容,绝对存在荒谬的文档。该文档不包含任何示例,这让我很痛苦。 这是我想出的代码,我能够连接到网络,但我的方法因“参数错误”而失败。

有人可以解释一下如何解决这样的问题吗?还有其他方法可以实现这一目标吗?我有另一种方法,需要运行 kubernetes 作业来运行脚本来创建新通道及其工件。但我认为 sdk 方法会是一种更干净的方法。


const getchannels = async () => {
    
    console.log("Here")
    
    try {
        
        const ccp = buildCCPOrg1();

    // build an instance of the fabric ca services client based on
    // the information in the network configuration
    const caClient = buildCAClient(
      FabricCAServices,
      ccp,
      "ca.org1.example.com"
    );

    // setup the wallet to hold the credentials of the application user
    const wallet = await buildWallet(Wallets, walletPath);

    // in a real application this would be done on an administrative flow, and only once
    await enrollAdmin(caClient, wallet, mspOrg1);
    
        const client = new Client('admin');
        // const identity = org1UserId;
        
        const identity = await wallet.get(org1UserId);
        
        console.log(identity);
        
        
        
        // await client.setUserContext('admin');
        await client.setUserContext(org1UserId, true);
        
        await client.loadFromConfig(ccp);
        
        const peer = new Client.Peer("grpc://peer0.org1.example.com:7051");
        
        const channel = await client.newChannel("newchannel");
        
        channel.addPeer(peer);
        
        const response = await client.queryChannels(peer);
        
        console.log("Result : \n");

        console.log(response);
    
        
    } catch (error) {
        console.log(error)
    }
}

//Build CCP
exports.buildCCPOrg1 = () => {
    // load the common connection configuration file
    const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
    const fileExists = fs.existsSync(ccpPath);
    if (!fileExists) {
        throw new Error(`no such file or directory: ${ccpPath}`);
    }
    const contents = fs.readFileSync(ccpPath, 'utf8');

    // build a JSON object from the file contents
    const ccp = JSON.parse(contents);

    console.log(`Loaded the network configuration located at ${ccpPath}`);
    return ccp;
};

这是查询代码。

错误:

Error: Missing parameter. Must have a username.
    at Client._setUserFromConfig (/home/haaris/go/src/github.com/haariskhatri/fabric-samples/asset-transfer-basic/application-javascript/node_modules/fabric-client/lib/Client.js:1400:10)
    at Client.setUserContext (/home/haaris/go/src/github.com/haariskhatri/fabric-samples/asset-transfer-basic/application-javascript/node_modules/fabric-client/lib/Client.js:1535:15)
    at getchannels (/home/haaris/go/src/github.com/haariskhatri/fabric-samples/asset-transfer-basic/application-javascript/app.js:124:16)
    at async /home/haaris/go/src/github.com/haariskhatri/fabric-samples/asset-transfer-basic/application-javascript/app.js:89:2
node.js hyperledger-fabric hyperledger
1个回答
0
投票

您似乎正在尝试使用 Hyperledger Fabric v1.4 的 fabric-client API。您缺少的 API 文档位于此处

请注意,Fabric v1.4 或 fabric-client API 均不再受支持。在 Fabric v2.2 中,fabric-client 完全被 fabric-common 取代,尽管这纯粹是作为低级实现而设计的,以支持高级 fabric-network API,这是推荐用于为 Fabric v1.4 和 v2.2 构建客户端应用程序的 API。从 Fabric v2.4 开始(包括当前的 v2.5 长期支持版本),唯一支持的用于客户端应用程序开发的 API 是Fabric Gateway 客户端 API

从 Fabric v2.2 开始,客户端 API 故意不支持管理操作,例如通道创建。对于管理任务,可以使用三种主要方法:

  1. Fabric CLI 命令。这是用于配置 Fabric 示例中使用的test-network的方法。
  2. Fabric 管理 SDK。在撰写本文时,主要的 Golang 实现接近但不完全达到稳定的 v1.0 版本级别。该存储库还包含 Node.js 管理 API(基于此处开发的代码),但它可能尚未处于稳定、受支持的状态。
  3. 如果部署到 Kubernetes,请使用专为管理 Fabric 设计的 Kubernetes Operator。我知道的两个替代方案是 Hyperledger Fabric Operatorfabric-operator
© www.soinside.com 2019 - 2024. All rights reserved.