|
| 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 | +``` |
0 commit comments