@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
210 lines (205 loc) • 6.89 kB
JavaScript
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
import { METHOD_TYPES, EIP_5792_METHODS, EIP_7702_METHODS } from '@toruslabs/ethereum-controllers';
import { createScaffoldMiddlewareV2, providerErrors, rpcErrors } from '@web3auth/auth';
async function createAaMiddleware({
eoaProvider,
handlers
}) {
const [eoaAddress] = await eoaProvider.request({
method: METHOD_TYPES.GET_ACCOUNTS
});
/**
* 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 handlers.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.`
});
}
async function normalizeSignSenderAddress(address, req) {
// sender is EOA address
if (address.toLowerCase() === eoaAddress.toLowerCase()) {
return eoaAddress;
}
const [smartAccountAddress] = await handlers.getAccounts(req);
// sender is smart account address
if (address.toLowerCase() === smartAccountAddress.toLowerCase()) {
// use EOA address as sender for signing
return eoaAddress;
}
throw rpcErrors.invalidParams({
message: `Invalid parameters: must provide valid sender address.`
});
}
async function lookupAccounts(params) {
return handlers.getAccounts(params.request);
}
async function fetchPrivateKey(params) {
if (!handlers.getPrivateKey) {
throw rpcErrors.methodNotSupported();
}
return handlers.getPrivateKey(params.request);
}
async function sendTransaction(params) {
if (!handlers.processTransaction) {
throw rpcErrors.methodNotSupported();
}
const req = params.request;
const txParams = req.params[0] || {
from: ""
};
txParams.from = await validateAndNormalizeKeyholder(txParams.from, req);
return handlers.processTransaction(txParams, req);
}
async function signTransaction(params) {
const req = params.request;
const txParams = req.params[0] || {
from: ""
};
// normalize sender address
if (txParams.from) {
txParams.from = await normalizeSignSenderAddress(txParams.from, req);
}
return handlers.processSignTransaction(txParams, req);
}
async function ethSign(params) {
const req = params.request;
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);
return handlers.processEthSignMessage(msgParams, req);
}
async function signTypedDataV4(params) {
const req = params.request;
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
};
}
return handlers.processTypedMessageV4(msgParams, req);
}
async function personalSign(params) {
const req = params.request;
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);
return handlers.processPersonalMessage(msgParams, req);
}
async function fetchPublicKey(params) {
if (!handlers.getPublicKey) {
throw rpcErrors.methodNotSupported();
}
return handlers.getPublicKey(params.request);
}
return createScaffoldMiddlewareV2({
// account lookups
eth_accounts: lookupAccounts,
eth_requestAccounts: lookupAccounts,
eth_private_key: fetchPrivateKey,
private_key: fetchPrivateKey,
eth_public_key: fetchPublicKey,
public_key: fetchPublicKey,
// tx signatures
eth_sendTransaction: sendTransaction,
eth_signTransaction: signTransaction,
// message signatures
eth_sign: ethSign,
eth_signTypedData_v4: signTypedDataV4,
personal_sign: personalSign
});
}
async function createEoaMiddleware({
aaProvider
}) {
async function getAccounts() {
const [, eoaAddress] = await aaProvider.request({
method: METHOD_TYPES.GET_ACCOUNTS
});
return [eoaAddress];
}
async function requestAccounts() {
const [, eoaAddress] = await aaProvider.request({
method: METHOD_TYPES.ETH_REQUEST_ACCOUNTS
});
return [eoaAddress];
}
return createScaffoldMiddlewareV2({
eth_accounts: getAccounts,
eth_requestAccounts: requestAccounts
});
}
async function createEip7702And5792MiddlewareForAaProvider() {
const eip5792Methods = Object.values(EIP_5792_METHODS);
const eip7702Methods = Object.values(EIP_7702_METHODS);
const eip7702And5792Methods = [...eip5792Methods, ...eip7702Methods];
return async ({
request,
next
}) => {
if (eip7702And5792Methods.includes(request.method)) {
throw providerErrors.unsupportedMethod(`${request.method} is not supported for account abstraction provider`);
}
return next(request);
};
}
function providerAsMiddleware(provider) {
return async ({
request
}) => {
return provider.request({
method: request.method,
params: request.params
});
};
}
export { createAaMiddleware, createEip7702And5792MiddlewareForAaProvider, createEoaMiddleware, providerAsMiddleware };