register method

Future<Transaction> register(
  1. WriteFilesFlowRegisterOptions options
)

Step 2: Build the register transaction.

Returns an unsigned Transaction for external signing. Sets the _walrusBlobType: 'quilt' attribute automatically.

Implementation

Future<Transaction> register(WriteFilesFlowRegisterOptions options) async {
  if (_step != _WriteFilesFlowStep.encoded) {
    throw StateError('Must call encode() before register()');
  }

  requireDeletableUpload(options.deletable);
  if (_relayClient == null &&
      (_encodedBlob == null || _directClient == null)) {
    throw StateError(
      "Direct writes require fully encoded slivers before registration",
    );
  }
  if (_relayClient == null) {
    _directClient!.validateDirectWriteReadiness(committee: _committee);
  }
  _committeeSize = _committee?.nodes.length;
  if (_stateReader != null) {
    final state = await _stateReader.systemState();
    _committeeSize = state.committeeSize;
    if (_shardCount != null && _shardCount != state.nShards) {
      throw StateError(
        'Shard count changed; create and encode a fresh flow before registering',
      );
    }
  }
  final reader =
      _stateReader ??
      (_suiClient == null
          ? null
          : SystemStateReader(
              suiClient: _suiClient,
              config: _txBuilder.packageConfig,
            ));
  if (reader == null) {
    throw StateError('A chain client is required to verify the flow');
  }
  _blobType = '${await reader.getPackageId()}::blob::Blob';
  _owner = options.owner;
  _deletable = options.deletable;
  final metadata = _metadata!;
  final quiltBytes = _quiltBytes!;

  final tx = Transaction();
  tx.setSenderIfNotSet(options.owner);

  // Add upload relay tip if configured.
  if (_relayClient != null && _tipConfig != null) {
    _txBuilder.sendUploadRelayTip(
      size: quiltBytes.length,
      encodedSize: encodedBlobLength(quiltBytes.length, _shardCount!),
      blobDigest: metadata.blobDigest,
      nonce: metadata.nonce,
      tipConfig: _tipConfig,
      transaction: tx,
    );
  }

  // Register the blob on-chain.
  String? walCoinObjectId = options.walCoinObjectId;
  String? walType = options.walType;
  BigInt? storageCost = options.storageCost;
  BigInt? writeCost = options.writeCost;
  int? encodedSize = options.encodedSize;

  // Auto-resolve from chain if state reader is available.
  if (_stateReader != null &&
      (walType == null ||
          storageCost == null ||
          writeCost == null ||
          encodedSize == null)) {
    walType ??= await _stateReader.getWalType();
    final costs = await _stateReader.storageCost(
      quiltBytes.length,
      options.epochs,
    );
    storageCost ??= costs.storageCost;
    writeCost ??= costs.writeCost;
    final state = await _stateReader.systemState();
    encodedSize ??= encodedBlobLength(quiltBytes.length, state.nShards);
  }

  // Auto-resolve WAL coin when not provided.
  if (walCoinObjectId == null &&
      _suiClient != null &&
      walType != null &&
      storageCost != null &&
      writeCost != null) {
    final totalWalNeeded = storageCost + writeCost;
    await for (final coin in walCoins(_suiClient, options.owner, walType)) {
      final balance = BigInt.tryParse(coin.balance) ?? BigInt.zero;
      if (balance >= totalWalNeeded) {
        walCoinObjectId = coin.coinObjectId;
        break;
      }
    }
  }

  if (walCoinObjectId != null &&
      walType != null &&
      storageCost != null &&
      writeCost != null &&
      encodedSize != null) {
    _txBuilder.registerBlobWithWal(
      RegisterBlobOptions(
        size: quiltBytes.length,
        epochs: options.epochs,
        blobId: metadata.blobId,
        rootHash: metadata.rootHash,
        deletable: options.deletable,
        owner: options.owner,
      ),
      walCoinObjectId: walCoinObjectId,
      walType: walType,
      storageCost: storageCost,
      writeCost: writeCost,
      encodedSize: encodedSize,
      transaction: tx,
    );
  } else {
    throw StateError(
      'WAL payment requires a WAL coin with sufficient balance. '
      'Either provide walCoinObjectId explicitly, or ensure a SuiClient '
      'and SystemStateReader are available for auto-resolution. '
      'Resolved: walCoinObjectId=$walCoinObjectId, walType=$walType, '
      'storageCost=$storageCost, writeCost=$writeCost, encodedSize=$encodedSize',
    );
  }

  _step = _WriteFilesFlowStep.registered;
  return tx;
}