@fedimint/core 0.1.0 → 0.1.3

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 (44) hide show
  1. package/dist/dts/FedimintWallet.d.ts +2 -1
  2. package/dist/dts/FedimintWallet.d.ts.map +1 -1
  3. package/dist/dts/WalletDirector.d.ts +16 -0
  4. package/dist/dts/WalletDirector.d.ts.map +1 -1
  5. package/dist/dts/services/BalanceService.d.ts +2 -1
  6. package/dist/dts/services/BalanceService.d.ts.map +1 -1
  7. package/dist/dts/services/FederationService.d.ts +2 -1
  8. package/dist/dts/services/FederationService.d.ts.map +1 -1
  9. package/dist/dts/services/LightningService.d.ts +6 -5
  10. package/dist/dts/services/LightningService.d.ts.map +1 -1
  11. package/dist/dts/services/MintService.d.ts +4 -3
  12. package/dist/dts/services/MintService.d.ts.map +1 -1
  13. package/dist/dts/services/RecoveryService.d.ts +3 -2
  14. package/dist/dts/services/RecoveryService.d.ts.map +1 -1
  15. package/dist/dts/services/WalletService.d.ts +3 -2
  16. package/dist/dts/services/WalletService.d.ts.map +1 -1
  17. package/dist/dts/testing.d.ts +2 -0
  18. package/dist/dts/testing.d.ts.map +1 -0
  19. package/dist/dts/transport/TransportClient.d.ts +3 -2
  20. package/dist/dts/transport/TransportClient.d.ts.map +1 -1
  21. package/dist/index.d.ts +20 -501
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/testing.d-CJcyXM5Q.d.ts +510 -0
  25. package/dist/testing.d.ts +2 -0
  26. package/dist/testing.js +2 -0
  27. package/dist/testing.js.map +1 -0
  28. package/package.json +17 -6
  29. package/src/FedimintWallet.ts +30 -22
  30. package/src/WalletDirector.ts +38 -3
  31. package/src/services/BalanceService.ts +11 -2
  32. package/src/services/FederationService.ts +21 -6
  33. package/src/services/LightningService.ts +20 -2
  34. package/src/services/MintService.ts +34 -10
  35. package/src/services/RecoveryService.ts +19 -3
  36. package/src/services/WalletService.ts +22 -7
  37. package/src/testing.ts +3 -0
  38. package/src/transport/TransportClient.ts +57 -17
  39. package/src/utils/logger.ts +2 -2
  40. package/src/services/BalanceService.test.ts +0 -26
  41. package/src/services/FederationService.test.ts +0 -58
  42. package/src/services/LightningService.test.ts +0 -265
  43. package/src/services/MintService.test.ts +0 -74
  44. package/src/services/WalletService.test.ts +0 -59
@@ -8,7 +8,9 @@ import {
8
8
  WalletService,
9
9
  } from './services'
10
10
 
11
- const DEFAULT_CLIENT_NAME = 'fm-default' as const
11
+ // The Rpc requires exactly 36 length uuid strings
12
+ // This is temporary until we have a proper client management system
13
+ const DEFAULT_CLIENT_NAME = 'dd5135b2-c228-41b7-a4f9-3b6e7afe3088' as const
12
14
 
