yenom / BitcoinKit

Bitcoin protocol toolkit for Swift
MIT License
844 stars 262 forks source link

♻️ Refactor address #232

Closed usatie closed 5 years ago

usatie commented 5 years ago

Description of the Change

Refactor Address, LegacyAddress, Cashaddr, Base58Check, Bech32. Before, Address was protocol, but it will be a struct in version 2.0.0. Before (v1.0.2 = Latest Release)

// Initialize from legacy address (Base58Check format) string
let legacy = try LegacyAddress("1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN")

// Initialize from cash address (Cashaddr format) string
let cashaddr = try Cashaddr("bitcoincash:qpjdpjrm5zvp2al5u4uzmp36t9m0ll7gd525rss978")

// Initialize from data
let p2pkhCashaddr = Cashaddr(data: pubkeyHash, type: .pubkeyHash, network: .mainnetBCH)
let p2shCashaddr = Cashaddr(data: scriptHash, type: .scriptHash, network: .mainnetBCH)
let p2pkhLegacyAddress = LegacyAddress(data: pubkeyHash, type: .pubkeyHash, network: .mainnetBCH)
let p2shCashaddr = LegacyAddress(data: scriptHash, type: .scriptHash, network: .mainnetBCH)

// Initialize from any format
let address1 = try AddressFactory.create("1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN")
let address2 = try AddressFactory.create("bitcoincash:qpjdpjrm5zvp2al5u4uzmp36t9m0ll7gd525rss978")

After (v1.1.0 = Next Release) v1.0.2 code is also available, but deprecated

// Initialize from legacy address (Base58Check format) string
let legacy = try BitcoinAddress(legacy: "1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN")

// Initialize from cash address (Cashaddr format) string
let cashaddr = try BitcoinAddress(cashaddr: "bitcoincash:qpjdpjrm5zvp2al5u4uzmp36t9m0ll7gd525rss978")

// Initialize from data
let p2pkhAddress = try BitcoinAddress(data: pubkeyHash, type: .pubkeyHash, network: .mainnetBCH)
let p2shAddress = try BitcoinAddress(data: scriptHash, type: .scriptHash, network: .mainnetBCH)

// Initialize from any format
let addressText = "1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN"
let address: BitcoinAddress
do {
    do {
        address = try BitcoinAddress(legacy: addressText)
    } catch {
        address = try BitcoinAddress(cashaddr: addressText)
    }
} catch {
    print("Address could not be initialized from \(addressText)")
}

Future (v2.0.0 = Major Update) v1.0.2 code is also unavailable

// Initialize from legacy address (Base58Check format) string
let legacy = try Address(legacy: "1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN")

// Initialize from cash address (Cashaddr format) string
let cashaddr = try Address(cashaddr: "bitcoincash:qpjdpjrm5zvp2al5u4uzmp36t9m0ll7gd525rss978")

// Initialize from data
let p2pkhAddress = try Address(data: pubkeyHash, type: .pubkeyHash, network: .mainnetBCH)
let p2shAddress = try Address(data: scriptHash, type: .scriptHash, network: .mainnetBCH)

// Initialize from any format
let addressText = "1AC4gh14wwZPULVPCdxUkgqbtPvC92PQPN"
let address: Address
do {
    do {
        address = try Address(legacy: addressText)
    } catch {
        address = try Address(cashaddr: addressText)
    }
} catch {
    print("Address could not be initialized from \(addressText)")
}

Benefits

Possible Drawbacks

usatie commented 5 years ago

I realized that the change is too destructive. I rewrite Address as a new struct BitcoinAddress.

usatie commented 5 years ago

Refactorization is finished.