@fedimint/core-web 0.0.10 → 0.0.11

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 (37) hide show
  1. package/dist/dts/FedimintWallet.d.ts +50 -0
  2. package/dist/dts/FedimintWallet.d.ts.map +1 -1
  3. package/dist/dts/services/BalanceService.d.ts +2 -21
  4. package/dist/dts/services/BalanceService.d.ts.map +1 -1
  5. package/dist/dts/services/FederationService.d.ts +1 -1
  6. package/dist/dts/services/FederationService.d.ts.map +1 -1
  7. package/dist/dts/services/LightningService.d.ts +11 -2
  8. package/dist/dts/services/LightningService.d.ts.map +1 -1
  9. package/dist/dts/services/MintService.d.ts +12 -6
  10. package/dist/dts/services/MintService.d.ts.map +1 -1
  11. package/dist/dts/services/RecoveryService.d.ts +1 -1
  12. package/dist/dts/services/RecoveryService.d.ts.map +1 -1
  13. package/dist/dts/types/utils.d.ts +8 -1
  14. package/dist/dts/types/utils.d.ts.map +1 -1
  15. package/dist/dts/types/wallet.d.ts +4 -10
  16. package/dist/dts/types/wallet.d.ts.map +1 -1
  17. package/dist/dts/types/worker.d.ts +1 -1
  18. package/dist/dts/types/worker.d.ts.map +1 -1
  19. package/dist/dts/worker/WorkerClient.d.ts +1 -1
  20. package/dist/dts/worker/WorkerClient.d.ts.map +1 -1
  21. package/dist/index.d.ts +87 -40
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/worker.js +1 -1
  25. package/dist/worker.js.map +1 -1
  26. package/package.json +8 -8
  27. package/src/FedimintWallet.ts +58 -0
  28. package/src/services/BalanceService.ts +5 -26
  29. package/src/services/FederationService.ts +9 -7
  30. package/src/services/LightningService.ts +81 -65
  31. package/src/services/MintService.ts +32 -25
  32. package/src/services/RecoveryService.ts +9 -7
  33. package/src/types/utils.ts +5 -1
  34. package/src/types/wallet.ts +12 -9
  35. package/src/types/worker.ts +2 -0
  36. package/src/worker/WorkerClient.ts +8 -9
  37. package/src/worker/worker.js +34 -0
@@ -6,31 +6,38 @@ import type {
6
6
  MintSpendNotesResponse,
7
7
  MSats,
8
8
  ReissueExternalNotesState,
9
+ SpendNotesState,
9
10
  } from '../types'
10
11
 
