@chipi-stack/backend 12.8.0 → 13.1.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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isValidApiKey, ChipiAuthError, STARKNET_NETWORKS, API_VERSIONING, validateErrorResponse, ChipiApiError, handleApiError, WALLET_RPC_ENDPOINTS, WALLET_CLASS_HASHES, API_ENDPOINTS, ChipiTransactionError, formatAmount, getUsdAmount, SKU_CONTRACTS, CARRIER_IDS, SERVICE_TYPES, CHAIN_TOKEN_TYPES, CHAIN_TYPES, ChipiSessionError, SESSION_ERRORS, SESSION_DEFAULTS, SESSION_ENTRYPOINTS, CONTRACT_ADDRESSES } from '@chipi-stack/shared';
1
+ import { isValidApiKey, ChipiAuthError, STARKNET_NETWORKS, API_VERSIONING, validateErrorResponse, ChipiApiError, handleApiError, WALLET_RPC_ENDPOINTS, WALLET_CLASS_HASHES, API_ENDPOINTS, ChipiTransactionError, formatAmount, SKU_CONTRACTS, CARRIER_IDS, SERVICE_TYPES, ChipiSessionError, SESSION_ERRORS, SESSION_DEFAULTS, SESSION_ENTRYPOINTS, CONTRACT_ADDRESSES } from '@chipi-stack/shared';
2
2
  import CryptoES from 'crypto-es';
3
3
  import { RpcProvider, ec, hash, Account, num, CairoCustomEnum, CairoOption, CairoOptionVariant, CallData, cairo, typedData } from 'starknet';
4
4
  import { STARKNET_CONTRACTS } from '@chipi-stack/types';
@@ -199,7 +199,8 @@ var ChipiWallets = class {
199
199
  userId,
200
200
  walletType = "CHIPI",
201
201
  // Default to CHIPI wallet
202
- usePasskey
202
+ usePasskey,
203
+ chain
203
204
  } = params;
