Skip to content

Commit

Permalink
It works!
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed May 21, 2024
1 parent 8dca1b4 commit 024da5b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
12 changes: 4 additions & 8 deletions Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ public static async Task<string> Send(ThirdwebTransaction transaction, string zk
transaction.Input.Gas ??= new HexBigInteger(await EstimateGasLimit(transaction));
if (transaction.Input.GasPrice == null)
{
Console.WriteLine("Sending as EIP-1559 tx");
var (maxFeePerGas, maxPriorityFeePerGas) = await EstimateGasFees(transaction);
transaction.Input.MaxFeePerGas ??= maxFeePerGas.ToHexBigInteger();
transaction.Input.MaxPriorityFeePerGas ??= maxPriorityFeePerGas.ToHexBigInteger();
}
else
{
Console.WriteLine("Sending as legacy tx");
transaction.Input.MaxFeePerGas = null;
transaction.Input.MaxPriorityFeePerGas = null;
}
Expand All @@ -226,22 +224,21 @@ public static async Task<string> Send(ThirdwebTransaction transaction, string zk
var zkTx = new AccountAbstraction.ZkSyncAATransaction
{
TxType = 0x71, // 712 can't be used as it has to be one byte long
From = transaction.Input.From,
To = transaction.Input.To,
From = new HexBigInteger(transaction.Input.From).Value,
To = new HexBigInteger(transaction.Input.To).Value,
GasLimit = transaction.Input.Gas.Value,
GasPerPubdataByteLimit = 50000,
MaxFeePerGas = transaction.Input.MaxFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
MaxPriorityFeePerGas = transaction.Input.MaxPriorityFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
Paymaster = zkSyncPaymaster,
// unsigned bigint of paymaster hex string
Paymaster = new HexBigInteger(zkSyncPaymaster).Value,
Nonce = transaction.Input.Nonce ?? new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", transaction.Input.From, "latest")),
Value = transaction.Input.Value.Value,
Data = transaction.Input.Data.HexToByteArray(),
FactoryDeps = new byte[] { },
PaymasterInput = zkSyncPaymasterInput.HexToByteArray()
};
Console.WriteLine($"ZkSync transaction: {JsonConvert.SerializeObject(zkTx)}");
var zkTxSigned = await EIP712.GenerateSignature_ZkSyncTransaction("zkSync", "2", transaction.Input.ChainId.Value, zkTx, transaction._wallet);
Console.WriteLine($"ZkSync transaction signed: {zkTxSigned}");
hash = await rpc.SendRequestAsync<string>("eth_sendRawTransaction", zkTxSigned);
}
else
Expand All @@ -262,7 +259,6 @@ public static async Task<string> Send(ThirdwebTransaction transaction, string zk
throw new NotImplementedException("Account type not supported");
}
}
Console.WriteLine($"Transaction hash: {hash}");
return hash;
}

Expand Down
30 changes: 7 additions & 23 deletions Thirdweb/Thirdweb.Wallets/EIP712.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,9 @@ IThirdwebWallet signer
)
{
var typedData = GetTypedDefinition_ZkSyncTransaction(domainName, version, chainId);
var typedDataEncoder = new Eip712TypedDataEncoder();
var hash = typedDataEncoder.EncodeAndHashTypedData(transaction, typedData);
Console.WriteLine($"Hash: {hash.ToHex(true)}");
var signatureHex = await signer.EthSign(hash);
Console.WriteLine($"Signature: {signatureHex}");
var signatureHex = await signer.SignTypedDataV4(transaction, typedData);
var signatureRaw = EthECDSASignatureFactory.ExtractECDSASignature(signatureHex);
var serializedTx = SerializeEip712(transaction, signatureRaw, chainId);
Console.WriteLine($"Serialized: {serializedTx}");
return serializedTx;
return SerializeEip712(transaction, signatureRaw, chainId);
}

public static TypedData<Domain> GetTypedDefinition_SmartAccount(string domainName, string version, BigInteger chainId, string verifyingContract)
Expand Down Expand Up @@ -116,18 +110,13 @@ private static string SerializeEip712(AccountAbstraction.ZkSyncAATransaction tra
throw new ArgumentException("Chain ID must be provided for EIP712 transactions!");
}

if (string.IsNullOrEmpty(transaction.From))
{
throw new ArgumentException("From address must be provided for EIP712 transactions!");
}

