encode method

Uint8List encode(
  1. dynamic registry,
  2. SignatureType signatureType
)

Implementation

Uint8List encode(dynamic registry, SignatureType signatureType) {
  if (customSignedExtensions.isNotEmpty && registry is! Registry) {
    throw Exception(
        'Custom signed extensions are not supported on this registry. Please use registry from `runtimeMetadata.chainInfo.scaleCodec.registry`.');
  }
  final ByteOutput output = ByteOutput();

  final int extrinsicVersion = registry.extrinsicVersion;
  // Unsigned transaction
  final int preByte = extrinsicVersion & 127;
  // ignore: unused_local_variable
  final String inHex = preByte.toRadixString(16);

  // Signed transaction
  final int extraByte = extrinsicVersion | 128;

  output
    ..pushByte(extraByte)
    // 00 = MultiAddress::Id
    ..pushByte(0)
    // Push Signer Address
    ..write(signer)
    // Push signature type byte
    ..pushByte(signatureType.type)
    // Push signature
    ..write(signature);

  late final SignedExtensions signedExtensions;

  final encodedMap = toEncodedMap(registry);

  if (usesChargeAssetTxPayment(registry)) {
    signedExtensions = SignedExtensions.assetHubSignedExtensions;
  } else {
    signedExtensions = SignedExtensions.substrateSignedExtensions;
  }

  late List<String> keys;
  {
    //
    //
    // Prepare keys for the encoding
    if (registry.getSignedExtensionTypes() is Map) {
      keys =
          (registry.getSignedExtensionTypes() as Map<String, Codec<dynamic>>)
              .keys
              .toList();
    } else {
      keys = (registry.getSignedExtensionTypes() as List<dynamic>)
          .cast<String>();
    }
  }

  for (final extension in keys) {
    final (payload, found) =
        signedExtensions.signedExtension(extension, encodedMap);
    if (found) {
      if (payload.isNotEmpty) {
        output.write(hex.decode(payload));
      }
    } else {
      if (registry.getSignedExtensionTypes() is List) {
        // This method call is from polkadot cli and not from the Reigstry of the polkadart_scale_codec.
        continue;
      }
      // Most probably, it is a custom signed extension.
      final signedExtensionMap = registry.getSignedExtensionTypes();

      // check if this signed extension is NullCodec or not!
      if (signedExtensionMap[extension] != null &&
          signedExtensionMap[extension] is! NullCodec &&
          signedExtensionMap[extension].hashCode !=
              NullCodec.codec.hashCode) {
        if (customSignedExtensions.containsKey(extension) == false) {
          // throw exception as this is encodable key and we need this key to be present in customSignedExtensions
          throw Exception(
              'Key `$extension` is missing in customSignedExtensions.');
        }
        signedExtensionMap[extension]
            .encodeTo(customSignedExtensions[extension], output);
      }
    }
  }

  // Add the method call -> transfer.....
  output.write(method);

  return U8SequenceCodec.codec.encode(output.toBytes());
}