13
15
  export class FedimintWallet {
14
16
  public balance: BalanceService
@@ -48,16 +50,19 @@ export class FedimintWallet {
48
50
  * lazyWallet.initialize();
49
51
  * lazyWallet.open();
50
52
  */
51
- constructor(private _client: TransportClient) {
53
+ constructor(
54
+ private _client: TransportClient,
55
+ private _clientName: string = DEFAULT_CLIENT_NAME,
56
+ ) {
52
57
  this._openPromise = new Promise((resolve) => {
53
58
  this._resolveOpen = resolve
54
59
  })
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)
60
+ this.mint = new MintService(this._client, this._clientName)
61
+ this.lightning = new LightningService(this._client, this._clientName)
62
+ this.balance = new BalanceService(this._client, this._clientName)
63
+ this.federation = new FederationService(this._client, this._clientName)
64
+ this.recovery = new RecoveryService(this._client, this._clientName)
65
+ this.wallet = new WalletService(this._client, this._clientName)
61
66
  }
62
67
 
63
68
  async waitForOpen() {
@@ -68,14 +73,17 @@ export class FedimintWallet {
68
73
  async open(clientName: string = DEFAULT_CLIENT_NAME) {
69
74
  // TODO: Determine if this should be safe or throw
70
75
  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
+ try {
77
+ await this._client.sendSingleMessage('open_client', {
78
+ client_name: clientName,
79
+ })
80
+ this._isOpen = true
76
81
  this._resolveOpen()
82
+ return true
83
+ } catch (e) {
84
+ this._client.logger.error('Error opening client', e)
85
+ throw e
77
86
  }
78
- return success
79
87
  }
80
88
 
81
89
  async joinFederation(
@@ -88,15 +96,15 @@ export class FedimintWallet {
88
96
  'The FedimintWallet is already open. You can only call `joinFederation` on closed clients.',
89
97
  )
90
98
  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
- }
99
+ await this._client.sendSingleMessage('join_federation', {
100
+ invite_code: inviteCode,
101
+ client_name: clientName,
102
+ force_recover: false,
103
+ })
104
+ this._isOpen = true
105
+ this._resolveOpen()
98
106
 
99
- return response.success
107
+ return true
100
108
  } catch (e) {
101
109
  this._client.logger.error('Error joining federation', e)
102
110
  return false
@@ -45,7 +45,7 @@ export class WalletDirector {
45
45
  const response = this._client.sendSingleMessage<{
46
46
  config: FederationConfig
47
47
  federation_id: string
48
- }>('previewFederation', { inviteCode })
48
+ }>('preview_federation', { invite_code: inviteCode })
49
49
  return response
50
50
  }
51
51
 
@@ -83,7 +83,7 @@ export class WalletDirector {
83
83
  type: string
84
84
  data: JSONValue
85
85
  requestId: number
86
- }>('parseInviteCode', { inviteCode })
86
+ }>('parse_invite_code', { inviteCode })
87
87
  return response
88
88
  }
89
89
 
@@ -113,7 +113,42 @@ export class WalletDirector {
113
113
  type: string
114
114
  data: JSONValue
115
115
  requestId: number
116
- }>('parseBolt11Invoice', { invoiceStr })
116
+ }>('parse_bolt11_invoice', { invoiceStr })
117
117
  return response
118
118
  }
119
+
120
+ /**
121
+ * Generates and sets a new mnemonic phrase.
122
+ * @returns {Promise<string[]>} A promise that resolves to the generated mnemonic phrase.
123
+ */
124
+ async generateMnemonic(): Promise<string[]> {
125
+ const result = await this._client.sendSingleMessage<{ mnemonic: string[] }>(
126
+ 'generate_mnemonic',
127
+ )
128
+ return result.mnemonic
129
+ }
130
+
131
+ /**
132
+ * Retrieves the current mnemonic phrase.
133
+ * @returns {Promise<string[]>} A promise that resolves to the current mnemonic phrase.
134
+ */
135
+ async getMnemonic(): Promise<string[]> {
136
+ const result = await this._client.sendSingleMessage<{ mnemonic: string[] }>(
137
+ 'get_mnemonic',
138
+ )
139
+ return result.mnemonic
140
+ }
141
+
142
+ /**
143
+ * Sets the mnemonic phrase.
144
+ * @param {string[]} words - The mnemonic words to set.
145
+ * @returns {Promise<boolean>} A promise that resolves to true if the mnemonic was set successfully.
146
+ */
147
+ async setMnemonic(words: string[]): Promise<boolean> {
148
+ const result = await this._client.sendSingleMessage<{ success: boolean }>(
149
+ 'set_mnemonic',
150
+ { words },
151
+ )
152
+ return result.success
153
+ }
119
154
  }
@@ -6,11 +6,19 @@ import { TransportClient } from '../transport'
6
6
  * The Balance Service provides methods to interact with the balance of a Fedimint wallet.
7
7
  */
