Skip to content

Commit 051f94f

Browse files
committed
Provide basic tolerance to EIP-1559 data
Test for the envelope 'type' if 0 or not present we assume legacy and can continue, otherwise abort as it is 1559 [or future] and we don't know its encoding Added some basic garbage-in garbage out checks to prevent calculations from generating an overflow. This will mean what comes out is garbage, but at least it didn't crash.
1 parent 5b45521 commit 051f94f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Sources/web3swift/Transaction/EthereumTransaction.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct EthereumTransaction: CustomStringConvertible {
2929
get{
3030
if (self.r == BigUInt(0) && self.s == BigUInt(0)) {
3131
return self.v
32-
} else if (self.v == BigUInt(27) || self.v == BigUInt(28)) {
32+
} else if (self.v == BigUInt(27) || self.v == BigUInt(28) || self.v < BigUInt(35)) {
3333
return nil
3434
} else {
3535
return ((self.v - BigUInt(1)) / BigUInt(2)) - BigUInt(17)
@@ -125,11 +125,12 @@ public struct EthereumTransaction: CustomStringConvertible {
125125
} else if self.v >= 27 && self.v <= 30 {
126126
d = BigUInt(27)
127127
}
128-
if (self.chainID != nil && self.chainID != BigUInt(0)) {
128+
if (self.chainID != nil && self.chainID != BigUInt(0) && self.v >= (d + self.chainID! + self.chainID!)) {
129129
normalizedV = self.v - d - self.chainID! - self.chainID!
130-
} else if (inferedChainID != nil) {
130+
} else if (inferedChainID != nil && self.v >= (d + self.inferedChainID! + self.inferedChainID!)) {
131131
normalizedV = self.v - d - inferedChainID! - inferedChainID!
132132
} else {
133+
if(d > v) { d = 0 }
133134
normalizedV = self.v - d
134135
}
135136
guard let vData = normalizedV.serialize().setLengthLeft(1) else {return nil}

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ extension EthereumTransaction:Decodable {
110110
case r
111111
case s
112112
case value
113+
case type // present in EIP-1559 transaction objects
113114
}
114115

115116
public init(from decoder: Decoder) throws {
116117
let options = try TransactionOptions(from: decoder)
117118
let container = try decoder.container(keyedBy: CodingKeys.self)
118119

120+
// test to see if it is a EIP-1559 wrapper
121+
let envelope = try decodeHexToBigUInt(container, key: .type, allowOptional: true)
122+
if( (envelope != nil) && (envelope! != BigInt(0)) ) {
123+
// if present and non-sero we are a new wrapper we can't decode
124+
throw Web3Error.dataError
125+
}
126+
119127
var data = try decodeHexToData(container, key: .data, allowOptional: true)
120128
if data != nil {
121129
self.data = data!

0 commit comments

Comments
 (0)