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,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UTXO (Unspent Transaction Output) module for ZK Cash
|
|
3
|
+
*
|
|
4
|
+
* Provides UTXO functionality for the ZK Cash system
|
|
5
|
+
* Based on: https://github.com/tornadocash/tornado-nova
|
|
6
|
+
*/
|
|
7
|
+
import BN from 'bn.js';
|
|
8
|
+
import { Keypair } from './keypair';
|
|
9
|
+
import * as hasher from '@lightprotocol/hasher.rs';
|
|
10
|
+
/**
|
|
11
|
+
* Simplified Utxo class inspired by Tornado Cash Nova
|
|
12
|
+
* Based on: https://github.com/tornadocash/tornado-nova/blob/f9264eeffe48bf5e04e19d8086ee6ec58cdf0d9e/src/utxo.js
|
|
13
|
+
*/
|
|
14
|
+
export declare class Utxo {
|
|
15
|
+
amount: BN;
|
|
16
|
+
blinding: BN;
|
|
17
|
+
keypair: Keypair;
|
|
18
|
+
index: number;
|
|
19
|
+
mintAddress: string;
|
|
20
|
+
version: 'v1' | 'v2';
|
|
21
|
+
private lightWasm;
|
|
22
|
+
constructor({ lightWasm, amount,
|
|
23
|
+
/**
|
|
24
|
+
* Tornado nova doesn't use solana eddsa with curve 25519 but their own "keypair"
|
|
25
|
+
* which is:
|
|
26
|
+
* - private key: random [31;u8]
|
|
27
|
+
* - public key: PoseidonHash(privateKey)
|
|
28
|
+
*
|
|
29
|
+
* Generate a new keypair for each UTXO
|
|
30
|
+
*/
|
|
31
|
+
keypair, blinding, // Use fixed value for consistency instead of randomBN()
|
|
32
|
+
index, mintAddress, // Default to Solana native SOL mint address,
|
|
33
|
+
version }: {
|
|
34
|
+
lightWasm: hasher.LightWasm;
|
|
35
|
+
amount?: BN | number | string;
|
|
36
|
+
keypair?: Keypair;
|
|
37
|
+
blinding?: BN | number | string;
|
|
38
|
+
index?: number;
|
|
39
|
+
mintAddress?: string;
|
|
40
|
+
version?: 'v1' | 'v2';
|
|
41
|
+
});
|
|
42
|
+
getCommitment(): Promise<string>;
|
|
43
|
+
getNullifier(): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Log all the UTXO's public properties and derived values in JSON format
|
|
46
|
+
* @returns Promise that resolves once all logging is complete
|
|
47
|
+
*/
|
|
48
|
+
log(): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=utxo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utxo.d.ts","sourceRoot":"","sources":["../../../src/models/utxo.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAC;AAInD;;;GAGG;AACH,qBAAa,IAAI;IACb,MAAM,EAAE,EAAE,CAAC;IACX,QAAQ,EAAE,EAAE,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,IAAI,GAAG,IAAI,CAAC;IACrB,OAAO,CAAC,SAAS,CAAmB;gBAExB,EACR,SAAS,EACT,MAAkB;IAClB;;;;;;;OAOG;IACH,OAAO,EACP,QAAyD,EAAE,wDAAwD;IACnH,KAAS,EACT,WAAgD,EAAE,6CAA6C;IAC/F,OAAc,EACjB,EAAE;QACC,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC;QAC5B,MAAM,CAAC,EAAE,EAAE,GAAG,MAAM,GAAG,MAAM,CAAC;QAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,QAAQ,CAAC,EAAE,EAAE,GAAG,MAAM,GAAG,MAAM,CAAC;QAChC,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,OAAO,CAAC,EAAE,IAAI,GAAG,IAAI,CAAA;KACxB;IAUK,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC;IAWhC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAOrC;;;OAGG;IACG,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;CAuB7B"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* UTXO (Unspent Transaction Output) module for ZK Cash
|
|
4
|
+
*
|
|
5
|
+
* Provides UTXO functionality for the ZK Cash system
|
|
6
|
+
* Based on: https://github.com/tornadocash/tornado-nova
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Utxo = void 0;
|
|
13
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
14
|
+
const keypair_1 = require("./keypair");
|
|
15
|
+
const ethers_1 = require("ethers");
|
|
16
|
+
const utils_1 = require("../utils/utils");
|
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
+
/**
|
|
19
|
+
* Simplified Utxo class inspired by Tornado Cash Nova
|
|
20
|
+
* Based on: https://github.com/tornadocash/tornado-nova/blob/f9264eeffe48bf5e04e19d8086ee6ec58cdf0d9e/src/utxo.js
|
|
21
|
+
*/
|
|
22
|
+
class Utxo {
|
|
23
|
+
constructor({ lightWasm, amount = new bn_js_1.default(0),
|
|
24
|
+
/**
|
|
25
|
+
* Tornado nova doesn't use solana eddsa with curve 25519 but their own "keypair"
|
|
26
|
+
* which is:
|
|
27
|
+
* - private key: random [31;u8]
|
|
28
|
+
* - public key: PoseidonHash(privateKey)
|
|
29
|
+
*
|
|
30
|
+
* Generate a new keypair for each UTXO
|
|
31
|
+
*/
|
|
32
|
+
keypair, blinding = new bn_js_1.default(Math.floor(Math.random() * 1000000000)), // Use fixed value for consistency instead of randomBN()
|
|
33
|
+
index = 0, mintAddress = '11111111111111111111111111111112', // Default to Solana native SOL mint address,
|
|
34
|
+
version = 'v2' }) {
|
|
35
|
+
this.amount = new bn_js_1.default(amount.toString());
|
|
36
|
+
this.blinding = new bn_js_1.default(blinding.toString());
|
|
37
|
+
this.lightWasm = lightWasm;
|
|
38
|
+
this.keypair = keypair || new keypair_1.Keypair(ethers_1.ethers.Wallet.createRandom().privateKey, lightWasm);
|
|
39
|
+
this.index = index;
|
|
40
|
+
this.mintAddress = mintAddress;
|
|
41
|
+
this.version = version;
|
|
42
|
+
}
|
|
43
|
+
async getCommitment() {
|
|
44
|
+
// return this.lightWasm.poseidonHashString([this.amount.toString(), this.keypair.pubkey.toString(), this.blinding.toString(), this.mintAddress]);
|
|
45
|
+
const mintAddressField = (0, utils_1.getMintAddressField)(new web3_js_1.PublicKey(this.mintAddress));
|
|
46
|
+
return this.lightWasm.poseidonHashString([
|
|
47
|
+
this.amount.toString(),
|
|
48
|
+
this.keypair.pubkey.toString(),
|
|
49
|
+
this.blinding.toString(),
|
|
50
|
+
mintAddressField
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
async getNullifier() {
|
|
54
|
+
const commitmentValue = await this.getCommitment();
|
|
55
|
+
const signature = this.keypair.sign(commitmentValue, new bn_js_1.default(this.index).toString());
|
|
56
|
+
return this.lightWasm.poseidonHashString([commitmentValue, new bn_js_1.default(this.index).toString(), signature]);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Log all the UTXO's public properties and derived values in JSON format
|
|
60
|
+
* @returns Promise that resolves once all logging is complete
|
|
61
|
+
*/
|
|
62
|
+
async log() {
|
|
63
|
+
// Prepare the UTXO data object
|
|
64
|
+
const utxoData = {
|
|
65
|
+
amount: this.amount.toString(),
|
|
66
|
+
blinding: this.blinding.toString(),
|
|
67
|
+
index: this.index,
|
|
68
|
+
mintAddress: this.mintAddress,
|
|
69
|
+
keypair: {
|
|
70
|
+
pubkey: this.keypair.pubkey.toString()
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
// Add derived values
|
|
74
|
+
try {
|
|
75
|
+
utxoData.commitment = await this.getCommitment();
|
|
76
|
+
utxoData.nullifier = await this.getNullifier();
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
utxoData.error = error.message;
|
|
80
|
+
}
|
|
81
|
+
// Output as formatted JSON
|
|
82
|
+
console.log(JSON.stringify(utxoData, null, 2));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.Utxo = Utxo;
|
|
86
|
+
//# sourceMappingURL=utxo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utxo.js","sourceRoot":"","sources":["../../../src/models/utxo.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,kDAAuB;AACvB,uCAAoC;AAEpC,mCAAgC;AAChC,0CAAqD;AACrD,6CAA4C;AAC5C;;;GAGG;AACH,MAAa,IAAI;IASb,YAAY,EACR,SAAS,EACT,MAAM,GAAG,IAAI,eAAE,CAAC,CAAC,CAAC;IAClB;;;;;;;OAOG;IACH,OAAO,EACP,QAAQ,GAAG,IAAI,eAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,UAAU,CAAC,CAAC,EAAE,wDAAwD;IACnH,KAAK,GAAG,CAAC,EACT,WAAW,GAAG,kCAAkC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C,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC;IAC1G,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,GAAG;QACL,+BAA+B;QAC/B,MAAM,QAAQ,GAAQ;YAClB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YAC9B,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE;YAClC,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,OAAO,EAAE;gBACL,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE;aACzC;SACJ,CAAC;QAEF,qBAAqB;QACrB,IAAI,CAAC;YACD,QAAQ,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;YACjD,QAAQ,CAAC,SAAS,GAAG,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;QACnD,CAAC;QAAC,OAAO,KAAU,EAAE,CAAC;YAClB,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC;QACnC,CAAC;QAED,2BAA2B;QAC3B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IACnD,CAAC;CACJ;AAxFD,oBAwFC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { X402ServerConfig } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* X402 Payment Server for Privacy Cash
|
|
5
|
+
* Handles HTTP 402 payment verification with privacy-preserving payments
|
|
6
|
+
*/
|
|
7
|
+
export declare class X402PaymentServer {
|
|
8
|
+
private config;
|
|
9
|
+
private connection;
|
|
10
|
+
private usedCommitments;
|
|
11
|
+
private pendingPayments;
|
|
12
|
+
private merchantClient;
|
|
13
|
+
private encryptionService;
|
|
14
|
+
private withdrawalInterval;
|
|
15
|
+
private network;
|
|
16
|
+
constructor(config: X402ServerConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Initialize merchant Privacy Cash client for withdrawals
|
|
19
|
+
*/
|
|
20
|
+
private initializeMerchant;
|
|
21
|
+
/**
|
|
22
|
+
* Create Express middleware for x402 payment flow
|
|
23
|
+
*/
|
|
24
|
+
middleware(): RequestHandler;
|
|
25
|
+
/**
|
|
26
|
+
* Generate payment quote for 402 response
|
|
27
|
+
*/
|
|
28
|
+
private generateQuote;
|
|
29
|
+
/**
|
|
30
|
+
* Verify x402 payment
|
|
31
|
+
*/
|
|
32
|
+
private verifyPayment;
|
|
33
|
+
/**
|
|
34
|
+
* Verify balance proof from client
|
|
35
|
+
* Checks that the proof is recent and balance meets requirement
|
|
36
|
+
*/
|
|
37
|
+
private verifyBalance;
|
|
38
|
+
/**
|
|
39
|
+
* Hash function for commitments
|
|
40
|
+
*/
|
|
41
|
+
private hashNote;
|
|
42
|
+
/**
|
|
43
|
+
* Start background withdrawal processor
|
|
44
|
+
*/
|
|
45
|
+
private startWithdrawalProcessor;
|
|
46
|
+
/**
|
|
47
|
+
* Process pending merchant withdrawals
|
|
48
|
+
*/
|
|
49
|
+
private processMerchantWithdrawals;
|
|
50
|
+
/**
|
|
51
|
+
* Get server statistics
|
|
52
|
+
*/
|
|
53
|
+
getStats(): {
|
|
54
|
+
usedCommitments: number;
|
|
55
|
+
pendingPayments: number;
|
|
56
|
+
totalPendingAmount: number;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Stop the server (cleanup)
|
|
60
|
+
*/
|
|
61
|
+
stop(): void;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create x402 middleware function (convenience export)
|
|
65
|
+
*/
|
|
66
|
+
export declare function createX402Middleware(config: X402ServerConfig): RequestHandler;
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAInF,OAAO,EACL,gBAAgB,EAMjB,MAAM,UAAU,CAAC;AAYlB;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,eAAe,CAA0B;IACjD,OAAO,CAAC,eAAe,CAA0C;IACjE,OAAO,CAAC,cAAc,CAA4B;IAClD,OAAO,CAAC,iBAAiB,CAAkC;IAC3D,OAAO,CAAC,kBAAkB,CAA+B;IACzD,OAAO,CAAC,OAAO,CAAgB;gBAEnB,MAAM,EAAE,gBAAgB;IAcpC;;OAEG;YACW,kBAAkB;IAoBhC;;OAEG;IACH,UAAU,IAAI,cAAc;IA+B5B;;OAEG;IACH,OAAO,CAAC,aAAa;IAkBrB;;OAEG;YACW,aAAa;IA6D3B;;;OAGG;IACH,OAAO,CAAC,aAAa;IA0BrB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAIhB;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAOhC;;OAEG;YACW,0BAA0B;IA4CxC;;OAEG;IACH,QAAQ,IAAI;QACV,eAAe,EAAE,MAAM,CAAC;QACxB,eAAe,EAAE,MAAM,CAAC;QACxB,kBAAkB,EAAE,MAAM,CAAC;KAC5B;IAUD;;OAEG;IACH,IAAI,IAAI,IAAI;CAKb;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,gBAAgB,GAAG,cAAc,CAG7E"}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.X402PaymentServer = void 0;
|
|
7
|
+
exports.createX402Middleware = createX402Middleware;
|
|
8
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
const privacycash_1 = require("privacycash");
|
|
11
|
+
const errors_1 = require("../errors");
|
|
12
|
+
const encryption_1 = require("../utils/encryption");
|
|
13
|
+
const constants_1 = require("../utils/constants");
|
|
14
|
+
/**
|
|
15
|
+
* X402 Payment Server for Privacy Cash
|
|
16
|
+
* Handles HTTP 402 payment verification with privacy-preserving payments
|
|
17
|
+
*/
|
|
18
|
+
class X402PaymentServer {
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.usedCommitments = new Set();
|
|
21
|
+
this.pendingPayments = new Map();
|
|
22
|
+
this.merchantClient = null;
|
|
23
|
+
this.encryptionService = null;
|
|
24
|
+
this.withdrawalInterval = null;
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.connection = new web3_js_1.Connection(config.rpcUrl, 'confirmed');
|
|
27
|
+
this.network = config.rpcUrl.includes('devnet') ? 'devnet' : 'mainnet-beta';
|
|
28
|
+
// Initialize merchant client if keypair provided
|
|
29
|
+
if (config.merchantKeypair) {
|
|
30
|
+
this.initializeMerchant(config.merchantKeypair);
|
|
31
|
+
}
|
|
32
|
+
// Start withdrawal processor
|
|
33
|
+
this.startWithdrawalProcessor();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Initialize merchant Privacy Cash client for withdrawals
|
|
37
|
+
*/
|
|
38
|
+
async initializeMerchant(keypairBytes) {
|
|
39
|
+
try {
|
|
40
|
+
const keypair = web3_js_1.Keypair.fromSecretKey(keypairBytes);
|
|
41
|
+
// Initialize encryption service
|
|
42
|
+
this.encryptionService = new encryption_1.EncryptionService();
|
|
43
|
+
this.encryptionService.deriveEncryptionKeyFromWallet(keypair);
|
|
44
|
+
// Initialize Privacy Cash client
|
|
45
|
+
this.merchantClient = new privacycash_1.PrivacyCash({
|
|
46
|
+
RPC_url: this.config.rpcUrl,
|
|
47
|
+
owner: keypair,
|
|
48
|
+
});
|
|
49
|
+
console.log('Merchant Privacy Cash client initialized');
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error('Failed to initialize merchant client:', error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Create Express middleware for x402 payment flow
|
|
57
|
+
*/
|
|
58
|
+
middleware() {
|
|
59
|
+
return async (req, res, next) => {
|
|
60
|
+
const xPaymentHeader = req.header('X-Payment');
|
|
61
|
+
const commitment = req.header('X-Privacy-Commitment');
|
|
62
|
+
const walletAddress = req.header('X-Wallet-Address');
|
|
63
|
+
// No payment header - return 402 with quote
|
|
64
|
+
if (!xPaymentHeader) {
|
|
65
|
+
const quote = this.generateQuote();
|
|
66
|
+
res.status(402).json(quote);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// Verify payment
|
|
70
|
+
try {
|
|
71
|
+
await this.verifyPayment(xPaymentHeader, commitment, walletAddress);
|
|
72
|
+
// Payment verified - continue to protected endpoint
|
|
73
|
+
next();
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (error instanceof errors_1.X402Error) {
|
|
77
|
+
res.status(error.statusCode).json(error.toJSON());
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
res.status(500).json({
|
|
81
|
+
error: 'Payment verification failed',
|
|
82
|
+
details: error instanceof Error ? error.message : 'Unknown error',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Generate payment quote for 402 response
|
|
90
|
+
*/
|
|
91
|
+
generateQuote() {
|
|
92
|
+
return {
|
|
93
|
+
payment: {
|
|
94
|
+
recipientWallet: this.config.merchantWallet,
|
|
95
|
+
tokenSymbol: 'SOL',
|
|
96
|
+
amount: this.config.apiPrice,
|
|
97
|
+
cluster: this.network,
|
|
98
|
+
},
|
|
99
|
+
message: 'Payment required. Deposit into Privacy Cash pool and provide payment proof.',
|
|
100
|
+
instructions: [
|
|
101
|
+
'1. Deposit SOL into Privacy Cash pool',
|
|
102
|
+
'2. Generate payment commitment',
|
|
103
|
+
'3. Send X-Payment header with proof',
|
|
104
|
+
'4. Include X-Privacy-Commitment and X-Wallet-Address headers',
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Verify x402 payment
|
|
110
|
+
*/
|
|
111
|
+
async verifyPayment(xPaymentHeader, commitment, walletAddress) {
|
|
112
|
+
// Decode x402 header
|
|
113
|
+
let decoded;
|
|
114
|
+
try {
|
|
115
|
+
decoded = JSON.parse(Buffer.from(xPaymentHeader, 'base64').toString('utf8'));
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
throw new errors_1.InvalidPaymentError('Invalid X-Payment header encoding');
|
|
119
|
+
}
|
|
120
|
+
// Validate scheme
|
|
121
|
+
if (decoded.scheme !== 'privacycash') {
|
|
122
|
+
throw new errors_1.InvalidPaymentError(`Invalid payment scheme: ${decoded.scheme}`);
|
|
123
|
+
}
|
|
124
|
+
// Validate network
|
|
125
|
+
const expectedNetwork = `solana-${this.network}`;
|
|
126
|
+
if (decoded.network !== expectedNetwork) {
|
|
127
|
+
throw new errors_1.NetworkMismatchError(expectedNetwork, decoded.network);
|
|
128
|
+
}
|
|
129
|
+
// Validate required headers
|
|
130
|
+
if (!commitment || !walletAddress) {
|
|
131
|
+
throw new errors_1.InvalidPaymentError('Missing X-Privacy-Commitment or X-Wallet-Address header');
|
|
132
|
+
}
|
|
133
|
+
// Verify commitment hash matches
|
|
134
|
+
const computedHash = this.hashNote(commitment);
|
|
135
|
+
if (computedHash !== decoded.payload.noteHash) {
|
|
136
|
+
throw new errors_1.InvalidPaymentError('Commitment hash mismatch');
|
|
137
|
+
}
|
|
138
|
+
// Check for double-spend
|
|
139
|
+
if (this.usedCommitments.has(commitment)) {
|
|
140
|
+
throw new errors_1.DoubleSpendError(commitment);
|
|
141
|
+
}
|
|
142
|
+
// Verify balance proof
|
|
143
|
+
const hasBalance = this.verifyBalance(decoded.payload.balanceProof, this.config.apiPrice);
|
|
144
|
+
if (!hasBalance) {
|
|
145
|
+
throw new errors_1.InsufficientBalanceError(this.config.apiPrice);
|
|
146
|
+
}
|
|
147
|
+
// Mark commitment as used
|
|
148
|
+
this.usedCommitments.add(commitment);
|
|
149
|
+
console.log('Commitment verified and marked as used');
|
|
150
|
+
// Queue for merchant withdrawal
|
|
151
|
+
this.pendingPayments.set(commitment, {
|
|
152
|
+
commitment,
|
|
153
|
+
amount: this.config.apiPrice,
|
|
154
|
+
userWallet: walletAddress,
|
|
155
|
+
timestamp: Date.now(),
|
|
156
|
+
verified: true,
|
|
157
|
+
});
|
|
158
|
+
console.log('Queued for merchant withdrawal');
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Verify balance proof from client
|
|
162
|
+
* Checks that the proof is recent and balance meets requirement
|
|
163
|
+
*/
|
|
164
|
+
verifyBalance(balanceProof, required) {
|
|
165
|
+
try {
|
|
166
|
+
const proof = JSON.parse(Buffer.from(balanceProof, 'base64').toString('utf8'));
|
|
167
|
+
// Check timestamp is recent (within validity window)
|
|
168
|
+
if (Date.now() - proof.timestamp > constants_1.BALANCE_PROOF_VALIDITY_MS) {
|
|
169
|
+
console.log('Balance proof expired');
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
// Check balance meets requirement
|
|
173
|
+
if (proof.balance < required) {
|
|
174
|
+
console.log(`Insufficient balance: ${proof.balance} < ${required}`);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
console.log(`Balance proof verified: ${proof.balance} >= ${required}`);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
console.error('Failed to parse balance proof:', error);
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Hash function for commitments
|
|
187
|
+
*/
|
|
188
|
+
hashNote(note) {
|
|
189
|
+
return crypto_1.default.createHash('sha256').update(note).digest('hex');
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Start background withdrawal processor
|
|
193
|
+
*/
|
|
194
|
+
startWithdrawalProcessor() {
|
|
195
|
+
// Run every 5 minutes
|
|
196
|
+
this.withdrawalInterval = setInterval(() => {
|
|
197
|
+
this.processMerchantWithdrawals();
|
|
198
|
+
}, 5 * 60 * 1000);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Process pending merchant withdrawals
|
|
202
|
+
*/
|
|
203
|
+
async processMerchantWithdrawals() {
|
|
204
|
+
const paymentsToProcess = Array.from(this.pendingPayments.values()).filter((p) => p.verified);
|
|
205
|
+
if (paymentsToProcess.length === 0) {
|
|
206
|
+
console.log('No pending withdrawals');
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
console.log(`\nProcessing ${paymentsToProcess.length} merchant withdrawals...`);
|
|
210
|
+
// If no merchant client, just log
|
|
211
|
+
if (!this.merchantClient) {
|
|
212
|
+
console.log('Merchant client not initialized - withdrawals simulated');
|
|
213
|
+
for (const payment of paymentsToProcess) {
|
|
214
|
+
console.log(` Simulated withdrawal: ${payment.amount} lamports`);
|
|
215
|
+
this.pendingPayments.delete(payment.commitment);
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
// Calculate total to withdraw
|
|
220
|
+
const total = paymentsToProcess.reduce((sum, p) => sum + p.amount, 0);
|
|
221
|
+
try {
|
|
222
|
+
// Actually withdraw from Privacy Cash
|
|
223
|
+
const result = await this.merchantClient.withdraw({
|
|
224
|
+
lamports: total,
|
|
225
|
+
recipientAddress: this.config.merchantWallet,
|
|
226
|
+
});
|
|
227
|
+
console.log(`Withdrawal successful: ${total} lamports`);
|
|
228
|
+
console.log(`Transaction: ${result.tx}`);
|
|
229
|
+
// Clear processed payments
|
|
230
|
+
for (const payment of paymentsToProcess) {
|
|
231
|
+
this.pendingPayments.delete(payment.commitment);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
console.error('Withdrawal failed:', error);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get server statistics
|
|
240
|
+
*/
|
|
241
|
+
getStats() {
|
|
242
|
+
return {
|
|
243
|
+
usedCommitments: this.usedCommitments.size,
|
|
244
|
+
pendingPayments: this.pendingPayments.size,
|
|
245
|
+
totalPendingAmount: Array.from(this.pendingPayments.values()).reduce((sum, p) => sum + p.amount, 0) /
|
|
246
|
+
1e9,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Stop the server (cleanup)
|
|
251
|
+
*/
|
|
252
|
+
stop() {
|
|
253
|
+
if (this.withdrawalInterval) {
|
|
254
|
+
clearInterval(this.withdrawalInterval);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
exports.X402PaymentServer = X402PaymentServer;
|
|
259
|
+
/**
|
|
260
|
+
* Create x402 middleware function (convenience export)
|
|
261
|
+
*/
|
|
262
|
+
function createX402Middleware(config) {
|
|
263
|
+
const server = new X402PaymentServer(config);
|
|
264
|
+
return server.middleware();
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":";;;;;;AA6TA,oDAGC;AA/TD,6CAAiE;AACjE,oDAA4B;AAC5B,6CAA0C;AAS1C,sCAOmB;AACnB,oDAAwD;AACxD,kDAA+D;AAE/D;;;GAGG;AACH,MAAa,iBAAiB;IAU5B,YAAY,MAAwB;QAP5B,oBAAe,GAAgB,IAAI,GAAG,EAAE,CAAC;QACzC,oBAAe,GAAgC,IAAI,GAAG,EAAE,CAAC;QACzD,mBAAc,G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YACL,eAAe,EAAE,IAAI,CAAC,eAAe,CAAC,IAAI;YAC1C,eAAe,EAAE,IAAI,CAAC,eAAe,CAAC,IAAI;YAC1C,kBAAkB,EAChB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;gBAC/E,GAAG;SACN,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC5B,aAAa,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;CACF;AA7RD,8CA6RC;AAED;;GAEG;AACH,SAAgB,oBAAoB,CAAC,MAAwB;IAC3D,MAAM,MAAM,GAAG,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC7C,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PublicKey, Keypair } from '@solana/web3.js';
|
|
2
|
+
export type SolanaNetwork = 'devnet' | 'mainnet-beta';
|
|
3
|
+
export type TokenSymbol = 'SOL' | 'USDC' | 'USDT';
|
|
4
|
+
export interface PaymentQuote {
|
|
5
|
+
payment: {
|
|
6
|
+
recipientWallet: string;
|
|
7
|
+
tokenSymbol: TokenSymbol;
|
|
8
|
+
amount: number;
|
|
9
|
+
cluster: SolanaNetwork;
|
|
10
|
+
};
|
|
11
|
+
message: string;
|
|
12
|
+
instructions?: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface PaymentPayload {
|
|
15
|
+
x402Version: number;
|
|
16
|
+
scheme: 'privacycash';
|
|
17
|
+
network: `solana-${SolanaNetwork}`;
|
|
18
|
+
payload: {
|
|
19
|
+
noteHash: string;
|
|
20
|
+
commitment: string;
|
|
21
|
+
timestamp: number;
|
|
22
|
+
balanceProof: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface BalanceProof {
|
|
26
|
+
balance: number;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
wallet: string;
|
|
29
|
+
}
|
|
30
|
+
export interface X402ServerConfig {
|
|
31
|
+
merchantWallet: string;
|
|
32
|
+
rpcUrl: string;
|
|
33
|
+
apiPrice: number;
|
|
34
|
+
merchantKeypair?: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
export interface X402ClientConfig {
|
|
37
|
+
rpcUrl: string;
|
|
38
|
+
keypair: Keypair;
|
|
39
|
+
mockMode?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface WalletAdapter {
|
|
42
|
+
publicKey: PublicKey;
|
|
43
|
+
signMessage(message: Uint8Array): Promise<Uint8Array>;
|
|
44
|
+
}
|
|
45
|
+
export interface PendingPayment {
|
|
46
|
+
commitment: string;
|
|
47
|
+
amount: number;
|
|
48
|
+
userWallet: string;
|
|
49
|
+
timestamp: number;
|
|
50
|
+
verified: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface PrivateBalance {
|
|
53
|
+
lamports: number;
|
|
54
|
+
sol: number;
|
|
55
|
+
}
|
|
56
|
+
export interface PaymentResult {
|
|
57
|
+
success: boolean;
|
|
58
|
+
data: any;
|
|
59
|
+
paymentDetails?: {
|
|
60
|
+
noteHash: string;
|
|
61
|
+
commitment: string;
|
|
62
|
+
verified: boolean;
|
|
63
|
+
timestamp: string;
|
|
64
|
+
amount: number;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGrD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,cAAc,CAAC;A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f,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAGD,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;CACb;AAGD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC;IACV,cAAc,CAAC,EAAE;QACf,QAAQ,EAAE,MAAM,CAAC;QACjB,UAAU,EAAE,MAAM,CAAC;QACnB,QAAQ,EAAE,OAAO,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import BN from 'bn.js';
|
|
3
|
+
export declare const FIELD_SIZE: BN;
|
|
4
|
+
export declare const PROGRAM_ID: PublicKey;
|
|
5
|
+
export declare const FEE_RECIPIENT: PublicKey;
|
|
6
|
+
export declare const FETCH_UTXOS_GROUP_SIZE = 20000;
|
|
7
|
+
export declare const TRANSACT_IX_DISCRIMINATOR: Buffer<ArrayBuffer>;
|
|
8
|
+
export declare const TRANSACT_SPL_IX_DISCRIMINATOR: Buffer<ArrayBuffer>;
|
|
9
|
+
export declare const MERKLE_TREE_DEPTH = 26;
|
|
10
|
+
export declare const ALT_ADDRESS: PublicKey;
|
|
11
|
+
export declare const RELAYER_API_URL: string;
|
|
12
|
+
export declare const SIGN_MESSAGE = "Privacy Money account sign in";
|
|
13
|
+
export declare const LSK_FETCH_OFFSET = "fetch_offset";
|
|
14
|
+
export declare const LSK_ENCRYPTED_OUTPUTS = "encrypted_outputs";
|
|
15
|
+
export declare const USDC_MINT: PublicKey;
|
|
16
|
+
declare const tokenList: readonly ["sol", "usdc", "usdt", "zec", "ore", "store"];
|
|
17
|
+
export type TokenList = typeof tokenList[number];
|
|
18
|
+
declare const splList: readonly ["usdc", "usdt", "zec", "ore", "store"];
|
|
19
|
+
export type SplList = typeof splList[number];
|
|
20
|
+
export type Token = {
|
|
21
|
+
name: TokenList;
|
|
22
|
+
prefix: string;
|
|
23
|
+
units_per_token: number;
|
|
24
|
+
pubkey: PublicKey;
|
|
25
|
+
};
|
|
26
|
+
export declare const tokens: Token[];
|
|
27
|
+
export declare const BALANCE_PROOF_VALIDITY_MS: number;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/utils/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,MAAM,OAAO,CAAC;AAEvB,eAAO,MAAM,UAAU,IAA0F,CAAA;AAEjH,eAAO,MAAM,UAAU,WAAyJ,CAAC;AAEjL,eAAO,MAAM,aAAa,WAAgE,CAAA;AAE1F,eAAO,MAAM,sBAAsB,QAAS,CAAA;AAE5C,eAAO,MAAM,yBAAyB,qBAAuD,CAAC;AAE9F,eAAO,MAAM,6BAA6B,qBAAsD,CAAC;AAEjG,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,eAAO,MAAM,WAAW,WAA2J,CAAC;AAEpL,eAAO,MAAM,eAAe,QAA4E,CAAC;AAEzG,eAAO,MAAM,YAAY,kCAAkC,CAAA;AAG3D,eAAO,MAAM,gBAAgB,iBAAiB,CAAA;AAC9C,eAAO,MAAM,qBAAqB,sBAAsB,CAAA;AAExD,eAAO,MAAM,SAAS,WAAuJ,CAAA;AAE7K,QAAA,MAAM,SAAS,yDAA0D,CAAC;AAC1E,MAAM,MAAM,SAAS,GAAG,OAAO,SAAS,CAAC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