Skip to content

Commit b450e3f

Browse files
authored
Merge pull request #1021 from Web3Auth/mpc-ck-rn
Add mpc ck react native migration guide
2 parents 19e96af + abe8442 commit b450e3f

File tree

44 files changed

+2942
-1070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2942
-1070
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
---
2+
title: MPC Core Kit React Native SDK Migration
3+
description: MPC Core Kit React Native SDK Migration | Documentation - Web3Auth"
4+
sidebar_label: React Native SDK Migration
5+
---
6+
7+
## Overview
8+
9+
This migration guide provides steps for upgrading from the Web3Auth MPC CoreKit Web SDK to the new
10+
MPC CoreKit React Native SDK. This new SDK is specifically designed and optimized for React Native
11+
applications, providing better performance and a more native experience.
12+
13+
#### Key Benefits of the React Native SDK:
14+
15+
- Native Performance: Optimized for React Native's architecture, for faster speeds across the board
16+
- Platform-specific Features: Takes advantage of React Native capabilities for session and share
17+
storage
18+
- Improved Developer Experience: APIs designed specifically for React Native development, reducing
19+
the need to multiple extra polyfills
20+
21+
## Breaking Changes
22+
23+
### Package Changes
24+
25+
Removes the need of using the `@toruslabs/react-native-tss-lib-bridge` & `@web3auth/mpc-core-kit`
26+
packages and introduces the `@web3auth/react-native-mpc-core-kit` package which includes the TSS
27+
library bridge and MPC Core Kit functionality in a single package.
28+
29+
:::note
30+
31+
You might still keep the `@web3auth/mpc-core-kit` package for some common helper functions and
32+
types.
33+
34+
:::
35+
36+
```tsx
37+
// remove-next-line
38+
import { Bridge, tssLib } from "@toruslabs/react-native-tss-lib-bridge";
39+
import { CHAIN_NAMESPACES } from "@web3auth/base";
40+
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider";
41+
import {
42+
COREKIT_STATUS,
43+
generateFactorKey,
44+
JWTLoginParams,
45+
keyToMnemonic,
46+
makeEthereumSigner,
47+
mnemonicToKey,
48+
parseToken,
49+
TssShareType,
50+
WEB3AUTH_NETWORK,
51+
// remove-next-line
52+
Web3AuthMPCCoreKit,
53+
} from "@web3auth/mpc-core-kit";
54+
55+
// add-next-line
56+
import { Bridge, mpclib, TssDklsLib } from "@web3auth/react-native-mpc-core-kit";
57+
```
58+
59+
### Constructor Changes
60+
61+
Use the `Web3AuthMPCCoreKitRN` class from the `mpclib` object imported from
62+
`@web3auth/react-native-mpc-core-kit` for creating an instance. Additionally, pass the `TssDklsLib`
63+
that is directly exposed from the SDK as the `tssLib` parameter.
64+
65+
:::info
66+
67+
- Use `TssDklsLib` for generating `secp256k1` curve signatures (ECDSA)
68+
- Use `TssFrostLib` for generating `ed25519` curve signatures (EdDSA)
69+
70+
:::
71+
72+
```tsx
73+
// remove-next-line
74+
const coreKitInstance = new Web3AuthMPCCoreKit({...}):
75+
// add-next-line
76+
const coreKitInstance = new mpclib.Web3AuthMPCCoreKitRN({
77+
web3AuthClientId,
78+
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
79+
// setupProviderOnInit: false, // needed to skip the provider setup
80+
uxMode: "react-native",
81+
// remove-next-line
82+
tssLib: tsslibInstance, // tss lib bridge for react native
83+
// add-next-line
84+
tssLib: TssDklsLib, // tss lib bridge for react native
85+
manualSync: true, // This is the recommended approach
86+
storage: asyncStorageKey, // Add the storage property
87+
});
88+
```
89+
90+
### Add the Bridge in your codebase
91+
92+
Add the Bridge component at the bottom of your App's codebase where the MPC Instance needs to be
93+
used. The Bridge component creates a WebView instance that executes the MPC Core Kit library. The
94+
Bridge component also exposes a `resolveReady` callback that notifies when the bridge is ready to be
95+
used.
96+
97+
```ts
98+
99+
// remove-next-line
100+
import { Bridge, tssLib } from "@toruslabs/react-native-tss-lib-bridge";
101+
// add-next-line
102+
import { Bridge, mpclib, TssDklsLib } from "@web3auth/react-native-mpc-core-kit";
103+
104+
function Home() {
105+
return (
106+
<View style={styles.container}>
107+
// remove-next-line
108+
<Bridge logLevel={"debug"} />
109+
// add-start
110+
<Bridge
111+
logLevel={"DEBUG"}
112+
resolveReady={(ready) => {
113+
setBridgeReady(ready);
114+
}}
115+
/>
116+
// add-end
117+
</View>
118+
);
119+
}
120+
```
121+
122+
### Check for the Bridge State before Initing
123+
124+
The new package of React Native exposes a `resolveReady` callback that notifies when the bridge is
125+
ready to be used. Use this to init the SDK after the bridge has been configured.
126+
127+
```js
128+
useEffect(() => {
129+
if (bridgeReady) {
130+
const init = async () => {
131+
try {
132+
await coreKitInstance.init();
133+
} catch (error: any) {
134+
uiConsole(error.message, "mounted caught");
135+
}
136+
setCoreKitStatus(coreKitInstance.status);
137+
};
138+
init();
139+
}
140+
}, [bridgeReady]);
141+
```
142+
143+
### Add a Custom Transformer
144+
145+
Add a custom transformer to your project by creating a new file called `customTransformer.js` and
146+
updating your metro config to use it. This transformer is specifically designed to handle the
147+
polyfills and transformations needed by the MPC Core Kit SDK.
148+
149+
```js title="customTransformer.js"
150+
const { nodeModulesPolyfillPlugin } = require("esbuild-plugins-node-modules-polyfill");
151+
const reactNativeReactBridgeTransformer = require("react-native-react-bridge/lib/plugin");
152+
const esbuildOptions = {
153+
plugins: [
154+
nodeModulesPolyfillPlugin({
155+
globals: {
156+
Buffer: true,
157+
crypto: true,
158+
},
159+
// modules: {
160+
// Buffer : true,
161+
// }
162+
}),
163+
],
164+
};
165+
module.exports.transform = function ({ src, filename, options }) {
166+
const transform = reactNativeReactBridgeTransformer.createTransformer(esbuildOptions);
167+
return transform({ src, filename, options });
168+
};
169+
```
170+
171+
```js title="metro.config.js"
172+
const config = getDefaultConfig(__dirname);
173+
174+
// focus-start
175+
// remove-next-line
176+
config.transformer.babelTransformerPath = require.resolve("react-native-react-bridge/lib/plugin");
177+
// add-next-line
178+
config.transformer.babelTransformerPath = require.resolve("./customTransformer.js");
179+
// focus-end
180+
181+
config.resolver.extraNodeModules = {
182+
...config.resolver.extraNodeModules,
183+
184+
assert: require.resolve("empty-module"), // assert can be polyfilled here if needed
185+
http: require.resolve("empty-module"), // stream-http can be polyfilled here if needed
186+
https: require.resolve("empty-module"), // https-browserify can be polyfilled here if needed
187+
os: require.resolve("empty-module"), // os-browserify can be polyfilled here if needed
188+
url: require.resolve("empty-module"), // url can be polyfilled here if needed
189+
zlib: require.resolve("empty-module"), // browserify-zlib can be polyfilled here if needed
190+
path: require.resolve("empty-module"),
191+
crypto: require.resolve("empty-module"),
192+
buffer: require.resolve("@craftzdog/react-native-buffer"),
193+
};
194+
// config.resolveRequest = (context, moduleName, platform) => {
195+
// if (moduleName === "crypto") {
196+
// // when importing crypto, resolve to react-native-quick-crypto
197+
// return context.resolveRequest(context, "react-native-quick-crypto", platform);
198+
// }
199+
// // otherwise chain to the standard Metro resolver.
200+
// return context.resolveRequest(context, moduleName, platform);
201+
// }
202+
module.exports = config;
203+
```
204+
205+
### Add Buffer in your globals/ entry level file
206+
207+
No need for `react-native-quick-crypto` any longer, just polyfill buffer in your entry file.
208+
209+
```js title="globals.ts"
210+
//remove-start
211+
import { install } from "react-native-quick-crypto";
212+
213+
install();
214+
// remove-end
215+
216+
// add-next-line
217+
global.Buffer = require("buffer").Buffer;
218+
```

