eventsourcingdb-merkle 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lib/merkleTree.js
CHANGED
|
@@ -89,8 +89,13 @@ export const getMerkleTreeVersion = (merkleRoot) => {
|
|
|
89
89
|
throw new UnsupportedMerkleTreeVersionError(version);
|
|
90
90
|
};
|
|
91
91
|
const isDigest = (value) => typeof value === 'string' && digestPattern.test(value);
|
|
92
|
-
const
|
|
93
|
-
|
|
92
|
+
const isText = (value) => typeof value === 'string';
|
|
93
|
+
// A sibling of version 1 or 2 is an object with its hash and its side. The hash
|
|
94
|
+
// of a sibling of version 2 is a digest, but that of version 1 has no fixed
|
|
95
|
+
// form, since the tree of version 1 takes any stored hash as a leaf, so the
|
|
96
|
+
// first sibling of a proof may be the stored hash of a neighbor.
|
|
97
|
+
const isSiblingWithSide = (isHash) => (sibling) => isObject(sibling) &&
|
|
98
|
+
isHash(sibling.hash) &&
|
|
94
99
|
(sibling.position === 'left' || sibling.position === 'right');
|
|
95
100
|
// An ID in a proof of version 3 is written as EventSourcingDB writes IDs, since
|
|
96
101
|
// get-proof writes it rather than anyone typing it, so that the same proof has
|
|
@@ -127,6 +132,9 @@ export const findProofStructureProblem = (proof) => {
|
|
|
127
132
|
if (hasLastEventId) {
|
|
128
133
|
return 'A proof of Merkle tree version 1 has no last event ID';
|
|
129
134
|
}
|
|
135
|
+
if (!proof.siblingHashes.every(isSiblingWithSide(isText))) {
|
|
136
|
+
return 'Every sibling has to have a hash, and the position left or right';
|
|
137
|
+
}
|
|
130
138
|
return undefined;
|
|
131
139
|
}
|
|
132
140
|
if (treeVersion === 2 && hasLastEventId) {
|
|
@@ -147,7 +155,7 @@ export const findProofStructureProblem = (proof) => {
|
|
|
147
155
|
return 'The salt has to be 64 hexadecimal digits';
|
|
148
156
|
}
|
|
149
157
|
if (treeVersion === 2) {
|
|
150
|
-
return proof.siblingHashes.every(isSiblingWithSide)
|
|
158
|
+
return proof.siblingHashes.every(isSiblingWithSide(isDigest))
|
|
151
159
|
? undefined
|
|
152
160
|
: 'Every sibling has to have a hash of 64 hexadecimal digits, and the position left or right';
|
|
153
161
|
}
|
|
@@ -46,10 +46,9 @@ export const generateVersion1Proof = (hashes, targetHash) => {
|
|
|
46
46
|
}
|
|
47
47
|
if (i === currentIndex || i + 1 === currentIndex) {
|
|
48
48
|
if (currentIndex % 2 === 0) {
|
|
49
|
-
// Target is on the left
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
49
|
+
// Target is on the left. Where it has no partner, the tree pairs it
|
|
50
|
+
// with itself, so that it is its own sibling on the right.
|
|
51
|
+
siblingHashes.push({ hash: right, position: 'right' });
|
|
53
52
|
}
|
|
54
53
|
else {
|
|
55
54
|
// Target is on the right
|