-
Notifications
You must be signed in to change notification settings - Fork 94
/
object_hash.js
173 lines (146 loc) · 6.32 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
/*jslint node: true */
"use strict";
var crypto = require('crypto');
var _ = require('lodash');
var chash = require('./chash.js');
var constants = require('./constants.js');
var getSourceString = require('./string_utils').getSourceString;
var getJsonSourceString = require('./string_utils').getJsonSourceString;
function getChash160(obj) {
var sourceString = (Array.isArray(obj) && obj.length === 2 && obj[0] === 'autonomous agent') ? getJsonSourceString(obj) : getSourceString(obj);
return chash.getChash160(sourceString);
}
function getChash288(obj) {
return chash.getChash288(getSourceString(obj));
}
function getHexHash(obj) {
return crypto.createHash("sha256").update(getSourceString(obj), "utf8").digest("hex");
}
function getBase64Hash(obj, bJsonBased) {
var sourceString = bJsonBased ? getJsonSourceString(obj) : getSourceString(obj)
return crypto.createHash("sha256").update(sourceString, "utf8").digest("base64");
}
function getNakedUnit(objUnit){
var objNakedUnit = _.cloneDeep(objUnit);
delete objNakedUnit.unit;
delete objNakedUnit.headers_commission;
delete objNakedUnit.payload_commission;
delete objNakedUnit.oversize_fee;
// delete objNakedUnit.tps_fee; // cannot be calculated from unit's content and environment, users might pay more than required
delete objNakedUnit.actual_tps_fee;
delete objNakedUnit.main_chain_index;
if (objUnit.version === constants.versionWithoutTimestamp)
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), objUnit.version !== constants.versionWithoutTimestamp);
}
function getUnitHash(objUnit) {
var bVersion2 = (objUnit.version !== constants.versionWithoutTimestamp);
if (objUnit.content_hash) // already stripped and objUnit doesn't have messages
return getBase64Hash(getNakedUnit(objUnit), bVersion2);
return getBase64Hash(getStrippedUnit(objUnit), bVersion2);
}
function getStrippedUnit(objUnit) {
var bVersion2 = (objUnit.version !== constants.versionWithoutTimestamp);
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 if (objUnit.witnesses)
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;
}
if (bVersion2)
objStrippedUnit.timestamp = objUnit.timestamp;
return objStrippedUnit;
}
function getUnitHashToSign(objUnit) {
var objNakedUnit = getNakedUnit(objUnit);
for (var i=0; i<objNakedUnit.authors.length; i++)
delete objNakedUnit.authors[i].authentifiers;
var sourceString = (typeof objUnit.version === 'undefined' || objUnit.version === constants.versionWithoutTimestamp) ? getSourceString(objNakedUnit) : getJsonSourceString(objNakedUnit);
return crypto.createHash("sha256").update(sourceString, "utf8").digest();
}
function getSignedPackageHashToSign(signedPackage) {
var unsignedPackage = _.cloneDeep(signedPackage);
for (var i=0; i<unsignedPackage.authors.length; i++)
delete unsignedPackage.authors[i].authentifiers;
var sourceString = (typeof signedPackage.version === 'undefined' || signedPackage.version === constants.versionWithoutTimestamp) ? getSourceString(unsignedPackage) : getJsonSourceString(unsignedPackage);
return crypto.createHash("sha256").update(sourceString, "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];
});
}
function cleanNullsDeep(obj){
Object.keys(obj).forEach(function(key){
if (obj[key] === null)
delete obj[key];
else if (typeof obj[key] === 'object') // array included
cleanNullsDeep(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;
cleanNullsDeep(objNakedDeviceMessage); // device messages have free format and we can't guarantee absence of malicious fields
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.cleanNullsDeep = cleanNullsDeep;
exports.getDeviceAddress = getDeviceAddress;
exports.getDeviceMessageHashToSign = getDeviceMessageHashToSign;
exports.getSignedPackageHashToSign = getSignedPackageHashToSign;