Hyperledger Fabric SDK 示例《balance-transfer》之《注冊用戶》

前言

上一篇文章我們已經(jīng)啟動了fabric網(wǎng)絡(luò)/node服務(wù)匙赞,這篇文章主要來詳細(xì)分析用戶注冊的過程梨熙。

具體實現(xiàn)

找到app.js中關(guān)于users的路由:

//注冊用戶 
app.post('/users', function(req, res) {
    // 1 參數(shù)校驗
    var username = req.body.username;
    var orgName = req.body.orgName;
    logger.debug('End point : /users');
    logger.debug('User name : ' + username);
    logger.debug('Org name  : ' + orgName);
    if (!username) {
        res.json(getErrorMessage('\'username\''));
        return;
    }
    if (!orgName) {
        res.json(getErrorMessage('\'orgName\''));
        return;
    }
    // 2 生成jwt  添加用戶名以及組織信息
    var token = jwt.sign({
        exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),
        username: username,
        orgName: orgName
    }, app.get('secret'));
    // 3  注冊用戶  如果成功連同jwt一起返回
    helper.getRegisteredUsers(username, orgName, true).then(function(response) {
        if (response && typeof response !== 'string') {
            response.token = token;
            res.json(response);
        } else {
            res.json({
                success: false,
                message: response
            });
        }
    });
});

從上面得知要看具體實現(xiàn),還需要進入helper.js中查看getRegisteredUsers()

var getRegisteredUsers = function(username, userOrg, isJson) {
    var member;
    var client = getClientForOrg(userOrg);
    var enrollmentSecret = null;
//   設(shè)置公私鑰存儲的地方   
    return hfc.newDefaultKeyValueStore({
        //  這里只要求path參數(shù)  來作為存儲的地方
        path: getKeyStoreForOrg(getOrgName(userOrg))
    }).then((store) => {
        //  設(shè)置狀態(tài)持久化存儲(如(證書艇搀,私鑰缴挖。繁成。))
        client.setStateStore(store);
        // clearing the user context before switching
        client._userContext = null;
        //1  獲取用戶信息  (從 內(nèi)存-->statestore  依次查找)
        return client.getUserContext(username, true).then((user) => {
            if (user && user.isEnrolled()) {
                //  從持久化中 成功加載此 user
                logger.info('Successfully loaded member from persistence');
                return user;
            } else {
                let caClient = caClients[userOrg];
                //2  獲得adminuser 來注冊賬戶
                return getAdminUser(userOrg).then(function(adminUserObj) {
                    member = adminUserObj;
                    return caClient.register({
                        enrollmentID: username,
                        //  與此用戶關(guān)聯(lián)的隸屬關(guān)系
                        affiliation: userOrg + '.department1'
                    }, member); // 注冊員
                }).then((secret) => {
                    enrollmentSecret = secret;
                    logger.debug(username + ' registered successfully');
                    //3  注冊成功后  拿著secret  來enroll
                    return caClient.enroll({
                        enrollmentID: username,
                        enrollmentSecret: secret
                    });
                }, (err) => {
                    logger.debug(username + ' failed to register');
                    return '' + err;
                    //return 'Failed to register '+username+'. Error: ' + err.stack ? err.stack : err;
                }).then((message) => {  // enroll 返回的信息
                    if (message && typeof message === 'string' && message.includes(
                            'Error:')) {
                        logger.error(username + ' enrollment failed');
                        return message;
                    }
                    logger.debug(username + ' enrolled successfully');
                    // 構(gòu)建user對象 
                    member = new User(username);
                    member._enrollmentSecret = enrollmentSecret;
                    //4  設(shè)置此User實例的注冊對象  來持久化在client中
                    return member.setEnrollment(message.key, message.certificate, getMspID(userOrg));
                }).then(() => {
                    //5  保存用戶的上下文環(huán)境(e.g 證書 私鑰 ) 用戶簽名 各種請求
                    client.setUserContext(member);
                    return member;
                }, (err) => {
                    logger.error(util.format('%s enroll failed: %s', username, err.stack ? err.stack : err));
                    return '' + err;
                });;
            }
        });
    }).then((user) => {
        // 6 返回用戶信息
        if (isJson && isJson === true) {
            var response = {
                success: true,
                secret: user._enrollmentSecret,
                message: username + ' enrolled Successfully',
            };
            return response;
        }
        return user;
    }, (err) => {
        logger.error(util.format('Failed to get registered user: %s, error: %s', username, err.stack ? err.stack : err));
        return '' + err;
    });
};

