icon-sdk-js icon indicating copy to clipboard operation
icon-sdk-js copied to clipboard

Need utility function for validating address

Open octocat512 opened this issue 4 years ago • 2 comments

octocat512 avatar Feb 18 '21 15:02 octocat512

Agreed.

We use the following code if that may help :

export const isICONAddress = function (address) {
  return /^(hx|cx)?[0-9a-f]{40}$/i.test(address)
}
export const isICONAddressContract = function (address) {
  return /^(cx)?[0-9a-f]{40}$/i.test(address)
}
export const isICONAddressEOA = function (address) {
  return /^(hx)?[0-9a-f]{40}$/i.test(address)
}

ICONationTeam avatar Feb 18 '21 15:02 ICONationTeam

Actually I just noticed there are already methods for that implemented in the SDK :

  • Validator.isAddress
  • Validator.isEoaAddress
  • Validator.isScoreAddress

https://github.com/icon-project/icon-sdk-js/blob/3ab30c0dbc2759cb480942b8463044fd0c89a703/lib/data/Validator.js#L59-L83

ICONationTeam avatar Feb 18 '21 15:02 ICONationTeam