@cashu/coco-expo-sqlite 1.0.0-rc.2 → 1.0.0-rc.4

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/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @cashu/coco-expo-sqlite
2
+
3
+ > ⚠️ Release candidate: Coco is stabilizing for v1, but breaking changes may
4
+ > still land before the final 1.0 release. Pin versions in production.
5
+
6
+ Expo SQLite storage adapter for Coco in React Native and Expo applications.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install @cashu/coco-core @cashu/coco-expo-sqlite
12
+ npx expo install expo-sqlite
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { initializeCoco } from '@cashu/coco-core';
19
+ import { ExpoSqliteRepositories } from '@cashu/coco-expo-sqlite';
20
+ import { openDatabaseAsync } from 'expo-sqlite';
21
+
22
+ const database = await openDatabaseAsync('coco.db');
23
+ const repositories = new ExpoSqliteRepositories({ database });
24
+ await repositories.init();
25
+
26
+ const manager = await initializeCoco({
27
+ repo: repositories,
28
+ seedGetter,
29
+ });
30
+ ```
31
+
32
+ ## Notes
33
+
34
+ - Pass an already opened `expo-sqlite` database instance via the `database` option.
35
+ - The adapter ensures schema creation and migrations when you call `init()`.
package/dist/index.cjs CHANGED
@@ -624,6 +624,18 @@ const MIGRATIONS = [
624
624
  id: "021_melt_finalized_data",
625
625
  sql: `
626
626
  ALTER TABLE coco_cashu_melt_operations ADD COLUMN finalizedDataJson TEXT;
