简体   繁体   English

Hyperledger Fabric 2.0,无法使用 Node.js SDK 访问用户的 Fabtoken

[英]Hyperledger Fabric 2.0, Can't Access Fabtokens of Users Using Node.js SDK

I'm trying to issue some Fabtokens to users and then use them in various scenarios such as transferring, redeeming, etc. I follow the Node SDK documentation here:https://fabric-sdk-node.github.io/master/tutorial-fabtoken.html I'm trying to issue some Fabtokens to users and then use them in various scenarios such as transferring, redeeming, etc. I follow the Node SDK documentation here:https://fabric-sdk-node.github.io/master/tutorial -fabtoken.html

This is how they do the Fabtoken operations:这就是他们执行 Fabtoken 操作的方式:

// create a TokenClient instance from client
const tokenclient = client.newTokenClient(mychannel);

// create a transaction ID for "issuer"
const txId = client.newTransactionID();

// create two parameters for issue, one for user1 and one for user2
const param1 = {
    owner: user1.getIdentity().serialize(),
    type: 'USD',
    quantity: '500',
};
const param2 = {
    owner: user2.getIdentity().serialize(),
    type: 'EURO',
    quantity: '300',
};

// create the token request for issue
const issueRequest = {
    params: [param1, param2],
    txId: txId,
};

// issuer calls issue method to issue tokens to user1 and user2
const result = await tokenClient.issue(issueRequest);

And then use a different tokenClient to list the tokens of user 1:然后使用不同的 tokenClient 列出用户 1 的令牌:

const user1Tokenclient = client1.newTokenClient(mychannel);

// user1 lists tokens
const mytokens = await user1TokenClient.list();

// iterate the tokens to get token id, type, and quantity for each token
for (const token of tokens) {
    // get token.id, token.type, and token.quantity
    // token.id will be used for transfer and redeem
}

It's mentioned on the Node SDK's Client class page here: https://fabric-sdk-node.github.io/master/Client.html that switching userContexts with the same client instance is an anti-pattern and not recommended since client instances are stateful. It's mentioned on the Node SDK's Client class page here: https://fabric-sdk-node.github.io/master/Client.html that switching userContexts with the same client instance is an anti-pattern and not recommended since client instances are有状态的。

As they suggest, I create my client instances with different user contexts.正如他们所建议的,我创建具有不同用户上下文的客户端实例。 This is how I create my clients, set their user context and create my tokenClient instances:这就是我创建客户、设置他们的用户上下文并创建我的 tokenClient 实例的方式:

const adminClient = new Fabric_Client();
const admin = await adminClient.createUser(user_opts);
adminClient.setUserContext(admin, true);
let adminConfig = {
    admin: admin,
    adminClient: adminClient,
    adminTokenClient: adminClient.newTokenClient(channel)
}

const server = await serverClient.createUser(server_opts); 
serverClient.setUserContext(server, true);
let serverConfig = {
    server: server,
    serverClient: serverClient,
    serverTokenClient: serverClient.newTokenClient(channel)
}

Later on, I'm using these config objects to issue some tokens to different users.稍后,我将使用这些config对象向不同的用户发布一些令牌。 How I issue tokens to my server account from my issuer (admin) account:我如何从我的颁发者(管理员)帐户向我的服务器帐户颁发令牌:

const txId = adminConfig.adminClient.newTransactionID();
let issueQuery = {
    tokenClient: adminConfig.adminTokenClient,
    txId: txId,
    channel: channel,
    params: []
}

for(let i=0; i < 3; ++i) {
    let param = {
        owner: serverConfig.server.getIdentity().serialize(),
        type: 'test',
        quantity: '1'
    }
    issueQuery.params.push(param);
}
let issueTx = await waitForIssue(issueQuery);

This successfully issue three tokens to the server as expected.这成功地向服务器发出了三个令牌,如预期的那样。 The problem is that when I try to access to the tokens of my server like the example they provide using a similar code:问题是,当我尝试访问我的服务器的令牌时,就像他们使用类似代码提供的示例一样:

let server_tokens = await serverConfig.serverTokenClient.list();
for (let server_token of server_tokens) {
    console.log(server_token.id);
}

Result is just empty and I don't get any error messages.结果只是空的,我没有收到任何错误消息。 However, when I check the transaction using queryTransaction(txId) for the token issue transaction I generate, I can see that owner of the issued tokens in that transaction is the server and that's how I can be sure that I can successfully issue the tokens to the server.但是,当我使用queryTransaction(txId)为我生成的令牌发行交易检查交易时,我可以看到该交易中已发行令牌的所有者是服务器,这就是我可以确定我可以成功地将令牌发行给服务器。 Is there any other way to check the tokens of my server?有没有其他方法可以检查我的服务器的令牌? Or shouldn't I use a different client and user context per each user as they suggest?或者我不应该按照他们的建议为每个用户使用不同的客户端和用户上下文? Because, previously I was able to see the tokens of the server when I used a single client and single user context to issue and list tokens.因为,以前当我使用单个客户端和单个用户上下文来发布和列出令牌时,我能够看到服务器的令牌。 But this approach caused me problems when I was trying to transfer my tokens asynchronously.但是当我尝试异步传输我的令牌时,这种方法给我带来了问题。

As far as I know FabTokens are removed from the Master branch of Fabric 2.0 now as described in these links:据我所知,FabTokens 现在已从 Fabric 2.0 的主分支中删除,如以下链接所述:

https://gerrit.hyperledger.org/r/c/fabric/+/32979 https://gerrit.hyperledger.org/r/c/fabric/+/32979

https://lists.hyperledger.org/g/fabric/topic/fabtoken/34150195?p=,,,20,0,0,0::recentpostdate%2Fsticky,,,20,2,0,34150195 https://lists.hyperledger.org/g/fabric/topic/fabtoken/34150195?p=,,,20,0,0,0::recentpostdate%2Fsticky,,,20,2,0,34150195

I would expect the tutorial and the information in the Fabric docs to be removed in due course.我希望在适当的时候删除教程和 Fabric 文档中的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM