-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (41 loc) · 1.43 KB
/
index.js
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
47
48
49
50
51
52
const request = require('request');
const crypto = require('crypto');
const fs = require('fs');
module.exports = antOpenUnionBlockchain;
function antOpenUnionBlockchain(options, callback) {
if(!options.AccessId) return callback('Please provide AccessId.');
if(!options.AccessKey) return callback('Please provide AccessKey Path.');
this.data = {
AccessId: options.AccessId,
AccessKey: options.AccessKey
}
return callback(null, 'Configured successfully')
}
antOpenUnionBlockchain.prototype.shakeHand=function(callback){
var privateKey = fs.readFileSync(this.data.AccessKey).toString();
const timestamp = Date.parse(new Date()).toString();
const message = this.data.AccessId+timestamp;
let sign = crypto.createSign('RSA-SHA256');
sign.update(new Buffer(message, 'utf-8'));
let sig = sign.sign(privateKey, 'hex');
let postdata={"accessId":this.data.AccessId,"time":timestamp,"secret":sig};
this.callApi("api/contract/shakeHand",postdata, (err, result) => {
if(err) throw err;
return callback(null,result);
});
}
antOpenUnionBlockchain.prototype.callApi = function(cmd,postdata,callback){
request({
rejectUnauthorized :false,
uri: 'https://rest.baas.alipay.com/'+cmd,
method: 'POST',
json:true,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: postdata
}, (err, res, body) => {
if(err) return callback(err);
return callback(null, body);
});
}