627
+ `
628
+ },
629
+ {
630
+ id: "022_melt_operation_unit",
631
+ sql: `
632
+ ALTER TABLE coco_cashu_melt_operations ADD COLUMN unit TEXT;
633
+ `
634
+ },
635
+ {
636
+ id: "023_receive_operation_unit",
637
+ sql: `
638
+ ALTER TABLE coco_cashu_receive_operations ADD COLUMN unit TEXT NOT NULL DEFAULT 'sat';
627
639
  `
628
640
  }
629
641
  ];
@@ -1258,6 +1270,14 @@ var ExpoHistoryRepository = class {
1258
1270
  const entry = this.rowToEntry(row);
1259
1271
  return entry.type === "send" ? entry : null;
1260
1272
  }
1273
+ async getReceiveHistoryEntry(mintUrl, operationId) {
1274
+ const row = await this.db.get(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1275
+ FROM coco_cashu_history WHERE mintUrl = ? AND operationId = ? AND type = 'receive'
1276
+ ORDER BY createdAt DESC, id DESC LIMIT 1`, [mintUrl, operationId]);
1277
+ if (!row) return null;
1278
+ const entry = this.rowToEntry(row);
1279
+ return entry.type === "receive" ? entry : null;
1280
+ }
1261
1281
  async getPaginatedHistoryEntries(limit, offset) {
1262
1282
  return (await this.db.all(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1263
1283
  FROM coco_cashu_history
@@ -1289,10 +1309,12 @@ var ExpoHistoryRepository = class {
1289
1309
  quoteId = history.quoteId;
1290
1310
  state = history.state;
1291
1311
  paymentRequest = history.paymentRequest;
1312
+ operationId = history.operationId ?? null;
1292
1313
  break;
1293
1314
  case "melt":
1294
1315
  quoteId = history.quoteId;
1295
1316
  state = history.state;
1317
+ operationId = history.operationId ?? null;
1296
1318
  break;
1297
1319
  case "send":
1298
1320
  tokenJson = history.token ? JSON.stringify(history.token) : null;
@@ -1301,6 +1323,8 @@ var ExpoHistoryRepository = class {
1301
1323
  break;
1302
1324
  case "receive":
1303
1325
  tokenJson = history.token ? JSON.stringify(history.token) : null;
1326
+ operationId = history.operationId ?? null;
1327
+ state = history.state;
1304
1328
  break;
1305
1329
  }
1306
1330
  const id = (await this.db.run(`INSERT INTO coco_cashu_history (mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId)
@@ -1347,13 +1371,14 @@ var ExpoHistoryRepository = class {
1347
1371
  if (!history.quoteId) throw new Error("quoteId required for mint entry");
1348
1372
  state = history.state;
1349
1373
  paymentRequest = history.paymentRequest;
1350
- await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, paymentRequest = ?, metadata = ?
1374
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, paymentRequest = ?, metadata = ?, operationId = ?
1351
1375
  WHERE mintUrl = ? AND quoteId = ? AND type = 'mint'`, [
1352
1376
  history.unit,
1353
1377
  history.amount,
1354
1378
  state,
1355
1379
  paymentRequest,
1356
1380
  history.metadata ? JSON.stringify(history.metadata) : null,
1381
+ history.operationId ?? null,
1357
1382
  history.mintUrl,
1358
1383
  history.quoteId
1359
1384
  ]);
@@ -1365,12 +1390,13 @@ var ExpoHistoryRepository = class {
1365
1390
  } else if (history.type === "melt") {
1366
1391
  if (!history.quoteId) throw new Error("quoteId required for melt entry");
1367
1392
  state = history.state;
1368
- await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, metadata = ?
1393
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, metadata = ?, operationId = ?
1369
1394
  WHERE mintUrl = ? AND quoteId = ? AND type = 'melt'`, [
1370
1395
  history.unit,
1371
1396
  history.amount,
1372
1397
  state,
1373
1398
  history.metadata ? JSON.stringify(history.metadata) : null,
1399
+ history.operationId ?? null,
1374
1400
  history.mintUrl,
1375
1401
  history.quoteId
1376
1402
  ]);
@@ -1398,7 +1424,26 @@ var ExpoHistoryRepository = class {
1398
1424
  ORDER BY createdAt DESC, id DESC LIMIT 1`, [history.mintUrl, history.operationId]);
1399
1425
  if (!row) throw new Error("Updated history entry not found");
1400
1426
  return this.rowToEntry(row);
1401
- } else throw new Error("updateHistoryEntry does not support receive entries");
1427
+ } else if (history.type === "receive") {
1428
+ if (!history.operationId) throw new Error("operationId required for receive entry");
1429
+ state = history.state;
1430
+ tokenJson = history.token ? JSON.stringify(history.token) : null;
1431
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, tokenJson = ?, metadata = ?
1432
+ WHERE mintUrl = ? AND operationId = ? AND type = 'receive'`, [
1433
+ history.unit,
1434
+ history.amount,
1435
+ state,
1436
+ tokenJson,
1437
+ history.metadata ? JSON.stringify(history.metadata) : null,
1438
+ history.mintUrl,
1439
+ history.operationId
1440
+ ]);
1441
+ const row = await this.db.get(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1442
+ FROM coco_cashu_history WHERE mintUrl = ? AND operationId = ? AND type = 'receive'
1443
+ ORDER BY createdAt DESC, id DESC LIMIT 1`, [history.mintUrl, history.operationId]);
1444
+ if (!row) throw new Error("Updated history entry not found");
1445
+ return this.rowToEntry(row);
1446
+ } else throw new Error(`Unsupported history entry type: ${String(history.type)}`);
1402
1447
  }
1403
1448
  async updateSendHistoryState(mintUrl, operationId, state) {
1404
1449
  await this.db.run(`UPDATE coco_cashu_history SET state = ?
@@ -1408,6 +1453,14 @@ var ExpoHistoryRepository = class {
1408
1453
  operationId
1409
1454
  ]);
1410
1455
  }
1456
+ async updateReceiveHistoryState(mintUrl, operationId, state) {
1457
+ await this.db.run(`UPDATE coco_cashu_history SET state = ?
1458
+ WHERE mintUrl = ? AND operationId = ? AND type = 'receive'`, [
1459
+ state,
1460
+ mintUrl,
1461
+ operationId
1462
+ ]);
1463
+ }
1411
1464
  async deleteHistoryEntry(mintUrl, quoteId) {
1412
1465
  await this.db.run("DELETE FROM coco_cashu_history WHERE mintUrl = ? AND quoteId = ?", [mintUrl, quoteId]);
1413
1466
  }
@@ -1430,6 +1483,7 @@ var ExpoHistoryRepository = class {
1430
1483
  type: "mint",
1431
1484
  paymentRequest: row.paymentRequest ?? "",
1432
1485
  quoteId: row.quoteId ?? "",
1486
+ operationId: row.operationId ?? void 0,
1433
1487
  state: row.state ?? "UNPAID",
1434
1488
  amount: row.amount
1435
1489
  };
@@ -1437,6 +1491,7 @@ var ExpoHistoryRepository = class {
1437
1491
  ...base,
1438
1492
  type: "melt",
1439
1493
  quoteId: row.quoteId ?? "",
1494
+ operationId: row.operationId ?? void 0,
1440
1495
  state: row.state ?? "UNPAID",
1441
1496
  amount: row.amount
1442
1497
  };
@@ -1453,6 +1508,8 @@ var ExpoHistoryRepository = class {
1453
1508
  ...base,
1454
1509
  type: "receive",
1455
1510
  amount: row.amount,
1511
+ operationId: row.operationId ?? void 0,
1512
+ state: row.state ?? "finalized",
1456
1513
  token
1457
1514
  };
1458
1515
  }
@@ -1648,6 +1705,7 @@ const rowToOperation$2 = (row) => {
1648
1705
  };
1649
1706
  const preparedData = {
1650
1707
  quoteId: row.quoteId ?? "",
1708
+ unit: row.unit ?? "sat",
1651
1709
  amount: row.amount ?? 0,
1652
1710
  fee_reserve: row.fee_reserve ?? 0,
1653
1711
  swap_fee: row.swap_fee ?? 0,
@@ -1697,6 +1755,7 @@ const operationToParams$2 = (operation) => {
1697
1755
  null,
1698
1756
  null,
1699
1757
  null,
1758
+ null,
1700
1759
  null
1701
1760
  ];
1702
1761
  const settlement = operation;
@@ -1713,6 +1772,7 @@ const operationToParams$2 = (operation) => {
1713
1772
  operation.method,
1714
1773
  methodDataJson,
1715
1774
  operation.quoteId,
1775
+ operation.unit,
1716
1776
  operation.amount,
1717
1777
  operation.fee_reserve,
1718
1778
  operation.swap_fee,
@@ -1736,8 +1796,8 @@ var ExpoMeltOperationRepository = class {
1736
1796
  if (await this.db.get("SELECT id FROM coco_cashu_melt_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`MeltOperation with id ${operation.id} already exists`);
1737
1797
  const params = operationToParams$2(operation);
1738
1798
  await this.db.run(`INSERT INTO coco_cashu_melt_operations
1739
- (id, mintUrl, state, createdAt, updatedAt, error, method, methodDataJson, quoteId, amount, fee_reserve, swap_fee, needsSwap, inputAmount, inputProofSecretsJson, changeOutputDataJson, swapOutputDataJson, changeAmount, effectiveFee, finalizedDataJson)
1740
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
1799
+ (id, mintUrl, state, createdAt, updatedAt, error, method, methodDataJson, quoteId, unit, amount, fee_reserve, swap_fee, needsSwap, inputAmount, inputProofSecretsJson, changeOutputDataJson, swapOutputDataJson, changeAmount, effectiveFee, finalizedDataJson)
1800
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
1741
1801
  }
1742
1802
  async update(operation) {
1743
1803
  if (operation.state === "failed") throw new Error("Cannot persist failed melt operation");
@@ -1758,7 +1818,7 @@ var ExpoMeltOperationRepository = class {
1758
1818
  }
1759
1819
  const settlement = operation;
1760
1820
  await this.db.run(`UPDATE coco_cashu_melt_operations
1761
- SET state = ?, updatedAt = ?, error = ?, method = ?, methodDataJson = ?, quoteId = ?, amount = ?, fee_reserve = ?, swap_fee = ?, needsSwap = ?, inputAmount = ?, inputProofSecretsJson = ?, changeOutputDataJson = ?, swapOutputDataJson = ?, changeAmount = ?, effectiveFee = ?, finalizedDataJson = ?
1821
+ SET state = ?, updatedAt = ?, error = ?, method = ?, methodDataJson = ?, quoteId = ?, unit = ?, amount = ?, fee_reserve = ?, swap_fee = ?, needsSwap = ?, inputAmount = ?, inputProofSecretsJson = ?, changeOutputDataJson = ?, swapOutputDataJson = ?, changeAmount = ?, effectiveFee = ?, finalizedDataJson = ?
1762
1822
  WHERE id = ?`, [
1763
1823
  operation.state,
1764
1824
  updatedAtSeconds,
@@ -1766,6 +1826,7 @@ var ExpoMeltOperationRepository = class {
1766
1826
  operation.method,
1767
1827
  JSON.stringify(operation.methodData),
1768
1828
  operation.quoteId,
1829
+ operation.unit,
1769
1830
  operation.amount,
1770
1831
  operation.fee_reserve,
1771
1832
  operation.swap_fee,
@@ -2019,10 +2080,14 @@ var ExpoMintOperationRepository = class {
2019
2080
 
2020
2081
  //#endregion
2021
2082
  //#region src/repositories/ReceiveOperationRepository.ts
2083
+ function getOperationUnit(op) {
2084
+ return op.unit ?? "sat";
2085
+ }
2022
2086
  function rowToOperation(row) {
2023
2087
  const base = {
2024
2088
  id: row.id,
2025
2089
  mintUrl: row.mintUrl,
2090
+ unit: row.unit ?? "sat",
2026
2091
  amount: row.amount,
2027
2092
  inputProofs: row.inputProofsJson ? JSON.parse(row.inputProofsJson) : [],
2028
2093
  createdAt: row.createdAt * 1e3,
@@ -2067,6 +2132,7 @@ function operationToParams(op) {
2067
2132
  if (op.state === "init") return [
2068
2133
  op.id,
2069
2134
  op.mintUrl,
2135
+ getOperationUnit(op),
2070
2136
  op.amount,
2071
2137
  op.state,
2072
2138
  createdAtSeconds,
@@ -2079,6 +2145,7 @@ function operationToParams(op) {
2079
2145
  return [
2080
2146
  op.id,
2081
2147
  op.mintUrl,
2148
+ getOperationUnit(op),
2082
2149
  op.amount,
2083
2150
  op.state,
2084
2151
  createdAtSeconds,
@@ -2098,27 +2165,29 @@ var ExpoReceiveOperationRepository = class {
2098
2165
  if (await this.db.get("SELECT id FROM coco_cashu_receive_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`ReceiveOperation with id ${operation.id} already exists`);
2099
2166
  const params = operationToParams(operation);
2100
2167
  await this.db.run(`INSERT INTO coco_cashu_receive_operations
2101
- (id, mintUrl, amount, state, createdAt, updatedAt, error, fee, inputProofsJson, outputDataJson)
2102
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
2168
+ (id, mintUrl, unit, amount, state, createdAt, updatedAt, error, fee, inputProofsJson, outputDataJson)
2169
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
2103
2170
  }
2104
2171
  async update(operation) {
2105
2172
  if (!await this.db.get("SELECT id FROM coco_cashu_receive_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`ReceiveOperation with id ${operation.id} not found`);
2106
2173
  const updatedAtSeconds = getUnixTimeSeconds();
2107
2174
  if (operation.state === "init") await this.db.run(`UPDATE coco_cashu_receive_operations
2108
- SET state = ?, updatedAt = ?, error = ?, inputProofsJson = ?
2175
+ SET state = ?, updatedAt = ?, error = ?, unit = ?, inputProofsJson = ?
2109
2176
  WHERE id = ?`, [
2110
2177
  operation.state,
2111
2178
  updatedAtSeconds,
2112
2179
  operation.error ?? null,
2180
+ getOperationUnit(operation),
2113
2181
  JSON.stringify(operation.inputProofs),
2114
2182
  operation.id
2115
2183
  ]);
2116
2184
  else await this.db.run(`UPDATE coco_cashu_receive_operations
2117
- SET state = ?, updatedAt = ?, error = ?, fee = ?, inputProofsJson = ?, outputDataJson = ?
2185
+ SET state = ?, updatedAt = ?, error = ?, unit = ?, fee = ?, inputProofsJson = ?, outputDataJson = ?
2118
2186
  WHERE id = ?`, [
2119
2187
  operation.state,
2120
2188
  updatedAtSeconds,
2121
2189
  operation.error ?? null,
2190
+ getOperationUnit(operation),
2122
2191
  operation.fee,
2123
2192
  JSON.stringify(operation.inputProofs),
2124
2193
  operation.outputData ? JSON.stringify(operation.outputData) : null,
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthSession, AuthSessionRepository, CoreProof, Counter, CounterRepository, HistoryEntry, KeyRingRepository, Keypair, Keyset, KeysetRepository, MeltHistoryEntry, MeltOperationRepository, MeltQuote, MeltQuoteRepository, Mint, MintHistoryEntry, MintOperationRepository, MintQuote, MintQuoteRepository, MintRepository, ProofRepository, ProofState, ReceiveHistoryEntry, ReceiveOperation, ReceiveOperationRepository, ReceiveOperationState, Repositories, RepositoryTransactionScope, SendHistoryEntry, SendOperation, SendOperationRepository, SendOperationState } from "@cashu/coco-core";
1
+ import { AuthSession, AuthSessionRepository, CoreProof, Counter, CounterRepository, HistoryEntry, KeyRingRepository, Keypair, Keyset, KeysetRepository, MeltHistoryEntry, MeltOperationRepository, MeltQuote, MeltQuoteRepository, Mint, MintHistoryEntry, MintOperationRepository, MintQuote, MintQuoteRepository, MintRepository, ProofRepository, ProofState, ReceiveHistoryEntry, ReceiveHistoryState, ReceiveOperation, ReceiveOperationRepository, ReceiveOperationState, Repositories, RepositoryTransactionScope, SendHistoryEntry, SendOperation, SendOperationRepository, SendOperationState } from "@cashu/coco-core";
2
2
  import { SQLiteDatabase } from "expo-sqlite";
3
3
 
4
4
  //#region src/db.d.ts
@@ -179,13 +179,14 @@ type MintQuoteState = MintHistoryEntry['state'];
179
179
  type MeltQuoteState = MeltHistoryEntry['state'];
180
180
  type SendHistoryState = SendHistoryEntry['state'];
181
181
  type NewHistoryEntry = Omit<MintHistoryEntry, 'id'> | Omit<MeltHistoryEntry, 'id'> | Omit<SendHistoryEntry, 'id'> | Omit<ReceiveHistoryEntry, 'id'>;
182
- type UpdatableHistoryEntry = Omit<MintHistoryEntry, 'id' | 'createdAt'> | Omit<MeltHistoryEntry, 'id' | 'createdAt'> | Omit<SendHistoryEntry, 'id' | 'createdAt'>;
182
+ type UpdatableHistoryEntry = Omit<MintHistoryEntry, 'id' | 'createdAt'> | Omit<MeltHistoryEntry, 'id' | 'createdAt'> | Omit<SendHistoryEntry, 'id' | 'createdAt'> | Omit<ReceiveHistoryEntry, 'id' | 'createdAt'>;
183
183
  declare class ExpoHistoryRepository {
184
184
  private readonly db;
185
185
  constructor(db: ExpoSqliteDb);
186
186
  getMintHistoryEntry(mintUrl: string, quoteId: string): Promise<MintHistoryEntry | null>;
187
187
  getMeltHistoryEntry(mintUrl: string, quoteId: string): Promise<MeltHistoryEntry | null>;
188
188
  getSendHistoryEntry(mintUrl: string, operationId: string): Promise<SendHistoryEntry | null>;
189
+ getReceiveHistoryEntry(mintUrl: string, operationId: string): Promise<ReceiveHistoryEntry | null>;
189
190
  getPaginatedHistoryEntries(limit: number, offset: number): Promise<HistoryEntry[]>;
190
191
  getHistoryEntryById(id: string): Promise<HistoryEntry | null>;
191
192
  addHistoryEntry(history: NewHistoryEntry): Promise<HistoryEntry>;
@@ -193,6 +194,7 @@ declare class ExpoHistoryRepository {
193
194
  updateHistoryMeltEntry(mintUrl: string, quoteId: string, state: MeltQuoteState): Promise<HistoryEntry>;
194
195
  updateHistoryEntry(history: UpdatableHistoryEntry): Promise<HistoryEntry>;
195
196
  updateSendHistoryState(mintUrl: string, operationId: string, state: SendHistoryState): Promise<void>;
197
+ updateReceiveHistoryState(mintUrl: string, operationId: string, state: ReceiveHistoryState): Promise<void>;
196
198
  deleteHistoryEntry(mintUrl: string, quoteId: string): Promise<void>;
197
199
  private getById;
198
200
  private rowToEntry;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthSession, AuthSessionRepository, CoreProof, Counter, CounterRepository, HistoryEntry, KeyRingRepository, Keypair, Keyset, KeysetRepository, MeltHistoryEntry, MeltOperationRepository, MeltQuote, MeltQuoteRepository, Mint, MintHistoryEntry, MintOperationRepository, MintQuote, MintQuoteRepository, MintRepository, ProofRepository, ProofState, ReceiveHistoryEntry, ReceiveOperation, ReceiveOperationRepository, ReceiveOperationState, Repositories, RepositoryTransactionScope, SendHistoryEntry, SendOperation, SendOperationRepository, SendOperationState } from "@cashu/coco-core";
1
+ import { AuthSession, AuthSessionRepository, CoreProof, Counter, CounterRepository, HistoryEntry, KeyRingRepository, Keypair, Keyset, KeysetRepository, MeltHistoryEntry, MeltOperationRepository, MeltQuote, MeltQuoteRepository, Mint, MintHistoryEntry, MintOperationRepository, MintQuote, MintQuoteRepository, MintRepository, ProofRepository, ProofState, ReceiveHistoryEntry, ReceiveHistoryState, ReceiveOperation, ReceiveOperationRepository, ReceiveOperationState, Repositories, RepositoryTransactionScope, SendHistoryEntry, SendOperation, SendOperationRepository, SendOperationState } from "@cashu/coco-core";
2
2
  import { SQLiteDatabase } from "expo-sqlite";
3
3
 
4
4
  //#region src/db.d.ts
@@ -179,13 +179,14 @@ type MintQuoteState = MintHistoryEntry['state'];
179
179
  type MeltQuoteState = MeltHistoryEntry['state'];
180
180
  type SendHistoryState = SendHistoryEntry['state'];
181
181
  type NewHistoryEntry = Omit<MintHistoryEntry, 'id'> | Omit<MeltHistoryEntry, 'id'> | Omit<SendHistoryEntry, 'id'> | Omit<ReceiveHistoryEntry, 'id'>;
182
- type UpdatableHistoryEntry = Omit<MintHistoryEntry, 'id' | 'createdAt'> | Omit<MeltHistoryEntry, 'id' | 'createdAt'> | Omit<SendHistoryEntry, 'id' | 'createdAt'>;
182
+ type UpdatableHistoryEntry = Omit<MintHistoryEntry, 'id' | 'createdAt'> | Omit<MeltHistoryEntry, 'id' | 'createdAt'> | Omit<SendHistoryEntry, 'id' | 'createdAt'> | Omit<ReceiveHistoryEntry, 'id' | 'createdAt'>;
183
183
  declare class ExpoHistoryRepository {
184
184
  private readonly db;
185
185
  constructor(db: ExpoSqliteDb);
186
186
  getMintHistoryEntry(mintUrl: string, quoteId: string): Promise<MintHistoryEntry | null>;
187
187
  getMeltHistoryEntry(mintUrl: string, quoteId: string): Promise<MeltHistoryEntry | null>;
188
188
  getSendHistoryEntry(mintUrl: string, operationId: string): Promise<SendHistoryEntry | null>;
189
+ getReceiveHistoryEntry(mintUrl: string, operationId: string): Promise<ReceiveHistoryEntry | null>;
189
190
  getPaginatedHistoryEntries(limit: number, offset: number): Promise<HistoryEntry[]>;
190
191
  getHistoryEntryById(id: string): Promise<HistoryEntry | null>;
191
192
  addHistoryEntry(history: NewHistoryEntry): Promise<HistoryEntry>;
@@ -193,6 +194,7 @@ declare class ExpoHistoryRepository {
193
194
  updateHistoryMeltEntry(mintUrl: string, quoteId: string, state: MeltQuoteState): Promise<HistoryEntry>;
194
195
  updateHistoryEntry(history: UpdatableHistoryEntry): Promise<HistoryEntry>;
195
196
  updateSendHistoryState(mintUrl: string, operationId: string, state: SendHistoryState): Promise<void>;
197
+ updateReceiveHistoryState(mintUrl: string, operationId: string, state: ReceiveHistoryState): Promise<void>;
196
198
  deleteHistoryEntry(mintUrl: string, quoteId: string): Promise<void>;
197
199
  private getById;
198
200
  private rowToEntry;
package/dist/index.js CHANGED
@@ -624,6 +624,18 @@ const MIGRATIONS = [
624
624
  id: "021_melt_finalized_data",
625
625
  sql: `
626
626
  ALTER TABLE coco_cashu_melt_operations ADD COLUMN finalizedDataJson TEXT;
627
+ `
628
+ },
629
+ {
630
+ id: "022_melt_operation_unit",
631
+ sql: `
632
+ ALTER TABLE coco_cashu_melt_operations ADD COLUMN unit TEXT;
633
+ `
634
+ },
635
+ {
636
+ id: "023_receive_operation_unit",
637
+ sql: `
638
+ ALTER TABLE coco_cashu_receive_operations ADD COLUMN unit TEXT NOT NULL DEFAULT 'sat';
627
639
  `
628
640
  }
629
641
  ];
@@ -1258,6 +1270,14 @@ var ExpoHistoryRepository = class {
1258
1270
  const entry = this.rowToEntry(row);
1259
1271
  return entry.type === "send" ? entry : null;
1260
1272
  }
1273
+ async getReceiveHistoryEntry(mintUrl, operationId) {
1274
+ const row = await this.db.get(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1275
+ FROM coco_cashu_history WHERE mintUrl = ? AND operationId = ? AND type = 'receive'
1276
+ ORDER BY createdAt DESC, id DESC LIMIT 1`, [mintUrl, operationId]);
1277
+ if (!row) return null;
1278
+ const entry = this.rowToEntry(row);
1279
+ return entry.type === "receive" ? entry : null;
1280
+ }
1261
1281
  async getPaginatedHistoryEntries(limit, offset) {
1262
1282
  return (await this.db.all(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1263
1283
  FROM coco_cashu_history
@@ -1289,10 +1309,12 @@ var ExpoHistoryRepository = class {
1289
1309
  quoteId = history.quoteId;
1290
1310
  state = history.state;
1291
1311
  paymentRequest = history.paymentRequest;
1312
+ operationId = history.operationId ?? null;
1292
1313
  break;
1293
1314
  case "melt":
1294
1315
  quoteId = history.quoteId;
1295
1316
  state = history.state;
1317
+ operationId = history.operationId ?? null;
1296
1318
  break;
1297
1319
  case "send":
1298
1320
  tokenJson = history.token ? JSON.stringify(history.token) : null;
@@ -1301,6 +1323,8 @@ var ExpoHistoryRepository = class {
1301
1323
  break;
1302
1324
  case "receive":
1303
1325
  tokenJson = history.token ? JSON.stringify(history.token) : null;
1326
+ operationId = history.operationId ?? null;
1327
+ state = history.state;
1304
1328
  break;
1305
1329
  }
1306
1330
  const id = (await this.db.run(`INSERT INTO coco_cashu_history (mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId)
@@ -1347,13 +1371,14 @@ var ExpoHistoryRepository = class {
1347
1371
  if (!history.quoteId) throw new Error("quoteId required for mint entry");
1348
1372
  state = history.state;
1349
1373
  paymentRequest = history.paymentRequest;
1350
- await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, paymentRequest = ?, metadata = ?
1374
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, paymentRequest = ?, metadata = ?, operationId = ?
1351
1375
  WHERE mintUrl = ? AND quoteId = ? AND type = 'mint'`, [
1352
1376
  history.unit,
1353
1377
  history.amount,
1354
1378
  state,
1355
1379
  paymentRequest,
1356
1380
  history.metadata ? JSON.stringify(history.metadata) : null,
1381
+ history.operationId ?? null,
1357
1382
  history.mintUrl,
1358
1383
  history.quoteId
1359
1384
  ]);
@@ -1365,12 +1390,13 @@ var ExpoHistoryRepository = class {
1365
1390
  } else if (history.type === "melt") {
1366
1391
  if (!history.quoteId) throw new Error("quoteId required for melt entry");
1367
1392
  state = history.state;
1368
- await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, metadata = ?
1393
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, metadata = ?, operationId = ?
1369
1394
  WHERE mintUrl = ? AND quoteId = ? AND type = 'melt'`, [
1370
1395
  history.unit,
1371
1396
  history.amount,
1372
1397
  state,
1373
1398
  history.metadata ? JSON.stringify(history.metadata) : null,
1399
+ history.operationId ?? null,
1374
1400
  history.mintUrl,
1375
1401
  history.quoteId
1376
1402
  ]);
@@ -1398,7 +1424,26 @@ var ExpoHistoryRepository = class {
1398
1424
  ORDER BY createdAt DESC, id DESC LIMIT 1`, [history.mintUrl, history.operationId]);
1399
1425
  if (!row) throw new Error("Updated history entry not found");
1400
1426
  return this.rowToEntry(row);
1401
- } else throw new Error("updateHistoryEntry does not support receive entries");
1427
+ } else if (history.type === "receive") {
1428
+ if (!history.operationId) throw new Error("operationId required for receive entry");
1429
+ state = history.state;
1430
+ tokenJson = history.token ? JSON.stringify(history.token) : null;
1431
+ await this.db.run(`UPDATE coco_cashu_history SET unit = ?, amount = ?, state = ?, tokenJson = ?, metadata = ?
1432
+ WHERE mintUrl = ? AND operationId = ? AND type = 'receive'`, [
1433
+ history.unit,
1434
+ history.amount,
1435
+ state,
1436
+ tokenJson,
1437
+ history.metadata ? JSON.stringify(history.metadata) : null,
1438
+ history.mintUrl,
1439
+ history.operationId
1440
+ ]);
1441
+ const row = await this.db.get(`SELECT id, mintUrl, type, unit, amount, createdAt, quoteId, state, paymentRequest, tokenJson, metadata, operationId
1442
+ FROM coco_cashu_history WHERE mintUrl = ? AND operationId = ? AND type = 'receive'
1443
+ ORDER BY createdAt DESC, id DESC LIMIT 1`, [history.mintUrl, history.operationId]);
1444
+ if (!row) throw new Error("Updated history entry not found");
1445
+ return this.rowToEntry(row);
1446
+ } else throw new Error(`Unsupported history entry type: ${String(history.type)}`);
1402
1447
  }
1403
1448
  async updateSendHistoryState(mintUrl, operationId, state) {
1404
1449
  await this.db.run(`UPDATE coco_cashu_history SET state = ?
@@ -1408,6 +1453,14 @@ var ExpoHistoryRepository = class {
1408
1453
  operationId
1409
1454
  ]);
1410
1455
  }
1456
+ async updateReceiveHistoryState(mintUrl, operationId, state) {
1457
+ await this.db.run(`UPDATE coco_cashu_history SET state = ?
1458
+ WHERE mintUrl = ? AND operationId = ? AND type = 'receive'`, [
1459
+ state,
1460
+ mintUrl,
1461
+ operationId
1462
+ ]);
1463
+ }
1411
1464
  async deleteHistoryEntry(mintUrl, quoteId) {
1412
1465
  await this.db.run("DELETE FROM coco_cashu_history WHERE mintUrl = ? AND quoteId = ?", [mintUrl, quoteId]);
1413
1466
  }
@@ -1430,6 +1483,7 @@ var ExpoHistoryRepository = class {
1430
1483
  type: "mint",
1431
1484
  paymentRequest: row.paymentRequest ?? "",
1432
1485
  quoteId: row.quoteId ?? "",
1486
+ operationId: row.operationId ?? void 0,
1433
1487
  state: row.state ?? "UNPAID",
1434
1488
  amount: row.amount
1435
1489
  };
@@ -1437,6 +1491,7 @@ var ExpoHistoryRepository = class {
1437
1491
  ...base,
1438
1492
  type: "melt",
1439
1493
  quoteId: row.quoteId ?? "",
1494
+ operationId: row.operationId ?? void 0,
1440
1495
  state: row.state ?? "UNPAID",
1441
1496
  amount: row.amount
1442
1497
  };
@@ -1453,6 +1508,8 @@ var ExpoHistoryRepository = class {
1453
1508
  ...base,
1454
1509
  type: "receive",
1455
1510
  amount: row.amount,
1511
+ operationId: row.operationId ?? void 0,
1512
+ state: row.state ?? "finalized",
1456
1513
  token
1457
1514
  };
1458
1515
  }
@@ -1648,6 +1705,7 @@ const rowToOperation$2 = (row) => {
1648
1705
  };
1649
1706
  const preparedData = {
1650
1707
  quoteId: row.quoteId ?? "",
1708
+ unit: row.unit ?? "sat",
1651
1709
  amount: row.amount ?? 0,
1652
1710
  fee_reserve: row.fee_reserve ?? 0,
1653
1711
  swap_fee: row.swap_fee ?? 0,
@@ -1697,6 +1755,7 @@ const operationToParams$2 = (operation) => {
1697
1755
  null,
1698
1756
  null,
1699
1757
  null,
1758
+ null,
1700
1759
  null
1701
1760
  ];
1702
1761
  const settlement = operation;
@@ -1713,6 +1772,7 @@ const operationToParams$2 = (operation) => {
1713
1772
  operation.method,
1714
1773
  methodDataJson,
1715
1774
  operation.quoteId,
1775
+ operation.unit,
1716
1776
  operation.amount,
1717
1777
  operation.fee_reserve,
1718
1778
  operation.swap_fee,
@@ -1736,8 +1796,8 @@ var ExpoMeltOperationRepository = class {
1736
1796
  if (await this.db.get("SELECT id FROM coco_cashu_melt_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`MeltOperation with id ${operation.id} already exists`);
1737
1797
  const params = operationToParams$2(operation);
1738
1798
  await this.db.run(`INSERT INTO coco_cashu_melt_operations
1739
- (id, mintUrl, state, createdAt, updatedAt, error, method, methodDataJson, quoteId, amount, fee_reserve, swap_fee, needsSwap, inputAmount, inputProofSecretsJson, changeOutputDataJson, swapOutputDataJson, changeAmount, effectiveFee, finalizedDataJson)
1740
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
1799
+ (id, mintUrl, state, createdAt, updatedAt, error, method, methodDataJson, quoteId, unit, amount, fee_reserve, swap_fee, needsSwap, inputAmount, inputProofSecretsJson, changeOutputDataJson, swapOutputDataJson, changeAmount, effectiveFee, finalizedDataJson)
1800
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
1741
1801
  }
1742
1802
  async update(operation) {
1743
1803
  if (operation.state === "failed") throw new Error("Cannot persist failed melt operation");
@@ -1758,7 +1818,7 @@ var ExpoMeltOperationRepository = class {
1758
1818
  }
1759
1819
  const settlement = operation;
1760
1820
  await this.db.run(`UPDATE coco_cashu_melt_operations
1761
- SET state = ?, updatedAt = ?, error = ?, method = ?, methodDataJson = ?, quoteId = ?, amount = ?, fee_reserve = ?, swap_fee = ?, needsSwap = ?, inputAmount = ?, inputProofSecretsJson = ?, changeOutputDataJson = ?, swapOutputDataJson = ?, changeAmount = ?, effectiveFee = ?, finalizedDataJson = ?
1821
+ SET state = ?, updatedAt = ?, error = ?, method = ?, methodDataJson = ?, quoteId = ?, unit = ?, amount = ?, fee_reserve = ?, swap_fee = ?, needsSwap = ?, inputAmount = ?, inputProofSecretsJson = ?, changeOutputDataJson = ?, swapOutputDataJson = ?, changeAmount = ?, effectiveFee = ?, finalizedDataJson = ?
1762
1822
  WHERE id = ?`, [
1763
1823
  operation.state,
1764
1824
  updatedAtSeconds,
@@ -1766,6 +1826,7 @@ var ExpoMeltOperationRepository = class {
1766
1826
  operation.method,
1767
1827
  JSON.stringify(operation.methodData),
1768
1828
  operation.quoteId,
1829
+ operation.unit,
1769
1830
  operation.amount,
1770
1831
  operation.fee_reserve,
1771
1832
  operation.swap_fee,
@@ -2019,10 +2080,14 @@ var ExpoMintOperationRepository = class {
2019
2080
 
2020
2081
  //#endregion
2021
2082
  //#region src/repositories/ReceiveOperationRepository.ts
2083
+ function getOperationUnit(op) {
2084
+ return op.unit ?? "sat";
2085
+ }
2022
2086
  function rowToOperation(row) {
2023
2087
  const base = {
2024
2088
  id: row.id,
2025
2089
  mintUrl: row.mintUrl,
2090
+ unit: row.unit ?? "sat",
2026
2091
  amount: row.amount,
2027
2092
  inputProofs: row.inputProofsJson ? JSON.parse(row.inputProofsJson) : [],
2028
2093
  createdAt: row.createdAt * 1e3,
@@ -2067,6 +2132,7 @@ function operationToParams(op) {
2067
2132
  if (op.state === "init") return [
2068
2133
  op.id,
2069
2134
  op.mintUrl,
2135
+ getOperationUnit(op),
2070
2136
  op.amount,
2071
2137
  op.state,
2072
2138
  createdAtSeconds,
@@ -2079,6 +2145,7 @@ function operationToParams(op) {
2079
2145
  return [
2080
2146
  op.id,
2081
2147
  op.mintUrl,
2148
+ getOperationUnit(op),
2082
2149
  op.amount,
2083
2150
  op.state,
2084
2151
  createdAtSeconds,
@@ -2098,27 +2165,29 @@ var ExpoReceiveOperationRepository = class {
2098
2165
  if (await this.db.get("SELECT id FROM coco_cashu_receive_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`ReceiveOperation with id ${operation.id} already exists`);
2099
2166
  const params = operationToParams(operation);
2100
2167
  await this.db.run(`INSERT INTO coco_cashu_receive_operations
2101
- (id, mintUrl, amount, state, createdAt, updatedAt, error, fee, inputProofsJson, outputDataJson)
2102
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
2168
+ (id, mintUrl, unit, amount, state, createdAt, updatedAt, error, fee, inputProofsJson, outputDataJson)
2169
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, params);
2103
2170
  }
2104
2171
  async update(operation) {
2105
2172
  if (!await this.db.get("SELECT id FROM coco_cashu_receive_operations WHERE id = ? LIMIT 1", [operation.id])) throw new Error(`ReceiveOperation with id ${operation.id} not found`);
2106
2173
  const updatedAtSeconds = getUnixTimeSeconds();
2107
2174
  if (operation.state === "init") await this.db.run(`UPDATE coco_cashu_receive_operations
2108
- SET state = ?, updatedAt = ?, error = ?, inputProofsJson = ?
2175
+ SET state = ?, updatedAt = ?, error = ?, unit = ?, inputProofsJson = ?
2109
2176
  WHERE id = ?`, [
2110
2177
  operation.state,
2111
2178
  updatedAtSeconds,
2112
2179
  operation.error ?? null,
2180
+ getOperationUnit(operation),
2113
2181
  JSON.stringify(operation.inputProofs),
2114
2182
  operation.id
2115
2183
  ]);
2116
2184
  else await this.db.run(`UPDATE coco_cashu_receive_operations
2117
- SET state = ?, updatedAt = ?, error = ?, fee = ?, inputProofsJson = ?, outputDataJson = ?
2185
+ SET state = ?, updatedAt = ?, error = ?, unit = ?, fee = ?, inputProofsJson = ?, outputDataJson = ?
2118
2186
  WHERE id = ?`, [
2119
2187
  operation.state,
2120
2188
  updatedAtSeconds,
2121
2189
  operation.error ?? null,
2190
+ getOperationUnit(operation),
2122
2191
  operation.fee,
2123
2192
  JSON.stringify(operation.inputProofs),
2124
2193
  operation.outputData ? JSON.stringify(operation.outputData) : null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashu/coco-expo-sqlite",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0-rc.4",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  "README.md"
14
14
  ],
15
15
  "devDependencies": {
16
- "@cashu/coco-adapter-tests": "1.0.0-rc.2"
16
+ "@cashu/coco-adapter-tests": "1.0.0-rc.4"
17
17
  },
18
18
  "exports": {
19
19
  ".": {
@@ -31,6 +31,6 @@
31
31
  "peerDependencies": {
32
32
  "typescript": "^5",
33
33
  "expo-sqlite": "*",
34
- "@cashu/coco-core": "1.0.0-rc.2"
34
+ "@cashu/coco-core": "1.0.0-rc.4"
35
35
  }
36
36
  }