Skip to content

Commit a702fdb

Browse files
author
Alex Vlasov
committed
start an initial work on ObjC bridging
1 parent 0fda182 commit a702fdb

14 files changed

+363
-2
lines changed

web3swift.xcodeproj/project.pbxproj

Lines changed: 78 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// BigUInt+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import BigInt
11+
12+
@objc(BigUInt)
13+
public final class _ObjCBigUInt: NSObject{
14+
private (set) var biguint: BigUInt?
15+
16+
public init(value: String) {
17+
self.biguint = BigUInt(value)
18+
}
19+
20+
public init(value: String, radix: Int) {
21+
self.biguint = BigUInt(value, radix: radix)
22+
}
23+
24+
init(value: BigUInt) {
25+
self.biguint = value
26+
}
27+
28+
public func toString(radix: Int = 10) -> NSString {
29+
guard let val = self.biguint else {return "" as NSString}
30+
return String(val, radix: radix) as NSString
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// EthereumAddress+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(EthereumAddress)
12+
public final class _ObjCEthereumAddress: NSObject{
13+
private (set) var address: EthereumAddress?
14+
15+
public init(address: String) {
16+
self.address = EthereumAddress(address)
17+
}
18+
19+
public init(address: Data) {
20+
self.address = EthereumAddress(address)
21+
}
22+
23+
init(address: EthereumAddress) {
24+
self.address = address
25+
}
26+
27+
public static var contractDeploymentAddress: _ObjCEthereumAddress {
28+
return _ObjCEthereumAddress(address: EthereumAddress.contractDeploymentAddress())
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// KeystoreManager+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(KeystoreManager)
12+
public final class _ObjCKeystoreManager: NSObject{
13+
private (set) var keystoreManager: KeystoreManager?
14+
15+
init(plainKeystore: _ObjCPlainKeystore) {
16+
guard let ks = plainKeystore.keystore else {return}
17+
self.keystoreManager = KeystoreManager([ks])
18+
}
19+
20+
init(plainKeystore: PlainKeystore) {
21+
self.keystoreManager = KeystoreManager([plainKeystore])
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// PlainKeystore+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(PlainKeystore)
12+
public final class _ObjCPlainKeystore: NSObject{
13+
private (set) var keystore: PlainKeystore?
14+
15+
init(privateKey: String) {
16+
self.keystore = PlainKeystore(privateKey: privateKey)
17+
}
18+
19+
init(privateKey: Data) {
20+
self.keystore = PlainKeystore(privateKey: privateKey)
21+
}
22+
23+
init(privateKey: NSData) {
24+
self.keystore = PlainKeystore(privateKey: privateKey as Data)
25+
}
26+
27+
init(keystore: PlainKeystore) {
28+
self.keystore = keystore
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Web3+Eth+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(web3Eth)
12+
public final class _ObjCweb3Eth: NSObject {
13+
private (set) weak var web3: web3?
14+
15+
init(web3: web3?) {
16+
self.web3 = web3
17+
}
18+
19+
public func getBalance(address: _ObjCEthereumAddress, onBlock: NSString = "latest", error: NSErrorPointer) -> _ObjCBigUInt? {
20+
guard let addr = address.address else {
21+
error?.pointee = Web3Error.inputError("Address is empty") as NSError
22+
return nil
23+
}
24+
guard let result = self.web3?.eth.getBalance(address: addr, onBlock: onBlock as String) else {
25+
error?.pointee = Web3Error.processingError("Web3 object was not properly initialized") as NSError
26+
return nil
27+
}
28+
switch result {
29+
case .success(let balance):
30+
let biguint = _ObjCBigUInt(value: balance)
31+
return biguint
32+
case .failure(let web3error):
33+
error?.pointee = web3error as NSError
34+
return nil
35+
}
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Web3+Provider+ObjC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(Web3HttpProvider)
12+
final class _ObjCWeb3HttpProvider: NSObject{
13+
private (set) var web3Provider: Web3HttpProvider?
14+
15+
init(providerURL: NSURL, network: _ObjCNetwork, keystoreManager: _ObjCKeystoreManager){
16+
let network = Networks.fromInt(network.networkID)
17+
guard let ks = keystoreManager.keystoreManager else {return}
18+
self.web3Provider = Web3HttpProvider(providerURL as URL, network: network, keystoreManager: ks)
19+
}
20+
21+
init(web3Provider: Web3HttpProvider) {
22+
self.web3Provider = web3Provider
23+
}
24+
}
25+
26+
@objc(Network)
27+
final class _ObjCNetwork: NSObject {
28+
let networkID: Int
29+
30+
init(networkID: Int) {
31+
self.networkID = networkID
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Web3Instnace+ObjectiveC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(web3)
12+
public final class _ObjCweb3: NSObject {
13+
private (set) var web3: web3?
14+
15+
init(web3: web3?) {
16+
self.web3 = web3
17+
}
18+
19+
public var web3Eth: _ObjCweb3Eth {
20+
return _ObjCweb3Eth(web3: self.web3)
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Web3+ObjectiveC.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
@objc(Web3)
12+
public final class _ObjCWeb3: NSObject {
13+
public static func InfuraMainnetWeb3() -> _ObjCweb3 {
14+
let web3 = Web3.InfuraMainnetWeb3()
15+
return _ObjCweb3(web3: web3)
16+
}
17+
18+
public static func InfuraRinkebyWeb3() -> _ObjCweb3 {
19+
let web3 = Web3.InfuraRinkebyWeb3()
20+
return _ObjCweb3(web3: web3)
21+
}
22+
23+
public static func new(providerURL: NSURL) -> _ObjCweb3 {
24+
let web3 = Web3.new(providerURL as URL)
25+
return _ObjCweb3(web3: web3)
26+
}
27+
}
28+

web3swift/web3swift-Bridging-Header.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// web3swift-Bridging-Header.h
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 08.08.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
#ifndef web3swift_Bridging_Header_h
10+
#define web3swift_Bridging_Header_h
11+
12+
13+
#endif /* web3swift_Bridging_Header_h */

0 commit comments

Comments
 (0)