Skip to content

Commit

Permalink
Coin and blockchain improvements (#17)
Browse files Browse the repository at this point in the history
alejandro-isaza authored Jul 27, 2018
1 parent 680570c commit 4659b30
Showing 10 changed files with 106 additions and 38 deletions.
3 changes: 0 additions & 3 deletions Sources/Address.swift
Original file line number Diff line number Diff line change
@@ -13,9 +13,6 @@ public protocol Address: CustomStringConvertible {
/// Validates that the string is a valid address.
static func isValid(string: String) -> Bool

/// Coin this address is for.
var coin: Coin { get }

/// Raw representation of the address.
var data: Data { get }

3 changes: 0 additions & 3 deletions Sources/Bitcoin/BitcoinAddress.swift
Original file line number Diff line number Diff line change
@@ -48,9 +48,6 @@ public struct BitcoinAddress: Address, Hashable {
return true
}

/// Coin this address is for.
public let coin = Coin.bitcoin

/// Raw representation of the address.
public let data: Data

37 changes: 37 additions & 0 deletions Sources/Blockchain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright © 2017-2018 Trust.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

import Foundation

/// Blockchain types
public enum BlockchainType {
case bitcoin
case ethereum
}

/// Blockchains
public struct Blockchain: Equatable {
public var chainID: Int
public var type: BlockchainType

public init(chainID: Int, type: BlockchainType) {
self.chainID = chainID
self.type = type
}
}

extension Blockchain {
public static let bitcoin = Blockchain(chainID: 0, type: .bitcoin)

public static let ethereum = Blockchain(chainID: 1, type: .ethereum)
public static let ropsten = Blockchain(chainID: 3, type: .ethereum)
public static let rinkeby = Blockchain(chainID: 4, type: .ethereum)
public static let eosClassic = Blockchain(chainID: 20, type: .ethereum)
public static let kovan = Blockchain(chainID: 42, type: .ethereum)
public static let go = Blockchain(chainID: 60, type: .ethereum)
public static let ethereumClassic = Blockchain(chainID: 61, type: .ethereum)
public static let ethereumClassicTestnet = Blockchain(chainID: 62, type: .ethereum)
}
47 changes: 38 additions & 9 deletions Sources/Coin.swift
Original file line number Diff line number Diff line change
@@ -6,13 +6,42 @@

import Foundation

/// Supported coins.
/// Index based on https://github.com/satoshilabs/slips/blob/master/slip-0044.md
public enum Coin: Int {
case bitcoin = 0
case ethereum = 60
case ethereumClassic = 61
case poa = 178
case callisto = 820
case gochain = 6060
/// Coin types for Level 2 of BIP44.
///
/// - SeeAlso: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
public struct Coin: Equatable {
public var coinType: Int
public var blockchain: Blockchain

public init(coinType: Int, blockchain: Blockchain) {
self.coinType = coinType
self.blockchain = blockchain
}

public init(coinType: Int) {
self.coinType = coinType
switch coinType {
case Coin.bitcoin.coinType, Coin.bitcoinTestNet.coinType:
blockchain = .bitcoin
case Coin.ethereumTestNet.coinType:
blockchain = .ropsten
case Coin.ethereumClassic.coinType:
blockchain = .ethereumClassic
default:
blockchain = .ethereum
}
}
}

extension Coin {
public static let bitcoin = Coin(coinType: 0, blockchain: .bitcoin)
public static let bitcoinTestNet = Coin(coinType: 1, blockchain: .bitcoin)

public static let ethereum = Coin(coinType: 60, blockchain: .ethereum)
public static let ethereumTestNet = Coin(coinType: 1, blockchain: .ropsten)

public static let ethereumClassic = Coin(coinType: 61, blockchain: .ethereumClassic)
public static let poa = Coin(coinType: 178, blockchain: .ethereum)
public static let callisto = Coin(coinType: 820, blockchain: .ethereum)
public static let gochain = Coin(coinType: 6060, blockchain: .ethereum)
}
17 changes: 2 additions & 15 deletions Sources/Ethereum/EthereumAddress.swift
Original file line number Diff line number Diff line change
@@ -23,9 +23,6 @@ public struct EthereumAddress: Address, Hashable {
return string == eip55String
}

/// Coin this address is for.
public let coin: Coin

/// Raw address bytes, length 20.
public let data: Data

@@ -35,33 +32,23 @@ public struct EthereumAddress: Address, Hashable {
/// Creates an address with `Data`.
///
/// - Precondition: data contains exactly 20 bytes
public init?(data: Data, coin: Coin) {
public init?(data: Data) {
if !EthereumAddress.isValid(data: data) {
return nil
}
self.data = data
self.coin = coin
eip55String = EthereumAddress.computeEIP55String(for: data)
}

/// Creates an address with an hexadecimal string representation.
public init?(string: String, coin: Coin) {
public init?(string: String) {
guard let data = Data(hexString: string), data.count == Ethereum.addressSize else {
return nil
}
self.data = data
self.coin = coin
eip55String = EthereumAddress.computeEIP55String(for: data)
}

public init?(string: String) {
self.init(string: string, coin: .ethereum)
}

public init?(data: Data) {
self.init(data: data, coin: .ethereum)
}

public var description: String {
return eip55String
}
2 changes: 1 addition & 1 deletion Sources/HDWallet/HDWallet.swift
Original file line number Diff line number Diff line change
@@ -54,6 +54,6 @@ public class HDWallet {

extension Coin {
public func derivationPath(at index: Int) -> DerivationPath {
return DerivationPath(purpose: 44, coinType: self.rawValue, account: 0, change: 0, address: index)
return DerivationPath(purpose: 44, coinType: self.coinType, account: 0, change: 0, address: index)
}
}
10 changes: 3 additions & 7 deletions Sources/PrivateKey.swift
Original file line number Diff line number Diff line change
@@ -62,15 +62,11 @@ public final class PrivateKey: Hashable, CustomStringConvertible {
}

/// Public key.
public func publicKey(for coin: Coin) -> PublicKey {
switch coin {
public func publicKey(for type: BlockchainType) -> PublicKey {
switch type {
case .bitcoin:
return BitcoinPublicKey(data: Crypto.getPublicKey(from: data))!
case .ethereum,
.poa,
.ethereumClassic,
.callisto,
.gochain:
case .ethereum:
return EthereumPublicKey(data: Crypto.getPublicKey(from: data))!
}
}
15 changes: 15 additions & 0 deletions Tests/CoinTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright © 2017-2018 Trust.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.

import TrustCore
import XCTest

class CoinTests: XCTestCase {
func testCreateWithIDOnly() {
XCTAssertEqual(Coin(coinType: 0).blockchain, .bitcoin)
XCTAssertEqual(Coin(coinType: 60).blockchain, .ethereum)
}
}
2 changes: 2 additions & 0 deletions Tests/Ethereum/Solidity/ENS/ReverseResolverEncoderTests.swift
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ class ReverseResolverEncoderTests: XCTestCase {
let node = namehash(reverse_name)
XCTAssertEqual(node.hexString, "2103fd044150f573e47fcb48a7eedec6afd0911f9af1b0ff9167014ff22edd24")
let result = ReverseResolverEncoder.encodeSetName(node, name: ens_name)

// swiftlint:disable:next line_length
XCTAssertEqual(result.hexString, "773722132103fd044150f573e47fcb48a7eedec6afd0911f9af1b0ff9167014ff22edd2400000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000007666f6f2e65746800000000000000000000000000000000000000000000000000")
}
}
8 changes: 8 additions & 0 deletions TrustCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -53,6 +53,8 @@
61A728EC20F48A580005045D /* HDWalletTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61A728EB20F48A580005045D /* HDWalletTests.swift */; };
61D0BA292075D771004F71E1 /* ABIEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61D0BA282075D771004F71E1 /* ABIEncoder.swift */; };
61D0BA2D2075EF53004F71E1 /* ABIEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61D0BA2C2075EF53004F71E1 /* ABIEncoderTests.swift */; };
61DAB58A210AED1200BBB826 /* Blockchain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61DAB589210AED1200BBB826 /* Blockchain.swift */; };
61DAB58C210AF3DD00BBB826 /* CoinTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61DAB58B210AF3DD00BBB826 /* CoinTests.swift */; };
887FD54919AD2FA39E88EAF0 /* Pods_TrustCore_TrustCoreTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A1C9DA59AE410E04E68E1F89 /* Pods_TrustCore_TrustCoreTests.framework */; };
BB06149A920620338B3323D1 /* Pods_TrustCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 682B6374F9856982618A8501 /* Pods_TrustCore.framework */; };
BB3864A72090088D00CBF773 /* ENSEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3864A62090088D00CBF773 /* ENSEncoder.swift */; };
@@ -126,6 +128,8 @@
61A728EB20F48A580005045D /* HDWalletTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HDWalletTests.swift; sourceTree = "<group>"; };
61D0BA282075D771004F71E1 /* ABIEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ABIEncoder.swift; sourceTree = "<group>"; };
61D0BA2C2075EF53004F71E1 /* ABIEncoderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ABIEncoderTests.swift; sourceTree = "<group>"; };
61DAB589210AED1200BBB826 /* Blockchain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Blockchain.swift; sourceTree = "<group>"; };
61DAB58B210AF3DD00BBB826 /* CoinTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoinTests.swift; sourceTree = "<group>"; };
682B6374F9856982618A8501 /* Pods_TrustCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TrustCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
99258028638D3902B177E80C /* Pods-TrustCore.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TrustCore.debug.xcconfig"; path = "Pods/Target Support Files/Pods-TrustCore/Pods-TrustCore.debug.xcconfig"; sourceTree = "<group>"; };
A1C9DA59AE410E04E68E1F89 /* Pods_TrustCore_TrustCoreTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TrustCore_TrustCoreTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -193,6 +197,7 @@
615AE1E620F1DC1F00A5A0ED /* Crypto.m */,
615AE1E120F1D71000A5A0ED /* Address.swift */,
6185E71621096306000B4B79 /* BinaryCoding.swift */,
61DAB589210AED1200BBB826 /* Blockchain.swift */,
61A728E220F474A10005045D /* Coin.swift */,
610C52BC20F44CAC004A602C /* Data+Clear.swift */,
6149A6CB201E496900A2F35C /* Data+Hex.swift */,
@@ -218,6 +223,7 @@
6149A6C3201E43E200A2F35C /* Tests */ = {
isa = PBXGroup;
children = (
61DAB58B210AF3DD00BBB826 /* CoinTests.swift */,
61645D132058A74E00411565 /* CryptoTests.swift */,
6149A6CD201E499B00A2F35C /* DataHexTests.swift */,
611EFEC420F5BD8700A00E53 /* DerivationPathTests.swift */,
@@ -550,6 +556,7 @@
615AE1D320F1D1DF00A5A0ED /* BitcoinAddress.swift in Sources */,
610C52BD20F44CAC004A602C /* Data+Clear.swift in Sources */,
61A728D320F46ECF0005045D /* String+Clear.swift in Sources */,
61DAB58A210AED1200BBB826 /* Blockchain.swift in Sources */,
6185E71521095789000B4B79 /* BitcoinTransaction.swift in Sources */,
6149A6CA201E472000A2F35C /* EthereumTransaction.swift in Sources */,
615AE1E220F1D71000A5A0ED /* Address.swift in Sources */,
@@ -590,6 +597,7 @@
BB3864AD20904C3900CBF773 /* NamehashTests.swift in Sources */,
61675573207ADB9B00018DC8 /* FunctionTests.swift in Sources */,
BB3864A92090145600CBF773 /* ENSEncoderTests.swift in Sources */,
61DAB58C210AF3DD00BBB826 /* CoinTests.swift in Sources */,
61A728EC20F48A580005045D /* HDWalletTests.swift in Sources */,
61645D142058A74E00411565 /* CryptoTests.swift in Sources */,
6167558A207C7A1500018DC8 /* ERC20EncoderTests.swift in Sources */,

0 comments on commit 4659b30

Please sign in to comment.