cd ~/go/src/github.com/hyperledger/fabric git clone -b release-1.4 https://github.com/hyperledger/fabric-sdk-node.git
根据package.json安装依赖
1 2 3 4 5 6 7 8
cd ~/go/src/github.com/hyperledger/fabric/fabric-sdk-node cd fabric-common && npm install && cd .. cd fabric-ca-client && npm install && cd .. cd fabric-client && npm install && cd .. cd fabric-network && npm install && cd .. cd ~/go/src/github.com/hyperledger/fabric/fabric-sdk-node npm install
这个过程有可能出错,一般的警告可以忽略。提示需要且未安装的包可自行安装
1
npm install <pacakge>@[version] --save
–save参数是将该包加入package.json的dependencies中。
nodejs脚本编写及运行
1
cd ~/go/src/github.com/hyperledger/fabric/checkinsystem/node_sdk
// Create a new CA client for interacting with the CA. const keyPath = path.join(__dirname, "../../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/5ff3716b887dd7c97b8276eab73bebc95cd1119625110ea0984cc8d559ed16a2_sk"); const keyPEM = Buffer.from(fs.readFileSync(keyPath)).toString(); const certPath = path.join(__dirname, "../../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"); const certPEM = Buffer.from(fs.readFileSync(certPath)).toString();
// 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: ${walletPath}`);
// 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" already exists in the wallet'); return; }
// Enroll the admin user, and import the new identity into the wallet. const identity = X509WalletMixin.createIdentity('Org1MSP', certPEM, keyPEM); wallet.import('admin', identity); console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
} catch (error) { console.error(`Failed to enroll admin user "admin": ${error}`); process.exit(1); } }
asyncfunctionmain() { try { const keyPath = path.join(__dirname, "../../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/cc111873571e461c95bce39dfe59af3bb17d4600643753dca9ff4fd84409c9d1_sk"); const keyPEM = Buffer.from(fs.readFileSync(keyPath)).toString(); const certPath = path.join(__dirname, "../../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem"); const certPEM = Buffer.from(fs.readFileSync(certPath)).toString(); // 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: ${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" 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(path.join(__dirname, './connprofile.json'), { wallet: wallet, identity: 'admin' });
// Register the user, enroll the user, and import the new identity into the wallet. const userIdentity = X509WalletMixin.createIdentity('Org1MSP', certPEM, keyPEM); wallet.import('user1', userIdentity); console.log('Successfully registered and enrolled admin user "user1" and imported it into the wallet');
} catch (error) { console.error(`Failed to register user "user1": ${error}`); process.exit(1); } }
// 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: ${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(path.join(__dirname, './connprofile.json'), { wallet, identity: 'user1', discovery: { enabled: false } });
// 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('checkinsystem');
// 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: ${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(path.join(__dirname, './connprofile.json'), { wallet, identity: 'user1', discovery: { enabled: false } });
// 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('checkinsystem');
// Evaluate the specified transaction. const result = await contract.evaluateTransaction('query','ACCOUNT0'); console.log(`Transaction has been evaluated, result is: ${result.toString()}`);