@okxweb3/coin-kaia
Version:
An kaia SDK for building Web3 wallets and applications.
127 lines • 4.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.poll = exports.sleep = exports.populateFeePayerAndSignatures = exports.populateChainId = exports.eip155sign = exports.populateGasPrice = exports.populateGasLimit = exports.populateNonce = exports.populateTo = exports.populateFrom = exports.getTransactionRequest = void 0;
const ethers6_1 = require("ethers6");
const lodash_1 = __importDefault(require("lodash"));
const js_ext_core_1 = require("@kaiachain/js-ext-core");
async function getTransactionRequest(transactionOrRLP) {
if (lodash_1.default.isString(transactionOrRLP)) {
return (0, js_ext_core_1.parseTransaction)(transactionOrRLP);
}
else {
if (transactionOrRLP instanceof ethers6_1.Transaction) {
return transactionOrRLP.toJSON();
}
const resolvedTx = await (0, ethers6_1.resolveProperties)(transactionOrRLP);
if (typeof resolvedTx?.type === "string") {
resolvedTx.type = (0, js_ext_core_1.parseTxType)(resolvedTx.type);
}
return resolvedTx;
}
}
exports.getTransactionRequest = getTransactionRequest;
async function populateFrom(tx, expectedFrom) {
if (!tx.from || tx.from == "0x") {
tx.from = expectedFrom;
}
else {
(0, ethers6_1.assert)(tx.from?.toString().toLowerCase() === expectedFrom?.toLowerCase(), `from address mismatch (wallet address=${expectedFrom}) (tx.from=${tx.from})`, "INVALID_ARGUMENT", { argument: "from", value: tx.from });
tx.from = expectedFrom;
}
}
exports.populateFrom = populateFrom;
async function populateTo(tx) {
if (!tx.to || tx.to == "0x") {
tx.to = ethers6_1.ZeroAddress;
}
else {
tx.to = await (0, ethers6_1.resolveAddress)(tx.to, null);
}
}
exports.populateTo = populateTo;
async function populateNonce(tx, fromAddress) {
if (!tx.nonce) {
throw new Error("MISSING_ARGUMENT");
}
}
exports.populateNonce = populateNonce;
async function populateGasLimit(tx) {
if (!tx.gasLimit) {
throw new Error("MISSING_ARGUMENT");
}
}
exports.populateGasLimit = populateGasLimit;
async function populateGasPrice(tx) {
if (!tx.gasPrice) {
throw new Error("MISSING_ARGUMENT");
}
}
exports.populateGasPrice = populateGasPrice;
function eip155sign(key, digest, chainId) {
const sig = key.sign(digest);
const recoveryParam = sig.v === 27 ? 0 : 1;
const v = recoveryParam + +chainId * 2 + 35;
return { r: sig.r, s: sig.s, v };
}
exports.eip155sign = eip155sign;
async function populateChainId(tx) {
if (!tx.chainId) {
tx.chainId =
(0, js_ext_core_1.getChainIdFromSignatureTuples)(tx.txSignatures) ??
(0, js_ext_core_1.getChainIdFromSignatureTuples)(tx.feePayerSignatures);
}
}
exports.populateChainId = populateChainId;
async function populateFeePayerAndSignatures(tx, expectedFeePayer) {
if (!tx.feePayer || tx.feePayer == ethers6_1.ZeroAddress) {
tx.feePayer = expectedFeePayer;
}
else {
(0, ethers6_1.assert)(tx.feePayer.toLowerCase() === expectedFeePayer.toLowerCase(), "feePayer address mismatch", "INVALID_ARGUMENT", {
argument: "feePayer",
value: tx.feePayer,
});
tx.feePayer = expectedFeePayer;
}
if (lodash_1.default.isArray(tx.feePayerSignatures)) {
tx.feePayerSignatures = tx.feePayerSignatures.filter((sig) => {
return !(lodash_1.default.isArray(sig) &&
sig.length == 3 &&
sig[0] == "0x01" &&
sig[1] == "0x" &&
sig[2] == "0x");
});
}
}
exports.populateFeePayerAndSignatures = populateFeePayerAndSignatures;
function sleep(time) {
return new Promise((res, _) => {
setTimeout(() => res(), time);
});
}
exports.sleep = sleep;
async function poll(callback, verify, retries = 100) {
let result;
for (let i = 0; i < retries; i++) {
try {
const output = await callback();
if (output && verify(output)) {
result = output;
break;
}
}
catch (_) {
continue;
}
await sleep(250);
}
(0, ethers6_1.assert)(result, "Transaction timeout!", "NETWORK_ERROR", {
event: "pollTransactionInPool",
});
return result;
}
exports.poll = poll;
//# sourceMappingURL=txutil.js.map