1
I am developing a Bitcoin wallet SPV and using 3rd party SDK for the same.
However, I tried to create wallet and it gets successfully created and I can also restore those wallets but amount and transactions not coming. Making amount always 0 as amount calculation depends on transactions.
//
// XORWallet.swift
//
//
// Created by Paresh Thakor on 06/07/19.
//
import UIKit
let KEY_MNEMONIC = "wallet_mnemonic"
class XORWallet: NSObject {
static let shared = XORWallet()
var peerGroup: WSPeerGroup?
var wallet: WSHDWallet?
func create(_ seed: WSSeed? = nil) -> WSHDWallet? {
let parameters: WSParameters = WSParametersForNetworkType(WSNetworkTypeMain)
var theSeed: WSSeed? = seed
parameters.scriptAddressVersion()
if theSeed == nil {
theSeed = (WSSeedGenerator.sharedInstance()?.generateRandomSeed())!
}
if let s = theSeed {
let store: WSBlockStore = WSMemoryBlockStore.init(parameters: parameters)
wallet = WSHDWallet.init(parameters: parameters, seed: s)
let downloader: WSBlockChainDownloader = WSBlockChainDownloader.init(store: store, wallet: wallet)
// wallet?.usedAddresses()
print(wallet?.usedAddresses()!)
print(wallet?.receiveAddress()!)
print(wallet?.sortedTransactions())
print(theSeed?.mnemonic()!)
let peerGroup: WSPeerGroup = WSPeerGroup.init(parameters: parameters)
peerGroup.startConnections()
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "WSPeerGroupDidStartDownload"), object: nil, queue: nil) {(note) in
print("Jay Mataji")
}
peerGroup.startDownload(with: downloader)
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "WSPeerGroupDidFinishDownload"), object: nil, queue: nil){(note) in
print("Completed")
}
self.peerGroup = peerGroup
}
return wallet
}
func generateSeed() -> WSSeed? {
let theSeed: WSSeed? = WSSeedGenerator.sharedInstance()?.generateRandomSeed()
return theSeed
}
func saveToUserDefaults() -> Bool {
if let w = wallet,
let m = w.seed()?.mnemonic() {
let def = UserDefaults.standard
def.set(m, forKey: KEY_MNEMONIC)
def.synchronize()
return true
}
return false
}
func loadSavedWallet() -> WSHDWallet? {
let def = UserDefaults.standard
if let m = def.string(forKey: KEY_MNEMONIC) {
let seed = WSSeed(mnemonic: m)
return create(seed)
}
return create()
}
}
Create a wallet like,
if let wallet = XORWallet.shared.create(XORWallet.shared.generateSeed()) {
print(wallet.allReceiveAddresses()!)
print(wallet.balance())
}
Please help me solving the issue as here wallet.balance() always comes 0. There is balance for this account.
I am new to bitcoin dev and may messed up something.
1Are you rescanning the blockchain once you create your wallet? Or in the case of SPV "replaying" the chain once all your addresses are added to the bloom filter? – pinhead – 2019-09-03T21:22:14.567
@pinhead I did not really get you. As mentioned in code, I am creating wallet by
create()and then access balance and address by specific methods. I don't get about bloom filters and "replaying" stuff. – Paresh Thakor – 2019-09-04T05:47:30.853