This commit is contained in:
sibukixxx 2019-12-12 14:13:57 +09:00
parent 0d7c7a1643
commit 49650868a7
26 changed files with 3282 additions and 143 deletions

View file

@ -0,0 +1,49 @@
{
"name": "first-network-org1",
"version": "1.0.0",
"client": {
"organization": "Org1",
"connection": {
"timeout": {
"peer": {
"endorser": "300"
}
}
}
},
"organizations": {
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
],
"certificateAuthorities": [
"ca.org1.example.com"
]
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpc://34.84.165.140:30110",
"tlsCACerts": {
"pem": "-----BEGIN CERTIFICATE-----\nMIICWDCCAf6gAwIBAgIRALJY+dA4GIVe9ywwcmUWmd8wCgYIKoZIzj0EAwIwdjEL\nMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBG\ncmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHzAdBgNVBAMTFnRs\nc2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTkxMTI3MDU1ODAwWhcNMjkxMTI0MDU1\nODAwWjB2MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UE\nBxMNU2FuIEZyYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0G\nA1UEAxMWdGxzY2Eub3JnMS5leGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqGSM49\nAwEHA0IABOhD2K41HVjl/F8yGyLZXvCpvUC4iGliZHynpRRkcfxwJ4/RC1lpCPT/\nYBOl8Uw7l1sKuE6b8YFVNS9ETkeK5zWjbTBrMA4GA1UdDwEB/wQEAwIBpjAdBgNV\nHSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUwAwEB/zApBgNV\nHQ4EIgQgYmRZVerk8L3CerXqP/WwQiIRe0YPp32hSeEI2Zuz75EwCgYIKoZIzj0E\nAwIDSAAwRQIhAIonZp1IizVOmyTM2SSdIZJ4RLa9xOO14gkuFEsbzi8yAiB9JJ1M\nIdNfUSF4Ft7CzA1k4utfv1T7clCTFPvO663L7g==\n-----END CERTIFICATE-----\n"
},
"grpcOptions": {
"ssl-target-name-override": "peer0.org1.example.com",
"hostnameOverride": "peer0.org1.example.com"
}
}
},
"certificateAuthorities": {
"ca.org1.example.com": {
"url": "http://34.84.165.140:30054",
"caName": "fabric-ca-server",
"tlsCACerts": {
"pem": "-----BEGIN CERTIFICATE-----\nMIICUTCCAfegAwIBAgIQQjxDrTT3lL+VIl0iT/YlHzAKBggqhkjOPQQDAjBzMQsw\nCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\nYW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu\nb3JnMS5leGFtcGxlLmNvbTAeFw0xOTExMjcwNTU4MDBaFw0yOTExMjQwNTU4MDBa\nMHMxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T\nYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcxLmV4YW1wbGUuY29tMRwwGgYDVQQD\nExNjYS5vcmcxLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE\nBwtbQK6RDJATyDGGGhE/o9owRT1dctmfTmXze64U3bJuZhWD1ikReRPxleoYye+j\nTiw74wh9dErhmRHkwFFGe6NtMGswDgYDVR0PAQH/BAQDAgGmMB0GA1UdJQQWMBQG\nCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdDgQiBCAY\niqiaL3YArjMsizqR6bqEdWsFl6PXSRWHj42BWJRAmjAKBggqhkjOPQQDAgNIADBF\nAiEAnGR+dUamftCLU0QAeuCFxQq/8SRFMtvbvYSzIPix8TICIHmlj7+EXKsR2n3N\naX4g/6pNDhfMNHCCihBtGc3cooQP\n-----END CERTIFICATE-----\n"
},
"httpOptions": {
"verify": false
}
}
}
}

View file

