Skip to content

Commit

Permalink
Merge branch 'main' into update/token-ownership-script
Browse files Browse the repository at this point in the history
  • Loading branch information
blockchainguyy authored Jul 30, 2024
2 parents 24e6207 + 71c829b commit 598ed8c
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 11 deletions.
51 changes: 51 additions & 0 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ function timeout(prom, time, exception) {
);
}

/**
* Determines if a given input is a valid keccak256 hash.
*
* @param {string} input - The string to validate.
* @returns {boolean} - Returns true if the input is a valid keccak256 hash, false otherwise.
*/
function isKeccak256Hash(input) {
// Ensure it's a string of 66 characters length and starts with '0x'
if (typeof input !== 'string' || input.length !== 66 || input.slice(0, 2) !== '0x') {
return false;
}

// Ensure all characters after the '0x' prefix are hexadecimal (0-9, a-f, A-F)
const hexPattern = /^[a-fA-F0-9]{64}$/;

return hexPattern.test(input.slice(2));
}

/**
* Validate if the input string matches the time format YYYY-MM-DDTHH:mm:ss
*
Expand All @@ -214,6 +232,37 @@ function isValidTimeFormat(timeString) {
return regex.test(timeString);
}

const validationFunctions = {
isNonEmptyString,
isNumber,
isValidNumber,
isValidDecimal,
isNumberArray,
isKeccak256Hash,
isString,
isNonEmptyStringArray,
isValidTimeFormat,
};

function validateParameters(parameters) {
for (const [validatorFunctionString, paramsObj] of Object.entries(parameters)) {
const validatorFunction = validationFunctions[validatorFunctionString];

if (typeof validatorFunction !== 'function') {
throw new Error(`Validator function ${validatorFunction} is not defined`);
}

for (const paramKey of Object.keys(paramsObj)) {
const paramValue = paramsObj[paramKey];
const isValid = validatorFunction(paramValue);

if (!isValid) {
throw new Error(`Input validation failed for ${validatorFunctionString} with parameter ${paramKey}: ${paramValue}`);
}
}
}
}

const dateToEta = (utcTimeString) => {
if (utcTimeString === '0') {
return 0;
Expand Down Expand Up @@ -290,6 +339,7 @@ module.exports = {
printWarn,
printError,
printLog,
isKeccak256Hash,
isNonEmptyString,
isString,
isStringArray,
Expand All @@ -311,4 +361,5 @@ module.exports = {
findProjectRoot,
toBigNumberString,
timeout,
validateParameters,
};
2 changes: 1 addition & 1 deletion sui/deploy-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { saveConfig, prompt, printInfo } = require('../evm/utils');
const { saveConfig, prompt, printInfo } = require('../common/utils');
const { Command } = require('commander');
const { loadSuiConfig, deployPackage, getBcsBytesByObjectId } = require('./utils');
const { singletonStruct } = require('./types-utils');
Expand Down
2 changes: 1 addition & 1 deletion sui/deploy-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Command, Option } = require('commander');
const { TxBuilder, updateMoveToml } = require('@axelar-network/axelar-cgp-sui');
const { bcs } = require('@mysten/bcs');
const { fromB64, toB64 } = require('@mysten/bcs');
const { saveConfig, printInfo, validateParameters, prompt, writeJSON } = require('../evm/utils');
const { saveConfig, printInfo, validateParameters, prompt, writeJSON } = require('../common/utils');
const { addBaseOptions } = require('./cli-utils');
const { getWallet } = require('./sign-utils');
const { loadSuiConfig, getObjectIdsByObjectTypes, suiPackageAddress } = require('./utils');
Expand Down
2 changes: 1 addition & 1 deletion sui/faucet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { addBaseOptions } = require('./cli-utils');
const { requestSuiFromFaucetV0, getFaucetHost } = require('@mysten/sui/faucet');
const { getWallet, printWalletInfo } = require('./sign-utils');
const { Command } = require('commander');
const { saveConfig, loadConfig, printInfo } = require('../evm/utils');
const { saveConfig, loadConfig, printInfo } = require('../common/utils');

async function processCommand(config, chain, options) {
const [keypair, client] = getWallet(chain, options);
Expand Down
2 changes: 1 addition & 1 deletion sui/gas-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { saveConfig, printInfo, printError } = require('../evm/utils');
const { saveConfig, printInfo, printError } = require('../common/utils');
const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
Expand Down
2 changes: 1 addition & 1 deletion sui/gateway.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { saveConfig, printInfo } = require('../evm/utils');
const { saveConfig, printInfo } = require('../common/utils');
const { Command, Option } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
Expand Down
2 changes: 1 addition & 1 deletion sui/generate-keypair.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { addBaseOptions } = require('./cli-utils');
const { generateKeypair, getRawPrivateKey } = require('./sign-utils');
const { Command, Option } = require('commander');
const { saveConfig, loadConfig, printInfo } = require('../evm/utils');
const { saveConfig, loadConfig, printInfo } = require('../common/utils');

const { ethers } = require('hardhat');
const { hexlify } = ethers.utils;
Expand Down
2 changes: 1 addition & 1 deletion sui/gmp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { saveConfig, printInfo } = require('../evm/utils');
const { saveConfig, printInfo } = require('../common/utils');
const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
Expand Down
2 changes: 1 addition & 1 deletion sui/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { addBaseOptions } = require('./cli-utils');
const { getWallet, getMultisig, signTransactionBlockBytes, broadcastSignature } = require('./sign-utils');
const { getSignedTx, storeSignedTx } = require('../evm/sign-utils');
const { loadSuiConfig } = require('./utils');
const { printInfo, validateParameters } = require('../evm/utils');
const { printInfo, validateParameters } = require('../common/utils');

async function signTx(keypair, client, options) {
const txFileData = getSignedTx(options.txBlockPath);
Expand Down
2 changes: 1 addition & 1 deletion sui/sign-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { Secp256k1Keypair, Secp256k1PublicKey } = require('@mysten/sui/keypairs/s
const { Secp256r1Keypair, Secp256r1PublicKey } = require('@mysten/sui/keypairs/secp256r1');
const { SuiClient, getFullnodeUrl } = require('@mysten/sui/client');
const { fromB64, fromHEX } = require('@mysten/bcs');
const { printInfo } = require('../evm/utils');
const { printInfo } = require('../common/utils');
const { ethers } = require('hardhat');
const {
utils: { hexlify },
Expand Down
2 changes: 1 addition & 1 deletion sui/transfer-object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Transaction } = require('@mysten/sui/transactions');
const { Command, Option } = require('commander');
const { printInfo, validateParameters } = require('../evm/utils');
const { printInfo, validateParameters } = require('../common/utils');
const { addExtendedOptions } = require('./cli-utils');
const { getWallet, printWalletInfo } = require('./sign-utils');
const { loadSuiConfig } = require('./utils');
Expand Down
4 changes: 3 additions & 1 deletion sui/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { ethers } = require('hardhat');
const { loadConfig } = require('../evm/utils');
const { loadConfig } = require('../common/utils');
const {
BigNumber,
utils: { arrayify, hexlify },
Expand Down Expand Up @@ -87,6 +87,8 @@ const getObjectIdsByObjectTypes = (txn, objectTypes) =>
if (!objectId) {
throw new Error(`No object found for type: ${objectType}`);
}

return objectId;
});

module.exports = {
Expand Down

0 comments on commit 598ed8c

Please sign in to comment.