@lendasat/lendaswap-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.
Files changed (46) hide show
  1. package/README.md +105 -0
  2. package/dist/api.d.ts +413 -0
  3. package/dist/api.d.ts.map +1 -0
  4. package/dist/api.js +306 -0
  5. package/dist/api.js.map +1 -0
  6. package/dist/index.d.ts +42 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +45 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/price-feed.d.ts +121 -0
  11. package/dist/price-feed.d.ts.map +1 -0
  12. package/dist/price-feed.js +178 -0
  13. package/dist/price-feed.js.map +1 -0
  14. package/dist/storage/dexieSwapStorage.d.ts +111 -0
  15. package/dist/storage/dexieSwapStorage.d.ts.map +1 -0
  16. package/dist/storage/dexieSwapStorage.js +139 -0
  17. package/dist/storage/dexieSwapStorage.js.map +1 -0
  18. package/dist/storage/dexieWalletStorage.d.ts +99 -0
  19. package/dist/storage/dexieWalletStorage.d.ts.map +1 -0
  20. package/dist/storage/dexieWalletStorage.js +139 -0
  21. package/dist/storage/dexieWalletStorage.js.map +1 -0
  22. package/dist/storage/index.d.ts +18 -0
  23. package/dist/storage/index.d.ts.map +1 -0
  24. package/dist/storage/index.js +20 -0
  25. package/dist/storage/index.js.map +1 -0
  26. package/dist/storage/indexedDB.d.ts +30 -0
  27. package/dist/storage/indexedDB.d.ts.map +1 -0
  28. package/dist/storage/indexedDB.js +108 -0
  29. package/dist/storage/indexedDB.js.map +1 -0
  30. package/dist/storage/localStorage.d.ts +32 -0
  31. package/dist/storage/localStorage.d.ts.map +1 -0
  32. package/dist/storage/localStorage.js +58 -0
  33. package/dist/storage/localStorage.js.map +1 -0
  34. package/dist/storage/memory.d.ts +35 -0
  35. package/dist/storage/memory.d.ts.map +1 -0
  36. package/dist/storage/memory.js +50 -0
  37. package/dist/storage/memory.js.map +1 -0
  38. package/dist/types.d.ts +59 -0
  39. package/dist/types.d.ts.map +1 -0
  40. package/dist/types.js +5 -0
  41. package/dist/types.js.map +1 -0
  42. package/dist/wallet.d.ts +108 -0
  43. package/dist/wallet.d.ts.map +1 -0
  44. package/dist/wallet.js +188 -0
  45. package/dist/wallet.js.map +1 -0
  46. package/package.json +58 -0
