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

fix: encoding ints/uints #854

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions Sources/Web3Core/EthereumABI/ABIEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ public struct ABIEncoder {
/// - encoding has failed (e.g. type mismatch).
public static func encodeSingleType(type: ABI.Element.ParameterType, value: Any) -> Data? {
switch type {
case .uint:
case .uint(let bits):
let biguint = convertToBigUInt(value)
return biguint == nil ? nil : biguint!.abiEncode(bits: 256)
case .int:
return biguint == nil ? nil : biguint!.abiEncode(bits: bits)
case .int(let bits):
let bigint = convertToBigInt(value)
return bigint == nil ? nil : bigint!.abiEncode(bits: 256)
return bigint == nil ? nil : bigint!.abiEncode(bits: bits)
case .address:
if let string = value as? String {
guard let address = EthereumAddress(string) else { return nil }
Expand Down
17 changes: 8 additions & 9 deletions Tests/web3swiftTests/localTests/ABIEncoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import BigInt
class ABIEncoderTest: XCTestCase {

func testEncodeInt() {
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: -10 as AnyObject)?.toHexString(), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff6")
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: 10 as AnyObject)?.toHexString(), "000000000000000000000000000000000000000000000000000000000000000a")
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: -10 as AnyObject)?.toHexString(), "fffffff6")
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .int(bits: 32), value: 10 as AnyObject)?.toHexString(), "0000000a")
}

func testEncodeUInt() {
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .uint(bits: 32), value: -10 as AnyObject), nil)
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .uint(bits: 32), value: 10 as AnyObject)?.toHexString(), "000000000000000000000000000000000000000000000000000000000000000a")
XCTAssertEqual(ABIEncoder.encodeSingleType(type: .uint(bits: 32), value: 10 as AnyObject)?.toHexString(), "0000000a")
}

func testSoliditySha3() throws {
Expand Down Expand Up @@ -202,22 +202,21 @@ class ABIEncoderTest: XCTestCase {
XCTAssertEqual(hexData, "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000")
hexData = ABIEncoder.encode(types: [.array(type: .uint(bits: 8), length: 0)], values: [[1, 2, 3, 4]])?.toHexString()
XCTAssertEqual(hexData?[0..<64], "0000000000000000000000000000000000000000000000000000000000000020")
XCTAssertEqual(hexData, "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004")

XCTAssertEqual(hexData, "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000401020304")
// This one shouldn't have data offset
hexData = ABIEncoder.encode(types: [.array(type: .uint(bits: 8), length: 4)], values: [[1, 2, 3, 4]])?.toHexString()
// First 32 bytes are the first value from the array
XCTAssertEqual(hexData?[0..<64], "0000000000000000000000000000000000000000000000000000000000000001")
XCTAssertEqual(hexData, "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004")
// XCTAssertEqual(hexData?[0..<32], "0000000000000000000000000000000000000000000000000000000000000001")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure about this line. @JeneaVranceanu @yaroslavyaroslav

Is it actually needed or could it be deleted? Because of the bug fix of uint bits this hex string is now signficantly shorter.

XCTAssertEqual(hexData, "01020304")

let types: [ABI.Element.ParameterType] = [.uint(bits: 8),
.bool,
.array(type: .uint(bits: 8), length: 0),
.bytes(length: 2)]
let values: [Any] = [10, false, [1, 2, 3, 4], Data(count: 2)]
hexData = ABIEncoder.encode(types: types, values: values)?.toHexString()
XCTAssertEqual(hexData?[128..<192], "0000000000000000000000000000000000000000000000000000000000000080")
XCTAssertEqual(hexData, "000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004")
XCTAssertEqual(hexData?[128..<192], "6100000000000000000000000000000000000000000000000000000000000000")
XCTAssertEqual(hexData, "0a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000401020304")
}

/// Test for the expected output when encoding dynamic types.
Expand Down
Loading