hyperledger fabric nodejs SDK 개발 (二) --- SDK 사용자 등록 메커니즘

SDK 사용자 등록 메커니즘
'use strict';

/*

 * 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 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);

    var   tlsOptions = {

             trustedRoots: [],

             verify: false

    };

    // be sure to change the http to https when the CA is running TLS enabled    IP        ,    CA  ,     CA    

    fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', null , '', crypto_suite);

    //           

    // first check to see if the admin is already enrolled

    return fabric_client.getUserContext('admin', 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;

    } else {

        throw new Error('Failed to get admin.... run enrollAdmin.js');  //         

    }

    // at this point we should have the admin user              

    // first need to register the user with the CA server   CA  ,     、    、         

    return fabric_ca_client.register({enrollmentID: 'user1', affiliation: 'org1.department1'}, admin_user);

}).then((secret) => {   //         ,       

    // next we need to enroll the user with CA server

    console.log('Successfully registered user1 - secret:'+ secret);

            ID

    return fabric_ca_client.enroll({enrollmentID: 'user1', enrollmentSecret: secret});

}).then((enrollment) => {  //      ,    

  console.log('Successfully enrolled member user "user1" ');

  return fabric_client.createUser(  //           ,     、     、                。

     {username: 'user1',        

     mspid: 'Org1MSP',

     cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }

     });

}).then((user) => {

     member_user = user;  //         



     return fabric_client.setUserContext(member_user);  //             

}).then(()=>{

     console.log('User1 was successfully registered and enrolled and is ready to intreact 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.
' +                   'Try again after deleting the contents of the store directory '+store_path);          } });

흐름:
파일 경 로 를 설정 하고 암호 화 모듈 을 설정 하여 둘 을 연결 하여 암호 화 모듈 인 스 턴 스 를 생 성 한 다음 에 CA 서비스 가 존재 하 는 IP, TLS 설정 과 결합 하여 CA 서비스 인 스 턴 스 를 생 성하 고 관리자 등록 여 부 를 검사 하 며 등록 하면 관리자 로 연결 하고 CA 서 비 스 를 통 해 사용자 이름, 부속 조직, 관리 자 를 등록 하 며 성공 하면 밀 스푼 을 생 성 합 니 다.스푼 과 ID 를 통 해 사용 자 를 등록 하고 등록 정 보 를 통 해 사용자 인 스 턴 스 를 생 성 한 다음 에 이 사용 자 를 일반 사용자 로 설정 하 는 동시에 현재 사용 자 를 클 라 이언 트 의 인 스 턴 스 로 설정 합 니 다. 오류 가 발생 하면 인쇄 오류 가 발생 합 니 다.

좋은 웹페이지 즐겨찾기