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

- ABIDecoding getting data slice in followTheData changed to using start data index #802

Merged
merged 5 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
10 changes: 5 additions & 5 deletions Sources/Web3Core/Contract/ContractProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ extension ContractProtocol {

func decodeInputData(_ data: Data) -> [String: Any]? {
guard data.count >= 4 else { return nil }
let methodId = data[0..<4].toHexString()
let data = data[4...]
let methodId = data[data.indices.startIndex ..< data.indices.startIndex + 4].toHexString()
let data = data[(data.indices.startIndex + 4)...]
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
return decodeInputData(methodId, data: data)
}
}
Expand Down Expand Up @@ -333,14 +333,14 @@ extension DefaultContractProtocol {

public func decodeInputData(_ data: Data) -> [String: Any]? {
guard data.count % 32 == 4 else { return nil }
let methodSignature = data[0..<4].toHexString().addHexPrefix().lowercased()
let methodSignature = data[data.indices.startIndex ..< data.indices.startIndex + 4].toHexString().addHexPrefix().lowercased()

guard let function = methods[methodSignature]?.first else { return nil }
return function.decodeInputData(Data(data[4 ..< data.count]))
return function.decodeInputData(Data(data[data.indices.startIndex + 4 ..< data.indices.startIndex + data.count]))
}

public func getFunctionCalled(_ data: Data) -> ABI.Element.Function? {
guard data.count >= 4 else { return nil }
return methods[data[0..<4].toHexString().addHexPrefix()]?.first
return methods[data[data.indices.startIndex ..< data.indices.startIndex + 4].toHexString().addHexPrefix()]?.first
}
}
4 changes: 2 additions & 2 deletions Sources/Web3Core/EthereumABI/ABIDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ extension ABIDecoder {
fileprivate static func followTheData(type: ABI.Element.ParameterType, data: Data, pointer: UInt64 = 0) -> (elementEncoding: Data?, nextElementPointer: UInt64?) {
if type.isStatic {
guard data.count >= pointer + type.memoryUsage else {return (nil, nil)}
let elementItself = data[pointer ..< pointer + type.memoryUsage]
let elementItself = data[data.indices.startIndex + Int(pointer) ..< data.indices.startIndex + Int(pointer + type.memoryUsage)]
let nextElement = pointer + type.memoryUsage
return (Data(elementItself), nextElement)
} else {
guard data.count >= pointer + type.memoryUsage else {return (nil, nil)}
let dataSlice = data[pointer ..< pointer + type.memoryUsage]
let dataSlice = data[data.indices.startIndex + Int(pointer) ..< data.indices.startIndex + Int(pointer + type.memoryUsage)]
let bn = BigUInt(dataSlice)
if bn > UInt64.max || bn >= data.count {
// there are ERC20 contracts that use bytes32 instead of string. Let's be optimistic and return some data
Expand Down
10 changes: 5 additions & 5 deletions Sources/Web3Core/EthereumABI/ABIElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ extension ABI.Element.Function {
/// 4) `messageLength` is used to determine where message bytes end to decode string correctly.
/// 5) The rest of the `data` must be 0 bytes or empty.
if data.bytes.count >= 100,
Data(data[0..<4]) == Data.fromHex("08C379A0"),
BigInt(data[4..<36]) == 32,
let messageLength = Int(Data(data[36..<68]).toHexString(), radix: 16),
Data(data[data.indices.startIndex ..< data.indices.startIndex + 4]) == Data.fromHex("08C379A0"),
BigInt(data[data.indices.startIndex + 4 ..< data.indices.startIndex + 36]) == 32,
let messageLength = Int(Data(data[data.indices.startIndex + 36 ..< data.indices.startIndex + 68]).toHexString(), radix: 16),
let message = String(bytes: data.bytes[68..<(68+messageLength)], encoding: .utf8),
(68+messageLength == data.count || data.bytes[68+messageLength..<data.count].reduce(0) { $0 + $1 } == 0) {
return ["_success": false,
Expand All @@ -410,11 +410,11 @@ extension ABI.Element.Function {

if data.count >= 4,
let errors = errors,
let customError = errors[data[0..<4].toHexString().stripHexPrefix()] {
let customError = errors[data[data.indices.startIndex + 0 ..< data.indices.startIndex + 4].toHexString().stripHexPrefix()] {
JeneaVranceanu marked this conversation as resolved.
Show resolved Hide resolved
var errorResponse: [String: Any] = ["_success": false, "_abortedByRevertOrRequire": true, "_error": customError.errorDeclaration]

if (data.count > 32 && !customError.inputs.isEmpty),
let decodedInputs = ABIDecoder.decode(types: customError.inputs, data: Data(data[4..<data.count])) {
let decodedInputs = ABIDecoder.decode(types: customError.inputs, data: Data(data[data.indices.startIndex + 4 ..< data.indices.startIndex + data.count])) {
for idx in decodedInputs.indices {
errorResponse["\(idx)"] = decodedInputs[idx]
if !customError.inputs[idx].name.isEmpty {
Expand Down