Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit a0c1019

Browse files
committed
ObjectiveC Update!
W3 classes now automatically casts to swift classes in transaction arguments Added SolidityFunction support Added hex string to data and data to hex string functions Added W3UInt init with decimals You can now easily add accounts with mnemonics and private key using [web3 addAccountWithMnemonics:password:] and [web3 addAccountWithPrivateKey:password:]
1 parent 514b538 commit a0c1019

8 files changed

+93
-18
lines changed

Cartfile.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
github "attaswift/BigInt" "v3.1.0"
22
github "attaswift/SipHash" "v1.2.2"
3-
github "mxcl/PromiseKit" "6.7.0"
3+
github "mxcl/PromiseKit" "6.7.1"

Sources/web3swift/ObjectiveC/Bridging.swift

+32-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,43 @@
88

99
import Foundation
1010

11-
public protocol SwiftBridgeable {
11+
12+
// This protocol allows you to init objc class with its original swift class
13+
public protocol SwiftContainer: SwiftBridgeable {
14+
init(_ swift: SwiftType)
15+
}
16+
17+
public protocol SwiftBridgeable: SwiftBridgeableHack {
1218
associatedtype SwiftType
1319
var swift: SwiftType { get }
1420
}
15-
public protocol SwiftContainer: SwiftBridgeable {
16-
init(_ swift: SwiftType)
21+
22+
// This protocol allows you to get swift value as Any.
23+
// Without this protocol .toSwift() function cannot convert Any to SwiftBridgeable
24+
// because it contains associatedtype
25+
public protocol SwiftBridgeableHack {
26+
var __swift: Any { get }
27+
}
28+
extension SwiftBridgeable {
29+
public var __swift: Any { return swift }
30+
}
31+
func toSwift(_ any: Any) -> Any {
32+
if let string = any as? String {
33+
return string
34+
} else if let objc = any as? SwiftBridgeableHack {
35+
return objc.__swift
36+
} else {
37+
return any
38+
}
1739
}
1840

41+
extension Array where Element: Any {
42+
var swift: [Any] {
43+
return map(toSwift)
44+
}
45+
}
46+
47+
1948
//public extension _ObjectiveCBridgeable where _ObjectiveCType: SwiftBridgeable, _ObjectiveCType.SwiftType == Self {
2049
// static func _forceBridgeFromObjectiveC(_ source: _ObjectiveCType, result: inout Self?) {
2150
// result = source.swift

Sources/web3swift/ObjectiveC/W3Contract.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ extension ContractProtocol {
7676

7777
@objc public func deploy(bytecode: Data, parameters: [Any], extraData: Data?, options: W3Options?) throws -> W3EthereumTransaction {
7878
let extraData = extraData ?? Data()
79-
return try swift.deploy(bytecode: bytecode, parameters: parameters, extraData: extraData, options: options?.swift).objc
79+
return try swift.deploy(bytecode: bytecode, parameters: parameters.swift, extraData: extraData, options: options?.swift).objc
8080
}
8181

8282
@objc public func method(_ method: String, parameters: [Any], extraData: Data?, options: W3Options?) throws -> W3EthereumTransaction {
8383
let extraData = extraData ?? Data()
84-
return try swift.method(method, parameters: parameters, extraData: extraData, options: options?.swift).objc
84+
return try swift.method(method, parameters: parameters.swift, extraData: extraData, options: options?.swift).objc
8585
}
8686

8787
@objc public func parseEvent(_ eventLog: W3EventLog) -> W3ContractParsedEvent {
@@ -105,4 +105,3 @@ extension ContractProtocol {
105105
return swift.decodeInputData(data)
106106
}
107107
}
108-

Sources/web3swift/ObjectiveC/W3JsonRpc.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ extension JsonRpcParams {
163163

164164
@objc public var params: [Any] {
165165
get { return swift.params }
166-
set { swift.params = newValue }
166+
set { swift.params = newValue.swift }
167167
}
168168
}
169169

Sources/web3swift/ObjectiveC/W3Structs.swift

+27-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
import Foundation
1010

11+
@objc public extension NSData {
12+
@objc func hexString() -> NSString {
13+
return (self as Data).hex as NSString
14+
}
15+
}
16+
17+
@objc public extension NSString {
18+
@objc func hexData() -> NSData? {
19+
return (try? (self as String).dataFromHex()) as NSData?
20+
}
21+
}
22+
1123
var opt: ObjcError {
1224
return .returnsOptionalValue
1325
}
@@ -44,11 +56,11 @@ extension Address.AddressType {
4456
public required init(_ swift: Address) {
4557
self.swift = swift
4658
}
47-
@objc public init(string: String, type: W3AddressType = .normal) {
48-
swift = Address(string, type: type.swift)
59+
@objc public init(string: String) {
60+
swift = Address(string, type: .normal)
4961
}
50-
@objc public init(data: Data, type: W3AddressType = .normal) {
51-
swift = Address(data, type: type.swift)
62+
@objc public init(data: Data) {
63+
swift = Address(data, type: .normal)
5264
}
5365
@objc public var isValid: Bool {
5466
return swift.isValid
@@ -78,9 +90,19 @@ extension Address.AddressType {
7890
@objc public override var description: String {
7991
return swift.description
8092
}
93+
94+
@objc public func call(method: String, arguments: [Any]) throws -> W3SolidityDataReader {
95+
return try swift.call(method, arguments.swift).wait().objc
96+
}
97+
@objc public func send(method: String, arguments: [Any], password: String, options: W3Options?) throws -> W3TransactionSendingResult {
98+
return try swift.send(method, arguments.swift, password: password, web3: .default, options: options?.swift).wait().objc
99+
}
81100
}
82101

83102
@objc public extension NSString {
103+
var address: W3Address {
104+
return W3Address(string: self as String)
105+
}
84106
var isContractAddress: Bool {
85107
return (self as String).hex.count > 0
86108
}
@@ -90,7 +112,7 @@ extension Address.AddressType {
90112
}
91113

92114
var contractAddress: W3Address {
93-
return W3Address(string: self as String, type: .contractDeployment)
115+
return W3Address(string: self as String)
94116
}
95117
}
96118

Sources/web3swift/ObjectiveC/W3TransactionIntermediate.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import Foundation
1010

1111
extension SolidityDataReader {
12-
public var objc: W3DataResponse {
13-
return W3DataResponse(self)
12+
public var objc: W3SolidityDataReader {
13+
return W3SolidityDataReader(self)
1414
}
1515
}
1616

17-
@objc public class W3DataResponse: NSObject, SwiftContainer {
17+
@objc public class W3SolidityDataReader: NSObject, SwiftContainer {
1818
public let swift: SolidityDataReader
1919
public required init(_ swift: SolidityDataReader) {
2020
self.swift = swift

Sources/web3swift/ObjectiveC/W3UInt.swift

+13-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ extension BigUInt {
106106
self.value = value
107107
}
108108

109-
public init?(_ string: String, decimals: Int) {
109+
@objc public init?(_ string: String, decimals: Int) {
110110
guard let value = BigUInt(string, decimals: decimals) else { return nil }
111111
self.value = value
112112
}
@@ -126,14 +126,21 @@ extension BigUInt {
126126
@objc public func string(unitDecimals: Int, decimals: Int = 18, decimalSeparator: String = ".", options: W3StringOptions = .default) -> String {
127127
return value.string(unitDecimals: unitDecimals, decimals: decimals, decimalSeparator: decimalSeparator, options: options.swift)
128128
}
129+
130+
public override var description: String {
131+
return swift.description
132+
}
129133
}
130134

131135
extension BigInt {
132136
public var objc: W3Int {
133137
return W3Int(value: self)
134138
}
135139
}
136-
@objc public class W3Int: NSObject {
140+
@objc public class W3Int: NSObject, SwiftBridgeable {
141+
public var swift: BigInt {
142+
return value
143+
}
137144
let value: BigInt
138145
init(value: BigInt) {
139146
self.value = value
@@ -220,6 +227,10 @@ extension BigInt {
220227
@objc public func string(units: W3Units, decimals: Int = 18, decimalSeparator: String = ".", options: W3StringOptions = .default) -> String {
221228
return value.string(units: units.swift, decimals: decimals, decimalSeparator: decimalSeparator, options: options.swift)
222229
}
230+
231+
public override var description: String {
232+
return swift.description
233+
}
223234
}
224235

225236
extension Web3Units {

Sources/web3swift/ObjectiveC/W3Web3.swift

+14
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,18 @@ extension Web3 {
9494
super.init()
9595
options = W3Options(object: self)
9696
}
97+
98+
@objc public func addAccount(mnemonics: String, password: String) throws -> W3Address {
99+
let mnemonics = try Mnemonics(mnemonics)
100+
let keystore = try BIP32Keystore(mnemonics: mnemonics, password: password)
101+
keystoreManager.swift.append(keystore)
102+
return keystore.addresses.first!.objc
103+
}
104+
105+
@objc public func addAccount(privateKey: Data, password: String) throws -> W3Address {
106+
guard let keystore = try EthereumKeystoreV3(privateKey: privateKey, password: password)
107+
else { throw SECP256K1Error.invalidPrivateKeySize }
108+
keystoreManager.swift.append(keystore)
109+
return keystore.addresses.first!.objc
110+
}
97111
}

0 commit comments

Comments
 (0)