Skip to content

Commit 6eddd2c

Browse files
authored
Merge pull request #276 from hakumai-iida/detect_read_function_aborting_by_messaged_require_statement
[recreated] Detect read function aborting by messaged require statement
2 parents a4e4985 + aa70690 commit 6eddd2c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Sources/web3swift/EthereumABI/ABIElements.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,40 @@ extension ABI.Element {
175175
case .fallback(_):
176176
return nil
177177
case .function(let function):
178+
// the response size greater than equal 100 bytes, when read function aborted by "require" statement.
179+
// if "require" statement has no message argument, the response is empty (0 byte).
180+
if( data.bytes.count >= 100 ){
181+
let check00_31 = BigUInt( "08C379A000000000000000000000000000000000000000000000000000000000", radix:16 )!
182+
let check32_63 = BigUInt( "0000002000000000000000000000000000000000000000000000000000000000", radix:16 )!
183+
184+
// check data[00-31] and data[32-63]
185+
if check00_31 == BigUInt( data[0...31] ) && check32_63 == BigUInt( data[32...63] ) {
186+
// data.bytes[64-67] contains the length of require message
187+
let len = (Int(data.bytes[64])<<24) | (Int(data.bytes[65])<<16) | (Int(data.bytes[66])<<8) | Int(data.bytes[67])
188+
189+
let message = String( bytes:data.bytes[68..<(68+len)], encoding:.utf8 )!
190+
191+
print( "read function aborted by require statement: \(message)" )
192+
193+
var returnArray = [String:Any]()
194+
195+
// set infomation
196+
returnArray["_abortedByRequire"] = true
197+
returnArray["_errorMessageFromRequire"] = message
198+
199+
// set empty values
200+
for i in 0 ..< function.outputs.count {
201+
let name = "\(i)"
202+
returnArray[name] = function.outputs[i].type.emptyValue
203+
if function.outputs[i].name != "" {
204+
returnArray[function.outputs[i].name] = function.outputs[i].type.emptyValue
205+
}
206+
}
207+
208+
return returnArray
209+
}
210+
}
211+
// the "require" statement with no message argument will be caught here
178212
if (data.count == 0 && function.outputs.count == 1) {
179213
let name = "0"
180214
let value = function.outputs[0].type.emptyValue
@@ -198,6 +232,8 @@ extension ABI.Element {
198232
}
199233
i = i + 1
200234
}
235+
// set a flag to detect the request succeeded
236+
returnArray["_success"] = true
201237
return returnArray
202238
}
203239
}

0 commit comments

Comments
 (0)