mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-09 10:41:56 +00:00
efccac79ad
* Refactor BaseNode to be an interface INode * Initial contract commit * Remove redundant fallback ABI function * First working iteration of Contract generator to be used in ENS branch * Hide abi to clean up logging output * Strip 0x prefix from output decode * Handle unnamed output params * Implement ability to supply output mappings to ABI functions * Fix null case in outputMapping * Add flow typing * Add .call method to functions * Partial commit for type refactor * Temp contract type fix -- waiting for NPM modularization * Misc. Optimizations to tsconfig + webpack * Convert Contracts to TS * Remove nested prop passing from contracts, get rid of contract reducers / sagas / redux state * Add disclaimer modal to footer * Remove duplicate code & unnecessary styles * Add contracts to nav * Wrap Contracts in App * Add ether/hex validation override for contract creation calls * First iteration of working deploy contract * Delete routing file that shouldnt exist * Revert "Misc. Optimizations to tsconfig + webpack" This reverts commit 70cba3a07f4255153a9e277b3c41032a4b661c94. * Cleanup HOC code * Fix formatting noise * remove un-used css style * Remove deterministic contract address computation * Remove empty files * Cleanup contract * Add call request to node interface * Fix output mapping types * Revert destructuring overboard * Add sendCallRequest to rpcNode class and add typing * Use enum for selecting ABI methods * Fix tslint error & add media query for modals * Nest Media Query * Fix contracts to include new router fixes * Add transaction capability to contracts * Get ABI parsing + contract calls almost fully integrated using dynamic contract parser lib * Refactor contract deploy to have a reusable HOC for contract interact * Move modal and tx comparasion up file tree * Include ABI outputs in display * Cleanup privaite/public members * Remove broadcasting step from a contract transaction * Update TX contract components to inter-op with interact and deploy * Finish contracts-interact functionality * Add transaction capability to contracts * Cleanup privaite/public members * Remove broadcasting step from a contract transaction * Apply James's CSS fix * Cleanup uneeded types * Remove unecessary class * Add UI side validation and helper utils, addresess PR comments * Fix spacing + remove unused imports / types * Fix spacing + remove unused imports / types * Address PR comments * Actually address PR comments * Actually address PR comments
180 lines
4.8 KiB
TypeScript
180 lines
4.8 KiB
TypeScript
import { toChecksumAddress } from 'ethereumjs-util';
|
|
import { RawTransaction } from 'libs/transaction';
|
|
import WalletAddressValidator from 'wallet-address-validator';
|
|
import { normalise } from './ens';
|
|
|
|
export function isValidETHAddress(address: string): boolean {
|
|
if (!address) {
|
|
return false;
|
|
}
|
|
if (address === '0x0000000000000000000000000000000000000000') {
|
|
return false;
|
|
}
|
|
return validateEtherAddress(address);
|
|
}
|
|
|
|
export function isValidBTCAddress(address: string): boolean {
|
|
return WalletAddressValidator.validate(address, 'BTC');
|
|
}
|
|
|
|
export function isValidHex(str: string): boolean {
|
|
if (typeof str !== 'string') {
|
|
return false;
|
|
}
|
|
if (str === '') {
|
|
return true;
|
|
}
|
|
str =
|
|
str.substring(0, 2) === '0x'
|
|
? str.substring(2).toUpperCase()
|
|
: str.toUpperCase();
|
|
const re = /^[0-9A-F]*$/g; // Match 0 -> unlimited times, 0 being "0x" case
|
|
return re.test(str);
|
|
}
|
|
|
|
export function isValidENSorEtherAddress(address: string): boolean {
|
|
return isValidETHAddress(address) || isValidENSAddress(address);
|
|
}
|
|
|
|
export function isValidENSName(str: string) {
|
|
try {
|
|
return (
|
|
str.length > 6 && normalise(str) !== '' && str.substring(0, 2) !== '0x'
|
|
);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isValidENSAddress(address: string): boolean {
|
|
try {
|
|
const normalized = normalise(address);
|
|
const tld = normalized.substr(normalized.lastIndexOf('.') + 1);
|
|
const validTLDs = {
|
|
eth: true,
|
|
test: true,
|
|
reverse: true
|
|
};
|
|
if (validTLDs[tld]) {
|
|
return true;
|
|
}
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isChecksumAddress(address: string): boolean {
|
|
return address === toChecksumAddress(address);
|
|
}
|
|
|
|
// FIXME we probably want to do checksum checks sideways
|
|
function validateEtherAddress(address: string): boolean {
|
|
if (address.substring(0, 2) !== '0x') {
|
|
return false;
|
|
} else if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
return false;
|
|
} else if (
|
|
/^(0x)?[0-9a-f]{40}$/.test(address) ||
|
|
/^(0x)?[0-9A-F]{40}$/.test(address)
|
|
) {
|
|
return true;
|
|
} else {
|
|
return isChecksumAddress(address);
|
|
}
|
|
}
|
|
|
|
export function isValidPrivKey(privkey: string | Buffer): boolean {
|
|
if (typeof privkey === 'string') {
|
|
return privkey.length === 64;
|
|
} else if (privkey instanceof Buffer) {
|
|
return privkey.length === 32;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isValidEncryptedPrivKey(privkey: string): boolean {
|
|
if (typeof privkey === 'string') {
|
|
return privkey.length === 128 || privkey.length === 132;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isPositiveIntegerOrZero(num: number): boolean {
|
|
if (isNaN(num) || !isFinite(num)) {
|
|
return false;
|
|
}
|
|
return num >= 0 && parseInt(num.toString(), 10) === num;
|
|
}
|
|
|
|
export function isValidRawTx(rawTx: RawTransaction): boolean {
|
|
const propReqs = [
|
|
{ name: 'nonce', type: 'string', lenReq: true },
|
|
{ name: 'gasPrice', type: 'string', lenReq: true },
|
|
{ name: 'gasLimit', type: 'string', lenReq: true },
|
|
{ name: 'to', type: 'string', lenReq: true },
|
|
{ name: 'value', type: 'string', lenReq: true },
|
|
{ name: 'data', type: 'string', lenReq: false },
|
|
{ name: 'chainId', type: 'number', lenReq: false }
|
|
];
|
|
|
|
// ensure rawTx has above properties
|
|
// ensure all specified types match
|
|
// ensure length !0 for strings where length is required
|
|
// ensure valid hex for strings
|
|
// ensure all strings begin with '0x'
|
|
// ensure valid address for 'to' prop
|
|
// ensure rawTx only has above properties
|
|
|
|
for (const prop of propReqs) {
|
|
const value = rawTx[prop.name];
|
|
|
|
if (!rawTx.hasOwnProperty(prop.name)) {
|
|
return false;
|
|
}
|
|
if (typeof value !== prop.type) {
|
|
return false;
|
|
}
|
|
if (prop.type === 'string') {
|
|
if (prop.lenReq && value.length === 0) {
|
|
return false;
|
|
}
|
|
if (value.length && value.substring(0, 2) !== '0x') {
|
|
return false;
|
|
}
|
|
if (!isValidHex(value)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Object.keys(rawTx).length !== propReqs.length) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Full length deterministic wallet paths from BIP44
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
// normal path length is 4, ledger is the exception at 3
|
|
export function isValidPath(dPath: string) {
|
|
// TODO: use a regex to detect proper paths
|
|
const len = dPath.split("'/").length;
|
|
return len === 3 || len === 4;
|
|
}
|
|
|
|
export const isValidValue = (value: string) =>
|
|
!!(value && isFinite(parseFloat(value)) && parseFloat(value) >= 0);
|
|
|
|
export const isValidGasPrice = (gasLimit: string) =>
|
|
!!(gasLimit && isFinite(parseFloat(gasLimit)) && parseFloat(gasLimit) > 0);
|
|
|
|
export const isValidByteCode = (byteCode: string) =>
|
|
byteCode && byteCode.length > 0 && byteCode.length % 2 === 0;
|
|
|
|
export const isValidAbiJson = (abiJson: string) =>
|
|
abiJson && abiJson.startsWith('[') && abiJson.endsWith(']');
|