Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding support for WebSocket #714

Draft
wants to merge 36 commits into
base: develop-4.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b26f3a6
moved keystore manager to web3 main object
odanylovych Dec 29, 2021
d3b410c
renamed attachedKeystoreManager
odanylovych Dec 29, 2021
791a863
added Web3SubscriptionProvider protocol
odanylovych Dec 31, 2021
9368301
some fixes for subscription provider
odanylovych Dec 31, 2021
5659133
Merge branch 'skywinder:develop' into develop
odanylovych Dec 31, 2021
98b0796
optional websocket delegator
odanylovych Jan 4, 2022
21779a2
moved common methods to default websocket provider
odanylovych Jan 4, 2022
d65eae1
merged jsonrpc methods
odanylovych Jan 4, 2022
c716c26
added verification for supported request methods
odanylovych Jan 4, 2022
e0bff8d
implemented sendAsync in WebsocketProvider
odanylovych Jan 5, 2022
a8a2120
implemented sendAsync for batch requests
odanylovych Jan 5, 2022
0ed27cf
added filter requests
odanylovych Jan 6, 2022
400913e
added subscribe methods to Eth
odanylovych Jan 7, 2022
ec49477
set queue when calling subscribe
odanylovych Jan 10, 2022
a3f7b5c
added internalQueue to WebsocketProvider
odanylovych Jan 10, 2022
46abd66
removed unneeded IWebsocketProvider
odanylovych Jan 10, 2022
4e6a096
added queue as param in subscribe
odanylovych Jan 10, 2022
90329c3
added method to convert SubscribeEventFilter to params
odanylovych Jan 10, 2022
9044573
reworked writeTimer to pendingRequests
odanylovych Jan 10, 2022
1ba9885
moved infura specific constructor
odanylovych Jan 11, 2022
4db90c1
removed url session from websocket
odanylovych Jan 11, 2022
bf9997c
fixes for subscribe methods
odanylovych Jan 12, 2022
86088cf
tests for subscribe methods
odanylovych Jan 12, 2022
b570545
added init to ErrorMessage, made batch requests public
odanylovych Jan 19, 2022
69b0a3f
created JSONRPCresponse.Result struct
odanylovych Jan 20, 2022
428251d
minor improvement: removed guard statement
odanylovych Feb 23, 2022
2d90894
removed LogItem and used EventLog
odanylovych Feb 24, 2022
54cf7e9
Merge remote-tracking branch 'upstream/develop' into develop
odanylovych May 3, 2022
d1add3b
added hash field in BlockHeader struct
odanylovych May 6, 2022
2c3cf29
Merge remote-tracking branch 'upstream/develop' into develop
odanylovych May 6, 2022
907d3da
included new files in project
odanylovych May 7, 2022
7c90489
updated websockets documentation
odanylovych May 13, 2022
4cfa03b
made EthereumTransaction.encode public
odanylovych May 17, 2022
e5a6579
chore: merge with develop
JeneaVranceanu Dec 20, 2022
2a0625e
chore: merge branch 'develop-upstream' into feat/webscoket-support
JeneaVranceanu Dec 20, 2022
6861408
fix: returned back the type of `network` to `Networks?` (optional)
JeneaVranceanu Dec 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@

**Closed issues:**

