-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23db732
commit b4c496d
Showing
18 changed files
with
160 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
isHexString, | ||
isValidAddress, | ||
isValidChecksumAddress, | ||
} from 'ethereumjs-util'; | ||
|
||
export const BURN_ADDRESS = '0x0000000000000000000000000000000000000000'; | ||
|
||
export function isBurnAddress(address) { | ||
return address === BURN_ADDRESS; | ||
} | ||
|
||
export function isValidHexAddress(possibleAddress) { | ||
if (!isHexString(possibleAddress)) { | ||
return false; | ||
} | ||
|
||
const prefixRemoved = possibleAddress.slice(2); | ||
const lower = prefixRemoved.toLowerCase(); | ||
const upper = prefixRemoved.toUpperCase(); | ||
const allOneCase = prefixRemoved === lower || prefixRemoved === upper; | ||
if (!allOneCase) { | ||
return isValidChecksumAddress(possibleAddress); | ||
} | ||
|
||
return isValidAddress(possibleAddress); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { strict as assert } from 'assert'; | ||
import { toChecksumAddress } from 'ethereumjs-util'; | ||
import { isValidHexAddress } from './hexstring-utils'; | ||
|
||
describe('hexstring utils', function () { | ||
describe('isValidHexAddress', function () { | ||
it('should NOT allow 40-char non-prefixed hex', function () { | ||
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b825'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, false); | ||
}); | ||
|
||
it('should allow 42-char prefixed hex', function () { | ||
const address = '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, true); | ||
}); | ||
|
||
it('should NOT allow any length of non hex-prefixed string', function () { | ||
const address = 'fdea65c8e26263f6d9a1b5de9555d2931a33b85'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, false); | ||
}); | ||
|
||
it('should NOT allow less than 42 character hex-prefixed string', function () { | ||
const address = '0xfdea65ce26263f6d9a1b5de9555d2931a33b85'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, false); | ||
}); | ||
|
||
it('should recognize correct capitalized checksum', function () { | ||
const address = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, true); | ||
}); | ||
|
||
it('should recognize incorrect capitalized checksum', function () { | ||
const address = '0xFDea65C8e26263F6d9A1B5de9555D2931A33b825'; | ||
const result = isValidHexAddress(address); | ||
assert.equal(result, false); | ||
}); | ||
|
||
it('should recognize this sample hashed address', function () { | ||
const address = '0x5Fda30Bb72B8Dfe20e48A00dFc108d0915BE9Bb0'; | ||
const result = isValidHexAddress(address); | ||
const hashed = toChecksumAddress(address.toLowerCase()); | ||
assert.equal(hashed, address); | ||
assert.equal(result, true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.