Hyperledger Composer 给已创建的User发放新的身份,并将新身份导入钱包

介绍两种方法,给已创建的User发放新的身份,并将新身份导入钱包(wallet)。

一,通过NodeJs,给已创建的User发放新的身份,并将新身份导入钱包(wallet),具体代码如下:

首先在package.json中加入下面的依赖文件:

"dependencies": {
........
"composer-common": "latest",
"composer-client": "latest",
"composer-admin": "latest",
"composer-cli": "latest",
"composer-rest-server": "latest",
........
}

执行npm install,把依赖文件下载带node_modules文件夹。

新创建register.js文件,把下面代码粘贴并修改红色字体。

二,通过composer命令,给已创建的User发放新的身份,并将新身份导入钱包(wallet),具体代码如下:

const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const IdCard                    = require('composer-common').IdCard;
const AdminConnection           = require('composer-admin').AdminConnection;
//const CardExport                = require('composer-cli').Card.Export;
//const ParticipantAdd 			= require('composer-cli').Participant.Add;

class Sample {

  // Initialize
  init() {
	let businessNetworkConnection = new BusinessNetworkConnection();
	const adminConnection         = new AdminConnection();
	var cardName;
	//命名空间+User(这个user,在链上必须存在)
	const participant = namespace + '#' + User;
	//CardId(生成Card的userID,可以自己命名) 
	const userID = CardId;
	
	return businessNetworkConnection.connect(businessNetwork)
	.then(() => {
        return businessNetworkConnection.issueIdentity(participant, userID)
    })
	.then((identity) => {
		// Create and import card
		const metadata = {
            userName: identity.userID,
            version: 1,
            enrollmentSecret: identity.userSecret,
            businessNetwork: businessNetwork
        };
		cardName = metadata.userName + "@" + metadata.businessNetwork
		//connection.json文件内容
		const connectionProfile = {......};
		//const connectionProfile = require('./local_connection.json').
		//const card = new IdCard(metadata, connectionProfile);
		const card = new IdCard(metadata, connectionProfile);

        return adminConnection.importCard(cardName, card)
	})
	.then((result) => {
      	console.log("Import Card Successed !");
    }).catch((error) => {
      	console.error(error);
    });
  }
}

var sam = new Sample();
sam.init();


1,给已创建的User发放新的身份

composer identity issue --card admin@iot-perishable-network --file ID_CARD_FILE --newUserId IDENTITY --participantId 'resource:org.acme.shipping.perishable.PARTICIPANT#PARTICIPANT_ID'
其中:

ID_CARD_FILE— 是用于存储 ID 卡的文件名。

IDENTITY— 是要发放的身份。

PARTICIPANT_CLASS— 是参与者类。

PARTICIPANT_ID— 是在注册表中实例化的参与者的 ID。

2,将新身份导入钱包(wallet)

composer card import --file XXXXXXXX.card

要查看发放的卡,可以运行 composer card list 命令


你可能感兴趣的:(区块链)