@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
172 lines (168 loc) • 5.62 kB
JavaScript
;
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var auth = require('@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 auth.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 auth.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 auth.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 auth.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 auth.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 auth.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 auth.rpcErrors.methodNotSupported();
}
res.result = await getPrivateKey(req);
}
async function fetchPublicKey(req, res) {
if (!getPublicKey) {
throw auth.rpcErrors.methodNotSupported();
}
res.result = await getPublicKey(req);
}
return auth.createScaffoldMiddleware({
// account lookups
eth_accounts: auth.createAsyncMiddleware(lookupAccounts),
eth_requestAccounts: auth.createAsyncMiddleware(lookupAccounts),
eth_private_key: auth.createAsyncMiddleware(fetchPrivateKey),
eth_public_key: auth.createAsyncMiddleware(fetchPublicKey),
public_key: auth.createAsyncMiddleware(fetchPublicKey),
private_key: auth.createAsyncMiddleware(fetchPrivateKey),
// tx signatures
eth_sendTransaction: auth.createAsyncMiddleware(sendTransaction),
eth_signTransaction: auth.createAsyncMiddleware(signTransaction),
// message signatures
eth_sign: auth.createAsyncMiddleware(ethSign),
eth_signTypedData_v4: auth.createAsyncMiddleware(signTypedDataV4),
personal_sign: auth.createAsyncMiddleware(personalSign)
});
}
exports.createWalletMiddleware = createWalletMiddleware;