yenom / BitcoinKit

Bitcoin protocol toolkit for Swift
MIT License
839 stars 261 forks source link

How to properly validate if any string is a valid bitcoin address? #246

Open djalfirevic opened 4 years ago

djalfirevic commented 4 years ago

Is there any method where you can post a string, and that it returns whether this is a valid address or not?

I've tried to use something like this

private func isValidAddress() -> Bool {
    if let _ = try? BitcoinAddress(cashaddr: address) {
        return true
    } else if let _ = try? BitcoinAddress(legacy: address) {
        return true
    }

    return true
}
SwenVanZanten commented 4 years ago

Thats actually the correct way of validating an address. Only I would change the last return and actually pass the address 😜