Skip to content

Commit 2c8e6b9

Browse files
chore: fixed conflicts with develop
2 parents 96d1e31 + c781699 commit 2c8e6b9

File tree

13 files changed

+70
-185
lines changed

13 files changed

+70
-185
lines changed

Sources/web3swift/Convenience/Data+Extension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public extension Data {
6666
let result = data.withUnsafeMutableBytes { (body: UnsafeMutableRawBufferPointer) -> Int32? in
6767
if let bodyAddress = body.baseAddress, body.count > 0 {
6868
let pointer = bodyAddress.assumingMemoryBound(to: UInt8.self)
69-
return SecRandomCopyBytes(kSecRandomDefault, 32, pointer)
69+
return SecRandomCopyBytes(kSecRandomDefault, length, pointer)
7070
} else {
7171
return nil
7272
}

Sources/web3swift/Convenience/SECP256k1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ extension SECP256K1 {
343343
let result = data.withUnsafeMutableBytes { (mutableRBBytes) -> Int32? in
344344
if let mutableRBytes = mutableRBBytes.baseAddress, mutableRBBytes.count > 0 {
345345
let mutableBytes = mutableRBytes.assumingMemoryBound(to: UInt8.self)
346-
return SecRandomCopyBytes(kSecRandomDefault, 32, mutableBytes)
346+
return SecRandomCopyBytes(kSecRandomDefault, length, mutableBytes)
347347
} else {
348348
return nil
349349
}

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ extension web3.BrowserFunctions {
7474

7575

7676
public func sendTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> [String:Any]? {
77-
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
78-
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
79-
var transactionOptions = TransactionOptions()
80-
transactionOptions.from = options.from
81-
transactionOptions.to = options.to
82-
transactionOptions.value = options.value ?? BigUInt(0)
83-
transactionOptions.gasLimit = options.gasLimit ?? .automatic
84-
transactionOptions.gasPrice = options.gasPrice ?? .automatic
85-
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
77+
do {
78+
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
79+
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
80+
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
81+
var transactionOptions = TransactionOptions()
82+
transactionOptions.from = options.from
83+
transactionOptions.to = options.to
84+
transactionOptions.value = options.value ?? 0
85+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
86+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
87+
return self.sendTransaction(transaction, transactionOptions: transactionOptions, password: password)
88+
} catch { return nil }
8689
}
8790

8891
public func sendTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> [String:Any]? {
@@ -95,15 +98,18 @@ extension web3.BrowserFunctions {
9598
}
9699

97100
public func estimateGas(_ transactionJSON: [String: Any]) -> BigUInt? {
98-
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
99-
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
100-
var transactionOptions = TransactionOptions()
101-
transactionOptions.from = options.from
102-
transactionOptions.to = options.to
103-
transactionOptions.value = options.value ?? BigUInt(0)
104-
transactionOptions.gasLimit = .automatic
105-
transactionOptions.gasPrice = options.gasPrice ?? .automatic
106-
return self.estimateGas(transaction, transactionOptions: transactionOptions)
101+
do {
102+
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
103+
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
104+
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
105+
var transactionOptions = TransactionOptions()
106+
transactionOptions.from = options.from
107+
transactionOptions.to = options.to
108+
transactionOptions.value = options.value ?? 0
109+
transactionOptions.gasLimit = .automatic
110+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
111+
return self.estimateGas(transaction, transactionOptions: transactionOptions)
112+
} catch { return nil }
107113
}
108114

109115
public func estimateGas(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions) -> BigUInt? {
@@ -116,9 +122,10 @@ extension web3.BrowserFunctions {
116122
}
117123

118124
public func prepareTxForApproval(_ transactionJSON: [String: Any]) -> (transaction: EthereumTransaction?, options: TransactionOptions?) {
119-
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return (nil, nil)}
120-
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return (nil, nil)}
121125
do {
126+
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
127+
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
128+
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
122129
return try self.prepareTxForApproval(transaction, options: options)
123130
} catch {
124131
return (nil, nil)
@@ -144,20 +151,23 @@ extension web3.BrowserFunctions {
144151
}
145152

146153
public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
147-
guard let transaction = EthereumTransaction.fromJSON(transactionJSON) else {return nil}
148-
guard let options = TransactionOptions.fromJSON(transactionJSON) else {return nil}
149-
var transactionOptions = TransactionOptions()
150-
transactionOptions.from = options.from
151-
transactionOptions.to = options.to
152-
transactionOptions.value = options.value ?? BigUInt(0)
153-
transactionOptions.gasLimit = options.gasLimit ?? .automatic
154-
transactionOptions.gasPrice = options.gasPrice ?? .automatic
155-
if let nonceString = transactionJSON["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
156-
transactionOptions.nonce = .manual(nonce)
157-
} else {
158-
transactionOptions.nonce = .pending
159-
}
160-
return self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
154+
do {
155+
let jsonData: Data = try JSONSerialization.data(withJSONObject: transactionJSON, options: [])
156+
let transaction: EthereumTransaction = try JSONDecoder().decode(EthereumTransaction.self, from: jsonData)
157+
let options: TransactionOptions = try JSONDecoder().decode(TransactionOptions.self, from: jsonData)
158+
var transactionOptions = TransactionOptions()
159+
transactionOptions.from = options.from
160+
transactionOptions.to = options.to
161+
transactionOptions.value = options.value ?? 0
162+
transactionOptions.gasLimit = options.gasLimit ?? .automatic
163+
transactionOptions.gasPrice = options.gasPrice ?? .automatic
164+
if let nonceString = transactionJSON["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
165+
transactionOptions.nonce = .manual(nonce)
166+
} else {
167+
transactionOptions.nonce = .pending
168+
}
169+
return self.signTransaction(transaction, transactionOptions: transactionOptions, password: password)
170+
} catch { return nil }
161171
}
162172

163173
public func signTransaction(_ trans: EthereumTransaction, transactionOptions: TransactionOptions, password: String = "web3swift") -> String? {

Sources/web3swift/KeystoreManager/EthereumKeystoreV3.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class EthereumKeystoreV3: AbstractKeystore {
9696
if (keyData == nil) {
9797
throw AbstractKeystoreError.encryptionError("Encryption without key data")
9898
}
99-
let saltLen = 32;
99+
let saltLen = 32
100100
guard let saltData = Data.randomBytes(length: saltLen) else {
101101
throw AbstractKeystoreError.noEntropyError
102102
}

Sources/web3swift/Promises/Promise+Web3+Contract+GetIndexedEvents.swift

Lines changed: 0 additions & 11 deletions
This file was deleted.

Sources/web3swift/Tokens/ERC721/Web3+ERC721.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ extension ERC721: IERC721Metadata {
272272
let contract = self.contract
273273
var transactionOptions = TransactionOptions()
274274
transactionOptions.callOnBlock = .latest
275-
let result = try contract.read("tokenId", parameters: [tokenId] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
275+
let result = try contract.read("tokenURI", parameters: [tokenId] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
276276
guard let res = result["0"] as? String else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
277277
return res
278278
}

Sources/web3swift/Tokens/ERC721x/Web3+ERC721x.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public class ERC721x: IERC721x {
248248
let contract = self.contract
249249
var transactionOptions = TransactionOptions()
250250
transactionOptions.callOnBlock = .latest
251-
let result = try contract.read("tokenId", parameters: [tokenId] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
251+
let result = try contract.read("tokenURI", parameters: [tokenId] as [AnyObject], extraData: Data(), transactionOptions: self.transactionOptions)!.call(transactionOptions: transactionOptions)
252252
guard let res = result["0"] as? String else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
253253
return res
254254
}

Sources/web3swift/Transaction/EthereumTransaction.swift

Lines changed: 7 additions & 52 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)) {
129-
normalizedV = self.v - d - self.chainID! - self.chainID!
130-
} else if (inferedChainID != nil) {
131-
normalizedV = self.v - d - inferedChainID! - inferedChainID!
128+
if let testID = self.chainID, testID != BigUInt(0) && self.v >= (d + testID + testID) {
129+
normalizedV = self.v - d - testID - testID
130+
} else if let testID = inferedChainID, self.v >= (d + testID + testID) {
131+
normalizedV = self.v - d - testID - testID
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}
@@ -353,51 +354,5 @@ public extension EthereumTransaction {
353354
}
354355
return tx
355356
}
356-
357-
static func fromJSON(_ json: [String: Any]) -> EthereumTransaction? {
358-
guard let options = TransactionOptions.fromJSON(json) else {return nil}
359-
guard let toString = json["to"] as? String else {return nil}
360-
var to: EthereumAddress
361-
if toString == "0x" || toString == "0x0" {
362-
to = EthereumAddress.contractDeploymentAddress()
363-
} else {
364-
guard let ethAddr = EthereumAddress(toString) else {return nil}
365-
to = ethAddr
366-
}
367-
// if (!to.isValid) {
368-
// return nil
369-
// }
370-
var dataString = json["data"] as? String
371-
if (dataString == nil) {
372-
dataString = json["input"] as? String
373-
}
374-
guard dataString != nil, let data = Data.fromHex(dataString!) else {return nil}
375-
var transaction = EthereumTransaction(to: to, data: data, options: options)
376-
if let nonceString = json["nonce"] as? String {
377-
guard let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) else {return nil}
378-
transaction.nonce = nonce
379-
}
380-
if let vString = json["v"] as? String {
381-
guard let v = BigUInt(vString.stripHexPrefix(), radix: 16) else {return nil}
382-
transaction.v = v
383-
}
384-
if let rString = json["r"] as? String {
385-
guard let r = BigUInt(rString.stripHexPrefix(), radix: 16) else {return nil}
386-
transaction.r = r
387-
}
388-
if let sString = json["s"] as? String {
389-
guard let s = BigUInt(sString.stripHexPrefix(), radix: 16) else {return nil}
390-
transaction.s = s
391-
}
392-
if let valueString = json["value"] as? String {
393-
guard let value = BigUInt(valueString.stripHexPrefix(), radix: 16) else {return nil}
394-
transaction.value = value
395-
}
396-
let inferedChainID = transaction.inferedChainID
397-
if (transaction.inferedChainID != nil && transaction.v >= BigUInt(37)) {
398-
transaction.chainID = inferedChainID
399-
}
400-
return transaction
401-
}
402-
357+
403358
}

Sources/web3swift/Web3/Web3+Options.swift

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public struct TransactionOptions {
108108
return nil
109109
}
110110
}
111-
111+
112112
public func merge(_ otherOptions: TransactionOptions?) -> TransactionOptions {
113113
guard let other = otherOptions else {return self}
114114
var opts = TransactionOptions()
@@ -121,45 +121,7 @@ public struct TransactionOptions {
121121
opts.callOnBlock = mergeIfNotNil(first: self.callOnBlock, second: other.callOnBlock)
122122
return opts
123123
}
124-
125-
public static func fromJSON(_ json: [String: Any]) -> TransactionOptions? {
126-
var options = TransactionOptions()
127-
if let gas = json["gas"] as? String, let gasBiguint = BigUInt(gas.stripHexPrefix().lowercased(), radix: 16) {
128-
options.gasLimit = .manual(gasBiguint)
129-
} else if let gasLimit = json["gasLimit"] as? String, let gasgasLimitBiguint = BigUInt(gasLimit.stripHexPrefix().lowercased(), radix: 16) {
130-
options.gasLimit = .limited(gasgasLimitBiguint)
131-
} else {
132-
options.gasLimit = .automatic
133-
}
134-
if let gasPrice = json["gasPrice"] as? String, let gasPriceBiguint = BigUInt(gasPrice.stripHexPrefix().lowercased(), radix: 16) {
135-
options.gasPrice = .manual(gasPriceBiguint)
136-
} else {
137-
options.gasPrice = .automatic
138-
}
139-
if let value = json["value"] as? String, let valueBiguint = BigUInt(value.stripHexPrefix().lowercased(), radix: 16) {
140-
options.value = valueBiguint
141-
}
142-
if let toString = json["to"] as? String {
143-
guard let addressTo = EthereumAddress(toString) else {return nil}
144-
options.to = addressTo
145-
}
146-
if let fromString = json["from"] as? String {
147-
guard let addressFrom = EthereumAddress(fromString) else {return nil}
148-
options.from = addressFrom
149-
}
150-
if let nonceString = json["nonce"] as? String, let nonce = BigUInt(nonceString.stripHexPrefix(), radix: 16) {
151-
options.nonce = .manual(nonce)
152-
} else {
153-
options.nonce = .pending
154-
}
155-
if let callOnBlockString = json["callOnBlock"] as? String, let callOnBlock = BigUInt(callOnBlockString.stripHexPrefix(), radix: 16) {
156-
options.callOnBlock = .exactBlockNumber(callOnBlock)
157-
} else {
158-
options.callOnBlock = .pending
159-
}
160-
return options
161-
}
162-
124+
163125
/// Merges two sets of topions by overriding the parameters from the first set by parameters from the second
164126
/// set if those are not nil.
165127
///

Sources/web3swift/Web3/Web3+Structures.swift

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ 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+
if let envelope = try decodeHexToBigUInt(container, key: .type, allowOptional: true) {
122+
// if present and non-sero we are a new wrapper we can't decode
123+
if(envelope != BigInt(0)) { throw Web3Error.dataError }
124+
}
125+
119126
var data = try decodeHexToData(container, key: .data, allowOptional: true)
120127
if data != nil {
121128
self.data = data!
@@ -198,25 +205,6 @@ public struct TransactionDetails: Decodable {
198205
let transaction = try EthereumTransaction(from: decoder)
199206
self.transaction = transaction
200207
}
201-
202-
public init? (_ json: [String: AnyObject]) {
203-
let bh = json["blockHash"] as? String
204-
if (bh != nil) {
205-
guard let blockHash = Data.fromHex(bh!) else {return nil}
206-
self.blockHash = blockHash
207-
}
208-
let bn = json["blockNumber"] as? String
209-
let ti = json["transactionIndex"] as? String
210-
211-
guard let transaction = EthereumTransaction.fromJSON(json) else {return nil}
212-
self.transaction = transaction
213-
if bn != nil {
214-
blockNumber = BigUInt(bn!.stripHexPrefix(), radix: 16)
215-
}
216-
if ti != nil {
217-
transactionIndex = BigUInt(ti!.stripHexPrefix(), radix: 16)
218-
}
219-
}
220208
}
221209

222210
public struct TransactionReceipt: Decodable {
@@ -418,27 +406,12 @@ public enum TransactionInBlock:Decodable {
418406
if let string = try? value.decode(String.self) {
419407
guard let d = Data.fromHex(string) else {throw Web3Error.dataError}
420408
self = .hash(d)
421-
} else if let dict = try? value.decode([String:String].self) {
422-
// guard let t = try? EthereumTransaction(from: decoder) else {throw Web3Error.dataError}
423-
guard let t = EthereumTransaction.fromJSON(dict) else {throw Web3Error.dataError}
424-
self = .transaction(t)
409+
} else if let transaction = try? value.decode(EthereumTransaction.self) {
410+
self = .transaction(transaction)
425411
} else {
426412
self = .null
427413
}
428414
}
429-
430-
431-
public init?(_ data: AnyObject) {
432-
if let string = data as? String {
433-
guard let d = Data.fromHex(string) else {return nil}
434-
self = .hash(d)
435-
} else if let dict = data as? [String:AnyObject] {
436-
guard let t = EthereumTransaction.fromJSON(dict) else {return nil}
437-
self = .transaction(t)
438-
} else {
439-
return nil
440-
}
441-
}
442415
}
443416

444417
public struct Block:Decodable {

0 commit comments

Comments
 (0)