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)")
}
Add Base58Check
Remove publicKeyHashToAddress()
Refactor related directories
Refactor Encoding files
Break down address related files into smaller files
Introduce Address struct instead of protocol
Add BitcoinScheme
Add Address.VersionByte
Add Address.HashSize
Add Address.HashType (AddressType is renamed)
Deprecate Cashaddr, LegacyAddress
Add documentation for Address, Cashaddr, LegacyAddress
Rename Network.mainnet to Network.mainnetBCH
Rename Network.testnetBCH to Network.testnetBCH
Modify Address.cashaddr and .legacy to be computed properties
Benefits
Easy to put another address format to Address (ex. Segwit)
Address+Legacy.swift and Address+Cashaddr.swift are good example of implementing new formats.
Possible Drawbacks
Interface change like deprecate Address.publicKey may cause users to rewrite their codes. However, the public key is not the information which should be kept in Address. So those who need public key information with address should use PublicKey instead of Address.
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)After (v1.1.0 = Next Release) v1.0.2 code is also available, but deprecated
Future (v2.0.0 = Major Update) v1.0.2 code is also unavailable
Benefits
Address+Legacy.swift
andAddress+Cashaddr.swift
are good example of implementing new formats.Possible Drawbacks
Address.publicKey
may cause users to rewrite their codes. However, the public key is not the information which should be kept in Address. So those who need public key information with address should usePublicKey
instead ofAddress
.