Skip to content

Commit 23ef94a

Browse files
Add Data type support by conforming LiteralInitiableFromString protocol.
- Rename init of `DecodableFromHex` protocol to make it crossing with `LiteralInitiableFromString`.
1 parent 41ac98f commit 23ef94a

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

Sources/web3swift/API/HexDecodableProtocols.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import BigInt
9+
import Foundation
910

1011
public protocol APIResultType: Decodable { }
1112

@@ -37,7 +38,6 @@ extension LiteralInitiableFromString where Self: IntegerInitableWithRadix {
3738
}
3839
}
3940

40-
4141
extension Int: LiteralInitiableFromString { }
4242

4343
extension UInt: LiteralInitiableFromString { }
@@ -46,6 +46,8 @@ extension BigInt: LiteralInitiableFromString { }
4646

4747
extension BigUInt: LiteralInitiableFromString { }
4848

49+
extension Data: LiteralInitiableFromString { }
50+
4951
public protocol IntegerInitableWithRadix {
5052
init?<S: StringProtocol>(_ text: S, radix: Int)
5153
}

Sources/web3swift/Convenience/Decodable+Extensions.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension KeyedDecodingContainer {
3333
/// - throws: `Web3Error.dataError` if value associated with key are unable to be initialized as `DecodableFromHex`.
3434
public func decodeHex<T: DecodableFromHex>(_ type: T.Type, forKey: KeyedDecodingContainer<K>.Key) throws -> T {
3535
let hexString = try self.decode(String.self, forKey: forKey)
36-
guard let value = T(fromHex: hexString) else { throw Web3Error.dataError }
36+
guard let value = T(from: hexString) else { throw Web3Error.dataError }
3737
return value
3838
}
3939

@@ -92,7 +92,7 @@ public extension UnkeyedDecodingContainer {
9292
var array: [T] = []
9393
while !isAtEnd {
9494
let hexString = try decode(String.self)
95-
guard let item = T(fromHex: hexString) else { continue }
95+
guard let item = T(from: hexString) else { continue }
9696
array.append(item)
9797
}
9898
return array
@@ -119,31 +119,31 @@ public extension UnkeyedDecodingContainer {
119119
}
120120

121121
public protocol DecodableFromHex: Decodable {
122-
init?(fromHex hexString: String)
122+
init?(from hexString: String)
123123
}
124124

125125
extension Data: DecodableFromHex {
126-
public init?(fromHex hexString: String) {
126+
public init?(from hexString: String) {
127127
self.init()
128128
guard let tmp = Self.fromHex(hexString) else { return nil }
129129
self = tmp
130130
}
131131
}
132132

133133
extension UInt: DecodableFromHex {
134-
public init?(fromHex hexString: String) {
134+
public init?(from hexString: String) {
135135
self.init(hexString.stripHexPrefix(), radix: 16)
136136
}
137137
}
138138

139139
extension BigUInt: DecodableFromHex {
140-
public init?(fromHex hexString: String) {
140+
public init?(from hexString: String) {
141141
self.init(hexString.stripHexPrefix(), radix: 16)
142142
}
143143
}
144144

145145
extension Date: DecodableFromHex {
146-
public init?(fromHex hexString: String) {
146+
public init?(from hexString: String) {
147147
self.init()
148148
let stripedHexString = hexString.stripHexPrefix()
149149
guard let timestampInt = UInt(stripedHexString, radix: 16) else { return nil }
@@ -152,7 +152,7 @@ extension Date: DecodableFromHex {
152152
}
153153

154154
extension EthereumAddress: DecodableFromHex {
155-
public init?(fromHex hexString: String) {
155+
public init?(from hexString: String) {
156156
self.init(hexString, ignoreChecksum: true)
157157
}
158158
}

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension web3.Eth {
1414
// FIXME: Add appropriate error
1515
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError}
1616
let request: APIRequest = .call(transactionParameters, transactionOptions?.callOnBlock ?? .latest)
17-
let response: APIResponse<Data> = try await APIRequest.sendRequest(with: self.provider, for: request)
17+
let response: APIResponse<Data> = try await APIRequest.sendRequest(with: self.provider, for: request)
1818
return response.result
1919
}
2020
}

Sources/web3swift/Transaction/EIP2930Envelope.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public struct AccessListEntry: CustomStringConvertible, Decodable {
333333
self.storageKeys = []
334334
if let keyStrings = try? container.decode([String].self, forKey: .storageKeys) {
335335
for keyString in keyStrings {
336-
guard let number = BigUInt(fromHex: keyString) else { throw Web3Error.dataError }
336+
guard let number = BigUInt(from: keyString) else { throw Web3Error.dataError }
337337
self.storageKeys.append(number)
338338
}
339339
}

Tests/web3swiftTests/localTests/EIP1559BlockTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import web3swift
77
class EIP1559BlockTests: LocalTestCase {
88
let uselessBlockPart = (
99
number: BigUInt(12_965_000),
10-
hash: Data(fromHex: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":
11-
parentHash: Data(fromHex: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88")!, // "parentHash":
12-
nonce: Data(fromHex: "0xfb6e1a62d119228b"), // "nonce":
13-
sha3Uncles: Data(fromHex: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "sha3Uncles":
14-
receiptsRoot: Data(fromHex: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "receiptsRoot":
15-
logsBloom: EthereumBloomFilter(Data(fromHex: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!), // "logsBloom":
16-
transactionsRoot: Data(fromHex: "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee")!, // "transactionsRoot":
17-
stateRoot: Data(fromHex: "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb")!, // "stateRoot":
18-
miner: EthereumAddress( Data(fromHex: "0x8888f1f195afa192cfee860698584c030f4c9db1")!)!, // "miner":
10+
hash: Data(from: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash":
11+
parentHash: Data(from: "0x2302e1c0b972d00932deb5dab9eb2982f570597d9d42504c05d9c2147eaf9c88")!, // "parentHash":
12+
nonce: Data(from: "0xfb6e1a62d119228b"), // "nonce":
13+
sha3Uncles: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "sha3Uncles":
14+
receiptsRoot: Data(from: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")!, // "receiptsRoot":
15+
logsBloom: EthereumBloomFilter(Data(from: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!), // "logsBloom":
16+
transactionsRoot: Data(from: "0x3a1b03875115b79539e5bd33fb00d8f7b7cd61929d5a3c574f507b8acf415bee")!, // "transactionsRoot":
17+
stateRoot: Data(from: "0xf1133199d44695dfa8fd1bcfe424d82854b5cebef75bddd7e40ea94cda515bcb")!, // "stateRoot":
18+
miner: EthereumAddress( Data(from: "0x8888f1f195afa192cfee860698584c030f4c9db1")!)!, // "miner":
1919
difficulty: BigUInt(21345678965432), // "difficulty":
2020
totalDifficulty: BigUInt(324567845321), // "totalDifficulty":
2121
size: BigUInt(616), // "size":
22-
extraData: Data(fromHex: "0x")!, // extraData":
22+
extraData: Data(from: "0x")!, // extraData":
2323
gasLimit: BigUInt(3141592), // "gasLimit":
2424
gasUsed: BigUInt(21662), // "gasUsed":
2525
timestamp: Date(), // "timestamp":

0 commit comments

Comments
 (0)