@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
ThePortal
class that houses three main sub-classes
api
makes requests to the Portal apimpc
facilitates the generation, backup, and recovery of MPC walletsprovider
the core Portal provider (EIP-1193 compliant)
Instantiation
When instantiating thePortal
class, you must provide a PortalOptions
object. This object is used to initialize all of the sub-classes (and their sub-classes).
Example instantiation
PortalOptions
ThePortalOptions
object is where you configure your instance of Portal
. It contains a set of required properties and a set of optional properties.
Required properties
apiKey
string a valid Portal Client API Key created via the Portal REST APIbackup
objectkey
BackupMethods - an entry from theBackupMethods
enum (exported by@portal-hq/core
value
CloudStorageAdapter - an instance of either @portal-hq/gdrive-storage or @portal-hq/icloud-storage
chainId
number the ID of the current Ethereum chain you’d like to perform actions on- You can update this property on your
Portal
instance later – if your app requires this – by settingportal.chainId
property
- You can update this property on your
gatewayConfig
string 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 thekey
is thechainId
as a number and the value is the base url you’d like to use when performing actions on thischainId
isSimulator
boolean 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)keychain
MobileStorageAdapter - an instance of either @portal-hq/keychain or @portal-hq/mobile-key-values
autoApprove
boolean (default:false
) whether you’d like the provider to auto-approve transactions
portal.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.
portal.api.getNFTs
Fetches a list of non-fungible tokens (NFTs). The response is an array of objects where each object represents a unique NFT.
portal.api.getTransactions
Fetches a list of the client’s transaction history ordered by blockTimestamp
descending (latest transactions will come first). This includes both inbound and outbound transactions.
limit
and offset
can optionally be provided as arguments to paginate the list of transactions.portal.api.getBalances
Fetches a list of the client’s ERC20 token balances.
portal.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
portal.mpc.generate
Performs the MPC generate process to create an MPC wallet and its signing shares
Example usage
portal.mpc.backup
Performs the MPC backup process to create the backup shares for the generated MPC wallet.
Example usage
portal.mpc.recover
Performs the MPC recovery process to generate new signing shares for the MPC wallet.
Example usage
portal.provider
The provider
property contains an EIP-1193 compliant provider.
portal.provider.request
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-1193.
For a full list of supported JSON RPC methods and their associated params, please see Ethereum’s JSON-RPC API docs.
Example usage
portal.setChainId
This method allows you to specify the chain ID of the blockchain network that your Portal instance should interact with. This method is particularly useful in a multi-chain or cross-chain context where your application might need to switch between different blockchains.
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 thePortalContext
in your app.
PortalContextProvider
a React Context Provider to provide yourPortal
instance to the components in your app
Getters
usePortal
a React hook to use thePortal
instance in any component within thePortalContextProvider
scope (any component being rendered as either a shallow or deep child of thePortalContextProvider
)
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
usePortal
The usePortal
hook allows any child in the component tree below a PortalContextProvider
to access the Portal instance.
Additional exports
In addition to thePortal
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 sharesContract
- a type representing Portal Contract recordsDapp
- a type representing Portal Dapp records returnsDapp[]
PortalOptions
- a type representing the object used to configure thePortal
class on initialization