@chipi-stack/backend 0.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/client-3HAMR0rB.d.mts +24 -0
- package/dist/client-3HAMR0rB.d.ts +24 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +722 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +710 -0
- package/dist/index.mjs.map +1 -0
- package/dist/skus.d.mts +52 -0
- package/dist/skus.d.ts +52 -0
- package/dist/skus.js +87 -0
- package/dist/skus.js.map +1 -0
- package/dist/skus.mjs +85 -0
- package/dist/skus.mjs.map +1 -0
- package/dist/transactions.d.mts +50 -0
- package/dist/transactions.d.ts +50 -0
- package/dist/transactions.js +174 -0
- package/dist/transactions.js.map +1 -0
- package/dist/transactions.mjs +168 -0
- package/dist/transactions.mjs.map +1 -0
- package/dist/wallets.d.mts +38 -0
- package/dist/wallets.d.ts +38 -0
- package/dist/wallets.js +190 -0
- package/dist/wallets.js.map +1 -0
- package/dist/wallets.mjs +184 -0
- package/dist/wallets.mjs.map +1 -0
- package/package.json +99 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,722 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var shared = require('@chipi-stack/shared');
|
|
4
|
+
var starknet = require('starknet');
|
|
5
|
+
var CryptoJS2 = require('crypto-js');
|
|
6
|
+
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var CryptoJS2__default = /*#__PURE__*/_interopDefault(CryptoJS2);
|
|
10
|
+
|
|
11
|
+
// src/chipi-sdk.ts
|
|
12
|
+
var ChipiClient = class {
|
|
13
|
+
constructor(config) {
|
|
14
|
+
if (!shared.isValidApiKey(config.apiPublicKey)) {
|
|
15
|
+
throw new shared.ChipiAuthError("Invalid API key format");
|
|
16
|
+
}
|
|
17
|
+
this.apiPublicKey = config.apiPublicKey;
|
|
18
|
+
this.environment = config.environment || "production";
|
|
19
|
+
this.baseUrl = this.getBaseUrl();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the API public key (for internal SDK use)
|
|
23
|
+
*/
|
|
24
|
+
getApiPublicKey() {
|
|
25
|
+
return this.apiPublicKey;
|
|
26
|
+
}
|
|
27
|
+
getBaseUrl() {
|
|
28
|
+
if (this.environment === "development") {
|
|
29
|
+
return "http://localhost:3000/v1";
|
|
30
|
+
}
|
|
31
|
+
return "https://api.chipipay.com/v1";
|
|
32
|
+
}
|
|
33
|
+
getHeaders(bearerToken) {
|
|
34
|
+
const headers = {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
"x-api-key": this.apiPublicKey
|
|
37
|
+
};
|
|
38
|
+
if (bearerToken) {
|
|
39
|
+
headers["Authorization"] = `Bearer ${bearerToken}`;
|
|
40
|
+
}
|
|
41
|
+
return headers;
|
|
42
|
+
}
|
|
43
|
+
async get(endpoint, params, bearerToken) {
|
|
44
|
+
try {
|
|
45
|
+
const url = new URL(`${this.baseUrl}${endpoint}`);
|
|
46
|
+
if (params) {
|
|
47
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
48
|
+
if (value !== void 0 && value !== null) {
|
|
49
|
+
url.searchParams.append(key, String(value));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const response = await fetch(url.toString(), {
|
|
54
|
+
method: "GET",
|
|
55
|
+
headers: this.getHeaders(bearerToken)
|
|
56
|
+
});
|
|
57
|
+
const rawData = await response.json();
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const errorData = shared.validateErrorResponse(rawData);
|
|
60
|
+
throw new shared.ChipiApiError(
|
|
61
|
+
errorData.message,
|
|
62
|
+
errorData.code || `HTTP_${response.status}`,
|
|
63
|
+
response.status
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
success: true,
|
|
68
|
+
data: shared.validateApiResponse(rawData)
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw shared.handleApiError(error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async post(endpoint, body, bearerToken) {
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: this.getHeaders(bearerToken),
|
|
79
|
+
body: body ? JSON.stringify(body) : void 0
|
|
80
|
+
});
|
|
81
|
+
const rawData = await response.json();
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
const errorData = shared.validateErrorResponse(rawData);
|
|
84
|
+
throw new shared.ChipiApiError(
|
|
85
|
+
errorData.message,
|
|
86
|
+
errorData.code || `HTTP_${response.status}`,
|
|
87
|
+
response.status
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
success: true,
|
|
92
|
+
data: shared.validateApiResponse(rawData)
|
|
93
|
+
};
|
|
94
|
+
} catch (error) {
|
|
95
|
+
throw shared.handleApiError(error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async put(endpoint, body, bearerToken) {
|
|
99
|
+
try {
|
|
100
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
101
|
+
method: "PUT",
|
|
102
|
+
headers: this.getHeaders(bearerToken),
|
|
103
|
+
body: body ? JSON.stringify(body) : void 0
|
|
104
|
+
});
|
|
105
|
+
const rawData = await response.json();
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
const errorData = shared.validateErrorResponse(rawData);
|
|
108
|
+
throw new shared.ChipiApiError(
|
|
109
|
+
errorData.message,
|
|
110
|
+
errorData.code || `HTTP_${response.status}`,
|
|
111
|
+
response.status
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
success: true,
|
|
116
|
+
data: shared.validateApiResponse(rawData)
|
|
117
|
+
};
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw shared.handleApiError(error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async delete(endpoint, bearerToken) {
|
|
123
|
+
try {
|
|
124
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
125
|
+
method: "DELETE",
|
|
126
|
+
headers: this.getHeaders(bearerToken)
|
|
127
|
+
});
|
|
128
|
+
const rawData = await response.json();
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
const errorData = shared.validateErrorResponse(rawData);
|
|
131
|
+
throw new shared.ChipiApiError(
|
|
132
|
+
errorData.message,
|
|
133
|
+
errorData.code || `HTTP_${response.status}`,
|
|
134
|
+
response.status
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
success: true,
|
|
139
|
+
data: shared.validateApiResponse(rawData)
|
|
140
|
+
};
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw shared.handleApiError(error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var ChipiWallets = class {
|
|
147
|
+
constructor(client) {
|
|
148
|
+
this.client = client;
|
|
149
|
+
this.encryptPrivateKey = (privateKey, password) => {
|
|
150
|
+
if (!privateKey || !password) {
|
|
151
|
+
throw new Error("Private key and password are required");
|
|
152
|
+
}
|
|
153
|
+
return CryptoJS2__default.default.AES.encrypt(privateKey, password).toString();
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Prepare wallet creation data
|
|
158
|
+
*/
|
|
159
|
+
// async prepareWalletCreation(
|
|
160
|
+
// params: Omit<PrepareWalletCreationParams, 'apiPublicKey'>
|
|
161
|
+
// ): Promise<PrepareWalletCreationResponse> {
|
|
162
|
+
// const response = await this.client.post<PrepareWalletCreationResponse>(
|
|
163
|
+
// `${API_ENDPOINTS.CHIPI_WALLETS}/prepare-creation`,
|
|
164
|
+
// params
|
|
165
|
+
// );
|
|
166
|
+
// return response.data!;
|
|
167
|
+
// }
|
|
168
|
+
/**
|
|
169
|
+
* Create a new wallet
|
|
170
|
+
*/
|
|
171
|
+
// async createWallet(
|
|
172
|
+
// params: Omit<CreateWalletParams, 'apiPublicKey' | 'nodeUrl'>,
|
|
173
|
+
// nodeUrl?: string
|
|
174
|
+
// ): Promise<CreateWalletResponse> {
|
|
175
|
+
// const response = await this.client.post<CreateWalletResponse>(
|
|
176
|
+
// API_ENDPOINTS.CHIPI_WALLETS,
|
|
177
|
+
// {
|
|
178
|
+
// ...params,
|
|
179
|
+
// nodeUrl,
|
|
180
|
+
// },
|
|
181
|
+
// params.bearerToken
|
|
182
|
+
// );
|
|
183
|
+
// return response.data!;
|
|
184
|
+
// }
|
|
185
|
+
async createWallet(params) {
|
|
186
|
+
try {
|
|
187
|
+
const { encryptKey, apiPublicKey, bearerToken, nodeUrl, backendUrl } = params;
|
|
188
|
+
const provider = new starknet.RpcProvider({ nodeUrl });
|
|
189
|
+
const privateKeyAX = starknet.stark.randomAddress();
|
|
190
|
+
const starkKeyPubAX = starknet.ec.starkCurve.getStarkKey(privateKeyAX);
|
|
191
|
+
const accountClassHash = "0x036078334509b514626504edc9fb252328d1a240e4e948bef8d0c08dff45927f";
|
|
192
|
+
const axSigner = new starknet.CairoCustomEnum({
|
|
193
|
+
Starknet: { pubkey: starkKeyPubAX }
|
|
194
|
+
});
|
|
195
|
+
const axGuardian = new starknet.CairoOption(starknet.CairoOptionVariant.None);
|
|
196
|
+
const AXConstructorCallData = starknet.CallData.compile({
|
|
197
|
+
owner: axSigner,
|
|
198
|
+
guardian: axGuardian
|
|
199
|
+
});
|
|
200
|
+
const publicKey = starknet.hash.calculateContractAddressFromHash(
|
|
201
|
+
starkKeyPubAX,
|
|
202
|
+
accountClassHash,
|
|
203
|
+
AXConstructorCallData,
|
|
204
|
+
0
|
|
205
|
+
);
|
|
206
|
+
const account = new starknet.Account(provider, publicKey, privateKeyAX);
|
|
207
|
+
console.log("apiPublicKey", apiPublicKey);
|
|
208
|
+
const typeDataResponse = await fetch(`${backendUrl}/chipi-wallets/prepare-creation`, {
|
|
209
|
+
method: "POST",
|
|
210
|
+
headers: {
|
|
211
|
+
"Content-Type": "application/json",
|
|
212
|
+
"Authorization": `Bearer ${bearerToken}`,
|
|
213
|
+
"x-api-key": apiPublicKey
|
|
214
|
+
},
|
|
215
|
+
body: JSON.stringify({
|
|
216
|
+
publicKey
|
|
217
|
+
})
|
|
218
|
+
});
|
|
219
|
+
const { typeData, accountClassHash: accountClassHashResponse } = await typeDataResponse.json();
|
|
220
|
+
const userSignature = await account.signMessage(typeData);
|
|
221
|
+
const deploymentData = {
|
|
222
|
+
class_hash: accountClassHashResponse,
|
|
223
|
+
salt: starkKeyPubAX,
|
|
224
|
+
unique: `${starknet.num.toHex(0)}`,
|
|
225
|
+
calldata: AXConstructorCallData.map((value) => starknet.num.toHex(value))
|
|
226
|
+
};
|
|
227
|
+
const encryptedPrivateKey = this.encryptPrivateKey(privateKeyAX, encryptKey);
|
|
228
|
+
const executeTransactionResponse = await fetch(`${backendUrl}/chipi-wallets`, {
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers: {
|
|
231
|
+
"Content-Type": "application/json",
|
|
232
|
+
"Authorization": `Bearer ${bearerToken}`,
|
|
233
|
+
"x-api-key": apiPublicKey
|
|
234
|
+
},
|
|
235
|
+
body: JSON.stringify({
|
|
236
|
+
apiPublicKey,
|
|
237
|
+
publicKey,
|
|
238
|
+
userSignature: {
|
|
239
|
+
r: userSignature.r.toString(),
|
|
240
|
+
s: userSignature.s.toString(),
|
|
241
|
+
recovery: userSignature.recovery
|
|
242
|
+
},
|
|
243
|
+
typeData,
|
|
244
|
+
encryptedPrivateKey,
|
|
245
|
+
deploymentData: {
|
|
246
|
+
...deploymentData,
|
|
247
|
+
salt: `${deploymentData.salt}`,
|
|
248
|
+
calldata: deploymentData.calldata.map((data) => `${data}`)
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
});
|
|
252
|
+
const executeTransaction = await executeTransactionResponse.json();
|
|
253
|
+
if (executeTransaction.success) {
|
|
254
|
+
return {
|
|
255
|
+
success: true,
|
|
256
|
+
txHash: executeTransaction.txHash,
|
|
257
|
+
walletPublicKey: executeTransaction.walletPublicKey,
|
|
258
|
+
wallet: executeTransaction.wallet
|
|
259
|
+
};
|
|
260
|
+
} else {
|
|
261
|
+
return {
|
|
262
|
+
success: false,
|
|
263
|
+
txHash: "",
|
|
264
|
+
walletPublicKey: "",
|
|
265
|
+
wallet: {
|
|
266
|
+
publicKey: "",
|
|
267
|
+
encryptedPrivateKey: ""
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
} catch (error) {
|
|
272
|
+
console.error("Error detallado:", error);
|
|
273
|
+
if (error instanceof Error && error.message.includes("SSL")) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
"Error de conexi\xF3n SSL. Intenta usando NODE_TLS_REJECT_UNAUTHORIZED=0 o verifica la URL del RPC"
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
throw new shared.ChipiTransactionError(
|
|
279
|
+
`Failed to create wallet: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
280
|
+
"WALLET_CREATION_FAILED"
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Create a custodial merchant wallet
|
|
286
|
+
*/
|
|
287
|
+
async createCustodialWallet(params, orgId) {
|
|
288
|
+
const response = await this.client.post(
|
|
289
|
+
`${shared.API_ENDPOINTS.CHIPI_WALLETS}/merchant`,
|
|
290
|
+
{
|
|
291
|
+
...params,
|
|
292
|
+
orgId
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
return response.data;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Get merchant wallet by API key and chain
|
|
299
|
+
*/
|
|
300
|
+
async getMerchantWallet(params) {
|
|
301
|
+
const response = await this.client.get(
|
|
302
|
+
`${shared.API_ENDPOINTS.CHIPI_WALLETS}/merchant`,
|
|
303
|
+
{
|
|
304
|
+
apiKeyId: params.apiKeyId,
|
|
305
|
+
chain: params.chain
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
return response.data;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Get wallets for an organization
|
|
312
|
+
*/
|
|
313
|
+
async getWallets(query) {
|
|
314
|
+
const response = await this.client.get(
|
|
315
|
+
shared.API_ENDPOINTS.CHIPI_WALLETS,
|
|
316
|
+
query
|
|
317
|
+
);
|
|
318
|
+
return response.data;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var encryptPrivateKey = (privateKey, password) => {
|
|
322
|
+
if (!privateKey || !password) {
|
|
323
|
+
throw new Error("Private key and password are required");
|
|
324
|
+
}
|
|
325
|
+
return CryptoJS2__default.default.AES.encrypt(privateKey, password).toString();
|
|
326
|
+
};
|
|
327
|
+
var decryptPrivateKey = (encryptedPrivateKey, password) => {
|
|
328
|
+
if (!encryptedPrivateKey || !password) {
|
|
329
|
+
console.error("Encrypted private key and password are required");
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
const bytes = CryptoJS2__default.default.AES.decrypt(encryptedPrivateKey, password);
|
|
334
|
+
const decrypted = bytes.toString(CryptoJS2__default.default.enc.Utf8);
|
|
335
|
+
if (!decrypted) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
return decrypted;
|
|
339
|
+
} catch (error) {
|
|
340
|
+
console.error("Decryption failed:", error);
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// src/gasless.ts
|
|
346
|
+
var executePaymasterTransaction = async (params) => {
|
|
347
|
+
try {
|
|
348
|
+
const { encryptKey, wallet, calls, apiPublicKey, bearerToken, backendUrl } = params;
|
|
349
|
+
const privateKeyDecrypted = decryptPrivateKey(
|
|
350
|
+
wallet.encryptedPrivateKey,
|
|
351
|
+
encryptKey
|
|
352
|
+
);
|
|
353
|
+
if (!privateKeyDecrypted) {
|
|
354
|
+
throw new Error("Failed to decrypt private key");
|
|
355
|
+
}
|
|
356
|
+
const provider = new starknet.RpcProvider({
|
|
357
|
+
nodeUrl: "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.7"
|
|
358
|
+
});
|
|
359
|
+
const account = new starknet.Account(
|
|
360
|
+
provider,
|
|
361
|
+
wallet.publicKey,
|
|
362
|
+
privateKeyDecrypted
|
|
363
|
+
);
|
|
364
|
+
const typeDataResponse = await fetch(`${backendUrl}/transactions/prepare-typed-data`, {
|
|
365
|
+
method: "POST",
|
|
366
|
+
headers: {
|
|
367
|
+
"Content-Type": "application/json",
|
|
368
|
+
"Authorization": `Bearer ${bearerToken}`,
|
|
369
|
+
"X-API-Key": apiPublicKey
|
|
370
|
+
},
|
|
371
|
+
body: JSON.stringify({
|
|
372
|
+
publicKey: wallet.publicKey,
|
|
373
|
+
calls,
|
|
374
|
+
accountClassHash: "0x036078334509b514626504edc9fb252328d1a240e4e948bef8d0c08dff45927f"
|
|
375
|
+
})
|
|
376
|
+
});
|
|
377
|
+
if (!typeDataResponse.ok) {
|
|
378
|
+
const errorText = await typeDataResponse.text();
|
|
379
|
+
throw new Error(`Error en la API: ${errorText}`);
|
|
380
|
+
}
|
|
381
|
+
const typeData = await typeDataResponse.json();
|
|
382
|
+
const userSignature = await account.signMessage(typeData);
|
|
383
|
+
const executeTransaction = await fetch(`${backendUrl}/transactions/execute-sponsored-transaction`, {
|
|
384
|
+
method: "POST",
|
|
385
|
+
headers: {
|
|
386
|
+
"Content-Type": "application/json",
|
|
387
|
+
"Authorization": `Bearer ${bearerToken}`,
|
|
388
|
+
"X-API-Key": apiPublicKey
|
|
389
|
+
},
|
|
390
|
+
body: JSON.stringify({
|
|
391
|
+
publicKey: wallet.publicKey,
|
|
392
|
+
typeData,
|
|
393
|
+
userSignature: {
|
|
394
|
+
r: userSignature.r.toString(),
|
|
395
|
+
s: userSignature.s.toString(),
|
|
396
|
+
recovery: userSignature.recovery
|
|
397
|
+
}
|
|
398
|
+
})
|
|
399
|
+
});
|
|
400
|
+
if (!executeTransaction.ok) {
|
|
401
|
+
const errorText = await executeTransaction.text();
|
|
402
|
+
throw new Error(`Error en la API de ejecuci\xF3n: ${errorText}`);
|
|
403
|
+
}
|
|
404
|
+
const result = await executeTransaction.json();
|
|
405
|
+
if (!result.transactionHash) {
|
|
406
|
+
throw new Error("La respuesta no contiene el hash de la transacci\xF3n");
|
|
407
|
+
}
|
|
408
|
+
return result.transactionHash;
|
|
409
|
+
} catch (error) {
|
|
410
|
+
console.error("Error sending transaction with paymaster", error);
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
// src/transactions.ts
|
|
416
|
+
var ChipiTransactions = class {
|
|
417
|
+
constructor(client) {
|
|
418
|
+
this.client = client;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Execute a gasless transaction using paymaster
|
|
422
|
+
*/
|
|
423
|
+
async executeTransaction(params) {
|
|
424
|
+
return executePaymasterTransaction({
|
|
425
|
+
...params,
|
|
426
|
+
apiPublicKey: this.client.getApiPublicKey(),
|
|
427
|
+
backendUrl: this.client.baseUrl
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Transfer tokens
|
|
432
|
+
*/
|
|
433
|
+
async transfer(params) {
|
|
434
|
+
const formattedAmount = shared.formatAmount(params.amount, params.decimals);
|
|
435
|
+
return this.executeTransaction({
|
|
436
|
+
encryptKey: params.encryptKey,
|
|
437
|
+
wallet: params.wallet,
|
|
438
|
+
bearerToken: params.bearerToken,
|
|
439
|
+
calls: [
|
|
440
|
+
{
|
|
441
|
+
contractAddress: params.contractAddress,
|
|
442
|
+
entrypoint: "transfer",
|
|
443
|
+
calldata: [
|
|
444
|
+
params.recipient,
|
|
445
|
+
formattedAmount,
|
|
446
|
+
"0x0"
|
|
447
|
+
]
|
|
448
|
+
}
|
|
449
|
+
]
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Approve token spending
|
|
454
|
+
*/
|
|
455
|
+
async approve(params) {
|
|
456
|
+
const formattedAmount = shared.formatAmount(params.amount, params.decimals);
|
|
457
|
+
return this.executeTransaction({
|
|
458
|
+
encryptKey: params.encryptKey,
|
|
459
|
+
wallet: params.wallet,
|
|
460
|
+
bearerToken: params.bearerToken,
|
|
461
|
+
calls: [
|
|
462
|
+
{
|
|
463
|
+
contractAddress: params.contractAddress,
|
|
464
|
+
entrypoint: "approve",
|
|
465
|
+
calldata: [
|
|
466
|
+
params.spender,
|
|
467
|
+
formattedAmount,
|
|
468
|
+
"0x0"
|
|
469
|
+
]
|
|
470
|
+
}
|
|
471
|
+
]
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Call any contract method
|
|
476
|
+
*/
|
|
477
|
+
async callAnyContract(params) {
|
|
478
|
+
return this.executeTransaction({
|
|
479
|
+
encryptKey: params.encryptKey,
|
|
480
|
+
wallet: params.wallet,
|
|
481
|
+
bearerToken: params.bearerToken,
|
|
482
|
+
calls: params.calls
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
var ChipiSkus = class {
|
|
487
|
+
constructor(client) {
|
|
488
|
+
this.client = client;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Find available SKUs
|
|
492
|
+
*/
|
|
493
|
+
async findSkus(params = {}) {
|
|
494
|
+
const response = await this.client.get(
|
|
495
|
+
shared.API_ENDPOINTS.SKUS,
|
|
496
|
+
{
|
|
497
|
+
categories: params.categories
|
|
498
|
+
}
|
|
499
|
+
);
|
|
500
|
+
return response.data;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Get SKU by ID
|
|
504
|
+
*/
|
|
505
|
+
async getSku(skuId) {
|
|
506
|
+
const response = await this.client.get(
|
|
507
|
+
`${shared.API_ENDPOINTS.SKUS}/${skuId}`
|
|
508
|
+
);
|
|
509
|
+
return response.data;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Create a SKU transaction
|
|
513
|
+
*/
|
|
514
|
+
async createSkuTransaction(params) {
|
|
515
|
+
const response = await this.client.post(
|
|
516
|
+
shared.API_ENDPOINTS.SKU_TRANSACTIONS,
|
|
517
|
+
params
|
|
518
|
+
);
|
|
519
|
+
return response.data;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Get SKU transaction by ID
|
|
523
|
+
*/
|
|
524
|
+
async getSkuTransaction(transactionId) {
|
|
525
|
+
const response = await this.client.get(
|
|
526
|
+
`${shared.API_ENDPOINTS.SKU_TRANSACTIONS}/${transactionId}`
|
|
527
|
+
);
|
|
528
|
+
return response.data;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Get SKU transactions for a wallet
|
|
532
|
+
*/
|
|
533
|
+
async getSkuTransactionsByWallet(walletAddress, params = {}) {
|
|
534
|
+
const response = await this.client.get(
|
|
535
|
+
shared.API_ENDPOINTS.SKU_TRANSACTIONS,
|
|
536
|
+
{
|
|
537
|
+
walletAddress,
|
|
538
|
+
...params
|
|
539
|
+
}
|
|
540
|
+
);
|
|
541
|
+
return response.data;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Purchase a SKU
|
|
545
|
+
* This is a convenience method that combines finding a SKU and creating a transaction
|
|
546
|
+
*/
|
|
547
|
+
async purchaseSku(params) {
|
|
548
|
+
const sku = await this.getSku(params.skuId);
|
|
549
|
+
const transaction = await this.createSkuTransaction({
|
|
550
|
+
walletAddress: params.walletAddress,
|
|
551
|
+
skuId: params.skuId,
|
|
552
|
+
chain: params.chain,
|
|
553
|
+
chainToken: params.chainToken,
|
|
554
|
+
mxnAmount: params.mxnAmount,
|
|
555
|
+
reference: params.reference,
|
|
556
|
+
transactionHash: params.transactionHash
|
|
557
|
+
});
|
|
558
|
+
return {
|
|
559
|
+
sku,
|
|
560
|
+
transaction
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// src/chipi-sdk.ts
|
|
566
|
+
var ChipiSDK = class {
|
|
567
|
+
constructor(config) {
|
|
568
|
+
this.client = new ChipiClient(config);
|
|
569
|
+
this.nodeUrl = config.nodeUrl || shared.STARKNET_NETWORKS.MAINNET;
|
|
570
|
+
this.wallets = new ChipiWallets(this.client);
|
|
571
|
+
this.transactions = new ChipiTransactions(this.client);
|
|
572
|
+
this.skus = new ChipiSkus(this.client);
|
|
573
|
+
this.executeTransaction = this.executeTransaction.bind(this);
|
|
574
|
+
this.transfer = this.transfer.bind(this);
|
|
575
|
+
this.approve = this.approve.bind(this);
|
|
576
|
+
this.stakeVesuUsdc = this.stakeVesuUsdc.bind(this);
|
|
577
|
+
this.withdrawVesuUsdc = this.withdrawVesuUsdc.bind(this);
|
|
578
|
+
this.callAnyContract = this.callAnyContract.bind(this);
|
|
579
|
+
this.createWallet = this.createWallet.bind(this);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Execute a gasless transaction
|
|
583
|
+
*/
|
|
584
|
+
async executeTransaction(input) {
|
|
585
|
+
return this.transactions.executeTransaction(input);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Transfer tokens
|
|
589
|
+
*/
|
|
590
|
+
async transfer(params) {
|
|
591
|
+
const { encryptKey, wallet, contractAddress, recipient, amount, decimals, bearerToken } = params;
|
|
592
|
+
const formattedAmount = shared.formatAmount(amount, decimals);
|
|
593
|
+
return this.executeTransaction({
|
|
594
|
+
encryptKey,
|
|
595
|
+
wallet,
|
|
596
|
+
bearerToken,
|
|
597
|
+
calls: [
|
|
598
|
+
{
|
|
599
|
+
contractAddress,
|
|
600
|
+
entrypoint: "transfer",
|
|
601
|
+
calldata: [
|
|
602
|
+
recipient,
|
|
603
|
+
formattedAmount,
|
|
604
|
+
"0x0"
|
|
605
|
+
]
|
|
606
|
+
}
|
|
607
|
+
]
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Approve token spending
|
|
612
|
+
*/
|
|
613
|
+
async approve(params) {
|
|
614
|
+
const { encryptKey, wallet, contractAddress, spender, amount, decimals, bearerToken } = params;
|
|
615
|
+
return this.executeTransaction({
|
|
616
|
+
encryptKey,
|
|
617
|
+
wallet,
|
|
618
|
+
bearerToken,
|
|
619
|
+
calls: [
|
|
620
|
+
{
|
|
621
|
+
contractAddress,
|
|
622
|
+
entrypoint: "approve",
|
|
623
|
+
calldata: [
|
|
624
|
+
spender,
|
|
625
|
+
shared.formatAmount(amount, decimals),
|
|
626
|
+
"0x0"
|
|
627
|
+
]
|
|
628
|
+
}
|
|
629
|
+
]
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Stake USDC in Vesu protocol
|
|
634
|
+
*/
|
|
635
|
+
async stakeVesuUsdc(params) {
|
|
636
|
+
const { encryptKey, wallet, amount, receiverWallet, bearerToken } = params;
|
|
637
|
+
const formattedAmount = shared.formatAmount(amount, 6);
|
|
638
|
+
return this.executeTransaction({
|
|
639
|
+
encryptKey,
|
|
640
|
+
wallet,
|
|
641
|
+
bearerToken,
|
|
642
|
+
calls: [
|
|
643
|
+
{
|
|
644
|
+
contractAddress: shared.CONTRACT_ADDRESSES.USDC_MAINNET,
|
|
645
|
+
entrypoint: "approve",
|
|
646
|
+
calldata: [
|
|
647
|
+
shared.CONTRACT_ADDRESSES.VESU_USDC_MAINNET,
|
|
648
|
+
formattedAmount,
|
|
649
|
+
"0x0"
|
|
650
|
+
]
|
|
651
|
+
},
|
|
652
|
+
{
|
|
653
|
+
contractAddress: shared.CONTRACT_ADDRESSES.VESU_USDC_MAINNET,
|
|
654
|
+
entrypoint: "deposit",
|
|
655
|
+
calldata: [
|
|
656
|
+
formattedAmount,
|
|
657
|
+
"0x0",
|
|
658
|
+
receiverWallet
|
|
659
|
+
]
|
|
660
|
+
}
|
|
661
|
+
]
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Withdraw USDC from Vesu protocol
|
|
666
|
+
*/
|
|
667
|
+
async withdrawVesuUsdc(params) {
|
|
668
|
+
const { encryptKey, wallet, amount, recipient, bearerToken } = params;
|
|
669
|
+
const formattedAmount = shared.formatAmount(amount, 6);
|
|
670
|
+
return this.executeTransaction({
|
|
671
|
+
encryptKey,
|
|
672
|
+
wallet,
|
|
673
|
+
bearerToken,
|
|
674
|
+
calls: [
|
|
675
|
+
{
|
|
676
|
+
contractAddress: shared.CONTRACT_ADDRESSES.VESU_USDC_MAINNET,
|
|
677
|
+
entrypoint: "withdraw",
|
|
678
|
+
calldata: [
|
|
679
|
+
formattedAmount,
|
|
680
|
+
recipient,
|
|
681
|
+
"0x0"
|
|
682
|
+
]
|
|
683
|
+
}
|
|
684
|
+
]
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Call any contract method
|
|
689
|
+
*/
|
|
690
|
+
async callAnyContract(params) {
|
|
691
|
+
const { encryptKey, wallet, calls, bearerToken } = params;
|
|
692
|
+
return this.executeTransaction({
|
|
693
|
+
encryptKey,
|
|
694
|
+
wallet,
|
|
695
|
+
bearerToken,
|
|
696
|
+
calls
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Create a new wallet
|
|
701
|
+
*/
|
|
702
|
+
async createWallet(params) {
|
|
703
|
+
const { encryptKey, bearerToken } = params;
|
|
704
|
+
return this.wallets.createWallet({
|
|
705
|
+
encryptKey,
|
|
706
|
+
apiPublicKey: this.client.getApiPublicKey(),
|
|
707
|
+
bearerToken,
|
|
708
|
+
nodeUrl: this.nodeUrl,
|
|
709
|
+
backendUrl: this.client.baseUrl
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
|
|
714
|
+
exports.ChipiClient = ChipiClient;
|
|
715
|
+
exports.ChipiSDK = ChipiSDK;
|
|
716
|
+
exports.ChipiSkus = ChipiSkus;
|
|
717
|
+
exports.ChipiTransactions = ChipiTransactions;
|
|
718
|
+
exports.ChipiWallets = ChipiWallets;
|
|
719
|
+
exports.decryptPrivateKey = decryptPrivateKey;
|
|
720
|
+
exports.encryptPrivateKey = encryptPrivateKey;
|
|
721
|
+
//# sourceMappingURL=index.js.map
|
|
722
|
+
//# sourceMappingURL=index.js.map
|