var fields = new List<byte[]>
{
transaction.Nonce.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.MaxPriorityFeePerGas.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.MaxFeePerGas.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.GasLimit.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.To.HexToByteArray(),
transaction.To.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.Value == 0 ? new byte[0] : transaction.Value.ToByteArray(isUnsigned: true, isBigEndian: true),
transaction.Data == null ? new byte[0] : transaction.Data,
};
Expand All @@ -137,21 +126,16 @@ private static string SerializeEip712(AccountAbstraction.ZkSyncAATransaction tra
fields.Add(signature.S);

fields.Add(chainId.ToByteArray(isUnsigned: true, isBigEndian: true));
fields.Add(transaction.From.HexToByteArray());
fields.Add(transaction.From.ToByteArray(isUnsigned: true, isBigEndian: true));

// Add meta
fields.Add(transaction.GasPerPubdataByteLimit.ToByteArray(isUnsigned: true, isBigEndian: true));
fields.Add(new byte[] { }); // TODO: FactoryDeps
fields.Add(signature.CreateStringSignature().HexToByteArray());
// add array of rlp encoded paymaster/paymasterinput
fields.Add(RLP.EncodeElement(transaction.Paymaster.ToByteArray(isUnsigned: true, isBigEndian: true)).Concat(RLP.EncodeElement(transaction.PaymasterInput)).ToArray());

fields.Add(transaction.Paymaster.HexToByteArray());
fields.Add(transaction.PaymasterInput);

// 0x71f901240c84017d784084017d78408401312d009483e13cd6b1179be8b8cb5858accbba84394cf9a7808080a0b200bd413af7e1440b3b5ef7ff46360936d702438a805960373d07a2c6159014a00a667fddcbda9b4620fd2a39940df01f5634fe7eade921160eb2fae9ec984f8e82012c9483e13cd6b1179be8b8cb5858accbba84394cf9a782c350c0b841b200bd413af7e1440b3b5ef7ff46360936d702438a805960373d07a2c61590140a667fddcbda9b4620fd2a39940df01f5634fe7eade921160eb2fae9ec984f8e1b94ba226d47cbb2731cbaa67c916c57d68484aa269fb8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000

// 0x71f901260c84017d784084017d78408401312d009483e13cd6b1179be8b8cb5858accbba84394cf9a7808080a02ce7dea3c25ac28c69ef5d425933bf6195c7c5648e4e228e3dca1f62f147449ea06b626df4ccad17b8c472ddba39b113c0e8f49569572fdd4ac2f6e2ddfc29726682012c9483e13cd6b1179be8b8cb5858accbba84394cf9a782c350c0b8412ce7dea3c25ac28c69ef5d425933bf6195c7c5648e4e228e3dca1f62f147449e6b626df4ccad17b8c472ddba39b113c0e8f49569572fdd4ac2f6e2ddfc2972661bf85b94ba226d47cbb2731cbaa67c916c57d68484aa269fb8448c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000

return "0x71" + RLP.EncodeDataItemsAsElementOrListAndCombineAsList(fields.ToArray(), new int[] { 13 }).ToHex(); // 13 = FactoryDeps
return "0x71" + RLP.EncodeDataItemsAsElementOrListAndCombineAsList(fields.ToArray(), new int[] { 13, 15 }).ToHex();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ public class ZkSyncAATransaction
[Parameter("uint256", "txType", 1)]
public virtual BigInteger TxType { get; set; }

[Parameter("address", "from", 2)]
public virtual string From { get; set; }
[Parameter("uint256", "from", 2)]
public virtual BigInteger From { get; set; }

[Parameter("address", "to", 3)]
public virtual string To { get; set; }
[Parameter("uint256", "to", 3)]
public virtual BigInteger To { get; set; }

[Parameter("uint256", "gasLimit", 4)]
public virtual BigInteger GasLimit { get; set; }
Expand All @@ -198,8 +198,8 @@ public class ZkSyncAATransaction
[Parameter("uint256", "maxPriorityFeePerGas", 7)]
public virtual BigInteger MaxPriorityFeePerGas { get; set; }

[Parameter("address", "paymaster", 8)]
public virtual string Paymaster { get; set; }
[Parameter("uint256", "paymaster", 8)]
public virtual BigInteger Paymaster { get; set; }

[Parameter("uint256", "nonce", 9)]
public virtual BigInteger Nonce { get; set; }
Expand Down

0 comments on commit 024da5b

Please sign in to comment.