Skip to content

Conversation

@Klakurka
Copy link
Member

@Klakurka Klakurka commented Aug 7, 2025

Fixed potential Chronik service hashing bug with unexpected decodeCashAddress return type, added tests.

Related to #918.

Test plan

Run yarn docker testcoverage and make sure nothing related to addresses / hashing is broken.

@Klakurka Klakurka added this to the Phase 3 milestone Aug 7, 2025
@Klakurka Klakurka requested a review from chedieck August 7, 2025 16:54
@Klakurka Klakurka self-assigned this Aug 7, 2025
@Klakurka Klakurka added the enhancement (development) Improvement to the development situation label Aug 7, 2025
@Klakurka Klakurka requested a review from Copilot August 7, 2025 17:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fixed a potential bug in the Chronik service where decodeCashAddress could return either a string or Uint8Array for the hash value, but the code was only expecting a string. Added comprehensive test coverage for ChronikService functions.

  • Fixed type handling bug in toHash160 function to handle both string and Uint8Array return types from decodeCashAddress
  • Added extensive test suite covering address conversion functions, blockchain client methods, and edge cases
  • Added tests for error conditions and input validation

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
services/chronikService.ts Fixed potential bug handling return type from decodeCashAddress in toHash160 function
tests/unittests/chronikService.test.ts Added comprehensive test coverage with mocking setup and 1000+ lines of tests

})

it('Should handle 4c push data prefix', () => {
// Test with 4c prefix for larger data pushes
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment mentions '4c prefix for larger data pushes' but doesn't explain what this prefix signifies. Consider adding more context about what the 4c prefix represents in Bitcoin script opcodes (OP_PUSHDATA1).

Suggested change
// Test with 4c prefix for larger data pushes
// Test with 4c prefix (OP_PUSHDATA1) for larger data pushes.
// In Bitcoin script, 0x4c (OP_PUSHDATA1) indicates that the next byte specifies the length of the data to be pushed onto the stack.

Copilot uses AI. Check for mistakes.

it('Should handle fromHash160 with invalid network slug', () => {
expect(() => {
fromHash160('invalid-network', 'p2pkh', 'c5d2460186f7233c927e7db2dcc703c0e500b653d2')
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash160 value has 42 characters instead of 40. This should be 'c5d2460186f7233c927e7db2dcc703c0e500b653' (40 chars) to be a valid 20-byte hash160.

Copilot uses AI. Check for mistakes.
Comment on lines +506 to +507
// Hash160 should be 40 chars (20 bytes), this is 42 chars (21 bytes)
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b653d2088ac'
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash160 in this P2PKH script has 42 characters (21 bytes) instead of the required 40 characters (20 bytes). Should be 'c5d2460186f7233c927e7db2dcc703c0e500b653' to properly test the validation logic.

Suggested change
// Hash160 should be 40 chars (20 bytes), this is 42 chars (21 bytes)
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b653d2088ac'
// Hash160 should be 40 chars (20 bytes), this is 42 chars (21 bytes). Should be 'c5d2460186f7233c927e7db2dcc703c0e500b653' to properly test the validation logic.
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b653d2d2088ac'

Copilot uses AI. Check for mistakes.

it('Should handle outputScriptToAddress with wrong hash length in P2SH', () => {
// Hash160 should be 40 chars (20 bytes), this is 38 chars (19 bytes)
const outputScript = 'a914c5d2460186f7233c927e7db2dcc703c0e500b6587'
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash160 in this P2SH script has 38 characters (19 bytes) instead of the required 40 characters (20 bytes). This doesn't properly test the intended edge case since the script structure itself is malformed.

Suggested change
const outputScript = 'a914c5d2460186f7233c927e7db2dcc703c0e500b6587'
// Use structurally valid P2SH script with wrong hash length (19 bytes)
const outputScript = 'a913c5d2460186f7233c927e7db2dcc703c0e500b687'

Copilot uses AI. Check for mistakes.

it('Should handle very short hash160 in outputScriptToAddress', () => {
// Hash160 too short (less than 40 chars) - 38 chars
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b653d88ac'
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hash160 has 40 characters which is correct, but the test comment says it should be 'too short (less than 40 chars) - 38 chars'. The actual hash160 here is the correct 40 characters, making this test not match its intended purpose.

Suggested change
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b653d88ac'
// 38-char hash160: c5d2460186f7233c927e7db2dcc703c0e500b6
const outputScript = '76a914c5d2460186f7233c927e7db2dcc703c0e500b688ac'

Copilot uses AI. Check for mistakes.
Comment on lines +1212 to +1213
// Test with exactly 39 characters (too short)
const shortHash160 = 'c5d2460186f7233c927e7db2dcc703c0e500b653'[0] // Missing one char
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line takes only the first character '[0]' of the hash, but then the next line uses a different 39-character hash. The variable name and usage are inconsistent. Should either use the single character or rename the variable.

Suggested change
// Test with exactly 39 characters (too short)
const shortHash160 = 'c5d2460186f7233c927e7db2dcc703c0e500b653'[0] // Missing one char
// Test with a 39-character hash160 (one character short)

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

@chedieck chedieck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are a good addition, but I don't get this so-called 'potential' bug, since the function decodeCashAddress returns an object with a property hash of type string. The AI is saying that it can be a Uint8Array, but that is not what the library says:
image
image

@Klakurka Klakurka requested a review from chedieck August 9, 2025 01:45
Copy link
Member Author

@Klakurka Klakurka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like unnecessary / dubious precaution.

@Klakurka Klakurka merged commit 47a2914 into master Aug 9, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement (development) Improvement to the development situation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants