@portal-hq/core
v6.0.1
Published
Portal MPC Support for React Native
Readme
Portal
The @portal-hq/core package includes a class constructor for the Portal class, a React Context Provider for the PortalContext, the usePortal hook for use within child components, and some helpful types and enums for working with Portal in your app. These pieces allow you to initialize Portal in your app, expose the instance to your component tree, and consume the instance in your child components.
The Portal Class
The Portal class that houses three main sub-classes
apimakes requests to the Portal apimpcfacilitates the generation, backup, and recovery of MPC walletsproviderthe core Portal provider (EIP-1139 compliant)
Instantiation
When instantiating the Portal class, you must provide a PortalOptions object. This object is used to initialize all of the sub-classes (and their sub-classes).
Example instantiation
import { BackupMethods, Portal } from '@portal-hq/core'
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),
keychain: keychain, // See: @portal-hq/keychain
})PortalOptions
The PortalOptions object is where you configure your instance of Portal. It contains a set of required properties and a set of optional properties.
Required properties
apiKeystring a valid Portal Client API Key created via the Portal REST APIbackupConfigobjectgdriveStorage (optional) expects an instance of@portal-hq/gdrive-storageicloudStorage (optional) expects an instance of@portal-hq/icloud-storage
gatewayConfigstring or GatewayConfig the base url (including your API key) you'd like us to use for Gateway requests (reading from and writing to chain) Note: this will be required in the futureGatewayConfig– if you don't want to use the same url for all requests, you can instead provide a chain-level config for all Gateway RPC calls this can be passed in as an object with key/value pairs where thekeyis thechainIdas a number and the value is the base url you'd like to use when performing actions on thischainId
isSimulatorboolean whether or not you're currently running the app in a simulator (this is required to ensure that keychain storage is configured appropriately for the current device)keychainKeychain expects an instance of either@portal-hq/keychainor@portal-hq/mobile-key-values
Optional properties
autoApproveboolean (default:false) whether you'd like the provider to auto-approve transactionschainIdnumber (default:11155111) Deprecated – will be removed in a future major version. The legacy numeric ID of the default EVM chain. Pass a CAIP-2chainIdon every call instead, e.g.portal.request(PortalRequestMethod.EthSendTransaction, [transaction], 'eip155:1')orportal.sendAsset(params, 'eip155:1')- Warning:
portal.updateChain('eip155:1')moves only the provider's global chain and does not updateportal.chainId, so it is not by itself a complete migration. Calls that omit an explicitchain– such asportal.sendAsset(params)– still resolve through the legacyportal.chainIdand can broadcast on a different chain than the one passed toportal.updateChain().
- Warning:
mpcEnabledboolean (default:true) whether or not to use MPC
api
The api property contains an instance of the PortalApi class, which has a number of helper methods to facilitate the retrieval of relevant application data from the Portal REST API.
Methods
getEnabledDapps
Fetches a list of allowed dApps based on your dApp settings in the Portal web app.
Example usage
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),,
keychain: keychain // See: @portal-hq/keychain
})
const dapps = await portal.api.getEnabledDapps()getNetworks
Fetches a list of supported networks based on your dApp settings in the Portal web app.
Example usage
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),,
keychain: keychain // See: @portal-hq/keychain
})
const networks = await portal.api.getNetworks()mpc
The mpc property contains an instance of the PortalMpc class, which has a number of helper methods to facilitate the management of Portal MPC wallets.
Methods
generate
Performs the MPC generate process to create an MPC wallet and its signing shares
Example usage
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),,
keychain: keychain // See: @portal-hq/keychain
})
const createWallet = async () => {
await portal.createWallet()
}backup
Performs the MPC backup process to create the backup shares for the generated MPC wallet.
Example usage
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),,
keychain: keychain, // See: @portal-hq/keychain
})
const backupWallet = async () => {
await portal.backupWallet(BackupMethods.GDrive)
}recover
Performs the MPC recovery process to generate new signing shares for the MPC wallet.
Example usage
const portal = new Portal({
apiKey: 'YOUR_PORTAL_CLIENT_API_KEY',
backup: {
gdrive: gDriveStorage, // See: @portal-hq/gdrive-storage
icloud: iCloudStorage, // See: @portal-hq/icloud-storage
},
gatewayConfig: 'https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY',
isSimulator: DeviceInfo.isEmulator(),
keychain: keychain, // See: @portal-hq/keychain
})
const recoverWallet = async () => {
await portal.mpc.recoverWallet(cipherText, BackupMethods.GDrive)
}provider
The provider property contains an EIP-1139 compliant provider.
Making requests through the provider
In order to perform basic web3 operations, such as eth_accounts and eth_sendTransaction, you can use the provider's request method.
This method conforms to EIP-1139.
Example usage
const transaction = {
data: '',
to: toAddress,
value: BigNumber.from('1').toHexString(),
gas: '0x6000',
from: portal.address, // The address of the current connected wallet
}
const txHash = await portal.provider.request({
method: 'eth_sendTransaction',
params: transaction,
})For more information on how to use this provider natively within your React Native app, see the Provider Docs.
The Portal context
PortalContext is used to persist an instance of Portal within your application. This allows you to initialize Portal once, and easily share this instance between all of the components within a given scope in your app.
The exported members allow for 2 ways to set the PortalContext and one way to get the PortalContext.
Setters
You only need to choose one of these when implementing the PortalContext in your app.
PortalContextProvidera React Context Provider to provide yourPortalinstance to the components in your app
Getters
usePortala React hook to use thePortalinstance in any component within thePortalContextProviderscope (any component being rendered as either a shallow or deep child of thePortalContextProvider)
For more info on working with React Context, check out the docs.
PortalContextProvider
The PortalContextProvider allows you to share your instance of the Portal class with all components in your component tree. Providing a value prop with your Portal instance and wrapping your components in the PortalContextProvider enables this behavior. All children of the PortalContextProvider can access the Portal instance using the usePortal hook (see below).
Note: The easiest way to ensure you're exposing Portal to all components in your component tree is to use it in your App component
// MyRootComponent.tsx
import { Portal, PortalContextProvider } from '@portal-hq/react-native'
import { useEffect, useState } from 'react'
const MyRootComponent = () => {
const [portal, setPortal] = useState<Portal>(null)
useEffect(() => {
if (!portal) {
setPortal(
new Portal({
// TODO: Add all of the appropriate config options
}),
)
}
}, [portal])
return (
<PortalContextProvider value={portal}>
{/* Now all children rendered in this scope will have access to the `Portal` instance via the `usePortal` hook */}
</PortalContextProvider>
)
}usePortal
The usePortal hook allows any child in the component tree below a PortalContextProvider to access the Portal instance.
// MyChildComponent.tsx
import { usePortal } from '@portal-hq/react-native'
const MyChildComponent = () => {
const portal = usePortal()
// You can now do things with the `Portal` instance directly, such as:
// - `await portal.api.getEnabledDapps()`
// - `await portal.mpc.generate()`
//
// For more information on this, please see the `Portal` docs in `src/lib/portal`
...
}
Additional exports
In addition to the Portal class and context exports, the @portal-hq/core package exports some helpful types and enums for use when building your app.
The additional exports are as follows:
BackupMethods– an enum of supported storage methods for backup MPC key sharesAddress- a type representing Portal Address recordsDapp- a type representing Portal Dapp records (portal.api.getEnabledDapps()returnsDapp[])PortalOptions- a type representing the object used to configure thePortalclass on initialization
Native Bridge Files
The iOS implementation includes native bridge files (MpcBridge.h and MpcBridge.m) that provide C-compatible function declarations and implementations to interface with the underlying Go MPC library via gomobile.
Important Note: When the Go build/library is updated, these bridge files (
packages/core/ios/MpcBridge.handpackages/core/ios/MpcBridge.m) should be reviewed and updated accordingly to ensure compatibility with any new function signatures or API changes from the Go layer.
iOS (CocoaPods + React Native)
use_frameworks! :linkage => :static
Firebase and other Swift pods commonly require static frameworks. The PortalMpc pod vendors Mpc.xcframework (Go-mobile output is a static archive inside Mpc.framework). With React Native’s static-framework path, the pod target is assembled with libtool, which does not apply OTHER_LDFLAGS, so the Go symbols stay unresolved until the app link step. The podspec therefore sets user_target_xcconfig with SDK-conditional -Wl,-force_load,<path-to-Mpc.framework/Mpc> so the final Clang/ld link pulls in Mobile*.
Default install layout
user_target_xcconfig expands the vendored archive path as:
$(PODS_ROOT)/../../node_modules/@portal-hq/core/ios/Frameworks/Mpc.xcframework/...
That matches the usual React Native layout (ios/Podfile next to node_modules). If you integrate the pod from a custom :path without this node_modules layout, add your own post_install / target OTHER_LDFLAGS with a correct -force_load to the same Mpc.framework/Mpc binary inside the matching xcframework slice.
Case-sensitive filesystems
The vendored bundle is named Mpc.xcframework so the linker flag name matches the inner Mpc.framework (avoids mpc vs Mpc issues on case-sensitive CI).
Xcode 26 + React Native 0.80.x (fmt)
Apple Clang 21 rejects consteval in the fmt version pinned by RN 0.80.x. Until you upgrade to RN ≥ 0.83 (fmt 12.1.0+), apply the small Pods/fmt/include/fmt/base.h patch in your app’s Podfile post_install (disable FMT_USE_CONSTEVAL). See examples/react-native-example/ios/Podfile for a labeled, idempotent snippet.
