-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d9aa99
commit 119fdbe
Showing
14 changed files
with
665 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network'); | ||
const path = require('path'); | ||
|
||
const ccpPath = path.resolve(__dirname, '..', '..', '..', 'first-network', 'connection-org1.json'); | ||
|
||
async function main(userName) { | ||
try { | ||
|
||
// Create a new file system based wallet for managing identities. | ||
const walletPath = path.join(process.cwd(), 'wallet'); | ||
const wallet = new FileSystemWallet(walletPath); | ||
console.log(`Wallet path(creatUser): ${walletPath}`); | ||
|
||
// Check to see if we've already enrolled the user. | ||
const userExists = await wallet.exists(userName); | ||
if (userExists) { | ||
console.log('An identity for the user '+userName+' already exists in the wallet'); | ||
return; | ||
} | ||
|
||
// Check to see if we've already enrolled the admin user. | ||
const adminExists = await wallet.exists('admin'); | ||
if (!adminExists) { | ||
console.log('An identity for the admin user "admin" does not exist in the wallet'); | ||
console.log('Run the enrollAdmin.js application before retrying'); | ||
return; | ||
} | ||
|
||
// Create a new gateway for connecting to our peer node. | ||
const gateway = new Gateway(); | ||
await gateway.connect(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } }); | ||
|
||
// Get the CA client object from the gateway for interacting with the CA. | ||
const ca = gateway.getClient().getCertificateAuthority(); | ||
const adminIdentity = gateway.getCurrentIdentity(); | ||
|
||
// Register the user, enroll the user, and import the new identity into the wallet. | ||
const secret = await ca.register({ affiliation: 'org1.department1', enrollmentID: userName, role: 'client' }, adminIdentity); | ||
const enrollment = await ca.enroll({ enrollmentID: userName, enrollmentSecret: secret }); | ||
const userIdentity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes()); | ||
await wallet.import(userName, userIdentity); | ||
console.log('Successfully registered and enrolled admin user '+userName+' and imported it into the wallet'); | ||
|
||
} catch (error) { | ||
console.error(`Failed to register user '+userName+': ${error}`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
// main(); | ||
module.exports = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { FileSystemWallet, Gateway } = require('fabric-network'); | ||
const path = require('path'); | ||
|
||
const ccpPath = path.resolve(__dirname, '..', '..', '..', 'first-network', 'connection-org1.json'); | ||
|
||
async function main() { | ||
try { | ||
|
||
// Create a new file system based wallet for managing identities. | ||
const walletPath = path.join(process.cwd(), 'wallet'); | ||
const wallet = new FileSystemWallet(walletPath); | ||
console.log(`Wallet path(login): ${walletPath}`); | ||
|
||
// Check to see if we've already enrolled the user. | ||
const userExists = await wallet.exists('user1'); | ||
if (!userExists) { | ||
console.log('An identity for the user "user1" does not exist in the wallet'); | ||
console.log('Run the registerUser.js application before retrying'); | ||
return; | ||
} | ||
|
||
// Create a new gateway for connecting to our peer node. | ||
const gateway = new Gateway(); | ||
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } }); | ||
|
||
// Get the network (channel) our contract is deployed to. | ||
const network = await gateway.getNetwork('mychannel'); | ||
|
||
// Get the contract from the network. | ||
const contract = network.getContract('IntellectualProperty'); | ||
|
||
// Evaluate the specified transaction. | ||
// queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') | ||
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars') | ||
// const result = await contract.evaluateTransaction('queryAllCars'); | ||
const result = await contract.evaluateTransaction('queryCar', 'CAR4'); | ||
console.log(`Transaction has been evaluated, result is: ${result.toString()}`); | ||
return result.toString(); | ||
|
||
} catch (error) { | ||
console.error(`Failed to evaluate transaction: ${error}`); | ||
process.exit(1); | ||
return error; | ||
} | ||
} | ||
|
||
// main(); | ||
module.exports = main; |
Oops, something went wrong.