Skip to content

Commit

Permalink
Merge pull request #470 from 0xPolygonID/feature/crosschain_resolver
Browse files Browse the repository at this point in the history
Added crosschain proof data retrieval function.
  • Loading branch information
5eeman authored Jan 20, 2025
2 parents 996bb0f + 2cbd6af commit 7108bdf
Show file tree
Hide file tree
Showing 21 changed files with 1,091 additions and 91 deletions.
3 changes: 2 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ analyzer:

linter:
rules:
prefer_double_quotes: false
prefer_double_quotes: false
unnecessary_this: false
92 changes: 92 additions & 0 deletions lib/common/abi_encode/abi_encode.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:flutter/foundation.dart';

import 'package:web3dart/crypto.dart';
import 'package:web3dart/web3dart.dart';

/// An object that can be encoded to ABI.
abstract interface class AbiEncodable {
/// Serialize the object to a list of arguments that can be passed to the ABI encoder.
/// Example class represented by the object:
/// {
/// a: "c",
/// b: "d",
/// }
/// should return
/// ["c", 896314698613489613]
List toAbiEncodeArgs();

/// The ABI type of the object.
/// Should be retrieved from actual contract ABI.
/// Example:
/// {
/// a: "c",
/// b: 896314698613489613,
/// }
/// should return
/// [StringType(), UintType()]
AbiType get abiType;
}

extension EncodableExtension on AbiEncodable {
/// Encode the object to a byte array.
Uint8List encode() {
final buffer = LengthTrackingByteSink();

abiType.encode(
toAbiEncodeArgs(),
buffer,
);

return buffer.asBytes();
}

/// Encode the object to a hex string.
String encodeToHex() {
return bytesToHex(encode());
}
}

/// An extension on [Iterable] of [AbiEncodable] objects.
extension EncodableIterable<T extends AbiEncodable> on Iterable<T> {
/// Encode the list of objects to a byte array.
AbiType getAbiType(AbiType elementType) {
return TupleType([
DynamicLengthArray(
type: elementType,
),
]);
}

Uint8List encode() {
if (isEmpty) {
// TODO Check that this is correct
return Uint8List(0);
}

final type = getAbiType(first.abiType);

final buffer = LengthTrackingByteSink();

type.encode(
[
map((m) => m.toAbiEncodeArgs()).toList(),
],
buffer,
);

return buffer.asBytes();
}

String encodeToHex() => bytesToHex(encode());
}

extension HexEncodable on AbiType {
String encodeToHex(List<dynamic> params) {
final buffer = LengthTrackingByteSink();
encode(params, buffer);

final result = bytesToHex(buffer.asBytes());

return result;
}
}
Loading

0 comments on commit 7108bdf

Please sign in to comment.