@ -1,48 +1,77 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'; 'use strict';
/*
const FabricCAServices = require('fabric-ca-client'); * Copyright IBM Corp All Rights Reserved
const { FileSystemWallet, X509WalletMixin } = require('fabric-network'); *
const fs = require('fs'); * SPDX-License-Identifier: Apache-2.0
const path = require('path'); */
/*
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json'); * Enroll the admin user
const ccpJSON = fs.readFileSync(ccpPath, 'utf8'); */
const ccp = JSON.parse(ccpJSON);
var Fabric_Client = require('fabric-client');
async function main() { var Fabric_CA_Client = require('fabric-ca-client');
try {
var path = require('path');
// Create a new CA client for interacting with the CA. var util = require('util');
const caInfo = ccp.certificateAuthorities['ca.org1.example.com']; var os = require('os');
const caTLSCACerts = caInfo.tlsCACerts.pem; var fs = require('fs');
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
//
// Create a new file system based wallet for managing identities. var fabric_client = new Fabric_Client();
const walletPath = path.join(process.cwd(), 'wallet'); var fabric_ca_client = null;
const wallet = new FileSystemWallet(walletPath); var admin_user = null;
console.log(`Wallet path: ${walletPath}`); var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
// Check to see if we've already enrolled the admin user. console.log(' Store path:'+store_path);
const adminExists = await wallet.exists('admin');
if (adminExists) { // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
console.log('An identity for the admin user "admin" already exists in the wallet'); Fabric_Client.newDefaultKeyValueStore({ path: store_path
return; }).then((state_store) => {
} // assign the store to the fabric client
fabric_client.setStateStore(state_store);
// Enroll the admin user, and import the new identity into the wallet. var crypto_suite = Fabric_Client.newCryptoSuite();
const enrollment = await ca.enroll({ enrollmentID: 'admin', enrollmentSecret: 'adminpw' }); // use the same location for the state store (where the users' certificate are kept)
const identity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes()); // and the crypto store (where the users' keys are kept)
await wallet.import('admin', identity); var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
console.log('Successfully enrolled admin user "admin" and imported it into the wallet'); crypto_suite.setCryptoKeyStore(crypto_store);
fabric_client.setCryptoSuite(crypto_suite);
} catch (error) { // var tlsOptions = {
console.error(`Failed to enroll admin user "admin": ${error}`); // trustedRoots: [fs.readFileSync('/home/ec2-user/managedblockchain-tls-chain.pem')],
process.exit(1); // verify: true
// };
// be sure to change the http to https when the CA is running TLS enabled
fabric_ca_client = new Fabric_CA_Client('http://35.189.137.237:30054');
// first check to see if the admin is already enrolled
return fabric_client.getUserContext('admin2', true);
}).then((user_from_store) => {
if (user_from_store && user_from_store.isEnrolled()) {
console.log('Successfully loaded admin from persistence');
admin_user = user_from_store;
return null;
} else {
// need to enroll it with CA server
return fabric_ca_client.enroll({
enrollmentID: 'testnetwork',
enrollmentSecret: 'testnetwork'
}).then((enrollment) => {
console.log('Successfully enrolled admin user "admin"');
return fabric_client.createUser(
{username: 'admin2',
mspid: 'Org1MSP',
cryptoContent: { privateKeyPEM: enrollment.key.toBytes(),
signedCertPEM: enrollment.certificate }
});
}).then((user) => {
admin_user = user;
return fabric_client.setUserContext(admin_user);
}).catch((err) => {
console.error('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err);
throw new Error('Failed to enroll admin');
});
} }
} }).then(() => {
console.log('Assigned the admin user to the fabric client ::' + admin_user.toString());
main(); }).catch((err) => {
console.error('Failed to enroll admin: ' + err);
});

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg6mhnlu6HYuVNKmsa
JLxUTX4bw4w0SvjzCRCkBlwR31ahRANCAAS69kNE+DxNm8XZ7Q93MCkOLYMfGPA9
Mqc9KNwjq6xkVo0QgrmryIXapEOEZzMz984mbt04LhsnEQ1ubOkMhWyk
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEuvZDRPg8TZvF2e0PdzApDi2DHxjw
PTKnPSjcI6usZFaNEIK5q8iF2qRDhGczM/fOJm7dOC4bJxENbmzpDIVspA==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgOKOhezCD+Jq8YQgx
M6TfkC/U8MezbkwBf/luMqiaGBmhRANCAASVym+iXI3B+BRf6Q+Js0s+Hn3VxGOb
ZCreWT6JjhNRvXjp9Cj9dnGYFz/Uu1oE5YqIcsDATQ0U1I43iG+o8uEf
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElcpvolyNwfgUX+kPibNLPh591cRj
m2Qq3lk+iY4TUb146fQo/XZxmBc/1LtaBOWKiHLAwE0NFNSON4hvqPLhHw==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgwu7V2GfSVvqBIADZ
HbpA6g8qbe6HArpiAYAYh9Gj5+GhRANCAATymLdWazHYg3DQ1yhP5sEiXZniwt7P
WotiPdLOWuamJT73cuixHiSkE/Tp2YS2SD0Dv9gMADcPkdNMdfLykiBN
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8pi3Vmsx2INw0NcoT+bBIl2Z4sLe
z1qLYj3SzlrmpiU+93LosR4kpBP06dmEtkg9A7/YDAA3D5HTTHXy8pIgTQ==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgKYY17lO5D6JA+1iF
+Th5IDbsBg/AnoY7/66lw73+U6+hRANCAATdqvzC0ibzsJLMIEEdFE9ZOAr1LyW6
NXeGTi53IMjp1l2pG/Z+zKUtcAZzSwHAfQ6hZtRzd83PKyG/3uBmb4bS
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3ar8wtIm87CSzCBBHRRPWTgK9S8l
ujV3hk4udyDI6dZdqRv2fsylLXAGc0sBwH0OoWbUc3fNzyshv97gZm+G0g==
-----END PUBLIC KEY-----