8
8
  export class BalanceService {
9
- constructor(private client: TransportClient) {}
9
+ constructor(
10
+ private client: TransportClient,
11
+ private clientName: string,
12
+ ) {}
10
13
 
11
14
  /** https://web.fedimint.org/core/FedimintWallet/BalanceService/getBalance */
12
15
  async getBalance() {
13
- return await this.client.rpcSingle<number>('', 'get_balance', {})
16
+ return await this.client.rpcSingle<number>(
17
+ '',
18
+ 'get_balance',
19
+ {},
20
+ this.clientName,
21
+ )
14
22
  }
15
23
 
16
24
  /** https://web.fedimint.org/core/FedimintWallet/BalanceService/subscribeBalance */
@@ -22,6 +30,7 @@ export class BalanceService {
22
30
  '',
23
31
  'subscribe_balance_changes',
24
32
  {},
33
+ this.clientName,
25
34
  (res) => onSuccess(parseInt(res)),
26
35
  onError,
27
36
  )
@@ -12,20 +12,33 @@ import type {
12
12
  import { TransportClient } from '../transport'
13
13
 
14
14
  export class FederationService {
15
- constructor(private client: TransportClient) {}
15
+ constructor(
16
+ private client: TransportClient,
17
+ private clientName: string,
18
+ ) {}
16
19
 
17
20
  async getConfig() {
18
- return await this.client.rpcSingle('', 'get_config', {})
21
+ return await this.client.rpcSingle('', 'get_config', {}, this.clientName)
19
22
  }
20
23
 
21
24
  async getFederationId() {
22
- return await this.client.rpcSingle<string>('', 'get_federation_id', {})
25
+ return await this.client.rpcSingle<string>(
26
+ '',
27
+ 'get_federation_id',
28
+ {},
29
+ this.clientName,
30
+ )
23
31
  }
24
32
 
25
33
  async getInviteCode(peer: number = 0) {
26
- return await this.client.rpcSingle<string | null>('', 'get_invite_code', {
27
- peer,
28
- })
34
+ return await this.client.rpcSingle<string | null>(
35
+ '',
36
+ 'get_invite_code',
37
+ {
38
+ peer,
39
+ },
40
+ this.clientName,
41
+ )
29
42
  }
30
43
 
31
44
  async listOperations(
@@ -39,6 +52,7 @@ export class FederationService {
39
52
  limit: limit ?? null,
40
53
  last_seen: last_seen ?? null,
41
54
  },
55
+ this.clientName,
42
56
  )
43
57
  }
44
58
 
@@ -47,6 +61,7 @@ export class FederationService {
47
61
  '',
48
62
  'get_operation',
49
63
  { operation_id: operationId },
64
+ this.clientName,
50
65
  )
51
66
  }
52
67
 
@@ -11,7 +11,10 @@ import type {
11
11
  } from '../types'
12
12
 
13
13
  export class LightningService {
14
- constructor(private client: TransportClient) {}
14
+ constructor(
15
+ private client: TransportClient,
16
+ private clientName: string,
17
+ ) {}
15
18
 
16
19
  /** https://web.fedimint.org/core/FedimintWallet/LightningService/createInvoice#lightning-createinvoice */
17
20
  async createInvoice(
@@ -32,6 +35,7 @@ export class LightningService {
32
35
  extra_meta: extraMeta ?? {},
33
36
  gateway,
34
37
  },
38
+ this.clientName,
35
39
  )
36
40
  }
37
41
 
@@ -57,6 +61,7 @@ export class LightningService {
57
61
  extra_meta: extraMeta ?? {},
58
62
  gateway,
59
63
  },
64
+ this.clientName,
60
65
  )
61
66
  }
62
67
 
@@ -74,6 +79,7 @@ export class LightningService {
74
79
  indices,
75
80
  extra_meta: extraMeta ?? {},
76
81
  },
82
+ this.clientName,
77
83
  )
78
84
  }
79
85
 
@@ -98,6 +104,7 @@ export class LightningService {
98
104
  invoice,
99
105
  extra_meta: extraMeta ?? {},
100
106
  },
107
+ this.clientName,
101
108
  )
102
109
  }
103
110
 
@@ -151,6 +158,7 @@ export class LightningService {
151
158
  'ln',
152
159
  'subscribe_internal_pay',
153
160
  { operation_id: operation_id },
161
+ this.clientName,
154
162
  onSuccess,
155
163
  onError,
156
164
  )
@@ -166,6 +174,7 @@ export class LightningService {
166
174
  'ln',
167
175
  'subscribe_ln_claim',
168
176
  { operation_id: operationId },
177
+ this.clientName,
169
178
  onSuccess,
170
179
  onError,
171
180
  )
