繁体   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

这就是他们执行 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);

然后使用不同的 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有状态的。

正如他们所建议的,我创建具有不同用户上下文的客户端实例。 这就是我创建客户、设置他们的用户上下文并创建我的 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)
}

稍后,我将使用这些config对象向不同的用户发布一些令牌。 我如何从我的颁发者(管理员)帐户向我的服务器帐户颁发令牌:

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);

这成功地向服务器发出了三个令牌,如预期的那样。 问题是,当我尝试访问我的服务器的令牌时,就像他们使用类似代码提供的示例一样:

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

结果只是空的,我没有收到任何错误消息。 但是,当我使用queryTransaction(txId)为我生成的令牌发行交易检查交易时,我可以看到该交易中已发行令牌的所有者是服务器,这就是我可以确定我可以成功地将令牌发行给服务器。 有没有其他方法可以检查我的服务器的令牌? 或者我不应该按照他们的建议为每个用户使用不同的客户端和用户上下文? 因为,以前当我使用单个客户端和单个用户上下文来发布和列出令牌时,我能够看到服务器的令牌。 但是当我尝试异步传输我的令牌时,这种方法给我带来了问题。

据我所知,FabTokens 现在已从 Fabric 2.0 的主分支中删除,如以下链接所述:

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

我希望在适当的时候删除教程和 Fabric 文档中的信息。

暂无
暂无

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

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