- Use custom JSONRPCmethod and Units [\#148](https://github.com/skywinder/web3swift/issues/148)
- Use custom JSONRPCMethod and Units [\#148](https://github.com/skywinder/web3swift/issues/148)
- ERC20 some functions are not working [\#146](https://github.com/skywinder/web3swift/issues/146)
- fix `pod install` absolute paths [\#97](https://github.com/skywinder/web3swift/issues/97)
- Installing issue by pod [\#76](https://github.com/skywinder/web3swift/issues/76)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//
// APIRequest+Methods.swift
//
//
// Created by Yaroslav Yashin on 12.07.2022.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import Foundation

Here's an example of using this enum in field.
```swift
let jsonRPCParams: [APIRequestParameterType] = [
let JSONRPCParams: [APIRequestParameterType] = [
.init(rawValue: 12)!,
.init(rawValue: "this")!,
.init(rawValue: 12.2)!,
.init(rawValue: [12.2, 12.4])!
]
let encoded = try JSONEncoder().encode(jsonRPCParams)
let encoded = try JSONEncoder().encode(JSONRPCParams)
print(String(data: encoded, encoding: .utf8)!)
//> [12,\"this\",12.2,[12.2,12.4]]`
```
Expand Down
33 changes: 21 additions & 12 deletions Sources/Web3Core/KeystoreManager/BIP32Keystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public class BIP32Keystore: AbstractKeystore {
}

public init?(_ jsonData: Data) {
guard var keystorePars = try? JSONDecoder().decode(KeystoreParamsBIP32.self, from: jsonData) else {return nil}
if keystorePars.version != Self.KeystoreParamsBIP32Version {return nil}
if keystorePars.crypto.version != nil && keystorePars.crypto.version != "1" {return nil}
if !keystorePars.isHDWallet {return nil}
guard var keystorePars = try? JSONDecoder().decode(KeystoreParamsBIP32.self, from: jsonData) else { return nil }
if keystorePars.version != Self.KeystoreParamsBIP32Version { return nil }
if keystorePars.crypto.version != nil && keystorePars.crypto.version != "1" { return nil }
if !keystorePars.isHDWallet { return nil }

addressStorage = PathAddressStorage(pathAddressPairs: keystorePars.pathAddressPairs)

Expand All @@ -88,8 +88,14 @@ public class BIP32Keystore: AbstractKeystore {
rootPrefix = keystoreParams!.rootPath!
}

public convenience init?(mnemonics: String, password: String, mnemonicsPassword: String = "", language: BIP39Language = BIP39Language.english, prefixPath: String = HDNode.defaultPathMetamaskPrefix, aesMode: String = "aes-128-cbc") throws {
guard var seed = BIP39.seedFromMmemonics(mnemonics, password: mnemonicsPassword, language: language) else {
public convenience init?(mnemonics: String,
password: String,
mnemonicsPassword: String = "",
language: BIP39Language = BIP39Language.english,
prefixPath: String = HDNode.defaultPathMetamaskPrefix,
aesMode: String = "aes-128-cbc") throws {
guard var seed = BIP39.seedFromMmemonics(mnemonics, password: mnemonicsPassword, language: language)
else {
throw AbstractKeystoreError.noEntropyError
}
defer {
Expand All @@ -98,9 +104,12 @@ public class BIP32Keystore: AbstractKeystore {
try self.init(seed: seed, password: password, prefixPath: prefixPath, aesMode: aesMode)
}

public init? (seed: Data, password: String, prefixPath: String = HDNode.defaultPathMetamaskPrefix, aesMode: String = "aes-128-cbc") throws {
public init?(seed: Data,
password: String,
prefixPath: String = HDNode.defaultPathMetamaskPrefix,
aesMode: String = "aes-128-cbc") throws {
addressStorage = PathAddressStorage()
guard let rootNode = HDNode(seed: seed)?.derive(path: prefixPath, derivePrivateKey: true) else {return nil}
guard let rootNode = HDNode(seed: seed)?.derive(path: prefixPath, derivePrivateKey: true) else { return nil }
self.rootPrefix = prefixPath
try createNewAccount(parentNode: rootNode, password: password)
guard let serializedRootNode = rootNode.serialize(serializePublic: false) else {
Expand All @@ -127,7 +136,7 @@ public class BIP32Keystore: AbstractKeystore {
try encryptDataToStorage(password, data: serializedRootNode, aesMode: self.keystoreParams!.crypto.cipher)
}

func createNewAccount(parentNode: HDNode, password: String ) throws {
func createNewAccount(parentNode: HDNode, password: String) throws {
var newIndex = UInt32(0)
for p in addressStorage.paths {
guard let idx = UInt32(p.components(separatedBy: "/").last!) else {continue}
Expand All @@ -151,7 +160,8 @@ public class BIP32Keystore: AbstractKeystore {
addressStorage.add(address: newAddress, for: newPath)
}

public func createNewCustomChildAccount(password: String, path: String) throws {guard let decryptedRootNode = try? self.getPrefixNodeData(password) else {
public func createNewCustomChildAccount(password: String, path: String) throws {
guard let decryptedRootNode = try? self.getPrefixNodeData(password) else {
throw AbstractKeystoreError.encryptionError("Failed to decrypt a keystore")
}
guard let rootNode = HDNode(decryptedRootNode) else {
Expand Down Expand Up @@ -363,8 +373,7 @@ public class BIP32Keystore: AbstractKeystore {
guard let params = self.keystoreParams else {
return nil
}
let data = try JSONEncoder().encode(params)
return data
return try JSONEncoder().encode(params)
}

public func serializeRootNodeToString(password: String ) throws -> String {
Expand Down
4 changes: 1 addition & 3 deletions Sources/Web3Core/Structure/Block/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import BigInt
/// Ethereum Block
///
/// Official specification: [](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.json)
public struct Block {
public struct Block: APIResultType {

public var number: BigUInt // MARK: This is optional in web3js, but required in Ethereum JSON-RPC
public var hash: Data // MARK: This is optional in web3js, but required in Ethereum JSON-RPC
Expand Down Expand Up @@ -103,5 +103,3 @@ extension Block: Decodable {
}
}
}

extension Block: APIResultType { }
25 changes: 25 additions & 0 deletions Sources/Web3Core/Structure/Block/BlockHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// BlockHeader.swift
//
// Created by JeneaVranceanu on 16.12.2022.
//

import Foundation

public struct BlockHeader: Decodable {
public let hash: String
public let difficulty: String
public let extraData: String
public let gasLimit: String
public let gasUsed: String
public let logsBloom: String
public let miner: String
public let nonce: String
public let number: String
public let parentHash: String
public let receiptsRoot: String
public let sha3Uncles: String
public let stateRoot: String
public let timestamp: String
public let transactionsRoot: String
}
21 changes: 21 additions & 0 deletions Sources/Web3Core/Structure/Block/SyncingInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// SyncingInfo.swift
//
// Created by JeneaVranceanu on 16.12.2022.
//

import Foundation

/// Returned to a WebSocket connections that subscribed on `"syncing"` event.
public struct SyncingInfo: Decodable {
public struct Status: Decodable {
public let startingBlock: Int
public let currentBlock: Int
public let highestBlock: Int
public let pulledStates: Int
public let knownStates: Int
}

public let syncing: Bool
public let status: Status?
}
Loading