@@ -183,6 +192,7 @@ export class LightningService {
183
192
  'ln',
184
193
  'subscribe_ln_pay',
185
194
  { operation_id: operationId },
195
+ this.clientName,
186
196
  onSuccess,
187
197
  onError,
188
198
  )
@@ -230,6 +240,7 @@ export class LightningService {
230
240
  'ln',
231
241
  'subscribe_ln_receive',
232
242
  { operation_id: operationId },
243
+ this.clientName,
233
244
  onSuccess,
234
245
  onError,
235
246
  )
@@ -272,6 +283,7 @@ export class LightningService {
272
283
  gateway_id: gatewayId,
273
284
  force_internal: forceInternal,
274
285
  },
286
+ this.clientName,
275
287
  )
276
288
  }
277
289
 
@@ -280,10 +292,16 @@ export class LightningService {
280
292
  'ln',
281
293
  'list_gateways',
282
294
  {},
295
+ this.clientName,
283
296
  )
284
297
  }
285
298
 
286
299
  async updateGatewayCache() {
287
- return await this.client.rpcSingle('ln', 'update_gateway_cache', {})
300
+ return await this.client.rpcSingle(
301
+ 'ln',
302
+ 'update_gateway_cache',
303
+ {},
304
+ this.clientName,
305
+ )
288
306
  }
289
307
  }
@@ -11,7 +11,10 @@ import type {
11
11
  } from '../types'
12
12
 
13
13
  export class MintService {
14
- constructor(private client: TransportClient) {}
14
+ constructor(
15
+ private client: TransportClient,
16
+ private clientName: string,
17
+ ) {}
15
18
 
16
19
  /** https://web.fedimint.org/core/FedimintWallet/MintService/redeemEcash */
17
20
  async redeemEcash(notes: string) {
@@ -22,6 +25,7 @@ export class MintService {
22
25
  oob_notes: notes, // "out of band notes"
23
26
  extra_meta: null,
24
27
  },
28
+ this.clientName,
25
29
  )
26
30
  }
27
31
 
@@ -33,6 +37,7 @@ export class MintService {
33
37
  oob_notes: oobNotes,
34
38
  extra_meta: extraMeta,
35
39
  },
40
+ this.clientName,
36
41
  )
37
42
  }
38
43
 
@@ -45,6 +50,7 @@ export class MintService {
45
50
  'mint',
46
51
  'subscribe_reissue_external_notes',
47
52
  { operation_id: operationId },
53
+ this.clientName,
48
54
  onSuccess,
49
55
  onError,
50
56
  )
@@ -76,6 +82,7 @@ export class MintService {
76
82
  include_invite: includeInvite,
77
83
  extra_meta: extraMeta,
78
84
  },
85
+ this.clientName,
79
86
  )
80
87
  const notes = res[1]
81
88
  const operationId = res[0]
@@ -88,15 +95,25 @@ export class MintService {
88
95
 
89
96
  /** https://web.fedimint.org/core/FedimintWallet/MintService/parseEcash */
90
97
  async parseNotes(oobNotes: string) {
91
- return await this.client.rpcSingle<MSats>('mint', 'validate_notes', {
92
- oob_notes: oobNotes,
93
- })
98
+ return await this.client.rpcSingle<MSats>(
99
+ 'mint',
100
+ 'validate_notes',
101
+ {
102
+ oob_notes: oobNotes,
103
+ },
104
+ this.clientName,
105
+ )
94
106
  }
95
107
 
96
108
  async tryCancelSpendNotes(operationId: string) {
97
- await this.client.rpcSingle('mint', 'try_cancel_spend_notes', {
98
- operation_id: operationId,
99
- })
109
+ await this.client.rpcSingle(
110
+ 'mint',
111
+ 'try_cancel_spend_notes',
112
+ {
113
+ operation_id: operationId,
114
+ },
115
+ this.clientName,
116
+ )
100
117
  }
101
118
 
102
119
  subscribeSpendNotes(
@@ -108,15 +125,21 @@ export class MintService {
108
125
  'mint',
109
126
  'subscribe_spend_notes',
110
127
  { operation_id: operationId },
128
+ this.clientName,
111
129
  (res) => onSuccess(res),
112
130
  onError,
113
131
  )
114
132
  }
115
133
 
116
134
  async awaitSpendOobRefund(operationId: string) {
117
- return await this.client.rpcSingle('mint', 'await_spend_oob_refund', {
118
- operation_id: operationId,
119
- })
135
+ return await this.client.rpcSingle(
136
+ 'mint',
137
+ 'await_spend_oob_refund',
138
+ {
139
+ operation_id: operationId,
140
+ },
141
+ this.clientName,
142
+ )
120
143
  }
121
144
 
122
145
  async getNotesByDenomination() {
@@ -124,6 +147,7 @@ export class MintService {
124
147
  'mint',
125
148
  'note_counts_by_denomination',
126
149
  {},
150
+ this.clientName,
127
151
  )
128
152
  }
129
153
  }
