Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthMike committed Dec 13, 2023
2 parents 2bbb2f2 + c85e063 commit 1e75f98
Show file tree
Hide file tree
Showing 63 changed files with 1,411 additions and 512 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Lint
run: ./scripts/runSwiftFormat.sh -l
# - name: Lint
# run: ./scripts/runSwiftFormat.sh -l
macos:
needs: lint
# needs: lint
runs-on: macos-latest
env:
TESTS_PRIVATEKEY: ${{ secrets.TESTS_PRIVATEKEY }}
Expand All @@ -26,10 +26,10 @@ jobs:
- name: Tests
run: swift test -v
linux:
needs: lint
# needs: lint
runs-on: ubuntu-latest
container:
image: swift:5.5-bionic
image: swift:5.7-bionic
env:
TESTS_PRIVATEKEY: ${{ secrets.TESTS_PRIVATEKEY }}
steps:
Expand Down
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GEM
specs:
CFPropertyList (3.0.5)
rexml
activesupport (6.1.7.2)
activesupport (6.1.7.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -54,7 +54,7 @@ GEM
netrc (~> 0.11)
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.2.0)
concurrent-ruby (1.2.2)
escape (0.0.4)
ethon (0.15.0)
ffi (>= 1.15.0)
Expand All @@ -63,10 +63,10 @@ GEM
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
httpclient (2.8.3)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.1)
minitest (5.17.0)
minitest (5.19.0)
molinillo (0.8.0)
nanaimo (0.3.0)
nap (1.1.0)
Expand All @@ -85,7 +85,7 @@ GEM
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (~> 3.2.4)
zeitwerk (2.6.6)
zeitwerk (2.6.11)

