Comment on page
🔓
Backing up a wallet
This guide will walk you through how backups of a user's wallet are set up with Portal.
This product is currently in Beta and is under active development.
MPC backups allow your users to recover their MPC wallets in the event that their device is lost, stolen, or is bricked.
At the time of recovery, these two backups are used together to generate new shares to be stored on-device, allowing the user to migrate their wallet with a new device.
Storing a user MPC backup is done by storing an encryption key with the user's cloud storage provider and the encrypted share within your infrastructure.
In order to support user MPC backups, three main dependencies must be met:
- 1.You must have cloud storage configured for storing the user's share of the MPC backup
- 2.Your server must be able to store the encrypted share
- 3.Your app must send the encrypted share to your server for storage
The Web SDK currently support Google Drive as the backup mechanism. When a user backs up their wallet, the encryption key for their backup share is stored in their Google Drive folder.
In order to use the Google Drive functionality for backup you must first get an oAuth Credential for your Google Project.
Setting up your oAuth Credential
- 1.
- 2.From the projects list, select a project or create a new one.
- 3.If the APIs & services page isn't already open, open the console left side menu and select APIs & services.
- 4.On the left, click Credentials.
- 5.Click New Credentials, then select OAuth client ID.
- 6.For application type, choose
Web Application
- 7.In the
Authorized JavaScript Origins
section, addhttps://web.portalhq.io
- 8.In the
Authorized Redirect URIs
section, add https://web.portalhq.io/auth/index.html - 9.If this is your first time creating a client ID, you can also configure your consent screen by clicking Consent Screen. You won't be prompted to configure the consent screen after you do it the first time.
- 10.Click Create client ID
To use Google Drive backups with Portal, add the
Client ID
for your oAuth Client to your initialization config.const Portal = new Portal({
apiKey: 'YOUR-CLIENT-API-KEY',
autoApprove: true, // This should only be used for local development
chainId: 5,
gatewayConfig: 'YOUR-INFURA-OR-ALCHEMY-URL',
gdrive: {
clientId: 'YOUR-OAUTH-CLIENT-ID',
},
})
On your server, create an endpoint to accept the encrypted share and store it.
server.js
1
/*
2
* This endpoint can be defined by you however you want
3
*/
4
app.post('/portal/clients/:clientId/backup', async (req, res) => {
5
const { clientId } = req.params
6
const { backupShare } = req.body
7
8
// Example db function updating a "users" table with the backupShare
9
await db.users.update({
10
where: {
11
clientId,
12
},
13
data: {
14
backupShare,
15
},
16
})
17
res.status(200).send()
18
})
In order to add support for user MPC backups to your app, you must perform three steps:
- 1.Generate an encrypted client backup share using
portal.mpc.backup
. - 2.Send the resulting backup share to your API and store it.
- 3.Call
portal.api.storedClientBackupShare
to notify Portal that the encrypted client backup share was saved successfully. (Alternatively you can make an HTTP request to our API directly.)
BackupButton.tsx
1
const BackupButton: React.FC = () => {
2
const handleBackup = async () => {
3
// Get an encryped client backup share from running backup.
4
const backupShare = await portal.backupWallet()
5
6
try {
7
// Send the backup share to your API and store it.
8
await axios.post('{your_server}/clients/[clientId]/backup', {
9
data: { backupShare }
10
})
11
12
// ✅ Notify Portal that the client backup share was stored! 🙌
13
await portal.storedClientBackupShare(true)
14
} catch (error) {
15
// ❌ Notify Portal that the client backup share was not stored.
16
await portal.storedClientBackupShare(false)
17
}
18
}
19
20
return (
21
<button onPress={handleBackup}>Backup your wallet</button>
22
)
23
}
24
25
export default BackupButton
Storing a custodian MPC backup is done by Portal generating a custodian backup share and sending the share – via
webhook
– to be stored within your infrastructure.In order to support custodian MPC backups, two main dependencies must be met:
- 1.Register a
webhooks_root
with Portal - 2.Implement the
/{webhooks_root}/backup
route in your server to store your backup share
Portal will send the custodian MPC backup share to
/{webhooks_root}/backup
via a POST
request. The body of this POST
request will contain two fields:clientId
- The PortalclientId
for the usershare
- aJSON.stringified
version of the backup share
server.js
1
import express from 'express'
2
3
const app = express()
4
5
app.post('/webhook/backup', async (req, res) => {
6
// Destructure the provided fields from the request body
7
const {
8
clientId, // The Portal clientId for the user
9
share, // a JSON.strinfigied version of the backup share
10
} = req.body
11
12
// TODO: Store the backup share within your infrastructure
13
14
res.status(200).send()
15
})
When Portal makes a request to your
/backup
webhook, another immediate request is made to /backup/fetch
in order to validate the backup share was stored successfully.Last modified 1mo ago