-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #319 from argentlabs/feature/zksync-era-support
ZKSync Era support
- Loading branch information
Showing
24 changed files
with
683 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// web3.swift | ||
// Copyright © 2022 Argent Labs Limited. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import web3_zksync | ||
@testable import web3 | ||
import XCTest | ||
import BigInt | ||
|
||
|
||
final class EthereumClientZKSyncTests: XCTestCase { | ||
let eoaAccount = try! EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey)) | ||
let client = ZKSyncClient(url: TestConfig.ZKSync.clientURL) | ||
var eoaEthTransfer = ZKSyncTransaction( | ||
from: .init(TestConfig.publicKey), | ||
to: .init("0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793"), | ||
value: 100, | ||
data: Data(), | ||
gasLimit: 300000 | ||
) | ||
|
||
func test_GivenEOAAccount_WhenSendETH_ThenSendsCorrectly() async { | ||
do { | ||
let gasPrice = try await client.eth_gasPrice() | ||
eoaEthTransfer.gasPrice = gasPrice | ||
let txHash = try await client.eth_sendRawZKSyncTransaction(eoaEthTransfer, withAccount: eoaAccount) | ||
XCTAssertNotNil(txHash, "No tx hash, ensure key is valid in TestConfig.swift") | ||
} catch { | ||
XCTFail("Expected tx but failed \(error).") | ||
} | ||
} | ||
|
||
// TODO: Integrate paymaster | ||
// func test_GivenEOAAccount_WhenSendETH_AndFeeIsInUSDC_ThenSendsCorrectly() async { | ||
// do { | ||
// let txHash = try await client.eth_sendRawZKSyncTransaction(with(eoaEthTransfer) { | ||
// $0.feeToken = EthereumAddress("0x54a14D7559BAF2C8e8Fa504E019d32479739018c") | ||
// }, withAccount: eoaAccount) | ||
// XCTAssertNotNil(txHash, "No tx hash, ensure key is valid in TestConfig.swift") | ||
// } catch { | ||
// XCTFail("Expected tx but failed \(error).") | ||
// } | ||
// } | ||
|
||
func test_GivenEOATransaction_gasEstimationCorrect() async { | ||
do { | ||
let estimate = try await client.estimateGas( | ||
with(eoaEthTransfer) { | ||
$0.gasPrice = nil | ||
$0.gasLimit = nil | ||
} | ||
) | ||
XCTAssertGreaterThan(estimate, 1000) | ||
} catch { | ||
XCTFail("Expected value but failed \(error).") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// web3.swift | ||
// Copyright © 2022 Argent Labs Limited. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import web3_zksync | ||
@testable import web3 | ||
import BigInt | ||
|
||
final class ZKSyncTransactionTests: XCTestCase { | ||
|
||
let eoaAccount = try! EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey)) | ||
|
||
let eoaTransfer = ZKSyncTransaction( | ||
from: .init(TestConfig.publicKey), | ||
to: .init("0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793"), | ||
value: BigUInt(hex: "0x5af3107a4000")!, | ||
data: Data(), | ||
chainId: TestConfig.ZKSync.chainId, | ||
nonce: 4, | ||
gasPrice: BigUInt(hex: "0x05f5e100")!, | ||
gasLimit: BigUInt(hex: "0x080a22")! | ||
) | ||
|
||
func test_GivenEOATransfer_EncodesCorrectly() { | ||
let signature = "0x55943b2228183717fd3be583bde0f6ec168247ea8d304eb13b3e7e76ebf6bf2c3c77734e163711c5963ac25a15f95d9ac63b82c2c427fd4eb011c5e3a22f89221b".web3.hexData! | ||
let signed = ZKSyncSignedTransaction( | ||
transaction: eoaTransfer, signature: .init(raw: signature) | ||
) | ||
XCTAssertEqual(signed.raw?.web3.hexString, "0x71f891048405f5e1008405f5e10083080a229464d0ea4fc60f27e74f1a70aa6f39d403bbe56793865af3107a400080820118808082011894e78e5ecb061fe3dd1672ddda7b5116213b23b99a82c350c0b84155943b2228183717fd3be583bde0f6ec168247ea8d304eb13b3e7e76ebf6bf2c3c77734e163711c5963ac25a15f95d9ac63b82c2c427fd4eb011c5e3a22f89221bc0") | ||
} | ||
|
||
func test_GivenEOATransfer_WhenSigningWithEOAAccount_ThenSignsAndEncodesCorrectly() { | ||
let signed = try? eoaAccount.sign(zkTransaction: eoaTransfer) | ||
|
||
XCTAssertEqual(signed?.raw?.web3.hexString, | ||
"0x71f891048405f5e1008405f5e10083080a229464d0ea4fc60f27e74f1a70aa6f39d403bbe56793865af3107a400080820118808082011894e78e5ecb061fe3dd1672ddda7b5116213b23b99a82c350c0b841c956ba7bfdf54a6d3f3b21c51465ad37df22b6258835b6e162259d6d3eec02ae11f9d17c3aafd47df49bd77e33befed87bbaff44e4c497228bfa8bcc9fa64bc31bc0") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// web3.swift | ||
// Copyright © 2022 Argent Labs Limited. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct Signature: Equatable { | ||
public let r: Data | ||
public let s: Data | ||
public let v: Int | ||
public let recoveryParam: Int | ||
let raw: Data | ||
|
||
public var flattened: Data { | ||
raw | ||
} | ||
|
||
public init( | ||
r: Data, | ||
s: Data, | ||
v: Int, | ||
recoveryParam: Int | ||
) { | ||
self.r = r | ||
self.s = s | ||
self.v = v | ||
self.recoveryParam = recoveryParam | ||
self.raw = r + s + Data([UInt8(v)]) | ||
} | ||
|
||
public init( | ||
raw: Data | ||
) { | ||
self.raw = raw | ||
(self.r, self.s, self.v) = raw.extractRSV() | ||
self.recoveryParam = 1 - (self.v % 2) | ||
} | ||
|
||
public static let zero: Signature = .init(raw: Data(repeating: 0, count: 65)) | ||
} | ||
|
||
extension Data { | ||
func extractRSV() -> (Data, Data, Int) { | ||
guard count >= 65 else { | ||
fatalError("Invalid usage: Need a correctly sized signature") | ||
} | ||
|
||
let r = subdata(in: 0 ..< 32) | ||
let s = subdata(in: 32 ..< 64) | ||
var v = Int(self[64]) | ||
if v < 27 { // recid == v | ||
v += 27 | ||
} | ||
|
||
return (r.web3.strippingZeroesFromBytes, s.web3.strippingZeroesFromBytes, v) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.