@triadxyz/triad-protocol 3.3.0-beta → 3.3.2-beta
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/utils/merkle.d.ts +13 -0
- package/dist/utils/merkle.js +67 -3
- package/package.json +1 -1
package/dist/utils/merkle.d.ts
CHANGED
|
@@ -20,3 +20,16 @@ export declare function createClaimData(data: {
|
|
|
20
20
|
user: string;
|
|
21
21
|
amount: number;
|
|
22
22
|
}[]): ClaimData[];
|
|
23
|
+
export declare function debugMerkleProof(userPubkey: PublicKey, amount: number, merkleProof: number[][], merkleRoot: number[]): {
|
|
24
|
+
isValid: boolean;
|
|
25
|
+
steps: Array<{
|
|
26
|
+
step: number;
|
|
27
|
+
currentHash: string;
|
|
28
|
+
proofElement: string;
|
|
29
|
+
isCurrentSmaller: boolean;
|
|
30
|
+
combined: string;
|
|
31
|
+
newHash: string;
|
|
32
|
+
}>;
|
|
33
|
+
finalHash: string;
|
|
34
|
+
expectedRoot: string;
|
|
35
|
+
};
|
package/dist/utils/merkle.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createClaimData = exports.generateMerkleTree = exports.MerkleTree = void 0;
|
|
3
|
+
exports.debugMerkleProof = exports.createClaimData = exports.generateMerkleTree = exports.MerkleTree = void 0;
|
|
4
4
|
const sha3_1 = require("@noble/hashes/sha3");
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
6
|
class MerkleTree {
|
|
@@ -30,7 +30,7 @@ class MerkleTree {
|
|
|
30
30
|
for (let i = 0; i < currentLevel.length; i += 2) {
|
|
31
31
|
const left = currentLevel[i];
|
|
32
32
|
const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
|
|
33
|
-
const combined = left.compare(right)
|
|
33
|
+
const combined = left.compare(right) < 0
|
|
34
34
|
? Buffer.concat([left, right])
|
|
35
35
|
: Buffer.concat([right, left]);
|
|
36
36
|
nextLevel.push(Buffer.from((0, sha3_1.keccak_256)(combined)));
|
|
@@ -61,6 +61,9 @@ class MerkleTree {
|
|
|
61
61
|
if (siblingIndex < this.tree[level].length) {
|
|
62
62
|
proof.push(this.tree[level][siblingIndex]);
|
|
63
63
|
}
|
|
64
|
+
else {
|
|
65
|
+
proof.push(this.tree[level][currentIndex]);
|
|
66
|
+
}
|
|
64
67
|
currentIndex = Math.floor(currentIndex / 2);
|
|
65
68
|
}
|
|
66
69
|
return proof.map((hash) => Array.from(hash));
|
|
@@ -85,7 +88,7 @@ class MerkleTree {
|
|
|
85
88
|
let currentHash = Buffer.from((0, sha3_1.keccak_256)(leafData));
|
|
86
89
|
for (const proofElement of proof) {
|
|
87
90
|
const proofBuffer = Buffer.from(proofElement);
|
|
88
|
-
const combined = currentHash.compare(proofBuffer)
|
|
91
|
+
const combined = currentHash.compare(proofBuffer) < 0
|
|
89
92
|
? Buffer.concat([currentHash, proofBuffer])
|
|
90
93
|
: Buffer.concat([proofBuffer, currentHash]);
|
|
91
94
|
currentHash = Buffer.from((0, sha3_1.keccak_256)(combined));
|
|
@@ -110,3 +113,64 @@ function createClaimData(data) {
|
|
|
110
113
|
}));
|
|
111
114
|
}
|
|
112
115
|
exports.createClaimData = createClaimData;
|
|
116
|
+
function debugMerkleProof(userPubkey, amount, merkleProof, merkleRoot) {
|
|
117
|
+
console.log('🔍 Debug Merkle Proof Validation (Rust Compatible)');
|
|
118
|
+
console.log('User:', userPubkey.toBase58());
|
|
119
|
+
console.log('Amount:', amount);
|
|
120
|
+
console.log('Proof length:', merkleProof.length);
|
|
121
|
+
console.log('Max theoretical elements:', Math.pow(2, merkleProof.length));
|
|
122
|
+
const leafData = Buffer.concat([
|
|
123
|
+
userPubkey.toBytes(),
|
|
124
|
+
(() => {
|
|
125
|
+
const amountBytes = Buffer.alloc(8);
|
|
126
|
+
const amountLamports = BigInt(Math.floor(amount * Math.pow(10, 6)));
|
|
127
|
+
amountBytes.writeBigUInt64LE(amountLamports, 0);
|
|
128
|
+
return amountBytes;
|
|
129
|
+
})()
|
|
130
|
+
]);
|
|
131
|
+
let currentHash = Buffer.from((0, sha3_1.keccak_256)(leafData));
|
|
132
|
+
console.log('Initial leaf hash:', currentHash.toString('hex'));
|
|
133
|
+
const steps = [];
|
|
134
|
+
for (let i = 0; i < merkleProof.length; i++) {
|
|
135
|
+
const proofElement = merkleProof[i];
|
|
136
|
+
const proofBuffer = Buffer.from(proofElement);
|
|
137
|
+
const isCurrentSmaller = currentHash.compare(proofBuffer) < 0;
|
|
138
|
+
let combined;
|
|
139
|
+
if (isCurrentSmaller) {
|
|
140
|
+
combined = Buffer.concat([currentHash, proofBuffer]);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
combined = Buffer.concat([proofBuffer, currentHash]);
|
|
144
|
+
}
|
|
145
|
+
const newHash = Buffer.from((0, sha3_1.keccak_256)(combined));
|
|
146
|
+
const step = {
|
|
147
|
+
step: i + 1,
|
|
148
|
+
currentHash: currentHash.toString('hex'),
|
|
149
|
+
proofElement: proofBuffer.toString('hex'),
|
|
150
|
+
isCurrentSmaller,
|
|
151
|
+
combined: combined.toString('hex'),
|
|
152
|
+
newHash: newHash.toString('hex')
|
|
153
|
+
};
|
|
154
|
+
steps.push(step);
|
|
155
|
+
console.log(`Step ${i + 1}:`);
|
|
156
|
+
console.log(` Current: ${step.currentHash}`);
|
|
157
|
+
console.log(` Proof: ${step.proofElement}`);
|
|
158
|
+
console.log(` Order: ${isCurrentSmaller ? 'current + proof' : 'proof + current'}`);
|
|
159
|
+
console.log(` Result: ${step.newHash}`);
|
|
160
|
+
currentHash = newHash;
|
|
161
|
+
}
|
|
162
|
+
const rootBuffer = Buffer.from(merkleRoot);
|
|
163
|
+
const finalHash = currentHash.toString('hex');
|
|
164
|
+
const expectedRoot = rootBuffer.toString('hex');
|
|
165
|
+
const isValid = currentHash.equals(rootBuffer);
|
|
166
|
+
console.log('Final hash:', finalHash);
|
|
167
|
+
console.log('Expected root:', expectedRoot);
|
|
168
|
+
console.log('Valid:', isValid);
|
|
169
|
+
return {
|
|
170
|
+
isValid,
|
|
171
|
+
steps,
|
|
172
|
+
finalHash,
|
|
173
|
+
expectedRoot
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
exports.debugMerkleProof = debugMerkleProof;
|