PLATFORMS
ruby
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/attaswift/BigInt",
"state": {
"branch": null,
"revision": "889a1ecacd73ccc189c5cb29288048f186c44ed9",
"version": "5.2.1"
"revision": "0ed110f7555c34ff468e72e1686e59721f2b0da6",
"version": "5.3.0"
}
},
{
Expand All @@ -24,8 +24,8 @@
"repositoryURL": "https://github.com/GigaBitcoin/secp256k1.swift.git",
"state": {
"branch": null,
"revision": "39dd39248e769ea88253a0ce300399b402a64529",
"version": "0.9.2"
"revision": "1a14e189def5eaa92f839afdd2faad8e43b61a6e",
"version": "0.12.2"
}
},
{
Expand Down
18 changes: 14 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ let package = Package(
.watchOS(.v7)
],
products: [
.library(name: "web3.swift", targets: ["web3"])
.library(name: "web3.swift", targets: ["web3"]),
.library(name: "web3-zksync.swift", targets: ["web3-zksync"])
],
dependencies: [
.package(name: "BigInt", url: "https://github.com/attaswift/BigInt", from: "5.0.0"),
.package(name: "BigInt", url: "https://github.com/attaswift/BigInt", from: "5.3.0"),
.package(name: "GenericJSON", url: "https://github.com/iwill/generic-json-swift", .upToNextMajor(from: "2.0.0")),
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMajor(from: "0.6.0")),
.package(url: "https://github.com/vapor/websocket-kit.git", from: "2.0.0"),
Expand All @@ -32,7 +33,16 @@ let package = Package(
.product(name: "WebSocketKit", package: "websocket-kit"),
.product(name: "Logging", package: "swift-log")
],
path: "web3swift/src"
path: "web3swift/src",
exclude: ["ZKSync"]
),
.target(
name: "web3-zksync",
dependencies:
[
.target(name: "web3")
],
path: "web3swift/src/ZKSync"
),
.target(
name: "keccaktiny",
Expand All @@ -53,7 +63,7 @@ let package = Package(
),
.testTarget(
name: "web3swiftTests",
dependencies: ["web3"],
dependencies: ["web3", "web3-zksync"],
path: "web3sTests",
resources: [
.copy("Resources/rlptests.json"),
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ We support querying ERC721 token data via the `ERC721` struct. Including:
- Retrieve `Transfer` events
- Decode standard JSON for NFT metadata. Please be aware some smart contracts are not 100% compliant with standard.


### ZKSync Era

We also include additional helpers to interact with [ZKSync Era](https://zksync.io/), by importing `web3_zksync`.

Take a look at [ZKSyncTransaction](https://github.com/argentlabs/web3.swift/blob/develop/web3swift/src/ZKSync/ZKSyncTransaction.swift) or use directly
[ZKSyncClient](https://github.com/argentlabs/web3.swift/blob/develop/web3swift/src/ZKSync/ZKSyncProvider.swift) which has similar API as the `EthereumClient`

### Running Tests

Some of the tests require a private key, which is not stored in the repository. You can ignore these while testing locally, as CI will use the encrypted secret key from Github.
Expand All @@ -183,7 +191,7 @@ Package dependencies:
- [Vapor Websocket](https://github.com/vapor/websocket-kit.git)
- [Apple Swift-log](https://github.com/apple/swift-log.git)

Also for Linux build, we can't se Apple crypto APIs, so we embedded a small subset of CryptoSwift (instead of importing the whole library). Credit to [Marcin Krzyżanowski](https://github.com/krzyzanowskim/CryptoSwift)
Also for Linux build, we can't use Apple crypto APIs, so we embedded a small subset of CryptoSwift (instead of importing the whole library). Credit to [Marcin Krzyżanowski](https://github.com/krzyzanowskim/CryptoSwift)

## Contributors

Expand Down
2 changes: 1 addition & 1 deletion scripts/swiftformat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@
--wraptypealiases preserve
--xcodeindentation disabled
--yodaswap always
--disable enumNamespaces,extensionAccessControl,fileHeader,genericExtensions,modifierOrder,numberFormatting,opaqueGenericParameters,preferKeyPath,redundantBackticks,redundantExtensionACL,redundantFileprivate,redundantPattern,redundantRawValues,redundantSelf,sortDeclarations,spaceAroundGenerics,strongOutlets,trailingClosures,trailingCommas,unusedArguments,wrap,wrapMultilineStatementBraces,wrapSingleLineComments,yodaConditions
--disable enumNamespaces,extensionAccessControl,fileHeader,genericExtensions,modifierOrder,numberFormatting,opaqueGenericParameters,preferKeyPath,redundantBackticks,redundantExtensionACL,redundantFileprivate,redundantPattern,redundantRawValues,redundantSelf,sortDeclarations,spaceAroundGenerics,strongOutlets,trailingClosures,trailingCommas,unusedArguments,wrap,wrapMultilineStatementBraces,wrapSingleLineComments,yodaConditions,consecutiveSpaces
--enable blankLineAfterImports,blankLinesBetweenImports,isEmpty,wrapConditionalBodies
2 changes: 1 addition & 1 deletion web3.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'web3.swift'
s.version = '1.5.0'
s.version = '1.6.1'
s.license = 'MIT'
s.summary = 'Ethereum API for Swift'
s.homepage = 'https://github.com/argentlabs/web3.swift'
Expand Down
17 changes: 5 additions & 12 deletions web3sTests/Account/EthereumAccount+SignTransactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,11 @@ class EthereumAccount_SignTransactionTests: XCTestCase {
let gasLimit = BigUInt(hex: "0x5208")!
let to = EthereumAddress("0x3535353535353535353535353535353535353535")
let value = BigUInt(hex: "0x0")!
let v = Int(hex: "0x25")!
let r = Data(hex: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d")!
let s = Data(hex: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d")!

var chainId = v
if chainId >= 37 {
chainId = (chainId - 35) / 2
}

let tx = EthereumTransaction(from: nil, to: to, value: value, data: nil, nonce: nonce, gasPrice: gasPrice, gasLimit: gasLimit, chainId: chainId)
let signed = SignedTransaction(transaction: tx, v: v, r: r, s: s)

let signature = "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d25".web3.hexData!

let tx = EthereumTransaction(from: nil, to: to, value: value, data: nil, nonce: nonce, gasPrice: gasPrice, gasLimit: gasLimit, chainId: 37)
let signed = SignedTransaction(transaction: tx, signature: signature)

let raw = signed.raw!.web3.hexString
let hash = signed.hash!.web3.hexString

Expand Down
8 changes: 4 additions & 4 deletions web3sTests/Account/EthereumAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class EthereumAccountTests: XCTestCase {

func testLoadAccountAndAddress() {
let account = try! EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey))
XCTAssertEqual(account.address.value.lowercased(), TestConfig.publicKey.lowercased(), "Failed to load private key. Ensure key is valid in TestConfig.swift")
XCTAssertEqual(account.address, EthereumAddress(TestConfig.publicKey), "Failed to load private key. Ensure key is valid in TestConfig.swift")
}

func testLoadAccountAndAddressMultiple() {
let storage = TestEthereumMultipleKeyStorage(privateKey: TestConfig.privateKey)
let account = try! EthereumAccount(addressString: TestConfig.publicKey, keyStorage: storage)
XCTAssertEqual(account.address.value.lowercased(), TestConfig.publicKey.lowercased(), "Failed to load private key. Ensure key is valid in TestConfig.swift")
XCTAssertEqual(account.address, EthereumAddress(TestConfig.publicKey), "Failed to load private key. Ensure key is valid in TestConfig.swift")
}

func testCreateAccount() {
Expand All @@ -42,14 +42,14 @@ class EthereumAccountTests: XCTestCase {
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(replacing: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")

XCTAssertEqual(account.address.value, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
XCTAssertEqual(account.address, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
}

func testImportAccountMultiple() {
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")

XCTAssertEqual(account.address.value, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
XCTAssertEqual(account.address, "0x675f5810feb3b09528e5cd175061b4eb8de69075")
}

func testFetchAccounts() {
Expand Down
49 changes: 49 additions & 0 deletions web3sTests/Address/EthereumAddressTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// web3.swift
// Copyright © 2023 Argent Labs Limited. All rights reserved.
//

import XCTest
@testable import web3

class EthereumAddressTests: XCTestCase {
private var values: Set<EthereumAddress>!
private let addr1 = EthereumAddress("0x162142f0508F557C02bEB7C473682D7C91Bcef41")
private let addr1Padded = EthereumAddress("0x0162142f0508F557C02bEB7C473682D7C91Bcef41")
private let addr2 = EthereumAddress("0x162142f0508F557C02bEB7C473682D7C91Bcef42")

func testGivenAddress_WhenComparingWithSameAddressString_AddressIsEqual() {
XCTAssertEqual(addr1, addr1)
}

func testGivenAddress_WhenHashingWithSameAddressString_AddressIsEqual() {
values = [addr1]
XCTAssertTrue(values.contains(addr1))
}

func testGivenAddress_WhenComparingWithDifferentAddressString_AddressNotEqual() {
XCTAssertNotEqual(addr1, addr2)
}

func testGivenAddress_WhenComparingWith0PaddedAddress_AddressIsEqual() {
XCTAssertEqual(addr1, addr1Padded)
}

func testGivenAddress_WhenHashingWith0PaddedAddress_AddressIsEqual() {
values = [addr1]
XCTAssertTrue(values.contains(addr1Padded))
}

func testGiven0PaddedAddress_WhenHashingWithNotPaddedAddress_AddressIsEqual() {
values = [addr1Padded]
XCTAssertTrue(values.contains(addr1))
}

func testGivenAddress_WhenHashing_EqualToSameAddressHash() {
XCTAssertEqual(addr1.hashValue, addr1.hashValue)
}

func testGivenAddress_WhenHashing_EqualToPaddedAddressHash() {
XCTAssertEqual(addr1.hashValue, addr1Padded.hashValue)
}
}
16 changes: 8 additions & 8 deletions web3sTests/Client/EthereumClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ class EthereumClientTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
account = try? EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey))
print("Public address: \(account?.address.value ?? "NONE")")
}

func testEthGetTransactionCount() async {
Expand Down Expand Up @@ -116,7 +115,8 @@ class EthereumClientTests: XCTestCase {

func testEthSendRawTransaction() async {
do {
let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1600000), data: nil, nonce: 2, gasPrice: BigUInt(4000000), gasLimit: BigUInt(500000), chainId: EthereumNetwork.goerli.intValue)
let gasPrice = try await client?.eth_gasPrice()
let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1), data: nil, nonce: 2, gasPrice: gasPrice ?? BigUInt(9000000), gasLimit: BigUInt(30000), chainId: EthereumNetwork.goerli.intValue)

let txHash = try await client?.eth_sendRawTransaction(tx, withAccount: account!)
XCTAssertNotNil(txHash, "No tx hash, ensure key is valid in TestConfig.swift")
Expand Down Expand Up @@ -208,8 +208,8 @@ class EthereumClientTests: XCTestCase {
func testGivenMinedTransactionHash_ThenGetsTransactionByHash() async {
do {
let transaction = try await client?.eth_getTransaction(byHash: "0x706bbe6f2593235942b8e76c2f37af3824d47a64caf65f7ae5e0c5ee1e886132")
XCTAssertEqual(transaction?.from?.value, "0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793")
XCTAssertEqual(transaction?.to.value, "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
XCTAssertEqual(transaction?.from, "0x64d0ea4fc60f27e74f1a70aa6f39d403bbe56793")
XCTAssertEqual(transaction?.to, "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984")
XCTAssertEqual(transaction?.gas, "85773")
XCTAssertEqual(transaction?.gasPrice, BigUInt(14300000000))
XCTAssertEqual(transaction?.nonce, 23)
Expand Down Expand Up @@ -435,14 +435,14 @@ class EthereumWebSocketClientTests: EthereumClientTests {

override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)

}
#if os(Linux)
// On Linux some tests are fail. Need investigation
#else
func testWebSocketNoAutomaticOpen() {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false))
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false), network: TestConfig.network)

guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
Expand All @@ -453,7 +453,7 @@ class EthereumWebSocketClientTests: EthereumClientTests {
}

func testWebSocketConnect() {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false))
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false), network: TestConfig.network)

guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
Expand Down
4 changes: 2 additions & 2 deletions web3sTests/Contract/ABIEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ABIEventTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}

func test_givenEventWithData4_ItParsesCorrectly() async {
Expand Down Expand Up @@ -60,7 +60,7 @@ class ABIEventTests: XCTestCase {
class ABIEventWebSocketTests: ABIEventTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}

Expand Down
10 changes: 5 additions & 5 deletions web3sTests/ENS/ENSOffchainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ENSOffchainTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}

func testDNSEncode() {
Expand Down Expand Up @@ -60,7 +60,7 @@ class ENSOffchainTests: XCTestCase {
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x19c2d5d0f035563344dbb7be5fd09c8dad62b001"), ens)
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class ENSOffchainTests: XCTestCase {
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x19c2d5d0f035563344dbb7be5fd09c8dad62b001"), ens)
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
Expand Down Expand Up @@ -139,7 +139,7 @@ class ENSOffchainTests: XCTestCase {
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x19c2d5d0f035563344dbb7be5fd09c8dad62b001"), ens)
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
Expand All @@ -149,6 +149,6 @@ class ENSOffchainTests: XCTestCase {
class ENSOffchainWebSocketTests: ENSOffchainTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
Loading

0 comments on commit 1e75f98

Please sign in to comment.