@triadxyz/triad-protocol 3.1.5-beta → 3.1.7-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.
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClaimData = exports.generateMerkleTree = exports.MerkleTree = void 0;
4
+ const sha3_1 = require("@noble/hashes/sha3");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ class MerkleTree {
7
+ constructor(claimData) {
8
+ this.claimData = claimData;
9
+ this.leaves = this.generateLeaves(claimData);
10
+ this.tree = this.buildTree(this.leaves);
11
+ }
12
+ generateLeaves(claimData) {
13
+ return claimData.map((data) => {
14
+ const userBytes = data.user.toBytes();
15
+ const amountBytes = Buffer.alloc(8);
16
+ const amountLamports = BigInt(Math.floor(data.amount * Math.pow(10, 6)));
17
+ amountBytes.writeBigUInt64LE(amountLamports, 0);
18
+ const leafData = Buffer.concat([userBytes, amountBytes]);
19
+ return Buffer.from((0, sha3_1.keccak_256)(leafData));
20
+ });
21
+ }
22
+ buildTree(leaves) {
23
+ if (leaves.length === 0) {
24
+ throw new Error('Cannot build tree with no leaves');
25
+ }
26
+ let currentLevel = [...leaves];
27
+ const tree = [currentLevel];
28
+ while (currentLevel.length > 1) {
29
+ const nextLevel = [];
30
+ for (let i = 0; i < currentLevel.length; i += 2) {
31
+ const left = currentLevel[i];
32
+ const right = i + 1 < currentLevel.length ? currentLevel[i + 1] : left;
33
+ const combined = left.compare(right) <= 0
34
+ ? Buffer.concat([left, right])
35
+ : Buffer.concat([right, left]);
36
+ nextLevel.push(Buffer.from((0, sha3_1.keccak_256)(combined)));
37
+ }
38
+ currentLevel = nextLevel;
39
+ tree.push(currentLevel);
40
+ }
41
+ return tree;
42
+ }
43
+ getRoot() {
44
+ if (this.tree.length === 0) {
45
+ throw new Error('Tree not built');
46
+ }
47
+ const root = this.tree[this.tree.length - 1][0];
48
+ return Array.from(root);
49
+ }
50
+ getProof(userPubkey, amount) {
51
+ const targetLeaf = this.generateLeaves([{ user: userPubkey, amount }])[0];
52
+ const leafIndex = this.leaves.findIndex((leaf) => leaf.equals(targetLeaf));
53
+ if (leafIndex === -1) {
54
+ throw new Error('User not found in merkle tree');
55
+ }
56
+ const proof = [];
57
+ let currentIndex = leafIndex;
58
+ for (let level = 0; level < this.tree.length - 1; level++) {
59
+ const isRightNode = currentIndex % 2 === 1;
60
+ const siblingIndex = isRightNode ? currentIndex - 1 : currentIndex + 1;
61
+ if (siblingIndex < this.tree[level].length) {
62
+ proof.push(this.tree[level][siblingIndex]);
63
+ }
64
+ currentIndex = Math.floor(currentIndex / 2);
65
+ }
66
+ return proof.map((hash) => Array.from(hash));
67
+ }
68
+ getAllProofs() {
69
+ return this.claimData.map((data) => ({
70
+ user: data.user,
71
+ amount: data.amount,
72
+ proof: this.getProof(data.user, data.amount)
73
+ }));
74
+ }
75
+ verifyProof(userPubkey, amount, proof) {
76
+ const leafData = Buffer.concat([
77
+ userPubkey.toBytes(),
78
+ (() => {
79
+ const amountBytes = Buffer.alloc(8);
80
+ const amountLamports = BigInt(Math.floor(amount * Math.pow(10, 6)));
81
+ amountBytes.writeBigUInt64LE(amountLamports, 0);
82
+ return amountBytes;
83
+ })()
84
+ ]);
85
+ let currentHash = Buffer.from((0, sha3_1.keccak_256)(leafData));
86
+ for (const proofElement of proof) {
87
+ const proofBuffer = Buffer.from(proofElement);
88
+ const combined = currentHash.compare(proofBuffer) <= 0
89
+ ? Buffer.concat([currentHash, proofBuffer])
90
+ : Buffer.concat([proofBuffer, currentHash]);
91
+ currentHash = Buffer.from((0, sha3_1.keccak_256)(combined));
92
+ }
93
+ const root = Buffer.from(this.getRoot());
94
+ return currentHash.equals(root);
95
+ }
96
+ }
97
+ exports.MerkleTree = MerkleTree;
98
+ function generateMerkleTree(claimData) {
99
+ const tree = new MerkleTree(claimData);
100
+ return {
101
+ merkleRoot: tree.getRoot(),
102
+ proofs: tree.getAllProofs()
103
+ };
104
+ }
105
+ exports.generateMerkleTree = generateMerkleTree;
106
+ function createClaimData(data) {
107
+ return data.map((item) => ({
108
+ user: new web3_js_1.PublicKey(item.user),
109
+ amount: item.amount
110
+ }));
111
+ }
112
+ exports.createClaimData = createClaimData;
@@ -1,6 +1,6 @@
1
- import { TransactionInstruction, AddressLookupTableAccount } from '@solana/web3.js';
1
+ import { TransactionInstruction, VersionedTransaction, AddressLookupTableAccount } from '@solana/web3.js';
2
2
  import { Program } from '@coral-xyz/anchor';
3
3
  import { RpcOptions } from '../types';
4
4
  import { TriadProtocol } from '../types/triad_protocol';
5
- declare const sendVersionedTransaction: (program: Program<TriadProtocol>, ixs: TransactionInstruction[], options?: RpcOptions, addressLookupTableAccounts?: AddressLookupTableAccount[]) => Promise<string>;
5
+ declare const sendVersionedTransaction: (program: Program<TriadProtocol>, ixs: TransactionInstruction[], options?: RpcOptions, addressLookupTableAccounts?: AddressLookupTableAccount[]) => Promise<VersionedTransaction | string>;
6
6
  export default sendVersionedTransaction;
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const web3_js_1 = require("@solana/web3.js");
16
16
  const getPriorityFee_1 = __importDefault(require("./getPriorityFee"));
17
17
  const sendVersionedTransaction = (program, ixs, options, addressLookupTableAccounts) => __awaiter(void 0, void 0, void 0, function* () {
18
+ var _a;
18
19
  if (options === null || options === void 0 ? void 0 : options.microLamports) {
19
20
  ixs.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
20
21
  microLamports: options.microLamports
@@ -27,6 +28,13 @@ const sendVersionedTransaction = (program, ixs, options, addressLookupTableAccou
27
28
  }));
28
29
  }
29
30
  const { blockhash } = yield program.provider.connection.getLatestBlockhash();
31
+ if (program.provider.publicKey.toBase58() !== ((_a = options === null || options === void 0 ? void 0 : options.payer) === null || _a === void 0 ? void 0 : _a.toBase58())) {
32
+ return new web3_js_1.VersionedTransaction(new web3_js_1.TransactionMessage({
33
+ instructions: ixs,
34
+ recentBlockhash: blockhash,
35
+ payerKey: options === null || options === void 0 ? void 0 : options.payer
36
+ }).compileToV0Message(addressLookupTableAccounts));
37
+ }
30
38
  return program.provider.sendAndConfirm(new web3_js_1.VersionedTransaction(new web3_js_1.TransactionMessage({
31
39
  instructions: ixs,
32
40
  recentBlockhash: blockhash,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triadxyz/triad-protocol",
3
- "version": "3.1.5-beta",
3
+ "version": "3.1.7-beta",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",