Skip to content

Commit f8bf43f

Browse files
Merge branch 'develop' into chore/ec-recover-message-and-hash
2 parents 2a155d3 + 58219ae commit f8bf43f

File tree

11 files changed

+243
-236
lines changed

11 files changed

+243
-236
lines changed

Sources/Web3Core/EthereumNetwork/RequestParameter/RequestParameter+Encodable.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
import Foundation
99

1010
extension RequestParameter: Encodable {
11-
/**
12-
This encoder encodes `RequestParameter` associated value ignoring self value
13-
14-
This is required to encode mixed types array, like
15-
16-
```swift
17-
let someArray: [RequestParameter] = [
18-
.init(rawValue: 12)!,
19-
.init(rawValue: "this")!,
20-
.init(rawValue: 12.2)!,
21-
.init(rawValue: [12.2, 12.4])!
22-
]
23-
let encoded = try JSONEncoder().encode(someArray)
24-
print(String(data: encoded, encoding: .utf8)!)
25-
//> [12,\"this\",12.2,[12.2,12.4]]`
26-
```
27-
*/
11+
12+
/// This encoder encodes `RequestParameter` associated value ignoring self value
13+
///
14+
/// This is required to encode mixed types array, like
15+
///
16+
/// ```swift
17+
/// let someArray: [RequestParameter] = [
18+
/// .init(rawValue: 12)!,
19+
/// .init(rawValue: "this")!,
20+
/// .init(rawValue: 12.2)!,
21+
/// .init(rawValue: [12.2, 12.4])!
22+
/// ]
23+
/// let encoded = try JSONEncoder().encode(someArray)
24+
/// print(String(data: encoded, encoding: .utf8)!)
25+
/// //> [12,\"this\",12.2,[12.2,12.4]]`
26+
/// ```
27+
/// - Parameter encoder: The encoder to write data to.
2828
func encode(to encoder: Encoder) throws {
2929
var enumContainer = encoder.singleValueContainer()
3030
/// force casting in this switch is safe because

Sources/Web3Core/EthereumNetwork/RequestParameter/RequestParameter+RawRepresentable.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import Foundation
99

1010
extension RequestParameter: RawRepresentable {
11-
/**
12-
This init is required by ``RawRepresentable`` protocol, which is required to encode mixed type values array in JSON.
1311

14-
This protocol is used to implement custom `encode` method for that enum,
15-
which encodes an array of self-assosiated values.
16-
17-
You're totally free to use explicit and more convenience member init as `RequestParameter.int(12)` in your code.
18-
*/
12+
/// This init is required by ``RawRepresentable`` protocol, which is required
13+
/// to encode mixed type values array in JSON.
14+
///
15+
/// This protocol is used to implement custom `encode` method for that enum,
16+
/// which encodes an array of self-assosiated values.
17+
///
18+
/// You're totally free to use explicit and more convenience member init as `RequestParameter.int(12)` in your code.
19+
/// - Parameter rawValue: one of the supported types like `Int`, `UInt` etc.
1920
init?(rawValue: APIRequestParameterType) {
2021
/// force casting in this switch is safe because
2122
/// each `rawValue` forced to casts only in exact case which is runs based on `rawValues` type

Sources/Web3Core/EthereumNetwork/RequestParameter/RequestParameter.swift

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
//
22
// RequestParameter.swift
3-
//
3+
//
44
//
55
// Created by Yaroslav Yashin on 12.07.2022.
66
//
77

88
import Foundation
99

10-
/**
11-
Enum to compose request to the node params.
12-
13-
In most cases request params are passed to Ethereum JSON RPC request as array of mixed type values, such as `[12,"this",true]`.
14-
15-
Meanwhile swift don't provide strict way to compose such array it gives some hacks to solve this task
16-
and one of them is using `RawRepresentable` protocol.
17-
18-
This protocol allows the designated type to represent itself in `String` representation.
19-
20-
So in our case we're using it to implement custom `encode` method for all possible types used as request params.
21-
22-
This `encode` method is required to encode array of `RequestParameter` to not to `[RequestParameter.int(1)]`, but to `[1]`.
23-
24-
Here's an example of using this enum in field.
25-
```swift
26-
let jsonRPCParams: [APIRequestParameterType] = [
27-
.init(rawValue: 12)!,
28-
.init(rawValue: "this")!,
29-
.init(rawValue: 12.2)!,
30-
.init(rawValue: [12.2, 12.4])!
31-
]
32-
let encoded = try JSONEncoder().encode(jsonRPCParams)
33-
print(String(data: encoded, encoding: .utf8)!)
34-
//> [12,\"this\",12.2,[12.2,12.4]]`
35-
```
36-
*/
10+
/// Enum to compose request to the node params.
11+
///
12+
/// In most cases request params are passed to Ethereum JSON RPC request as array of
13+
/// mixed type values, such as `[12,"this",true]`.
14+
///
15+
/// Meanwhile swift don't provide strict way to compose such array it gives
16+
/// some hacks to solve this task
17+
/// and one of them is using `RawRepresentable` protocol.
18+
///
19+
/// This protocol allows the designated type to represent itself in `String` representation.
20+
///
21+
/// So in our case we're using it to implement custom `encode` method for all possible
22+
/// types used as request params.
23+
///
24+
/// This `encode` method is required to encode array of `RequestParameter`
25+
/// to not to `[RequestParameter.int(1)]`, but to `[1]`.
26+
///
27+
/// Here's an example of using this enum in field.
28+
/// ```swift
29+
/// let jsonRPCParams: [APIRequestParameterType] = [
30+
/// .init(rawValue: 12)!,
31+
/// .init(rawValue: "this")!,
32+
/// .init(rawValue: 12.2)!,
33+
/// .init(rawValue: [12.2, 12.4])!
34+
/// ]
35+
/// let encoded = try JSONEncoder().encode(jsonRPCParams)
36+
/// print(String(data: encoded, encoding: .utf8)!)
37+
/// //> [12,\"this\",12.2,[12.2,12.4]]`
38+
/// ```
3739
enum RequestParameter {
3840
case int(Int)
3941
case intArray([Int])

Sources/Web3Core/KeystoreManager/BIP32HDNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class HDNode {
9797
let hmacKey = "Bitcoin seed".data(using: .ascii)!
9898
let hmac: Authenticator = HMAC(key: hmacKey.bytes, variant: HMAC.Variant.sha2(.sha512))
9999
guard let entropy = try? hmac.authenticate(seed.bytes) else { return nil }
100-
guard entropy.count == 64 else { return nil}
100+
guard entropy.count == 64 else { return nil }
101101
let I_L = entropy[0..<32]
102102
let I_R = entropy[32..<64]
103103
chaincode = Data(I_R)

Sources/Web3Core/KeystoreManager/BIP44.swift

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
import Foundation
77

88
public protocol BIP44 {
9-
/**
10-
Derive an ``HDNode`` based on the provided path. The function will throw ``BIP44Error.warning`` if it was invoked with `throwOnWarning` equal to
11-
`true` and the root key doesn't have a previous child with at least one transaction. If it is invoked with `throwOnWarning` equal to `false` the child node will be
12-
derived directly using the derive function of ``HDNode``. This function needs to query the blockchain history when `throwOnWarning` is `true`, so it can throw
13-
network errors.
14-
- Parameter path: valid BIP44 path.
15-
- Parameter throwOnWarning: `true` to use
16-
[Account Discovery](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery) standard,
17-
otherwise it will dervive the key using the derive function of ``HDNode``.
18-
- Throws: ``BIP44Error.warning`` if the child key shouldn't be used according to
19-
[Account Discovery](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery) standard.
20-
- Returns: an ``HDNode`` child key for the provided `path` if it can be created, otherwise `nil`
21-
*/
9+
/// Derive an ``HDNode`` based on the provided path. The function will throw ``BIP44Error.warning``
10+
/// if it was invoked with `throwOnWarning` equal to `true` and the root key doesn't have a previous child
11+
/// with at least one transaction. If it is invoked with `throwOnWarning` equal to `false` the child node will
12+
/// be derived directly using the derive function of ``HDNode``. This function needs to query the blockchain
13+
/// history when `throwOnWarning` is `true`, so it can throw network errors.
14+
/// - Parameter path: valid BIP44 path.
15+
/// - Parameter throwOnWarning: `true` to use
16+
/// [Account Discovery](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery) standard,
17+
/// otherwise it will dervive the key using the derive function of ``HDNode``.
18+
/// - Throws: ``BIP44Error.warning`` if the child key shouldn't be used according to
19+
/// [Account Discovery](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#account-discovery) standard.
20+
/// - Returns: an ``HDNode`` child key for the provided `path` if it can be created, otherwise `nil`
2221
func derive(path: String, throwOnWarning: Bool, transactionChecker: TransactionChecker) async throws -> HDNode?
2322
}
2423

@@ -35,12 +34,10 @@ public enum BIP44Error: LocalizedError, Equatable {
3534
}
3635

3736
public protocol TransactionChecker {
38-
/**
39-
It verifies if the provided address has at least one transaction
40-
- Parameter address: to be queried
41-
- Throws: any error related to query the blockchain provider
42-
- Returns: `true` if the address has at least one transaction, `false` otherwise
43-
*/
37+
/// It verifies if the provided address has at least one transaction
38+
/// - Parameter ethereumAddress: to be queried
39+
/// - Throws: any error related to query the blockchain provider
40+
/// - Returns: `true` if the address has at least one transaction, `false` otherwise
4441
func hasTransactions(ethereumAddress: EthereumAddress) async throws -> Bool
4542
}
4643

@@ -104,12 +101,11 @@ extension String {
104101
return account
105102
}
106103

107-
/**
108-
Transforms a bip44 path into a new one changing account & index. The resulting one will have the change value equal to `0` to represent the external chain. The format will be `m/44'/coin_type'/account'/change/address_index`
109-
- Parameter account: the new account to use
110-
- Parameter addressIndex: the new addressIndex to use
111-
- Returns: a valid bip44 path with the provided account, addressIndex and external change or `nil` otherwise
112-
*/
104+
/// Transforms a bip44 path into a new one changing account & index. The resulting one will have the change value equal to `0` to
105+
/// represent the external chain. The format will be `m/44'/coin_type'/account'/change/address_index`
106+
/// - Parameter account: the new account to use
107+
/// - Parameter addressIndex: the new addressIndex to use
108+
/// - Returns: a valid bip44 path with the provided account, addressIndex and external change or `nil` otherwise
113109
func newPath(account: Int, addressIndex: Int) -> String? {
114110
guard isBip44Path else {
115111
return nil

Sources/Web3Core/Transaction/EventfilterParameters.swift

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,44 +56,42 @@ extension EventFilterParameters {
5656
}
5757

5858
extension EventFilterParameters {
59-
/**
60-
This enum covers the optional nested Arrays
61-
62-
``EventFilterParameters`` include ``topic`` property with is array of optional values,
63-
and where `nil` value is a thing, and should be kept in server request.
64-
65-
This is not a trivial case for swift lang or any other stricktly typed lang.
66-
67-
So to make this possible ``Topic`` enum is provided.
68-
69-
It handle two cases: ``.string(String?)`` and ``.strings([Topic?]?)``,
70-
where former should be used to assign first demention value,
71-
and the latter to assign second dimension value into ``EventFilterParameters.topics`` property.
72-
73-
So to encode as a parameter follow JSON array:
74-
```JSON
75-
[
76-
"0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
77-
null,
78-
[
79-
"0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
80-
"0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"
81-
]
82-
]
83-
```
84-
85-
you have to pass to the ``topics`` property follow swift array:
86-
```swift
87-
let topics: [Topic?] = [
88-
.string("0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
89-
.string(nil),
90-
.strings([
91-
.string("0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
92-
.string("0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"),
93-
])
94-
]
95-
```
96-
*/
59+
/// This enum covers the optional nested Arrays
60+
///
61+
/// ``EventFilterParameters`` include ``topic`` property with is array of optional values,
62+
/// and where `nil` value is a thing, and should be kept in server request.
63+
///
64+
/// This is not a trivial case for swift lang or any other stricktly typed lang.
65+
///
66+
/// So to make this possible ``Topic`` enum is provided.
67+
///
68+
/// It handle two cases: ``.string(String?)`` and ``.strings([Topic?]?)``,
69+
/// where former should be used to assign first demention value,
70+
/// and the latter to assign second dimension value into ``EventFilterParameters.topics`` property.
71+
///
72+
/// So to encode as a parameter follow JSON array:
73+
/// ```JSON
74+
/// [
75+
/// "0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
76+
/// null,
77+
/// [
78+
/// "0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
79+
/// "0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"
80+
/// ]
81+
/// ]
82+
/// ```
83+
///
84+
/// you have to pass to the ``topics`` property follow swift array:
85+
/// ```swift
86+
/// let topics: [Topic?] = [
87+
/// .string("0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
88+
/// .string(nil),
89+
/// .strings([
90+
/// .string("0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"),
91+
/// .string("0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"),
92+
/// ])
93+
/// ]
94+
/// ```
9795
public enum Topic: Encodable {
9896
case string(String?)
9997
case strings([Topic?]?)

0 commit comments

Comments
 (0)