-
Notifications
You must be signed in to change notification settings - Fork 23
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
Improved schema validation for eth_getBalance #598
Draft
cygnusv
wants to merge
5
commits into
nucypher:epic-v0.6.x
Choose a base branch
from
cygnusv:blocknumber
base: epic-v0.6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−29
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ecfeed
refactor: improvements to eth_getBalance schema
cygnusv 978f56b
refactor: Don't use eth address regex, use existing schema instead
cygnusv f6084df
test: improve eth address schema unit tests
cygnusv 942258b
feat: new BlockIdentifierSchema
cygnusv 677e7e7
feat: Add block identifier schema validation to eth_getBalance
cygnusv 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 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 |
---|---|---|
@@ -1,15 +1,69 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { EthAddressSchema } from '../src'; | ||
import { EthAddressSchema, BlockIdentifierSchema } from '../src'; | ||
|
||
describe('ethereum address schema', () => { | ||
it('should accept valid ethereum address', () => { | ||
const validAddress = '0x1234567890123456789012345678901234567890'; | ||
EthAddressSchema.parse(validAddress); | ||
}); | ||
|
||
it('should reject invalid ethereum address', () => { | ||
it('should accept unchecksummed ethereum address', () => { | ||
const validAddress = '0x0123456789abcdefedcb0123456789abcdefedcb'; | ||
EthAddressSchema.parse(validAddress); | ||
}); | ||
|
||
it('should accept checksummed ethereum address', () => { | ||
const validAddress = '0x0123456789aBcDeFEdCb0123456789abcdEfeDcb'; | ||
EthAddressSchema.parse(validAddress); | ||
}); | ||
|
||
it('should reject invalid ethereum address (shorter)', () => { | ||
const invalidAddress = '0x123456789012345678901234567890123456789'; | ||
expect(() => EthAddressSchema.parse(invalidAddress)).toThrow(); | ||
}); | ||
|
||
it('should reject invalid ethereum address (longer)', () => { | ||
const invalidAddress = '0x12345678901234567890123456789012345678901'; | ||
expect(() => EthAddressSchema.parse(invalidAddress)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('block identifier address schema', () => { | ||
it('should accept valid block numbers (ints >= 0)', () => { | ||
BlockIdentifierSchema.parse(0); | ||
BlockIdentifierSchema.parse(1); | ||
BlockIdentifierSchema.parse(1234); | ||
BlockIdentifierSchema.parse(9007199254740991); // Max safe integer | ||
}); | ||
|
||
it('should accept valid block tags', () => { | ||
BlockIdentifierSchema.parse('earliest'); | ||
BlockIdentifierSchema.parse('finalized'); | ||
BlockIdentifierSchema.parse('safe'); | ||
BlockIdentifierSchema.parse('latest'); | ||
BlockIdentifierSchema.parse('pending'); | ||
}); | ||
|
||
it('should accept valid block hashes', () => { | ||
const validBlockHash = '0x1234567890123456789012345678901234567890123456789012345678901234'; | ||
BlockIdentifierSchema.parse(validBlockHash); | ||
}); | ||
|
||
it('should reject invalid block numbers (e.g., negative ints)', () => { | ||
expect(() => BlockIdentifierSchema.parse(-42)).toThrow(); | ||
}); | ||
|
||
it('should reject invalid block numbers (e.g., float)', () => { | ||
expect(() => BlockIdentifierSchema.parse(34.56)).toThrow(); | ||
}); | ||
|
||
it('should reject invalid block identifiers', () => { | ||
expect(() => BlockIdentifierSchema.parse('foo')).toThrow(); | ||
}); | ||
|
||
it('should reject invalid block hashes', () => { | ||
const invalidBlockHash = '0x1234'; | ||
expect(() => BlockIdentifierSchema.parse(invalidBlockHash)).toThrow(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,16 +60,55 @@ describe('validation', () => { | |
}); | ||
}); | ||
|
||
it('accepts a single UserAddress as address', () => { | ||
const result = RpcCondition.validate(rpcConditionSchema, { | ||
...testRpcConditionObj, | ||
parameters: [":userAddress"], | ||
}); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testRpcConditionObj, | ||
parameters: [":userAddress"], | ||
}); | ||
}); | ||
|
||
it('accepts a single context variable as address', () => { | ||
const result = RpcCondition.validate(rpcConditionSchema, { | ||
...testRpcConditionObj, | ||
parameters: [":testContextVar"], | ||
}); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testRpcConditionObj, | ||
parameters: [":testContextVar"], | ||
}); | ||
}); | ||
|
||
it('accepts a single address and a block number', () => { | ||
const result = RpcCondition.validate(rpcConditionSchema, { | ||
...testRpcConditionObj, | ||
parameters: [TEST_CONTRACT_ADDR, 'latest'], | ||
parameters: [TEST_CONTRACT_ADDR, 2345], | ||
}); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testRpcConditionObj, | ||
parameters: [TEST_CONTRACT_ADDR, 2345], | ||
}); | ||
}); | ||
|
||
it('accepts context params for address and block number', () => { | ||
const result = RpcCondition.validate(rpcConditionSchema, { | ||
...testRpcConditionObj, | ||
parameters: [":testAddress", ":testBlockNumber"], | ||
}); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testRpcConditionObj, | ||
parameters: [TEST_CONTRACT_ADDR, 'latest'], | ||
parameters: [":testAddress", ":testBlockNumber"], | ||
}); | ||
}); | ||
|
||
|
@@ -88,12 +127,7 @@ describe('validation', () => { | |
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
parameters: { | ||
'1': { | ||
_errors: ['Invalid', 'Invalid Ethereum address'], | ||
}, | ||
'2': { | ||
_errors: ['Invalid', 'Invalid Ethereum address'], | ||
}, | ||
_errors: ['Array must contain at most 2 element(s)'], | ||
}, | ||
}); | ||
}); | ||
|
@@ -109,7 +143,7 @@ describe('validation', () => { | |
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
parameters: { | ||
_errors: ['Array must contain at least 1 element(s)'], | ||
_errors: ['Array must contain at least 2 element(s)', 'Array must contain at least 1 element(s)'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did some digging around to see if there is a way to work around this - it's a little conflicting to have both i.e. |
||
}, | ||
}); | ||
}); | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.