@@ -2,18 +2,27 @@ import type { JSONValue } from '../types'
2
2
  import { TransportClient } from '../transport'
3
3
 
4
4
  export class RecoveryService {
5
- constructor(private client: TransportClient) {}
5
+ constructor(
6
+ private client: TransportClient,
7
+ private clientName: string,
8
+ ) {}
6
9
 
7
10
  async hasPendingRecoveries() {
8
11
  return await this.client.rpcSingle<boolean>(
9
12
  '',
10
13
  'has_pending_recoveries',
11
14
  {},
15
+ this.clientName,
12
16
  )
13
17
  }
14
18
 
15
19
  async waitForAllRecoveries() {
16
- await this.client.rpcSingle('', 'wait_for_all_recoveries', {})
20
+ await this.client.rpcSingle(
21
+ '',
22
+ 'wait_for_all_recoveries',
23
+ {},
24
+ this.clientName,
25
+ )
17
26
  }
18
27
 
19
28
  subscribeToRecoveryProgress(
@@ -23,6 +32,13 @@ export class RecoveryService {
23
32
  return this.client.rpcStream<{
24
33
  module_id: number
25
34
  progress: JSONValue
26
- }>('', 'subscribe_to_recovery_progress', {}, onSuccess, onError)
35
+ }>(
36
+ '',
37
+ 'subscribe_to_recovery_progress',
38
+ {},
39
+ this.clientName,
40
+ onSuccess,
41
+ onError,
42
+ )
27
43
  }
28
44
  }
@@ -7,10 +7,18 @@ import {
7
7
  import { TransportClient } from '../transport'
8
8
 
9
9
  export class WalletService {
10
- constructor(private client: TransportClient) {}
10
+ constructor(
11
+ private client: TransportClient,
12
+ private clientName: string,
13
+ ) {}
11
14
 
12
15
  async getWalletSummary(): Promise<WalletSummary> {
13
- return await this.client.rpcSingle('wallet', 'get_wallet_summary', {})
16
+ return await this.client.rpcSingle(
17
+ 'wallet',
18
+ 'get_wallet_summary',
19
+ {},
20
+ this.clientName,
21
+ )
14
22
  }
15
23
 
16
24
  async generateAddress(extraMeta: JSONValue = {}) {
@@ -20,6 +28,7 @@ export class WalletService {
20
28
  {
21
29
  extra_meta: extraMeta,
22
30
  },
31
+ this.clientName,
23
32
  )
24
33
  }
25
34
 
@@ -28,11 +37,16 @@ export class WalletService {
28
37
  address: string,
29
38
  extraMeta: JSONValue = {},
30
39
  ): Promise<{ operation_id: string }> {
31
- return await this.client.rpcSingle('wallet', 'peg_out', {
32
- amount_sat: amountSat,
33
- destination_address: address,
34
- extra_meta: extraMeta,
35
- })
40
+ return await this.client.rpcSingle(
41
+ 'wallet',
42
+ 'peg_out',
43
+ {
44
+ amount_sat: amountSat,
45
+ destination_address: address,
46
+ extra_meta: extraMeta,
47
+ },
48
+ this.clientName,
49
+ )
36
50
  }
37
51
  subscribeDeposit(
38
52
  operation_id: string,
@@ -43,6 +57,7 @@ export class WalletService {
43
57
  'ln',
44
58
  'subscribe_deposit',
45
59
  { operation_id: operation_id },
60
+ this.clientName,
46
61
  onSuccess,
47
62
  onError,
48
63
  )
package/src/testing.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Exports the FedimintWallet constructor only for testing.
2
+ // Not intended for use in production.
3
+ export { FedimintWallet } from './FedimintWallet'