-
Notifications
You must be signed in to change notification settings - Fork 29
fixes issues with v
#613
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
fixes issues with v
#613
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
88b1fa3
fixes issues with `v`
netbonus 04528c9
fix lint
netbonus 985dd1b
fix: improve v parameter handling and transaction validation
netbonus cbf84bb
fix lock
netbonus b018c9e
address feedback
netbonus dc606c2
fix bigint
netbonus 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,172 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { Buffer } from 'buffer'; | ||
| import { parseGenericSigningResponse } from '../../genericSigning'; | ||
| import { Constants } from '../../index'; | ||
| import secp256k1 from 'secp256k1'; | ||
| import { Hash } from 'ox'; | ||
| import { RLP } from '@ethereumjs/rlp'; | ||
|
|
||
| describe('parseGenericSigningResponse', () => { | ||
| // Helper to create a DER signature | ||
| const createDERSignature = (r: Buffer, s: Buffer): Buffer => { | ||
| const rLen = r.length; | ||
| const sLen = s.length; | ||
| const totalLen = 4 + rLen + sLen; | ||
| const sig = Buffer.alloc(totalLen + 2); | ||
|
|
||
| sig[0] = 0x30; // DER sequence | ||
| sig[1] = totalLen; | ||
| sig[2] = 0x02; // Integer type | ||
| sig[3] = rLen; | ||
| r.copy(sig, 4); | ||
| sig[4 + rLen] = 0x02; // Integer type | ||
| sig[4 + rLen + 1] = sLen; | ||
| s.copy(sig, 4 + rLen + 2); | ||
|
|
||
| // Pad to 74 bytes (standard for Lattice) | ||
| const padded = Buffer.alloc(74); | ||
| sig.copy(padded, 0); | ||
| return padded; | ||
| }; | ||
|
|
||
| it('should handle generic KECCAK256 message (not EVM transaction)', () => { | ||
| // Simulate signing a plain text message "Test!" | ||
| const payload = Buffer.from('Test!'); | ||
| const hash = Buffer.from(Hash.keccak256(payload)); | ||
|
|
||
| // Create a fake signature | ||
| const privateKey = Buffer.from( | ||
| '0101010101010101010101010101010101010101010101010101010101010101', | ||
| 'hex', | ||
| ); | ||
| const sigObj = secp256k1.ecdsaSign(hash, privateKey); | ||
| const publicKey = secp256k1.publicKeyCreate(privateKey, false); | ||
|
|
||
| // Create DER-encoded signature response | ||
| const derSig = createDERSignature( | ||
| Buffer.from(sigObj.signature.slice(0, 32)), | ||
| Buffer.from(sigObj.signature.slice(32, 64)), | ||
| ); | ||
|
|
||
| // Create mock response buffer | ||
| const mockResponse = Buffer.concat([ | ||
| Buffer.from([0x04]), // Uncompressed pubkey prefix | ||
| publicKey.slice(1), // Remove compression prefix from secp256k1 output (64 bytes) | ||
| derSig, | ||
| ]); | ||
|
|
||
| const req = { | ||
| curveType: Constants.SIGNING.CURVES.SECP256K1, | ||
| hashType: Constants.SIGNING.HASHES.KECCAK256, | ||
| encodingType: null, // Not EVM encoding | ||
| origPayloadBuf: payload, | ||
| }; | ||
|
|
||
| const result = parseGenericSigningResponse(mockResponse, 0, req); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.sig).toBeDefined(); | ||
| expect(result.sig.v).toBeDefined(); | ||
| expect(typeof result.sig.v).toBe('bigint'); | ||
|
|
||
| // For non-EVM generic messages, v should be 27 or 28 | ||
| expect([27n, 28n]).toContain(result.sig.v); | ||
| }); | ||
|
|
||
| it('should handle EVM transaction encoding', () => { | ||
| // Simulate an unsigned legacy transaction | ||
| const unsignedTx = Buffer.from( | ||
| 'e9808504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080', | ||
| 'hex', | ||
| ); | ||
| const hash = Buffer.from(Hash.keccak256(unsignedTx)); | ||
|
|
||
| // Create a fake signature | ||
| const privateKey = Buffer.from( | ||
| '0101010101010101010101010101010101010101010101010101010101010101', | ||
| 'hex', | ||
| ); | ||
| const sigObj = secp256k1.ecdsaSign(hash, privateKey); | ||
| const publicKey = secp256k1.publicKeyCreate(privateKey, false); | ||
|
|
||
| // Create DER-encoded signature response | ||
| const derSig = createDERSignature( | ||
| Buffer.from(sigObj.signature.slice(0, 32)), | ||
| Buffer.from(sigObj.signature.slice(32, 64)), | ||
| ); | ||
|
|
||
| // Create mock response buffer | ||
| const mockResponse = Buffer.concat([ | ||
| Buffer.from([0x04]), // Uncompressed pubkey prefix | ||
| publicKey.slice(1), // Remove compression prefix from secp256k1 output (64 bytes) | ||
| derSig, | ||
| ]); | ||
|
|
||
| const req = { | ||
| curveType: Constants.SIGNING.CURVES.SECP256K1, | ||
| hashType: Constants.SIGNING.HASHES.KECCAK256, | ||
| encodingType: Constants.SIGNING.ENCODINGS.EVM, | ||
| origPayloadBuf: unsignedTx, | ||
| }; | ||
|
|
||
| const result = parseGenericSigningResponse(mockResponse, 0, req); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.sig).toBeDefined(); | ||
| expect(result.sig.v).toBeDefined(); | ||
| expect(typeof result.sig.v).toBe('bigint'); | ||
|
|
||
| // For pre-EIP155 transactions, v should be 27 or 28 | ||
| const vNumber = Number(result.sig.v); | ||
| expect([27, 28]).toContain(vNumber); | ||
| }); | ||
|
|
||
| it('should handle RLP-encoded data that looks like a transaction', () => { | ||
| // Create an RLP-encoded array with 6+ elements (looks like a transaction) | ||
| const txLikeData = [ | ||
| Buffer.from([0x01]), // nonce | ||
| Buffer.from([0x02]), // gasPrice | ||
| Buffer.from([0x03]), // gasLimit | ||
| Buffer.from([0x04]), // to | ||
| Buffer.from([0x05]), // value | ||
| Buffer.from([0x06]), // data | ||
| ]; | ||
| const rlpEncoded = Buffer.from(RLP.encode(txLikeData)); | ||
| const hash = Buffer.from(Hash.keccak256(rlpEncoded)); | ||
|
|
||
| // Create a fake signature | ||
| const privateKey = Buffer.from( | ||
| '0101010101010101010101010101010101010101010101010101010101010101', | ||
| 'hex', | ||
| ); | ||
| const sigObj = secp256k1.ecdsaSign(hash, privateKey); | ||
| const publicKey = secp256k1.publicKeyCreate(privateKey, false); | ||
|
|
||
| // Create DER-encoded signature response | ||
| const derSig = createDERSignature( | ||
| Buffer.from(sigObj.signature.slice(0, 32)), | ||
| Buffer.from(sigObj.signature.slice(32, 64)), | ||
| ); | ||
|
|
||
| // Create mock response buffer | ||
| const mockResponse = Buffer.concat([ | ||
| Buffer.from([0x04]), // Uncompressed pubkey prefix | ||
| publicKey.slice(1), // Remove compression prefix from secp256k1 output (64 bytes) | ||
| derSig, | ||
| ]); | ||
|
|
||
| const req = { | ||
| curveType: Constants.SIGNING.CURVES.SECP256K1, | ||
| hashType: Constants.SIGNING.HASHES.KECCAK256, | ||
| encodingType: null, // Not explicitly EVM | ||
| origPayloadBuf: rlpEncoded, | ||
| }; | ||
|
|
||
| const result = parseGenericSigningResponse(mockResponse, 0, req); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result.sig).toBeDefined(); | ||
| expect(result.sig.v).toBeDefined(); | ||
| expect(typeof result.sig.v).toBe('bigint'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.