Skip to content

bug: wallet.listUnspent() can throw RangeError(byteOffset) due to ChainPosition offset drift #49

@reez

Description

@reez

Problem

Calling wallet.listUnspent() can fail during Dart FFI decode with:

RangeError (byteOffset): Index out of range: index should be less than 1: 7

Stack trace

  • Script.read (...)
  • FfiConverterTxOut.read (...)
  • FfiConverterLocalOutput.read (...)
  • FfiConverterSequenceLocalOutput.read (...)
  • Wallet.listUnspent (...)

Error Logs:

 Future<List<dynamic>> getUtxos() async {
    print('[DEBUG] Starting getUtxos()');
    List<dynamic> finalUtxos = [];

    print('[DEBUG] Fetching wallet UTXOs...');
    final walletUtxos = wallet.listUnspent();
    print('[DEBUG] Found ${walletUtxos.length} UTXOs in wallet');
    
    ...
}

I/flutter ( 6052): [DEBUG] Starting getUtxos()
I/flutter ( 6052): [DEBUG] Fetching wallet UTXOs...
I/flutter ( 6052): Error during _syncWallet: RangeError (byteOffset): Index out of range: index should be less than 1: 7
I/flutter ( 6052): #0      _byteDataByteOffsetCheck (dart:typed_data-patch/typed_data_patch.dart:5419:5)
I/flutter ( 6052): #1      _ByteDataView.getInt64 (dart:typed_data-patch/typed_data_patch.dart:5214:5)
I/flutter ( 6052): #2      Script.read (package:bdk_dart/bdk.dart:20644:61)
I/flutter ( 6052): #3      FfiConverterTxOut.read (package:bdk_dart/bdk.dart:3221:40)
I/flutter ( 6052): #4      FfiConverterLocalOutput.read (package:bdk_dart/bdk.dart:1711:44)
I/flutter ( 6052): #5      FfiConverterSequenceLocalOutput.read (package:bdk_dart/bdk.dart:25741:43)
I/flutter ( 6052): #6      FfiConverterSequenceLocalOutput.lift (package:bdk_dart/bdk.dart:25733:44)
I/flutter ( 6052): #7      rustCallWithLifter (package:bdk_dart/bdk.dart:22780:18)
I/flutter ( 6052): #8      Wallet.listUnspent (package:bdk_dart/bdk.dart:22316:12)
I/flutter ( 6052): #9      WalletService.getUtxos (package:flutter_wallet/services/wallet_service.dart:897:32)
I/flutter ( 6052): #10     SharedWalletState._syncWallet (package:flutter_wallet/wallet_pages/shared_wallet.dart:556:47)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #11     WalletUiHelpers.handleRefresh (package:flutter_wallet/wallet_helpers/wallet_ui_helpers.dart:1166:7)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #12     SharedWalletState.build.<anonymous closure> (package:flutter_wallet/wallet_pages/shared_wallet.dart:777:17)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #13     RefreshIndicatorState._show.<anonymous closure>.<anonymous closure> (package:flutter/src/material/refresh_indicator.dart:579:40)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): 
I/flutter ( 6052): Sync error: Exception: Sync Error: RangeError (byteOffset): Index out of range: index should be less than 1: 7
I/flutter ( 6052): Stack trace: #0      SharedWalletState._syncWallet (package:flutter_wallet/wallet_pages/shared_wallet.dart:605:7)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #1      WalletUiHelpers.handleRefresh (package:flutter_wallet/wallet_helpers/wallet_ui_helpers.dart:1166:7)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #2      SharedWalletState.build.<anonymous closure> (package:flutter_wallet/wallet_pages/shared_wallet.dart:777:17)
I/flutter ( 6052): <asynchronous suspension>
I/flutter ( 6052): #3      RefreshIndicatorState._show.<anonymous closure>.<anonymous closure> (package:flutter/src/material/refresh_indicator.dart:579:40)
I/flutter ( 6052): <asynchronous suspension>

Description

This looks like the same root cause class as #46 (non-flat enum offset accounting), but in a different call path.

listUnspent() decodes List<LocalOutput>, and LocalOutput includes chainPosition: ChainPosition. If ChainPosition reports absolute byte offsets instead of relative bytes consumed, sequence offset accounting drifts. A later LocalOutput decode can then hand a truncated buffer to Script.read, which calls getInt64(0) (needs 8 bytes), producing the byteOffset ... : 7 error when only ~1 byte remains.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions