@frontiertower/frontier-sdk 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/index.js ADDED
@@ -0,0 +1,480 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ChainAccess: () => ChainAccess,
24
+ FrontierSDK: () => FrontierSDK,
25
+ StorageAccess: () => StorageAccess,
26
+ UserAccess: () => UserAccess,
27
+ WalletAccess: () => WalletAccess
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/access/wallet.ts
32
+ var WalletAccess = class {
33
+ constructor(sdk) {
34
+ this.sdk = sdk;
35
+ }
36
+ /**
37
+ * Get the current wallet balance
38
+ *
39
+ * Returns the total USD stablecoin balance for the current network,
40
+ * normalized to 18 decimals for consistency.
41
+ *
42
+ * @returns Balance as bigint (18 decimals)
43
+ * @throws {Error} If no wallet exists
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const balance = await sdk.getWallet().getBalance();
48
+ * console.log('Balance:', balance.toString());
49
+ * ```
50
+ */
51
+ async getBalance() {
52
+ return this.sdk.request("wallet:getBalance");
53
+ }
54
+ /**
55
+ * Get the current wallet balance formatted for display
56
+ *
57
+ * Returns the total USD stablecoin balance as a formatted string
58
+ * with currency symbol (e.g., '$10.50').
59
+ *
60
+ * @returns Formatted balance string with $ sign
61
+ * @throws {Error} If no wallet exists
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const balance = await sdk.getWallet().getBalanceFormatted();
66
+ * console.log('Balance:', balance); // '$10.50'
67
+ * ```
68
+ */
69
+ async getBalanceFormatted() {
70
+ return this.sdk.request("wallet:getBalanceFormatted");
71
+ }
72
+ /**
73
+ * Get the wallet address for the current network
74
+ *
75
+ * Returns the smart account contract address for the current chain.
76
+ *
77
+ * @returns The wallet address as a hex string
78
+ * @throws {Error} If no wallet exists
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * const address = await sdk.getWallet().getAddress();
83
+ * console.log('Address:', address);
84
+ * ```
85
+ */
86
+ async getAddress() {
87
+ return this.sdk.request("wallet:getAddress");
88
+ }
89
+ /**
90
+ * Get smart account for the current network
91
+ *
92
+ * Returns detailed information about the smart account including
93
+ * deployment status and network information.
94
+ *
95
+ * @returns Smart account information
96
+ * @throws {Error} If no smart account found for current network
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const account = await sdk.getWallet().getSmartAccount();
101
+ * console.log('Contract address:', account.contractAddress);
102
+ * console.log('Network:', account.network);
103
+ * ```
104
+ */
105
+ async getSmartAccount() {
106
+ return this.sdk.request("wallet:getSmartAccount");
107
+ }
108
+ /**
109
+ * Transfer ERC20 tokens
110
+ *
111
+ * Sends ERC20 tokens to a recipient address using the current network.
112
+ * Requires biometric authentication and sufficient balance.
113
+ *
114
+ * @param tokenAddress - ERC20 token contract address
115
+ * @param to - Recipient address
116
+ * @param amount - Amount to send (in token's smallest unit, e.g., wei)
117
+ * @param overrides - Optional gas overrides
118
+ * @returns User operation receipt with transaction details
119
+ * @throws {Error} If insufficient balance or transaction fails
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * import { parseUnits } from 'viem';
124
+ *
125
+ * const receipt = await sdk.getWallet().transferERC20(
126
+ * '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC on Sepolia
127
+ * '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
128
+ * parseUnits('10.5', 6) // 10.5 USDC (6 decimals)
129
+ * );
130
+ * console.log('Transaction:', receipt.transactionHash);
131
+ * ```
132
+ */
133
+ async transferERC20(tokenAddress, to, amount, overrides) {
134
+ return this.sdk.request("wallet:transferERC20", {
135
+ tokenAddress,
136
+ to,
137
+ amount,
138
+ overrides
139
+ });
140
+ }
141
+ /**
142
+ * Approve ERC20 tokens for spending
143
+ *
144
+ * Approves a spender to transfer tokens on your behalf.
145
+ * Required before interacting with DeFi protocols.
146
+ *
147
+ * @param tokenAddress - ERC20 token contract address
148
+ * @param spender - Address allowed to spend tokens
149
+ * @param amount - Amount to approve (in token's smallest unit)
150
+ * @param overrides - Optional gas overrides
151
+ * @returns User operation receipt with transaction details
152
+ * @throws {Error} If transaction fails
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * import { parseUnits } from 'viem';
157
+ *
158
+ * const receipt = await sdk.getWallet().approveERC20(
159
+ * '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // USDC
160
+ * '0xProtocolAddress',
161
+ * parseUnits('100', 6) // Approve 100 USDC
162
+ * );
163
+ * ```
164
+ */
165
+ async approveERC20(tokenAddress, spender, amount, overrides) {
166
+ return this.sdk.request("wallet:approveERC20", {
167
+ tokenAddress,
168
+ spender,
169
+ amount,
170
+ overrides
171
+ });
172
+ }
173
+ /**
174
+ * Transfer native currency (ETH)
175
+ *
176
+ * Sends native currency to a recipient address.
177
+ * Requires biometric authentication and sufficient balance.
178
+ *
179
+ * @param to - Recipient address
180
+ * @param amount - Amount to send in wei
181
+ * @param overrides - Optional gas overrides
182
+ * @returns User operation receipt with transaction details
183
+ * @throws {Error} If insufficient balance or transaction fails
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * import { parseEther } from 'viem';
188
+ *
189
+ * const receipt = await sdk.getWallet().transferNative(
190
+ * '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb',
191
+ * parseEther('0.1') // 0.1 ETH
192
+ * );
193
+ * ```
194
+ */
195
+ async transferNative(to, amount, overrides) {
196
+ return this.sdk.request("wallet:transferNative", {
197
+ to,
198
+ amount,
199
+ overrides
200
+ });
201
+ }
202
+ /**
203
+ * Execute arbitrary contract call
204
+ *
205
+ * Executes a custom contract interaction with full control over
206
+ * the target address, value, and calldata.
207
+ *
208
+ * @param call - Execute call parameters
209
+ * @param overrides - Optional gas overrides
210
+ * @returns User operation receipt with transaction details
211
+ * @throws {Error} If transaction fails
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * import { encodeFunctionData } from 'viem';
216
+ *
217
+ * const receipt = await sdk.getWallet().executeCall({
218
+ * to: '0xContractAddress',
219
+ * value: 0n,
220
+ * data: encodeFunctionData({
221
+ * abi: contractABI,
222
+ * functionName: 'someFunction',
223
+ * args: [arg1, arg2]
224
+ * })
225
+ * });
226
+ * ```
227
+ */
228
+ async executeCall(call, overrides) {
229
+ return this.sdk.request("wallet:executeCall", {
230
+ call,
231
+ overrides
232
+ });
233
+ }
234
+ };
235
+
236
+ // src/access/storage.ts
237
+ var StorageAccess = class {
238
+ constructor(sdk) {
239
+ this.sdk = sdk;
240
+ }
241
+ /**
242
+ * Get data from persistent storage
243
+ * Requires permission: storage:get or storage:*
244
+ */
245
+ async get(key) {
246
+ return this.sdk.request("storage:get", { key });
247
+ }
248
+ /**
249
+ * Store data persistently
250
+ * Requires permission: storage:set or storage:*
251
+ */
252
+ async set(key, value) {
253
+ return this.sdk.request("storage:set", { key, value });
254
+ }
255
+ /**
256
+ * Remove data from persistent storage
257
+ * Requires permission: storage:remove or storage:*
258
+ */
259
+ async remove(key) {
260
+ return this.sdk.request("storage:remove", { key });
261
+ }
262
+ /**
263
+ * Clear all data from persistent storage
264
+ * Requires permission: storage:clear or storage:*
265
+ */
266
+ async clear() {
267
+ return this.sdk.request("storage:clear");
268
+ }
269
+ };
270
+
271
+ // src/access/chain.ts
272
+ var ChainAccess = class {
273
+ constructor(sdk) {
274
+ this.sdk = sdk;
275
+ }
276
+ /**
277
+ * Get the current network name
278
+ *
279
+ * Returns the network identifier for the currently active chain
280
+ * (e.g., 'base', 'base-sepolia', 'ethereum').
281
+ *
282
+ * @returns Network identifier string
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * const network = await sdk.getChain().getCurrentNetwork();
287
+ * console.log('Current network:', network); // 'base-sepolia'
288
+ * ```
289
+ */
290
+ async getCurrentNetwork() {
291
+ return this.sdk.request("chain:getCurrentNetwork");
292
+ }
293
+ /**
294
+ * Get all available networks
295
+ *
296
+ * Returns a list of network identifiers that the app can switch to.
297
+ *
298
+ * @returns Array of network identifier strings
299
+ *
300
+ * @example
301
+ * ```typescript
302
+ * const networks = await sdk.getChain().getAvailableNetworks();
303
+ * console.log('Available networks:', networks); // ['base', 'base-sepolia']
304
+ * ```
305
+ */
306
+ async getAvailableNetworks() {
307
+ return this.sdk.request("chain:getAvailableNetworks");
308
+ }
309
+ /**
310
+ * Switch to a different network
311
+ *
312
+ * Changes the active blockchain network. This will affect all subsequent
313
+ * wallet operations and contract interactions.
314
+ *
315
+ * @param network - The network identifier to switch to
316
+ * @throws {Error} If the network is not available or switching fails
317
+ *
318
+ * @example
319
+ * ```typescript
320
+ * await sdk.getChain().switchNetwork('base');
321
+ * console.log('Switched to Base mainnet');
322
+ * ```
323
+ */
324
+ async switchNetwork(network) {
325
+ return this.sdk.request("chain:switchNetwork", { network });
326
+ }
327
+ /**
328
+ * Get full chain configuration for current network
329
+ *
330
+ * Returns detailed configuration including chain ID, RPC URLs,
331
+ * block explorer, and native currency information.
332
+ *
333
+ * @returns Complete chain configuration object
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * const config = await sdk.getChain().getCurrentChainConfig();
338
+ * console.log('Chain ID:', config.id);
339
+ * console.log('Block explorer:', config.blockExplorer.url);
340
+ * ```
341
+ */
342
+ async getCurrentChainConfig() {
343
+ return this.sdk.request("chain:getCurrentChainConfig");
344
+ }
345
+ };
346
+
347
+ // src/access/user.ts
348
+ var UserAccess = class {
349
+ constructor(sdk) {
350
+ this.sdk = sdk;
351
+ }
352
+ /**
353
+ * Get current user details
354
+ *
355
+ * Returns basic information about the currently authenticated user,
356
+ * including their ID, email, and name.
357
+ *
358
+ * @returns User object with basic information
359
+ * @throws {Error} If user is not authenticated
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * const user = await sdk.getUser().getDetails();
364
+ * console.log('User email:', user.email);
365
+ * console.log('User name:', `${user.firstName} ${user.lastName}`);
366
+ * ```
367
+ */
368
+ async getDetails() {
369
+ return this.sdk.request("user:getDetails");
370
+ }
371
+ /**
372
+ * Get user profile by ID
373
+ *
374
+ * Returns detailed profile information for a specific user,
375
+ * including social media handles, preferences, and community information.
376
+ *
377
+ * @param id - The profile ID to fetch
378
+ * @returns UserProfile object with detailed information
379
+ * @throws {Error} If profile is not found or access is denied
380
+ *
381
+ * @example
382
+ * ```typescript
383
+ * const profile = await sdk.getUser().getProfile(123);
384
+ * console.log('Nickname:', profile.nickname);
385
+ * console.log('GitHub:', profile.githubHandle);
386
+ * console.log('Community:', profile.communityName);
387
+ * ```
388
+ */
389
+ async getProfile(id) {
390
+ return this.sdk.request("user:getProfile", { id });
391
+ }
392
+ };
393
+
394
+ // src/sdk.ts
395
+ var FrontierSDK = class {
396
+ constructor() {
397
+ this.requestId = 0;
398
+ this.pendingRequests = /* @__PURE__ */ new Map();
399
+ this.handleMessage = (event) => {
400
+ if (event.source !== window.parent) return;
401
+ const response = event.data;
402
+ const pending = this.pendingRequests.get(response.requestId);
403
+ if (pending) {
404
+ if (response.type === "error") {
405
+ pending.reject(new Error(response.error));
406
+ } else {
407
+ pending.resolve(response.result);
408
+ }
409
+ this.pendingRequests.delete(response.requestId);
410
+ }
411
+ };
412
+ this.wallet = new WalletAccess(this);
413
+ this.storage = new StorageAccess(this);
414
+ this.chain = new ChainAccess(this);
415
+ this.user = new UserAccess(this);
416
+ window.addEventListener("message", this.handleMessage);
417
+ this.notifyReady();
418
+ }
419
+ /**
420
+ * Internal request method used by access classes
421
+ * @internal
422
+ */
423
+ request(type, payload) {
424
+ return new Promise((resolve, reject) => {
425
+ const requestId = `${Date.now()}-${this.requestId++}`;
426
+ this.pendingRequests.set(requestId, { resolve, reject });
427
+ const request = { type, requestId, payload };
428
+ window.parent.postMessage(request, "*");
429
+ setTimeout(() => {
430
+ if (this.pendingRequests.has(requestId)) {
431
+ this.pendingRequests.delete(requestId);
432
+ reject(new Error("Request timeout"));
433
+ }
434
+ }, 3e4);
435
+ });
436
+ }
437
+ notifyReady() {
438
+ window.parent.postMessage({ type: "app:ready", payload: null }, "*");
439
+ }
440
+ /**
441
+ * Get wallet access instance
442
+ */
443
+ getWallet() {
444
+ return this.wallet;
445
+ }
446
+ /**
447
+ * Get storage access instance
448
+ */
449
+ getStorage() {
450
+ return this.storage;
451
+ }
452
+ /**
453
+ * Get chain access instance
454
+ */
455
+ getChain() {
456
+ return this.chain;
457
+ }
458
+ /**
459
+ * Get user access instance
460
+ */
461
+ getUser() {
462
+ return this.user;
463
+ }
464
+ /**
465
+ * Cleanup: Remove event listeners
466
+ * Call this when your app is being destroyed
467
+ */
468
+ destroy() {
469
+ window.removeEventListener("message", this.handleMessage);
470
+ this.pendingRequests.clear();
471
+ }
472
+ };
473
+ // Annotate the CommonJS export names for ESM import in node:
474
+ 0 && (module.exports = {
475
+ ChainAccess,
476
+ FrontierSDK,
477
+ StorageAccess,
478
+ UserAccess,
479
+ WalletAccess
480
+ });