UNPKG

@kaiachain/viem-ext

Version:
77 lines (76 loc) 2.25 kB
import { parseTransaction } from '@kaiachain/js-ext-core'; import { toHex } from 'viem'; export function isKaiaTransactionRequest(transactionOrRLP) { return typeof transactionOrRLP === 'object'; } export async function getTransactionRequestForSigning(client, transactionOrRLP) { let txObj; switch (typeof transactionOrRLP) { case 'string': txObj = parseTransaction(transactionOrRLP); break; case 'object': txObj = transactionOrRLP; break; default: throw new Error('Invalid transaction'); } if (typeof client?.chain?.id !== 'undefined') { txObj.chainId = client.chain.id; } // wallet such as kaia wallet will pre populated the fee payers signature will zero value, which cause r,s,v error. if (Array.isArray(txObj.feePayerSignatures)) { txObj.feePayerSignatures = txObj.feePayerSignatures.filter((sig) => { return !(Array.isArray(sig) && sig.length == 3 && sig[0] == "0x01" && sig[1] == "0x" && sig[2] == "0x"); }); } return txObj; } export function convertSignatureToKaiaFormat(signature, chainId) { const { r, s, yParity } = signature; const v = Number(yParity) + chainId * 2 + 35; return { r, s, v, }; } export const getValidRawRpcObj = (txObj) => { const result = {}; if (txObj.from) { result.from = txObj.from; } if (txObj.to) { result.to = txObj.to; } if (txObj.value) { result.value = toHex(txObj.value); } if (txObj.data) { result.data = txObj.data; } if (txObj.type) { result.type = txObj.type; } if (txObj.key) { result.key = txObj.key; } if (txObj.gasPrice) { result.gasPrice = txObj.gasPrice; } if (txObj.gasLimit) { result.gasLimit = txObj.gasLimit; } return result; }; export const getEstimateGasPayload = async (client, txObj) => { const estimatedGas = (await client.request({ method: 'klay_estimateGas', params: [getValidRawRpcObj(txObj)], })); return Math.floor(Number.parseInt(estimatedGas, 16) * 2.5); };