@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
84 lines (80 loc) • 3.88 kB
JavaScript
'use strict';
var kit = require('@solana/kit');
var walletStandardFeatures = require('@solana/wallet-standard-features');
const getSolanaChainByChainConfig = chainConfig => {
switch (chainConfig.chainId) {
case "0x65":
return "solana:mainnet";
case "0x66":
return "solana:testnet";
case "0x67":
return "solana:devnet";
default:
return null;
}
};
const base58Decoder = kit.getBase58Decoder();
const base64Encoder = kit.getBase64Encoder();
const transactionDecoder = kit.getTransactionDecoder();
const serializeTx = transaction => new Uint8Array(base64Encoder.encode(kit.getBase64EncodedWireTransaction(transaction)));
/**
* Signs and sends a transaction via the wallet standard feature.
* Returns the transaction signature as a base58 string.
*/
const walletSignAndSendTransaction = async (wallet, transaction) => {
var _wallet$accounts, _wallet$chains;
const feature = wallet.features[walletStandardFeatures.SolanaSignAndSendTransaction];
if (!feature) throw new Error("Wallet does not support signAndSendTransaction");
const account = (_wallet$accounts = wallet.accounts) === null || _wallet$accounts === void 0 ? void 0 : _wallet$accounts[0];
const chain = (_wallet$chains = wallet.chains) === null || _wallet$chains === void 0 ? void 0 : _wallet$chains[0];
if (!account) throw new Error("No account found");
const [output] = await feature.signAndSendTransaction({
account,
transaction: serializeTx(transaction),
chain
});
return base58Decoder.decode(new Uint8Array(output.signature));
};
/**
* Signs a transaction via the wallet standard feature.
* Returns the first signature as a base58 string.
*/
const walletSignTransaction = async (wallet, transaction) => {
var _wallet$accounts2, _wallet$chains2;
const feature = wallet.features[walletStandardFeatures.SolanaSignTransaction];
if (!feature) throw new Error("Wallet does not support signTransaction");
const account = (_wallet$accounts2 = wallet.accounts) === null || _wallet$accounts2 === void 0 ? void 0 : _wallet$accounts2[0];
if (!account) throw new Error("No account found");
const chain = (_wallet$chains2 = wallet.chains) === null || _wallet$chains2 === void 0 ? void 0 : _wallet$chains2[0];
const [output] = await feature.signTransaction({
account,
transaction: serializeTx(transaction),
chain
});
// Extract the first signature from the signed transaction
const decodedTx = transactionDecoder.decode(output.signedTransaction);
const sig = decodedTx.signatures[account.address];
if (!sig) throw new Error("No signature in signed transaction");
return base58Decoder.decode(new Uint8Array(sig));
};
/**
* Signs a message via the wallet standard feature.
* Returns the signature as a base58 string.
*/
const walletSignMessage = async (wallet, message, from) => {
var _ref, _wallet$accounts3, _wallet$accounts4;
const feature = wallet.features[walletStandardFeatures.SolanaSignMessage];
if (!feature) throw new Error("Wallet does not support signMessage");
const account = (_ref = from ? (_wallet$accounts3 = wallet.accounts) === null || _wallet$accounts3 === void 0 ? void 0 : _wallet$accounts3.find(a => a.address === from) : null) !== null && _ref !== void 0 ? _ref : (_wallet$accounts4 = wallet.accounts) === null || _wallet$accounts4 === void 0 ? void 0 : _wallet$accounts4[0];
if (!account) throw new Error("No account found");
const msgBytes = new TextEncoder().encode(message);
const [output] = await feature.signMessage({
account,
message: msgBytes
});
return base58Decoder.decode(new Uint8Array(output.signature));
};
exports.getSolanaChainByChainConfig = getSolanaChainByChainConfig;
exports.walletSignAndSendTransaction = walletSignAndSendTransaction;
exports.walletSignMessage = walletSignMessage;
exports.walletSignTransaction = walletSignTransaction;