@fedimint/core 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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -0
  3. package/dist/dts/FedimintWallet.d.ts +51 -0
  4. package/dist/dts/FedimintWallet.d.ts.map +1 -0
  5. package/dist/dts/WalletDirector.d.ts +79 -0
  6. package/dist/dts/WalletDirector.d.ts.map +1 -0
  7. package/dist/dts/index.d.ts +5 -0
  8. package/dist/dts/index.d.ts.map +1 -0
  9. package/dist/dts/services/BalanceService.d.ts +15 -0
  10. package/dist/dts/services/BalanceService.d.ts.map +1 -0
  11. package/dist/dts/services/FederationService.d.ts +13 -0
  12. package/dist/dts/services/FederationService.d.ts.map +1 -0
  13. package/dist/dts/services/LightningService.d.ts +48 -0
  14. package/dist/dts/services/LightningService.d.ts.map +1 -0
  15. package/dist/dts/services/MintService.d.ts +23 -0
  16. package/dist/dts/services/MintService.d.ts.map +1 -0
  17. package/dist/dts/services/RecoveryService.d.ts +13 -0
  18. package/dist/dts/services/RecoveryService.d.ts.map +1 -0
  19. package/dist/dts/services/WalletService.d.ts +13 -0
  20. package/dist/dts/services/WalletService.d.ts.map +1 -0
  21. package/dist/dts/services/index.d.ts +7 -0
  22. package/dist/dts/services/index.d.ts.map +1 -0
  23. package/dist/dts/transport/TransportClient.d.ts +55 -0
  24. package/dist/dts/transport/TransportClient.d.ts.map +1 -0
  25. package/dist/dts/transport/index.d.ts +2 -0
  26. package/dist/dts/transport/index.d.ts.map +1 -0
  27. package/dist/dts/types/index.d.ts +4 -0
  28. package/dist/dts/types/index.d.ts.map +1 -0
  29. package/dist/dts/types/transport.d.ts +2 -0
  30. package/dist/dts/types/transport.d.ts.map +1 -0
  31. package/dist/dts/types/utils.d.ts +21 -0
  32. package/dist/dts/types/utils.d.ts.map +1 -0
  33. package/dist/dts/types/wallet.d.ts +241 -0
  34. package/dist/dts/types/wallet.d.ts.map +1 -0
  35. package/dist/dts/utils/logger.d.ts +24 -0
  36. package/dist/dts/utils/logger.d.ts.map +1 -0
  37. package/dist/index.d.ts +578 -0
  38. package/dist/index.js +2 -0
  39. package/dist/index.js.map +1 -0
  40. package/package.json +40 -0
  41. package/src/FedimintWallet.ts +119 -0
  42. package/src/WalletDirector.ts +119 -0
  43. package/src/index.ts +4 -0
  44. package/src/services/BalanceService.test.ts +26 -0
  45. package/src/services/BalanceService.ts +29 -0
  46. package/src/services/FederationService.test.ts +58 -0
  47. package/src/services/FederationService.ts +216 -0
  48. package/src/services/LightningService.test.ts +265 -0
  49. package/src/services/LightningService.ts +289 -0
  50. package/src/services/MintService.test.ts +74 -0
  51. package/src/services/MintService.ts +129 -0
  52. package/src/services/RecoveryService.ts +28 -0
  53. package/src/services/WalletService.test.ts +59 -0
  54. package/src/services/WalletService.ts +50 -0
  55. package/src/services/index.ts +6 -0
  56. package/src/transport/TransportClient.ts +254 -0
  57. package/src/transport/index.ts +1 -0
  58. package/src/types/index.ts +3 -0
  59. package/src/types/transport.ts +1 -0
  60. package/src/types/utils.ts +23 -0
  61. package/src/types/wallet.ts +298 -0
  62. package/src/utils/logger.ts +69 -0
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@fedimint/core",
3
+ "description": "Core Fedimint client library for JavaScript environments",
4
+ "version": "0.1.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/fedimint/fedimint-web-sdk.git",
8
+ "directory": "packages/core"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "src"
13
+ ],
14
+ "sideEffects": [
15
+ "./dist/index.js"
16
+ ],
17
+ "type": "module",
18
+ "main": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "dependencies": {
21
+ "@fedimint/types": "0.0.1"
22
+ },
23
+ "devDependencies": {
24
+ "@rollup/plugin-terser": "^0.4.4",
25
+ "@rollup/plugin-typescript": "^11.1.6",
26
+ "@types/node": "^20.17.49",
27
+ "rollup": "^4.41.0",
28
+ "rollup-plugin-dts": "^6.2.1",
29
+ "rollup-plugin-typescript2": "^0.36.0",
30
+ "tslib": "^2.8.1",
31
+ "typescript": "^5.8.3"
32
+ },
33
+ "scripts": {
34
+ "build": "rollup --config",
35
+ "clean": "rm -rf dist tsconfig.tsbuildinfo",
36
+ "clean:deep": "pnpm run clean && rm -rf node_modules",
37
+ "typecheck": "tsc --noEmit",
38
+ "typedoc": "typedoc"
39
+ }
40
+ }
@@ -0,0 +1,119 @@
1
+ import { TransportClient } from './transport'
2
+ import {
3
+ BalanceService,
4
+ MintService,
5
+ LightningService,
6
+ FederationService,
7
+ RecoveryService,
8
+ WalletService,
9
+ } from './services'
10
+
11
+ const DEFAULT_CLIENT_NAME = 'fm-default' as const
12
+
13
+ export class FedimintWallet {
14
+ public balance: BalanceService
15
+ public mint: MintService
16
+ public lightning: LightningService
17
+ public federation: FederationService
18
+ public recovery: RecoveryService
19
+ public wallet: WalletService
20
+
21
+ private _openPromise: Promise<void> | undefined = undefined
22
+ private _resolveOpen: () => void = () => {}
23
+ private _isOpen: boolean = false
24
+
25
+ /**
26
+ * Creates a new instance of FedimintWallet.
27
+ *
28
+ * This constructor initializes a FedimintWallet instance, which manages communication
29
+ * with a Web Worker. The Web Worker is responsible for running WebAssembly code that
30
+ * handles the core Fedimint Client operations.
31
+ *
32
+ * (default) When not in lazy mode, the constructor immediately initializes the
33
+ * Web Worker and begins loading the WebAssembly module in the background. This
34
+ * allows for faster subsequent operations but may increase initial load time.
35
+ *
36
+ * In lazy mode, the Web Worker and WebAssembly initialization are deferred until
37
+ * the first operation that requires them, reducing initial overhead at the cost
38
+ * of a slight delay on the first operation.
39
+ *
40
+ * @example
41
+ * // Create a wallet with immediate initialization
42
+ * const wallet = new FedimintWallet();
43
+ * wallet.open();
44
+ *
45
+ * // Create a wallet with lazy initialization
46
+ * const lazyWallet = new FedimintWallet(true);
47
+ * // Some time later...
48
+ * lazyWallet.initialize();
49
+ * lazyWallet.open();
50
+ */
51
+ constructor(private _client: TransportClient) {
52
+ this._openPromise = new Promise((resolve) => {
53
+ this._resolveOpen = resolve
54
+ })
55
+ this.mint = new MintService(this._client)
56
+ this.lightning = new LightningService(this._client)
57
+ this.balance = new BalanceService(this._client)
58
+ this.federation = new FederationService(this._client)
59
+ this.recovery = new RecoveryService(this._client)
60
+ this.wallet = new WalletService(this._client)
61
+ }
62
+
63
+ async waitForOpen() {
64
+ if (this._isOpen) return Promise.resolve()
65
+ return this._openPromise
66
+ }
67
+
68
+ async open(clientName: string = DEFAULT_CLIENT_NAME) {
69
+ // TODO: Determine if this should be safe or throw
70
+ if (this._isOpen) throw new Error('The FedimintWallet is already open.')
71
+ const { success } = await this._client.sendSingleMessage<{
72
+ success: boolean
73
+ }>('open', { clientName })
74
+ if (success) {
75
+ this._isOpen = !!success
76
+ this._resolveOpen()
77
+ }
78
+ return success
79
+ }
80
+
81
+ async joinFederation(
82
+ inviteCode: string,
83
+ clientName: string = DEFAULT_CLIENT_NAME,
84
+ ) {
85
+ // TODO: Determine if this should be safe or throw
86
+ if (this._isOpen)
87
+ throw new Error(
88
+ 'The FedimintWallet is already open. You can only call `joinFederation` on closed clients.',
89
+ )
90
+ try {
91
+ const response = await this._client.sendSingleMessage<{
92
+ success: boolean
93
+ }>('join', { inviteCode, clientName })
94
+ if (response.success) {
95
+ this._isOpen = true
96
+ this._resolveOpen()
97
+ }
98
+
99
+ return response.success
100
+ } catch (e) {
101
+ this._client.logger.error('Error joining federation', e)
102
+ return false
103
+ }
104
+ }
105
+
106
+ /**
107
+ * This should ONLY be called when UNLOADING the wallet client.
108
+ * After this call, the FedimintWallet instance should be discarded.
109
+ */
110
+ async cleanup() {
111
+ this._openPromise = undefined
112
+ this._isOpen = false
113
+ await this._client.cleanup()
114
+ }
115
+
116
+ isOpen() {
117
+ return this._isOpen
118
+ }
119
+ }
@@ -0,0 +1,119 @@
1
+ import { TransportClient } from './transport'
2
+ import { type LogLevel } from './utils/logger'
3
+ import { FederationConfig, JSONValue } from './types'
4
+ import { Transport } from '@fedimint/types'
5
+ import { FedimintWallet } from './FedimintWallet'
6
+
7
+ export class WalletDirector {
8
+ // Protected to allow for TestWalletDirector to access the client
9
+ protected _client: TransportClient
10
+
11
+ /**
12
+ * Creates a new instance of WalletDirector.
13
+ *
14
+ * @param {Transport} [transport] - Optional worker client instance. Provide your
15
+ * own to use a custom transport (e.g. React Native).
16
+ *
17
+ * @param {boolean} lazy - If true, delays Web Worker and WebAssembly initialization
18
+ * until needed. Default is false.
19
+ */
20
+ constructor(transport: Transport, lazy: boolean = false) {
21
+ if (!transport) {
22
+ throw new Error('WalletDirector requires a transport implementation')
23
+ }
24
+ this._client = new TransportClient(transport)
25
+ this._client.logger.info('WalletDirector instantiated')
26
+ if (!lazy) {
27
+ this.initialize()
28
+ }
29
+ }
30
+
31
+ async initialize() {
32
+ this._client.logger.info('Initializing TransportClient')
33
+ await this._client.initialize()
34
+ this._client.logger.info('TransportClient initialized')
35
+ }
36
+
37
+ // TODO: Make this stateful... handle listing/joining/opening/closing wallets at this level
38
+ async createWallet() {
39
+ await this._client.initialize()
40
+ return new FedimintWallet(this._client)
41
+ }
42
+
43
+ async previewFederation(inviteCode: string) {
44
+ await this._client.initialize()
45
+ const response = this._client.sendSingleMessage<{
46
+ config: FederationConfig
47
+ federation_id: string
48
+ }>('previewFederation', { inviteCode })
49
+ return response
50
+ }
51
+
52
+ /**
53
+ * Sets the log level for the library.
54
+ * @param level The desired log level ('DEBUG', 'INFO', 'WARN', 'ERROR', 'NONE').
55
+ */
56
+ setLogLevel(level: LogLevel) {
57
+ this._client.logger.setLevel(level)
58
+ this._client.logger.info(`Log level set to ${level}.`)
59
+ }
60
+
61
+ /**
62
+ * Parses a federation invite code and retrieves its details.
63
+ *
64
+ * This method sends the provided invite code to the TransportClient for parsing.
65
+ * The response includes the federation_id and url.
66
+ *
67
+ * @param {string} inviteCode - The invite code to be parsed.
68
+ * @returns {Promise<{ federation_id: string, url: string}>}
69
+ * A promise that resolves to an object containing:
70
+ * - `federation_id`: The id of the feder.
71
+ * - `url`: One of the apipoints to connect to the federation
72
+ *
73
+ * @throws {Error} If the TransportClient encounters an issue during the parsing process.
74
+ *
75
+ * @example
76
+ * const inviteCode = "example-invite-code";
77
+ * const parsedCode = await wallet.parseInviteCode(inviteCode);
78
+ * console.log(parsedCode.federation_id, parsedCode.url);
79
+ */
80
+ async parseInviteCode(inviteCode: string) {
81
+ await this._client.initialize()
82
+ const response = await this._client.sendSingleMessage<{
83
+ type: string
84
+ data: JSONValue
85
+ requestId: number
86
+ }>('parseInviteCode', { inviteCode })
87
+ return response
88
+ }
89
+
90
+ /**
91
+ * Parses a BOLT11 Lightning invoice and retrieves its details.
92
+ *
93
+ * This method sends the provided invoice string to the TransportClient for parsing.
94
+ * The response includes details such as the amount, expiry, and memo.
95
+ *
96
+ * @param {string} invoiceStr - The BOLT11 invoice string to be parsed.
97
+ * @returns {Promise<{ amount: string, expiry: number, memo: string }>}
98
+ * A promise that resolves to an object containing:
99
+ * - `amount`: The amount specified in the invoice.
100
+ * - `expiry`: The expiry time of the invoice in seconds.
101
+ * - `memo`: A description or memo attached to the invoice.
102
+ *
103
+ * @throws {Error} If the TransportClient encounters an issue during the parsing process.
104
+ *
105
+ * @example
106
+ * const invoiceStr = "lnbc1...";
107
+ * const parsedInvoice = await wallet.parseBolt11Invoice(invoiceStr);
108
+ * console.log(parsedInvoice.amount, parsedInvoice.expiry, parsedInvoice.memo);
109
+ */
110
+ async parseBolt11Invoice(invoiceStr: string) {
111
+ await this._client.initialize()
112
+ const response = await this._client.sendSingleMessage<{
113
+ type: string
114
+ data: JSONValue
115
+ requestId: number
116
+ }>('parseBolt11Invoice', { invoiceStr })
117
+ return response
118
+ }
119
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { WalletDirector } from './WalletDirector'
2
+ export { TransportClient } from './transport'
3
+ export type { FedimintWallet } from './FedimintWallet'
4
+ export type * from './types'
@@ -0,0 +1,26 @@
1
+ import { expect } from 'vitest'
2
+ import { walletTest } from '../test/fixtures'
3
+
4
+ walletTest('getBalance should be initially zero', async ({ wallet }) => {
5
+ expect(wallet).toBeDefined()
6
+ expect(wallet.isOpen()).toBe(true)
7
+ const beforeGetBalance = wallet.testing.getRequestCounter()
8
+ await expect(wallet.balance.getBalance()).resolves.toEqual(0)
9
+ expect(wallet.testing.getRequestCounter()).toBe(beforeGetBalance + 1)
10
+ })
11
+
12
+ walletTest('subscribe balance', async ({ wallet }) => {
13
+ expect(wallet).toBeDefined()
14
+ expect(wallet.isOpen()).toBe(true)
15
+
16
+ const counterBefore = wallet.testing.getRequestCounter()
17
+ const callbacksBefore = wallet.testing.getRequestCallbackMap().size
18
+ const unsubscribe = await wallet.balance.subscribeBalance((balance) => {
19
+ expect(balance).toEqual(0)
20
+ })
21
+ expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
22
+ expect(wallet.testing.getRequestCallbackMap().size).toBe(callbacksBefore + 1)
23
+
24
+ await expect(wallet.balance.getBalance()).resolves.toEqual(0)
25
+ expect(unsubscribe()).toBeUndefined()
26
+ })
@@ -0,0 +1,29 @@
1
+ import { TransportClient } from '../transport'
2
+
3
+ /**
4
+ * Balance Service
5
+ *
6
+ * The Balance Service provides methods to interact with the balance of a Fedimint wallet.
7
+ */
8
+ export class BalanceService {
9
+ constructor(private client: TransportClient) {}
10
+
11
+ /** https://web.fedimint.org/core/FedimintWallet/BalanceService/getBalance */
12
+ async getBalance() {
13
+ return await this.client.rpcSingle<number>('', 'get_balance', {})
14
+ }
15
+
16
+ /** https://web.fedimint.org/core/FedimintWallet/BalanceService/subscribeBalance */
17
+ subscribeBalance(
18
+ onSuccess: (balanceMsats: number) => void = () => {},
19
+ onError: (error: string) => void = () => {},
20
+ ) {
21
+ return this.client.rpcStream<string>(
22
+ '',
23
+ 'subscribe_balance_changes',
24
+ {},
25
+ (res) => onSuccess(parseInt(res)),
26
+ onError,
27
+ )
28
+ }
29
+ }
@@ -0,0 +1,58 @@
1
+ import { expect } from 'vitest'
2
+ import { walletTest } from '../test/fixtures'
3
+
4
+ walletTest(
5
+ 'getConfig should return the federation config',
6
+ async ({ wallet }) => {
7
+ expect(wallet).toBeDefined()
8
+ expect(wallet.isOpen()).toBe(true)
9
+ const counterBefore = wallet.testing.getRequestCounter()
10
+ await expect(wallet.federation.getConfig()).resolves.toMatchObject({
11
+ api_endpoints: expect.any(Object),
12
+ broadcast_public_keys: expect.any(Object),
13
+ consensus_version: expect.any(Object),
14
+ meta: expect.any(Object),
15
+ modules: expect.any(Object),
16
+ })
17
+ expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
18
+ },
19
+ )
20
+
21
+ walletTest(
22
+ 'getFederationId should return the federation id',
23
+ async ({ wallet }) => {
24
+ expect(wallet).toBeDefined()
25
+ expect(wallet.isOpen()).toBe(true)
26
+
27
+ const counterBefore = wallet.testing.getRequestCounter()
28
+ const federationId = await wallet.federation.getFederationId()
29
+ expect(federationId).toBeTypeOf('string')
30
+ expect(federationId).toHaveLength(64)
31
+ expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
32
+ },
33
+ )
34
+
35
+ walletTest(
36
+ 'getInviteCode should return the invite code',
37
+ async ({ wallet }) => {
38
+ expect(wallet).toBeDefined()
39
+ expect(wallet.isOpen()).toBe(true)
40
+
41
+ const counterBefore = wallet.testing.getRequestCounter()
42
+ const inviteCode = await wallet.federation.getInviteCode(0)
43
+ expect(inviteCode).toBeTypeOf('string')
44
+ expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
45
+ },
46
+ )
47
+
48
+ walletTest(
49
+ 'listOperations should return the list of operations',
50
+ async ({ wallet }) => {
51
+ expect(wallet).toBeDefined()
52
+ expect(wallet.isOpen()).toBe(true)
53
+
54
+ const counterBefore = wallet.testing.getRequestCounter()
55
+ await expect(wallet.federation.listOperations()).resolves.toMatchObject([])
56
+ expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
57
+ },
58
+ )
@@ -0,0 +1,216 @@
1
+ import type {
2
+ EcashTransaction,
3
+ LightningTransaction,
4
+ LnVariant,
5
+ MintVariant,
6
+ OperationKey,
7
+ OperationLog,
8
+ Transactions,
9
+ WalletTransaction,
10
+ WalletVariant,
11
+ } from '../types'
12
+ import { TransportClient } from '../transport'
13
+
14
+ export class FederationService {
15
+ constructor(private client: TransportClient) {}
16
+
17
+ async getConfig() {
18
+ return await this.client.rpcSingle('', 'get_config', {})
19
+ }
20
+
21
+ async getFederationId() {
22
+ return await this.client.rpcSingle<string>('', 'get_federation_id', {})
23
+ }
24
+
25
+ async getInviteCode(peer: number = 0) {
26
+ return await this.client.rpcSingle<string | null>('', 'get_invite_code', {
27
+ peer,
28
+ })
29
+ }
30
+
31
+ async listOperations(
32
+ limit?: number,
33
+ last_seen?: OperationKey,
34
+ ): Promise<[OperationKey, OperationLog][]> {
35
+ return await this.client.rpcSingle<[OperationKey, OperationLog][]>(
36
+ '',
37
+ 'list_operations',
38
+ {
39
+ limit: limit ?? null,
40
+ last_seen: last_seen ?? null,
41
+ },
42
+ )
43
+ }
44
+
45
+ async getOperation(operationId: string) {
46
+ return await this.client.rpcSingle<OperationLog | null>(
47
+ '',
48
+ 'get_operation',
49
+ { operation_id: operationId },
50
+ )
51
+ }
52
+
53
+ async listTransactions(
54
+ limit?: number,
55
+ last_seen?: OperationKey,
56
+ ): Promise<Transactions[]> {
57
+ const operations = await this.listOperations(limit, last_seen)
58
+ return operations
59
+ .filter(
60
+ (item): item is [OperationKey, OperationLog] =>
61
+ Array.isArray(item) && item.length === 2,
62
+ )
63
+ .filter(([_, op]) => {
64
+ const { operation_module_kind, meta } = op
65
+ const variant = meta.variant
66
+ return (
67
+ (operation_module_kind === 'ln' &&
68
+ ((variant as LnVariant).pay || (variant as LnVariant).receive)) ||
69
+ (operation_module_kind === 'mint' &&
70
+ ((variant as MintVariant).spend_o_o_b ||
71
+ (variant as MintVariant).reissuance)) ||
72
+ (operation_module_kind === 'wallet' &&
73
+ ((variant as WalletVariant).deposit ||
74
+ (variant as WalletVariant).withdraw))
75
+ )
76
+ })
77
+ .map(([key, op]) => {
78
+ const timestamp = key.creation_time
79
+ ? Math.round(
80
+ key.creation_time.secs_since_epoch * 1000 +
81
+ key.creation_time.nanos_since_epoch / 1_000_000,
82
+ )
83
+ : 0
84
+ const operationId = key.operation_id
85
+ const kind = op.operation_module_kind as 'ln' | 'mint' | 'wallet'
86
+ const meta = op.meta
87
+ const variant = meta.variant
88
+
89
+ let outcome: string | undefined
90
+ if (op.outcome && op.outcome.outcome) {
91
+ if (typeof op.outcome.outcome === 'string') {
92
+ outcome = op.outcome.outcome
93
+ } else if (
94
+ typeof op.outcome.outcome === 'object' &&
95
+ op.outcome.outcome !== null
96
+ ) {
97
+ if ('success' in op.outcome.outcome) outcome = 'success'
98
+ else if ('canceled' in op.outcome.outcome) outcome = 'canceled'
99
+ else if ('claimed' in op.outcome.outcome) outcome = 'claimed'
100
+ else if ('funded' in op.outcome.outcome) outcome = 'funded'
101
+ else if ('awaiting_funds' in op.outcome.outcome)
102
+ outcome = 'awaiting_funds'
103
+ else if ('unexpected_error' in op.outcome.outcome)
104
+ outcome = 'unexpected_error'
105
+ else if ('created' in op.outcome.outcome) outcome = 'created'
106
+ else if ('waiting_for_refund' in op.outcome.outcome)
107
+ outcome = 'canceled'
108
+ else if ('awaiting_change' in op.outcome.outcome)
109
+ outcome = 'pending'
110
+ else if ('refunded' in op.outcome.outcome) outcome = 'refunded'
111
+ else if ('waiting_for_payment' in op.outcome.outcome)
112
+ outcome = 'awaiting_funds'
113
+ else if ('Created' in op.outcome.outcome) outcome = 'Created'
114
+ else if ('Success' in op.outcome.outcome) outcome = 'Success'
115
+ else if ('Refunded' in op.outcome.outcome) outcome = 'Refunded'
116
+ else if ('UserCanceledProcessing' in op.outcome.outcome)
117
+ outcome = 'UserCanceledProcessing'
118
+ else if ('UserCanceledSuccess' in op.outcome.outcome)
119
+ outcome = 'UserCanceledSuccess'
120
+ else if ('UserCanceledFailure' in op.outcome.outcome)
121
+ outcome = 'UserCanceledFailure'
122
+ else if ('WaitingForTransaction' in op.outcome.outcome)
123
+ outcome = 'pending'
124
+ else if ('WaitingForConfirmation' in op.outcome.outcome)
125
+ outcome = 'pending'
126
+ else if ('Confirmed' in op.outcome.outcome) outcome = 'Confirmed'
127
+ else if ('Claimed' in op.outcome.outcome) outcome = 'Claimed'
128
+ else if ('Failed' in op.outcome.outcome) outcome = 'Failed'
129
+ }
130
+ }
131
+
132
+ if (kind === 'ln') {
133
+ const isPay = !!(variant as LnVariant).pay
134
+ const txId =
135
+ (variant as LnVariant).pay?.out_point.txid ||
136
+ (variant as LnVariant).receive?.out_point.txid ||
137
+ ''
138
+ const type = (variant as LnVariant).pay ? 'send' : 'receive'
139
+ const invoice =
140
+ (variant as LnVariant).pay?.invoice ||
141
+ (variant as LnVariant).receive?.invoice ||
142
+ ''
143
+ const gateway =
144
+ (variant as LnVariant).pay?.gateway_id ||
145
+ (variant as LnVariant).receive?.gateway_id ||
146
+ ''
147
+ const fee = (variant as LnVariant).pay?.fee
148
+ const internalPay = (variant as LnVariant).pay?.is_internal_payment
149
+ const preimage =
150
+ isPay &&
151
+ op.outcome?.outcome &&
152
+ typeof op.outcome.outcome === 'object' &&
153
+ 'success' in op.outcome.outcome
154
+ ? op.outcome.outcome.success.preimage
155
+ : undefined
156
+
157
+ return {
158
+ timestamp,
159
+ operationId,
160
+ kind,
161
+ txId,
162
+ type,
163
+ invoice,
164
+ internalPay,
165
+ fee,
166
+ gateway,
167
+ outcome: outcome as LightningTransaction['outcome'],
168
+ } as LightningTransaction
169
+ } else if (kind === 'mint') {
170
+ const txId = (variant as MintVariant).reissuance?.txid
171
+ const type = (variant as MintVariant).reissuance
172
+ ? 'reissue'
173
+ : 'spend_oob'
174
+ const amountMsats = meta.amount
175
+ const notes = (variant as MintVariant).spend_o_o_b?.oob_notes
176
+
177
+ return {
178
+ timestamp,
179
+ type,
180
+ txId,
181
+ outcome: outcome as EcashTransaction['outcome'],
182
+ operationId,
183
+ amountMsats,
184
+ notes,
185
+ kind,
186
+ } as EcashTransaction
187
+ } else if (kind === 'wallet') {
188
+ const type = (variant as WalletVariant).deposit
189
+ ? 'deposit'
190
+ : 'withdraw'
191
+ const address =
192
+ (variant as WalletVariant).deposit?.address ||
193
+ (variant as WalletVariant).withdraw?.address ||
194
+ ''
195
+ const feeRate = (variant as WalletVariant).withdraw?.fee.fee_rate
196
+ .sats_per_kvb
197
+ const amountMsats =
198
+ (variant as WalletVariant).withdraw?.amountMsats || 0
199
+
200
+ return {
201
+ timestamp,
202
+ type,
203
+ onchainAddress: address,
204
+ fee: feeRate || 0,
205
+ amountMsats,
206
+ outcome,
207
+ kind,
208
+ operationId,
209
+ } as WalletTransaction
210
+ }
211
+ })
212
+ .filter(
213
+ (transaction): transaction is Transactions => transaction !== undefined,
214
+ )
215
+ }
216
+ }