基本流程

API訪問

org1組織建立一個用戶名為Jim的用戶。在命令行里輸入:

echo "POST request Enroll on Org1  ..."
echo
ORG1_TOKEN=$(curl -s -X POST \
  http://localhost:4000/users \
  -H "content-type: application/x-www-form-urlencoded" \
  -d 'username=Jim&orgName=org1')    
echo $ORG1_TOKEN

控制面板打印結(jié)果:

POST request Enroll on Org1  ...

{"success":true,"secret":"xDfluXIPURbu","message":"Jim enrolled Successfully","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgxNTkyMjQsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MDgxMjMyMjR9.rE5UuViSzmDcdHNZRXt22izpopY5eVkJ7YbfXILjSQk"}


ORG1 token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDgxNTkyMjQsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE1MDgxMjMyMjR9.rE5UuViSzmDcdHNZRXt22izpopY5eVkJ7YbfXILjSQk


// 上面的token是 訪問后面API的必要參數(shù)

可以看出 Jim已經(jīng)成功被注冊了己儒,其公私鑰存放在/tmp/fabric-client-kvs_peerOrg1中(可在config.json中自定義keyValueStore路徑):

這里寫圖片描述

node服務(wù)后臺打悠槠瘛:

[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - End point : /users
[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - User name : Jim
[2017-10-16 11:07:04.222] [DEBUG] SampleWebApp - Org name  : org1
[2017-10-16 11:07:04.226] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.228] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue
[2017-10-16 11:07:04.230] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.231] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- getValue

//----->公私鑰存放路徑

[2017-10-16 11:07:04.310] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/tmp/fabric-client-kvs_peerOrg1"}}
[2017-10-16 11:07:04.311] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:04.311] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks
[2017-10-16 11:07:04.312] [DEBUG] Helper - [crypto_ecdsa_aes]: generateKey, store.setValue
[2017-10-16 11:07:04.313] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.313] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.319] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue

//----->獲取admin

[2017-10-16 11:07:04.715] [INFO] Helper - Successfully enrolled user 'admin'
[2017-10-16 11:07:04.716] [DEBUG] Helper - Msp ID : Org1MSP
[2017-10-16 11:07:04.717] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:04.721] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:04.722] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:04.723] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.723] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.724] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:04.726] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.726] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.727] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.727] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.728] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 5f8df5637db9f5e75af2990de09b57214b1eefcf834002cd79e6e7a634a23b68
[2017-10-16 11:07:04.728] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: bc8ce6511008bff20bb18ad6f105af889eb3c880cd2275cd97184d3378c47751
[2017-10-16 11:07:04.728] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:04.743] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature:  Signature {
  r: <BN: f1034e3347d230585a94f1ea21318d6217a20774754798641f599e33b8a9571f>,
  s: <BN: 5e4d05280fe82c2bbe93eb0e8fefb8998bacf160b6422e370b561c109b1c0ef2>,
  recoveryParam: 0 }
 
  //-----> Jim  register 成功
  
[2017-10-16 11:07:04.950] [DEBUG] Helper - Jim registered successfully
[2017-10-16 11:07:05.033] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store
[2017-10-16 11:07:05.034] [DEBUG] Helper - [crypto_ecdsa_aes]: generateKey, store.setValue
[2017-10-16 11:07:05.034] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.034] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349

 //-----> Jim  enrolled 成功
 
