privacycash-x402 1.0.0
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/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/examples/client-example.d.ts +13 -0
- package/dist/examples/client-example.d.ts.map +1 -0
- package/dist/examples/client-example.js +108 -0
- package/dist/examples/client-example.js.map +1 -0
- package/dist/examples/server-example.d.ts +13 -0
- package/dist/examples/server-example.d.ts.map +1 -0
- package/dist/examples/server-example.js +104 -0
- package/dist/examples/server-example.js.map +1 -0
- package/dist/src/client/index.d.ts +89 -0
- package/dist/src/client/index.d.ts.map +1 -0
- package/dist/src/client/index.js +259 -0
- package/dist/src/client/index.js.map +1 -0
- package/dist/src/config.d.ts +10 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +16 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/errors/index.d.ts +81 -0
- package/dist/src/errors/index.d.ts.map +1 -0
- package/dist/src/errors/index.js +110 -0
- package/dist/src/errors/index.js.map +1 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +47 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/models/keypair.d.ts +27 -0
- package/dist/src/models/keypair.d.ts.map +1 -0
- package/dist/src/models/keypair.js +48 -0
- package/dist/src/models/keypair.js.map +1 -0
- package/dist/src/models/utxo.d.ts +50 -0
- package/dist/src/models/utxo.d.ts.map +1 -0
- package/dist/src/models/utxo.js +86 -0
- package/dist/src/models/utxo.js.map +1 -0
- package/dist/src/server/index.d.ts +67 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +266 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/types/index.d.ts +67 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +3 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/utils/constants.d.ts +29 -0
- package/dist/src/utils/constants.d.ts.map +1 -0
- package/dist/src/utils/constants.js +65 -0
- package/dist/src/utils/constants.js.map +1 -0
- package/dist/src/utils/encryption.d.ts +108 -0
- package/dist/src/utils/encryption.d.ts.map +1 -0
- package/dist/src/utils/encryption.js +420 -0
- package/dist/src/utils/encryption.js.map +1 -0
- package/dist/src/utils/logger.d.ts +10 -0
- package/dist/src/utils/logger.d.ts.map +1 -0
- package/dist/src/utils/logger.js +40 -0
- package/dist/src/utils/logger.js.map +1 -0
- package/dist/src/utils/utils.d.ts +65 -0
- package/dist/src/utils/utils.d.ts.map +1 -0
- package/dist/src/utils/utils.js +214 -0
- package/dist/src/utils/utils.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = void 0;
|
|
4
|
+
exports.setLogger = setLogger;
|
|
5
|
+
const defaultLogger = (level, message) => {
|
|
6
|
+
const prefix = `[${level.toUpperCase()}]`;
|
|
7
|
+
console.log(prefix, message);
|
|
8
|
+
};
|
|
9
|
+
let userLogger = defaultLogger;
|
|
10
|
+
function setLogger(logger) {
|
|
11
|
+
userLogger = logger;
|
|
12
|
+
}
|
|
13
|
+
function argToStr(args) {
|
|
14
|
+
return args.map(arg => {
|
|
15
|
+
if (typeof arg === "object" && arg !== null) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.stringify(arg);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return String(arg);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return String(arg);
|
|
24
|
+
}).join(" ");
|
|
25
|
+
}
|
|
26
|
+
exports.logger = {
|
|
27
|
+
debug: (...args) => {
|
|
28
|
+
userLogger('debug', argToStr(args));
|
|
29
|
+
},
|
|
30
|
+
info: (...args) => {
|
|
31
|
+
userLogger('info', argToStr(args));
|
|
32
|
+
},
|
|
33
|
+
warn: (...args) => {
|
|
34
|
+
userLogger('warn', argToStr(args));
|
|
35
|
+
},
|
|
36
|
+
error: (...args) => {
|
|
37
|
+
userLogger('error', argToStr(args));
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":";;;AAYA,8BAEC;AATD,MAAM,aAAa,GAAa,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;IAC/C,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,WAAW,EAAE,GAAG,CAAC;IAC1C,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjC,CAAC,CAAC;AAEF,IAAI,UAAU,GAAa,aAAa,CAAC;AAEzC,SAAgB,SAAS,CAAC,MAAgB;IACtC,UAAU,GAAG,MAAM,CAAC;AACxB,CAAC;AAED,SAAS,QAAQ,CAAC,IAAe;IAC7B,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;QAClB,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YAC1C,IAAI,CAAC;gBACD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;YAC/B,CAAC;YAAC,MAAM,CAAC;gBACL,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;YACvB,CAAC;QACL,CAAC;QACD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjB,CAAC;AACY,QAAA,MAAM,GAAG;IAClB,KAAK,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;QAC1B,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IACvC,CAAC;IACD,IAAI,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;QACzB,UAAU,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IACtC,CAAC;IACD,IAAI,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;QACzB,UAAU,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IACtC,CAAC;IACD,KAAK,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;QAC1B,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;IACvC,CAAC;CACJ,CAAA"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for ZK Cash
|
|
3
|
+
*
|
|
4
|
+
* Provides common utility functions for the ZK Cash system
|
|
5
|
+
* Based on: https://github.com/tornadocash/tornado-nova
|
|
6
|
+
*/
|
|
7
|
+
import BN from 'bn.js';
|
|
8
|
+
import { Utxo } from '../models/utxo';
|
|
9
|
+
import { PublicKey } from '@solana/web3.js';
|
|
10
|
+
/**
|
|
11
|
+
* Calculate deposit fee based on deposit amount and fee rate
|
|
12
|
+
* @param depositAmount Amount being deposited in lamports
|
|
13
|
+
* @returns Fee amount in lamports
|
|
14
|
+
*/
|
|
15
|
+
export declare function calculateDepositFee(depositAmount: number): Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* Calculate withdrawal fee based on withdrawal amount and fee rate
|
|
18
|
+
* @param withdrawalAmount Amount being withdrawn in lamports
|
|
19
|
+
* @returns Fee amount in lamports
|
|
20
|
+
*/
|
|
21
|
+
export declare function calculateWithdrawalFee(withdrawalAmount: number): Promise<number>;
|
|
22
|
+
/**
|
|
23
|
+
* Mock encryption function - in real implementation this would be proper encryption
|
|
24
|
+
* For testing, we just return a fixed prefix to ensure consistent extDataHash
|
|
25
|
+
* @param value Value to encrypt
|
|
26
|
+
* @returns Encrypted string representation
|
|
27
|
+
*/
|
|
28
|
+
export declare function mockEncrypt(value: Utxo): string;
|
|
29
|
+
/**
|
|
30
|
+
* Calculates the hash of ext data using Borsh serialization
|
|
31
|
+
* @param extData External data object containing recipient, amount, encrypted outputs, fee, fee recipient, and mint address
|
|
32
|
+
* @returns The hash as a Uint8Array (32 bytes)
|
|
33
|
+
*/
|
|
34
|
+
export declare function getExtDataHash(extData: {
|
|
35
|
+
recipient: string | PublicKey;
|
|
36
|
+
extAmount: string | number | BN;
|
|
37
|
+
encryptedOutput1?: string | Uint8Array;
|
|
38
|
+
encryptedOutput2?: string | Uint8Array;
|
|
39
|
+
fee: string | number | BN;
|
|
40
|
+
feeRecipient: string | PublicKey;
|
|
41
|
+
mintAddress: string | PublicKey;
|
|
42
|
+
}): Uint8Array;
|
|
43
|
+
export declare function fetchMerkleProof(commitment: string, tokenName?: string): Promise<{
|
|
44
|
+
pathElements: string[];
|
|
45
|
+
pathIndices: number[];
|
|
46
|
+
}>;
|
|
47
|
+
export declare function findNullifierPDAs(proof: any): {
|
|
48
|
+
nullifier0PDA: PublicKey;
|
|
49
|
+
nullifier1PDA: PublicKey;
|
|
50
|
+
};
|
|
51
|
+
export declare function queryRemoteTreeState(tokenName?: string): Promise<{
|
|
52
|
+
root: string;
|
|
53
|
+
nextIndex: number;
|
|
54
|
+
}>;
|
|
55
|
+
export declare function getProgramAccounts(): {
|
|
56
|
+
treeAccount: PublicKey;
|
|
57
|
+
treeTokenAccount: PublicKey;
|
|
58
|
+
globalConfigAccount: PublicKey;
|
|
59
|
+
};
|
|
60
|
+
export declare function findCrossCheckNullifierPDAs(proof: any): {
|
|
61
|
+
nullifier2PDA: PublicKey;
|
|
62
|
+
nullifier3PDA: PublicKey;
|
|
63
|
+
};
|
|
64
|
+
export declare function getMintAddressField(mint: PublicKey): string;
|
|
65
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/utils/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,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CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,CAa3D"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility functions for ZK Cash
|
|
4
|
+
*
|
|
5
|
+
* Provides common utility functions for the ZK Cash system
|
|
6
|
+
* Based on: https://github.com/tornadocash/tornado-nova
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
42
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.calculateDepositFee = calculateDepositFee;
|
|
46
|
+
exports.calculateWithdrawalFee = calculateWithdrawalFee;
|
|
47
|
+
exports.mockEncrypt = mockEncrypt;
|
|
48
|
+
exports.getExtDataHash = getExtDataHash;
|
|
49
|
+
exports.fetchMerkleProof = fetchMerkleProof;
|
|
50
|
+
exports.findNullifierPDAs = findNullifierPDAs;
|
|
51
|
+
exports.queryRemoteTreeState = queryRemoteTreeState;
|
|
52
|
+
exports.getProgramAccounts = getProgramAccounts;
|
|
53
|
+
exports.findCrossCheckNullifierPDAs = findCrossCheckNullifierPDAs;
|
|
54
|
+
exports.getMintAddressField = getMintAddressField;
|
|
55
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
56
|
+
const borsh = __importStar(require("borsh"));
|
|
57
|
+
const sha2_1 = require("@ethersproject/sha2");
|
|
58
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
59
|
+
const constants_1 = require("./constants");
|
|
60
|
+
const logger_1 = require("./logger");
|
|
61
|
+
const config_1 = require("../config");
|
|
62
|
+
/**
|
|
63
|
+
* Calculate deposit fee based on deposit amount and fee rate
|
|
64
|
+
* @param depositAmount Amount being deposited in lamports
|
|
65
|
+
* @returns Fee amount in lamports
|
|
66
|
+
*/
|
|
67
|
+
async function calculateDepositFee(depositAmount) {
|
|
68
|
+
return Math.floor(depositAmount * (await (0, config_1.getConfig)('deposit_fee_rate')) / 10000);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Calculate withdrawal fee based on withdrawal amount and fee rate
|
|
72
|
+
* @param withdrawalAmount Amount being withdrawn in lamports
|
|
73
|
+
* @returns Fee amount in lamports
|
|
74
|
+
*/
|
|
75
|
+
async function calculateWithdrawalFee(withdrawalAmount) {
|
|
76
|
+
return Math.floor(withdrawalAmount * (await (0, config_1.getConfig)('withdraw_fee_rate')) / 10000);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Mock encryption function - in real implementation this would be proper encryption
|
|
80
|
+
* For testing, we just return a fixed prefix to ensure consistent extDataHash
|
|
81
|
+
* @param value Value to encrypt
|
|
82
|
+
* @returns Encrypted string representation
|
|
83
|
+
*/
|
|
84
|
+
function mockEncrypt(value) {
|
|
85
|
+
return JSON.stringify(value);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Calculates the hash of ext data using Borsh serialization
|
|
89
|
+
* @param extData External data object containing recipient, amount, encrypted outputs, fee, fee recipient, and mint address
|
|
90
|
+
* @returns The hash as a Uint8Array (32 bytes)
|
|
91
|
+
*/
|
|
92
|
+
function getExtDataHash(extData) {
|
|
93
|
+
// Convert all inputs to their appropriate types
|
|
94
|
+
const recipient = extData.recipient instanceof web3_js_1.PublicKey
|
|
95
|
+
? extData.recipient
|
|
96
|
+
: new web3_js_1.PublicKey(extData.recipient);
|
|
97
|
+
const feeRecipient = extData.feeRecipient instanceof web3_js_1.PublicKey
|
|
98
|
+
? extData.feeRecipient
|
|
99
|
+
: new web3_js_1.PublicKey(extData.feeRecipient);
|
|
100
|
+
const mintAddress = extData.mintAddress instanceof web3_js_1.PublicKey
|
|
101
|
+
? extData.mintAddress
|
|
102
|
+
: new web3_js_1.PublicKey(extData.mintAddress);
|
|
103
|
+
// Convert to BN for proper i64/u64 handling
|
|
104
|
+
const extAmount = new bn_js_1.default(extData.extAmount.toString());
|
|
105
|
+
const fee = new bn_js_1.default(extData.fee.toString());
|
|
106
|
+
// Handle encrypted outputs - they might not be present in Account Data Separation approach
|
|
107
|
+
const encryptedOutput1 = extData.encryptedOutput1
|
|
108
|
+
? Buffer.from(extData.encryptedOutput1)
|
|
109
|
+
: Buffer.alloc(0); // Empty buffer if not provided
|
|
110
|
+
const encryptedOutput2 = extData.encryptedOutput2
|
|
111
|
+
? Buffer.from(extData.encryptedOutput2)
|
|
112
|
+
: Buffer.alloc(0); // Empty buffer if not provided
|
|
113
|
+
// Define the borsh schema matching the Rust struct
|
|
114
|
+
const schema = {
|
|
115
|
+
struct: {
|
|
116
|
+
recipient: { array: { type: 'u8', len: 32 } },
|
|
117
|
+
extAmount: 'i64',
|
|
118
|
+
encryptedOutput1: { array: { type: 'u8' } },
|
|
119
|
+
encryptedOutput2: { array: { type: 'u8' } },
|
|
120
|
+
fee: 'u64',
|
|
121
|
+
feeRecipient: { array: { type: 'u8', len: 32 } },
|
|
122
|
+
mintAddress: { array: { type: 'u8', len: 32 } },
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const value = {
|
|
126
|
+
recipient: recipient.toBytes(),
|
|
127
|
+
extAmount: extAmount, // BN instance - Borsh handles it correctly with i64 type
|
|
128
|
+
encryptedOutput1: encryptedOutput1,
|
|
129
|
+
encryptedOutput2: encryptedOutput2,
|
|
130
|
+
fee: fee, // BN instance - Borsh handles it correctly with u64 type
|
|
131
|
+
feeRecipient: feeRecipient.toBytes(),
|
|
132
|
+
mintAddress: mintAddress.toBytes(),
|
|
133
|
+
};
|
|
134
|
+
// Serialize with Borsh
|
|
135
|
+
const serializedData = borsh.serialize(schema, value);
|
|
136
|
+
// Calculate the SHA-256 hash
|
|
137
|
+
const hashHex = (0, sha2_1.sha256)(serializedData);
|
|
138
|
+
// Convert from hex string to Uint8Array
|
|
139
|
+
return Buffer.from(hashHex.slice(2), 'hex');
|
|
140
|
+
}
|
|
141
|
+
// Function to fetch Merkle proof from API for a given commitment
|
|
142
|
+
async function fetchMerkleProof(commitment, tokenName) {
|
|
143
|
+
try {
|
|
144
|
+
logger_1.logger.debug(`Fetching Merkle proof for commitment: ${commitment}`);
|
|
145
|
+
let url = `${constants_1.RELAYER_API_URL}/merkle/proof/${commitment}`;
|
|
146
|
+
if (tokenName) {
|
|
147
|
+
url += '?token=' + tokenName;
|
|
148
|
+
}
|
|
149
|
+
const response = await fetch(url);
|
|
150
|
+
if (!response.ok) {
|
|
151
|
+
throw new Error(`Failed to fetch Merkle proof: ${url}`);
|
|
152
|
+
}
|
|
153
|
+
const data = await response.json();
|
|
154
|
+
logger_1.logger.debug(`✓ Fetched Merkle proof with ${data.pathElements.length} elements`);
|
|
155
|
+
return data;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
console.error(`Failed to fetch Merkle proof for commitment ${commitment}:`, error);
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Find nullifier PDAs for the given proof
|
|
163
|
+
function findNullifierPDAs(proof) {
|
|
164
|
+
const [nullifier0PDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("nullifier0"), Buffer.from(proof.inputNullifiers[0])], constants_1.PROGRAM_ID);
|
|
165
|
+
const [nullifier1PDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("nullifier1"), Buffer.from(proof.inputNullifiers[1])], constants_1.PROGRAM_ID);
|
|
166
|
+
return { nullifier0PDA, nullifier1PDA };
|
|
167
|
+
}
|
|
168
|
+
// Function to query remote tree state from indexer API
|
|
169
|
+
async function queryRemoteTreeState(tokenName) {
|
|
170
|
+
try {
|
|
171
|
+
logger_1.logger.debug('Fetching Merkle root and nextIndex from API...');
|
|
172
|
+
let url = `${constants_1.RELAYER_API_URL}/merkle/root`;
|
|
173
|
+
if (tokenName) {
|
|
174
|
+
url += '?token=' + tokenName;
|
|
175
|
+
}
|
|
176
|
+
const response = await fetch(url);
|
|
177
|
+
if (!response.ok) {
|
|
178
|
+
throw new Error(`Failed to fetch Merkle root and nextIndex: ${response.status} ${response.statusText}`);
|
|
179
|
+
}
|
|
180
|
+
const data = await response.json();
|
|
181
|
+
logger_1.logger.debug(`Fetched root from API: ${data.root}`);
|
|
182
|
+
logger_1.logger.debug(`Fetched nextIndex from API: ${data.nextIndex}`);
|
|
183
|
+
return data;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
console.error('Failed to fetch root and nextIndex from API:', error);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function getProgramAccounts() {
|
|
191
|
+
// Derive PDA (Program Derived Addresses) for the tree account and other required accounts
|
|
192
|
+
const [treeAccount] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('merkle_tree')], constants_1.PROGRAM_ID);
|
|
193
|
+
const [treeTokenAccount] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('tree_token')], constants_1.PROGRAM_ID);
|
|
194
|
+
const [globalConfigAccount] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('global_config')], constants_1.PROGRAM_ID);
|
|
195
|
+
return { treeAccount, treeTokenAccount, globalConfigAccount };
|
|
196
|
+
}
|
|
197
|
+
function findCrossCheckNullifierPDAs(proof) {
|
|
198
|
+
const [nullifier2PDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("nullifier0"), Buffer.from(proof.inputNullifiers[1])], constants_1.PROGRAM_ID);
|
|
199
|
+
const [nullifier3PDA] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("nullifier1"), Buffer.from(proof.inputNullifiers[0])], constants_1.PROGRAM_ID);
|
|
200
|
+
return { nullifier2PDA, nullifier3PDA };
|
|
201
|
+
}
|
|
202
|
+
function getMintAddressField(mint) {
|
|
203
|
+
const mintStr = mint.toString();
|
|
204
|
+
// Special case for SOL (system program)
|
|
205
|
+
if (mintStr === '11111111111111111111111111111112') {
|
|
206
|
+
return mintStr;
|
|
207
|
+
}
|
|
208
|
+
// For SPL tokens (USDC, USDT, etc): use first 31 bytes (248 bits)
|
|
209
|
+
// This provides better collision resistance than 8 bytes while still fitting in the field
|
|
210
|
+
// We will only suppport private SOL, USDC and USDT send, so there won't be any collision.
|
|
211
|
+
const mintBytes = mint.toBytes();
|
|
212
|
+
return new bn_js_1.default(mintBytes.slice(0, 31), 'be').toString();
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/utils/utils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBH,kDAEC;AAOD,wDAEC;AAQD,kCAEC;AAOD,wCAgEC;AAID,4CAkBC;AAGD,8CAYC;AAGD,oDAmBC;AAED,gDAiBC;AAGD,kEAYC;AAED,kDAaC;AAtND,kDAAuB;AAEvB,6CAA+B;AAC/B,8CAA6C;AAC7C,6CAA4C;AAC5C,2CAA0D;AAC1D,qCAAkC;AAClC,sCAAsC;AAEtC;;;;GAIG;AACI,KAAK,UAAU,mBAAmB,CAAC,aAAqB;IAC7D,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,GAAG,CAAC,MAAM,IAAA,kBAAS,EAAC,kBAAkB,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AACnF,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,sBAAsB,CAAC,gBAAwB;IACnE,OAAO,IAAI,CAAC,KAAK,CAAC,gBAAgB,GAAG,CAAC,MAAM,IAAA,kBAAS,EAAC,mBAAmB,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AACvF,CAAC;AAED;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,KAAW;IACrC,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,SAAgB,cAAc,CAAC,OAQ9B;IACC,gDAAgD;IAChD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,YAAY,mBAAS;QACtD,CAAC,CAAC,OAAO,CAAC,SAAS;QACnB,CAAC,CAAC,IAAI,mBAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAErC,MAAM,YAAY,GAAG,OAAO,C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AAM,CAAC,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,EAClE,sBAAU,CACX,CAAC;IAEF,MAAM,CAAC,aAAa,CAAC,GAAG,mBAAS,CAAC,sBAAsB,CACtD,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,EAClE,sBAAU,CACX,CAAC;IAEF,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAC1C,CAAC;AAED,SAAgB,mBAAmB,CAAC,IAAe;IACjD,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;IAEhC,wCAAwC;IACxC,IAAI,OAAO,KAAK,kCAAkC,EAAE,CAAC;QACnD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,kEAAkE;IAClE,0FAA0F;IAC1F,0FAA0F;IAC1F,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;IACjC,OAAO,IAAI,eAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC;AACzD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "privacycash-x402",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"prepublishOnly": "npm run build",
|
|
9
|
+
"example:server": "ts-node examples/server-example.ts",
|
|
10
|
+
"example:client": "ts-node examples/client-example.ts",
|
|
11
|
+
"dev": "concurrently \"npm run example:server\" \"npm run example:client\""
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"keywords": ["solana", "privacy", "x402", "payments", "zk-proofs"],
|
|
18
|
+
"author": "Rohan Singla rohansinglawork@gmail.com",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/Rohan-Singla/p402"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/Rohan-Singla/p402",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/yourusername/privacycash-x402/issues"
|
|
27
|
+
},
|
|
28
|
+
"description": "Privacy-preserving x402 payment SDK for Solana using Privacy Cash",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@ethersproject/keccak256": "^5.8.0",
|
|
38
|
+
"@ethersproject/sha2": "^5.8.0",
|
|
39
|
+
"@lightprotocol/hasher.rs": "^0.2.1",
|
|
40
|
+
"@solana/spl-token": "^0.4.14",
|
|
41
|
+
"@solana/web3.js": "^1.98.4",
|
|
42
|
+
"@types/bn.js": "^5.2.0",
|
|
43
|
+
"axios": "^1.13.2",
|
|
44
|
+
"bn.js": "^5.2.2",
|
|
45
|
+
"borsh": "^2.0.0",
|
|
46
|
+
"bs58": "^6.0.0",
|
|
47
|
+
"dotenv": "^17.2.3",
|
|
48
|
+
"ethers": "^6.16.0",
|
|
49
|
+
"express": "^5.2.1",
|
|
50
|
+
"node-fetch": "^3.3.2",
|
|
51
|
+
"privacycash": "^1.1.11",
|
|
52
|
+
"tweetnacl": "^1.0.3"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"nodemon": "^3.1.11",
|
|
56
|
+
"@types/express": "^5.0.6",
|
|
57
|
+
"@types/node": "^25.0.6",
|
|
58
|
+
"concurrently": "^8.2.2",
|
|
59
|
+
"ts-node": "^10.9.2",
|
|
60
|
+
"typescript": "^5.9.3"
|
|
61
|
+
}
|
|
62
|
+
}
|