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

♻️ Refactor address #232

Merged
merged 5 commits into from
Sep 21, 2019
Merged

♻️ Refactor address #232

merged 5 commits into from
Sep 21, 2019

Conversation

usatie
Copy link
Member

@usatie usatie commented Sep 20, 2019

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)")
}
  • 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.

- Add Base58Check
- Remove publicKeyHashToAddress()
- Refactor related directories
- Refactor Encoding files
@usatie usatie added the enhancement New feature or request label Sep 20, 2019
@usatie
Copy link
Member Author

usatie commented Sep 20, 2019

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

@usatie usatie changed the title Refactor address 🚧 [WIP] Refactor address Sep 20, 2019
@usatie usatie force-pushed the refactor-address branch 2 times, most recently from 5a36a29 to f385670 Compare September 21, 2019 02:42
@usatie usatie changed the title 🚧 [WIP] Refactor address ♻️ Refactor address Sep 21, 2019
@usatie
Copy link
Member Author

usatie commented Sep 21, 2019

Refactorization is finished.

- Braek 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
Copy link
Member Author

@usatie usatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@usatie usatie merged commit fd1d400 into master Sep 21, 2019
@usatie usatie deleted the refactor-address branch September 21, 2019 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant