Skip to content

Commit 6d8ea4c

Browse files
Almost all signing test working.
Except EIP-155
1 parent 68cb9a3 commit 6d8ea4c

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

Sources/Core/Transaction/EncodableTransaction.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public struct EncodableTransaction {
4949
/// the chainId that transaction is targeted for
5050
/// should be set for all types, except some Legacy transactions (Pre EIP-155)
5151
/// will not have this set
52-
public var chainID: BigUInt?
52+
public var chainID: BigUInt {
53+
get { return envelope.chainID }
54+
set { envelope.chainID = newValue }
55+
}
5356

5457
/// the native value of the transaction
5558
public var value: BigUInt
@@ -60,7 +63,10 @@ public struct EncodableTransaction {
6063
// MARK: - Properties transaction type related either sends to a node if exist
6164

6265
/// the max number of gas units allowed to process this transaction
63-
public var gasLimit: BigUInt?
66+
public var gasLimit: BigUInt {
67+
get { return envelope.gasLimit }
68+
set { return envelope.gasLimit = newValue }
69+
}
6470

6571
/// the price per gas unit for the tranaction (Legacy and EIP-2930 only)
6672
public var gasPrice: BigUInt?
@@ -155,7 +161,7 @@ public struct EncodableTransaction {
155161
if self.type == type { return }
156162

157163
let newEnvelope = EnvelopeFactory.createEnvelope(type: type, to: self.envelope.to,
158-
nonce: self.envelope.nonce, parameters: self.envelope.parameters)
164+
nonce: self.envelope.nonce)
159165
self.envelope = newEnvelope
160166
}
161167

@@ -222,14 +228,14 @@ extension EncodableTransaction: Codable {
222228
try containier.encode(type.rawValue.hexString, forKey: .type)
223229
try containier.encode(from, forKey: .from)
224230
try containier.encode(nonce.hexString, forKey: .nonce)
225-
try containier.encode(chainID?.hexString, forKey: .chainID)
231+
try containier.encode(chainID.hexString, forKey: .chainID)
226232
try containier.encode(accessList, forKey: .accessList)
227233
try containier.encode(data.toHexString().addHexPrefix(), forKey: .data)
228234
try containier.encode(value.hexString, forKey: .value)
229235

230236
// Encoding only fields with value.
231237
// TODO: Rewrite me somehow better.
232-
if let gasLimit = gasLimit, !gasLimit.isZero {
238+
if !gasLimit.isZero {
233239
try containier.encode(gasLimit.hexString, forKey: .gasLimit)
234240
}
235241

@@ -284,7 +290,6 @@ extension EncodableTransaction {
284290
chainID: BigUInt? = nil, value: BigUInt? = nil, data: Data = Data(),
285291
v: BigUInt = 1, r: BigUInt = 0, s: BigUInt = 0) {
286292

287-
self.chainID = chainID ?? 0
288293
self.value = value ?? 0
289294
self.data = data
290295

Sources/Core/Transaction/Envelope/AbstractEnvelope.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ public protocol AbstractEnvelope: CustomStringConvertible { // possibly add Coda
7474

7575
var sender: EthereumAddress? { get }
7676

77+
var chainID: BigUInt { get set }
78+
7779
/// On chain address that this transaction is being sent to
7880
var to: EthereumAddress { get set }
7981

80-
// /// The native value of the transaction in Wei
82+
/// The native value of the transaction in Wei
8183
var value: BigUInt { get set }
84+
85+
var gasLimit: BigUInt { get set }
8286

83-
// /// Any encoded data accompanying the transaction
87+
/// Any encoded data accompanying the transaction
8488
var data: Data { get set }
8589

8690
// Signature data should not be set directly
@@ -93,11 +97,6 @@ public protocol AbstractEnvelope: CustomStringConvertible { // possibly add Coda
9397
/// signature S compoonent
9498
var s: BigUInt { get set }
9599

96-
/// Transaction Parameters object
97-
/// used to provide external access to the otherwise
98-
/// protected parameters of the object
99-
var parameters: EncodableTransaction { get set }
100-
101100
/// - Returns: the public key decoded from the signature data
102101
var publicKey: Data? { get }
103102

Sources/Core/Transaction/Envelope/LegacyEnvelope.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct LegacyEnvelope: AbstractEnvelope {
1212

1313
// common parameters for any transaction
1414
public var nonce: BigUInt = 0
15-
public var chainID: BigUInt? {
15+
public var chainID: BigUInt {
1616
get {
1717
if let id = explicitChainID, id != 0 { return id }
1818
return impliedChainID
@@ -33,9 +33,9 @@ public struct LegacyEnvelope: AbstractEnvelope {
3333
// legacy chainID Mechanism
3434
private var explicitChainID: BigUInt? // set directly or via options
3535
// private var impliedChainID: BigUInt? // we calculate this once, or when explicitely asked to
36-
private var impliedChainID: BigUInt? {
36+
private var impliedChainID: BigUInt {
3737
if r == 0 && s == 0 { return v }
38-
if v == 27 || v == 28 || v < 35 { return nil }
38+
if v == 27 || v == 28 || v < 35 { return 0 }
3939
return ((v - 1) / 2) - 17
4040
}
4141

@@ -225,7 +225,7 @@ extension LegacyEnvelope {
225225
switch type {
226226
case .transaction: fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, v, r, s] as [AnyObject]
227227
case .signature:
228-
if let chainID = self.chainID, chainID != 0 {
228+
if chainID != 0 {
229229
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject]
230230
} else {
231231
fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value, self.data] as [AnyObject]

Sources/web3swift/Web3/Web3+Contract.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ extension web3 {
6767
extraData: extraData)
6868
else { return nil }
6969

70-
tx.chainID = self.web3.provider.network?.chainID
70+
if let network = self.web3.provider.network {
71+
tx.chainID = network.chainID
72+
}
7173
return WriteTransaction(transaction: tx,
7274
web3: self.web3,
7375
contract: self.contract,
@@ -84,7 +86,9 @@ extension web3 {
8486
public func method(_ method: String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data(), transactionOptions: TransactionOptions? = nil) -> WriteTransaction? {
8587
let mergedOptions = self.transactionOptions?.merge(transactionOptions)
8688
guard var tx = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil}
87-
tx.chainID = self.web3.provider.network?.chainID
89+
if let network = self.web3.provider.network {
90+
tx.chainID = network.chainID
91+
}
8892
let writeTX = WriteTransaction.init(transaction: tx, web3: self.web3, contract: self.contract, method: method, transactionOptions: mergedOptions)
8993
return writeTX
9094
}
@@ -101,7 +105,9 @@ extension web3 {
101105
let mergedOptions = self.transactionOptions?.merge(transactionOptions)
102106
// MARK: - Encoding ABI Data flow
103107
guard var tx = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil}
104-
tx.chainID = self.web3.provider.network?.chainID
108+
if let network = self.web3.provider.network {
109+
tx.chainID = network.chainID
110+
}
105111
// MARK: Read data from ABI flow
106112
let writeTX = ReadTransaction.init(transaction: tx, web3: self.web3, contract: self.contract, method: method, transactionOptions: mergedOptions)
107113
return writeTX
@@ -117,7 +123,9 @@ extension web3 {
117123
public func write(_ method: String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data(), transactionOptions: TransactionOptions? = nil) -> WriteTransaction? {
118124
let mergedOptions = self.transactionOptions?.merge(transactionOptions)
119125
guard var tx = self.contract.method(method, parameters: parameters, extraData: extraData) else {return nil}
120-
tx.chainID = self.web3.provider.network?.chainID
126+
if let network = self.web3.provider.network {
127+
tx.chainID = network.chainID
128+
}
121129
let writeTX = WriteTransaction.init(transaction: tx, web3: self.web3, contract: self.contract, method: method, transactionOptions: mergedOptions)
122130
return writeTX
123131
}

Sources/web3swift/Web3/Web3+ReadingTransaction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class ReadTransaction {
2929
self.contract = contract
3030
self.method = method
3131
self.transactionOptions = self.transactionOptions.merge(transactionOptions)
32-
if self.web3.provider.network != nil {
33-
self.transaction.chainID = self.web3.provider.network?.chainID
32+
if let network = self.web3.provider.network {
33+
self.transaction.chainID = network.chainID
3434
}
3535
}
3636

Tests/web3swiftTests/localTests/TransactionsTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,11 @@ class TransactionsTests: XCTestCase {
613613
print(transaction)
614614
let hash = transaction.hashForSignature()
615615
let expectedHash = "0xdaf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53".stripHexPrefix()
616-
XCTAssert(hash!.toHexString() == expectedHash, "Transaction signature failed")
616+
XCTAssertEqual(hash!.toHexString(), expectedHash, "Transaction signature failed")
617617
try transaction.sign(privateKey: privateKeyData, useExtraEntropy: false)
618618
print(transaction)
619-
XCTAssert(transaction.v == 37, "Transaction signature failed")
620-
XCTAssert(sender == transaction.sender)
619+
XCTAssertEqual(transaction.v, 37, "Transaction signature failed")
620+
XCTAssertEqual(sender, transaction.sender)
621621
} catch {
622622
print(error)
623623
XCTFail()

0 commit comments

Comments
 (0)