From ef06fbfc735ab687ebf89c937a9d1ffe6d81d37f Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu Date: Tue, 27 Sep 2022 12:40:47 +0100 Subject: [PATCH 1/3] fix: data field in CodableTransaction must be passed into the envelope --- Sources/Core/Transaction/CodableTransaction.swift | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Sources/Core/Transaction/CodableTransaction.swift b/Sources/Core/Transaction/CodableTransaction.swift index 288a82d5d..ea0038b63 100644 --- a/Sources/Core/Transaction/CodableTransaction.swift +++ b/Sources/Core/Transaction/CodableTransaction.swift @@ -56,9 +56,10 @@ public struct CodableTransaction { set { envelope.value = newValue } } - // MARK: - Ruins signing and decoding tests if tied to envelop - /// any additional data for the transaction - public var data: Data + public var data: Data { + get { return envelope.data } + set { envelope.data = newValue } + } // MARK: - Properties transaction type related either sends to a node if exist @@ -174,8 +175,6 @@ public struct CodableTransaction { public init?(rawValue: Data) { guard let env = EnvelopeFactory.createEnvelope(rawValue: rawValue) else { return nil } self.envelope = env - // FIXME: This is duplication and should be fixed. - data = Data() noncePolicy = .latest gasLimitPolicy = .automatic gasPricePolicy = .automatic @@ -233,9 +232,6 @@ extension CodableTransaction: Codable { public init(from decoder: Decoder) throws { guard let env = try EnvelopeFactory.createEnvelope(from: decoder) else { throw Web3Error.dataError } self.envelope = env - // FIXME: This is duplication and should be fixed. - data = Data() - noncePolicy = .latest gasLimitPolicy = .automatic gasPricePolicy = .automatic @@ -420,8 +416,6 @@ extension CodableTransaction { chainID: BigUInt = 0, value: BigUInt = 0, data: Data = Data(), gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil, accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) { - // FIXME: This is duplication and should be fixed. - self.data = data self.accessList = accessList self.gasLimitPolicy = .automatic self.noncePolicy = .pending From d745acae4db45813a25e8a8f8a342279d2575327 Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu Date: Thu, 10 Nov 2022 11:57:39 +0200 Subject: [PATCH 2/3] chore: updated docs and removed + minor clean up --- .../Core/Transaction/CodableTransaction.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/Core/Transaction/CodableTransaction.swift b/Sources/Core/Transaction/CodableTransaction.swift index ea0038b63..3b27f9933 100644 --- a/Sources/Core/Transaction/CodableTransaction.swift +++ b/Sources/Core/Transaction/CodableTransaction.swift @@ -407,7 +407,7 @@ extension CodableTransaction { /// - nonce: nonce for this transaction (default 0) /// - chainID: chainId the transaction belongs to (default: type specific) /// - value: Native value for the transaction (default 0) - /// - data: Payload data for the transaction (required) + /// - data: Payload data for the transaction (default 0 bytes) /// - v: signature v parameter (default 1) - will get set properly once signed /// - r: signature r parameter (default 0) - will get set properly once signed /// - s: signature s parameter (default 0) - will get set properly once signed @@ -417,14 +417,14 @@ extension CodableTransaction { gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil, accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) { self.accessList = accessList - self.gasLimitPolicy = .automatic - self.noncePolicy = .pending - self.gasPricePolicy = .automatic - self.maxFeePerGasPolicy = .automatic - self.maxPriorityFeePerGasPolicy = .automatic - self.callOnBlock = .latest - - 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) + gasLimitPolicy = .automatic + noncePolicy = .pending + gasPricePolicy = .automatic + maxFeePerGasPolicy = .automatic + maxPriorityFeePerGasPolicy = .automatic + callOnBlock = .latest + + 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) } } From 119d23cb0f79bf9d782a4beef1391528833c80d2 Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu Date: Thu, 10 Nov 2022 12:13:57 +0200 Subject: [PATCH 3/3] chore: intorduced EIP2930Compatible protocol to support easy access to accessList --- Sources/Core/Transaction/CodableTransaction.swift | 12 +++++++++--- .../Core/Transaction/Envelope/EIP1559Envelope.swift | 2 +- .../Core/Transaction/Envelope/EIP2930Envelope.swift | 2 +- .../Envelope/Protocols/EIP2930Compatible.swift | 12 ++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 Sources/Core/Transaction/Envelope/Protocols/EIP2930Compatible.swift diff --git a/Sources/Core/Transaction/CodableTransaction.swift b/Sources/Core/Transaction/CodableTransaction.swift index 1f9635b2e..19f5773fe 100644 --- a/Sources/Core/Transaction/CodableTransaction.swift +++ b/Sources/Core/Transaction/CodableTransaction.swift @@ -95,7 +95,15 @@ public struct CodableTransaction { public var callOnBlock: BlockNumber? /// access list for contract execution (EIP-2930 and EIP-1559 only) - public var accessList: [AccessListEntry]? + public var accessList: [AccessListEntry]? { + get { + (envelope as? EIP2930Compatible)?.accessList + } + set { + var eip2930Compatible = (envelope as? EIP2930Compatible) + eip2930Compatible?.accessList = newValue ?? [] + } + } // MARK: - Properties to contract encode/sign data only @@ -287,8 +295,6 @@ extension CodableTransaction { chainID: BigUInt = 0, value: BigUInt = 0, data: Data = Data(), gasLimit: BigUInt = 0, maxFeePerGas: BigUInt? = nil, maxPriorityFeePerGas: BigUInt? = nil, gasPrice: BigUInt? = nil, accessList: [AccessListEntry]? = nil, v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) { - // TODO: accessList must be returned from envelope and not stored as a separate variable in CodableTransaction - self.accessList = accessList callOnBlock = .latest 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) diff --git a/Sources/Core/Transaction/Envelope/EIP1559Envelope.swift b/Sources/Core/Transaction/Envelope/EIP1559Envelope.swift index 578c7e800..102848dcc 100644 --- a/Sources/Core/Transaction/Envelope/EIP1559Envelope.swift +++ b/Sources/Core/Transaction/Envelope/EIP1559Envelope.swift @@ -7,7 +7,7 @@ import Foundation import BigInt -public struct EIP1559Envelope: EIP2718Envelope { +public struct EIP1559Envelope: EIP2718Envelope, EIP2930Compatible { public let type: TransactionType = .eip1559 // common parameters for any transaction diff --git a/Sources/Core/Transaction/Envelope/EIP2930Envelope.swift b/Sources/Core/Transaction/Envelope/EIP2930Envelope.swift index 628da7191..f6a71c1ce 100644 --- a/Sources/Core/Transaction/Envelope/EIP2930Envelope.swift +++ b/Sources/Core/Transaction/Envelope/EIP2930Envelope.swift @@ -7,7 +7,7 @@ import Foundation import BigInt -public struct EIP2930Envelope: EIP2718Envelope { +public struct EIP2930Envelope: EIP2718Envelope, EIP2930Compatible { public let type: TransactionType = .eip2930 // common parameters for any transaction diff --git a/Sources/Core/Transaction/Envelope/Protocols/EIP2930Compatible.swift b/Sources/Core/Transaction/Envelope/Protocols/EIP2930Compatible.swift new file mode 100644 index 000000000..4144639c6 --- /dev/null +++ b/Sources/Core/Transaction/Envelope/Protocols/EIP2930Compatible.swift @@ -0,0 +1,12 @@ +// +// EIP2930Compatible.swift +// +// Created by JeneaVranceanu on 10.11.2022. +// + +import Foundation + +/// Protocol to support `EIP-2930` properties access +public protocol EIP2930Compatible { + var accessList: [AccessListEntry] { get set } +}