@atxp/client 0.8.2 → 0.9.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.
@@ -1,108 +0,0 @@
1
- import { PaymentNetworkError, InsufficientFundsError } from './types.js';
2
- import { PublicKey, ComputeBudgetProgram, sendAndConfirmTransaction, Connection, Keypair } from '@solana/web3.js';
3
- import { ValidateTransferError as ValidateTransferError$1, createTransfer } from '@solana/pay';
4
- import { getAssociatedTokenAddress, getAccount } from '@solana/spl-token';
5
- import bs58 from 'bs58';
6
- import BigNumber from 'bignumber.js';
7
- import { generateJWT, ConsoleLogger } from '@atxp/common';
8
- import { importJWK } from 'jose';
9
-
10
- // this is a global public key for USDC on the solana mainnet
11
- const USDC_MINT = new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
12
- const ValidateTransferError = ValidateTransferError$1;
13
- class SolanaPaymentMaker {
14
- constructor(solanaEndpoint, sourceSecretKey, logger) {
15
- this.generateJWT = async ({ paymentRequestId, codeChallenge, accountId }) => {
16
- // Solana/Web3.js secretKey is 64 bytes:
17
- // first 32 bytes are the private scalar, last 32 are the public key.
18
- // JWK expects only the 32-byte private scalar for 'd'
19
- const jwk = {
20
- kty: 'OKP',
21
- crv: 'Ed25519',
22
- d: Buffer.from(this.source.secretKey.slice(0, 32)).toString('base64url'),
23
- x: Buffer.from(this.source.publicKey.toBytes()).toString('base64url'),
24
- };
25
- const privateKey = await importJWK(jwk, 'EdDSA');
26
- if (!(privateKey instanceof CryptoKey)) {
27
- throw new Error('Expected CryptoKey from importJWK');
28
- }
29
- return generateJWT(this.source.publicKey.toBase58(), privateKey, paymentRequestId || '', codeChallenge || '', accountId);
30
- };
31
- this.makePayment = async (destinations, memo, _paymentRequestId) => {
32
- // Filter to solana chain destinations
33
- const solanaDestinations = destinations.filter(d => d.chain === 'solana');
34
- if (solanaDestinations.length === 0) {
35
- this.logger.debug('SolanaPaymentMaker: No solana destinations found, cannot handle payment');
36
- return null; // Cannot handle these destinations
37
- }
38
- // Pick first solana destination
39
- const dest = solanaDestinations[0];
40
- const amount = dest.amount;
41
- const currency = dest.currency;
42
- const receiver = dest.address;
43
- if (currency.toUpperCase() !== 'USDC') {
44
- throw new PaymentNetworkError('Only USDC currency is supported; received ' + currency);
45
- }
46
- const receiverKey = new PublicKey(receiver);
47
- this.logger.info(`Making payment of ${amount} ${currency} to ${receiver} on Solana from ${this.source.publicKey.toBase58()}`);
48
- try {
49
- // Check balance before attempting payment
50
- const tokenAccountAddress = await getAssociatedTokenAddress(USDC_MINT, this.source.publicKey);
51
- const tokenAccount = await getAccount(this.connection, tokenAccountAddress);
52
- const balance = new BigNumber(tokenAccount.amount.toString()).dividedBy(10 ** 6); // USDC has 6 decimals
53
- if (balance.lt(amount)) {
54
- this.logger.warn(`Insufficient ${currency} balance for payment. Required: ${amount}, Available: ${balance}`);
55
- throw new InsufficientFundsError(currency, amount, balance, 'solana');
56
- }
57
- // Get the destination token account address (this will be the transactionId)
58
- const destinationTokenAccount = await getAssociatedTokenAddress(USDC_MINT, receiverKey);
59
- // Increase compute units to handle both memo and token transfer
60
- // Memo uses ~6000 CUs, token transfer needs ~6500 CUs
61
- const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
62
- units: 50000,
63
- });
64
- const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({
65
- microLamports: 20000,
66
- });
67
- const transaction = await createTransfer(this.connection, this.source.publicKey, {
68
- amount: amount,
69
- recipient: receiverKey,
70
- splToken: USDC_MINT,
71
- memo,
72
- });
73
- transaction.add(modifyComputeUnits);
74
- transaction.add(addPriorityFee);
75
- const transactionSignature = await sendAndConfirmTransaction(this.connection, transaction, [this.source]);
76
- // Return transaction signature as transactionId and token account address as transactionSubId
77
- return {
78
- transactionId: transactionSignature,
79
- transactionSubId: destinationTokenAccount.toBase58(),
80
- chain: 'solana',
81
- currency: 'USDC'
82
- };
83
- }
84
- catch (error) {
85
- if (error instanceof InsufficientFundsError || error instanceof PaymentNetworkError) {
86
- throw error;
87
- }
88
- // Wrap other errors in PaymentNetworkError
89
- throw new PaymentNetworkError(`Payment failed on Solana network: ${error.message}`, error);
90
- }
91
- };
92
- if (!solanaEndpoint) {
93
- throw new Error('Solana endpoint is required');
94
- }
95
- if (!sourceSecretKey) {
96
- throw new Error('Source secret key is required');
97
- }
98
- this.connection = new Connection(solanaEndpoint, { commitment: 'confirmed' });
99
- this.source = Keypair.fromSecretKey(bs58.decode(sourceSecretKey));
100
- this.logger = logger ?? new ConsoleLogger();
101
- }
102
- getSourceAddress(_params) {
103
- return this.source.publicKey.toBase58();
104
- }
105
- }
106
-
107
- export { SolanaPaymentMaker, ValidateTransferError };
108
- //# sourceMappingURL=solanaPaymentMaker.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"solanaPaymentMaker.js","sources":["../src/solanaPaymentMaker.ts"],"sourcesContent":[null],"names":["_ValidateTransferError"],"mappings":";;;;;;;;;AAYA;AACA,MAAM,SAAS,GAAG,IAAI,SAAS,CAAC,8CAA8C,CAAC;AAExE,MAAM,qBAAqB,GAAGA;MAExB,kBAAkB,CAAA;AAK7B,IAAA,WAAA,CAAY,cAAsB,EAAE,eAAuB,EAAE,MAAe,EAAA;QAgB5E,IAAA,CAAA,WAAW,GAAG,OAAM,EAAC,gBAAgB,EAAE,aAAa,EAAE,SAAS,EAAkF,KAAqB;;;;AAIpK,YAAA,MAAM,GAAG,GAAG;AACV,gBAAA,GAAG,EAAE,KAAK;AACV,gBAAA,GAAG,EAAE,SAAS;gBACd,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;AACxE,gBAAA,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAC,QAAQ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