evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
195 lines • 9.01 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateERC20Transfer = validateERC20Transfer;
exports.validateEtherTransfer = validateEtherTransfer;
const ethers_1 = require("ethers");
const mathjs = __importStar(require("mathjs"));
const addressUtils = __importStar(require("./address-utils"));
const constants_1 = require("../common/constants");
const services_1 = require("../services");
function validateERC20Transfer(txData_1, validationData_1) {
return __awaiter(this, arguments, void 0, function* (txData, validationData, blockchainService = new services_1.BlockchainService(null)) {
try {
const { from: executorAddress, to: calledContractAddress, data, value, confirmations, } = txData;
const { signature, signContent, destinationAddress, amountData, minConfirmations, } = validationData;
if (minConfirmations && confirmations < minConfirmations) {
return {
isValid: false,
message: "confirmation too low",
code: constants_1.ERR_CODE.CONFIRMATION_TOO_LOW,
};
}
if (signature && signContent) {
const signerAddress = blockchainService.recoverMessageSigner(signContent, signature);
if (!addressUtils.areAddressesSame(signerAddress, executorAddress)) {
return {
isValid: false,
message: "signer is not transaction executor",
code: constants_1.ERR_CODE.INVALID_SIGNER_ADDRESS,
};
}
}
if (validationData.userAddress) {
if (!addressUtils.areAddressesSame(validationData.userAddress, executorAddress)) {
return {
isValid: false,
message: "executor is not user address",
code: constants_1.ERR_CODE.INVALID_SIGNER_ADDRESS,
};
}
}
const { args, signature: functionName } = yield blockchainService.parseERC20TxByNetwork(data, value, txData.hash);
if (functionName !== constants_1.ERC20_FN_SIGNATURE.TRANSFER) {
return {
isValid: false,
message: "invalid method",
code: constants_1.ERR_CODE.NOT_TRANSFER_METHOD,
};
}
const [transferredToAddress, transferredAmount] = args;
if (destinationAddress &&
!addressUtils.areAddressesSame(transferredToAddress, destinationAddress)) {
return {
isValid: false,
message: "invalid destination address",
code: constants_1.ERR_CODE.INVALID_DESTINATION_ADDRESS,
};
}
if (amountData === null || amountData === void 0 ? void 0 : amountData.length) {
const foundValidTransferAmountData = amountData.find((data) => {
if (!addressUtils.areAddressesSame(calledContractAddress, data.contractAddress)) {
return false;
}
if (!data.amount) {
return true;
}
const parsedAmount = ethers_1.ethers.utils.parseEther(data.amount);
if (!data.tolerancePercentage) {
return parsedAmount.eq(transferredAmount);
}
const parsedTransferredAmount = ethers_1.utils.formatEther(transferredAmount);
const priceDiff = mathjs.subtract(Number(data.amount), Number(parsedTransferredAmount));
if (priceDiff === 0) {
return true;
}
const diffPercentage = mathjs.divide(priceDiff, Number(parsedTransferredAmount));
return diffPercentage * 100 <= data.tolerancePercentage;
});
if (!foundValidTransferAmountData) {
return {
isValid: false,
message: "invalid currency or amount",
code: constants_1.ERR_CODE.INVALID_CURRENCY_OR_AMOUNT,
};
}
return {
isValid: true,
data: {
amountData: foundValidTransferAmountData,
},
};
}
return {
isValid: true,
};
}
catch (error) {
return {
isValid: false,
message: error.message,
};
}
});
}
function validateEtherTransfer(txData_1, validationData_1) {
return __awaiter(this, arguments, void 0, function* (txData, validationData, blockchainService = new services_1.BlockchainService(null)) {
try {
const { from: executorAddress, to: recipientAddress, confirmations, } = txData;
const { signature, signContent, destinationAddress, minConfirmations, userAddress, } = validationData;
if (minConfirmations && confirmations < minConfirmations) {
return {
isValid: false,
message: "confirmation too low",
code: constants_1.ERR_CODE.CONFIRMATION_TOO_LOW,
};
}
if (signature && signContent) {
const signerAddress = blockchainService.recoverMessageSigner(signContent, signature);
if (!addressUtils.areAddressesSame(signerAddress, executorAddress)) {
return {
isValid: false,
message: "signer is not transaction executor",
code: constants_1.ERR_CODE.INVALID_SIGNER_ADDRESS,
};
}
}
if (userAddress &&
!addressUtils.areAddressesSame(userAddress, executorAddress)) {
return {
isValid: false,
message: "executor is not user address",
code: constants_1.ERR_CODE.INVALID_SIGNER_ADDRESS,
};
}
if (!addressUtils.areAddressesSame(recipientAddress, destinationAddress)) {
return {
isValid: false,
message: "invalid recipient address",
code: constants_1.ERR_CODE.INVALID_DESTINATION_ADDRESS,
};
}
return {
isValid: true,
};
}
catch (error) {
return {
isValid: false,
message: error.message,
};
}
});
}
//# sourceMappingURL=validation-utils.js.map