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 Nov 30, 2022
2 parents 02dae99 + c7a6282 commit d65094c
Show file tree
Hide file tree
Showing 36 changed files with 829 additions and 303 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
},
{
"package": "GenericJSON",
"repositoryURL": "https://github.com/zoul/generic-json-swift",
"repositoryURL": "https://github.com/iwill/generic-json-swift",
"state": {
"branch": null,
"revision": "a137894b2a217abe489cdf1ea287e6f7819b1051",
"version": "2.0.1"
"revision": "0a06575f4038b504e78ac330913d920f1630f510",
"version": "2.0.2"
}
},
{
Expand Down
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
],
dependencies: [
.package(name: "BigInt", url: "https://github.com/attaswift/BigInt", from: "5.0.0"),
.package(name: "GenericJSON", url: "https://github.com/zoul/generic-json-swift", from: "2.0.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"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
Expand Down Expand Up @@ -56,7 +56,9 @@ let package = Package(
path: "web3sTests",
resources: [
.copy("Resources/rlptests.json"),
.copy("Account/cryptofights_712.json")
.copy("Account/cryptofights_712.json"),
.copy("Account/ethermail_signTypedDataV4.json"),
.copy("Account/real_word_opensea_signTypedDataV4.json"),
]
)
]
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ $ pod install

### Getting Started

Create an instance of `EthereumAccount` with a `EthereumKeyStorage` provider. This provides a wrapper around your key for web3.swift to use. **NOTE** We recommend you implement your own KeyStorage provider, instead of relying on the provided `EthereumKeyLocalStorage` class. This is provided as an example for conformity to the `EthereumKeyStorageProtocol`.
Create an instance of `EthereumAccount` with a `EthereumKeyStorage` provider. This provides a wrapper around your key for web3.swift to use. **NOTE We recommend you implement your own KeyStorage provider, instead of relying on the provided `EthereumKeyLocalStorage` class. This is provided as an example for conformity to the `EthereumSingleKeyStorageProtocol`.**

```swift
import web3


// This is just an example. EthereumKeyLocalStorage should not be used in production code
let keyStorage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "MY_PASSWORD")
let account = try? EthereumAccount.create(replacing: keyStorage, keystorePassword: "MY_PASSWORD")
```

Create an instance of `EthereumHttpClient` or `EthereumWebSocketClient`. This will then provide you access to a set of functions for interacting with the Blockchain.
Expand All @@ -50,7 +50,7 @@ OR

`EthereumWebSocketClient`
```swift
guard let clientUrl = URL(string: "wss://ropsten.infura.io/ws/v3//123") else { return }
guard let clientUrl = URL(string: "wss://goerli.infura.io/ws/v3//123") else { return }
let client = EthereumWebSocketClient(url: clientUrl)
```

Expand Down Expand Up @@ -159,7 +159,7 @@ We support querying ERC721 token data via the `ERC721` struct. Including:

### Running Tests

The tests will all pass when running against Ropsten. You will need to provide a URL for the blockchain proxy (e.g. on Infura), and a key-pair in `TestConfig.swift`. Some of the account signing tests will fail, given the signature assertions are against a specific (unprovided) key.
The tests will all pass when running against Goerli. You will need to provide a URL for the blockchain proxy (e.g. on Infura), and a key-pair in `TestConfig.swift`. Some of the account signing tests will fail, given the signature assertions are against a specific (unprovided) key.

## Dependencies

Expand Down
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.3.0'
s.version = '1.4.0'
s.license = 'MIT'
s.summary = 'Ethereum API for Swift'
s.homepage = 'https://github.com/argentlabs/web3.swift'
Expand Down
12 changes: 12 additions & 0 deletions web3sTests/Account/EthereumAccount+SignTypedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ class EthereumAccount_SignTypedTests: XCTestCase {
let typedData = try! decoder.decode(TypedData.self, from: data)
XCTAssertEqual(try! typedData.signableHash().web3.hexString, "0xdb12328a6d193965801548e1174936c3aa7adbe1b54b3535a3c905bd4966467c")
}

func test_GivenCustomTypeArray_V4_ItHashesCorrectly() {
let simpleUrl = Bundle.module.url(forResource: "ethermail_signTypedDataV4", withExtension: "json")!
let simpleData = try! Data(contentsOf: simpleUrl)
let simpleTypedData = try! decoder.decode(TypedData.self, from: simpleData)
XCTAssertEqual(try! simpleTypedData.signableHash().web3.hexString, "0x8a2c45f690057d91a9738b313da3f65916327e1d5b9a1348b9fc1cff0dc4091e")

let realWorldUrl = Bundle.module.url(forResource: "real_word_opensea_signTypedDataV4", withExtension: "json")!
let realWorldData = try! Data(contentsOf: realWorldUrl)
let realWorldTypedData = try! decoder.decode(TypedData.self, from: realWorldData)
XCTAssertEqual(try! realWorldTypedData.signableHash().web3.hexString, "0x76a61293096587b582305a07a60785f92b99ae6c8647c4bcf46d6651db0bd778")
}

func test_givenExampleWithDynamicData_ItHashesCorrectly() {
let typedData = try! decoder.decode(TypedData.self, from: example4)
Expand Down
24 changes: 12 additions & 12 deletions web3sTests/Account/EthereumAccountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,41 @@ class EthereumAccountTests: XCTestCase {
}

func testCreateAccount() {
let storage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let account = try? EthereumAccount.create(keyStorage: storage, keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(replacing: storage, keystorePassword: "PASSWORD")
XCTAssertNotNil(account, "Failed to create account. Ensure key is valid in TestConfig.swift")
}

func testCreateAccountMultiple() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try? EthereumAccount.create(keyStorage: storage, keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try? EthereumAccount.create(addingTo: storage, keystorePassword: "PASSWORD")
XCTAssertNotNil(account, "Failed to create account. Ensure key is valid in TestConfig.swift")
}

func testImportAccount() {
let storage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(replacing: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")

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

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

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

func testFetchAccounts() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let accounts = try! storage.fetchAccounts()
XCTAssertTrue(accounts.contains(account.address))
}

func testDeleteAccount() {
let storage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let account = try! EthereumAccount.importAccount(keyStorage: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let storage = EthereumKeyLocalStorage()
let account = try! EthereumAccount.importAccount(addingTo: storage, privateKey: "0x2639f727ded571d584643895d43d02a7a190f8249748a2c32200cfc12dde7173", keystorePassword: "PASSWORD")
let ethereumAddress = EthereumAddress("0x675f5810feb3b09528e5cd175061b4eb8de69075")
try! storage.deletePrivateKey(for: ethereumAddress)
let accounts = try! storage.fetchAccounts()
Expand Down
10 changes: 5 additions & 5 deletions web3sTests/Account/EthereumKeyStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EthereumKeyStorageTests: XCTestCase {

func testEncryptAndStorePrivateKey() {
let randomData = Data.randomOfLength(256)!
let keyStorage = EthereumKeyLocalStorage() as EthereumKeyStorageProtocol
let keyStorage = EthereumKeyLocalStorage() as EthereumSingleKeyStorageProtocol
let password = "myP4ssw0rD"

do {
Expand Down Expand Up @@ -73,11 +73,11 @@ class EthereumKeyStorageTests: XCTestCase {
}

func testDeleteAllPrivateKeys() {
let keyStorage = EthereumKeyLocalStorage() as EthereumMultipleKeyStorageProtocol
let keyStorage = EthereumKeyLocalStorage()
do {
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(keyStorage: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
_ = try EthereumAccount.create(addingTo: keyStorage, keystorePassword: "PASSWORD")
try keyStorage.deleteAllKeys()
let countAfterDeleting = try keyStorage.fetchAccounts()
XCTAssertEqual(countAfterDeleting.count, 0)
Expand Down
70 changes: 70 additions & 0 deletions web3sTests/Account/ethermail_signTypedDataV4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"domain": {
"chainId": "137",
"name": "Etaher Mail",
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
"version": "1"
},
"message": {
"contents": "Hello, Bob!",
"from": {
"name": "Cow",
"wallets": [
"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"
]
},
"to": [{
"name": "Cow",
"wallets": [
"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"
]
}]
},
"primaryType": "Mail",
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"Mail": [
{
"name": "from",
"type": "Person"
},
{
"name": "to",
"type": "Person[]"
},
{
"name": "contents",
"type": "string"
}
],
"Person": [
{
"name": "name",
"type": "string"
},
{
"name": "wallets",
"type": "address[]"
}
]
}
}
Loading

0 comments on commit d65094c

Please sign in to comment.