@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
201 lines (197 loc) • 8.54 kB
JavaScript
;
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var auth = require('@web3auth/auth');
var viem = require('viem');
function createWalletMiddlewareV2({
getAccounts,
getPrivateKey,
getPublicKey,
processEthSignMessage,
processPersonalMessage,
processTransaction,
processSignTransaction,
processTypedMessageV4,
processGetCapabilities,
processSendCalls,
processGetCallsStatus,
processShowCallsStatus
}) {
if (!getAccounts) throw new Error("opts.getAccounts is required");
async function validateAndNormalizeKeyholder(address, req) {
if (typeof address === "string" && address.length > 0) {
const accounts = await getAccounts(req);
const normalizedAddress = address.toLowerCase();
if (accounts.map(a => a.toLowerCase()).includes(normalizedAddress)) return normalizedAddress;
}
throw auth.rpcErrors.invalidParams({
message: `Invalid parameters: must provide an Ethereum address.`
});
}
// Account lookups
function ethAccountsHandler(p) {
return getAccounts(p.request);
}
// Tx signatures
async function ethSendTransactionHandler(p) {
var _req$params$, _req$params;
if (!processTransaction) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
const txParams = (_req$params$ = (_req$params = req.params) === null || _req$params === void 0 ? void 0 : _req$params[0]) !== null && _req$params$ !== void 0 ? _req$params$ : {
from: ""
};
txParams.from = await validateAndNormalizeKeyholder(txParams.from, req);
return processTransaction(txParams, req);
}
async function ethSignTransactionHandler(p) {
var _req$params$2, _req$params2;
if (!processSignTransaction) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
const txParams = (_req$params$2 = (_req$params2 = req.params) === null || _req$params2 === void 0 ? void 0 : _req$params2[0]) !== null && _req$params$2 !== void 0 ? _req$params$2 : {
from: ""
};
txParams.from = await validateAndNormalizeKeyholder(txParams.from, req);
return processSignTransaction(txParams, req);
}
// Message signatures
async function ethSignHandler(p) {
if (!processEthSignMessage) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
let msgParams;
if (Array.isArray(req.params)) {
if (req.params.length !== 2) throw new Error(`WalletMiddleware - incorrect params for eth_sign. expected [address, message]`);
const [address, message] = req.params;
msgParams = {
from: address,
data: message
};
} else {
var _req$params3;
msgParams = (_req$params3 = req.params) !== null && _req$params3 !== void 0 ? _req$params3 : {
from: "",
data: ""
};
}
return processEthSignMessage(msgParams, req);
}
async function ethSignTypedDataV4Handler(p) {
if (!processTypedMessageV4) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
if (!(req !== null && req !== void 0 && req.params)) throw new Error("WalletMiddleware - missing params");
let msgParams;
if (Array.isArray(req.params)) {
if (req.params.length !== 2) throw new Error(`WalletMiddleware - incorrect params for eth_signTypedData_v4. expected [address, typedData]`);
const [address, message] = req.params;
msgParams = {
from: address,
data: message
};
} else {
msgParams = req.params;
}
return processTypedMessageV4(msgParams, req);
}
async function personalSignHandler(p) {
if (!processPersonalMessage) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
let msgParams;
if (Array.isArray(req.params)) {
if (req.params.length < 2) throw new Error(`WalletMiddleware - incorrect params for personal_sign. expected [message, address]`);
const params = req.params;
if (typeof params[0] === "object" && params[0] !== null && "challenge" in params[0] && "address" in params[0]) {
const {
challenge,
address
} = params[0];
msgParams = {
from: address,
data: challenge
};
} else {
msgParams = {
from: params[1],
data: typeof params[0] === "string" ? params[0] : ""
};
}
} else {
var _req$params4;
msgParams = (_req$params4 = req.params) !== null && _req$params4 !== void 0 ? _req$params4 : {
from: "",
data: ""
};
}
return processPersonalMessage(msgParams, req);
}
async function ethPrivateKeyHandler(p) {
if (!getPrivateKey) throw auth.rpcErrors.methodNotSupported();
return getPrivateKey(p.request);
}
async function ethPublicKeyHandler(p) {
if (!getPublicKey) throw auth.rpcErrors.methodNotSupported();
return getPublicKey(p.request);
}
async function walletGetCapabilitiesHandler(p) {
var _req$params$3;
if (!processGetCapabilities) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
if (!req.params || !Array.isArray(req.params) || req.params.length < 2) throw auth.rpcErrors.invalidParams("Invalid parameters");
const account = req.params[0];
if (!viem.isHex(account)) throw auth.rpcErrors.invalidParams("Invalid account address");
let chainIds = (_req$params$3 = req.params[1]) !== null && _req$params$3 !== void 0 ? _req$params$3 : [];
if (!Array.isArray(chainIds)) throw auth.rpcErrors.invalidParams(`Invalid params, received: ${chainIds}. expected: Array`);
chainIds = chainIds.map(chainId => viem.isHex(chainId) ? chainId : viem.toHex(chainId));
const getCapabilitiesParams = [account, chainIds];
return processGetCapabilities(getCapabilitiesParams);
}
async function walletSendCallsHandler(p) {
if (!processSendCalls) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
const params = Array.isArray(req.params) ? req.params[0] : req.params;
if (!params || typeof params !== "object") throw auth.rpcErrors.invalidParams("Missing or invalid params for wallet_sendCalls");
if (!params.version || typeof params.version !== "string") throw auth.rpcErrors.invalidParams(`Invalid version: expected string, got "${params.version || "undefined"}"`);
if (!params.chainId) throw auth.rpcErrors.invalidParams("Missing required field: chainId");
if (!Array.isArray(params.calls) || params.calls.length === 0) throw auth.rpcErrors.invalidParams("calls must be a non-empty array");
const from = params.from;
if (from) await validateAndNormalizeKeyholder(from, req);
const walletSendCallsParams = _objectSpread(_objectSpread({}, params), {}, {
chainId: viem.isHex(params.chainId) ? params.chainId : viem.toHex(params.chainId)
});
return processSendCalls(walletSendCallsParams);
}
async function walletBatchCallStatusHandler(p) {
if (!processGetCallsStatus) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
const batchId = Array.isArray(req.params) ? req.params[0] : req.params;
if (!batchId || typeof batchId !== "string") throw auth.rpcErrors.invalidParams("Missing or invalid batchId");
return processGetCallsStatus(batchId);
}
async function walletShowCallsStatusHandler(p) {
if (!processShowCallsStatus) throw auth.rpcErrors.methodNotSupported();
const req = p.request;
const batchId = Array.isArray(req.params) ? req.params[0] : req.params;
if (!batchId || typeof batchId !== "string") throw auth.rpcErrors.invalidParams("Missing or invalid batchId");
await processShowCallsStatus(batchId);
return true;
}
return auth.createScaffoldMiddlewareV2({
// account lookups
eth_accounts: ethAccountsHandler,
eth_requestAccounts: ethAccountsHandler,
eth_private_key: ethPrivateKeyHandler,
eth_public_key: ethPublicKeyHandler,
public_key: ethPublicKeyHandler,
private_key: ethPrivateKeyHandler,
// tx signatures
eth_sendTransaction: ethSendTransactionHandler,
eth_signTransaction: ethSignTransactionHandler,
// message signatures
eth_sign: ethSignHandler,
eth_signTypedData_v4: ethSignTypedDataV4Handler,
personal_sign: personalSignHandler,
// EIP5792
wallet_getCapabilities: walletGetCapabilitiesHandler,
wallet_sendCalls: walletSendCallsHandler,
wallet_batchCallStatus: walletBatchCallStatusHandler,
wallet_showCallsStatus: walletShowCallsStatusHandler
});
}
exports.createWalletMiddlewareV2 = createWalletMiddlewareV2;