@chipi-stack/backend 0.1.0 → 0.2.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,6 +1,7 @@
1
- import { isValidApiKey, ChipiAuthError, validateErrorResponse, ChipiApiError, validateApiResponse, handleApiError, ChipiTransactionError, API_ENDPOINTS, formatAmount, STARKNET_NETWORKS, CONTRACT_ADDRESSES } from '@chipi-stack/shared';
1
+ import { isValidApiKey, ChipiAuthError, validateErrorResponse, ChipiApiError, validateApiResponse, handleApiError, API_ENDPOINTS, ChipiTransactionError, formatAmount, STARKNET_NETWORKS, CONTRACT_ADDRESSES } from '@chipi-stack/shared';
2
2
  import { RpcProvider, stark, ec, CairoCustomEnum, CairoOption, CairoOptionVariant, CallData, hash, Account, num } from 'starknet';
3
3
  import CryptoJS2 from 'crypto-js';
4
+ import { STARKNET_CONTRACTS } from '@chipi-stack/types';
4
5
 
5
6
  // src/chipi-sdk.ts
6
7
  var ChipiClient = class {
@@ -34,7 +35,11 @@ var ChipiClient = class {
34
35
  }
35
36
  return headers;
36
37
  }
37
- async get(endpoint, params, bearerToken) {
38
+ async get({
39
+ endpoint,
40
+ params,
41
+ bearerToken
42
+ }) {
38
43
  try {
39
44
  const url = new URL(`${this.baseUrl}${endpoint}`);
40
45
  if (params) {
@@ -65,7 +70,11 @@ var ChipiClient = class {
65
70
  throw handleApiError(error);
66
71
  }
67
72
  }
68
- async post(endpoint, body, bearerToken) {
73
+ async post({
74
+ endpoint,
75
+ bearerToken,
76
+ body
77
+ }) {
69
78
  try {
70
79
  const response = await fetch(`${this.baseUrl}${endpoint}`, {
71
80
  method: "POST",
@@ -89,7 +98,11 @@ var ChipiClient = class {
89
98
  throw handleApiError(error);
90
99
  }
91
100
  }
92
- async put(endpoint, body, bearerToken) {
101
+ async put({
102
+ endpoint,
103
+ bearerToken,
104
+ body
105
+ }) {
93
106
  try {
94
107
  const response = await fetch(`${this.baseUrl}${endpoint}`, {
95
108
  method: "PUT",
@@ -113,7 +126,10 @@ var ChipiClient = class {
113
126
  throw handleApiError(error);
114
127
  }
115
128
  }
116
- async delete(endpoint, bearerToken) {
129
+ async delete({
130
+ endpoint,
131
+ bearerToken
132
+ }) {
117
133
  try {
118
134
  const response = await fetch(`${this.baseUrl}${endpoint}`, {
119
135
  method: "DELETE",
@@ -178,7 +194,7 @@ var ChipiWallets = class {
178
194
  // }
179
195
  async createWallet(params) {
180
196
  try {
181
- const { encryptKey, apiPublicKey, bearerToken, nodeUrl, backendUrl } = params;
197
+ const { encryptKey, nodeUrl, externalUserId, bearerToken } = params;
182
198
  const provider = new RpcProvider({ nodeUrl });
183
199
  const privateKeyAX = stark.randomAddress();
184
200
  const starkKeyPubAX = ec.starkCurve.getStarkKey(privateKeyAX);
@@ -198,19 +214,14 @@ var ChipiWallets = class {
198
214
  0
199
215
  );
200
216
  const account = new Account(provider, publicKey, privateKeyAX);
201
- console.log("apiPublicKey", apiPublicKey);
202
- const typeDataResponse = await fetch(`${backendUrl}/chipi-wallets/prepare-creation`, {
203
- method: "POST",
204
- headers: {
205
- "Content-Type": "application/json",
206
- "Authorization": `Bearer ${bearerToken}`,
207
- "x-api-key": apiPublicKey
208
- },
209
- body: JSON.stringify({
217
+ const typeDataResponse = await this.client.post({
218
+ endpoint: `${API_ENDPOINTS.CHIPI_WALLETS}/prepare-creation`,
219
+ bearerToken,
220
+ body: {
210
221
  publicKey
211
- })
222
+ }
212
223
  });
213
- const { typeData, accountClassHash: accountClassHashResponse } = await typeDataResponse.json();
224
+ const { typeData, accountClassHash: accountClassHashResponse } = typeDataResponse.data;
214
225
  const userSignature = await account.signMessage(typeData);
215
226
  const deploymentData = {
216
227
  class_hash: accountClassHashResponse,
@@ -218,16 +229,15 @@ var ChipiWallets = class {
218
229
  unique: `${num.toHex(0)}`,
219
230
  calldata: AXConstructorCallData.map((value) => num.toHex(value))
220
231
  };
221
- const encryptedPrivateKey = this.encryptPrivateKey(privateKeyAX, encryptKey);
222
- const executeTransactionResponse = await fetch(`${backendUrl}/chipi-wallets`, {
223
- method: "POST",
224
- headers: {
225
- "Content-Type": "application/json",
226
- "Authorization": `Bearer ${bearerToken}`,
227
- "x-api-key": apiPublicKey
228
- },
229
- body: JSON.stringify({
230
- apiPublicKey,
232
+ const encryptedPrivateKey = this.encryptPrivateKey(
233
+ privateKeyAX,
234
+ encryptKey
235
+ );
236
+ const executeTransactionResponse = await this.client.post({
237
+ endpoint: `${API_ENDPOINTS.CHIPI_WALLETS}`,
238
+ bearerToken,
239
+ body: {
240
+ externalUserId,
231
241
  publicKey,
232
242
  userSignature: {
233
243
  r: userSignature.r.toString(),
@@ -241,9 +251,9 @@ var ChipiWallets = class {
241
251
  salt: `${deploymentData.salt}`,
242
252
  calldata: deploymentData.calldata.map((data) => `${data}`)
243
253
  }
244
- })
254
+ }
245
255
  });
246
- const executeTransaction = await executeTransactionResponse.json();
256
+ const executeTransaction = executeTransactionResponse.data;
247
257
  if (executeTransaction.success) {
248
258
  return {
249
259
  success: true,
@@ -252,21 +262,16 @@ var ChipiWallets = class {
252
262
  wallet: executeTransaction.wallet
253
263
  };
254
264
  } else {
255
- return {
256
- success: false,
257
- txHash: "",
258
- walletPublicKey: "",
259
- wallet: {
260
- publicKey: "",
261
- encryptedPrivateKey: ""
262
- }
263
- };
265
+ throw new ChipiTransactionError(
266
+ `Failed to create wallet: ${executeTransaction.message}`,
267
+ "WALLET_CREATION_FAILED"
268
+ );
264
269
  }
265
270
  } catch (error) {
266
271
  console.error("Error detallado:", error);
267
272
  if (error instanceof Error && error.message.includes("SSL")) {
268
273
  throw new Error(
269
- "Error de conexi\xF3n SSL. Intenta usando NODE_TLS_REJECT_UNAUTHORIZED=0 o verifica la URL del RPC"
274
+ "SSL connection error. Try using NODE_TLS_REJECT_UNAUTHORIZED=0 or verify the RPC URL"
270
275
  );
271
276
  }
272
277
  throw new ChipiTransactionError(
@@ -278,37 +283,15 @@ var ChipiWallets = class {
278
283
  /**
279
284
  * Create a custodial merchant wallet
280
285
  */
281
- async createCustodialWallet(params, orgId) {
282
- const response = await this.client.post(
283
- `${API_ENDPOINTS.CHIPI_WALLETS}/merchant`,
284
- {
285
- ...params,
286
- orgId
287
- }
288
- );
289
- return response.data;
290
- }
291
- /**
292
- * Get merchant wallet by API key and chain
293
- */
294
- async getMerchantWallet(params) {
295
- const response = await this.client.get(
296
- `${API_ENDPOINTS.CHIPI_WALLETS}/merchant`,
297
- {
298
- apiKeyId: params.apiKeyId,
299
- chain: params.chain
300
- }
301
- );
302
- return response.data;
303
- }
304
- /**
305
- * Get wallets for an organization
306
- */
307
- async getWallets(query) {
308
- const response = await this.client.get(
309
- API_ENDPOINTS.CHIPI_WALLETS,
310
- query
311
- );
286
+ async createCustodialWallet({
287
+ params,
288
+ bearerToken
289
+ }) {
290
+ const response = await this.client.post({
291
+ endpoint: `${API_ENDPOINTS.CHIPI_WALLETS}/custodial`,
292
+ bearerToken,
293
+ body: params
294
+ });
312
295
  return response.data;
313
296
  }
314
297
  };
@@ -424,18 +407,32 @@ var ChipiTransactions = class {
424
407
  /**
425
408
  * Transfer tokens
426
409
  */
427
- async transfer(params) {
428
- const formattedAmount = formatAmount(params.amount, params.decimals);
410
+ async transfer({
411
+ params,
412
+ bearerToken
413
+ }) {
414
+ const { encryptKey, wallet, token, otherToken, recipient, amount } = params;
415
+ const contract = STARKNET_CONTRACTS[token];
416
+ let contractAddress = contract.contractAddress;
417
+ let decimals = contract.decimals;
418
+ if (token === "OTHER") {
419
+ if (!otherToken) {
420
+ throw new Error("Other token is required when token is OTHER");
421
+ }
422
+ contractAddress = otherToken.contractAddress;
423
+ decimals = otherToken.decimals;
424
+ }
425
+ const formattedAmount = formatAmount(amount, decimals);
429
426
  return this.executeTransaction({
430
- encryptKey: params.encryptKey,
431
- wallet: params.wallet,
432
- bearerToken: params.bearerToken,
427
+ encryptKey,
428
+ wallet,
429
+ bearerToken,
433
430
  calls: [
434
431
  {
435
- contractAddress: params.contractAddress,
432
+ contractAddress,
436
433
  entrypoint: "transfer",
437
434
  calldata: [
438
- params.recipient,
435
+ recipient,
439
436
  formattedAmount,
440
437
  "0x0"
441
438
  ]
@@ -476,83 +473,26 @@ var ChipiTransactions = class {
476
473
  calls: params.calls
477
474
  });
478
475
  }
479
- };
480
- var ChipiSkus = class {
481
- constructor(client) {
482
- this.client = client;
483
- }
484
476
  /**
485
- * Find available SKUs
477
+ * Record a send transaction
486
478
  */
487
- async findSkus(params = {}) {
488
- const response = await this.client.get(
489
- API_ENDPOINTS.SKUS,
490
- {
491
- categories: params.categories
492
- }
493
- );
494
- return response.data;
495
- }
496
- /**
497
- * Get SKU by ID
498
- */
499
- async getSku(skuId) {
500
- const response = await this.client.get(
501
- `${API_ENDPOINTS.SKUS}/${skuId}`
502
- );
503
- return response.data;
504
- }
505
- /**
506
- * Create a SKU transaction
507
- */
508
- async createSkuTransaction(params) {
509
- const response = await this.client.post(
510
- API_ENDPOINTS.SKU_TRANSACTIONS,
511
- params
512
- );
513
- return response.data;
514
- }
515
- /**
516
- * Get SKU transaction by ID
517
- */
518
- async getSkuTransaction(transactionId) {
519
- const response = await this.client.get(
520
- `${API_ENDPOINTS.SKU_TRANSACTIONS}/${transactionId}`
521
- );
522
- return response.data;
523
- }
524
- /**
525
- * Get SKU transactions for a wallet
526
- */
527
- async getSkuTransactionsByWallet(walletAddress, params = {}) {
528
- const response = await this.client.get(
529
- API_ENDPOINTS.SKU_TRANSACTIONS,
530
- {
531
- walletAddress,
532
- ...params
533
- }
534
- );
479
+ async recordSendTransaction({
480
+ params,
481
+ bearerToken
482
+ }) {
483
+ const response = await this.client.post({
484
+ endpoint: `${API_ENDPOINTS.TRANSACTIONS}/record-send`,
485
+ bearerToken,
486
+ body: params
487
+ });
535
488
  return response.data;
536
489
  }
537
- /**
538
- * Purchase a SKU
539
- * This is a convenience method that combines finding a SKU and creating a transaction
540
- */
541
- async purchaseSku(params) {
542
- const sku = await this.getSku(params.skuId);
543
- const transaction = await this.createSkuTransaction({
544
- walletAddress: params.walletAddress,
545
- skuId: params.skuId,
546
- chain: params.chain,
547
- chainToken: params.chainToken,
548
- mxnAmount: params.mxnAmount,
549
- reference: params.reference,
550
- transactionHash: params.transactionHash
551
- });
552
- return {
553
- sku,
554
- transaction
555
- };
490
+ };
491
+
492
+ // src/skus.ts
493
+ var ChipiSkus = class {
494
+ constructor(client) {
495
+ this.client = client;
556
496
  }
557
497
  };
558
498
 
@@ -571,6 +511,7 @@ var ChipiSDK = class {
571
511
  this.withdrawVesuUsdc = this.withdrawVesuUsdc.bind(this);
572
512
  this.callAnyContract = this.callAnyContract.bind(this);
573
513
  this.createWallet = this.createWallet.bind(this);
514
+ this.recordSendTransaction = this.recordSendTransaction.bind(this);
574
515
  }
575
516
  /**
576
517
  * Execute a gasless transaction
@@ -581,31 +522,28 @@ var ChipiSDK = class {
581
522
  /**
582
523
  * Transfer tokens
583
524
  */
584
- async transfer(params) {
585
- const { encryptKey, wallet, contractAddress, recipient, amount, decimals, bearerToken } = params;
586
- const formattedAmount = formatAmount(amount, decimals);
587
- return this.executeTransaction({
588
- encryptKey,
589
- wallet,
590
- bearerToken,
591
- calls: [
592
- {
593
- contractAddress,
594
- entrypoint: "transfer",
595
- calldata: [
596
- recipient,
597
- formattedAmount,
598
- "0x0"
599
- ]
600
- }
601
- ]
525
+ async transfer({
526
+ params,
527
+ bearerToken
528
+ }) {
529
+ return this.transactions.transfer({
530
+ params,
531
+ bearerToken
602
532
  });
603
533
  }
604
534
  /**
605
535
  * Approve token spending
606
536
  */
607
537
  async approve(params) {
608
- const { encryptKey, wallet, contractAddress, spender, amount, decimals, bearerToken } = params;
538
+ const {
539
+ encryptKey,
540
+ wallet,
541
+ contractAddress,
542
+ spender,
543
+ amount,
544
+ decimals,
545
+ bearerToken
546
+ } = params;
609
547
  return this.executeTransaction({
610
548
  encryptKey,
611
549
  wallet,
@@ -614,11 +552,7 @@ var ChipiSDK = class {
614
552
  {
615
553
  contractAddress,
616
554
  entrypoint: "approve",
617
- calldata: [
618
- spender,
619
- formatAmount(amount, decimals),
620
- "0x0"
621
- ]
555
+ calldata: [spender, formatAmount(amount, decimals), "0x0"]
622
556
  }
623
557
  ]
624
558
  });
@@ -646,11 +580,7 @@ var ChipiSDK = class {
646
580
  {
647
581
  contractAddress: CONTRACT_ADDRESSES.VESU_USDC_MAINNET,
648
582
  entrypoint: "deposit",
649
- calldata: [
650
- formattedAmount,
651
- "0x0",
652
- receiverWallet
653
- ]
583
+ calldata: [formattedAmount, "0x0", receiverWallet]
654
584
  }
655
585
  ]
656
586
  });
@@ -669,11 +599,7 @@ var ChipiSDK = class {
669
599
  {
670
600
  contractAddress: CONTRACT_ADDRESSES.VESU_USDC_MAINNET,
671
601
  entrypoint: "withdraw",
672
- calldata: [
673
- formattedAmount,
674
- recipient,
675
- "0x0"
676
- ]
602
+ calldata: [formattedAmount, recipient, "0x0"]
677
603
  }
678
604
  ]
679
605
  });
@@ -693,14 +619,22 @@ var ChipiSDK = class {
693
619
  /**
694
620
  * Create a new wallet
695
621
  */
696
- async createWallet(params) {
697
- const { encryptKey, bearerToken } = params;
622
+ async createWallet({
623
+ params,
624
+ bearerToken
625
+ }) {
698
626
  return this.wallets.createWallet({
699
- encryptKey,
700
- apiPublicKey: this.client.getApiPublicKey(),
701
- bearerToken,
702
- nodeUrl: this.nodeUrl,
703
- backendUrl: this.client.baseUrl
627
+ ...params,
628
+ bearerToken
629
+ });
630
+ }
631
+ async recordSendTransaction({
632
+ params,
633
+ bearerToken
634
+ }) {
635
+ return this.transactions.recordSendTransaction({
636
+ params,
637
+ bearerToken
704
638
  });
705
639
  }
706
640
  };