@fedimint/core-web 0.0.11 → 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.
- package/dist/dts/FedimintWallet.d.ts +4 -62
- package/dist/dts/FedimintWallet.d.ts.map +1 -1
- package/dist/dts/WalletDirector.d.ts +78 -0
- package/dist/dts/WalletDirector.d.ts.map +1 -0
- package/dist/dts/index.d.ts +2 -1
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/services/BalanceService.d.ts +2 -2
- package/dist/dts/services/BalanceService.d.ts.map +1 -1
- package/dist/dts/services/FederationService.d.ts +7 -5
- package/dist/dts/services/FederationService.d.ts.map +1 -1
- package/dist/dts/services/LightningService.d.ts +4 -3
- package/dist/dts/services/LightningService.d.ts.map +1 -1
- package/dist/dts/services/MintService.d.ts +4 -3
- package/dist/dts/services/MintService.d.ts.map +1 -1
- package/dist/dts/services/RecoveryService.d.ts +2 -2
- package/dist/dts/services/RecoveryService.d.ts.map +1 -1
- package/dist/dts/services/WalletService.d.ts +13 -0
- package/dist/dts/services/WalletService.d.ts.map +1 -0
- package/dist/dts/services/index.d.ts +1 -0
- package/dist/dts/services/index.d.ts.map +1 -1
- package/dist/dts/{worker/WorkerClient.d.ts → transport/TransportClient.d.ts} +20 -9
- package/dist/dts/transport/TransportClient.d.ts.map +1 -0
- package/dist/dts/transport/index.d.ts +3 -0
- package/dist/dts/transport/index.d.ts.map +1 -0
- package/dist/dts/transport/wasmTransport/WasmWorkerTransport.d.ts +12 -0
- package/dist/dts/transport/wasmTransport/WasmWorkerTransport.d.ts.map +1 -0
- package/dist/dts/types/index.d.ts +1 -1
- package/dist/dts/types/index.d.ts.map +1 -1
- package/dist/dts/types/transport.d.ts +35 -0
- package/dist/dts/types/transport.d.ts.map +1 -0
- package/dist/dts/types/wallet.d.ts +159 -3
- package/dist/dts/types/wallet.d.ts.map +1 -1
- package/dist/dts/utils/logger.d.ts +10 -3
- package/dist/dts/utils/logger.d.ts.map +1 -1
- package/dist/index.d.ts +275 -29
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/worker.js +1 -1
- package/dist/worker.js.map +1 -1
- package/package.json +2 -2
- package/src/FedimintWallet.test.ts +13 -0
- package/src/FedimintWallet.ts +6 -91
- package/src/WalletDirector.ts +118 -0
- package/src/index.ts +2 -1
- package/src/services/BalanceService.test.ts +4 -28
- package/src/services/BalanceService.ts +2 -2
- package/src/services/FederationService.ts +197 -5
- package/src/services/LightningService.test.ts +57 -0
- package/src/services/LightningService.ts +17 -2
- package/src/services/MintService.test.ts +41 -0
- package/src/services/MintService.ts +11 -2
- package/src/services/RecoveryService.ts +2 -2
- package/src/services/WalletService.test.ts +59 -0
- package/src/services/WalletService.ts +50 -0
- package/src/services/index.ts +1 -0
- package/src/test/TestFedimintWallet.ts +13 -8
- package/src/test/TestWalletDirector.ts +14 -0
- package/src/test/TestingService.ts +8 -7
- package/src/test/fixtures.ts +35 -11
- package/src/transport/TransportClient.test.ts +6 -0
- package/src/{worker/WorkerClient.ts → transport/TransportClient.ts} +60 -45
- package/src/transport/index.ts +2 -0
- package/src/transport/wasmTransport/WasmWorkerTransport.ts +39 -0
- package/src/{worker → transport/wasmTransport}/worker.js +28 -0
- package/src/{worker → transport/wasmTransport}/worker.test.ts +3 -3
- package/src/types/index.ts +1 -1
- package/src/types/transport.ts +54 -0
- package/src/types/wallet.ts +186 -2
- package/src/utils/logger.ts +13 -5
- package/dist/dts/types/worker.d.ts +0 -4
- package/dist/dts/types/worker.d.ts.map +0 -1
- package/dist/dts/worker/WorkerClient.d.ts.map +0 -1
- package/dist/dts/worker/index.d.ts +0 -2
- package/dist/dts/worker/index.d.ts.map +0 -1
- package/src/types/worker.ts +0 -15
- package/src/worker/WorkerClient.test.ts +0 -6
- package/src/worker/index.ts +0 -1
package/src/test/fixtures.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import { expect, test } from 'vitest'
|
|
2
2
|
import { TestFedimintWallet } from './TestFedimintWallet'
|
|
3
|
-
import {
|
|
3
|
+
import { TransportClient } from '../transport/TransportClient'
|
|
4
|
+
import { WasmWorkerTransport } from '../transport/wasmTransport/WasmWorkerTransport'
|
|
5
|
+
import { TestWalletDirector } from './TestWalletDirector'
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
* Adds Fixtures for setting up and tearing down
|
|
8
|
+
* Adds Fixtures for setting up and tearing down test FedimintWallet/WalletDirector instances
|
|
7
9
|
*/
|
|
8
10
|
export const walletTest = test.extend<{
|
|
11
|
+
walletDirector: TestWalletDirector
|
|
9
12
|
wallet: TestFedimintWallet
|
|
10
13
|
fundedWallet: TestFedimintWallet
|
|
14
|
+
fundedWalletBeefy: TestFedimintWallet
|
|
15
|
+
unopenedWallet: TestFedimintWallet
|
|
11
16
|
}>({
|
|
12
|
-
|
|
17
|
+
walletDirector: async ({}, use) => {
|
|
18
|
+
const walletDirector = new TestWalletDirector()
|
|
19
|
+
await use(walletDirector)
|
|
20
|
+
},
|
|
21
|
+
wallet: async ({ walletDirector }, use) => {
|
|
13
22
|
const randomTestingId = Math.random().toString(36).substring(2, 15)
|
|
14
|
-
const wallet =
|
|
23
|
+
const wallet = await walletDirector.createTestWallet()
|
|
15
24
|
expect(wallet).toBeDefined()
|
|
16
25
|
const inviteCode = await wallet.testing.getInviteCode()
|
|
17
26
|
await expect(
|
|
@@ -33,9 +42,19 @@ export const walletTest = test.extend<{
|
|
|
33
42
|
},
|
|
34
43
|
|
|
35
44
|
fundedWallet: async ({ wallet }, use) => {
|
|
45
|
+
// 10K MSats
|
|
36
46
|
await wallet.fundWallet(10_000)
|
|
37
47
|
await use(wallet)
|
|
38
48
|
},
|
|
49
|
+
fundedWalletBeefy: async ({ wallet }, use) => {
|
|
50
|
+
// 1M MSats
|
|
51
|
+
await wallet.fundWallet(1_000_000)
|
|
52
|
+
await use(wallet)
|
|
53
|
+
},
|
|
54
|
+
unopenedWallet: async ({ walletDirector }, use) => {
|
|
55
|
+
const wallet = await walletDirector.createTestWallet()
|
|
56
|
+
await use(wallet)
|
|
57
|
+
},
|
|
39
58
|
})
|
|
40
59
|
|
|
41
60
|
/**
|
|
@@ -44,12 +63,15 @@ export const walletTest = test.extend<{
|
|
|
44
63
|
export const workerTest = test.extend<{
|
|
45
64
|
worker: Worker
|
|
46
65
|
clientName: string
|
|
47
|
-
|
|
66
|
+
transportClient: TransportClient
|
|
48
67
|
}>({
|
|
49
68
|
worker: async ({}, use) => {
|
|
50
|
-
const worker = new Worker(
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
const worker = new Worker(
|
|
70
|
+
new URL('../transport/wasmTransport/worker.js', import.meta.url),
|
|
71
|
+
{
|
|
72
|
+
type: 'module',
|
|
73
|
+
},
|
|
74
|
+
)
|
|
53
75
|
await use(worker)
|
|
54
76
|
worker.terminate()
|
|
55
77
|
},
|
|
@@ -57,8 +79,10 @@ export const workerTest = test.extend<{
|
|
|
57
79
|
const randomTestingId = Math.random().toString(36).substring(2, 15)
|
|
58
80
|
await use(randomTestingId)
|
|
59
81
|
},
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
82
|
+
transportClient: async ({}, use) => {
|
|
83
|
+
// TODO: figure out how to use a different transport in runtime depending on the test
|
|
84
|
+
// Ideally, we don't want to create separate fixtures for each transport
|
|
85
|
+
const transportClient = new TransportClient(new WasmWorkerTransport())
|
|
86
|
+
await use(transportClient)
|
|
63
87
|
},
|
|
64
88
|
})
|
|
@@ -4,27 +4,34 @@ import type {
|
|
|
4
4
|
ModuleKind,
|
|
5
5
|
StreamError,
|
|
6
6
|
StreamResult,
|
|
7
|
-
|
|
7
|
+
TransportMessageType,
|
|
8
8
|
} from '../types'
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
import { Logger } from '../utils/logger'
|
|
10
|
+
import type { Transport, TransportMessage } from '../types/transport'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Handles communication with a generic transport.
|
|
14
|
+
* Must be instantiated with a platform-specific transport. (wasm for web, react native, etc.)
|
|
15
|
+
*/
|
|
16
|
+
export class TransportClient {
|
|
17
|
+
// Generic Transport. Can be wasm, react native, node, etc.
|
|
18
|
+
private readonly transport: Transport
|
|
15
19
|
private requestCounter = 0
|
|
16
20
|
private requestCallbacks = new Map<number, (value: any) => void>()
|
|
17
21
|
private initPromise: Promise<boolean> | undefined = undefined
|
|
22
|
+
logger: Logger
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
/**
|
|
25
|
+
* @summary Constructor for the TransportClient
|
|
26
|
+
* @param transport - The platform-specific transport to use. (wasm for web, react native, etc.)
|
|
27
|
+
*/
|
|
28
|
+
constructor(transport: Transport) {
|
|
29
|
+
this.transport = transport
|
|
30
|
+
this.logger = new Logger(transport.logger)
|
|
31
|
+
this.transport.setMessageHandler(this.handleTransportMessage)
|
|
32
|
+
this.transport.setErrorHandler(this.handleTransportError)
|
|
33
|
+
this.logger.info('TransportClient instantiated')
|
|
34
|
+
this.logger.debug('TransportClient transport', transport)
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
// Idempotent setup - Loads the wasm module
|
|
@@ -34,30 +41,31 @@ export class WorkerClient {
|
|
|
34
41
|
return this.initPromise
|
|
35
42
|
}
|
|
36
43
|
|
|
37
|
-
private
|
|
38
|
-
const { type, level, message, ...data } =
|
|
39
|
-
logger.
|
|
44
|
+
private handleLogMessage(message: TransportMessage) {
|
|
45
|
+
const { type, level, message: logMessage, ...data } = message
|
|
46
|
+
this.logger.info(String(level), String(logMessage), data)
|
|
40
47
|
}
|
|
41
48
|
|
|
42
|
-
private
|
|
43
|
-
logger.error('
|
|
49
|
+
private handleTransportError = (error: unknown) => {
|
|
50
|
+
this.logger.error('TransportClient error', error)
|
|
44
51
|
}
|
|
45
52
|
|
|
46
|
-
private
|
|
47
|
-
const { type, requestId, ...data } =
|
|
53
|
+
private handleTransportMessage = (message: TransportMessage) => {
|
|
54
|
+
const { type, requestId, ...data } = message
|
|
48
55
|
if (type === 'log') {
|
|
49
|
-
this.
|
|
56
|
+
this.handleLogMessage(message)
|
|
50
57
|
}
|
|
51
|
-
const streamCallback =
|
|
58
|
+
const streamCallback =
|
|
59
|
+
requestId !== undefined ? this.requestCallbacks.get(requestId) : undefined
|
|
52
60
|
// TODO: Handle errors... maybe have another callbacks list for errors?
|
|
53
|
-
logger.debug('
|
|
61
|
+
this.logger.debug('TransportClient - handleTransportMessage', message)
|
|
54
62
|
if (streamCallback) {
|
|
55
63
|
streamCallback(data) // {data: something} OR {error: something}
|
|
56
|
-
} else {
|
|
57
|
-
logger.warn(
|
|
58
|
-
'
|
|
64
|
+
} else if (requestId !== undefined) {
|
|
65
|
+
this.logger.warn(
|
|
66
|
+
'TransportClient - handleTransportMessage - received message with no callback',
|
|
59
67
|
requestId,
|
|
60
|
-
|
|
68
|
+
message,
|
|
61
69
|
)
|
|
62
70
|
}
|
|
63
71
|
}
|
|
@@ -69,30 +77,35 @@ export class WorkerClient {
|
|
|
69
77
|
sendSingleMessage<
|
|
70
78
|
Response extends JSONValue = JSONValue,
|
|
71
79
|
Payload extends JSONValue = JSONValue,
|
|
72
|
-
>(type:
|
|
80
|
+
>(type: TransportMessageType, payload?: Payload) {
|
|
73
81
|
return new Promise<Response>((resolve, reject) => {
|
|
74
82
|
const requestId = ++this.requestCounter
|
|
75
|
-
logger.debug(
|
|
83
|
+
this.logger.debug(
|
|
84
|
+
'TransportClient - sendSingleMessage',
|
|
85
|
+
requestId,
|
|
86
|
+
type,
|
|
87
|
+
payload,
|
|
88
|
+
)
|
|
76
89
|
this.requestCallbacks.set(
|
|
77
90
|
requestId,
|
|
78
91
|
(response: StreamResult<Response>) => {
|
|
79
92
|
this.requestCallbacks.delete(requestId)
|
|
80
|
-
logger.debug(
|
|
81
|
-
'
|
|
93
|
+
this.logger.debug(
|
|
94
|
+
'TransportClient - sendSingleMessage - response',
|
|
82
95
|
requestId,
|
|
83
96
|
response,
|
|
84
97
|
)
|
|
85
98
|
if (response.data) resolve(response.data)
|
|
86
99
|
else if (response.error) reject(response.error)
|
|
87
100
|
else
|
|
88
|
-
logger.warn(
|
|
89
|
-
'
|
|
101
|
+
this.logger.warn(
|
|
102
|
+
'TransportClient - sendSingleMessage - malformed response',
|
|
90
103
|
requestId,
|
|
91
104
|
response,
|
|
92
105
|
)
|
|
93
106
|
},
|
|
94
107
|
)
|
|
95
|
-
this.
|
|
108
|
+
this.transport.postMessage({ type, payload, requestId })
|
|
96
109
|
})
|
|
97
110
|
}
|
|
98
111
|
|
|
@@ -132,7 +145,13 @@ export class WorkerClient {
|
|
|
132
145
|
onEnd: () => void = () => {},
|
|
133
146
|
): CancelFunction {
|
|
134
147
|
const requestId = ++this.requestCounter
|
|
135
|
-
logger.debug(
|
|
148
|
+
this.logger.debug(
|
|
149
|
+
'TransportClient - rpcStream',
|
|
150
|
+
requestId,
|
|
151
|
+
module,
|
|
152
|
+
method,
|
|
153
|
+
body,
|
|
154
|
+
)
|
|
136
155
|
let unsubscribe: (value: void) => void = () => {}
|
|
137
156
|
let isSubscribed = false
|
|
138
157
|
|
|
@@ -180,10 +199,6 @@ export class WorkerClient {
|
|
|
180
199
|
unsubscribePromise: Promise<void>,
|
|
181
200
|
// Unsubscribe function
|
|
182
201
|
) {
|
|
183
|
-
// await this.openPromise
|
|
184
|
-
// if (!this.worker || !this._isOpen)
|
|
185
|
-
// throw new Error('FedimintWallet is not open')
|
|
186
|
-
|
|
187
202
|
this.requestCallbacks.set(requestId, (response: StreamResult<Response>) => {
|
|
188
203
|
if (response.error !== undefined) {
|
|
189
204
|
onError(response.error)
|
|
@@ -194,14 +209,14 @@ export class WorkerClient {
|
|
|
194
209
|
onEnd()
|
|
195
210
|
}
|
|
196
211
|
})
|
|
197
|
-
this.
|
|
212
|
+
this.transport.postMessage({
|
|
198
213
|
type: 'rpc',
|
|
199
214
|
payload: { module, method, body },
|
|
200
215
|
requestId,
|
|
201
216
|
})
|
|
202
217
|
|
|
203
218
|
unsubscribePromise.then(() => {
|
|
204
|
-
this.
|
|
219
|
+
this.transport.postMessage({
|
|
205
220
|
type: 'unsubscribe',
|
|
206
221
|
requestId,
|
|
207
222
|
})
|
|
@@ -213,7 +228,7 @@ export class WorkerClient {
|
|
|
213
228
|
Response extends JSONValue = JSONValue,
|
|
214
229
|
Error extends string = string,
|
|
215
230
|
>(module: ModuleKind, method: string, body: JSONValue) {
|
|
216
|
-
logger.debug('
|
|
231
|
+
this.logger.debug('TransportClient - rpcSingle', module, method, body)
|
|
217
232
|
return new Promise<Response>((resolve, reject) => {
|
|
218
233
|
this.rpcStream<Response>(module, method, body, resolve, reject)
|
|
219
234
|
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Transport,
|
|
3
|
+
TransportErrorHandler,
|
|
4
|
+
TransportLogger,
|
|
5
|
+
TransportMessageHandler,
|
|
6
|
+
TransportRequest,
|
|
7
|
+
} from '../../types/transport'
|
|
8
|
+
|
|
9
|
+
export class WasmWorkerTransport implements Transport {
|
|
10
|
+
private messageHandler: TransportMessageHandler = () => {}
|
|
11
|
+
private errorHandler: TransportErrorHandler = () => {}
|
|
12
|
+
private readonly worker: Worker
|
|
13
|
+
|
|
14
|
+
logger: TransportLogger = console
|
|
15
|
+
|
|
16
|
+
constructor() {
|
|
17
|
+
this.worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
18
|
+
type: 'module',
|
|
19
|
+
})
|
|
20
|
+
this.worker.onmessage = (event: MessageEvent) => {
|
|
21
|
+
this.messageHandler(event.data)
|
|
22
|
+
}
|
|
23
|
+
this.worker.onerror = (event: ErrorEvent) => {
|
|
24
|
+
this.errorHandler(event)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
postMessage(message: TransportRequest) {
|
|
29
|
+
this.worker.postMessage(message)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setMessageHandler(handler: TransportMessageHandler) {
|
|
33
|
+
this.messageHandler = handler
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setErrorHandler(handler: TransportErrorHandler) {
|
|
37
|
+
this.errorHandler = handler
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -17,6 +17,17 @@ const handleFree = (requestId) => {
|
|
|
17
17
|
|
|
18
18
|
console.log('Worker - init')
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Type definitions for the worker messages
|
|
22
|
+
*
|
|
23
|
+
* @typedef {import('../../types/transport').TransportMessageType} WorkerMessageType
|
|
24
|
+
* @typedef {{
|
|
25
|
+
* type: WorkerMessageType
|
|
26
|
+
* payload: any
|
|
27
|
+
* requestId: number
|
|
28
|
+
* }} WorkerMessage
|
|
29
|
+
* @param {{data: WorkerMessage}} event
|
|
30
|
+
*/
|
|
20
31
|
self.onmessage = async (event) => {
|
|
21
32
|
const { type, payload, requestId } = event.data
|
|
22
33
|
|
|
@@ -45,6 +56,23 @@ self.onmessage = async (event) => {
|
|
|
45
56
|
} catch (e) {
|
|
46
57
|
self.postMessage({ type: 'error', error: e.message, requestId })
|
|
47
58
|
}
|
|
59
|
+
} else if (type === 'previewFederation') {
|
|
60
|
+
const { inviteCode } = payload
|
|
61
|
+
try {
|
|
62
|
+
client = await WasmClient.preview_federation(inviteCode)
|
|
63
|
+
const parsed = JSON.parse(client)
|
|
64
|
+
self.postMessage({
|
|
65
|
+
type: 'previewFederation',
|
|
66
|
+
data: {
|
|
67
|
+
success: !!client,
|
|
68
|
+
config: parsed.config,
|
|
69
|
+
federation_id: parsed.federation_id,
|
|
70
|
+
},
|
|
71
|
+
requestId,
|
|
72
|
+
})
|
|
73
|
+
} catch (e) {
|
|
74
|
+
self.postMessage({ type: 'error', error: e.message, requestId })
|
|
75
|
+
}
|
|
48
76
|
} else if (type === 'rpc') {
|
|
49
77
|
const { module, method, body } = payload
|
|
50
78
|
console.log('RPC received', module, method, body)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { expect } from 'vitest'
|
|
2
|
-
import { TESTING_INVITE } from '
|
|
3
|
-
import { JSONObject } from '
|
|
4
|
-
import { workerTest } from '
|
|
2
|
+
import { TESTING_INVITE } from '../../test/TestingService'
|
|
3
|
+
import { JSONObject } from '../../types'
|
|
4
|
+
import { workerTest } from '../../test/fixtures'
|
|
5
5
|
|
|
6
6
|
// Waits for a message of a given type from the worker
|
|
7
7
|
const waitForWorkerResponse = (
|
package/src/types/index.ts
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { JSONValue } from './utils'
|
|
2
|
+
|
|
3
|
+
const TransportMessageTypes = [
|
|
4
|
+
'init',
|
|
5
|
+
'initialized',
|
|
6
|
+
'rpc',
|
|
7
|
+
'log',
|
|
8
|
+
'open',
|
|
9
|
+
'join',
|
|
10
|
+
'error',
|
|
11
|
+
'unsubscribe',
|
|
12
|
+
'cleanup',
|
|
13
|
+
'parseInviteCode',
|
|
14
|
+
'parseBolt11Invoice',
|
|
15
|
+
'previewFederation',
|
|
16
|
+
] as const
|
|
17
|
+
|
|
18
|
+
export type TransportMessageType = (typeof TransportMessageTypes)[number]
|
|
19
|
+
|
|
20
|
+
export type TransportRequest = {
|
|
21
|
+
type: TransportMessageType
|
|
22
|
+
requestId?: number
|
|
23
|
+
payload?: JSONValue
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type TransportMessage = {
|
|
27
|
+
type: TransportMessageType | string
|
|
28
|
+
requestId?: number
|
|
29
|
+
} & Record<string, unknown>
|
|
30
|
+
|
|
31
|
+
export type TransportMessageHandler = (message: TransportMessage) => void
|
|
32
|
+
|
|
33
|
+
export type TransportErrorHandler = (error: unknown) => void
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Generic Transport interface for communicating with a specific
|
|
37
|
+
* target of the FedimintClient. Can be Wasm, React Native, Node, etc.
|
|
38
|
+
*/
|
|
39
|
+
export interface Transport {
|
|
40
|
+
postMessage(message: TransportRequest): void
|
|
41
|
+
setMessageHandler(handler: TransportMessageHandler): void
|
|
42
|
+
setErrorHandler(handler: TransportErrorHandler): void
|
|
43
|
+
logger: TransportLogger
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Generic logger interface based on browser console.log
|
|
48
|
+
*/
|
|
49
|
+
export type TransportLogger = {
|
|
50
|
+
debug(message: string, ...args: any[]): void
|
|
51
|
+
info(message: string, ...args: any[]): void
|
|
52
|
+
warn(message: string, ...args: any[]): void
|
|
53
|
+
error(message: string, ...args: any[]): void
|
|
54
|
+
}
|
package/src/types/wallet.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { MSats, Duration, JSONValue } from './utils'
|
|
1
|
+
import { MSats, Duration, JSONValue, JSONObject } from './utils'
|
|
2
2
|
|
|
3
|
-
const MODULE_KINDS = ['', 'ln', 'mint'] as const
|
|
3
|
+
const MODULE_KINDS = ['', 'ln', 'mint', 'wallet'] as const
|
|
4
4
|
type ModuleKind = (typeof MODULE_KINDS)[number]
|
|
5
|
+
|
|
6
|
+
// TODO: Define the structure of FederationConfig
|
|
7
|
+
type FederationConfig = JSONObject
|
|
8
|
+
|
|
5
9
|
type GatewayInfo = {
|
|
6
10
|
gateway_id: string
|
|
7
11
|
api: string
|
|
@@ -50,6 +54,14 @@ type LnReceiveState =
|
|
|
50
54
|
| 'awaiting_funds'
|
|
51
55
|
| 'claimed'
|
|
52
56
|
|
|
57
|
+
type LnInternalPayState =
|
|
58
|
+
| 'funding'
|
|
59
|
+
| { preimage: string }
|
|
60
|
+
| { refund_success: { out_points: BtcOutPoint[]; error: string } }
|
|
61
|
+
| { refund_error: { error_message: string; error: string } }
|
|
62
|
+
| { funding_failed: { error: string } }
|
|
63
|
+
| { unexpected_error: string }
|
|
64
|
+
|
|
53
65
|
type CreateBolt11Response = {
|
|
54
66
|
operation_id: string
|
|
55
67
|
invoice: string
|
|
@@ -93,8 +105,165 @@ type SpendNotesState =
|
|
|
93
105
|
| 'Success'
|
|
94
106
|
| 'Refunded'
|
|
95
107
|
|
|
108
|
+
type TxOutputSummary = {
|
|
109
|
+
outpoint: {
|
|
110
|
+
txid: string
|
|
111
|
+
vout: number
|
|
112
|
+
}
|
|
113
|
+
amount: number
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type BtcOutPoint = {
|
|
117
|
+
txid: string
|
|
118
|
+
vout: number
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type WalletDepositState =
|
|
122
|
+
| 'WaitingForTransaction'
|
|
123
|
+
| {
|
|
124
|
+
WaitingForConfirmation: {
|
|
125
|
+
btc_deposited: number
|
|
126
|
+
btc_out_point: BtcOutPoint
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
| { Confirmed: { btc_deposited: number; btc_out_point: BtcOutPoint } }
|
|
130
|
+
| { Claimed: { btc_deposited: number; btc_out_point: BtcOutPoint } }
|
|
131
|
+
| { Failed: string }
|
|
132
|
+
|
|
133
|
+
type WalletSummary = {
|
|
134
|
+
spendable_utxos: TxOutputSummary[]
|
|
135
|
+
unsigned_peg_out_txos: TxOutputSummary[]
|
|
136
|
+
unsigned_change_utxos: TxOutputSummary[]
|
|
137
|
+
// TODO: fix typo in rust
|
|
138
|
+
unconfirmed_peg_out_txos: TxOutputSummary[]
|
|
139
|
+
unconfirmed_change_utxos: TxOutputSummary[]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
type LnVariant = {
|
|
143
|
+
pay?: {
|
|
144
|
+
gateway_id: string
|
|
145
|
+
invoice: string
|
|
146
|
+
fee: number
|
|
147
|
+
is_internal_payment: boolean
|
|
148
|
+
out_point: {
|
|
149
|
+
out_idx: number
|
|
150
|
+
txid: string
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
receive?: {
|
|
154
|
+
gateway_id: string
|
|
155
|
+
invoice: string
|
|
156
|
+
out_point: {
|
|
157
|
+
out_idx: number
|
|
158
|
+
txid: string
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type MintVariant = {
|
|
164
|
+
spend_o_o_b?: {
|
|
165
|
+
requested_amount: number
|
|
166
|
+
oob_notes: string
|
|
167
|
+
}
|
|
168
|
+
reissuance?: {
|
|
169
|
+
txid: string
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
type WalletVariant = {
|
|
174
|
+
deposit?: {
|
|
175
|
+
address: string
|
|
176
|
+
tweak_idx: number
|
|
177
|
+
}
|
|
178
|
+
withdraw?: {
|
|
179
|
+
address: string
|
|
180
|
+
amountMsats: number
|
|
181
|
+
fee: {
|
|
182
|
+
fee_rate: {
|
|
183
|
+
sats_per_kvb: number
|
|
184
|
+
}
|
|
185
|
+
total_weight: number
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
type OperationKey = {
|
|
191
|
+
creation_time: { nanos_since_epoch: number; secs_since_epoch: number }
|
|
192
|
+
operation_id: string
|
|
193
|
+
}
|
|
194
|
+
type OperationMeta = {
|
|
195
|
+
amount: number
|
|
196
|
+
extra_meta: JSONObject
|
|
197
|
+
variant: LnVariant | MintVariant | WalletVariant
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
type OperationLog = {
|
|
201
|
+
meta: OperationMeta
|
|
202
|
+
operation_module_kind: string
|
|
203
|
+
outcome: {
|
|
204
|
+
outcome: LnPayState | LnReceiveState | SpendNotesState | WalletDepositState
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
type BaseTransactions = {
|
|
209
|
+
timestamp: number
|
|
210
|
+
operationId: string
|
|
211
|
+
kind: 'ln' | 'mint' | 'wallet'
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
type LightningTransaction = BaseTransactions & {
|
|
215
|
+
type: 'send' | 'receive'
|
|
216
|
+
invoice: string
|
|
217
|
+
outcome:
|
|
218
|
+
| 'created'
|
|
219
|
+
| 'canceled'
|
|
220
|
+
| 'claimed'
|
|
221
|
+
| 'pending'
|
|
222
|
+
| 'success'
|
|
223
|
+
| 'funded'
|
|
224
|
+
| 'awaiting_funds'
|
|
225
|
+
| 'unexpected_error'
|
|
226
|
+
gateway: string
|
|
227
|
+
fee?: number
|
|
228
|
+
internalPay?: boolean
|
|
229
|
+
preimage?: string
|
|
230
|
+
txId: string
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
type EcashTransaction = BaseTransactions & {
|
|
234
|
+
type: 'spend_oob' | 'reissue'
|
|
235
|
+
amountMsats: number
|
|
236
|
+
outcome?: SpendNotesState | ReissueExternalNotesState
|
|
237
|
+
notes?: string
|
|
238
|
+
txId?: string
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
type WalletTransaction = BaseTransactions & {
|
|
242
|
+
type: 'withdraw' | 'deposit'
|
|
243
|
+
onchainAddress: string
|
|
244
|
+
amountMsats: number
|
|
245
|
+
fee: number
|
|
246
|
+
outcome?:
|
|
247
|
+
| 'WaitingForTransaction'
|
|
248
|
+
| 'WaitingForConfirmation'
|
|
249
|
+
| 'Confirmed'
|
|
250
|
+
| 'Claimed'
|
|
251
|
+
| 'Failed'
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
type Transactions = LightningTransaction | EcashTransaction | WalletTransaction
|
|
255
|
+
|
|
256
|
+
/** Keys are powers of 2 */
|
|
257
|
+
type NoteCountByDenomination = Record<number, number>
|
|
258
|
+
|
|
259
|
+
type GenerateAddressResponse = {
|
|
260
|
+
deposit_address: string
|
|
261
|
+
operation_id: string
|
|
262
|
+
}
|
|
263
|
+
|
|
96
264
|
export {
|
|
97
265
|
LightningGateway,
|
|
266
|
+
FederationConfig,
|
|
98
267
|
RouteHint,
|
|
99
268
|
FeeToAmount,
|
|
100
269
|
OutgoingLightningPayment,
|
|
@@ -108,7 +277,22 @@ export {
|
|
|
108
277
|
StreamResult,
|
|
109
278
|
ModuleKind,
|
|
110
279
|
CancelFunction,
|
|
280
|
+
LnInternalPayState,
|
|
111
281
|
ReissueExternalNotesState,
|
|
112
282
|
MintSpendNotesResponse,
|
|
113
283
|
SpendNotesState,
|
|
284
|
+
WalletSummary,
|
|
285
|
+
TxOutputSummary,
|
|
286
|
+
NoteCountByDenomination,
|
|
287
|
+
GenerateAddressResponse,
|
|
288
|
+
OperationKey,
|
|
289
|
+
OperationLog,
|
|
290
|
+
LnVariant,
|
|
291
|
+
MintVariant,
|
|
292
|
+
WalletVariant,
|
|
293
|
+
LightningTransaction,
|
|
294
|
+
EcashTransaction,
|
|
295
|
+
WalletTransaction,
|
|
296
|
+
Transactions,
|
|
297
|
+
WalletDepositState,
|
|
114
298
|
}
|