为基于 Hyperledger Fabric v0.6 的区块链网络开发客户端应用程序(9)
- UID
- 1066743
|
为基于 Hyperledger Fabric v0.6 的区块链网络开发客户端应用程序(9)
登记用户这是 doLogin 方法的代码段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| var chain = bcNetwork.getChain();
var chainAsync = Promise.promisifyAll(chain);
chainAsync.getMemberAsync(username)
.then(function(member){
var memberAsync = Promise.promisifyAll(member);
return memberAsync.enrollAsync(password);
})
.then(function(crypto){
logHelper.logMessage(logger, 'doLogin', 'Successfully logged in user on blockchain: '+username);
return resolve({statusCode: constants.SUCCESS, body: crypto});
})
.catch(function(err){
logHelper.logError(logger, 'doLogin', 'Could not login user on blockchain: '+username, err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not login user' });
});
|
- 和之前一样,第 1 到 6 行将获取链对象并从链中获取成员对象。
- 第 7 行将调用成员对象上的登记方法,并传入从注册响应中获得的密码。
- 第 9 行成功地从 ECA 获取 ECert 对。
创建抵押贷款申请这是 createMortgageApplication 方法的代码段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| function createMortgageApplication(params) {
return new Promise(function(resolve, reject){
var mortgageApplication;
try{
logHelper.logEntryAndInput(logger, 'createMortgageApplication', params);
if(!validate.isValidJson(params)){
logHelper.logError(logger, 'createMortgageApplication', 'Invalid params');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not create mortgage application. Invalid params' })
}
var user = params.user;
if(!validate.isValidString(user)){
logHelper.logError(logger, 'createMortgageApplication', 'Invalid user');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not create mortgage application. Invalid user' })
}
mortgageApplication = params.mortgageApplication;
if(!validate.isValidJson(mortgageApplication)){
logHelper.logError(logger, 'createMortgageApplication', 'Invalid mortgageApplication');
return reject({statusCode: constants.INVALID_INPUT, body: 'Could not create mortgage application. Invalid mortgageApplication' })
}
var id = mortgageApplication['id'];
var payload = JSON.stringify(mortgageApplication);
var reqSpec = getRequestSpec({functionName: 'CreateLoanApplication', args: [id, payload]});
recursiveInvoke({requestSpec: reqSpec, user: user})
.then(function(resp){
logHelper.logMessage(logger, 'createMortgageApplication', 'Successfully created mortgageApplication', resp.body);
return resolve({statusCode: constants.SUCCESS, body: mortgageApplication});
})
.catch(function(err){
logHelper.logError(logger, 'createMortgageApplication', 'Could not create mortgageApplication', err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not create mortgageApplication' });
});
}
catch(err){
logHelper.logError(logger, 'createMortgageApplication', 'Could not create mortgage application on blockchain ledger: ', err);
return reject({statusCode: constants.INTERNAL_SERVER_ERROR, body: 'Could not create mortgage application' });
}
});
}
|
- 第 7 到第 22 行执行输入验证。该方法需要两个参数:User(将用于调用链代码事务)和要持久保存在区块链上的抵押贷款申请 JSON 内容。
- 第 28 行将创建调用链代码需要使用的请求规范。
- 第 29 行将调用 recursiveInvoke 函数,该函数根据所提供的请求规范来实际调用链代码。
|
|
|
|
|
|