11
12
  export class MintService {
12
13
  constructor(private client: WorkerClient) {}
13
14
 
14
- async redeemEcash(notes: string): Promise<void> {
15
- await this.client.rpcSingle('mint', 'reissue_external_notes', {
16
- oob_notes: notes, // "out of band notes"
17
- extra_meta: null,
18
- })
15
+ /** https://web.fedimint.org/core/FedimintWallet/MintService/redeemEcash */
16
+ async redeemEcash(notes: string) {
17
+ return await this.client.rpcSingle<string>(
18
+ 'mint',
19
+ 'reissue_external_notes',
20
+ {
21
+ oob_notes: notes, // "out of band notes"
22
+ extra_meta: null,
23
+ },
24
+ )
19
25
  }
20
26
 
21
- async reissueExternalNotes(
22
- oobNotes: string,
23
- extraMeta: JSONObject = {},
24
- ): Promise<string> {
25
- return await this.client.rpcSingle('mint', 'reissue_external_notes', {
26
- oob_notes: oobNotes,
27
- extra_meta: extraMeta,
28
- })
27
+ async reissueExternalNotes(oobNotes: string, extraMeta: JSONObject = {}) {
28
+ return await this.client.rpcSingle<string>(
29
+ 'mint',
30
+ 'reissue_external_notes',
31
+ {
32
+ oob_notes: oobNotes,
33
+ extra_meta: extraMeta,
34
+ },
35
+ )
29
36
  }
30
37
 
31
38
  subscribeReissueExternalNotes(
32
39
  operationId: string,
33
- onSuccess: (state: JSONValue) => void = () => {},
40
+ onSuccess: (state: ReissueExternalNotesState) => void = () => {},
34
41
  onError: (error: string) => void = () => {},
35
42
  ) {
36
43
  const unsubscribe = this.client.rpcStream<ReissueExternalNotesState>(
@@ -44,6 +51,7 @@ export class MintService {
44
51
  return unsubscribe
45
52
  }
46
53
 
54
+ /** https://web.fedimint.org/core/FedimintWallet/MintService/spendNotes */
47
55
  async spendNotes(
48
56
  amountMsats: number,
49
57
  // Tells the wallet to automatically try to cancel the spend if it hasn't completed
@@ -52,17 +60,17 @@ export class MintService {
52
60
  tryCancelAfter: number | Duration = 3600 * 24, // defaults to 1 day
53
61
  includeInvite: boolean = false,
54
62
  extraMeta: JSONValue = {},
55
- ): Promise<MintSpendNotesResponse> {
63
+ ) {
56
64
  const duration =
57
65
  typeof tryCancelAfter === 'number'
58
66
  ? { nanos: 0, secs: tryCancelAfter }
59
67
  : tryCancelAfter
60
68
 
61
- const res = await this.client.rpcSingle<Array<string>>(
69
+ const res = await this.client.rpcSingle<MintSpendNotesResponse>(
62
70
  'mint',
63
71
  'spend_notes',
64
72
  {
65
- min_amount: amountMsats,
73
+ amount: amountMsats,
66
74
  try_cancel_after: duration,
67
75
  include_invite: includeInvite,
68
76
  extra_meta: extraMeta,
@@ -77,13 +85,14 @@ export class MintService {
77
85
  }
78
86
  }
79
87
 
80
- async parseNotes(oobNotes: string): Promise<MSats> {
81
- return await this.client.rpcSingle('mint', 'validate_notes', {
88
+ /** https://web.fedimint.org/core/FedimintWallet/MintService/parseEcash */
89
+ async parseNotes(oobNotes: string) {
90
+ return await this.client.rpcSingle<MSats>('mint', 'validate_notes', {
82
91
  oob_notes: oobNotes,
83
92
  })
84
93
  }
85
94
 
86
- async tryCancelSpendNotes(operationId: string): Promise<void> {
95
+ async tryCancelSpendNotes(operationId: string) {
87
96
  await this.client.rpcSingle('mint', 'try_cancel_spend_notes', {
88
97
  operation_id: operationId,
89
98
  })
@@ -91,21 +100,19 @@ export class MintService {
91
100
 
92
101
  subscribeSpendNotes(
93
102
  operationId: string,
94
- onSuccess: (state: JSONValue) => void = () => {},
103
+ onSuccess: (state: SpendNotesState) => void = () => {},
95
104
  onError: (error: string) => void = () => {},
96
105
  ) {
97
- const unsubscribe = this.client.rpcStream(
106
+ return this.client.rpcStream<SpendNotesState>(
98
107
  'mint',
99
108
  'subscribe_spend_notes',
100
109
  { operation_id: operationId },
101
110
  (res) => onSuccess(res),
102
111
  onError,
103
112
  )
104
-
105
- return unsubscribe
106
113
  }
107
114
 
108
- async awaitSpendOobRefund(operationId: string): Promise<JSONValue> {
115
+ async awaitSpendOobRefund(operationId: string) {
109
116
  return await this.client.rpcSingle('mint', 'await_spend_oob_refund', {
110
117
  operation_id: operationId,
111
118
  })
@@ -4,23 +4,25 @@ import { WorkerClient } from '../worker'
4
4
  export class RecoveryService {
5
5
  constructor(private client: WorkerClient) {}
6
6
 
7
- async hasPendingRecoveries(): Promise<boolean> {
8
- return await this.client.rpcSingle('', 'has_pending_recoveries', {})
7
+ async hasPendingRecoveries() {
8
+ return await this.client.rpcSingle<boolean>(
9
+ '',
10
+ 'has_pending_recoveries',
11
+ {},
12
+ )
9
13
  }
10
14
 
11
- async waitForAllRecoveries(): Promise<void> {
15
+ async waitForAllRecoveries() {
12
16
  await this.client.rpcSingle('', 'wait_for_all_recoveries', {})
13
17
  }
14
18
 
15
19
  subscribeToRecoveryProgress(
16
20
  onSuccess: (progress: { module_id: number; progress: JSONValue }) => void,
17
21
  onError: (error: string) => void,
18
- ): () => void {
19
- const unsubscribe = this.client.rpcStream<{
22
+ ) {
23
+ return this.client.rpcStream<{
20
24
  module_id: number
21
25
  progress: JSONValue
22
26
  }>('', 'subscribe_to_recovery_progress', {}, onSuccess, onError)
23
-
24
- return unsubscribe
25
27
  }
26
28
  }
@@ -22,4 +22,8 @@ type JSONValue =
22
22
 
23
23
  type JSONObject = Record<string, JSONValue>
24
24
 
25
- export { Alias, Resolve, Duration, MSats, Sats, JSONValue, JSONObject }
25
+ type Result<T, U = string> =
26
+ | { success: true; data?: T }
27
+ | { success: false; error: U }
28
+
29
+ export { Alias, Resolve, Duration, MSats, Sats, JSONValue, JSONObject, Result }
@@ -80,16 +80,18 @@ type StreamResult<T extends JSONValue> =
80
80
 
81
81
  type CancelFunction = () => void
82
82
 
83
- type ReissueExternalNotesState =
84
- | 'Created'
85
- | 'Issuing'
86
- | 'Done'
87
- | { Failed: { error: string } }
83
+ type ReissueExternalNotesState = 'Created' | 'Issuing' | 'Done'
84
+ // | { Failed: { error: string } }
88
85
 
89
- type MintSpendNotesResponse = {
90
- notes: string
91
- operation_id: string
92
- }
86
+ type MintSpendNotesResponse = Array<string>
87
+
88
+ type SpendNotesState =
89
+ | 'Created'
90
+ | 'UserCanceledProcessing'
91
+ | 'UserCanceledSuccess'
92
+ | 'UserCanceledFailure'
93
+ | 'Success'
94
+ | 'Refunded'
93
95
 
94
96
  export {
95
97
  LightningGateway,
@@ -108,4 +110,5 @@ export {
108
110
  CancelFunction,
109
111
  ReissueExternalNotesState,
110
112
  MintSpendNotesResponse,
113
+ SpendNotesState,
111
114
  }
@@ -8,6 +8,8 @@ const WorkerMessageTypes = [
8
8
  'error',
9
9
  'unsubscribe',
10
10
  'cleanup',
11
+ 'parseInviteCode',
12
+ 'parseBolt11Invoice',
11
13
  ] as const
12
14
 
13
15
  export type WorkerMessageType = (typeof WorkerMessageTypes)[number]
@@ -69,8 +69,8 @@ export class WorkerClient {
69
69
  sendSingleMessage<
70
70
  Response extends JSONValue = JSONValue,
71
71
  Payload extends JSONValue = JSONValue,
72
- >(type: WorkerMessageType, payload?: Payload): Promise<Response> {
73
- return new Promise((resolve, reject) => {
72
+ >(type: WorkerMessageType, payload?: Payload) {
73
+ return new Promise<Response>((resolve, reject) => {
74
74
  const requestId = ++this.requestCounter
75
75
  logger.debug('WorkerClient - sendSingleMessage', requestId, type, payload)
76
76
  this.requestCallbacks.set(
@@ -179,7 +179,7 @@ export class WorkerClient {
179
179
  onEnd: () => void = () => {},
180
180
  unsubscribePromise: Promise<void>,
181
181
  // Unsubscribe function
182
- ): Promise<void> {
182
+ ) {
183
183
  // await this.openPromise
184
184
  // if (!this.worker || !this._isOpen)
185
185
  // throw new Error('FedimintWallet is not open')
@@ -209,13 +209,12 @@ export class WorkerClient {
209
209
  })
210
210
  }
211
211
 
212
- rpcSingle<Response extends JSONValue = JSONValue>(
213
- module: ModuleKind,
214
- method: string,
215
- body: JSONValue,
216
- ): Promise<Response> {
212
+ rpcSingle<
213
+ Response extends JSONValue = JSONValue,
214
+ Error extends string = string,
215
+ >(module: ModuleKind, method: string, body: JSONValue) {
217
216
  logger.debug('WorkerClient - rpcSingle', module, method, body)
218
- return new Promise((resolve, reject) => {
217
+ return new Promise<Response>((resolve, reject) => {
219
218
  this.rpcStream<Response>(module, method, body, resolve, reject)
220
219
  })
221
220
  }
@@ -89,6 +89,40 @@ self.onmessage = async (event) => {
89
89
  requestId,
90
90
  })
91
91
  close()
92
+ } else if (type === 'parseInviteCode') {
93
+ const { inviteCode } = payload
94
+ try {
95
+ const res = WasmClient.parse_invite_code(inviteCode)
96
+ const parsedRes = JSON.parse(res)
97
+ self.postMessage({
98
+ type: 'parseInviteCode',
99
+ data: parsedRes,
100
+ requestId,
101
+ })
102
+ } catch (error) {
103
+ self.postMessage({
104
+ type: 'error',
105
+ error: `Failed to parse invite code: ${error.message}`,
106
+ requestId,
107
+ })
108
+ }
109
+ } else if (type === 'parseBolt11Invoice') {
110
+ const { invoiceStr } = payload
111
+ try {
112
+ const res = WasmClient.parse_bolt11_invoice(invoiceStr)
113
+ const parsedRes = JSON.parse(res)
114
+ self.postMessage({
115
+ type: 'parseBolt11Invoice',
116
+ data: parsedRes,
117
+ requestId,
118
+ })
119
+ } catch (error) {
120
+ self.postMessage({
121
+ type: 'error',
122
+ error: `Failed to parse invoice: ${error.message}`,
123
+ requestId,
124
+ })
125
+ }
92
126
  } else {
93
127
  self.postMessage({
94
128
  type: 'error',