Skip to content
19 changes: 14 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", .upToNextMinor(from: "5.4.0")),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.5.1"),
.package(name: "secp256k1", url: "https://github.com/GigaBitcoin/secp256k1.swift", .upToNextMinor(from: "0.10.0"))
.package(url: "https://github.com/21-DOT-DEV/swift-secp256k1", from: "0.21.1")
],
targets: [
.target(
name: "Web3Core",
dependencies: ["BigInt", "secp256k1", "CryptoSwift"]
),
name: "Web3Core",
dependencies: [
"BigInt",
.product(name: "P256K", package: "swift-secp256k1"),
.product(name: "libsecp256k1", package: "swift-secp256k1"),
"CryptoSwift"
],
swiftSettings: [
.unsafeFlags(["-suppress-warnings"])
]
),

.target(
name: "web3swift",
dependencies: ["Web3Core", "BigInt", "secp256k1"],
dependencies: ["Web3Core", "BigInt", .product(name: "P256K", package: "swift-secp256k1")],
resources: [
.copy("./Browser/browser.js"),
.copy("./Browser/browser.min.js"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

import BigInt

/// TODO: should we do more error explain like ethers.js?
Expand Down
4 changes: 4 additions & 0 deletions Sources/Web3Core/EthereumNetwork/Request/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

import BigInt

public typealias Hash = String // 32 bytes hash of block (64 chars length without 0x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif


@available(iOS, obsoleted: 15.0, message: "Use the built-in API instead")
@available(macOS, obsoleted: 12.0, message: "Use the built-in API instead")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif


public struct EtherscanTransactionChecker: TransactionChecker {
private let urlSession: URLSessionProxy
Expand Down
3 changes: 2 additions & 1 deletion Sources/Web3Core/KeystoreManager/PlainKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//

import Foundation
import secp256k1
import P256K
import libsecp256k1

public class PlainKeystore: AbstractKeystore {

Expand Down
15 changes: 12 additions & 3 deletions Sources/Web3Core/Structure/SECP256k1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//

import Foundation
import secp256k1
import P256K
import libsecp256k1

public struct SECP256K1 {
public struct UnmarshaledSignature {
Expand Down Expand Up @@ -339,6 +340,7 @@ extension SECP256K1 {
}

internal static func randomBytes(length: Int) -> Data? {
#if canImport(Security)
for _ in 0...1024 {
var data = Data(repeating: 0, count: length)
let result = data.withUnsafeMutableBytes { mutableRBBytes -> Int32? in
Expand All @@ -351,11 +353,18 @@ extension SECP256K1 {
}
if let res = result, res == errSecSuccess {
return data
} else {
continue
}
}
return nil
#else
var rng = SystemRandomNumberGenerator()
var data = Data(repeating: 0, count: length)
data.withUnsafeMutableBytes { buf in
guard let base = buf.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return }
for i in 0..<length { base[i] = UInt8.random(in: 0...255, using: &rng) }
}
return data
#endif
}

internal static func toByteArray<T>(_ value: T) -> [UInt8] {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Web3Core/Structure/Web3ProviderProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif


public protocol Web3Provider {
var network: Networks? {get set}
Expand Down
9 changes: 6 additions & 3 deletions Sources/Web3Core/Utility/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ public extension Data {
- Returns: optional `Data` object containing the generated random bytes, or `nil` if an error occurred during generation.
*/
static func randomBytes(length: Int) -> Data? {
#if canImport(Security)
var entropyBytes = [UInt8](repeating: 0, count: length)
let status = SecRandomCopyBytes(kSecRandomDefault, entropyBytes.count, &entropyBytes)
guard status == errSecSuccess else {
return nil
}
guard status == errSecSuccess else { return nil }
return Data(entropyBytes)
#else
var rng = SystemRandomNumberGenerator()
return Data((0..<length).map { _ in UInt8.random(in: 0...255, using: &rng) })
#endif
}

func bitsInRange(_ startingBit: Int, _ length: Int) -> UInt64? { // return max of 8 bytes for simplicity, non-public
Expand Down
5 changes: 4 additions & 1 deletion Sources/web3swift/Browser/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2017 Samaritan. All rights reserved.
//

#if canImport(WebKit)
import WebKit

/// Bridge for WKWebView and JavaScript
Expand Down Expand Up @@ -189,7 +190,7 @@ extension Bridge: WKScriptMessageHandler {
public extension WKWebView {

private struct STPrivateStatic {
fileprivate static var bridgeKey = "STPrivateStatic.bridgeKey"
fileprivate static var bridgeKey: UInt8 = 0
}

/// Bridge for WKWebView and JavaScript. Initialize `lazy`
Expand Down Expand Up @@ -245,3 +246,5 @@ fileprivate extension WKWebView {
evaluateJavaScript(jsString, completionHandler: completionHandler)
}
}

#endif // canImport(WebKit)
2 changes: 2 additions & 0 deletions Sources/web3swift/Utils/EIP/EIP67Code.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//

import Foundation
#if canImport(CoreImage)
import CoreImage
#endif
import BigInt
import Web3Core

Expand Down
4 changes: 3 additions & 1 deletion Sources/web3swift/Utils/EIP/EIP681.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ extension Web3 {
case .ensAddress(let ens):
guard let chainID = chainID else { return nil }
do {
let web = await Web3(provider: InfuraProvider(.fromInt(UInt(chainID)))!)
let network = Networks.fromInt(UInt(chainID))
let infuraProvider = try await InfuraProvider(net: network)
let web = Web3(provider: infuraProvider)
let ensModel = ENS(web3: web)
try await ensModel?.setENSResolver(withDomain: ens)
let address = try await ensModel?.getAddress(forNode: ens)
Expand Down
4 changes: 4 additions & 0 deletions Sources/web3swift/Web3/Web3+HttpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

import BigInt
import Web3Core

Expand Down