Skip to content

Commit 21cca21

Browse files
committed
make RlpKey enum CaseIterable and remove total to use RlpKey.allCases.count instead
1 parent a556b9d commit 21cca21

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

Sources/web3swift/Transaction/EIP1559Envelope.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ extension EIP1559Envelope {
122122
self.s = try container.decodeHex(to: BigUInt.self, key: .s)
123123
}
124124

125-
private enum RlpKey: Int {
125+
private enum RlpKey: Int, CaseIterable {
126126
case chainId
127127
case nonce
128128
case maxPriorityFeePerGas
@@ -135,17 +135,16 @@ extension EIP1559Envelope {
135135
case sig_v
136136
case sig_r
137137
case sig_s
138-
case total // not a real entry, used to auto-size based on number of keys
139138
}
140139

141140
public init?(rawValue: Data) {
142141
// pop the first byte from the stream [EIP-2718]
143142
let typeByte: UInt8 = rawValue.first ?? 0 // can't decode if we're the wrong type
144-
if typeByte != self.type.rawValue { return nil }
143+
guard self.type.rawValue == typeByte else { return nil }
145144

146145
guard let totalItem = RLP.decode(rawValue.dropFirst(1)) else { return nil }
147146
guard let rlpItem = totalItem[0] else { return nil }
148-
if rlpItem.count != RlpKey.total.rawValue { return nil }
147+
guard RlpKey.allCases.count == rlpItem.count else { return nil }
149148

150149
// we've validated the item count, so rlpItem[keyName] is guaranteed to return something not nil
151150
// swiftlint:disable force_unwrapping

Sources/web3swift/Transaction/EIP2930Envelope.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extension EIP2930Envelope {
105105
}
106106

107107
// RLP encoding positions
108-
fileprivate enum RlpKey: Int {
108+
private enum RlpKey: Int, CaseIterable {
109109
case chainId
110110
case nonce
111111
case gasPrice
@@ -117,17 +117,16 @@ extension EIP2930Envelope {
117117
case sig_v
118118
case sig_r
119119
case sig_s
120-
case total // not a real entry, used to auto-size based on number of keys
121120
}
122121

123122
public init?(rawValue: Data) {
124123
// pop the first byte from the stream [EIP-2718]
125124
let typeByte: UInt8 = rawValue.first ?? 0 // can't decode if we're the wrong type
126-
if typeByte != self.type.rawValue { return nil }
125+
guard self.type.rawValue == typeByte else { return nil }
127126

128127
guard let totalItem = RLP.decode(rawValue.dropFirst(1)) else { return nil }
129128
guard let rlpItem = totalItem[0] else { return nil }
130-
if rlpItem.count != RlpKey.total.rawValue { return nil }
129+
guard RlpKey.allCases.count == rlpItem.count else { return nil }
131130

132131
// we've validated the item count, so rlpItem[keyIndex] is guaranteed to return something not nil
133132
// swiftlint:disable force_unwrapping

Sources/web3swift/Transaction/LegacyEnvelope.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extension LegacyEnvelope {
107107
self.s = try container.decodeHex(to: BigUInt.self, key: .s)
108108
}
109109

110-
fileprivate enum RlpKey: Int {
110+
private enum RlpKey: Int, CaseIterable {
111111
case nonce
112112
case gasPrice
113113
case gasLimit
@@ -117,13 +117,12 @@ extension LegacyEnvelope {
117117
case sig_v
118118
case sig_r
119119
case sig_s
120-
case total
121120
}
122121

123122
public init?(rawValue: Data) {
124123
guard let totalItem = RLP.decode(rawValue) else { return nil }
125124
guard let rlpItem = totalItem[0] else { return nil }
126-
if rlpItem.count != RlpKey.total.rawValue { return nil }
125+
guard RlpKey.allCases.count == rlpItem.count else { return nil }
127126

128127
// we've validated the item count, so rlpItem[key] is guaranteed to return something not nil
129128
// swiftlint:disable force_unwrapping

0 commit comments

Comments
 (0)