Skip to content

Commit 0016aee

Browse files
Merge pull request #498 from mloit/feature/bug-fix-rollup
2 parents 239887d + 11517aa commit 0016aee

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

Sources/web3swift/EthereumABI/ABIDecoding.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension ABIDecoder {
2323
var toReturn = [AnyObject]()
2424
var consumed: UInt64 = 0
2525
for i in 0 ..< types.count {
26-
let (v, c) = decodeSignleType(type: types[i], data: data, pointer: consumed)
26+
let (v, c) = decodeSingleType(type: types[i], data: data, pointer: consumed)
2727
guard let valueUnwrapped = v, let consumedUnwrapped = c else {return nil}
2828
toReturn.append(valueUnwrapped)
2929
consumed = consumed + consumedUnwrapped
@@ -32,7 +32,7 @@ extension ABIDecoder {
3232
return toReturn
3333
}
3434

35-
public static func decodeSignleType(type: ABI.Element.ParameterType, data: Data, pointer: UInt64 = 0) -> (value: AnyObject?, bytesConsumed: UInt64?) {
35+
public static func decodeSingleType(type: ABI.Element.ParameterType, data: Data, pointer: UInt64 = 0) -> (value: AnyObject?, bytesConsumed: UInt64?) {
3636
let (elData, nextPtr) = followTheData(type: type, data: data, pointer: pointer)
3737
guard let elementItself = elData, let nextElementPointer = nextPtr else {
3838
return (nil, nil)
@@ -117,7 +117,7 @@ extension ABIDecoder {
117117
var subpointer: UInt64 = 32;
118118
var toReturn = [AnyObject]()
119119
for _ in 0 ..< length {
120-
let (v, c) = decodeSignleType(type: subType, data: elementItself, pointer: subpointer)
120+
let (v, c) = decodeSingleType(type: subType, data: elementItself, pointer: subpointer)
121121
guard let valueUnwrapped = v, let consumedUnwrapped = c else {break}
122122
toReturn.append(valueUnwrapped)
123123
subpointer = subpointer + consumedUnwrapped
@@ -134,7 +134,7 @@ extension ABIDecoder {
134134
var toReturn = [AnyObject]()
135135
// print("Dynamic array sub element itself: \n" + dataSlice.toHexString())
136136
for _ in 0 ..< length {
137-
let (v, c) = decodeSignleType(type: subType, data: dataSlice, pointer: subpointer)
137+
let (v, c) = decodeSingleType(type: subType, data: dataSlice, pointer: subpointer)
138138
guard let valueUnwrapped = v, let consumedUnwrapped = c else {break}
139139
toReturn.append(valueUnwrapped)
140140
if (subType.isStatic) {
@@ -152,7 +152,7 @@ extension ABIDecoder {
152152
var toReturn = [AnyObject]()
153153
var consumed:UInt64 = 0
154154
for _ in 0 ..< length {
155-
let (v, c) = decodeSignleType(type: subType, data: elementItself, pointer: consumed)
155+
let (v, c) = decodeSingleType(type: subType, data: elementItself, pointer: consumed)
156156
guard let valueUnwrapped = v, let consumedUnwrapped = c else {return (nil, nil)}
157157
toReturn.append(valueUnwrapped)
158158
consumed = consumed + consumedUnwrapped
@@ -170,10 +170,28 @@ extension ABIDecoder {
170170
var toReturn = [AnyObject]()
171171
var consumed:UInt64 = 0
172172
for i in 0 ..< subTypes.count {
173-
let (v, c) = decodeSignleType(type: subTypes[i], data: elementItself, pointer: consumed)
173+
let (v, c) = decodeSingleType(type: subTypes[i], data: elementItself, pointer: consumed)
174174
guard let valueUnwrapped = v, let consumedUnwrapped = c else {return (nil, nil)}
175175
toReturn.append(valueUnwrapped)
176-
consumed = consumed + consumedUnwrapped
176+
/*
177+
When decoding a tuple that is not static or an array with a subtype that is not static, the second value in the tuple returned by decodeSignleType is a pointer to the next element, NOT the length of the consumed element. So when decoding such an element, consumed should be set to consumedUnwrapped, NOT incremented by consumedUnwrapped.
178+
*/
179+
switch subTypes[i] {
180+
case .array(type: let subType, length: _):
181+
if !subType.isStatic {
182+
consumed = consumedUnwrapped
183+
} else {
184+
consumed = consumed + consumedUnwrapped
185+
}
186+
case .tuple(types: _):
187+
if !subTypes[i].isStatic {
188+
consumed = consumedUnwrapped
189+
} else {
190+
consumed = consumed + consumedUnwrapped
191+
}
192+
default:
193+
consumed = consumed + consumedUnwrapped
194+
}
177195
}
178196
// print("Tuple element is: \n" + String(describing: toReturn))
179197
if type.isStatic {
@@ -253,11 +271,11 @@ extension ABIDecoder {
253271
let data = logs[i+1]
254272
let input = indexedInputs[i]
255273
if !input.type.isStatic || input.type.isArray || input.type.memoryUsage != 32 {
256-
let (v, _) = ABIDecoder.decodeSignleType(type: .bytes(length: 32), data: data)
274+
let (v, _) = ABIDecoder.decodeSingleType(type: .bytes(length: 32), data: data)
257275
guard let valueUnwrapped = v else {return nil}
258276
indexedValues.append(valueUnwrapped)
259277
} else {
260-
let (v, _) = ABIDecoder.decodeSignleType(type: input.type, data: data)
278+
let (v, _) = ABIDecoder.decodeSingleType(type: input.type, data: data)
261279
guard let valueUnwrapped = v else {return nil}
262280
indexedValues.append(valueUnwrapped)
263281
}

Sources/web3swift/Transaction/TransactionSigner.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public struct Web3Signer {
5151
guard let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: serializedSignature) else {
5252
return false
5353
}
54-
let originalPublicKey = SECP256K1.privateToPublic(privateKey: privateKey)
54+
guard let originalPublicKey = SECP256K1.privateToPublic(privateKey: privateKey) else { return false }
5555
var d = BigUInt(0)
5656
if unmarshalledSignature.v >= 0 && unmarshalledSignature.v <= 3 {
5757
d = BigUInt(35)
@@ -64,7 +64,7 @@ public struct Web3Signer {
6464
transaction.r = BigUInt(Data(unmarshalledSignature.r))
6565
transaction.s = BigUInt(Data(unmarshalledSignature.s))
6666
let recoveredPublicKey = transaction.recoverPublicKey()
67-
if (!(originalPublicKey!.constantTimeComparisonTo(recoveredPublicKey))) {
67+
if !(originalPublicKey.constantTimeComparisonTo(recoveredPublicKey)) {
6868
return false
6969
}
7070
return true
@@ -89,7 +89,7 @@ public struct Web3Signer {
8989
guard let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: serializedSignature) else {
9090
return false
9191
}
92-
let originalPublicKey = SECP256K1.privateToPublic(privateKey: privateKey)
92+
guard let originalPublicKey = SECP256K1.privateToPublic(privateKey: privateKey) else { return false }
9393
transaction.chainID = nil
9494
var d = BigUInt(0)
9595
var a = BigUInt(0)
@@ -104,7 +104,7 @@ public struct Web3Signer {
104104
transaction.r = BigUInt(Data(unmarshalledSignature.r))
105105
transaction.s = BigUInt(Data(unmarshalledSignature.s))
106106
let recoveredPublicKey = transaction.recoverPublicKey()
107-
if (!(originalPublicKey!.constantTimeComparisonTo(recoveredPublicKey))) {
107+
if !(originalPublicKey.constantTimeComparisonTo(recoveredPublicKey)) {
108108
return false
109109
}
110110
return true

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ extension TransactionOptions: Decodable {
5252

5353
public init(from decoder: Decoder) throws {
5454
let container = try decoder.container(keyedBy: CodingKeys.self)
55-
if let gasLimit = try decodeHexToBigUInt(container, key: .gas) {
55+
if let gasLimit = try decodeHexToBigUInt(container, key: .gas, allowOptional:true) {
5656
self.gasLimit = .manual(gasLimit)
5757
} else {
5858
self.gasLimit = .automatic
5959
}
6060

61-
if let gasPrice = try decodeHexToBigUInt(container, key: .gasPrice) {
61+
if let gasPrice = try decodeHexToBigUInt(container, key: .gasPrice, allowOptional:true) {
6262
self.gasPrice = .manual(gasPrice)
6363
} else {
6464
self.gasPrice = .automatic
@@ -85,13 +85,13 @@ extension TransactionOptions: Decodable {
8585
let value = try decodeHexToBigUInt(container, key: .value)
8686
self.value = value
8787

88-
if let nonce = try decodeHexToBigUInt(container, key: .nonce) {
88+
if let nonce = try decodeHexToBigUInt(container, key: .nonce, allowOptional:true) {
8989
self.nonce = .manual(nonce)
9090
} else {
9191
self.nonce = .pending
9292
}
9393

94-
if let callOnBlock = try decodeHexToBigUInt(container, key: .nonce) {
94+
if let callOnBlock = try decodeHexToBigUInt(container, key: .callOnBlock, allowOptional:true) {
9595
self.callOnBlock = .exactBlockNumber(callOnBlock)
9696
} else {
9797
self.callOnBlock = .pending

0 commit comments

Comments
 (0)