View file

@ -0,0 +1 @@
{"name":"admin","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"637bc77c77ceaddacf93646c78160c556602735a6ce02ffb80f3b15fd8cdc18b","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIIB8TCCAZegAwIBAgIUCogaj0i33pft26pM8MMjfShzWt0wCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA4NDkwMFoXDTIwMTIxMDA4NTQwMFowJzEPMA0G\nA1UECxMGY2xpZW50MRQwEgYDVQQDEwt0ZXN0bmV0d29yazBZMBMGByqGSM49AgEG\nCCqGSM49AwEHA0IABN2q/MLSJvOwkswgQR0UT1k4CvUvJbo1d4ZOLncgyOnWXakb\n9n7MpS1wBnNLAcB9DqFm1HN3zc8rIb/e4GZvhtKjYDBeMA4GA1UdDwEB/wQEAwIH\ngDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBTeR8hT53KJFH+JrIFL1v/YUCBCwzAf\nBgNVHSMEGDAWgBSIjvDHx/b/TgeQambUxAAcI6yO7TAKBggqhkjOPQQDAgNIADBF\nAiEAk43B4gLp1PXMfO7clz84wtVDUizXimlpr+wAzLMyNt0CIDswPzod9ZsHwHuP\njcL07C3nTWEGCyE39ilRCtuGldfQ\n-----END CERTIFICATE-----\n"}}}

View file

@ -0,0 +1 @@
{"name":"admin2","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"e13be659046748b3e0c413ba75b9c27cb5487e440c5172dde307477e4222efdc","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIIB8TCCAZegAwIBAgIUBfcXoQttOVr+1einWFbW1g/8KyAwCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA4NTAwMFoXDTIwMTIxMDA4NTUwMFowJzEPMA0G\nA1UECxMGY2xpZW50MRQwEgYDVQQDEwt0ZXN0bmV0d29yazBZMBMGByqGSM49AgEG\nCCqGSM49AwEHA0IABGAdwh7cOnQ1fwdcCzik0g99mlROB0vuFXX9elsImjKEW83D\nEyLWn+XQDCauGPBA0BxlvZus+cDgIh7MkFdNtI2jYDBeMA4GA1UdDwEB/wQEAwIH\ngDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBQrhHfx0OKu1hohu6qZdJ/pMPaqAjAf\nBgNVHSMEGDAWgBSIjvDHx/b/TgeQambUxAAcI6yO7TAKBggqhkjOPQQDAgNIADBF\nAiEA4t4cRcXsQ93nJuwWFUTe4qVxbL+JxuJUS9IHiaqztzsCIElBnOD2UdVRZerm\nxVhkW2D6Kqg0S5vq2DWvs0TyaYxX\n-----END CERTIFICATE-----\n"}}}

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQglN+iv/SgdyqterDf
HXgXmS6kd0yfDprB/1jJ7utMaoChRANCAATLkK6/Rr4UE8oL4K6dUlBNNVZE9Ey1
0GqIBd3l99MOO4QS58B5LdT8NjJXacLHFiTe381BkZl5DjIoEH2qrIaf
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEy5Cuv0a+FBPKC+CunVJQTTVWRPRM
tdBqiAXd5ffTDjuEEufAeS3U/DYyV2nCxxYk3t/NQZGZeQ4yKBB9qqyGnw==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg7fO7yG14dSygk4Tr
sRdsgYiyMtzGJjMQSIbyGMDaXnyhRANCAARgHcIe3Dp0NX8HXAs4pNIPfZpUTgdL
7hV1/XpbCJoyhFvNwxMi1p/l0AwmrhjwQNAcZb2brPnA4CIezJBXTbSN
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEYB3CHtw6dDV/B1wLOKTSD32aVE4H
S+4Vdf16WwiaMoRbzcMTItaf5dAMJq4Y8EDQHGW9m6z5wOAiHsyQV020jQ==
-----END PUBLIC KEY-----

View file

@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgkbvWLZp06wWkP4DL
D3Z2VIiFw152Se0u3ML8LXV/a8ehRANCAAQDE+69ipzWA/ekcLJsyAw9M/xiOw1g
DnwowzTQSUcZXR94igj3lf41Er9tYNkegt6vEAzO3aXoR9gOAIvjWQIo
-----END PRIVATE KEY-----

View file

@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAxPuvYqc1gP3pHCybMgMPTP8YjsN
YA58KMM00ElHGV0feIoI95X+NRK/bWDZHoLerxAMzt2l6EfYDgCL41kCKA==
-----END PUBLIC KEY-----

View file

@ -0,0 +1 @@
{"name":"peer01-org1","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"cc4ca3569fb1e475c7c849b8ec90aa558b3ca7cb8cdfbf33e4472e8d40487666","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICZDCCAgqgAwIBAgIUSSKfkW9QzdX+OgHgtTLUHW5y/+QwCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA4NTYwMFoXDTIwMTIxMDA5MDEwMFowNDEcMA0G\nA1UECxMGY2xpZW50MAsGA1UECxMEb3JnMTEUMBIGA1UEAxMLcGVlcjAxLW9yZzEw\nWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATLkK6/Rr4UE8oL4K6dUlBNNVZE9Ey1\n0GqIBd3l99MOO4QS58B5LdT8NjJXacLHFiTe381BkZl5DjIoEH2qrIafo4HFMIHC\nMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBTy8Y7qjzOe\n0unIKb+KGP7ltJJXADAfBgNVHSMEGDAWgBSIjvDHx/b/TgeQambUxAAcI6yO7TBi\nBggqAwQFBgcIAQRWeyJhdHRycyI6eyJoZi5BZmZpbGlhdGlvbiI6Im9yZzEiLCJo\nZi5FbnJvbGxtZW50SUQiOiJwZWVyMDEtb3JnMSIsImhmLlR5cGUiOiJjbGllbnQi\nfX0wCgYIKoZIzj0EAwIDSAAwRQIhALFAWJ9jfE+Bfpf6N4roVab9jV5KQ/syq76+\n/mJqgQ0bAiA1uZCaEoCirO/P+Wr4AjIjLNBu/SM6RMjDwcFDf8ahjw==\n-----END CERTIFICATE-----\n"}}}

View file

@ -0,0 +1 @@
{"name":"peer1-org1-user1","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"f6a3153babf1d944e80f09ad047c412ebaa59f8c5864cdc24825cce51f36f104","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICbTCCAhSgAwIBAgIUTVbGd5fyDa8idM5ItN7SbKHTJcswCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA3MDgwMFoXDTIwMTIxMDA3MTMwMFowOTEcMA0G\nA1UECxMGY2xpZW50MAsGA1UECxMEb3JnMTEZMBcGA1UEAxMQcGVlcjEtb3JnMS11\nc2VyMTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABAMT7r2KnNYD96RwsmzIDD0z\n/GI7DWAOfCjDNNBJRxldH3iKCPeV/jUSv21g2R6C3q8QDM7dpehH2A4Ai+NZAiij\ngcowgccwDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFF3t\nCAddOcKlejqsStZJd0vhYuJjMB8GA1UdIwQYMBaAFIiO8MfH9v9OB5BqZtTEABwj\nrI7tMGcGCCoDBAUGBwgBBFt7ImF0dHJzIjp7ImhmLkFmZmlsaWF0aW9uIjoib3Jn\nMSIsImhmLkVucm9sbG1lbnRJRCI6InBlZXIxLW9yZzEtdXNlcjEiLCJoZi5UeXBl\nIjoiY2xpZW50In19MAoGCCqGSM49BAMCA0cAMEQCICqxxFNOmYzEK7UB53AiPkRI\nIzTsaikuCKkCULjuTrU4AiBSDv9l5L2nhy4aengjHSYmp0zFaE4AzoOXT6BB9XKD\nCw==\n-----END CERTIFICATE-----\n"}}}

View file

@ -0,0 +1 @@
{"name":"peer1-org1-user2","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"24020a933b56a529acbf2db369159c9130797623d0797ae85d1183fcd22f6c52","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICbTCCAhSgAwIBAgIUQNgr02FIC+hW2jM6ESqdSBILcg8wCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA5MzQwMFoXDTIwMTIxMDA5MzkwMFowOTEcMA0G\nA1UECxMGY2xpZW50MAsGA1UECxMEb3JnMTEZMBcGA1UEAxMQcGVlcjEtb3JnMS11\nc2VyMjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABLr2Q0T4PE2bxdntD3cwKQ4t\ngx8Y8D0ypz0o3COrrGRWjRCCuavIhdqkQ4RnMzP3ziZu3TguGycRDW5s6QyFbKSj\ngcowgccwDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFFAK\nTQVKr3rhGYEdInF6Cz7GhU1bMB8GA1UdIwQYMBaAFIiO8MfH9v9OB5BqZtTEABwj\nrI7tMGcGCCoDBAUGBwgBBFt7ImF0dHJzIjp7ImhmLkFmZmlsaWF0aW9uIjoib3Jn\nMSIsImhmLkVucm9sbG1lbnRJRCI6InBlZXIxLW9yZzEtdXNlcjIiLCJoZi5UeXBl\nIjoiY2xpZW50In19MAoGCCqGSM49BAMCA0cAMEQCIAgPxHBuQqJNfNDzFtSgYHxm\nGfW9gcJkBT7FzfIeb9GBAiAtnQFy2WXnQQzqhaGRHAxlrXBQx7k5SCydua/Wns2h\nqA==\n-----END CERTIFICATE-----\n"}}}

View file

@ -0,0 +1 @@
{"name":"peer3-org1","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"4b7b7e68ad61970a0d534ebce961f186e451841025daadfb51653912df4f6245","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICYTCCAgigAwIBAgIUb5AaIsJUeTBcC5pIABm2eCv83+owCgYIKoZIzj0EAwIw\naDELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK\nEwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMRkwFwYDVQQDExBmYWJyaWMt\nY2Etc2VydmVyMB4XDTE5MTIxMTA4NDQwMFoXDTIwMTIxMDA4NDkwMFowMzEcMA0G\nA1UECxMGY2xpZW50MAsGA1UECxMEb3JnMTETMBEGA1UEAxMKcGVlcjMtb3JnMTBZ\nMBMGByqGSM49AgEGCCqGSM49AwEHA0IABPKYt1ZrMdiDcNDXKE/mwSJdmeLC3s9a\ni2I90s5a5qYlPvdy6LEeJKQT9OnZhLZIPQO/2AwANw+R00x18vKSIE2jgcQwgcEw\nDgYDVR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFKrNwOVwrR46\n/8GA6Xct0Ijd6iG8MB8GA1UdIwQYMBaAFIiO8MfH9v9OB5BqZtTEABwjrI7tMGEG\nCCoDBAUGBwgBBFV7ImF0dHJzIjp7ImhmLkFmZmlsaWF0aW9uIjoib3JnMSIsImhm\nLkVucm9sbG1lbnRJRCI6InBlZXIzLW9yZzEiLCJoZi5UeXBlIjoiY2xpZW50In19\nMAoGCCqGSM49BAMCA0cAMEQCICe1QOUf4VVCfRzomUuqw8cJ1Yp6g1+067cT4Mss\n/BuFAiAKMLLUiap9qPE6kpSE7Tyynl2AAWggGjGRLnbtW7BaTA==\n-----END CERTIFICATE-----\n"}}}

View file

@ -1,53 +1,151 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'; 'use strict';
const { FileSystemWallet, Gateway } = require('fabric-network'); var Fabric_Client = require('fabric-client');
const path = require('path'); var Fabric_CA_Client = require('fabric-ca-client');
var path = require('path');
var util = require('util');
var os = require('os');
var fs = require('fs');
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json'); var fabric_client = new Fabric_Client();
async function main() { // setup the fabric network
try { var channel = fabric_client.newChannel('newchannel');
var peer = fabric_client.newPeer('grpc://34.84.165.140:30110');
channel.addPeer(peer);
var order = fabric_client.newOrderer('grpc://34.84.165.140:31010');
channel.addOrderer(order);
// Create a new file system based wallet for managing identities. // =================================================================
const walletPath = path.join(process.cwd(), 'wallet'); var member_user = null;
const wallet = new FileSystemWallet(walletPath); var store_path = path.join(__dirname, 'hfc-key-store');
console.log(`Wallet path: ${walletPath}`); console.log('Store path:'+store_path);
var tx_id = null;
// 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. Fabric_Client.newDefaultKeyValueStore({ path: store_path
const gateway = new Gateway(); }).then((state_store) => {
await gateway.connect(ccpPath, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } }); fabric_client.setStateStore(state_store);
var crypto_suite = Fabric_Client.newCryptoSuite();
// Get the network (channel) our contract is deployed to. var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
const network = await gateway.getNetwork('mychannel'); crypto_suite.setCryptoKeyStore(crypto_store);
fabric_client.setCryptoSuite(crypto_suite);
// Get the contract from the network.
const contract = network.getContract('fabcar'); return fabric_client.getUserContext('peer1-org1-user2', true);
}).then((user_from_store) => {
// Submit the specified transaction. if (user_from_store && user_from_store.isEnrolled()) {
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') console.log('Successfully loaded peer1-org1-user2 from persistence');
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave') member_user = user_from_store;
await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); } else {
console.log('Transaction has been submitted'); throw new Error('Failed to get peer1-org1-user2.... run registerUser.js');
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
} }
}
tx_id = fabric_client.newTransactionID();
console.log("Assigning transaction_id: ", tx_id._transaction_id);
var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'myccc',
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
// chainId: 'sychannel',
txId: tx_id
};
// send the transaction proposal to the peers
return channel.sendTransactionProposal(request);
}).then((results) => {
var proposalResponses = results[0];
var proposal = results[1];
let isProposalGood = false;
main(); console.log(proposal)
console.log(proposalResponses)
if (proposalResponses && proposalResponses[0].response &&
proposalResponses[0].response.status === 200) {
isProposalGood = true;
console.log('Transaction proposal was good');
} else {
console.error('Transaction proposal was bad');
}
if (isProposalGood) {
console.log(util.format(
'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
proposalResponses[0].response.status, proposalResponses[0].response.message));
// build up the request for the orderer to have the transaction committed
var request = {
proposalResponses: proposalResponses,
proposal: proposal
};
// set the transaction listener and set a timeout of 30 sec
// if the transaction did not get committed within the timeout period,
// report a TIMEOUT status
var transaction_id_string = tx_id.getTransactionID(); //Get the transaction ID string to be used by the event processing
var promises = [];
var sendPromise = channel.sendTransaction(request);
promises.push(sendPromise); //we want the send transaction first, so that we know where to check status
// get an eventhub once the fabric client has a user assigned. The user
// is required bacause the event registration must be signed
let event_hub = channel.newChannelEventHub(peer);
// using resolve the promise so that result status may be processed
// under the then clause rather than having the catch clause process
// the status
let txPromise = new Promise((resolve, reject) => {
let handle = setTimeout(() => {
event_hub.unregisterTxEvent(transaction_id_string);
event_hub.disconnect();
resolve({event_status : 'TIMEOUT'}); //we could use reject(new Error('Trnasaction did not complete within 30 seconds'));
}, 3000);
event_hub.registerTxEvent(transaction_id_string, (tx, code) => {
// this is the callback for transaction event status
// first some clean up of event listener
clearTimeout(handle);
// now let the application know what happened
var return_status = {event_status : code, tx_id : transaction_id_string};
if (code !== 'VALID') {
console.error('The transaction was invalid, code = ' + code);
resolve(return_status); // we could use reject(new Error('Problem with the tranaction, event status ::'+code));
} else {
console.log('The transaction has been committed on peer ' + event_hub.getPeerAddr());
resolve(return_status);
}
}, (err) => {
//this is the callback if something goes wrong with the event registration or processing
reject(new Error('There was a problem with the eventhub ::'+err));
},
{disconnect: true} //disconnect when complete
);
event_hub.connect();
});
promises.push(txPromise);
return Promise.all(promises);
} else {
console.error('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
throw new Error('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
}
}).then((results) => {
console.log('Send transaction promise and event listener promise have completed');
// check the results in the order the promises were added to the promise all list
if (results && results[0] && results[0].status === 'SUCCESS') {
console.log('Successfully sent transaction to the orderer.');
} else {
console.error('Failed to order the transaction. Error code: ' + results[0].status);
}
if(results && results[1] && results[1].event_status === 'VALID') {
console.log('Successfully committed the change to the ledger by the peer');
} else {
console.log('Transaction failed to be committed to the ledger due to ::'+results[1].event_status);
}
}).catch((err) => {
console.error('Failed to invoke successfully :: ' + err);
});

View file

@ -16,6 +16,7 @@
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"fabric-ca-client": "~1.4.0", "fabric-ca-client": "~1.4.0",
"fabric-client": "^1.4.4",
"fabric-network": "~1.4.0" "fabric-network": "~1.4.0"
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,56 +1,82 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'; 'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Register and Enroll a user
*/
var Fabric_Client = require('fabric-client');
var Fabric_CA_Client = require('fabric-ca-client');
var path = require('path');
var util = require('util');
var os = require('os');
var fs = require('fs');
//
var fabric_client = new Fabric_Client();
var fabric_ca_client = null;
var admin_user = null;
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
console.log(' Store path:'+store_path);
// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
Fabric_Client.newDefaultKeyValueStore({ path: store_path
}).then((state_store) => {
// assign the store to the fabric client
fabric_client.setStateStore(state_store);
var crypto_suite = Fabric_Client.newCryptoSuite();
// use the same location for the state store (where the users' certificate are kept)
// and the crypto store (where the users' keys are kept)
var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
crypto_suite.setCryptoKeyStore(crypto_store);
fabric_client.setCryptoSuite(crypto_suite);
const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network'); // be sure to change the http to https when the CA is running TLS enabled
const path = require('path'); fabric_ca_client = new Fabric_CA_Client('http://35.189.137.237:30054');
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json'); // first check to see if the admin is already enrolled
return fabric_client.getUserContext('admin', true);
async function main() { }).then((user_from_store) => {
try { if (user_from_store && user_from_store.isEnrolled()) {
console.log('Successfully loaded admin from persistence');
// Create a new file system based wallet for managing identities. admin_user = user_from_store;
const walletPath = path.join(process.cwd(), 'wallet'); } else {
const wallet = new FileSystemWallet(walletPath); throw new Error('Failed to get admin.... run enrolladmin.js');
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(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: 'user1', role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes());
await 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);
} }
}
// at this point we should have the admin user
main(); // first need to register the user with the CA server
return fabric_ca_client.register({
enrollmentID: 'peer1-org1-user2',
affiliation: 'org1', role: 'client'}, admin_user);
}).then((secret) => {
// next we need to enroll the user with CA server
console.log('Successfully registered peer1-org1-user2 - secret:'+ secret);
return fabric_ca_client.enroll({enrollmentID: 'peer1-org1-user2', enrollmentSecret: secret});
}).then((enrollment) => {
console.log('Successfully enrolled member user "peer1-org1-user2" ');
return fabric_client.createUser(
{username: 'peer1-org1-user2',
mspid: 'Org1MSP',
cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }
});
}).then((user) => {
member_user = user;
return fabric_client.setUserContext(member_user);
}).then(()=>{
console.log('peer1-org1-user2 was successfully registered and enrolled and is ready to interact with the fabric network');
}).catch((err) => {
console.error('Failed to register: ' + err);
if(err.toString().indexOf('Authorization') > -1) {
console.error('Authorization failures may be caused by having admin credentials from a previous CA instance.\n' +
'Try again after deleting the contents of the store directory '+store_path);
}
});

2867
fabcar/javascript/yarn.lock Normal file

File diff suppressed because it is too large Load diff