Skip to content

Commit 410e343

Browse files
Merge pull request #624 from JeneaVranceanu/fix/envelope-data-field
2 parents 9c9fca4 + 119d23c commit 410e343

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

Sources/Core/Transaction/CodableTransaction.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ public struct CodableTransaction {
5454
set { envelope.value = newValue }
5555
}
5656

57-
// MARK: - Ruins signing and decoding tests if tied to envelop
58-
/// any additional data for the transaction
59-
public var data: Data
57+
public var data: Data {
58+
get { return envelope.data }
59+
set { envelope.data = newValue }
60+
}
6061

6162
// MARK: - Properties transaction type related either sends to a node if exist
6263

@@ -94,7 +95,15 @@ public struct CodableTransaction {
9495
public var callOnBlock: BlockNumber?
9596

9697
/// access list for contract execution (EIP-2930 and EIP-1559 only)
97-
public var accessList: [AccessListEntry]?
98+
public var accessList: [AccessListEntry]? {
99+
get {
100+
(envelope as? EIP2930Compatible)?.accessList
101+
}
102+
set {
103+
var eip2930Compatible = (envelope as? EIP2930Compatible)
104+
eip2930Compatible?.accessList = newValue ?? []
105+
}
106+
}
98107

99108
// MARK: - Properties to contract encode/sign data only
100109

@@ -172,8 +181,6 @@ public struct CodableTransaction {
172181
public init?(rawValue: Data) {
173182
guard let env = EnvelopeFactory.createEnvelope(rawValue: rawValue) else { return nil }
174183
self.envelope = env
175-
// FIXME: This is duplication and should be fixed.
176-
data = Data()
177184
}
178185

179186
/// - Returns: a raw bytestream of the transaction, encoded according to the transactionType
@@ -205,8 +212,6 @@ extension CodableTransaction: Codable {
205212
public init(from decoder: Decoder) throws {
206213
guard let env = try EnvelopeFactory.createEnvelope(from: decoder) else { throw Web3Error.dataError }
207214
self.envelope = env
208-
// FIXME: This is duplication and should be fixed.
209-
data = Data()
210215

211216
// capture any metadata that might be present
212217
self.meta = try TransactionMetadata(from: decoder)
@@ -281,7 +286,7 @@ extension CodableTransaction {
281286
/// - nonce: nonce for this transaction (default 0)
282287
/// - chainID: chainId the transaction belongs to (default: type specific)
283288
/// - value: Native value for the transaction (default 0)
284-
/// - data: Payload data for the transaction (required)
289+
/// - data: Payload data for the transaction (default 0 bytes)
285290
/// - v: signature v parameter (default 1) - will get set properly once signed
286291
/// - r: signature r parameter (default 0) - will get set properly once signed
287292
/// - s: signature s parameter (default 0) - will get set properly once signed
@@ -290,12 +295,9 @@ extension CodableTransaction {
290295
chainID: BigUInt = 0, value: BigUInt = 0, data: Data = Data(),
291296
gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil,
292297
accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
293-
// FIXME: This is duplication and should be fixed.
294-
self.data = data
295-
self.accessList = accessList
296-
self.callOnBlock = .latest
298+
callOnBlock = .latest
297299

298-
self.envelope = EnvelopeFactory.createEnvelope(type: type, to: to, nonce: nonce, chainID: chainID, value: value, data: data, gasLimit: gasLimit, maxFeePerGas: maxFeePerGas, maxPriorityFeePerGas: maxPriorityFeePerGas, gasPrice: gasPrice, accessList: accessList, v: v, r: r, s: s)
300+
envelope = EnvelopeFactory.createEnvelope(type: type, to: to, nonce: nonce, chainID: chainID, value: value, data: data, gasLimit: gasLimit, maxFeePerGas: maxFeePerGas, maxPriorityFeePerGas: maxPriorityFeePerGas, gasPrice: gasPrice, accessList: accessList, v: v, r: r, s: s)
299301
}
300302
}
301303

Sources/Core/Transaction/Envelope/EIP1559Envelope.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import Foundation
88
import BigInt
99

10-
public struct EIP1559Envelope: EIP2718Envelope {
10+
public struct EIP1559Envelope: EIP2718Envelope, EIP2930Compatible {
1111
public let type: TransactionType = .eip1559
1212

1313
// common parameters for any transaction

Sources/Core/Transaction/Envelope/EIP2930Envelope.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import Foundation
88
import BigInt
99

10-
public struct EIP2930Envelope: EIP2718Envelope {
10+
public struct EIP2930Envelope: EIP2718Envelope, EIP2930Compatible {
1111
public let type: TransactionType = .eip2930
1212

1313
// common parameters for any transaction
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// EIP2930Compatible.swift
3+
//
4+
// Created by JeneaVranceanu on 10.11.2022.
5+
//
6+
7+
import Foundation
8+
9+
/// Protocol to support `EIP-2930` properties access
10+
public protocol EIP2930Compatible {
11+
var accessList: [AccessListEntry] { get set }
12+
}

0 commit comments

Comments
 (0)