使用Hyperledger Composer JavaScript API无法生成身份卡求助
I'm trying to create an identity card using Hyperledger Composer's JavaScript API. Here's my code:
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; async function identityIssue() { let businessNetworkConnection = new BusinessNetworkConnection(); try { await businessNetworkConnection.connect('admin@demoNetwork'); let result = await businessNetworkConnection.issueIdentity('org.acme.demoNetwork.Participant#testUser', 'test'); console.log(`userID = ${result.userID}`); console.log(`userSecret = ${result.userSecret}`); await businessNetworkConnection.disconnect(); } catch(error) { console.log(error); process.exit(1); } } identityIssue();I already have an existing participant named
testUser. The code runs successfully and I get theuserIDanduserSecret, but no identity card is generated. How can I generate the identity card without using the CLI?
The issueIdentity method from BusinessNetworkConnection only creates the identity on the blockchain and returns the credentials—it doesn't automatically generate or save an identity card locally. To generate an identity card programmatically, you'll need to use classes from the composer-common package to construct the card and save it to a card store (like the local file system).
Here's a modified version of your code that generates and saves the identity card:
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection; const { Card, FileSystemCardStore } = require('composer-common'); async function identityIssueAndCreateCard() { let businessNetworkConnection = new BusinessNetworkConnection(); try { // Connect as admin to issue the identity await businessNetworkConnection.connect('admin@demoNetwork'); const businessNetworkName = 'demoNetwork'; const participantId = 'org.acme.demoNetwork.Participant#testUser'; const identityName = 'test'; // Issue the identity and get credentials let result = await businessNetworkConnection.issueIdentity(participantId, identityName); const { userID, userSecret } = result; console.log(`userID = ${userID}`); console.log(`userSecret = ${userSecret}`); // Reuse the admin card's connection profile to avoid manual config const adminCard = await businessNetworkConnection.cardStore.get('admin@demoNetwork'); const connectionProfile = adminCard.getConnectionProfile(); // Construct the new identity card const card = new Card({ userName: userID, businessNetworkName: businessNetworkName, connectionProfile: connectionProfile, credentials: { certificate: '', // Will be populated automatically when the card is first used privateKey: '' }, metadata: { version: 1, userName: userID, businessNetwork: businessNetworkName, participant: participantId } }); // Attach the user secret to the card (required for authentication) card.setSecret(userSecret); // Use the file system card store to save the card locally const cardStore = new FileSystemCardStore(); const cardName = `${userID}@${businessNetworkName}`; await cardStore.put(cardName, card); console.log(`Identity card saved successfully: ${cardName}`); await businessNetworkConnection.disconnect(); } catch(error) { console.error('Error during identity card creation:', error); process.exit(1); } } identityIssueAndCreateCard();
Key details to understand:
- FileSystemCardStore: This class saves the card to your local
~/.composer/cards/directory, just like the CLI does. - Card construction: We build the card with all necessary metadata, including the participant ID, connection details, and credentials. The empty certificate/privateKey fields will be filled when you first use the card to connect to the network.
- Secret attachment: Calling
card.setSecret(userSecret)ensures the card can authenticate with the business network when used later.
Once saved, you can use the card to connect to the network directly:
await businessNetworkConnection.connect(`${userID}@demoNetwork`);
内容的提问来源于stack exchange,提问作者bunjee

