Fabric 2.2添加身份至FileSystemWallet时遇构造函数错误求助
问题:添加Hyperledger Fabric身份到钱包时报错TypeError: FileSystemWallet is not a constructor
错误信息
Error adding to wallet. TypeError: FileSystemWallet is not a constructor TypeError: FileSystemWallet is not a constructor at Object.main [as execute] (/home/sujan/hyperledger/fabric-samples/application/1_addToWallet.js:14:20) at /home/sujan/hyperledger/fabric-samples/application/index.js:25:14 at Layer.handle [as handle_request] (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/layer.js:95:5) at next (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/layer.js:95:5) at /home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/index.js:284:15 at Function.process_params (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/index.js:346:12) at next (/home/sujan/hyperledger/fabric-samples/application/node_modules/express/lib/router/index.js:280:10) at /home/sujan/hyperledger/fabric-samples/application/node_modules/body-parser/lib/read.js:137:5
环境与相关代码
使用Hyperledger Fabric 2.2版本,身份添加代码如下:
'use strict'; /** * This is a Node.JS module to load a user's Identity to his wallet. * This Identity will be used to sign transactions initiated by this user. */ const fs = require('fs'); // FileSystem Library const { FileSystemWallet, X509WalletMixin } = require('fabric-network'); // Wallet Library provided by Fabric async function main(certificatePath, privateKeyPath) { try { // A wallet is a filesystem path that stores a collection of Identities const wallet = new FileSystemWallet(`./identity/org1`); const certificate = fs.readFileSync(certificatePath).toString(); const privatekey = fs.readFileSync(privateKeyPath).toString(); // Load credentials into wallet const identityLabel = `org1_admin`; const identity = X509WalletMixin.createIdentity(`org1MSP`, certificate, privatekey); await wallet.import(identityLabel, identity); } catch (error) { console.log(`Error adding to wallet. ${error}`); console.log(error.stack); throw new Error(error); } } module.exports.execute = main;
项目package.json内容:
{ "name": "test", "version": "1.0.0", "description": "NodeJS Application Accessing The testnet chaincode on Fabric Network", "main": "index.js", "scripts": { "test": "rm -rf identity && node index.js" }, "author": "sujan", "dependencies": { "cors": "^2.8.5", "express": "^4.18.2", "expressjs": "^1.0.1", "fabric-client": "~1.4.4", "fabric-network": "^2.2.0", "js-yaml": "^3.12.0", "pug": "^3.0.2" }, "devDependencies": { "eslint": "^5.6.0" }, "license": "ISC" }
解决方案
原因分析
Hyperledger Fabric 2.x版本的fabric-network库废弃了旧的FileSystemWallet和X509WalletMixin API,改用全新的钱包接口;同时项目混用1.4.x版本的fabric-client与2.2版本的fabric-network,存在版本冲突问题。
修改步骤
- 更新身份添加代码,适配Fabric 2.x新API:
'use strict'; const fs = require('fs'); const { Wallets, X509Identity } = require('fabric-network'); async function main(certificatePath, privateKeyPath) { try { // 实例化文件系统钱包(2.x版本需异步调用) const wallet = await Wallets.newFileSystemWallet(`./identity/org1`); const certificate = fs.readFileSync(certificatePath).toString(); const privateKey = fs.readFileSync(privateKeyPath).toString(); const identityLabel = `org1_admin`; // 按2.x格式构造X.509身份 const identity = { credentials: { certificate: certificate, privateKey: privateKey, }, mspId: 'org1MSP', type: 'X.509', }; // 将身份存入钱包(使用put方法替代原import) await wallet.put(identityLabel, identity); console.log(`身份 ${identityLabel} 已成功导入钱包`); } catch (error) { console.log(`添加到钱包时出错: ${error}`); console.log(error.stack); throw new Error(error); } } module.exports.execute = main;
- 清理package.json依赖,移除过时的
fabric-client避免版本冲突:
修改后的dependencies部分:
"dependencies": { "cors": "^2.8.5", "express": "^4.18.2", "fabric-network": "^2.2.0", "js-yaml": "^3.12.0", "pug": "^3.0.2" }
(注:expressjs为冗余依赖,也可一并移除)
- 重新安装依赖:
npm install
内容的提问来源于stack exchange,提问作者sujan
相关产品推荐
相关产品推荐

