-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add validateAndParseAddress function
- Loading branch information
1 parent
fe8b1a5
commit c067fc4
Showing
3 changed files
with
30 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { validateAndParseAddress } from '../../src/utils/address'; | ||
|
||
describe('validateAndParseAddress', () => { | ||
test('should pass when correct starknet address is passed', () => { | ||
const addr = '0x07ee790591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'; | ||
|
||
return expect(validateAndParseAddress(addr)).toEqual(addr); | ||
}); | ||
|
||
test('should add 0x prefix if not provided', () => { | ||
const addr = '07ee790591d9fa3efc87067d95a643f8455e0b8190eb8cb7bfd39e4fb7571fdf'; | ||
|
||
return expect(validateAndParseAddress(addr)).toEqual(`0x${addr}`); | ||
}); | ||
}); |
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,14 @@ | ||
export function validateAndParseAddress(address: string): string { | ||
let result = null; | ||
if (typeof address !== 'string') { | ||
throw new Error('Invalid Address Type'); | ||
} | ||
|
||
if (!address.match(/^(0x)?[0-9a-fA-F]{63,64}$/)) { | ||
throw new Error('Invalid Address Format'); | ||
} | ||
|
||
result = address.substring(0, 2) === '0x' ? address : `0x${address}`; | ||
|
||
return result; | ||
} |