Typescript library for type check in ethereum.
ethers.js 에서 아래와 같이 타입 체크해주는 것 보고,
// is block hash?
if (!isHexString(blockHash, 32)) {
// throw error
}
// is address ?
if (!isHexString(address, 20)) {
// throw error
}
https://github.com/dbadoy/ether-typechecker
Simple and tiny ethereum type checker in Typescript.
npm install i ether-typechecker
EtherTypeAssertion(typc: typeChecker, value: string, postFnOrErrorMesg: postFuncion | string)
/*
interface typeChecker {
(value: string): boolean;
}
interface postFuncion {
(): void;
}
*/
// typeCheckers
isPublicKey(value: string): boolean
isPrivateKey(value: string): boolean
isBlockHash(value: string): boolean
isBlockHashOrBlockTag(value: string): boolean
isFromBlockOrBlockHash(value: string): boolean
isTopic(value: string): boolean
isAddress(value: string): boolean
isVerifyingContract(value: string): boolean
// use methods
let addr = "0x564369022fDE19d63c6d72a23b48Ad4e20CE235C";
if (!isAddress(addr) {
// throw error
}
// use TypeAssertion
// case 1
// throw errors if type assertion failed.
EtherTypeAssertion(isAddress, addr, "is not address.");
// case 2
// call custom function if type assertion failed.
let doSomething = () => {
// logic ...
}
EtherTypeAssertion(isAddress, addr, doSomthing);