forked from byteball/ocore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_hash.js
178 lines (152 loc) · 5.27 KB
/
object_hash.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
/*jslint node: true */
"use strict";
var crypto = require('crypto');
var _ = require('lodash');
var chash = require('./chash.js');
function getSourceString(obj) {
var arrComponents = [];
function extractComponents(variable){
if (variable === null)
throw Error("null value");
switch (typeof variable){
case "string":
arrComponents.push("s", variable);
break;
case "number":
arrComponents.push("n", variable.toString());
break;
case "boolean":
arrComponents.push("b", variable.toString());
break;
case "object":
if (Array.isArray(variable)){
if (variable.length === 0)
throw Error("empty array");
arrComponents.push('[');
for (var i=0; i<variable.length; i++)
extractComponents(variable[i]);
arrComponents.push(']');
}
else{
var keys = Object.keys(variable).sort();
if (keys.length === 0)
throw Error("empty object");
keys.forEach(function(key){
arrComponents.push(key);
extractComponents(variable[key]);
});
}
break;
default:
throw Error("hash: unknown type="+(typeof variable)+" of "+variable+", object: "+JSON.stringify(obj));
}
}
extractComponents(obj);
return arrComponents.join("\x00");
}
function getChash160(obj) {
return chash.getChash160(getSourceString(obj));
}
function getChash288(obj) {
return chash.getChash288(getSourceString(obj));
}
function getHexHash(obj) {
return crypto.createHash("sha256").update(getSourceString(obj), "utf8").digest("hex");
}
function getBase64Hash(obj) {
return crypto.createHash("sha256").update(getSourceString(obj), "utf8").digest("base64");
}
function getNakedUnit(objUnit){
var objNakedUnit = _.cloneDeep(objUnit);
delete objNakedUnit.unit;
delete objNakedUnit.headers_commission;
delete objNakedUnit.payload_commission;
delete objNakedUnit.main_chain_index;
delete objNakedUnit.timestamp;
//delete objNakedUnit.last_ball_unit;
if (objNakedUnit.messages){
for (var i=0; i<objNakedUnit.messages.length; i++){
delete objNakedUnit.messages[i].payload;
delete objNakedUnit.messages[i].payload_uri;
}
}
//console.log("naked Unit: ", objNakedUnit);
//console.log("original Unit: ", objUnit);
return objNakedUnit;
}
function getUnitContentHash(objUnit){
return getBase64Hash(getNakedUnit(objUnit));
}
function getUnitHash(objUnit) {
if (objUnit.content_hash) // already stripped
return getBase64Hash(getNakedUnit(objUnit));
var objStrippedUnit = {
content_hash: getUnitContentHash(objUnit),
version: objUnit.version,
alt: objUnit.alt,
authors: objUnit.authors.map(function(author){ return {address: author.address}; }) // already sorted
};
if (objUnit.witness_list_unit)
objStrippedUnit.witness_list_unit = objUnit.witness_list_unit;
else
objStrippedUnit.witnesses = objUnit.witnesses;
if (objUnit.parent_units){
objStrippedUnit.parent_units = objUnit.parent_units;
objStrippedUnit.last_ball = objUnit.last_ball;
objStrippedUnit.last_ball_unit = objUnit.last_ball_unit;
}
return getBase64Hash(objStrippedUnit);
}
function getUnitHashToSign(objUnit) {
var objNakedUnit = getNakedUnit(objUnit);
for (var i=0; i<objNakedUnit.authors.length; i++)
delete objNakedUnit.authors[i].authentifiers;
return crypto.createHash("sha256").update(getSourceString(objNakedUnit), "utf8").digest();
}
function getBallHash(unit, arrParentBalls, arrSkiplistBalls, bNonserial) {
var objBall = {
unit: unit
};
if (arrParentBalls && arrParentBalls.length > 0)
objBall.parent_balls = arrParentBalls;
if (arrSkiplistBalls && arrSkiplistBalls.length > 0)
objBall.skiplist_balls = arrSkiplistBalls;
if (bNonserial)
objBall.is_nonserial = true;
return getBase64Hash(objBall);
}
function getJointHash(objJoint) {
// we use JSON.stringify, we can't use objectHash here because it might throw errors
return crypto.createHash("sha256").update(JSON.stringify(objJoint), "utf8").digest("base64");
}
function cleanNulls(obj){
Object.keys(obj).forEach(function(key){
if (obj[key] === null)
delete obj[key];
});
}
// -----------------
// prefix device addresses with 0 to avoid confusion with payment addresses
// Note that 0 is not a member of base32 alphabet, which makes device addresses easily distinguishable from payment addresses
// but still selectable by double-click. Stripping the leading 0 will not produce a payment address that the device owner knows a private key for,
// because payment address is derived by c-hashing the definition object, while device address is produced from raw public key.
function getDeviceAddress(b64_pubkey){
return ('0' + getChash160(b64_pubkey));
}
function getDeviceMessageHashToSign(objDeviceMessage) {
var objNakedDeviceMessage = _.clone(objDeviceMessage);
delete objNakedDeviceMessage.signature;
return crypto.createHash("sha256").update(getSourceString(objNakedDeviceMessage), "utf8").digest();
}
exports.getChash160 = getChash160;
exports.getChash288 = getChash288;
exports.getHexHash = getHexHash;
exports.getBase64Hash = getBase64Hash;
exports.getUnitContentHash = getUnitContentHash;
exports.getUnitHash = getUnitHash;
exports.getUnitHashToSign = getUnitHashToSign;
exports.getBallHash = getBallHash;
exports.getJointHash = getJointHash;
exports.cleanNulls = cleanNulls;
exports.getDeviceAddress = getDeviceAddress;
exports.getDeviceMessageHashToSign = getDeviceMessageHashToSign;