[2017-10-16 11:07:05.391] [DEBUG] Helper - Jim enrolled successfully
[2017-10-16 11:07:05.391] [DEBUG] Helper - Msp ID : Org1MSP
[2017-10-16 11:07:05.392] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256
[2017-10-16 11:07:05.392] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256
[2017-10-16 11:07:05.392] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start
[2017-10-16 11:07:05.393] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config: fabric-client/lib/impl/FileKeyValueStore.js
[2017-10-16 11:07:05.393] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start
[2017-10-16 11:07:05.393] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular]
[2017-10-16 11:07:05.393] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/home/jiang/.hfc-key-store"}}
[2017-10-16 11:07:05.393] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor
[2017-10-16 11:07:05.394] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks
[2017-10-16 11:07:05.394] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.394] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.395] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
[2017-10-16 11:07:05.396] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.397] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.398] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.398] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.399] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: b3e2a75b9a4fd2b081589b1c45bde49f583aea9d7d466798c7b765c4ca96973d
[2017-10-16 11:07:05.400] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: c58820e5a9db75ab17841ac158e9389ad0d130a6827dc0682dcb2d2c5dccb349
[2017-10-16 11:07:05.400] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市闪湾,隨后出現(xiàn)的幾起案子冲甘,更是在濱河造成了極大的恐慌,老刑警劉巖途样,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件江醇,死亡現(xiàn)場離奇詭異,居然都是意外死亡娘纷,警方通過查閱死者的電腦和手機嫁审,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赖晶,“玉大人律适,你說我怎么就攤上這事《舨澹” “怎么了捂贿?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長胳嘲。 經(jīng)常有香客問我厂僧,道長,這世上最難降的妖魔是什么了牛? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任颜屠,我火速辦了婚禮,結(jié)果婚禮上鹰祸,老公的妹妹穿的比我還像新娘甫窟。我一直安慰自己,他們只是感情好蛙婴,可當(dāng)我...
    茶點故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布粗井。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪浇衬。 梳的紋絲不亂的頭發(fā)上懒构,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天,我揣著相機與錄音耘擂,去河邊找鬼胆剧。 笑死,一個胖子當(dāng)著我的面吹牛醉冤,可吹牛的內(nèi)容都是我干的赞赖。 我是一名探鬼主播,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼冤灾,長吁一口氣:“原來是場噩夢啊……” “哼前域!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起韵吨,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤匿垄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后归粉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體椿疗,經(jīng)...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年糠悼,在試婚紗的時候發(fā)現(xiàn)自己被綠了届榄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡倔喂,死狀恐怖铝条,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情席噩,我是刑警寧澤班缰,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站悼枢,受9級特大地震影響埠忘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜馒索,卻給世界環(huán)境...
    茶點故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一莹妒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绰上,春花似錦旨怠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至疯趟,卻和暖如春拘哨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背信峻。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工倦青, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盹舞。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓产镐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親踢步。 傳聞我的和親對象是個殘疾皇子癣亚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,627評論 2 350

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)获印,斷路器述雾,智...
    卡卡羅2017閱讀 134,637評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,845評論 25 707
  • # Hyperledger Fabric V1.0– 開發(fā)者快速入門 本文檔演示使用Hyperledger Fab...
    自由行走O閱讀 1,316評論 1 7
  • Node.js是目前非常火熱的技術(shù)兼丰,但是它的誕生經(jīng)歷卻很奇特玻孟。 眾所周知,在Netscape設(shè)計出JavaScri...
    w_zhuan閱讀 3,610評論 2 41
  • 獅嶺的謊言需要借口匣掸,誠實無需掩飾! Todywu2013-4-16 上周末交待“女包工廠”何總起四只需要油邊樣辦氮双,...
    todywu閱讀 564評論 0 50