ethereum - type assertion

hop6·2022년 6월 5일
0

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


ether-typechecker

Simple and tiny ethereum type checker in Typescript.

Install

npm install i ether-typechecker

Feature

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

Examples

// 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);

0개의 댓글