Pear Worklet WDK Configuration
Configure the Bare worklet context and the JSON payloads passed to initializeWDK and resetWdkWallets
This page explains how to build the worklet context, shape the worklet config payload, initialize-wdk, and reset-selected-wallets.
Worklet Context
You can bind the shipped RPC handlers to your Bare worklet using registerRpcHandlers():
const { registerRpcHandlers } = require('@tetherto/pear-wrk-wdk/worklet')
const { WDK } = require('@tetherto/wdk')
const EvmWalletManager = require('@tetherto/wdk-wallet-evm')
const SparkWalletManager = require('@tetherto/wdk-wallet-spark')
const context = {
wdk: null,
WDK,
walletManagers: {
ethereum: EvmWalletManager,
spark: SparkWalletManager
},
protocolManagers: {},
wdkLoadError: null
}
module.exports = (rpc) => {
registerRpcHandlers(rpc, context)
}Required Context Fields
wdk: The current WDK instance. Set this tonullbefore the first initialization.WDK: The WDK constructor used to create the seeded instance.walletManagers: A map from blockchain name to wallet manager implementation.protocolManagers: A map from protocol name to protocol manager implementation.wdkLoadError: Any startup error captured while loading WDK. Usenullwhen there is no load failure.
Worklet Config Payload
Both initializeWDK() and resetWdkWallets() expect config to be a JSON string. The decoded object must contain at least one entry under networks.
const workletConfig = {
networks: {
ethereum: {
blockchain: 'ethereum',
config: {
provider: 'https://rpc.ankr.com/eth_sepolia'
}
}
},
protocols: {
moonpay: {
blockchain: 'ethereum',
protocolName: 'moonpay',
config: {
environment: 'sandbox'
}
}
}
}Payload Rules
networksis required and must contain at least one network entry.- Each network entry must include
blockchainand an objectconfig. protocolsis optional during initialization.resetWdkWallets()reads only thenetworksportion of the decoded config.
Initialize WDK
You can create and register the WDK instance inside the worklet using initializeWDK():
const { HRPC } = require('@tetherto/pear-wrk-wdk')
const hrpc = new HRPC(ipcStream)
await hrpc.initializeWDK({
encryptionKey: secrets.encryptionKey,
encryptedSeed: secrets.encryptedSeedBuffer,
config: JSON.stringify(workletConfig)
})Initialization Rules
- Pass both
encryptionKeyandencryptedSeed, or omit both together. - On first initialization, the worklet must receive an encrypted seed pair so it can create
context.wdk. - If
context.wdkalready exists, a laterinitializeWDK()call disposes the existing instance before re-registering wallets and protocols from the new config.
Reset Selected Wallets
You can selectively dispose and re-register wallet modules using resetWdkWallets():
await hrpc.resetWdkWallets({
config: JSON.stringify({
networks: {
ethereum: {
blockchain: 'ethereum',
config: {
provider: 'https://rpc.ankr.com/eth_sepolia'
}
}
}
})
})Reset Rules
resetWdkWallets()requires an existing initializedcontext.wdk.- The handler calls
wdk.dispose(targetChains)with the blockchains extracted fromconfig.networks. - Only wallets listed in the request
networksobject are re-registered. - The reset flow does not re-register protocols.
Call Wallet Methods
You can execute wallet account methods through callMethod():
const result = await hrpc.callMethod({
methodName: 'getAddress',
network: 'ethereum',
accountIndex: 0
})Call Method Notes
argsis optional and must be a JSON string when provided.optionsis optional and must be a JSON string when provided.- When
argsdecodes to an array, the handler spreads the values as positional method arguments. - When
argsdecodes to an object or primitive, the handler passes it as a single argument.