@kaiachain/web3js-ext
Version:
web3.js extension for kaiachain blockchain
71 lines (67 loc) • 3.38 kB
JavaScript
;
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
// Taken from https://github.com/web3/web3.js/blob/v4.3.0/packages/web3-eth/src/utils/transaction_builder.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTransactionType = exports.getTransactionFromOrToAttr = void 0;
const web3_errors_1 = require("web3-errors");
const web3_eth_1 = require("web3-eth");
const web3_eth_accounts_1 = require("web3-eth-accounts");
const web3_types_1 = require("web3-types");
const web3_utils_1 = require("web3-utils");
const web3_validator_1 = require("web3-validator");
const getTransactionFromOrToAttr = (attr, web3Context, transaction, privateKey) => {
if (transaction !== undefined && attr in transaction && transaction[attr] !== undefined) {
if (typeof transaction[attr] === "string" && (0, web3_validator_1.isAddress)(transaction[attr])) {
return transaction[attr];
}
if (!(0, web3_validator_1.isHexStrict)(transaction[attr]) && (0, web3_validator_1.isNumber)(transaction[attr])) {
if (web3Context.wallet) {
const account = web3Context.wallet.get((0, web3_utils_1.format)({ format: "uint" }, transaction[attr], web3_eth_1.NUMBER_DATA_FORMAT));
if (!(0, web3_validator_1.isNullish)(account)) {
return account.address;
}
throw new web3_errors_1.LocalWalletNotAvailableError();
}
throw new web3_errors_1.LocalWalletNotAvailableError();
}
else {
throw attr === "from"
? new web3_errors_1.InvalidTransactionWithSender(transaction.from)
: // eslint-disable-next-line @typescript-eslint/no-unsafe-call
new web3_errors_1.InvalidTransactionWithReceiver(transaction.to);
}
}
if (attr === "from") {
if (!(0, web3_validator_1.isNullish)(privateKey)) {
return (0, web3_eth_accounts_1.privateKeyToAddress)(privateKey);
}
if (!(0, web3_validator_1.isNullish)(web3Context.defaultAccount)) {
return web3Context.defaultAccount;
}
}
return undefined;
};
exports.getTransactionFromOrToAttr = getTransactionFromOrToAttr;
const getTransactionType = (transaction, web3Context) => {
const inferredType = (0, web3_eth_1.detectTransactionType)(transaction, web3Context);
if (!(0, web3_validator_1.isNullish)(inferredType)) {
return inferredType;
}
if (!(0, web3_validator_1.isNullish)(web3Context.defaultTransactionType)) {
return (0, web3_utils_1.format)({ format: "uint" }, web3Context.defaultTransactionType, web3_types_1.ETH_DATA_FORMAT);
}
return undefined;
};
exports.getTransactionType = getTransactionType;