This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
/
SPVBlockchain.swift
78 lines (59 loc) · 2.78 KB
/
SPVBlockchain.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Foundation
import RxSwift
import HSHDWalletKit
class SPVBlockchain {
var ethereumAddress: String
var gasPriceInWei: Int = 0
var gasLimitEthereum: Int = 0
var gasLimitErc20: Int = 0
var syncState: EthereumKit.SyncState = .synced
weak var delegate: IBlockchainDelegate?
var peerGroup: IPeerGroup
let reachabilityManager: ReachabilityManager
let storage: ISPVStorage
init(storage: ISPVStorage, words: [String], network: INetwork, debugPrints: Bool = false) {
let hdWallet = HDWallet(seed: Mnemonic.seed(mnemonic: words), coinType: network.coinType, xPrivKey: network.privateKeyPrefix.bigEndian, xPubKey: network.publicKeyPrefix.bigEndian)
let addressKey = try! hdWallet.privateKey(account: 0, index: 0, chain: .external)
let publicKey = addressKey.publicKey(compressed: false).raw
let address = EIP55.encode(CryptoUtils.shared.sha3(publicKey.dropFirst()).suffix(20))
let addressData = Data(hex: String(address[address.index(address.startIndex, offsetBy: 2)...]))
let connectionKey = try! hdWallet.privateKey(account: 100, index: 100, chain: .external)
let connectionPublicKey = Data(connectionKey.publicKey(compressed: false).raw.suffix(from: 1))
let connectionECKey = ECKey(
privateKey: connectionKey.raw,
publicKeyPoint: ECPoint(nodeId: connectionPublicKey)
)
peerGroup = PeerGroup(network: Ropsten(), storage: storage, connectionKey: connectionECKey, address: addressData)
reachabilityManager = ReachabilityManager()
self.ethereumAddress = address
self.storage = storage
peerGroup.delegate = self
}
}
extension SPVBlockchain: IBlockchain {
func start() {
peerGroup.start()
}
func clear() {
}
func syncState(contractAddress: String) -> EthereumKit.SyncState {
return EthereumKit.SyncState.synced
}
func register(contractAddress: String) {
}
func unregister(contractAddress: String) {
}
func sendSingle(to address: String, amount: String, gasPriceInWei: Int?) -> Single<EthereumTransaction> {
let stubTransaction = EthereumTransaction(hash: "", nonce: 0, from: "", to: "", amount: "", gasLimit: 0, gasPriceInWei: 0)
return Single.just(stubTransaction)
}
func sendErc20Single(to address: String, contractAddress: String, amount: String, gasPriceInWei: Int?) -> Single<EthereumTransaction> {
let stubTransaction = EthereumTransaction(hash: "", nonce: 0, from: "", to: "", amount: "", gasLimit: 0, gasPriceInWei: 0)
return Single.just(stubTransaction)
}
}
extension SPVBlockchain: IPeerGroupDelegate {
func onUpdate(state: AccountState) {
delegate?.onUpdate(balance: state.balance.wei.asString(withBase: 10))
}
}