Import Identities into a Wallet
The next step is to import the identities into the wallet. test-network has pre-generated user identities. So, in this chapter, we will simply save them into the wallet. The creation of user identities from the application will be discussed in the Interaction with Fabric CA from an Application chapter.
A single wallet can hold multiple identities, each issued by a particular certificate authority (CA). Each identity has a standard structure comprising a descriptive label, an X.509 certificate containing a public key, a private key, the MSP identifier of an organization that the identity belongs to, and an identity type. Different wallet types map this structure appropriately to their storage mechanism.
There are two identity types supported by default: X.509 and HSM-X.509. X.509 is a default choice for wallets. HSM-X.509 is a choice if you want to use a hardware security module as an identity storage. The difference between the two types is that for HSM-X.509 identity, a private key is stored in an HSM instead of a wallet store.
Let’s now import the identities of User1 and Admin from both Org1 and Org2 located in the test-network/organizations folder. In addToWallet.js, compose a path to the credentials and extract the certificate and the private key for each user.
const fs = require('fs');
const path = require('path');
const { Wallets } = require('fabric-network');
const testNetworkRoot = path.resolve(require('os').homedir(), 'go/src/github.com/hyperledger/fabric-samples/test-network');
async function main() {
try {
const wallet = await Wallets.newFileSystemWallet('./wallet');
const predefinedOrgs = [
{
name: 'org1.example.com',
mspId: 'Org1MSP',
users: ['Admin', 'User1']
}, {
name: 'org2.example.com',
mspId: 'Org2MSP',
users: ['Admin', 'User1']
}
];
for (const org of predefinedOrgs) {
const credPath = path.join(testNetworkRoot, '/organizations/peerOrganizations/', org.name, '/users');
for (const user of org.users) {
const mspFolderPath = path.join(credPath, `${user}@${org.name}`, '/msp');
// expecting only one cert file and one key file to be in the directories
const certFile = path.join(mspFolderPath, '/signcerts/', fs.readdirSync(path.join(mspFolderPath, '/signcerts'))[0]);
const keyFile = path.join(mspFolderPath, '/keystore/', fs.readdirSync(path.join(mspFolderPath, '/keystore'))[0]);
const cert = fs.readFileSync(certFile).toString();
const key = fs.readFileSync(keyFile).toString();
// <...>
}
}
} catch (error) {
console.log(`Error adding to wallet. ${error}`);
console.log(error.stack);
}
}
Next, create the X.509 identity object.
for (const user of org.users) {
// <...>
const identity = {
credentials: {
certificate: cert,
privateKey: key,
},
mspID: org.mspID,
type: 'X.509',
};
}
Pay attention that the identity object contains MSP ID metadata. This data is required by the Gateway class. Gateway uses the MSP ID to identify particular peers from a connection profile. We will discuss the Gateway class, its configuration, and use in the following section.
Finally, we can import prepared identities into the wallet.
for (const user of org.users) {
// <...>
const identityLabel = `${user}@${org.name}`;
await wallet.put(identityLabel, identity);
}
Pay attention to the labels that we use to import the identities. These labels are associated with the identities and serve as keys to retrieve the identities.
Now, the addToWallet.js file is complete and ready to run. The resulting code can be found in the Resources tab in the Menu. Let’s now initialize the npm package and run the script.
Create a package.json file using npm init:
# npm init -y
The package.json file should contain the following data
{
"name": "balance_transfer",
"version": "1.0.0",
"description": "",
"main": "addToWallet.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Add the engines and dependencies blocks to the package.json file:
"engines": {
"node": ">=12",
"npm": ">=5"
}
"dependencies": {
"fabric-network": "^2.2.4"
}
Install all the dependencies required to run addToWallet.js:
npm install
The node_module folder should appear in the application directory.
Run addToWallet.js:
node addToWallet.js
As a result, you should see a newly created wallet folder containing the identities of User1 and Admin of both organizations.
Last updated
Was this helpful?