Skip to content

Commit f001cec

Browse files
fix: Data to HEX string
1 parent 0a76a4a commit f001cec

14 files changed

+53
-42
lines changed

Sources/Core/EthereumNetwork/Request/APIRequest+Methods.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension APIRequest {
3636

3737
/// This bit of code is purposed to work with literal types that comes in ``Response`` in hexString type.
3838
/// Currently it's just `Data` and any kind of Integers `(U)Int`, `Big(U)Int`.
39-
if Result.self == Data.self || Result.self == UInt.self || Result.self == Int.self || Result.self == BigInt.self || Result.self == BigUInt.self {
39+
if Result.self == Data.self || Result.self == UInt.self || Result.self == Int.self || Result.self == BigInt.self || Result.self == BigUInt.self || Result.self == String.self {
4040
guard let LiteralType = Result.self as? LiteralInitiableFromString.Type else { throw Web3Error.typeError }
4141
guard let responseAsString = try? JSONDecoder().decode(APIResponse<String>.self, from: data) else { throw Web3Error.dataError }
4242
guard let literalValue = LiteralType.init(from: responseAsString.result) else { throw Web3Error.dataError }

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Core
1010

1111
extension Web3.Eth {
1212
public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block {
13-
guard let hexString = String(data: hash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
1413
let requestCall: APIRequest = .getBlockByHash(hash.toHexString().addHexPrefix(), fullTransactions)
1514
return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result
1615
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import BigInt
88
import Core
99

1010
extension Web3.Eth {
11-
public func transactionDetails(_ txhash: Data) async throws -> TransactionDetails {
12-
guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
13-
let requestCall: APIRequest = .getTransactionByHash(hexString)
14-
return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result
11+
public func transactionDetails(_ txHash: Data) async throws -> TransactionDetails {
12+
try await transactionDetails(txHash.toHexString().addHexPrefix())
13+
}
14+
15+
public func transactionDetails(_ txHash: String) async throws -> TransactionDetails {
16+
try await APIRequest.sendRequest(with: self.provider, for: .getTransactionByHash(txHash)).result
1517
}
1618
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import BigInt
88
import Core
99

1010
extension Web3.Eth {
11-
public func transactionReceipt(_ txhash: Data) async throws -> TransactionReceipt {
12-
guard let hexString = String(data: txhash, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
13-
let requestCall: APIRequest = .getTransactionReceipt(hexString)
14-
return try await APIRequest.sendRequest(with: self.provider, for: requestCall).result
11+
public func transactionReceipt(_ txHash: Data) async throws -> TransactionReceipt {
12+
try await transactionReceipt(txHash.toHexString().addHexPrefix())
13+
}
14+
15+
public func transactionReceipt(_ txHash: String) async throws -> TransactionReceipt {
16+
try await APIRequest.sendRequest(with: self.provider,
17+
for: .getTransactionReceipt(txHash)).result
1518
}
1619
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import Core
99

1010
extension Web3.Eth {
1111
public func send(raw data: Data) async throws -> TransactionSendingResult {
12-
guard let hexString = String(data: data, encoding: .utf8)?.addHexPrefix() else { throw Web3Error.dataError }
13-
let request: APIRequest = .sendRawTransaction(hexString)
12+
let request: APIRequest = .sendRawTransaction(data.toHexString().addHexPrefix())
1413
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: request)
1514
return try TransactionSendingResult(data: data, hash: response.result)
1615
}

Sources/web3swift/Web3/Web3+Contract.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extension Web3 {
105105
///
106106
/// Returns a "Transaction intermediate" object.
107107
public func createWriteOperation(_ method: String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data()) -> WriteOperation? {
108-
guard var data = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil}
108+
guard let data = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil}
109109
transaction.data = data
110110
if let network = self.web3.provider.network {
111111
transaction.chainID = network.chainID

Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class AdvancedABIv2Tests: LocalTestCase {
2626
deployTx.transaction.gasLimitPolicy = .manual(3000000)
2727
// MARK: Sending Data flow
2828
let result = try await deployTx.writeToChain(password: "web3swift")
29-
let txHash = result.hash
29+
let txHash = result.hash.stripHexPrefix()
3030

3131
Thread.sleep(forTimeInterval: 1.0)
3232

33-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
33+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
3434
print(receipt)
3535

3636
switch receipt.status {
@@ -62,11 +62,11 @@ class AdvancedABIv2Tests: LocalTestCase {
6262
deployTx.transaction.from = allAddresses[0]
6363
deployTx.transaction.gasLimitPolicy = .manual(3000000)
6464
let result = try await deployTx.writeToChain(password: "web3swift")
65-
let txHash = result.hash
65+
let txHash = result.hash.stripHexPrefix()
6666

6767
Thread.sleep(forTimeInterval: 1.0)
6868

69-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
69+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
7070
print(receipt)
7171

7272
switch receipt.status {
@@ -98,11 +98,11 @@ class AdvancedABIv2Tests: LocalTestCase {
9898
deployTx.transaction.from = allAddresses[0]
9999
deployTx.transaction.gasLimitPolicy = .manual(3000000)
100100
let result = try await deployTx.writeToChain(password: "web3swift")
101-
let txHash = result.hash
101+
let txHash = result.hash.stripHexPrefix()
102102

103103
Thread.sleep(forTimeInterval: 1.0)
104104

105-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
105+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
106106
print(receipt)
107107

108108
switch receipt.status {
@@ -133,11 +133,11 @@ class AdvancedABIv2Tests: LocalTestCase {
133133
deployTx.transaction.from = allAddresses[0]
134134
deployTx.transaction.gasLimitPolicy = .manual(3000000)
135135
let result = try await deployTx.writeToChain(password: "web3swift")
136-
let txHash = result.hash
136+
let txHash = result.hash.stripHexPrefix()
137137

138138
Thread.sleep(forTimeInterval: 1.0)
139139

140-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
140+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
141141
print(receipt)
142142

143143
switch receipt.status {
@@ -169,11 +169,11 @@ class AdvancedABIv2Tests: LocalTestCase {
169169
deployTx.transaction.from = allAddresses[0]
170170
deployTx.transaction.gasLimitPolicy = .manual(3000000)
171171
let result = try await deployTx.writeToChain(password: "web3swift")
172-
let txHash = result.hash
172+
let txHash = result.hash.stripHexPrefix()
173173

174174
Thread.sleep(forTimeInterval: 1.0)
175175

176-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
176+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
177177
print(receipt)
178178

179179
switch receipt.status {

Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class BasicLocalNodeTests: LocalTestCase {
2828
deployTx.transaction.gasLimitPolicy = .manual(3000000)
2929

3030
let result = try await deployTx.writeToChain(password: "web3swift")
31-
let txHash = result.hash
31+
let txHash = result.hash.stripHexPrefix()
3232

3333
Thread.sleep(forTimeInterval: 1.0)
3434

35-
let receipt = try await web3.eth.transactionReceipt(txHash.stripHexPrefix().data(using: .utf8)!)
35+
let receipt = try await web3.eth.transactionReceipt(Data.fromHex(txHash)!)
3636
print(receipt)
3737

3838
switch receipt.status {
@@ -42,12 +42,13 @@ class BasicLocalNodeTests: LocalTestCase {
4242
break
4343
}
4444

45-
let details = try await web3.eth.transactionDetails(txHash.stripHexPrefix().data(using: .utf8)!)
45+
let details = try await web3.eth.transactionDetails(Data.fromHex(txHash)!)
4646
print(details)
4747
}
4848

4949
func testEthSendExampleWithRemoteSigning() async throws {
5050
let web3 = try await Web3.new(LocalTestCase.url)
51+
web3.addKeystoreManager(LocalTestCase.keyStoreManager)
5152
let allAddresses = try await web3.eth.ownedAccounts()
5253
let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
5354
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)!
@@ -64,12 +65,12 @@ class BasicLocalNodeTests: LocalTestCase {
6465
print("Balance before to: " + balanceBeforeTo.description)
6566
print("Balance before from: " + balanceBeforeFrom.description)
6667

67-
let result = try await sendTx.writeToChain(password: "web3swift")
68+
let result = try! await sendTx.writeToChain(password: "web3swift")
6869
let txHash = result.hash
6970

7071
Thread.sleep(forTimeInterval: 1.0)
7172

72-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
73+
let receipt = try await web3.eth.transactionReceipt(txHash)
7374
print(receipt)
7475

7576
switch receipt.status {
@@ -79,7 +80,7 @@ class BasicLocalNodeTests: LocalTestCase {
7980
break
8081
}
8182

82-
let details = try await web3.eth.transactionDetails(txHash.data(using: .utf8)!)
83+
let details = try await web3.eth.transactionDetails(txHash)
8384
print(details)
8485

8586

Tests/web3swiftTests/localTests/LocalTestCase.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import web3swift
1010
class LocalTestCase: XCTestCase {
1111

1212
static let url = URL(string: "http://127.0.0.1:8545")!
13+
static let keyStoreManager: KeystoreManager = KeystoreManager([try! EthereumKeystoreV3(password: "web3swift")!])
1314

1415
override func setUp() async throws {
1516
let web3 = try! await Web3.new(LocalTestCase.url)

Tests/web3swiftTests/localTests/PersonalSignatureTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class PersonalSignatureTests: XCTestCase {
4343
deployTx.transaction.from = allAddresses[0]
4444
deployTx.transaction.gasLimitPolicy = .manual(3000000)
4545
let deployResult = try await deployTx.writeToChain(password: "web3swift")
46-
let txHash = deployResult.hash
46+
let txHash = Data.fromHex(deployResult.hash.stripHexPrefix())!
4747

4848
Thread.sleep(forTimeInterval: 1.0)
4949

50-
let receipt = try await web3.eth.transactionReceipt(txHash.data(using: .utf8)!)
50+
let receipt = try await web3.eth.transactionReceipt(txHash)
5151
print(receipt)
5252

5353
switch receipt.status {
@@ -76,13 +76,13 @@ class PersonalSignatureTests: XCTestCase {
7676
var tx = contract.createReadOperation("hashPersonalMessage", parameters: [message as AnyObject])
7777
tx?.transaction.from = expectedAddress
7878
var result = try await tx!.callContractMethod()
79-
guard let hash = result["hash"]! as? Data else {return XCTFail()}
79+
guard let hash = result["hash"]! as? Data else { return XCTFail() }
8080
XCTAssert(Utilities.hashPersonalMessage(message.data(using: .utf8)!)! == hash)
8181

8282
tx = contract.createReadOperation("recoverSigner", parameters: [message, unmarshalledSignature.v, Data(unmarshalledSignature.r), Data(unmarshalledSignature.s)] as [AnyObject])
8383
tx?.transaction.from = expectedAddress
8484
result = try await tx!.callContractMethod()
85-
guard let signer = result["signer"]! as? EthereumAddress else {return XCTFail()}
85+
guard let signer = result["signer"]! as? EthereumAddress else { return XCTFail() }
8686
XCTAssert(signer == expectedAddress)
8787
}
8888

0 commit comments

Comments
 (0)