UNPKG

@web3auth/no-modal

Version:
178 lines (167 loc) 5.54 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import { createScaffoldMiddleware, createAsyncMiddleware, rpcErrors } from '@web3auth/auth'; function createWalletMiddleware({ getAccounts, getPrivateKey, getPublicKey, processEthSignMessage, processPersonalMessage, processTransaction, processSignTransaction, processTypedMessageV4 }) { if (!getAccounts) { throw new Error("opts.getAccounts is required"); } // // utility // /** * Validates the keyholder address, and returns a normalized (i.e. lowercase) * copy of it. * * an error */ async function validateAndNormalizeKeyholder(address, req) { if (typeof address === "string" && address.length > 0) { // ensure address is included in provided accounts const accounts = await getAccounts(req); const normalizedAccounts = accounts.map(_address => _address.toLowerCase()); const normalizedAddress = address.toLowerCase(); if (normalizedAccounts.includes(normalizedAddress)) { return normalizedAddress; } } throw rpcErrors.invalidParams({ message: `Invalid parameters: must provide an Ethereum address.` }); } // // account lookups // async function lookupAccounts(req, res) { res.result = await getAccounts(req); } // // transaction signatures // async function sendTransaction(req, res) { if (!processTransaction) { throw rpcErrors.methodNotSupported(); } const txParams = req.params[0] || { from: "" }; txParams.from = await validateAndNormalizeKeyholder(txParams.from, req); res.result = await processTransaction(txParams, req); } async function signTransaction(req, res) { if (!processSignTransaction) { throw rpcErrors.methodNotSupported(); } const txParams = req.params[0] || { from: "" }; txParams.from = await validateAndNormalizeKeyholder(txParams.from, req); res.result = await processSignTransaction(txParams, req); } // // message signatures // async function ethSign(req, res) { if (!processEthSignMessage) { throw rpcErrors.methodNotSupported(); } let msgParams = req.params; const extraParams = req.params[2] || {}; if (Array.isArray(req.params)) { if (!(req.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${req.method} method. expected [address, message]`); const params = req.params; const address = params[0]; const message = params[1]; msgParams = { from: address, data: message }; } msgParams = _objectSpread(_objectSpread({}, extraParams), msgParams); res.result = await processEthSignMessage(msgParams, req); } async function signTypedDataV4(req, res) { if (!processTypedMessageV4) { throw rpcErrors.methodNotSupported(); } if (!(req !== null && req !== void 0 && req.params)) throw new Error("WalletMiddleware - missing params"); let msgParams = req.params; if (Array.isArray(req.params)) { if (!(req.params.length === 2)) throw new Error(`WalletMiddleware - incorrect params for ${req.method} method. expected [address, typedData]`); const params = req.params; const address = params[0]; const message = params[1]; msgParams = { from: address, data: message }; } res.result = await processTypedMessageV4(msgParams, req); } async function personalSign(req, res) { if (!processPersonalMessage) { throw rpcErrors.methodNotSupported(); } let msgParams = req.params; const extraParams = req.params[2] || {}; if (Array.isArray(req.params)) { if (!(req.params.length >= 2)) throw new Error(`WalletMiddleware - incorrect params for ${req.method} method. expected [message, address]`); const params = req.params; if (typeof params[0] === "object") { const { challenge, address } = params[0]; msgParams = { from: address, data: challenge }; } else { const message = params[0]; const address = params[1]; msgParams = { from: address, data: message }; } } msgParams = _objectSpread(_objectSpread({}, extraParams), msgParams); res.result = await processPersonalMessage(msgParams, req); } async function fetchPrivateKey(req, res) { if (!getPrivateKey) { throw rpcErrors.methodNotSupported(); } res.result = await getPrivateKey(req); } async function fetchPublicKey(req, res) { if (!getPublicKey) { throw rpcErrors.methodNotSupported(); } res.result = await getPublicKey(req); } return createScaffoldMiddleware({ // account lookups eth_accounts: createAsyncMiddleware(lookupAccounts), eth_requestAccounts: createAsyncMiddleware(lookupAccounts), eth_private_key: createAsyncMiddleware(fetchPrivateKey), eth_public_key: createAsyncMiddleware(fetchPublicKey), public_key: createAsyncMiddleware(fetchPublicKey), private_key: createAsyncMiddleware(fetchPrivateKey), // tx signatures eth_sendTransaction: createAsyncMiddleware(sendTransaction), eth_signTransaction: createAsyncMiddleware(signTransaction), // message signatures eth_sign: createAsyncMiddleware(ethSign), eth_signTypedData_v4: createAsyncMiddleware(signTypedDataV4), personal_sign: createAsyncMiddleware(personalSign) }); } export { createWalletMiddleware };