@@ -0,0 +1,178 @@
1
+ /**
2
+ * WebSocket price feed service for real-time price updates.
3
+ *
4
+ * This module provides a WebSocket client that connects to the Lendaswap
5
+ * price feed endpoint and delivers real-time price updates with automatic
6
+ * reconnection support.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { PriceFeedService, type PriceUpdateMessage } from '@lendaswap/sdk';
11
+ *
12
+ * const priceFeed = new PriceFeedService('wss://api.lendaswap.com');
13
+ *
14
+ * const unsubscribe = priceFeed.subscribe((update) => {
15
+ * console.log('Price update:', update);
16
+ * });
17
+ *
18
+ * // Later, to unsubscribe:
19
+ * unsubscribe();
20
+ * ```
21
+ */
22
+ /**
23
+ * WebSocket price feed service with automatic reconnection.
24
+ *
25
+ * Manages connection to the /ws/prices endpoint with exponential backoff
26
+ * reconnection. Automatically connects when the first listener subscribes
27
+ * and disconnects when the last listener unsubscribes.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const priceFeed = new PriceFeedService('wss://api.lendaswap.com');
32
+ *
33
+ * // Subscribe to price updates
34
+ * const unsubscribe = priceFeed.subscribe((update) => {
35
+ * console.log('Prices:', update.pairs);
36
+ * });
37
+ *
38
+ * // Unsubscribe when done
39
+ * unsubscribe();
40
+ * ```
41
+ */
42
+ export class PriceFeedService {
43
+ wsUrl;
44
+ ws = null;
45
+ reconnectTimer = null;
46
+ listeners = new Set();
47
+ reconnectDelay = 1000; // Start with 1 second
48
+ maxReconnectDelay = 30000; // Max 30 seconds
49
+ isManualClose = false;
50
+ /**
51
+ * Create a new PriceFeedService.
52
+ *
53
+ * @param baseUrl - The base WebSocket URL (e.g., 'wss://api.lendaswap.com')
54
+ * or HTTP URL which will be converted to WebSocket.
55
+ */
56
+ constructor(baseUrl) {
57
+ // Convert HTTP to WebSocket URL if needed
58
+ this.wsUrl = baseUrl.replace(/^http/, "ws");
59
+ // Ensure we have the /ws/prices path
60
+ if (!this.wsUrl.endsWith("/ws/prices")) {
61
+ this.wsUrl = `${this.wsUrl}/ws/prices`;
62
+ }
63
+ }
64
+ /**
65
+ * Subscribe to price updates.
66
+ *
67
+ * @param callback - Function to call when prices are updated
68
+ * @returns Unsubscribe function
69
+ */
70
+ subscribe(callback) {
71
+ this.listeners.add(callback);
72
+ // Connect if not already connected
73
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
74
+ this.connect();
75
+ }
76
+ // Return unsubscribe function
77
+ return () => {
78
+ this.listeners.delete(callback);
79
+ // Close connection if no more listeners
80
+ if (this.listeners.size === 0) {
81
+ this.close();
82
+ }
83
+ };
84
+ }
85
+ /**
86
+ * Check if the WebSocket is currently connected.
87
+ */
88
+ isConnected() {
89
+ return this.ws?.readyState === WebSocket.OPEN;
90
+ }
91
+ /**
92
+ * Get the number of active listeners.
93
+ */
94
+ listenerCount() {
95
+ return this.listeners.size;
96
+ }
97
+ /**
98
+ * Connect to WebSocket price feed.
99
+ */
100
+ connect() {
101
+ if (this.ws?.readyState === WebSocket.OPEN) {
102
+ return;
103
+ }
104
+ this.isManualClose = false;
105
+ try {
106
+ this.ws = new WebSocket(this.wsUrl);
107
+ this.ws.onopen = () => {
108
+ console.log("Price feed WebSocket connected");
109
+ // Reset reconnect delay on successful connection
110
+ this.reconnectDelay = 1000;
111
+ };
112
+ this.ws.onmessage = (event) => {
113
+ try {
114
+ const update = JSON.parse(event.data);
115
+ // Notify all listeners
116
+ this.listeners.forEach((callback) => {
117
+ try {
118
+ callback(update);
119
+ }
120
+ catch (err) {
121
+ console.error("Error in price feed callback:", err);
122
+ }
123
+ });
124
+ }
125
+ catch (error) {
126
+ console.error("Failed to parse price update:", error);
127
+ }
128
+ };
129
+ this.ws.onerror = (error) => {
130
+ console.error("Price feed WebSocket error:", error);
131
+ };
132
+ this.ws.onclose = () => {
133
+ console.log("Price feed WebSocket closed");
134
+ this.ws = null;
135
+ // Only reconnect if we have listeners and it wasn't a manual close
136
+ if (this.listeners.size > 0 && !this.isManualClose) {
137
+ this.scheduleReconnect();
138
+ }
139
+ };
140
+ }
141
+ catch (error) {
142
+ console.error("Failed to create WebSocket:", error);
143
+ if (this.listeners.size > 0 && !this.isManualClose) {
144
+ this.scheduleReconnect();
145
+ }
146
+ }
147
+ }
148
+ /**
149
+ * Schedule reconnection with exponential backoff.
150
+ */
151
+ scheduleReconnect() {
152
+ if (this.reconnectTimer !== null) {
153
+ return;
154
+ }
155
+ console.log(`Reconnecting in ${this.reconnectDelay}ms...`);
156
+ this.reconnectTimer = setTimeout(() => {
157
+ this.reconnectTimer = null;
158
+ this.connect();
159
+ // Exponential backoff
160
+ this.reconnectDelay = Math.min(this.reconnectDelay * 2, this.maxReconnectDelay);
161
+ }, this.reconnectDelay);
162
+ }
163
+ /**
164
+ * Close WebSocket connection.
165
+ */
166
+ close() {
167
+ this.isManualClose = true;
168
+ if (this.reconnectTimer !== null) {
169
+ clearTimeout(this.reconnectTimer);
170
+ this.reconnectTimer = null;
171
+ }
172
+ if (this.ws) {
173
+ this.ws.close();
174
+ this.ws = null;
175
+ }
176
+ }
177
+ }
178
+ //# sourceMappingURL=price-feed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"price-feed.js","sourceRoot":"","sources":["../src/price-feed.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA0CH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,gBAAgB;IACnB,KAAK,CAAS;IACd,EAAE,GAAqB,IAAI,CAAC;IAC5B,cAAc,GAAyC,IAAI,CAAC;IAC5D,SAAS,GAA6B,IAAI,GAAG,EAAE,CAAC;IAChD,cAAc,GAAG,IAAI,CAAC,CAAC,sBAAsB;IAC7C,iBAAiB,GAAG,KAAK,CAAC,CAAC,iBA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AAI,CAAC,EAAE,GAAG,IAAI,CAAC;QACjB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Dexie-based swap storage provider for IndexedDB.
3
+ *
4
+ * This module provides a typed swap storage implementation using Dexie,
5
+ * which is a wrapper around IndexedDB that provides a simpler API.
6
+ */
7
+ import type { ExtendedSwapStorageData } from "../api.js";
8
+ /**
9
+ * Stored swap record in IndexedDB.
10
+ * Extends ExtendedSwapStorageData with an id field for Dexie's primary key.
11
+ */
12
+ interface SwapRecord extends ExtendedSwapStorageData {
13
+ id: string;
14
+ }
15
+ /**
16
+ * Dexie-based swap storage provider.
17
+ *
18
+ * Stores swap data as typed objects in IndexedDB using Dexie.
19
+ * This provides better performance and querying capabilities compared
20
+ * to storing serialized JSON strings.
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { DexieSwapStorageProvider } from '@lendaswap/sdk';
25
+ *
26
+ * const swapStorage = new DexieSwapStorageProvider();
27
+ *
28
+ * // Use with the Client
29
+ * const client = await Client.create(
30
+ * 'https://api.lendaswap.com',
31
+ * walletStorage,
32
+ * swapStorage,
33
+ * 'bitcoin',
34
+ * 'https://arkade.computer'
35
+ * );
36
+ * ```
37
+ */
38
+ export declare class DexieSwapStorageProvider {
39
+ private db;
40
+ /**
41
+ * Create a new DexieSwapStorageProvider.
42
+ *
43
+ * @param dbName - Optional database name (default: "lendaswap")
44
+ */
45
+ constructor(dbName?: string);
46
+ /**
47
+ * Get swap data by swap ID.
48
+ *
49
+ * @param swapId - The swap ID
50
+ * @returns The swap data, or null if not found
51
+ */
52
+ get(swapId: string): Promise<ExtendedSwapStorageData | null>;
53
+ /**
54
+ * Store swap data.
55
+ *
56
+ * @param swapId - The swap ID
57
+ * @param data - The swap data to store
58
+ */
59
+ store(swapId: string, data: ExtendedSwapStorageData): Promise<void>;
60
+ /**
61
+ * Delete swap data by swap ID.
62
+ *
63
+ * @param swapId - The swap ID
64
+ */
65
+ delete(swapId: string): Promise<void>;
66
+ /**
67
+ * List all stored swap IDs.
68
+ *
69
+ * @returns Array of swap IDs
70
+ */
71
+ list(): Promise<string[]>;
72
+ /**
73
+ * Clear all swap data.
74
+ */
75
+ clear(): Promise<void>;
76
+ /**
77
+ * Get all stored swaps.
78
+ *
79
+ * @returns Array of all swap data with their IDs
80
+ */
81
+ getAll(): Promise<SwapRecord[]>;
82
+ /**
83
+ * Close the database connection.
84
+ */
85
+ close(): void;
86
+ }
87
+ /**
88
+ * Create a Dexie-based swap storage provider.
89
+ *
90
+ * This is a convenience function for creating a DexieSwapStorageProvider.
91
+ *
92
+ * @param dbName - Optional database name (default: "lendaswap")
93
+ * @returns A new DexieSwapStorageProvider instance
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * import { createDexieSwapStorage, Client } from '@lendaswap/sdk';
98
+ *
99
+ * const swapStorage = createDexieSwapStorage();
100
+ * const client = await Client.create(
101
+ * 'https://api.lendaswap.com',
102
+ * walletStorage,
103
+ * swapStorage,
104
+ * 'bitcoin',
105
+ * 'https://arkade.computer'
106
+ * );
107
+ * ```
108
+ */
109
+ export declare function createDexieSwapStorage(dbName?: string): DexieSwapStorageProvider;
110
+ export {};
111
+ //# sourceMappingURL=dexieSwapStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dexieSwapStorage.d.ts","sourceRoot":"","sources":["../../src/storage/dexieSwapStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAEzD;;;GAGG;AACH,UAAU,UAAW,SAAQ,uBAAuB;IAClD,EAAE,EAAE,MAAM,CAAC;CACZ;AAgBD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,wBAAwB;IACnC,OAAO,CAAC,EAAE,CAAoB;IAE9B;;;;OAIG;gBACS,MAAM,CAAC,EAAE,MAAM;IAI3B;;;;;OAKG;IACG,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC;IAUlE;;;;;OAKG;IACG,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzE;;;;OAIG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI3C;;;;OAIG;IACG,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAI/B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;IAIrC;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,CAAC,EAAE,MAAM,GACd,wBAAwB,CAE1B"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Dexie-based swap storage provider for IndexedDB.
3
+ *
4
+ * This module provides a typed swap storage implementation using Dexie,
5
+ * which is a wrapper around IndexedDB that provides a simpler API.
6
+ */
7
+ import Dexie from "dexie";
8
+ /**
9
+ * Dexie database for storing swap data.
10
+ */
11
+ class LendaswapDatabase extends Dexie {
12
+ swaps;
13
+ constructor(dbName = "lendaswap") {
14
+ super(dbName);
15
+ this.version(1).stores({
16
+ swaps: "id", // Primary key only, no additional indexes needed
17
+ });
18
+ }
19
+ }
20
+ /**
21
+ * Dexie-based swap storage provider.
22
+ *
23
+ * Stores swap data as typed objects in IndexedDB using Dexie.
24
+ * This provides better performance and querying capabilities compared
25
+ * to storing serialized JSON strings.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { DexieSwapStorageProvider } from '@lendaswap/sdk';
30
+ *
31
+ * const swapStorage = new DexieSwapStorageProvider();
32
+ *
33
+ * // Use with the Client
34
+ * const client = await Client.create(
35
+ * 'https://api.lendaswap.com',
36
+ * walletStorage,
37
+ * swapStorage,
38
+ * 'bitcoin',
39
+ * 'https://arkade.computer'
40
+ * );
41
+ * ```
42
+ */
43
+ export class DexieSwapStorageProvider {
44
+ db;
45
+ /**
46
+ * Create a new DexieSwapStorageProvider.
47
+ *
48
+ * @param dbName - Optional database name (default: "lendaswap")
49
+ */
50
+ constructor(dbName) {
51
+ this.db = new LendaswapDatabase(dbName);
52
+ }
53
+ /**
54
+ * Get swap data by swap ID.
55
+ *
56
+ * @param swapId - The swap ID
57
+ * @returns The swap data, or null if not found
58
+ */
59
+ async get(swapId) {
60
+ const record = await this.db.swaps.get(swapId);
61
+ if (!record) {
62
+ return null;
63
+ }
64
+ // Remove the id field before returning (it's not part of ExtendedSwapStorageData)
65
+ const { id: _, ...data } = record;
66
+ return data;
67
+ }
68
+ /**
69
+ * Store swap data.
70
+ *
71
+ * @param swapId - The swap ID
72
+ * @param data - The swap data to store
73
+ */
74
+ async store(swapId, data) {
75
+ await this.db.swaps.put({ id: swapId, ...data });
76
+ }
77
+ /**
78
+ * Delete swap data by swap ID.
79
+ *
80
+ * @param swapId - The swap ID
81
+ */
82
+ async delete(swapId) {
83
+ await this.db.swaps.delete(swapId);
84
+ }
85
+ /**
86
+ * List all stored swap IDs.
87
+ *
88
+ * @returns Array of swap IDs
89
+ */
90
+ async list() {
91
+ return (await this.db.swaps.toCollection().primaryKeys());
92
+ }
93
+ /**
94
+ * Clear all swap data.
95
+ */
96
+ async clear() {
97
+ await this.db.swaps.clear();
98
+ }
99
+ /**
100
+ * Get all stored swaps.
101
+ *
102
+ * @returns Array of all swap data with their IDs
103
+ */
104
+ async getAll() {
105
+ return this.db.swaps.toArray();
106
+ }
107
+ /**
108
+ * Close the database connection.
109
+ */
110
+ close() {
111
+ this.db.close();
112
+ }
113
+ }
114
+ /**
115
+ * Create a Dexie-based swap storage provider.
116
+ *
117
+ * This is a convenience function for creating a DexieSwapStorageProvider.
118
+ *
119
+ * @param dbName - Optional database name (default: "lendaswap")
120
+ * @returns A new DexieSwapStorageProvider instance
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { createDexieSwapStorage, Client } from '@lendaswap/sdk';
125
+ *
126
+ * const swapStorage = createDexieSwapStorage();
127
+ * const client = await Client.create(
128
+ * 'https://api.lendaswap.com',
129
+ * walletStorage,
130
+ * swapStorage,
131
+ * 'bitcoin',
132
+ * 'https://arkade.computer'
133
+ * );
134
+ * ```
135
+ */
136
+ export function createDexieSwapStorage(dbName) {
137
+ return new DexieSwapStorageProvider(dbName);
138
+ }
139
+ //# sourceMappingURL=dexieSwapStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dexieSwapStorage.js","sourceRoot":"","sources":["../../src/storage/dexieSwapStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAqB,MAAM,OAAO,CAAC;AAW1C;;GAEG;AACH,MAAM,iBAAkB,SAAQ,KAAK;IACnC,KAAK,CAA6B;IAElC,YAAY,MAAM,GAAG,WAAW;QAC9B,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACrB,KAAK,EAAE,IAAI,EAAE,iDAAiD;SAC/D,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,wBAAwB;IAC3B,EAAE,CAAoB;IAE9B;;;;OAIG;IACH,YAAY,MAAe;QACzB,IAAI,CAAC,EAAE,GAAG,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,GAAG,CAAC,MAAc;QACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC/C,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QACD,kFAAkF;QAClF,MAAM,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,CAAC,MAAc,EAAE,IAA6B;QACvD,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;IACnD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,MAAc;QACzB,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,IAAI;QACR,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC,WAAW,EAAE,CAAa,CAAC;IACxE,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM;QACV,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAAe;IAEf,OAAO,IAAI,wBAAwB,CAAC,MAAM,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Dexie-based wallet storage provider for IndexedDB.
3
+ *
4
+ * This module provides a typed wallet storage implementation using Dexie,
5
+ * which is a wrapper around IndexedDB that provides a simpler API.
6
+ */
7
+ import type { WalletStorageProvider } from "../api.js";
8
+ /**
9
+ * Dexie-based wallet storage provider.
10
+ *
11
+ * Stores wallet data (mnemonic and key index) in IndexedDB using Dexie.
12
+ * This provides better security than localStorage since IndexedDB data
13
+ * is not accessible via JavaScript in the same way, and provides
14
+ * structured storage.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { DexieWalletStorageProvider, Client } from '@lendaswap/sdk';
19
+ *
20
+ * const walletStorage = new DexieWalletStorageProvider();
21
+ *
22
+ * // Use with the Client
23
+ * const client = await Client.create(
24
+ * 'https://api.lendaswap.com',
25
+ * walletStorage,
26
+ * swapStorage,
27
+ * 'bitcoin',
28
+ * 'https://arkade.computer'
29
+ * );
30
+ * ```
31
+ */
32
+ export declare class DexieWalletStorageProvider implements WalletStorageProvider {
33
+ private db;
34
+ private static readonly WALLET_ID;
35
+ /**
36
+ * Create a new DexieWalletStorageProvider.
37
+ *
38
+ * @param dbName - Optional database name (default: "lendaswap-wallet")
39
+ */
40
+ constructor(dbName?: string);
41
+ /**
42
+ * Get the mnemonic phrase from storage.
43
+ *
44
+ * @returns The mnemonic phrase, or null if not stored
45
+ */
46
+ getMnemonic(): Promise<string | null>;
47
+ /**
48
+ * Store the mnemonic phrase.
49
+ *
50
+ * @param mnemonic - The mnemonic phrase to store
51
+ */
52
+ setMnemonic(mnemonic: string): Promise<void>;
53
+ /**
54
+ * Get the current key derivation index.
55
+ *
56
+ * @returns The key index, or 0 if not set
57
+ */
58
+ getKeyIndex(): Promise<number>;
59
+ /**
60
+ * Set the key derivation index.
61
+ *
62
+ * @param index - The key index to store
63
+ */
64
+ setKeyIndex(index: number): Promise<void>;
65
+ /**
66
+ * Clear all wallet data.
67
+ */
68
+ clear(): Promise<void>;
69
+ /**
70
+ * Close the database connection.
71
+ */
72
+ close(): void;
73
+ }
74
+ /**
75
+ * Create a Dexie-based wallet storage provider.
76
+ *
77
+ * This is a convenience function for creating a DexieWalletStorageProvider.
78
+ *
79
+ * @param dbName - Optional database name (default: "lendaswap-wallet")
80
+ * @returns A new DexieWalletStorageProvider instance
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * import { createDexieWalletStorage, createDexieSwapStorage, Client } from '@lendaswap/sdk';
85
+ *
86
+ * const walletStorage = createDexieWalletStorage();
87
+ * const swapStorage = createDexieSwapStorage();
88
+ *
89
+ * const client = await Client.create(
90
+ * 'https://api.lendaswap.com',
91
+ * walletStorage,
92
+ * swapStorage,
93
+ * 'bitcoin',
94
+ * 'https://arkade.computer'
95
+ * );
96
+ * ```
97
+ */
98
+ export declare function createDexieWalletStorage(dbName?: string): DexieWalletStorageProvider;
99
+ //# sourceMappingURL=dexieWalletStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dexieWalletStorage.d.ts","sourceRoot":"","sources":["../../src/storage/dexieWalletStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AA4BvD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,0BAA2B,YAAW,qBAAqB;IACtE,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAEtD;;;;OAIG;gBACS,MAAM,CAAC,EAAE,MAAM;IAI3B;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAO3C;;;;OAIG;IACG,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWlD;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;IAOpC;;;;OAIG;IACG,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAW/C;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,CAAC,EAAE,MAAM,GACd,0BAA0B,CAE5B"}
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Dexie-based wallet storage provider for IndexedDB.
3
+ *
4
+ * This module provides a typed wallet storage implementation using Dexie,
5
+ * which is a wrapper around IndexedDB that provides a simpler API.
6
+ */
7
+ import Dexie from "dexie";
8
+ /**
9
+ * Dexie database for storing wallet data.
10
+ */
11
+ class WalletDatabase extends Dexie {
12
+ wallet;
13
+ constructor(dbName = "lendaswap-wallet") {
14
+ super(dbName);
15
+ this.version(1).stores({
16
+ wallet: "id", // Primary key only
17
+ });
18
+ }
19
+ }
20
+ /**
21
+ * Dexie-based wallet storage provider.
22
+ *
23
+ * Stores wallet data (mnemonic and key index) in IndexedDB using Dexie.
24
+ * This provides better security than localStorage since IndexedDB data
25
+ * is not accessible via JavaScript in the same way, and provides
26
+ * structured storage.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { DexieWalletStorageProvider, Client } from '@lendaswap/sdk';
31
+ *
32
+ * const walletStorage = new DexieWalletStorageProvider();
33
+ *
34
+ * // Use with the Client
35
+ * const client = await Client.create(
36
+ * 'https://api.lendaswap.com',
37
+ * walletStorage,
38
+ * swapStorage,
39
+ * 'bitcoin',
40
+ * 'https://arkade.computer'
41
+ * );
42
+ * ```
43
+ */
44
+ export class DexieWalletStorageProvider {
45
+ db;
46
+ static WALLET_ID = "wallet";
47
+ /**
48
+ * Create a new DexieWalletStorageProvider.
49
+ *
50
+ * @param dbName - Optional database name (default: "lendaswap-wallet")
51
+ */
52
+ constructor(dbName) {
53
+ this.db = new WalletDatabase(dbName);
54
+ }
55
+ /**
56
+ * Get the mnemonic phrase from storage.
57
+ *
58
+ * @returns The mnemonic phrase, or null if not stored
59
+ */
60
+ async getMnemonic() {
61
+ const record = await this.db.wallet.get(DexieWalletStorageProvider.WALLET_ID);
62
+ return record?.mnemonic ?? null;
63
+ }
64
+ /**
65
+ * Store the mnemonic phrase.
66
+ *
67
+ * @param mnemonic - The mnemonic phrase to store
68
+ */
69
+ async setMnemonic(mnemonic) {
70
+ const existing = await this.db.wallet.get(DexieWalletStorageProvider.WALLET_ID);
71
+ await this.db.wallet.put({
72
+ id: DexieWalletStorageProvider.WALLET_ID,
73
+ mnemonic,
74
+ keyIndex: existing?.keyIndex ?? 0,
75
+ });
76
+ }
77
+ /**
78
+ * Get the current key derivation index.
79
+ *
80
+ * @returns The key index, or 0 if not set
81
+ */
82
+ async getKeyIndex() {
83
+ const record = await this.db.wallet.get(DexieWalletStorageProvider.WALLET_ID);
84
+ return record?.keyIndex ?? 0;
85
+ }
86
+ /**
87
+ * Set the key derivation index.
88
+ *
89
+ * @param index - The key index to store
90
+ */
91
+ async setKeyIndex(index) {
92
+ const existing = await this.db.wallet.get(DexieWalletStorageProvider.WALLET_ID);
93
+ await this.db.wallet.put({
94
+ id: DexieWalletStorageProvider.WALLET_ID,
95
+ mnemonic: existing?.mnemonic ?? null,
96
+ keyIndex: index,
97
+ });
98
+ }
99
+ /**
100
+ * Clear all wallet data.
101
+ */
102
+ async clear() {
103
+ await this.db.wallet.clear();
104
+ }
105
+ /**
106
+ * Close the database connection.
107
+ */
108
+ close() {
109
+ this.db.close();
110
+ }
111
+ }
112
+ /**
113
+ * Create a Dexie-based wallet storage provider.
114
+ *
115
+ * This is a convenience function for creating a DexieWalletStorageProvider.
116
+ *
117
+ * @param dbName - Optional database name (default: "lendaswap-wallet")
118
+ * @returns A new DexieWalletStorageProvider instance
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import { createDexieWalletStorage, createDexieSwapStorage, Client } from '@lendaswap/sdk';
123
+ *
124
+ * const walletStorage = createDexieWalletStorage();
125
+ * const swapStorage = createDexieSwapStorage();
126
+ *
127
+ * const client = await Client.create(
128
+ * 'https://api.lendaswap.com',
129
+ * walletStorage,
130
+ * swapStorage,
131
+ * 'bitcoin',
132
+ * 'https://arkade.computer'
133
+ * );
134
+ * ```
135
+ */
136
+ export function createDexieWalletStorage(dbName) {
137
+ return new DexieWalletStorageProvider(dbName);
138
+ }
139
+ //# sourceMappingURL=dexieWalletStorage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dexieWalletStorage.js","sourceRoot":"","sources":["../../src/storage/dexieWalletStorage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAqB,MAAM,OAAO,CAAC;AAe1C;;GAEG;AACH,MAAM,cAAe,SAAQ,KAAK;IAChC,MAAM,CAA+B;IAErC,YAAY,MAAM,GAAG,kBAAkB;QACrC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACrB,MAAM,EAAE,IAAI,EAAE,mBAAmB;SAClC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,0BAA0B;IAC7B,EAAE,CAAiB;IACnB,MAAM,CAAU,SAAS,GAAG,QAAiB,CAAC;IAEtD;;;;OAIG;IACH,YAAY,MAAe;QACzB,IAAI,CAAC,EAAE,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,WAAW;QACf,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CACrC,0BAA0B,CAAC,SAAS,CACrC,CAAC;QACF,OAAO,MAAM,EAAE,QAAQ,IAAI,IAAI,CAAC;IAClC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,WAAW,CAAC,QAAgB;QAChC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CACvC,0BAA0B,CAAC,SAAS,CACrC,CAAC;QACF,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC;YACvB,EAAE,EAAE,0BAA0B,CAAC,SAAS;YACxC,QAAQ;YACR,QAAQ,EAAE,QAAQ,EAAE,QAAQ,IAAI,CAAC;SAClC,C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