Skip to content

Commit

Permalink
Implement a RawEVMTransaction (#6)
Browse files Browse the repository at this point in the history
* implement a raw evm transaction

* add check for the length of the decoded Uint8list

* fix dart analyze

---------

Co-authored-by: dev2-nomo <dev2@nomo.app>
  • Loading branch information
nomo-app and dev2-nomo authored Nov 22, 2023
1 parent 13e7266 commit 0a6af0b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
34 changes: 23 additions & 11 deletions lib/src/crypto/evm/rlp.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:walletkit_dart/src/crypto/evm/transaction/raw_evm_transaction.dart';
import 'package:walletkit_dart/src/utils/bigint_utils.dart';
import 'package:web3dart/web3dart.dart';

class DecodedRLP {
final int consumed;
Expand Down Expand Up @@ -107,13 +106,13 @@ DecodedRLP decodeRLP(Uint8List data, int offset) {
/**
* @param {String} messageHex
*
* This function takes a message hex string and returns a Transaction object.
* This function takes a message hex string and returns a RawEVMTransaction object.
*
* @returns {Transaction}
* @returns {RawEVMTransaction}
*
* @throws {Exception} If the message hash is invalid or the result length is less than 5
*/
Transaction getTransactionFromMessageHash(String messageHex) {
RawEVMTransaction getTransactionFromMessageHash(String messageHex) {
final message = Uint8List.fromList(
hex.decode(
messageHex.replaceFirst("0x", ""),
Expand All @@ -138,12 +137,25 @@ Transaction getTransactionFromMessageHash(String messageHex) {
BigInt value = parseAsHexBigInt(en.result[4] == "" ? "0x0" : en.result[4]);
final Uint8List data = Uint8List.fromList(hex.decode(en.result[5]));

return Transaction(
nonce: nonce.toInt(),
gasPrice: EtherAmount.inWei(gasPrice),
maxGas: gasLimit.toInt(),
to: EthereumAddress.fromHex(evmAddress),
value: EtherAmount.inWei(value),
if (en.result.length >= 7) {
BigInt chainId = parseAsHexBigInt(en.result[6]);
return RawEVMTransaction(
nonce: nonce,
gasPrice: gasPrice,
gasLimit: gasLimit,
to: evmAddress,
value: value,
data: data,
chainId: chainId,
);
}

return RawEVMTransaction(
nonce: nonce,
gasPrice: gasPrice,
gasLimit: gasLimit,
to: evmAddress,
value: value,
data: data,
);
}
21 changes: 21 additions & 0 deletions lib/src/crypto/evm/transaction/raw_evm_transaction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:typed_data';

class RawEVMTransaction {
final BigInt nonce;
final BigInt gasPrice;
final BigInt gasLimit;
final String to;
final BigInt value;
final Uint8List? data;
final BigInt? chainId;

RawEVMTransaction({
required this.nonce,
required this.gasPrice,
required this.gasLimit,
required String to,
required this.value,
required this.data,
this.chainId,
}) : to = to.startsWith("0x") ? to : "0x${to}";
}
1 change: 0 additions & 1 deletion lib/src/domain/entities/transactions/evm_transaction.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:typed_data';

import 'package:hive/hive.dart';
import 'package:walletkit_dart/src/crypto/evm/function_signature.dart';
import 'package:walletkit_dart/walletkit_dart.dart';

abstract base class EVMTransaction extends GenericTransaction {
Expand Down
4 changes: 0 additions & 4 deletions lib/src/domain/entities/transactions/raw_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,6 @@ class RawTransaction {

final hash = sha256Sha256Hash(buffer);

final hexHash = hex.encode(hash);

print("Hash: $hexHash");

return hash;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/walletkit_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ export 'src/domain/entities/address_type.dart';
export 'src/crypto/evm/rlp.dart';

export 'src/crypto/evm/function_signature.dart';

export 'src/crypto/evm/transaction/raw_evm_transaction.dart';
16 changes: 9 additions & 7 deletions test/ci/parsing/parse_hex_transaction_test.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import 'package:test/test.dart';
import 'package:walletkit_dart/src/crypto/evm/rlp.dart';
import 'package:web3dart/web3dart.dart';
import 'package:walletkit_dart/src/crypto/evm/transaction/raw_evm_transaction.dart';

void main() {
/// Send ZeniqSmart to own Wallet
test('parse hex transaction', () {
const String unsignedTxFromNomo = // from nomo.signEvmTransaction
"0xf38207488502540be4008252089405870f1507d820212e921e1f39f14660336231d188016345785d8a0000808559454e49518080";

final Transaction tx = getTransactionFromMessageHash(unsignedTxFromNomo);
final RawEVMTransaction tx =
getTransactionFromMessageHash(unsignedTxFromNomo);

expect(tx.nonce, 1864);
expect(tx.gasPrice!.getInWei, BigInt.from(10000000000));
expect(tx.maxGas, 21000);
expect(tx.to!.hex, "0x05870f1507d820212e921e1f39f14660336231d1");
expect(tx.value!.getInWei, BigInt.from(100000000000000000));
expect(tx.nonce, BigInt.from(1864));
expect(tx.gasPrice, BigInt.from(10000000000));
expect(tx.gasLimit, BigInt.from(21000));
expect(tx.to, "0x05870f1507d820212e921e1f39f14660336231d1");
expect(tx.value, BigInt.from(100000000000000000));
expect(tx.data, []);
expect(tx.chainId, BigInt.from(383414847825));
});
}
2 changes: 0 additions & 2 deletions test/ci/parsing/reverse-hash-computation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ void main() {
final FunctionSignature functionSignature =
FunctionSignature.fromData(data);

print(functionSignature.parameters);

expect(functionSignature.name, "mint");
expect(functionSignature.parameters, {
"to": "address",
Expand Down

0 comments on commit 0a6af0b

Please sign in to comment.