@fedimint/react-native-bindings 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/app.plugin.js +2 -0
- package/package.json +50 -0
- package/src/ReactNativeTransport.ts +102 -0
- package/src/index.ts +6 -0
package/app.plugin.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedimint/react-native-bindings",
|
|
3
|
+
"description": "React Native bindings for the Fedimint client",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/fedimint/fedimint-sdk.git",
|
|
8
|
+
"directory": "packages/react-native-bindings"
|
|
9
|
+
},
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"lib",
|
|
14
|
+
"app.plugin.js"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "bob build",
|
|
18
|
+
"clean": "rm -rf lib tsconfig.tsbuildinfo",
|
|
19
|
+
"clean:deep": "pnpm run clean && rm -rf node_modules",
|
|
20
|
+
"typecheck": "tsc --noEmit"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "*",
|
|
25
|
+
"react-native": "*"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"react-native-builder-bob": "^0.35.0",
|
|
29
|
+
"typescript": "^5.9.3"
|
|
30
|
+
},
|
|
31
|
+
"react-native-builder-bob": {
|
|
32
|
+
"source": "src",
|
|
33
|
+
"output": "lib",
|
|
34
|
+
"targets": [
|
|
35
|
+
[
|
|
36
|
+
"commonjs",
|
|
37
|
+
{
|
|
38
|
+
"esm": true
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
"module",
|
|
43
|
+
{
|
|
44
|
+
"esm": true
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"typescript"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Transport,
|
|
3
|
+
type TransportLogger,
|
|
4
|
+
type TransportRequest,
|
|
5
|
+
} from '@fedimint/types';
|
|
6
|
+
|
|
7
|
+
import { RpcHandler } from '@fedimint/react-native';
|
|
8
|
+
|
|
9
|
+
export class ReactNativeTransport extends Transport {
|
|
10
|
+
logger: TransportLogger = console;
|
|
11
|
+
private rpcHandler: RpcHandler;
|
|
12
|
+
|
|
13
|
+
constructor(dbPath: string) {
|
|
14
|
+
super();
|
|
15
|
+
if (!dbPath) {
|
|
16
|
+
throw new Error('ReactNativeTransport requires a dbPath');
|
|
17
|
+
}
|
|
18
|
+
this.rpcHandler = new RpcHandler(dbPath);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async postMessage(message: TransportRequest): Promise<void> {
|
|
22
|
+
console.log(
|
|
23
|
+
'ReactNativeTransport postMessage received:',
|
|
24
|
+
JSON.stringify(message)
|
|
25
|
+
);
|
|
26
|
+
const { type, payload, requestId } = message;
|
|
27
|
+
try {
|
|
28
|
+
// Handle init - just respond with success since we initialized in constructor
|
|
29
|
+
if (type === 'init') {
|
|
30
|
+
this.messageHandler({
|
|
31
|
+
type: 'data',
|
|
32
|
+
request_id: message.requestId,
|
|
33
|
+
data: true,
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
type === 'set_mnemonic' ||
|
|
40
|
+
type === 'generate_mnemonic' ||
|
|
41
|
+
type === 'get_mnemonic' ||
|
|
42
|
+
type === 'join_federation' ||
|
|
43
|
+
type === 'open_client' ||
|
|
44
|
+
type === 'close_client' ||
|
|
45
|
+
type === 'client_rpc' ||
|
|
46
|
+
type === 'cancel_rpc' ||
|
|
47
|
+
type === 'parse_invite_code' ||
|
|
48
|
+
type === 'parse_bolt11_invoice' ||
|
|
49
|
+
type === 'preview_federation' ||
|
|
50
|
+
type === 'parse_oob_notes' ||
|
|
51
|
+
type === 'has_mnemonic_set'
|
|
52
|
+
) {
|
|
53
|
+
const rustRequest = {
|
|
54
|
+
type: type,
|
|
55
|
+
request_id: requestId,
|
|
56
|
+
payload: payload ?? null,
|
|
57
|
+
};
|
|
58
|
+
const json = JSON.stringify(rustRequest);
|
|
59
|
+
console.log('ReactNativeTransport sending RPC:', json);
|
|
60
|
+
|
|
61
|
+
const responseStr = await new Promise<string>((resolve, reject) => {
|
|
62
|
+
try {
|
|
63
|
+
const callback = {
|
|
64
|
+
onResponse: (response: string) => {
|
|
65
|
+
resolve(response);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
this.rpcHandler.rpc(json, callback);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
reject(e);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
console.log('ReactNativeTransport RPC raw response:', responseStr);
|
|
74
|
+
|
|
75
|
+
const response = JSON.parse(responseStr);
|
|
76
|
+
console.log(
|
|
77
|
+
'ReactNativeTransport RPC parsed response:',
|
|
78
|
+
JSON.stringify(response)
|
|
79
|
+
);
|
|
80
|
+
if (response.type === 'error') {
|
|
81
|
+
throw new Error(response.error || 'Unknown RPC error');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.messageHandler(response);
|
|
85
|
+
} else if (type === 'cleanup') {
|
|
86
|
+
console.log('cleanup message received');
|
|
87
|
+
this.rpcHandler.uniffiDestroy();
|
|
88
|
+
} else {
|
|
89
|
+
this.logger.error('Unknown message type', type);
|
|
90
|
+
this.errorHandler('Unknown message type');
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
this.logger.error('RPC Error', error);
|
|
94
|
+
this.messageHandler({
|
|
95
|
+
type: 'error',
|
|
96
|
+
error: error instanceof Error ? error.message : String(error),
|
|
97
|
+
request_id: requestId,
|
|
98
|
+
});
|
|
99
|
+
this.errorHandler(error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|