Skip to content

fix(btcindexer): validate Sui recipient address in OP_RETURN#350

Merged
sczembor merged 3 commits intomasterfrom
stan/sui-addr-validation
Feb 9, 2026
Merged

fix(btcindexer): validate Sui recipient address in OP_RETURN#350
sczembor merged 3 commits intomasterfrom
stan/sui-addr-validation

Conversation

@sczembor
Copy link
Contributor

@sczembor sczembor commented Feb 9, 2026

Description

Closes: #340


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included doc comments for public functions
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary

Summary by Sourcery

Validate Sui recipient addresses extracted from Bitcoin OP_RETURN outputs and expand test coverage for parseSuiRecipientFromOpReturn.

Bug Fixes:

  • Reject OP_RETURN payloads where the encoded Sui address is not exactly 32 bytes or fails Sui address validation.

Enhancements:

  • Update BTC indexer test fixture data to reflect the new, strictly validated Sui address format in OP_RETURN transactions.

Tests:

  • Add unit tests covering empty/invalid OP_RETURN scripts, incorrect flags and lengths, and valid 32-byte Sui address extraction from OP_RETURN data.

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor requested a review from a team as a code owner February 9, 2026 08:53
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 9, 2026

Reviewer's Guide

Adds validation logic for Sui recipient addresses parsed from OP_RETURN outputs and expands unit test coverage to verify correct behavior for valid and invalid scripts, including fixture updates to match the new validation rules.

Class diagram for btcindexer Sui OP_RETURN parsing utilities

classDiagram
  class BtcIndexerModule {
    +parseSuiRecipientFromOpReturn(script Buffer) string_or_null
    +isValidSuiAddress(address string) bool
  }

  class parseSuiRecipientFromOpReturn {
    +script Buffer
    +return string_or_null
  }

  class isValidSuiAddress {
    +address string
    +return bool
  }

  BtcIndexerModule --> parseSuiRecipientFromOpReturn
  BtcIndexerModule --> isValidSuiAddress
  parseSuiRecipientFromOpReturn ..> isValidSuiAddress
Loading

File-Level Changes

Change Details Files
Tighten parseSuiRecipientFromOpReturn to validate Sui addresses before returning them.
  • Keep OP_RETURN and length-prefix checks, then interpret the first payload byte as a flag and only handle flag 0x00 for simple transfers
  • Require the remaining payload bytes to be exactly 32 bytes; return null if shorter or longer
  • Format the 32-byte payload as a 0x-prefixed hex string, then validate it using isValidSuiAddress; return null if invalid
  • Return the validated address string for the happy path
packages/btcindexer/src/btcindexer.ts
Update and extend btcindexer tests to cover Sui address validation behavior and adjust fixtures accordingly.
  • Adjust the REGTEST_DATA block 327 test fixture (hash, rawBlockHex, and transaction id/suiAddr) to align with the updated parsing/validation behavior
  • Add tests for parseSuiRecipientFromOpReturn covering empty script, non-OP_RETURN scripts, too-short scripts, unsupported payload flags, incorrect address lengths, valid 32-byte addresses, and addresses that are too long
  • Ensure the expected Sui address strings in tests match the new validation constraints and formatted output
packages/btcindexer/src/btcindexer.test.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#340 Validate that the Bitcoin OP_RETURN payload represents a valid 32-byte Sui address before returning it in parseSuiRecipientFromOpReturn in packages/btcindexer/src/btcindexer.ts.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Signed-off-by: sczembor <43810037+sczembor@users.noreply.github.com>
@sczembor
Copy link
Contributor Author

sczembor commented Feb 9, 2026

@sourcery-ai title

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `packages/btcindexer/src/btcindexer.ts:1312-1317` </location>
<code_context>
 	// Check simple transfer format: 1-byte flag (0x00)
-	// TODO: add validation for the sui address
 	if (payload[0] === 0x00) {
 		const addressBytes = payload.subarray(1);
-		return `0x${addressBytes.toString("hex")}`;
+		if (addressBytes.length !== 32) {
+			return null;
+		}
+		const address = `0x${addressBytes.toString("hex")}`;
+		if (!isValidSuiAddress(address)) {
+			return null;
+		}
</code_context>

<issue_to_address>
**suggestion:** Avoid hardcoding the 32-byte address length; use a named constant for maintainability and consistency.

This `32` is a protocol-level constant and already assumed elsewhere (e.g., `Buffer.alloc(32, …)` in tests). Please introduce a named constant like `const SUI_ADDRESS_NUM_BYTES = 32;` and reuse it here and anywhere else this assumption appears to keep things consistent and easier to update if the address size changes.

Suggested implementation:

```typescript
import { logError, logger } from "@gonative-cc/lib/logger";
import { isValidSuiAddress } from "@mysten/sui/utils";
import { OP_RETURN } from "./opcodes";
import { BitcoinMerkleTree } from "./bitcoin-merkle-tree";
import { SuiClient, type SuiClientI } from "./sui_client";

const SUI_ADDRESS_NUM_BYTES = 32;

	const payload = script.subarray(2);

	// Check simple transfer format: 1-byte flag (0x00)
	if (payload[0] === 0x00) {
		const addressBytes = payload.subarray(1);
		if (addressBytes.length !== SUI_ADDRESS_NUM_BYTES) {
			return null;
		}

```

```typescript
		const address = `0x${addressBytes.toString("hex")}`;
		if (!isValidSuiAddress(address)) {
			return null;
		}
		return address;
	}
	//TODO: in the future we need to update the relayer to correctly handle the flag 0x01
	// for now we cannot determine the recipient

```

You should also:
1. Search this file (and possibly related modules) for other occurrences where the Sui address length is assumed as `32` (e.g., `Buffer.alloc(32, ...)`, `new Uint8Array(32)`, or length checks) and replace those with `SUI_ADDRESS_NUM_BYTES` for consistency.
2. In tests that construct or assert on 32-byte Sui addresses (e.g., `Buffer.alloc(32, ...)`), introduce and reuse the same constant (or import it from a shared module) to keep test expectations aligned with the production constant.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@sourcery-ai sourcery-ai bot changed the title add sui address validation + unit tests fix(btcindexer): validate Sui recipient address in OP_RETURN Feb 9, 2026
Copy link
Contributor

@vuvoth vuvoth left a comment

Choose a reason for hiding this comment

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

pre-approve!

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

This PR addresses #340 by validating Sui recipient addresses extracted from Bitcoin OP_RETURN outputs in the BTC indexer, preventing malformed/invalid Sui addresses from being accepted during deposit parsing.

Changes:

  • Add Sui address validation (isValidSuiAddress) when parsing recipient data from OP_RETURN.
  • Update regtest fixture block/tx data to use the strictly validated Sui address encoding.
  • Add unit tests for parseSuiRecipientFromOpReturn covering invalid scripts/flags/lengths and valid extraction.

Reviewed changes

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

File Description
packages/btcindexer/src/btcindexer.ts Validates parsed Sui recipient with isValidSuiAddress before accepting OP_RETURN payloads.
packages/btcindexer/src/btcindexer.test.ts Updates regtest fixture data and adds unit tests for parseSuiRecipientFromOpReturn.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: sczembor <43810037+sczembor@users.noreply.github.com>
@sczembor sczembor merged commit 2b4ba91 into master Feb 9, 2026
12 checks passed
@sczembor sczembor deleted the stan/sui-addr-validation branch February 9, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

worker: Sui Address Validation

4 participants