Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch btc address #5436

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 67 additions & 109 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ class BitcoinAdapter: BitcoinBaseAdapter {
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
case let .btcAddress(address, _, mnemonicDerivation):
bitcoinKit = try BitcoinKit.Kit(
watchAddress: address,
purpose: mnemonicDerivation.purpose,
walletId: wallet.account.id,
syncMode: syncMode,
networkType: networkType,
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
default:
throw AdapterError.unsupportedAccount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class BitcoinCashAdapter: BitcoinBaseAdapter {
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
case let .btcAddress(address, _, _):
bitcoinCashKit = try BitcoinCashKit.Kit(
watchAddress: address,
walletId: wallet.account.id,
syncMode: syncMode,
networkType: networkType,
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: nil
)
default:
throw AdapterError.unsupportedAccount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class DashAdapter: BitcoinBaseAdapter {
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
case let .btcAddress(address, _, _):
dashKit = try DashKit.Kit(
watchAddress: address,
walletId: wallet.account.id,
syncMode: syncMode,
networkType: networkType,
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
default:
throw AdapterError.unsupportedAccount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class ECashAdapter: BitcoinBaseAdapter {
confirmationsThreshold: Self.eCashConfirmationsThreshold,
logger: logger
)
case let .btcAddress(address, _, _):
eCashKit = try ECashKit.Kit(
watchAddress: address,
walletId: wallet.account.id,
syncMode: syncMode,
networkType: networkType,
confirmationsThreshold: Self.eCashConfirmationsThreshold,
logger: logger
)
default:
throw AdapterError.unsupportedAccount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ class LitecoinAdapter: BitcoinBaseAdapter {
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
case let .btcAddress(address, _, mnemonicDerivation):
litecoinKit = try LitecoinKit.Kit(
watchAddress: address,
purpose: mnemonicDerivation.purpose,
walletId: wallet.account.id,
syncMode: syncMode,
hasher: hasher,
networkType: networkType,
confirmationsThreshold: BitcoinBaseAdapter.confirmationsThreshold,
logger: logger
)
default:
throw AdapterError.unsupportedAccount
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,45 @@ extension AddressUriParser {
case uri(AddressUri)
}
}

extension BlockchainType {
var uriScheme: String? {
if EvmBlockchainManager.blockchainTypes.contains(self) {
return "ethereum"
}

switch self {
case .bitcoin: return "bitcoin"
case .bitcoinCash: return "bitcoincash"
case .ecash: return "ecash"
case .litecoin: return "litecoin"
case .dash: return "dash"
case .zcash: return "zcash"
case .ethereum: return "ethereum"
case .binanceChain: return "binancecoin"
case .tron: return "tron"
case .ton: return "toncoin"
default: return nil
}
}

var removeScheme: Bool {
if EvmBlockchainManager.blockchainTypes.contains(self) {
return true
}

switch self {
case .bitcoinCash: return false
case .ecash: return false
case .bitcoin: return true
case .litecoin: return true
case .dash: return true
case .zcash: return true
case .ethereum: return true
case .binanceChain: return true
case .tron: return true
case .ton: return true
default: return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class BinanceAddressParserItem {
switch parserType {
case let .adapter(adapter):
try adapter.validate(address: address)
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
case let .validator(validator):
try validator.validate(address: address)
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
}

} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ class BitcoinAddressParserItem {
switch parserType {
case let .adapter(adapter):
try adapter.validate(address: address, pluginData: [:]) // validate
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
case let .converter(converter):
let _ = try converter.convert(address: address)
return Single.just(Address(raw: address, domain: nil))
let btcAddress = try converter.convert(address: address)
guard let mnemonicDerivation = btcAddress.scriptType.mnemonicDerivation else {
throw ParseError.couldNotInfereDerivation
}
return Single.just(BitcoinAddress(raw: address, domain: nil, blockchainType: blockchainType, mnemonicDerivation: mnemonicDerivation))
}
} catch {
return Single.error(error)
Expand All @@ -47,4 +50,8 @@ extension BitcoinAddressParserItem {
case adapter(ISendBitcoinAdapter)
case converter(IAddressConverter)
}

enum ParseError: Error {
case couldNotInfereDerivation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension DashAddressParserItem: IAddressParserItem {
func handle(address: String) -> Single<Address> {
do {
try adapter.validate(address: address)
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
} catch {
return Single.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EnsAddressParserItem {
rawAddressParserItem
.handle(address: address.raw)
.map { rawAddress in
Address(raw: rawAddress.raw, domain: address.domain)
Address(raw: rawAddress.raw, domain: address.domain, blockchainType: address.blockchainType)
}
}
}
Expand All @@ -33,15 +33,15 @@ extension EnsAddressParserItem: IAddressParserItem {
let blockchainType = blockchainType
return provider.address(domain: address)
.flatMap { [weak self] resolvedAddress in
let address = Address(raw: resolvedAddress.hex, domain: address)
let address = Address(raw: resolvedAddress.hex, domain: address, blockchainType: blockchainType)
return self?.rawAddressHandle(address: address) ?? Single.just(address)
}.catchError { _ in
.error(AddressService.AddressError.invalidAddress(blockchainName: blockchainType.uid))
}
}

func isValid(address: String) -> Single<Bool> {
let parts = address.components(separatedBy: ".")
let parts = address.trimmingCharacters(in: .whitespaces).components(separatedBy: ".")
if parts.count > 1,
let last = parts.last?.lowercased()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EvmAddressParser: IAddressParserItem {
func handle(address: String) -> Single<Address> {
do {
let address = try EvmKit.Address(hex: address)
return Single.just(Address(raw: address.hex))
return Single.just(Address(raw: address.hex, blockchainType: blockchainType))
} catch {
return Single.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import MarketKit
import RxSwift
import TonKitKmm

class TonAddressParserItem: IAddressParserItem {
private let adapter: ISendTonAdapter

init(adapter: ISendTonAdapter) {
self.adapter = adapter
}

var blockchainType: BlockchainType { .ton }
var blockchainType: MarketKit.BlockchainType = .ton

func handle(address: String) -> Single<Address> {
do {
try adapter.validate(address: address)
return Single.just(Address(raw: address))
try TonKit.companion.validate(address: address)
return Single.just(Address(raw: address, blockchainType: blockchainType))
} catch {
return Single.error(error)
}
}

func isValid(address: String) -> Single<Bool> {
do {
try adapter.validate(address: address)
try TonKit.companion.validate(address: address)
return Single.just(true)
} catch {
return Single.just(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TronAddressParser: IAddressParserItem {
func handle(address: String) -> Single<Address> {
do {
let address = try TronKit.Address(address: address)
return Single.just(Address(raw: address.base58))
return Single.just(Address(raw: address.base58, blockchainType: blockchainType))
} catch {
return Single.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UdnAddressParserItem {
rawAddressParserItem
.handle(address: address.raw)
.map { rawAddress in
Address(raw: rawAddress.raw, domain: address.domain)
Address(raw: rawAddress.raw, domain: address.domain, blockchainType: address.blockchainType)
}
}
}
Expand All @@ -57,10 +57,10 @@ extension UdnAddressParserItem: IAddressParserItem {
}

return resolve(singles: singles)
.flatMap { [weak self] result in
.flatMap { [weak self, blockchainType] result in
switch result {
case let .success(resolvedAddress):
let address = Address(raw: resolvedAddress, domain: address)
let address = Address(raw: resolvedAddress, domain: address, blockchainType: blockchainType)
return self?.rawAddressHandle(address: address) ?? Single.just(address)
case let .failure(error):
return Single.error(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class ZcashAddressParserItem {
switch parserType {
case let .adapter(adapter):
_ = try adapter.validate(address: address, checkSendToSelf: checkSendToSelf)
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
case let .validator(validator):
try validator.validate(address: address)
return Single.just(Address(raw: address, domain: nil))
return Single.just(Address(raw: address, domain: nil, blockchainType: blockchainType))
}

} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import HsCryptoKit
import HsExtensions
import Scrypt

class BackupCryptoHelper {
enum BackupCryptoHelper {
static let defaultCypher = "aes-128-ctr"
static let defaultKdf = "scrypt"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import LitecoinKit
import MarketKit
import ZcashLightClientKit

class AddressParserFactory {
enum AddressParserFactory {
static let uriBlockchainTypes: [BlockchainType] = [
.bitcoin,
.bitcoinCash,
Expand Down Expand Up @@ -110,7 +110,8 @@ class AddressParserFactory {

return [zcashParserItem]
case .solana: return []
case .ton: return []
case .ton:
return [TonAddressParserItem()]
case .unsupported: return []
}
}
Expand All @@ -128,43 +129,3 @@ class AddressParserFactory {
return AddressParserChain().append(handlers: handlers)
}
}

extension BlockchainType {
var uriScheme: String? {
if EvmBlockchainManager.blockchainTypes.contains(self) {
return "ethereum"
}

switch self {
case .bitcoin: return "bitcoin"
case .bitcoinCash: return "bitcoincash"
case .ecash: return "ecash"
case .litecoin: return "litecoin"
case .dash: return "dash"
case .zcash: return "zcash"
case .ethereum: return "ethereum"
case .binanceChain: return "binancecoin"
case .tron: return "tron"
default: return nil
}
}

var removeScheme: Bool {
if EvmBlockchainManager.blockchainTypes.contains(self) {
return true
}

switch self {
case .bitcoinCash: return false
case .ecash: return false
case .bitcoin: return true
case .litecoin: return true
case .dash: return true
case .zcash: return true
case .ethereum: return true
case .binanceChain: return true
case .tron: return true
default: return false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

class FaqUrlHelper {
enum FaqUrlHelper {
static var privateKeysUrl: URL? {
URL(string: "faq/en/management/what-are-private-keys-mnemonic-phrase-wallet-seed.md", relativeTo: AppConfig.faqIndexUrl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import RxRelay
import RxSwift

class BtcBlockchainManager {
private let blockchainTypes: [BlockchainType] = [
static let blockchainTypes: [BlockchainType] = [
.bitcoin,
.bitcoinCash,
.ecash,
Expand All @@ -25,7 +25,7 @@ class BtcBlockchainManager {
self.storage = storage

do {
allBlockchains = try marketKit.blockchains(uids: blockchainTypes.map(\.uid))
allBlockchains = try marketKit.blockchains(uids: Self.blockchainTypes.map(\.uid))
} catch {
allBlockchains = []
}
Expand Down Expand Up @@ -82,7 +82,7 @@ extension BtcBlockchainManager {

extension BtcBlockchainManager {
var backup: [BtcRestoreModeBackup] {
blockchainTypes.map {
Self.blockchainTypes.map {
BtcRestoreModeBackup(
blockchainTypeUid: $0.uid,
restoreMode: restoreMode(blockchainType: $0).rawValue,
Expand Down
Loading