@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/client-BaNfges3.d.mts +39 -0
- package/dist/client-BaNfges3.d.ts +39 -0
- package/dist/index.d.mts +19 -9
- package/dist/index.d.ts +19 -9
- package/dist/index.js +124 -190
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +125 -191
- package/dist/index.mjs.map +1 -1
- package/dist/skus.d.mts +2 -41
- package/dist/skus.d.ts +2 -41
- package/dist/skus.js +0 -75
- package/dist/skus.js.map +1 -1
- package/dist/skus.mjs +0 -75
- package/dist/skus.mjs.map +1 -1
- package/dist/transactions.d.mts +11 -9
- package/dist/transactions.d.ts +11 -9
- package/dist/transactions.js +36 -7
- package/dist/transactions.js.map +1 -1
- package/dist/transactions.mjs +37 -8
- package/dist/transactions.mjs.map +1 -1
- package/dist/wallets.d.mts +7 -15
- package/dist/wallets.d.ts +7 -15
- package/dist/wallets.js +32 -65
- package/dist/wallets.js.map +1 -1
- package/dist/wallets.mjs +33 -66
- package/dist/wallets.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/client-3HAMR0rB.d.mts +0 -24
- package/dist/client-3HAMR0rB.d.ts +0 -24
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { isValidApiKey, ChipiAuthError, validateErrorResponse, ChipiApiError, validateApiResponse, handleApiError,
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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,
|
|
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
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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 } =
|
|
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(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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 =
|
|
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
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
"
|
|
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(
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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(
|
|
428
|
-
|
|
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
|
|
431
|
-
wallet
|
|
432
|
-
bearerToken
|
|
427
|
+
encryptKey,
|
|
428
|
+
wallet,
|
|
429
|
+
bearerToken,
|
|
433
430
|
calls: [
|
|
434
431
|
{
|
|
435
|
-
contractAddress
|
|
432
|
+
contractAddress,
|
|
436
433
|
entrypoint: "transfer",
|
|
437
434
|
calldata: [
|
|
438
|
-
|
|
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
|
-
*
|
|
477
|
+
* Record a send transaction
|
|
486
478
|
*/
|
|
487
|
-
async
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
|
|
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
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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(
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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 {
|
|
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(
|
|
697
|
-
|
|
622
|
+
async createWallet({
|
|
623
|
+
params,
|
|
624
|
+
bearerToken
|
|
625
|
+
}) {
|
|
698
626
|
return this.wallets.createWallet({
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
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
|
};
|