docs/sdk/mpc-core-kit/mpc-core-kit-js/authentication/login-jwt.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ By default, the ed25519 key is formatted in base58 and is 64 bytes long. This co
151151
32 bytes as the seed (also known as the private key) and the last 32 bytes as the public key. Ensure
152152
that the first 32 bytes are provided in hexadecimal format when using the ed25519 seed.
153153

154+
:::info ED25519 Seed
155+
156+
The ed25519 seed is a 64-byte value, where the first 32 bytes represent the private key and the last
157+
32 bytes represent the public key. When using the ed25519 seed, ensure that the first 32 bytes
158+
(private key) are provided in hexadecimal format. For example, a sample ed25519 seed in hexadecimal
159+
format could be `0x1a2b3c4d5e6f7890a1b2c3d4e5f67890a1b2c3d4e5f67890a1b2c3d4e5f6789`.
160+
161+
:::
162+
154163
```tsx
155164
import { JWTLoginParams } from "@web3auth/mpc-core-kit";
156165

docs/sdk/mpc-core-kit/mpc-core-kit-js/authentication/login-oauth.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ By default, the ed25519 key is formatted in base58 and is 64 bytes long. This co
202202
32 bytes as the seed (also known as the private key) and the last 32 bytes as the public key. Ensure
203203
that the first 32 bytes are provided in hexadecimal format when using the ed25519 seed.
204204

205+
:::info ED25519 Seed
206+
207+
The ed25519 seed is a 64-byte value, where the first 32 bytes represent the private key and the last
208+
32 bytes represent the public key. When using the ed25519 seed, ensure that the first 32 bytes
209+
(private key) are provided in hexadecimal format. For example, a sample ed25519 seed in hexadecimal
210+
format could be `0x1a2b3c4d5e6f7890a1b2c3d4e5f67890a1b2c3d4e5f67890a1b2c3d4e5f6789`.
211+
212+
:::
213+
205214
```tsx
206215
import { SubVerifierDetailsParams } from "@web3auth/mpc-core-kit";
207216

