-
Notifications
You must be signed in to change notification settings - Fork 832
feat(utils): support shortstring and uint256 #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,22 @@ | ||
| import { decodeShortString, encodeShortString } from '../../src/utils/shortString'; | ||
|
|
||
| describe('shortString', () => { | ||
| test('should convert string to number', () => { | ||
| expect(encodeShortString('hello')).toMatchInlineSnapshot(`"0x68656c6c6f"`); | ||
| }); | ||
| test('should convert number to string', () => { | ||
| expect(decodeShortString('0x68656c6c6f')).toMatchInlineSnapshot(`"hello"`); | ||
| }); | ||
| test('should throw if string is too long', () => { | ||
| expect(() => | ||
| encodeShortString('hello world hello world hello world hello world hello world hello world') | ||
| ).toThrowErrorMatchingInlineSnapshot( | ||
| `"hello world hello world hello world hello world hello world hello world is too long"` | ||
| ); | ||
| }); | ||
| test('should throw if string contains non ascii chars', () => { | ||
| expect(() => encodeShortString('hello\uD83D\uDE00')).toThrowErrorMatchingInlineSnapshot( | ||
| `"hello😀 is not an ASCII string"` | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,32 @@ | ||
| import { ONE } from '../../src/constants'; | ||
| import { toBN } from '../../src/utils/number'; | ||
| import { UINT_128_MAX, UINT_256_MAX, bnToUint256, uint256ToBN } from '../../src/utils/uint256'; | ||
|
|
||
| describe('cairo uint256', () => { | ||
| test('should convert 0 from BN to uint256 struct', () => { | ||
| const uint256 = bnToUint256('0'); | ||
| expect(uint256).toMatchInlineSnapshot(` | ||
| Object { | ||
| "high": "0x0", | ||
| "low": "0x0", | ||
| } | ||
| `); | ||
| }); | ||
| test('should convert 0 from uint256 to BN', () => { | ||
| expect(uint256ToBN({ low: '0x0', high: '0x0' }).toString()).toMatchInlineSnapshot(`"0"`); | ||
| }); | ||
| test('should convert BN over 2^128 to uint256 struct', () => { | ||
| const uint256 = bnToUint256(UINT_128_MAX.add(ONE)); | ||
| expect(uint256).toMatchInlineSnapshot(` | ||
| Object { | ||
| "high": "0x1", | ||
| "low": "0x0", | ||
| } | ||
| `); | ||
| }); | ||
| test('should throw if BN over uint256 range', () => { | ||
| expect(() => bnToUint256(UINT_256_MAX.add(toBN(1)))).toThrowErrorMatchingInlineSnapshot( | ||
| `"Number is too large"` | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| import { addHexPrefix, removeHexPrefix } from './encode'; | ||
|
|
||
| export function isASCII(str: string) { | ||
| // eslint-disable-next-line no-control-regex | ||
| return /^[\x00-\x7F]*$/.test(str); | ||
| } | ||
|
|
||
| // function to check if string has less or equal 31 characters | ||
| export function isShortString(str: string) { | ||
| return str.length <= 31; | ||
| } | ||
|
|
||
| export function encodeShortString(str: string) { | ||
| if (!isASCII(str)) throw new Error(`${str} is not an ASCII string`); | ||
| if (!isShortString(str)) throw new Error(`${str} is too long`); | ||
| return addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16))); | ||
| } | ||
|
|
||
| export function decodeShortString(str: string) { | ||
| return removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16))); | ||
| } |
This file contains hidden or 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 hidden or 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,32 @@ | ||
| import { addHexPrefix } from './encode'; | ||
| import { BigNumberish, toBN } from './number'; | ||
|
|
||
| // Represents an integer in the range [0, 2^256). | ||
| export interface Uint256 { | ||
| // The low 128 bits of the value. | ||
| low: BigNumberish; | ||
| // The high 128 bits of the value. | ||
| high: BigNumberish; | ||
| } | ||
|
|
||
| // function to convert Uint256 to BN | ||
| export function uint256ToBN(uint256: Uint256) { | ||
| return toBN(uint256.high).shln(128).add(toBN(uint256.low)); | ||
| } | ||
|
|
||
| export const UINT_128_MAX = toBN(1).shln(128).sub(toBN(1)); | ||
| export const UINT_256_MAX = toBN(1).shln(256).sub(toBN(1)); | ||
| // function to check if BN is smaller or equal 2**256-1 | ||
| export function isUint256(bn: BigNumberish): boolean { | ||
| return toBN(bn).lte(UINT_256_MAX); | ||
| } | ||
|
|
||
| // function to convert BN to Uint256 | ||
| export function bnToUint256(bignumber: BigNumberish): Uint256 { | ||
| const bn = toBN(bignumber); | ||
| if (!isUint256(bn)) throw new Error('Number is too large'); | ||
| return { | ||
| low: addHexPrefix(bn.maskn(128).toString(16)), | ||
| high: addHexPrefix(bn.shrn(128).toString(16)), | ||
| }; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.