204
205
  if (!encryptKey) {
205
206
  if (usePasskey) {
@@ -229,6 +230,7 @@ var ChipiWallets = class {
229
230
  endpoint: `${API_ENDPOINTS.CHIPI_WALLETS}/prepare-creation`,
230
231
  bearerToken,
231
232
  body: {
233
+ chain,
232
234
  publicKey,
233
235
  walletType,
234
236
  starkKeyPubAX
@@ -252,6 +254,7 @@ var ChipiWallets = class {
252
254
  userId,
253
255
  publicKey,
254
256
  walletType,
257
+ chain,
255
258
  userSignature: {
256
259
  r: userSignature.r.toString(),
257
260
  s: userSignature.s.toString(),
@@ -369,7 +372,7 @@ var executePaymasterTransaction = async ({
369
372
  wallet.publicKey,
370
373
  privateKeyDecrypted
371
374
  );
372
- const response = await client.post({
375
+ const { typedData: typedData2, walletType } = await client.post({
373
376
  endpoint: "/transactions/prepare-typed-data",
374
377
  bearerToken,
375
378
  body: {
@@ -378,15 +381,6 @@ var executePaymasterTransaction = async ({
378
381
  accountClassHash
379
382
  }
380
383
  });
381
- let walletType;
382
- let typedData2;
383
- if (response.walletType) {
384
- walletType = response.walletType;
385
- typedData2 = response.typedData;
386
- } else {
387
- typedData2 = response;
388
- walletType = "READY";
389
- }
390
384
  const userSignature = await account.signMessage(typedData2);
391
385
  const result = await client.post({
392
386
  endpoint: "/transactions/execute-sponsored-transaction",
@@ -404,7 +398,6 @@ var executePaymasterTransaction = async ({
404
398
  }
405
399
  });
406
400
  if (!result.transactionHash) {
407
- console.error("result", result);
408
401
  throw new Error("The response does not contain the transaction hash");
409
402
  }
410
403
  return result.transactionHash;
@@ -478,7 +471,7 @@ var executePaymasterTransactionWithSession = async ({
478
471
  );
479
472
  }
480
473
  const accountClassHash = WALLET_CLASS_HASHES.CHIPI;
481
- const prepareResp = await client.post({
474
+ const typeDataResult = await client.post({
482
475
  endpoint: "/transactions/prepare-typed-data",
483
476
  bearerToken,
484
477
  body: {
@@ -488,13 +481,6 @@ var executePaymasterTransactionWithSession = async ({
488
481
  walletType: "CHIPI"
489
482
  }
490
483
  });
491
- const typeDataResult = prepareResp?.typedData || prepareResp?.typed_data || prepareResp;
492
- if (!typeDataResult || !typeDataResult.message || !typeDataResult.domain || !typeDataResult.types) {
493
- console.error("[ChipiSDK:Session:Execute] Invalid typed data response", {
494
- prepareResp
495
- });
496
- throw new Error("Invalid typed data response from prepare-typed-data");
497
- }
498
484
  const msgHash = typedData.getMessageHash(typeDataResult, wallet.publicKey);
499
485
  const { r, s } = ec.starkCurve.sign(msgHash, sessionPrivateKey);
500
486
  const sessionSignature = [
@@ -512,7 +498,7 @@ var executePaymasterTransactionWithSession = async ({
512
498
  // Session signature format - array of 4 hex strings
513
499
  userSignature: sessionSignature,
514
500
  saveToDatabase,
515
- walletType: wallet.walletType,
501
+ walletType: "CHIPI",
516
502
  isSessionSignature: true
517
503
  // Flag to indicate session signature format
518
504
  }
@@ -663,27 +649,59 @@ var ChipiTransactions = class {
663
649
  return response;
664
650
  }
665
651
  };
666
- var ChipiSkuTransactions = class {
652
+ var ChipiExchanges = class {
653
+ constructor(client) {
654
+ this.client = client;
655
+ }
656
+ async getUsdAmount(currencyAmount, currency, bearerToken) {
657
+ return this.client.get({
658
+ endpoint: `${API_ENDPOINTS.EXCHANGES}/usd-amount`,
659
+ bearerToken,
660
+ params: { currencyAmount, currency }
661
+ });
662
+ }
663
+ };
664
+
665
+ // src/sku-purchases.ts
666
+ var ChipiSkuPurchases = class {
667
667
  constructor(client) {
668
668
  this.client = client;
669
669
  this.transactions = new ChipiTransactions(client);
670
+ this.exchanges = new ChipiExchanges(client);
670
671
  }
671
672
  /**
672
- * Creates a SKU transaction with gasless execution
673
- * @param params - Transaction parameters including amount, SKU ID, wallet, etc.
673
+ * Purchase a SKU with gasless execution
674
+ * @param params - Purchase parameters including transaction hash, SKU ID, wallet, etc.
674
675
  * @param bearerToken - Authentication token for API calls
675
- * @returns Promise<SkuTransaction> - The created transaction record
676
+ * @returns Promise<SkuPurchaseResponse> - The purchase response with transaction details
676
677
  */
677
- async createSkuTransaction(params, bearerToken) {
678
- const { mxnAmount, skuId, wallet, reference, externalUserId } = params;
679
- this.client.baseUrl;
680
- const usdAmount = await getUsdAmount(mxnAmount, bearerToken, this.client);
681
- const tokenAddress = STARKNET_CONTRACTS.USDC.contractAddress;
678
+ async purchaseSku(params, bearerToken) {
679
+ const {
680
+ skuId,
681
+ skuReference,
682
+ currencyAmount,
683
+ currency,
684
+ chain,
685
+ token,
686
+ encryptKey,
687
+ wallet
688
+ } = params;
689
+ const usdAmount = await this.exchanges.getUsdAmount(
690
+ currencyAmount,
691
+ currency,
692
+ bearerToken
693
+ );
694
+ const tokenAddress = STARKNET_CONTRACTS[token].contractAddress;
682
695
  if (usdAmount <= 0) {
683
- throw new Error(`Invalid USD amount: ${usdAmount}. Amount must be positive.`);
696
+ throw new Error(
697
+ `Invalid USD amount: ${usdAmount}. Amount must be positive.`
698
+ );
684
699
  }
685
700
  const minUsdAmount = Math.max(usdAmount, 1e-6);
686
- const parsedAmount = formatAmount(minUsdAmount.toString(), STARKNET_CONTRACTS.USDC.decimals);
701
+ const parsedAmount = formatAmount(
702
+ minUsdAmount.toString(),
703
+ STARKNET_CONTRACTS[token].decimals
704
+ );
687
705
  const calls = [
688
706
  {
689
707
  contractAddress: tokenAddress,
@@ -715,49 +733,47 @@ var ChipiSkuTransactions = class {
715
733
  metadata: {
716
734
  service_type: SERVICE_TYPES.BUY_SERVICE,
717
735
  timestamp: Date.now(),
718
- chipi_user_id: hash.starknetKeccak(externalUserId || "0x0").toString(),
736
+ chipi_user_id: hash.starknetKeccak(wallet.publicKey || "0x0").toString(),
719
737
  kyc: true,
720
738
  amount: cairo.uint256(parsedAmount)
721
739
  }
722
740
  })
723
741
  }
724
742
  ];
725
- const transactionHash = await this.transactions.executeTransaction({
743
+ const txHash = await this.transactions.executeTransaction({
726
744
  params: {
727
- encryptKey: params.encryptKey,
745
+ encryptKey,
728
746
  wallet,
729
747
  calls
730
748
  },
731
749
  bearerToken,
732
750
  saveToDatabase: false
733
- // Internal: SKU transactions don't save to transactions table
734
751
  });
735
- const input = {
752
+ const body = {
753
+ transactionHash: txHash,
736
754
  walletAddress: wallet.publicKey,
737
755
  skuId,
738
- chain: CHAIN_TYPES.STARKNET,
739
- chainToken: CHAIN_TOKEN_TYPES.USDC,
740
- mxnAmount,
741
- reference,
742
- transactionHash,
743
- usdAmount: minUsdAmount
756
+ skuReference,
757
+ chain,
758
+ token,
759
+ currencyAmount
744
760
  };
745
761
  const response = await this.client.post({
746
- endpoint: `${API_ENDPOINTS.SKU_TRANSACTIONS}`,
762
+ endpoint: `${API_ENDPOINTS.SKU_PURCHASES}`,
747
763
  bearerToken,
748
- body: input
764
+ body
749
765
  });
750
766
  return response;
751
767
  }
752
768
  /**
753
- * Get a SKU transaction by transaction id
754
- * @param id - The SKU transaction id
769
+ * Get a SKU purchase by id
770
+ * @param id - The SKU purchase id
755
771
  * @param bearerToken - Authentication token for API calls
756
- * @returns Promise<SkuTransaction> - Single SKU transaction
772
+ * @returns Promise<SkuPurchaseResponse> - Single SKU purchase
757
773
  */
758
- async getSkuTransaction(id, bearerToken) {
774
+ async getSkuPurchase(id, bearerToken) {
759
775
  const response = await this.client.get({
760
- endpoint: `${API_ENDPOINTS.SKU_TRANSACTIONS}/${id}`,
776
+ endpoint: `${API_ENDPOINTS.SKU_PURCHASES}/${id}`,
761
777
  bearerToken
762
778
  });
763
779
  return response;
@@ -1216,10 +1232,11 @@ var ChipiSDK = class {
1216
1232
  this.apiSecretKey = config.apiSecretKey;
1217
1233
  this.wallets = new ChipiWallets(this.client);
1218
1234
  this.transactions = new ChipiTransactions(this.client);
1219
- this.skuTransactions = new ChipiSkuTransactions(this.client);
1235
+ this.skuPurchases = new ChipiSkuPurchases(this.client);
1220
1236
  this.skus = new ChipiSkus(this.client);
1221
1237
  this.users = new Users(this.client);
1222
1238
  this.sessions = new ChipiSessions(this.client);
1239
+ this.exchanges = new ChipiExchanges(this.client);
1223
1240
  this.executeTransaction = this.executeTransaction.bind(this);
1224
1241
  this.executeTransactionWithSession = this.executeTransactionWithSession.bind(this);
1225
1242
  this.transfer = this.transfer.bind(this);
@@ -1229,7 +1246,8 @@ var ChipiSDK = class {
1229
1246
  this.callAnyContract = this.callAnyContract.bind(this);
1230
1247
  this.createWallet = this.createWallet.bind(this);
1231
1248
  this.recordSendTransaction = this.recordSendTransaction.bind(this);
1232
- this.createSkuTransaction = this.createSkuTransaction.bind(this);
1249
+ this.purchaseSku = this.purchaseSku.bind(this);
1250
+ this.getUsdAmount = this.getUsdAmount.bind(this);
1233
1251
  }
1234
1252
  /**
1235
1253
  * Resolve bearer token - uses provided token or falls back to apiSecretKey
@@ -1444,17 +1462,26 @@ var ChipiSDK = class {
1444
1462
  });
1445
1463
  }
1446
1464
  /**
1447
- * Create a SKU transaction
1465
+ * Purchase a SKU
1448
1466
  */
1449
- async createSkuTransaction({
1467
+ async purchaseSku({
1450
1468
  params,
1451
1469
  bearerToken
1452
1470
  }) {
1453
- return this.skuTransactions.createSkuTransaction(
1471
+ return this.skuPurchases.purchaseSku(
1454
1472
  params,
1455
1473
  this.resolveBearerToken(bearerToken)
1456
1474
  );
1457
1475
  }
1476
+ /**
1477
+ * Get a SKU purchase by id
1478
+ */
1479
+ async getSkuPurchase(id, bearerToken) {
1480
+ return this.skuPurchases.getSkuPurchase(
1481
+ id,
1482
+ this.resolveBearerToken(bearerToken)
1483
+ );
1484
+ }
1458
1485
  /// SKUS
1459
1486
  async getSkuList(params, bearerToken) {
1460
1487
  return this.skus.getSkuList(params, this.resolveBearerToken(bearerToken));
@@ -1469,6 +1496,13 @@ var ChipiSDK = class {
1469
1496
  async createUser(params, bearerToken) {
1470
1497
  return this.users.createUser(params, this.resolveBearerToken(bearerToken));
1471
1498
  }
1499
+ async getUsdAmount(currencyAmount, currency, bearerToken) {
1500
+ return this.exchanges.getUsdAmount(
1501
+ currencyAmount,
1502
+ currency,
1503
+ this.resolveBearerToken(bearerToken)
1504
+ );
1505
+ }
1472
1506
  };
1473
1507
 
1474
1508
  // src/chipi-server-sdk.ts
@@ -1494,6 +1528,6 @@ var ChipiBrowserSDK = class extends ChipiSDK {
1494
1528
  // bearerToken parameter is required for each authenticated request
1495
1529
  };
1496
1530
 
1497
- export { ChipiBrowserSDK, ChipiClient, ChipiSDK, ChipiServerSDK, ChipiSessions, ChipiSkuTransactions, ChipiSkus, ChipiTransactions, ChipiWallets, decryptPrivateKey, encryptPrivateKey };
1531
+ export { ChipiBrowserSDK, ChipiClient, ChipiSDK, ChipiServerSDK, ChipiSessions, ChipiSkuPurchases, ChipiSkus, ChipiTransactions, ChipiWallets, decryptPrivateKey, encryptPrivateKey };
1498
1532
  //# sourceMappingURL=index.mjs.map
1499
1533
  //# sourceMappingURL=index.mjs.map