@fedimint/core-web 0.0.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/README.md +42 -0
- package/dist/FedimintWallet.d.ts +19 -0
- package/dist/FedimintWallet.d.ts.map +1 -0
- package/dist/FedimintWallet.js +106 -0
- package/dist/FedimintWallet.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/wasm.worker.d.ts +1 -0
- package/dist/wasm.worker.d.ts.map +1 -0
- package/dist/wasm.worker.js +37 -0
- package/dist/wasm.worker.js.map +1 -0
- package/package.json +32 -0
- package/src/FedimintWallet.ts +142 -0
- package/src/index.ts +3 -0
- package/src/vite-env.d.ts +1 -0
- package/src/wasm.worker.ts +41 -0
- package/wasm/README.md +169 -0
- package/wasm/fedimint_client_wasm.d.ts +94 -0
- package/wasm/fedimint_client_wasm.js +1181 -0
- package/wasm/fedimint_client_wasm_bg.js +873 -0
- package/wasm/fedimint_client_wasm_bg.wasm +0 -0
- package/wasm/fedimint_client_wasm_bg.wasm.d.ts +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Fedimint Client Typescript (Web)
|
|
2
|
+
|
|
3
|
+
### THIS IS A WORK IN PROGRESS AND NOT READY FOR USE
|
|
4
|
+
|
|
5
|
+
This package provides a typescript interface for the Fedimint client in the browser
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install core-web
|
|
11
|
+
# or
|
|
12
|
+
yarn add core-web
|
|
13
|
+
# or
|
|
14
|
+
pnpm add core-web
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { FedimintWallet } from 'core-web'
|
|
21
|
+
|
|
22
|
+
// federation invite code
|
|
23
|
+
const inviteCode = 'fed11qgqpw9thwvaz7t...'
|
|
24
|
+
|
|
25
|
+
// This should be called only once
|
|
26
|
+
await FedimintWallet.initFedimint()
|
|
27
|
+
|
|
28
|
+
const wallet = await FedimintWallet.joinFederation(inviteCode)
|
|
29
|
+
|
|
30
|
+
// Get Wallet Balance
|
|
31
|
+
const balance = await wallet.getBalance()
|
|
32
|
+
|
|
33
|
+
// Receive Ecash Payments
|
|
34
|
+
await wallet.reissueNotes('A11qgqpw9thwvaz7t...')
|
|
35
|
+
|
|
36
|
+
// Pay Lightning Invoice
|
|
37
|
+
await wallet.payInvoice('lnbc...')
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Check out the example
|
|
41
|
+
|
|
42
|
+
[`examples/vite-core`](../examples/vite-core/README.md)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare class FedimintWallet {
|
|
2
|
+
private _fed;
|
|
3
|
+
private initPromise;
|
|
4
|
+
private openPromise;
|
|
5
|
+
private resolveOpen;
|
|
6
|
+
constructor(lazy?: boolean);
|
|
7
|
+
initialize(): Promise<void>;
|
|
8
|
+
open(clientName?: string): Promise<boolean>;
|
|
9
|
+
joinFederation(inviteCode: string, clientName?: string): Promise<void>;
|
|
10
|
+
private _rpcStream;
|
|
11
|
+
private _rpcSingle;
|
|
12
|
+
getBalance(): Promise<number>;
|
|
13
|
+
payInvoice(invoice: string): Promise<void>;
|
|
14
|
+
redeemEcash(notes: string): Promise<void>;
|
|
15
|
+
cleanup(): Promise<void>;
|
|
16
|
+
isOpen(): boolean;
|
|
17
|
+
subscribeBalance(callback: (balance: number) => void): () => void;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=FedimintWallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FedimintWallet.d.ts","sourceRoot":"","sources":["../src/FedimintWallet.ts"],"names":[],"mappings":"AAkBA,qBAAa,cAAc;IACzB,OAAO,CAAC,IAAI,CAA0B;IACtC,OAAO,CAAC,WAAW,CAA6B;IAChD,OAAO,CAAC,WAAW,CAA6B;IAChD,OAAO,CAAC,WAAW,CAAuB;gBAG9B,IAAI,GAAE,OAAe;IAS3B,UAAU;IASV,IAAI,CAAC,UAAU,GAAE,MAA4B;IAU7C,cAAc,CAClB,UAAU,EAAE,MAAM,EAClB,UAAU,GAAE,MAA4B;YAQ5B,UAAU;YAWV,UAAU;IAiBlB,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;IAM7B,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB1C,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,OAAO;IAIb,MAAM;IAMN,gBAAgB,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI;CAUrD"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import init, { WasmClient } from '../wasm/fedimint_client_wasm.js';
|
|
2
|
+
const DEFAULT_CLIENT_NAME = 'fm-default';
|
|
3
|
+
export class FedimintWallet {
|
|
4
|
+
// private worker: Worker | null = null
|
|
5
|
+
constructor(lazy = false) {
|
|
6
|
+
this._fed = null;
|
|
7
|
+
this.initPromise = null;
|
|
8
|
+
this.openPromise = null;
|
|
9
|
+
this.resolveOpen = () => { };
|
|
10
|
+
if (lazy)
|
|
11
|
+
return;
|
|
12
|
+
this.initialize();
|
|
13
|
+
this.openPromise = new Promise((resolve) => {
|
|
14
|
+
this.resolveOpen = resolve;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
// Setup
|
|
18
|
+
async initialize() {
|
|
19
|
+
if (this.initPromise)
|
|
20
|
+
return this.initPromise;
|
|
21
|
+
// this.worker = new Worker(new URL('./wasm.worker.ts', import.meta.url))
|
|
22
|
+
this.initPromise = init().then(() => {
|
|
23
|
+
console.trace('Fedimint Client Wasm Initialization complete');
|
|
24
|
+
});
|
|
25
|
+
return this.initPromise;
|
|
26
|
+
}
|
|
27
|
+
async open(clientName = DEFAULT_CLIENT_NAME) {
|
|
28
|
+
await this.initialize();
|
|
29
|
+
const wasm = await WasmClient.open(clientName);
|
|
30
|
+
if (wasm === undefined)
|
|
31
|
+
return false;
|
|
32
|
+
this._fed = wasm;
|
|
33
|
+
this.resolveOpen();
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
async joinFederation(inviteCode, clientName = DEFAULT_CLIENT_NAME) {
|
|
37
|
+
await this.initialize();
|
|
38
|
+
this._fed = await WasmClient.join_federation(clientName, inviteCode);
|
|
39
|
+
this.resolveOpen();
|
|
40
|
+
}
|
|
41
|
+
// RPC
|
|
42
|
+
async _rpcStream(module, method, body = {}, cb) {
|
|
43
|
+
await this.openPromise;
|
|
44
|
+
if (!this._fed)
|
|
45
|
+
throw new Error('FedimintWallet is not open');
|
|
46
|
+
await this._fed.rpc(module, method, JSON.stringify(body), cb);
|
|
47
|
+
}
|
|
48
|
+
async _rpcSingle(module, method, body = {}) {
|
|
49
|
+
// console.warn('RPC', module, method, body)
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
if (!this._fed)
|
|
52
|
+
return reject('FedimintWallet is not open');
|
|
53
|
+
this._fed.rpc(module, method, JSON.stringify(body), (res) => {
|
|
54
|
+
const parsed = JSON.parse(res);
|
|
55
|
+
if (parsed.error) {
|
|
56
|
+
reject(parsed.error);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
resolve(parsed);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
// Client
|
|
65
|
+
async getBalance() {
|
|
66
|
+
return (await this._rpcSingle('', 'get_balance'));
|
|
67
|
+
}
|
|
68
|
+
// LN
|
|
69
|
+
async payInvoice(invoice) {
|
|
70
|
+
await this._rpcSingle('ln', 'pay_bolt11_invoice', {
|
|
71
|
+
invoice,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// async listGateways() {
|
|
75
|
+
// return await this._rpcSingle("ln", "list_gateways");
|
|
76
|
+
// }
|
|
77
|
+
//
|
|
78
|
+
// async verifyGateways() {
|
|
79
|
+
// return await this._rpcSingle("ln", "verify_gateway_availability");
|
|
80
|
+
// }
|
|
81
|
+
// Mint
|
|
82
|
+
async redeemEcash(notes) {
|
|
83
|
+
await this._rpcSingle('mint', 'reissue_external_notes', {
|
|
84
|
+
oob_notes: notes, // "out of band notes"
|
|
85
|
+
extra_meta: null,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Teardown
|
|
89
|
+
async cleanup() {
|
|
90
|
+
await this._fed?.free();
|
|
91
|
+
}
|
|
92
|
+
isOpen() {
|
|
93
|
+
return this._fed !== null;
|
|
94
|
+
}
|
|
95
|
+
// Streaming
|
|
96
|
+
subscribeBalance(callback) {
|
|
97
|
+
this._rpcStream('', 'subscribe_balance_changes', {}, (res) => {
|
|
98
|
+
callback(res);
|
|
99
|
+
});
|
|
100
|
+
// TODO: implement unsubscribe on wasm side
|
|
101
|
+
return () => {
|
|
102
|
+
// no-op (fake unsubscribe)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=FedimintWallet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FedimintWallet.js","sourceRoot":"","sources":["../src/FedimintWallet.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAgBlE,MAAM,mBAAmB,GAAG,YAAqB,CAAA;AAEjD,MAAM,OAAO,cAAc;IAKzB,uCAAuC;IAEvC,YAAY,OAAgB,KAAK;QANzB,SAAI,GAAsB,IAAI,CAAA;QAC9B,gBAAW,GAAyB,IAAI,CAAA;QACxC,gBAAW,GAAyB,IAAI,CAAA;QACxC,gBAAW,GAAe,GAAG,EAAE,GAAE,CAAC,CAAA;QAIxC,IAAI,IAAI;YAAE,OAAM;QAChB,IAAI,CAAC,UAAU,EAAE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AC,KAAa;QAC7B,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,wBAAwB,EAAE;YACtD,SAAS,EAAE,KAAK,EAAE,sBAAsB;YACxC,UAAU,EAAE,IAAI;SACjB,CAAC,CAAA;IACJ,CAAC;IAED,WAAW;IACX,KAAK,CAAC,OAAO;QACX,MAAM,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,CAAA;IACzB,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,CAAA;IAC3B,CAAC;IAED,YAAY;IAEZ,gBAAgB,CAAC,QAAmC;QAClD,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,2BAA2B,EAAE,EAAE,EAAE,CAAC,GAAQ,EAAE,EAAE;YAChE,QAAQ,CAAC,GAAG,CAAC,CAAA;QACf,CAAC,CAAC,CAAA;QAEF,2CAA2C;QAC3C,OAAO,GAAG,EAAE;YACV,2BAA2B;QAC7B,CAAC,CAAA;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,EAAE,cAAc,EAAE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAEpD,OAAO,EAAE,cAAc,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=wasm.worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm.worker.d.ts","sourceRoot":"","sources":["../src/wasm.worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import init, { InitOutput, WasmClient } from '../wasm/fedimint_client_wasm.js'
|
|
3
|
+
// let client: WasmClient | undefined
|
|
4
|
+
// async function workerInit() {
|
|
5
|
+
// await init(new URL('@fedi/common/wasm/fedi_wasm_bg.wasm', import.meta.url))
|
|
6
|
+
// }
|
|
7
|
+
// const initPromise = workerInit().catch((error) =>
|
|
8
|
+
// postMessage({ error: String(error) }),
|
|
9
|
+
// )
|
|
10
|
+
// async function rpcRequest(method: string, data: string): Promise<string> {
|
|
11
|
+
// await initPromise
|
|
12
|
+
// return await fedimint_rpc(method, data)
|
|
13
|
+
// }
|
|
14
|
+
// const handleMessage = async (token: string, method: string, data: string) => {
|
|
15
|
+
// if (method === 'open') {
|
|
16
|
+
// await initPromise
|
|
17
|
+
// client = await WasmClient.open(data)
|
|
18
|
+
// if (client !== undefined) {
|
|
19
|
+
// postMessage({ token, result: true })
|
|
20
|
+
// } else {
|
|
21
|
+
// postMessage({ token, result: false })
|
|
22
|
+
// }
|
|
23
|
+
// } else if (method === 'join_federation') {
|
|
24
|
+
// await initPromise
|
|
25
|
+
// const result = await WasmClient.join_federation(data)
|
|
26
|
+
// postMessage({ token, result })
|
|
27
|
+
// }
|
|
28
|
+
// }
|
|
29
|
+
// // Handles worker.postMessage calls
|
|
30
|
+
// addEventListener('message', (e) => {
|
|
31
|
+
// const { token, method, data } = e.data
|
|
32
|
+
// handleMessage(token, method, data)
|
|
33
|
+
// // rpcRequest(method, data)
|
|
34
|
+
// // .then((result) => postMessage({ token, result }))
|
|
35
|
+
// // .catch((error) => postMessage({ error: String(error) }))
|
|
36
|
+
// })
|
|
37
|
+
//# sourceMappingURL=wasm.worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm.worker.js","sourceRoot":"","sources":["../src/wasm.worker.ts"],"names":[],"mappings":";AAAA,iFAAiF;AAEjF,qCAAqC;AAErC,gCAAgC;AAChC,gFAAgF;AAChF,IAAI;AAEJ,oDAAoD;AACpD,2CAA2C;AAC3C,IAAI;AAEJ,6EAA6E;AAC7E,sBAAsB;AACtB,4CAA4C;AAC5C,IAAI;AAEJ,iFAAiF;AACjF,6BAA6B;AAC7B,wBAAwB;AACxB,2CAA2C;AAC3C,kCAAkC;AAClC,6CAA6C;AAC7C,eAAe;AACf,8CAA8C;AAC9C,QAAQ;AACR,+CAA+C;AAC/C,wBAAwB;AACxB,4DAA4D;AAC5D,qCAAqC;AACrC,MAAM;AACN,IAAI;AAEJ,sCAAsC;AACtC,uCAAuC;AACvC,2CAA2C;AAC3C,uCAAuC;AACvC,gCAAgC;AAChC,2DAA2D;AAC3D,kEAAkE;AAClE,KAAK"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedimint/core-web",
|
|
3
|
+
"description": "Library for building web apps with a fedimint client",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/fedimint/fedimint-web-sdk.git",
|
|
8
|
+
"directory": "packages/core-web"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"wasm",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^20.14.8",
|
|
21
|
+
"typescript": "^5.2.2"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "pnpm run clean && tsc --project tsconfig.json",
|
|
28
|
+
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
29
|
+
"clean:deep": "pnpm run clean && rm -rf node_modules",
|
|
30
|
+
"typecheck": "tsc --noEmit"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import init, { WasmClient } from '../wasm/fedimint_client_wasm.js'
|
|
2
|
+
|
|
3
|
+
type StreamError = {
|
|
4
|
+
error: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// type StreamSuccess = {
|
|
8
|
+
// data: {} | [] | null | undefined | number | string | boolean
|
|
9
|
+
// }
|
|
10
|
+
|
|
11
|
+
type StreamSuccess = any & { error: never }
|
|
12
|
+
|
|
13
|
+
type StreamResult = StreamSuccess | StreamError
|
|
14
|
+
|
|
15
|
+
type Body = string | null | Record<string, string | null>
|
|
16
|
+
|
|
17
|
+
const DEFAULT_CLIENT_NAME = 'fm-default' as const
|
|
18
|
+
|
|
19
|
+
export class FedimintWallet {
|
|
20
|
+
private _fed: WasmClient | null = null
|
|
21
|
+
private initPromise: Promise<void> | null = null
|
|
22
|
+
private openPromise: Promise<void> | null = null
|
|
23
|
+
private resolveOpen: () => void = () => {}
|
|
24
|
+
// private worker: Worker | null = null
|
|
25
|
+
|
|
26
|
+
constructor(lazy: boolean = false) {
|
|
27
|
+
if (lazy) return
|
|
28
|
+
this.initialize()
|
|
29
|
+
this.openPromise = new Promise((resolve) => {
|
|
30
|
+
this.resolveOpen = resolve
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Setup
|
|
35
|
+
async initialize() {
|
|
36
|
+
if (this.initPromise) return this.initPromise
|
|
37
|
+
// this.worker = new Worker(new URL('./wasm.worker.ts', import.meta.url))
|
|
38
|
+
this.initPromise = init().then(() => {
|
|
39
|
+
console.trace('Fedimint Client Wasm Initialization complete')
|
|
40
|
+
})
|
|
41
|
+
return this.initPromise
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async open(clientName: string = DEFAULT_CLIENT_NAME) {
|
|
45
|
+
await this.initialize()
|
|
46
|
+
const wasm = await WasmClient.open(clientName)
|
|
47
|
+
|
|
48
|
+
if (wasm === undefined) return false
|
|
49
|
+
this._fed = wasm
|
|
50
|
+
this.resolveOpen()
|
|
51
|
+
return true
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async joinFederation(
|
|
55
|
+
inviteCode: string,
|
|
56
|
+
clientName: string = DEFAULT_CLIENT_NAME,
|
|
57
|
+
) {
|
|
58
|
+
await this.initialize()
|
|
59
|
+
this._fed = await WasmClient.join_federation(clientName, inviteCode)
|
|
60
|
+
this.resolveOpen()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// RPC
|
|
64
|
+
private async _rpcStream(
|
|
65
|
+
module: string,
|
|
66
|
+
method: string,
|
|
67
|
+
body: Body = {},
|
|
68
|
+
cb: (res: string) => void,
|
|
69
|
+
) {
|
|
70
|
+
await this.openPromise
|
|
71
|
+
if (!this._fed) throw new Error('FedimintWallet is not open')
|
|
72
|
+
await this._fed.rpc(module, method, JSON.stringify(body), cb)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private async _rpcSingle(module: string, method: string, body: Body = {}) {
|
|
76
|
+
// console.warn('RPC', module, method, body)
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
if (!this._fed) return reject('FedimintWallet is not open')
|
|
79
|
+
this._fed.rpc(module, method, JSON.stringify(body), (res: string) => {
|
|
80
|
+
const parsed = JSON.parse(res)
|
|
81
|
+
if (parsed.error) {
|
|
82
|
+
reject(parsed.error)
|
|
83
|
+
} else {
|
|
84
|
+
resolve(parsed)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Client
|
|
91
|
+
|
|
92
|
+
async getBalance(): Promise<number> {
|
|
93
|
+
return (await this._rpcSingle('', 'get_balance')) as number
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// LN
|
|
97
|
+
|
|
98
|
+
async payInvoice(invoice: string): Promise<void> {
|
|
99
|
+
await this._rpcSingle('ln', 'pay_bolt11_invoice', {
|
|
100
|
+
invoice,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// async listGateways() {
|
|
105
|
+
// return await this._rpcSingle("ln", "list_gateways");
|
|
106
|
+
// }
|
|
107
|
+
//
|
|
108
|
+
// async verifyGateways() {
|
|
109
|
+
// return await this._rpcSingle("ln", "verify_gateway_availability");
|
|
110
|
+
// }
|
|
111
|
+
|
|
112
|
+
// Mint
|
|
113
|
+
|
|
114
|
+
async redeemEcash(notes: string): Promise<void> {
|
|
115
|
+
await this._rpcSingle('mint', 'reissue_external_notes', {
|
|
116
|
+
oob_notes: notes, // "out of band notes"
|
|
117
|
+
extra_meta: null,
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Teardown
|
|
122
|
+
async cleanup() {
|
|
123
|
+
await this._fed?.free()
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
isOpen() {
|
|
127
|
+
return this._fed !== null
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Streaming
|
|
131
|
+
|
|
132
|
+
subscribeBalance(callback: (balance: number) => void) {
|
|
133
|
+
this._rpcStream('', 'subscribe_balance_changes', {}, (res: any) => {
|
|
134
|
+
callback(res)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
// TODO: implement unsubscribe on wasm side
|
|
138
|
+
return () => {
|
|
139
|
+
// no-op (fake unsubscribe)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// import init, { InitOutput, WasmClient } from '../wasm/fedimint_client_wasm.js'
|
|
2
|
+
|
|
3
|
+
// let client: WasmClient | undefined
|
|
4
|
+
|
|
5
|
+
// async function workerInit() {
|
|
6
|
+
// await init(new URL('@fedi/common/wasm/fedi_wasm_bg.wasm', import.meta.url))
|
|
7
|
+
// }
|
|
8
|
+
|
|
9
|
+
// const initPromise = workerInit().catch((error) =>
|
|
10
|
+
// postMessage({ error: String(error) }),
|
|
11
|
+
// )
|
|
12
|
+
|
|
13
|
+
// async function rpcRequest(method: string, data: string): Promise<string> {
|
|
14
|
+
// await initPromise
|
|
15
|
+
// return await fedimint_rpc(method, data)
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// const handleMessage = async (token: string, method: string, data: string) => {
|
|
19
|
+
// if (method === 'open') {
|
|
20
|
+
// await initPromise
|
|
21
|
+
// client = await WasmClient.open(data)
|
|
22
|
+
// if (client !== undefined) {
|
|
23
|
+
// postMessage({ token, result: true })
|
|
24
|
+
// } else {
|
|
25
|
+
// postMessage({ token, result: false })
|
|
26
|
+
// }
|
|
27
|
+
// } else if (method === 'join_federation') {
|
|
28
|
+
// await initPromise
|
|
29
|
+
// const result = await WasmClient.join_federation(data)
|
|
30
|
+
// postMessage({ token, result })
|
|
31
|
+
// }
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
// // Handles worker.postMessage calls
|
|
35
|
+
// addEventListener('message', (e) => {
|
|
36
|
+
// const { token, method, data } = e.data
|
|
37
|
+
// handleMessage(token, method, data)
|
|
38
|
+
// // rpcRequest(method, data)
|
|
39
|
+
// // .then((result) => postMessage({ token, result }))
|
|
40
|
+
// // .catch((error) => postMessage({ error: String(error) }))
|
|
41
|
+
// })
|
package/wasm/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<a href="https://fedimint.org">
|
|
3
|
+
Fedimint
|
|
4
|
+
</a>
|
|
5
|
+
</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<img src="docs/banner.png">
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://github.com/fedimint/fedimint/actions/workflows/ci-nix.yml">
|
|
13
|
+
<img src="https://github.com/fedimint/fedimint/actions/workflows/ci-nix.yml/badge.svg" alt="GitHub Actions CI Build Status">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://chat.fedimint.org"><img alt="Developer Discord Chat" src="https://img.shields.io/discord/990354215060795454?label=dev%20chat"></a>
|
|
16
|
+
<a href="https://github.com/fedimint/fedimint/discussions">
|
|
17
|
+
<img src="https://img.shields.io/badge/community-discussion-blue" alt="GitHub Discussion">
|
|
18
|
+
</a>
|
|
19
|
+
<a href="https://docs.fedimint.org">
|
|
20
|
+
<img src="https://img.shields.io/static/v1?label=Docs&message=master&color=007ec6&logo=GitBook&logoColor=ffffff" alt="docs built from master">
|
|
21
|
+
</a>
|
|
22
|
+
<a href="https://app.radicle.xyz/nodes/radicle.fedimint.org/rad:z2eeB9LF8fDNJQaEcvAWxQmU7h2PG">
|
|
23
|
+
<img src="https://img.shields.io/badge/Radicle-explore-blue" alt="View on Radicle">
|
|
24
|
+
</a>
|
|
25
|
+
<img alt="Lines of code" src="https://tokei.rs/b1/github/fedimint/fedimint">
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
[Fedimint](https://fedimint.org) is a module based system for building federated applications. It is designed to be a
|
|
29
|
+
trust-minimized, censorship-resistant, and private alternative to centralized applications.
|
|
30
|
+
|
|
31
|
+
> **Fedimint is alpha software released under
|
|
32
|
+
an [MIT License](https://github.com/fedimint/fedimint/blob/master/LICENSE). This means that the software here is
|
|
33
|
+
provided "as is", without warranty of any kind. We are a small development team with limited resources. If you
|
|
34
|
+
experience a loss of funds due to a bug in this software, we may not have the means to help you recover the funds. We
|
|
35
|
+
recommend you run Fedimint on testnets like mutinynet, or on mainnet with small amounts of money. You can find our
|
|
36
|
+
latest release [here](https://github.com/fedimint/fedimint/releases/latest).**
|
|
37
|
+
|
|
38
|
+
Fedimint ships with 3 default
|
|
39
|
+
modules - [Bitcoin](https://github.com/bitcoin/bitcoin), [Lightning](https://github.com/lightning/bolts),
|
|
40
|
+
and [Chaumian Ecash](https://en.wikipedia.org/wiki/Ecash) - for out-of-the-box best practices for private and
|
|
41
|
+
trust-minimized payments. [You can write custom modules](https://github.com/fedimint/fedimint-custom-modules-example)
|
|
42
|
+
that define further consensus items and transaction types leveraging the payments modules to build your own federated
|
|
43
|
+
applications.
|
|
44
|
+
|
|
45
|
+
The Fedimint Developer Discord is the best place to get help and ask
|
|
46
|
+
questions. [Join the Discord](https://discord.gg/cEVEmqCgWG) and say hi! We are extremely active and work to onboard
|
|
47
|
+
developers of all skill levels to Fedimint and associated open-source Bitcoin projects. Fedimint touches many different
|
|
48
|
+
areas of Bitcoin development, so there is something for everyone. See below for more information on how to get involved.
|
|
49
|
+
|
|
50
|
+
## Running your own Fedimint
|
|
51
|
+
|
|
52
|
+
It's easy to set up and run your own federations. Fedimint is designed to
|
|
53
|
+
be [Byzantine Fault Tolerant](https://en.wikipedia.org/wiki/Byzantine_fault) so is resilient to `m` malicious nodes in a
|
|
54
|
+
federation of `3m + 1` nodes. If you run a federation of 4 guardians you are resilient to 1 malicious guardian, if you
|
|
55
|
+
run a federation of 7 guardians you are resilient to 2 guardians, etc.
|
|
56
|
+
|
|
57
|
+
Fedimint can also be run in "solo mode" with a single guardian. This is useful for testing and development, but is not
|
|
58
|
+
recommended for production use.
|
|
59
|
+
|
|
60
|
+
To do lightning payments, Fedimint requires
|
|
61
|
+
a [Lightning Gateway](https://github.com/fedimint/fedimint/blob/master/docs/gateway.md): a user of the federation that
|
|
62
|
+
is willing to swap ecash in exchange for sending/receiving lightning payments. The Lightning Gateway is not a guardian
|
|
63
|
+
and acts as an untrusted economic actor serving the federation.
|
|
64
|
+
|
|
65
|
+
### Running Fedimint on Mutinynet
|
|
66
|
+
|
|
67
|
+
See the [Fedimint Mutinynet Setup Guide](./docs/setup-docs.md). You can modify the configuration options to deploy it
|
|
68
|
+
with.
|
|
69
|
+
|
|
70
|
+
## For Developers
|
|
71
|
+
|
|
72
|
+
We are actively looking for developers to help build Fedimint and associated open-source Bitcoin projects. Fedimint
|
|
73
|
+
touches many different areas of Bitcoin development, so there is something for everyone. The best places to get started
|
|
74
|
+
are:
|
|
75
|
+
|
|
76
|
+
- [The Fedimint Developer Discord](https://discord.gg/cEVEmqCgWG): the best place to get help and ask questions.
|
|
77
|
+
- [Fedimint Technical Reference Documentation](https://docs.fedimint.org)
|
|
78
|
+
- [Fedimint Contributor Calendar](https://calendar.google.com/calendar/u/0/embed?src=fedimintcalendar@gmail.com): This
|
|
79
|
+
calendar contains all the developer calls and events.
|
|
80
|
+
- [Fedimint Developer Calls](https://meet.jit.si/fedimintdevcall): We have developer calls every Monday at 4PM UTC to
|
|
81
|
+
review PRs and discuss current development priorities. As a new developer, this is a great place to find good first
|
|
82
|
+
issues and mentorship from the core team on how to get started contributing to Fedimint.
|
|
83
|
+
- [PR Review Club](https://meet.jit.si/fedimintdevcall): We have PR review calls every Tuesday at 4PM UTC.
|
|
84
|
+
- [Weekly Deep Dive](https://meet.jit.si/fedimintdevcall): We have a deep dive every Thursday at 4PM UTC to discuss
|
|
85
|
+
technical topics relating to Fedimint in depth: cryptography, Rust programming, consensus, networking, etc. This is a
|
|
86
|
+
great place to learn about the internals of Fedimint and Bitcoin. We normally plan these calls based off requests from
|
|
87
|
+
contributors on aspects of Fedimint they want to learn more about, so please reach out if you have a topic you want to
|
|
88
|
+
learn more about.
|
|
89
|
+
|
|
90
|
+
For contribution guidelines, Areas of contributions and how to get involved, please refer to
|
|
91
|
+
the [Contributing Guidelines](CONTRIBUTING.md).
|
|
92
|
+
|
|
93
|
+
### Fedimint Repos and Projects to Contribute To
|
|
94
|
+
|
|
95
|
+
- [Fedimint](https://github.com/fedimint/fedimint/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22): The
|
|
96
|
+
core Fedimint repository. This is where the core consensus and networking code lives. Fedimint is an advanced Rust
|
|
97
|
+
project and is a great place to learn Rust, cryptography, networking, consensus, and bitcoin development. We have a
|
|
98
|
+
lot of good first issues, are happy to mentor new developers, and are always looking for experienced Rust developers
|
|
99
|
+
to help with the core codebase.
|
|
100
|
+
- [UI](https://github.com/fedimint/ui): The default Fedimint Guardian and Lightning Gateway UIs. These are Typescript
|
|
101
|
+
and React projects. Contributing to this repo helps with UI/UX design and development to make Fedimint more user
|
|
102
|
+
friendly.
|
|
103
|
+
- [Lightning Gateway](https://github.com/fedimint/fedimint/issues?q=is%3Aissue+is%3Aopen+label%3Alightning): Fedimint's
|
|
104
|
+
Lightning Gateway is implemented as an HTLC interceptor and currently works with CLN, LND, and LDK's sample-node
|
|
105
|
+
implementations. We are always looking for lightning developers to help with the Lightning Gateway, especially around
|
|
106
|
+
improving payment reliability and to add support for more lightning implementations.
|
|
107
|
+
- [Custom Modules](https://github.com/fedimint/fedimint-custom-modules-example): Fedimint ships with 3 default modules:
|
|
108
|
+
Bitcoin, Lightning, and Chaumian Ecash. You can write custom modules that define further consensus items and
|
|
109
|
+
transaction types leveraging the payments modules to build your own federated applications. We are always looking for
|
|
110
|
+
developers to help build custom modules and to help improve the module system.
|
|
111
|
+
|
|
112
|
+
## Spinning up the Fedimint Developer Environment
|
|
113
|
+
|
|
114
|
+
Fedimint is a Rust project and uses the [Nix package manager](https://nixos.org/) to manage dependencies and build the
|
|
115
|
+
project.
|
|
116
|
+
|
|
117
|
+
### Local Development
|
|
118
|
+
|
|
119
|
+
We have a detailed tutorial on how to use the cli to send/receive ecash, lightning payments, and perform other developer
|
|
120
|
+
operations in the [Fedimint Developer Tutorial](https://github.com/fedimint/fedimint/blob/master/docs/tutorial.md).
|
|
121
|
+
|
|
122
|
+
Fedimint's developer environment and rust build pipeline is managed
|
|
123
|
+
through [Nix Flakebox](https://github.com/rustshop/flakebox). To get started, install Nix.
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Then fork and clone the Fedimint repo.
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
git clone https://github.com/your-username/fedimint.git
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Then enter the nix developer environment.
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
nix develop
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
and use this command to start a local regtest network with 4 guardians, a bitcoin node, and a lightning gateway.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
just mprocs
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
You can then interact with the guardians and lightning gateway using the cli. For more details on how to use the cli,
|
|
148
|
+
see the [Fedimint Developer Tutorial](https://github.com/fedimint/fedimint/blob/master/docs/tutorial.md).
|
|
149
|
+
|
|
150
|
+
If you want to run with UIs, see the [UI](https://github.com/fedimint/ui) repo for developer environment instructions.
|
|
151
|
+
|
|
152
|
+
# Maintainers
|
|
153
|
+
|
|
154
|
+
| Area | Lead-Maintainer | Co-Maintainers | Status |
|
|
155
|
+
|-------------------|--------------------|------------------------------|---------------------------------------|
|
|
156
|
+
| Project Lead | @elsirion | @dpc @joschisan | X |
|
|
157
|
+
| Core Server | @joschisan | X | mostly well factored, no known issues |
|
|
158
|
+
| Core Consensus | @joschisan | @bradleystachurski | polished and documented |
|
|
159
|
+
| Lightning Module | @joschisan | @m1sterc001guy | active development, known issues |
|
|
160
|
+
| Mint Module | @joschisan | X | active development, known issues |
|
|
161
|
+
| Wallet Module | @bradleystachurski | @dpc @joschisan | active development, critical issues |
|
|
162
|
+
| Core Client | @dpc | X | X |
|
|
163
|
+
| Lightning Gateway | @m1sterc001guy | @joschisan | X |
|
|
164
|
+
| Database | @m1sterc001guy | X | X |
|
|
165
|
+
| Networking | X | X | X |
|
|
166
|
+
| CI / Nix | @dpc | @maan2003 @bradleystachurski | X |
|
|
167
|
+
| Testing | @bradleystachurski | X | X |
|
|
168
|
+
| Devimint | @maan2003 | X | X |
|
|
169
|
+
| Config Generation | X | X | X |
|