簡體   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