This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
multichain.js
207 lines (193 loc) · 4.4 KB
/
multichain.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//-------------------------------------------------------------------
// Multichain Library
//-------------------------------------------------------------------
var request = require("request");
module.exports = function (creds, opts, logger, cb) {
var multichain = {};
var streamId = opts.streamId || "SAP000S407W212743";
function _init(cb) {
request.post({
uri: creds.url,
json: true,
headers: {
apikey: creds.api_key
},
body: {
method: "create",
params: [
"stream",
streamId,
true
]
}
}, function (err, res, body){
logger.info("[multichain.js] create stream \"" + streamId + "\": " + (typeof body === "object" ? JSON.stringify(body) : body));
cb();
});
}
_init(function() {
/**
* Write to the given multichain stream.
*/
multichain.write = function (options, cb) {
request.post({
uri: creds.url,
json: true,
headers: {
apikey: creds.api_key
},
body: {
"method": "liststreamkeyitems",
"params": [
streamId,
options.args.assetId,
false,
1,
-1
]
}
}, function (err, res, body){
if (err) {
if(typeof err !== "string") {
err = JSON.stringify(err);
}
return cb(err);
}
var value = parseInt(options.args.assetValue);
var current = 0;
var unit = options.args.assetUnit;
current = (body && body.result && body.result[0]) ? body.result[0].data.hexDecode() : "0";
current = parseInt(current);
if (unit.startsWith("mi")) {
value = Math.floor(value * 1.609344);
}
if (current === value) {
return cb("Warning: The blockchain is up-to-date");
}
if (current >= value) {
return cb("Error: Rejecting value for odometer");
}
request.post({
uri: creds.url,
json: true,
headers: {
apikey: creds.api_key
},
body: {
method: "publish",
params: [
streamId,
options.args.assetId,
value.toString().hexEncode()
]
}
}, function (err, res, body) {
if (err) {
if (typeof err !== "string") {
err = JSON.stringify(err);
}
return cb(err);
}
if (res && res.statusCode !== 200) {
if (typeof body !== "string") {
body = JSON.stringify(body);
}
return cb(body);
}
return cb(null);
});
});
};
/**
* Read from the given multichain stream.
*/
multichain.read = function (options, cb) {
request.post({
uri: creds.url,
json: true,
headers: {
apikey: creds.api_key
},
body: {
"method": "liststreamkeyitems",
"params": [
streamId,
options.args.assetId,
false,
1,
-1
]
}
}, function (err, res, body){
if (err) {
if (typeof err !== "string") {
err = JSON.stringify(err);
}
return cb(err);
}
var value = body && body.result && body.result[0] ? body.result[0].data.hexDecode() : "0";
return cb(null, value);
});
};
/**
* Returns the history of the asset from the multichain stream.
*/
multichain.history = function (options, cb) {
request.post({
uri: creds.url,
json: true,
headers: {
apikey: creds.api_key
},
body: {
method: "liststreamkeyitems",
params: [
streamId,
options.args.assetId,
false,
100
]
}
}, function (err, res, body) {
if (err) {
return cb(err);
}
const results = body.result.reverse();
cb(null, parseHistoryArray(results));
});
};
cb(multichain);
});
};
//-------------------------------------------------------------------
// HELPERS
//-------------------------------------------------------------------
String.prototype.hexEncode = function() {
var hex, i;
var result = "";
for (i = 0; i < this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("0"+hex).slice(-2);
}
return result;
}
String.prototype.hexDecode = function() {
var hexes = this.match(/.{1,2}/g) || [];
var result = "";
for(var j = 0; j < hexes.length; j++) {
result += String.fromCharCode(parseInt(hexes[j], 16));
}
return result;
}
function parseHistoryArray(historyArray) {
const result = [];
historyArray.forEach(function(item) {
const odometer = item.data.hexDecode();
const timestamp = new Date(item.blocktime * 1000);
result.push({
kilometre: odometer,
timestamp: timestamp
});
});
return result;
}