docs/sdk/mpc-core-kit/mpc-core-kit-js/examples.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ hide_table_of_contents: true
88
import Examples from "@site/src/components/Examples";
99
import { coreKitMPCWebExamples, coreKitMPCReactNativeExamples } from "@site/src/common/maps";
1010

11-
<Examples exampleMap={[...coreKitMPCWebExamples, ...coreKitMPCReactNativeExamples]} />
11+
<Examples exampleMap={[...coreKitMPCWebExamples]} />

docs/sdk/mpc-core-kit/mpc-core-kit-js/initialize.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ After installation, the next step to use Web3Auth MPC Core Kit is to configure &
1919
Please note that these are the most critical steps where you need to pass on different parameters
2020
according to the preference of your project.
2121

22+
:::info
23+
24+
Ensure that you configure your authentication method and verifier prior to initializing the SDK in
25+
your codebase. This will streamline the implementation process.
26+
27+
:::
28+
2229
### Parameters
2330

2431
The Web3AuthMPCCoreKit constructor takes an object with `Web3AuthOptions` as input which helps you

docs/sdk/mpc-core-kit/mpc-core-kit-js/install.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ flexibility to customize the UI and UX of the authentication process.
1818
npm install @web3auth/mpc-core-kit
1919
```
2020

21+
:::note
22+
23+
If you were using the MPC Core Kit JS SDK in your react native application, you need to follow this
24+
[migration guide](/migration-guides/mpc-core-kit-react-native-migration) to migrate to the new MPC
25+
Core Kit React Native SDK.
26+
27+
:::
28+
2129
## Common Types and Interfaces
2230

2331
This package gives access to common types and interfaces for Web3Auth. This comes in handy by
@@ -42,7 +50,7 @@ npm install @toruslabs/tss-dkls-lib
4250

4351
This package gives EIP1193 compatible Ethereum signing provider. This provider is used to make calls
4452
to the selected blockchain, and can be used with `web3.js`, `ethers.js`, or `viem` to make
45-
integration with Ethereum-compatible chains more easier.
53+
integration with EVM compatible chains easier.
4654

4755
```bash npm2yarn
4856
npm install @web3auth/ethereum-mpc-provider
@@ -67,4 +75,3 @@ building the app. We have created guides for different bundlers to help you with
6775

6876
- Please check out our **[Webpack 5 Troubleshooting Guide](/troubleshooting/webpack-issues)**
6977
- Please check out our **[Vite Troubleshooting Guide](/troubleshooting/vite-issues)**
70-
- Please check out our **[React Native Troubleshooting Guide](/troubleshooting/metro-issues-mpc)**

0 commit comments

Comments
 (0)