Skip to content

Commit 5205653

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into Mnemonic-array
2 parents f18cfba + 2357868 commit 5205653

File tree

12 files changed

+267
-76
lines changed

12 files changed

+267
-76
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
- Update libs versions, Cartfile and Pods dependencies [\#334](https://github.com/skywinder/web3swift/pull/334) ([AnnaYatsun1](https://github.com/AnnaYatsun1))
163163
- fix crash when 'payable' nil [\#332](https://github.com/skywinder/web3swift/pull/332) ([xdozorx](https://github.com/xdozorx))
164164
- Update README.md [\#331](https://github.com/skywinder/web3swift/pull/331) ([Iysbaera](https://github.com/Iysbaera))
165-
- CrytoSwift update version 1.4.0 [\#327](https://github.com/skywinder/web3swift/pull/327) ([lzttxs](https://github.com/lzttxs))
165+
- CryptoSwift update version 1.4.0 [\#327](https://github.com/skywinder/web3swift/pull/327) ([lzttxs](https://github.com/lzttxs))
166166
- Update carthage libraries [\#325](https://github.com/skywinder/web3swift/pull/325) ([alex78pro](https://github.com/alex78pro))
167167
- Gas estimate fix [\#324](https://github.com/skywinder/web3swift/pull/324) ([frostiq](https://github.com/frostiq))
168168
- Update README.md [\#306](https://github.com/skywinder/web3swift/pull/306) ([manuG420](https://github.com/manuG420))
@@ -252,7 +252,7 @@
252252
- received transaction id from geth node server but not able to see that transaction id at etherscan.io [\#200](https://github.com/skywinder/web3swift/issues/200)
253253
- How do I fetch information such as balance, decimal,symbol and name of ERC20token ? [\#199](https://github.com/skywinder/web3swift/issues/199)
254254
- Starscream 3.1.0 not compatible with Swift 5.0 [\#195](https://github.com/skywinder/web3swift/issues/195)
255-
- How to Connect infuraWebsocket and subsribe particular event in swift? [\#193](https://github.com/skywinder/web3swift/issues/193)
255+
- How to Connect infuraWebsocket and subscribe particular event in swift? [\#193](https://github.com/skywinder/web3swift/issues/193)
256256
- Use of unresolved identifier 'Wallet' [\#192](https://github.com/skywinder/web3swift/issues/192)
257257
- V in Signed Message Hash not being calculated properly [\#191](https://github.com/skywinder/web3swift/issues/191)
258258
- Not possible to calculate fast, normal and cheap transaction fee ? [\#190](https://github.com/skywinder/web3swift/issues/190)

Example/myWeb3Wallet/myWeb3Wallet/ViewControllers/WalletController/WalletViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class WalletViewController: UIViewController {
102102
}
103103
} catch {
104104
#if DEBUG
105-
print("error creating keyStrore")
105+
print("error creating keyStore")
106106
print("Private key error.")
107107
#endif
108108
let alert = UIAlertController(title: "Error", message: "Please enter correct Private key", preferredStyle: .alert)

Sources/Web3Core/EthereumNetwork/RequestParameter/RequestParameter+RawRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension RequestParameter: RawRepresentable {
1313
/// to encode mixed type values array in JSON.
1414
///
1515
/// This protocol is used to implement custom `encode` method for that enum,
16-
/// which encodes an array of self-assosiated values.
16+
/// which encodes an array of self-associated values.
1717
///
1818
/// You're totally free to use explicit and more convenience member init as `RequestParameter.int(12)` in your code.
1919
/// - Parameter rawValue: one of the supported types like `Int`, `UInt` etc.

Sources/Web3Core/Structure/SECP256k1.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ extension SECP256K1 {
189189
} else if v >= 35 && v <= 38 {
190190
v -= 35
191191
}
192-
let result = serializedSignature.withUnsafeBytes { (serRawBufferPtr: UnsafeRawBufferPointer) -> Int32? in
193-
if let serRawPtr = serRawBufferPtr.baseAddress, serRawBufferPtr.count > 0 {
194-
let serPtr = serRawPtr.assumingMemoryBound(to: UInt8.self)
192+
let result = serializedSignature.withUnsafeBytes { (rawBufferPtr: UnsafeRawBufferPointer) -> Int32? in
193+
if let rawPtr = rawBufferPtr.baseAddress, rawBufferPtr.count > 0 {
194+
let ptr = rawPtr.assumingMemoryBound(to: UInt8.self)
195195
return withUnsafeMutablePointer(to: &recoverableSignature) { (signaturePointer: UnsafeMutablePointer<secp256k1_ecdsa_recoverable_signature>) -> Int32 in
196-
let res = secp256k1_ecdsa_recoverable_signature_parse_compact(context, signaturePointer, serPtr, v)
196+
let res = secp256k1_ecdsa_recoverable_signature_parse_compact(context, signaturePointer, ptr, v)
197197
return res
198198
}
199199
} else {
@@ -210,12 +210,12 @@ extension SECP256K1 {
210210
guard let context = context else { return nil }
211211
var serializedSignature = Data(repeating: 0x00, count: 64)
212212
var v: Int32 = 0
213-
let result = serializedSignature.withUnsafeMutableBytes { (serSignatureRawBufferPointer: UnsafeMutableRawBufferPointer) -> Int32? in
214-
if let serSignatureRawPointer = serSignatureRawBufferPointer.baseAddress, serSignatureRawBufferPointer.count > 0 {
215-
let serSignaturePointer = serSignatureRawPointer.assumingMemoryBound(to: UInt8.self)
213+
let result = serializedSignature.withUnsafeMutableBytes { (signatureRawBufferPointer: UnsafeMutableRawBufferPointer) -> Int32? in
214+
if let signatureRawPointer = signatureRawBufferPointer.baseAddress, signatureRawBufferPointer.count > 0 {
215+
let typedSignaturePointer = signatureRawPointer.assumingMemoryBound(to: UInt8.self)
216216
return withUnsafePointer(to: &recoverableSignature) { (signaturePointer: UnsafePointer<secp256k1_ecdsa_recoverable_signature>) -> Int32 in
217217
withUnsafeMutablePointer(to: &v) { (vPtr: UnsafeMutablePointer<Int32>) -> Int32 in
218-
let res = secp256k1_ecdsa_recoverable_signature_serialize_compact(context, serSignaturePointer, vPtr, signaturePointer)
218+
let res = secp256k1_ecdsa_recoverable_signature_serialize_compact(context, typedSignaturePointer, vPtr, signaturePointer)
219219
return res
220220
}
221221
}

Sources/web3swift/Tokens/ST20/Web3+SecurityToken.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protocol ISecurityToken: IST20, IOwnable {
5454
/// Queries balances as of a defined checkpoint
5555
func balanceOfAt(investor: EthereumAddress, checkpointId: BigUInt) async throws -> BigUInt
5656

57-
/// Creates a checkpoint that can be used to query historical balances / totalSuppy
57+
/// Creates a checkpoint that can be used to query historical balances / totalSupply
5858
func createCheckpoint(from: EthereumAddress) async throws -> WriteOperation
5959

6060
/// gets length of investors array
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// TransactionPollingTask.swift
3+
//
4+
// Created by JeneaVranceanu on 10.03.2023.
5+
//
6+
7+
import Foundation
8+
import Web3Core
9+
10+
/// Monitors a transaction's state on blockchain until transaction is completed successfully or not.
11+
final public class TransactionPollingTask {
12+
13+
private enum DelayUnit: UInt64 {
14+
case shortest = 1
15+
case medium = 5
16+
case longest = 60
17+
18+
func shouldIncreaseDelay(_ startTime: Date) -> Bool {
19+
let timePassed = Date().timeIntervalSince1970 - startTime.timeIntervalSince1970
20+
switch self {
21+
case .shortest:
22+
return timePassed > 10
23+
case .medium:
24+
return timePassed > 120
25+
case .longest:
26+
return false
27+
}
28+
}
29+
30+
var nextDelayUnit: DelayUnit {
31+
switch self {
32+
case .shortest:
33+
return .medium
34+
case .medium, .longest:
35+
return .longest
36+
}
37+
}
38+
}
39+
40+
public let transactionHash: Data
41+
42+
private let web3Instance: Web3
43+
private var delayUnit: DelayUnit = .shortest
44+
45+
public init(transactionHash: Data, web3Instance: Web3) {
46+
self.transactionHash = transactionHash
47+
self.web3Instance = web3Instance
48+
}
49+
50+
public func wait() async throws -> TransactionReceipt {
51+
let startTime = Date()
52+
while true {
53+
let transactionReceipt = try await web3Instance.eth.transactionReceipt(transactionHash)
54+
55+
if transactionReceipt.status != .notYetProcessed {
56+
return transactionReceipt
57+
}
58+
59+
if delayUnit.shouldIncreaseDelay(startTime) {
60+
delayUnit = delayUnit.nextDelayUnit
61+
}
62+
63+
try await Task.sleep(nanoseconds: delayUnit.rawValue)
64+
}
65+
}
66+
}

Sources/web3swift/Utils/EIP/EIP4361.swift

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ private let uriPattern = "(([^:?#\\s]+):)?(([^?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*
1515

1616
/// Sign-In with Ethereum protocol and parser implementation.
1717
///
18+
/// _Regular expressions were generated using ABNF grammar from https://github.com/spruceid/siwe/blob/main/packages/siwe-parser/lib/abnf.ts#L5
19+
/// and tool https://pypi.org/project/abnf-to-regexp/ that outputs Python supported regular expressions._
20+
///
1821
/// EIP-4361:
1922
/// - https://eips.ethereum.org/EIPS/eip-4361
2023
/// - https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4361.md
@@ -60,22 +63,64 @@ public final class EIP4361 {
6063
case resources
6164
}
6265

63-
private static let domain = "(?<\(EIP4361Field.domain.rawValue)>([^?#]*)) wants you to sign in with your Ethereum account:"
64-
private static let address = "\\n(?<\(EIP4361Field.address.rawValue)>0x[a-zA-Z0-9]{40})\\n\\n"
65-
private static let statementParagraph = "((?<\(EIP4361Field.statement.rawValue)>[^\\n]+)\\n)?"
66-
private static let uri = "\\nURI: (?<\(EIP4361Field.uri.rawValue)>(\(uriPattern))?)"
67-
private static let version = "\\nVersion: (?<\(EIP4361Field.version.rawValue)>[0-9]+)"
68-
private static let chainId = "\\nChain ID: (?<\(EIP4361Field.chainId.rawValue)>[0-9a-fA-F]+)"
69-
private static let nonce = "\\nNonce: (?<\(EIP4361Field.nonce.rawValue)>[a-zA-Z0-9]{8,})"
70-
private static let issuedAt = "\\nIssued At: (?<\(EIP4361Field.issuedAt.rawValue)>(\(datetimePattern)))"
71-
private static let expirationTime = "(\\nExpiration Time: (?<\(EIP4361Field.expirationTime.rawValue)>(\(datetimePattern))))?"
72-
private static let notBefore = "(\\nNot Before: (?<\(EIP4361Field.notBefore.rawValue)>(\(datetimePattern))))?"
73-
private static let requestId = "(\\nRequest ID: (?<\(EIP4361Field.requestId.rawValue)>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?"
74-
private static let resourcesParagraph = "(\\nResources:(?<\(EIP4361Field.resources.rawValue)>(\\n- (\(uriPattern))?)+))?"
75-
76-
private static var eip4361Pattern: String {
77-
"^\(domain)\(address)\(statementParagraph)\(uri)\(version)\(chainId)\(nonce)\(issuedAt)\(expirationTime)\(notBefore)\(requestId)\(resourcesParagraph)$"
78-
}
66+
private static let unreserved = "[a-zA-Z0-9\\-._~]"
67+
private static let pctEncoded = "%[0-9A-Fa-f][0-9A-Fa-f]"
68+
private static let subDelims = "[!$&'()*+,;=]"
69+
private static let userinfo = "(\(unreserved)|\(pctEncoded)|\(subDelims)|:)*"
70+
private static let h16 = "[0-9A-Fa-f]{1,4}"
71+
private static let decOctet = "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
72+
private static let ipv4address = "\(decOctet)\\.\(decOctet)\\.\(decOctet)\\.\(decOctet)"
73+
private static let ls32 = "(\(h16):\(h16)|\(ipv4address))"
74+
private static let ipv6address = "((\(h16):){6}\(ls32)|::(\(h16):){5}\(ls32)|(\(h16))?::(\(h16):){4}\(ls32)|((\(h16):)?\(h16))?::(\(h16):){3}\(ls32)|((\(h16):){2}\(h16))?::(\(h16):){2}\(ls32)|((\(h16):){3}\(h16))?::\(h16):\(ls32)|((\(h16):){4}\(h16))?::\(ls32)|((\(h16):){5}\(h16))?::\(h16)|((\(h16):){6}\(h16))?::)"
75+
private static let ipvfuture = "[vV][0-9A-Fa-f]+\\.(\(unreserved)|\(subDelims)|:)+"
76+
private static let ipLiteral = "\\[(\(ipv6address)|\(ipvfuture))\\]"
77+
private static let regName = "(\(unreserved)|\(pctEncoded)|\(subDelims))*"
78+
private static let host = "(\(ipLiteral)|\(ipv4address)|\(regName))"
79+
private static let port = "[0-9]*"
80+
private static let authority = "(\(userinfo)@)?\(host)(:\(port))?"
81+
private static let dateFullyear = "[0-9]{4}"
82+
private static let dateMday = "[0-9]{2}"
83+
private static let dateMonth = "[0-9]{2}"
84+
private static let fullDate = "\(dateFullyear)-\(dateMonth)-\(dateMday)"
85+
private static let timeHour = "[0-9]{2}"
86+
private static let timeMinute = "[0-9]{2}"
87+
private static let timeSecond = "[0-9]{2}"
88+
private static let timeSecfrac = "\\.[0-9]+"
89+
private static let partialTime = "\(timeHour):\(timeMinute):\(timeSecond)(\(timeSecfrac))?"
90+
private static let timeNumoffset = "[+\\-]\(timeHour):\(timeMinute)"
91+
private static let timeOffset = "([zZ]|\(timeNumoffset))"
92+
private static let fullTime = "\(partialTime)\(timeOffset)"
93+
private static let dateTime = "\(fullDate)[tT]\(fullTime)"
94+
private static let pchar = "(\(unreserved)|\(pctEncoded)|\(subDelims)|[:@])"
95+
private static let fragment = "(\(pchar)|[/?])*"
96+
private static let genDelims = "[:/?#\\[\\]@]"
97+
private static let segment = "(\(pchar))*"
98+
private static let pathAbempty = "(/\(segment))*"
99+
private static let segmentNz = "(\(pchar))+"
100+
private static let pathAbsolute = "/(\(segmentNz)(/\(segment))*)?"
101+
private static let pathRootless = "\(segmentNz)(/\(segment))*"
102+
private static let pathEmpty = "(\(pchar)){0}"
103+
private static let hierPart = "(//\(authority)\(pathAbempty)|\(pathAbsolute)|\(pathRootless)|\(pathEmpty))"
104+
private static let query = "(\(pchar)|[/?])*"
105+
private static let reserved = "(\(genDelims)|\(subDelims))"
106+
private static let scheme = "[a-zA-Z][a-zA-Z0-9+\\-.]*"
107+
private static let resource = "- \(uri)"
108+
109+
// MARK: The final regular expression parts
110+
private static let domain = authority
111+
private static let address = "0x[0-9A-Fa-f]{40}"
112+
private static let statement = "(\(reserved)|\(unreserved)| )+"
113+
private static let uri = "\(scheme):\(hierPart)(\\?\(query))?(\\#\(fragment))?"
114+
private static let version = "[0-9]+"
115+
private static let chainId = "[0-9]+"
116+
private static let nonce = "[a-zA-Z0-9]{8,}"
117+
private static let issuedAt = dateTime
118+
private static let expirationTime = dateTime
119+
private static let notBefore = dateTime
120+
private static let requestId = "(\(pchar))*"
121+
private static let resources = "(\\n\(resource))*"
122+
123+
private static let eip4361Pattern = "(?<\(EIP4361Field.domain.rawValue)>\(domain)) wants you to sign in with your Ethereum account:\\n(?<\(EIP4361Field.address.rawValue)>\(address))\\n\\n((?<\(EIP4361Field.statement.rawValue)>\(statement))\\n)?\\nURI: (?<\(EIP4361Field.uri.rawValue)>\(uri))\\nVersion: (?<\(EIP4361Field.version.rawValue)>\(version))\\nChain ID: (?<\(EIP4361Field.chainId.rawValue)>\(chainId))\\nNonce: (?<\(EIP4361Field.nonce.rawValue)>\(nonce))\\nIssued At: (?<\(EIP4361Field.issuedAt.rawValue)>\(issuedAt))(\\nExpiration Time: (?<\(EIP4361Field.expirationTime.rawValue)>\(expirationTime)))?(\\nNot Before: (?<\(EIP4361Field.notBefore.rawValue)>\(notBefore)))?(\\nRequest ID: (?<\(EIP4361Field.requestId.rawValue)>\(requestId)))?(\\nResources:(?<\(EIP4361Field.resources.rawValue)>\(resources)))?"
79124

80125
private static var _eip4361OptionalPattern: String?
81126
private static var eip4361OptionalPattern: String {
@@ -95,7 +140,7 @@ public final class EIP4361 {
95140

96141
let patternParts: [String] = ["^\(domain)",
97142
"(\(address))?",
98-
"\(statementParagraph)",
143+
"((?<\(EIP4361Field.statement.rawValue)>\(statement))\\n)?",
99144
"(\(uri))?",
100145
"(\(version))?",
101146
"(\(chainId))?",
@@ -113,12 +158,7 @@ public final class EIP4361 {
113158

114159
public static func validate(_ message: String) -> EIP4361ValidationResponse {
115160
// swiftlint:disable force_try
116-
let siweConstantMessageRegex = try! NSRegularExpression(pattern: "^\(domain)\\n")
117-
guard siweConstantMessageRegex.firstMatch(in: message, range: message.fullNSRange) != nil else {
118-
return EIP4361ValidationResponse(isEIP4361: false, eip4361: nil, capturedFields: [:])
119-
}
120-
121-
let eip4361Regex = try! NSRegularExpression(pattern: eip4361OptionalPattern)
161+
let eip4361Regex = try! NSRegularExpression(pattern: EIP4361.eip4361OptionalPattern)
122162
// swiftlint:enable force_try
123163
var capturedFields: [EIP4361Field: String] = [:]
124164
for (key, value) in eip4361Regex.captureGroups(string: message) {
@@ -129,7 +169,7 @@ public final class EIP4361 {
129169
// swiftlint:enable force_unwrapping
130170
}
131171
return EIP4361ValidationResponse(isEIP4361: true,
132-
eip4361: EIP4361(message),
172+
eip4361: EIP4361(message),
133173
capturedFields: capturedFields)
134174
}
135175

@@ -167,6 +207,7 @@ public final class EIP4361 {
167207
let dateFormatter = ISO8601DateFormatter()
168208
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
169209
guard let domain = groups["domain"],
210+
!domain.isEmpty,
170211
let rawAddress = groups["address"],
171212
let address = EthereumAddress(rawAddress),
172213
let rawUri = groups["uri"],

Sources/web3swift/Utils/EIP/EIP681.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ extension Web3 {
6262
switch targetAddress {
6363
case .ethereumAddress(let ethereumAddress):
6464
address = ethereumAddress.address
65-
case let .ensAddress(ensAdress):
66-
address = ensAdress
65+
case let .ensAddress(ensAddress):
66+
address = ensAddress
6767
}
6868
var link = "ethereum:\(address)\(chainID != nil ? "@\(chainID!.description)" : "")"
6969
if let functionName = functionName, !functionName.isEmpty {
@@ -431,14 +431,14 @@ extension Web3 {
431431
var nativeValueArray: [Any] = []
432432

433433
for value in rawValues {
434-
let intermidiateValue = await parseFunctionArgument(type,
434+
let intermediateValue = await parseFunctionArgument(type,
435435
value,
436436
chainID: chainID,
437437
inputNumber: inputNumber)?
438438
.parameter
439439
.value
440-
if let intermidiateValue = intermidiateValue {
441-
nativeValueArray.append(intermidiateValue)
440+
if let intermediateValue = intermediateValue {
441+
nativeValueArray.append(intermediateValue)
442442
}
443443
}
444444
nativeValue = nativeValueArray

0 commit comments

Comments
 (0)