Skip to content

Commit

Permalink
Merge pull request #132 from yuzushioh/support-private-net
Browse files Browse the repository at this point in the history
Support private net (local node)
  • Loading branch information
yuzushioh authored Jun 16, 2018
2 parents f693f8b + c18a779 commit e592148
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
4 changes: 4 additions & 0 deletions EthereumKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
AE35422F20300E4900007B80 /* EIP55.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE35422E20300E4900007B80 /* EIP55.swift */; };
AE3EB60320AC8D3600D61CE6 /* ERC20.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3EB60220AC8D3600D61CE6 /* ERC20.swift */; };
AE3EB60720AC941A00D61CE6 /* ERC20Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE3EB60620AC941A00D61CE6 /* ERC20Tests.swift */; };
AE53C96C20D4CF1C00714EF4 /* NetworkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE53C96B20D4CF1C00714EF4 /* NetworkTests.swift */; };
AE614679208744E600A9E77A /* HTTPClientType.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE614678208744E600A9E77A /* HTTPClientType.swift */; };
AE61467B20874FBE00A9E77A /* HTTPClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE61467A20874FBE00A9E77A /* HTTPClient.swift */; };
AE61467D20874FE700A9E77A /* Cancellable.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE61467C20874FE700A9E77A /* Cancellable.swift */; };
Expand Down Expand Up @@ -163,6 +164,7 @@
AE35422E20300E4900007B80 /* EIP55.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EIP55.swift; sourceTree = "<group>"; };
AE3EB60220AC8D3600D61CE6 /* ERC20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ERC20.swift; sourceTree = "<group>"; };
AE3EB60620AC941A00D61CE6 /* ERC20Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ERC20Tests.swift; sourceTree = "<group>"; };
AE53C96B20D4CF1C00714EF4 /* NetworkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkTests.swift; sourceTree = "<group>"; };
AE614678208744E600A9E77A /* HTTPClientType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClientType.swift; sourceTree = "<group>"; };
AE61467A20874FBE00A9E77A /* HTTPClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClient.swift; sourceTree = "<group>"; };
AE61467C20874FE700A9E77A /* Cancellable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cancellable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -365,6 +367,7 @@
isa = PBXGroup;
children = (
AE90A0771FFA698D005ED7AF /* EthereumKitTests.swift */,
AE53C96B20D4CF1C00714EF4 /* NetworkTests.swift */,
0AA12013206BFE800052AE8B /* CryptoTests.swift */,
AE0057372053A5E4007AA468 /* WalletTests.swift */,
AE0057352053A510007AA468 /* PrivateKeyAndPublicKeyTests.swift */,
Expand Down Expand Up @@ -612,6 +615,7 @@
AE005720204D7D59007AA468 /* RLPTests.swift in Sources */,
AE0057362053A510007AA468 /* PrivateKeyAndPublicKeyTests.swift in Sources */,
0AA12014206BFE800052AE8B /* CryptoTests.swift in Sources */,
AE53C96C20D4CF1C00714EF4 /* NetworkTests.swift in Sources */,
AE1F8C21208CDA0C0088A043 /* MessageSigningTests.swift in Sources */,
AE3EB60720AC941A00D61CE6 /* ERC20Tests.swift in Sources */,
AE90A0781FFA698D005ED7AF /* EthereumKitTests.swift in Sources */,
Expand Down
37 changes: 26 additions & 11 deletions EthereumKit/Helper/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@ public enum Network {
case main
case ropsten
case kovan
case `private`(chainID: Int)
case `private`(chainID: Int, testUse: Bool)

// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
public var coinType: UInt32 {
let mainnetCoinType = UInt32(60)
let testnetCoinType = UInt32(1)

switch self {
case .main:
return 60
case .ropsten, .kovan, .private:
return 1
return mainnetCoinType
case .ropsten, .kovan:
return testnetCoinType
case .private(_, let testUse):
return testUse ? testnetCoinType : mainnetCoinType
}
}

public var privateKeyPrefix: UInt32 {
let mainnetPrefix: UInt32 = 0x0488ade4
let testnetPrefix: UInt32 = 0x04358394

switch self {
case .main:
return 0x0488ade4
case .ropsten, .kovan, .private:
return 0x04358394
return mainnetPrefix
case .ropsten, .kovan:
return testnetPrefix
case .private(_, let testUse):
return testUse ? testnetPrefix : mainnetPrefix
}
}

public var publicKeyPrefix: UInt32 {
let mainnetPrefix: UInt32 = 0x0488b21e
let testnetPrefix: UInt32 = 0x043587cf

switch self {
case .main:
return 0x0488b21e
case .ropsten, .kovan, .private:
return 0x043587cf
return mainnetPrefix
case .ropsten, .kovan:
return testnetPrefix
case .private(_, let testUse):
return testUse ? testnetPrefix : mainnetPrefix
}
}

Expand All @@ -40,7 +55,7 @@ public enum Network {
return 3
case .kovan:
return 42
case .private(let chainID):
case .private(let chainID, _):
return chainID
}
}
Expand Down
45 changes: 45 additions & 0 deletions EthereumKitTests/NetworkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import XCTest
@testable import EthereumKit

final class NetworkTests: XCTestCase {

func testMainnet() {
let network = Network.main
XCTAssert(network.chainID == 1)
XCTAssert(network.coinType == 60)
XCTAssert(network.privateKeyPrefix == 0x0488ade4)
XCTAssert(network.publicKeyPrefix == 0x0488b21e)
}

func testRopsten() {
let network = Network.ropsten
XCTAssert(network.chainID == 3)
XCTAssert(network.coinType == 1)
XCTAssert(network.privateKeyPrefix == 0x04358394)
XCTAssert(network.publicKeyPrefix == 0x043587cf)
}

func testKovan() {
let network = Network.kovan
XCTAssert(network.chainID == 42)
XCTAssert(network.coinType == 1)
XCTAssert(network.privateKeyPrefix == 0x04358394)
XCTAssert(network.publicKeyPrefix == 0x043587cf)
}

func testPrivateNetTestUse() {
let network = Network.private(chainID: 100, testUse: true)
XCTAssert(network.chainID == 100)
XCTAssert(network.coinType == 1)
XCTAssert(network.privateKeyPrefix == 0x04358394)
XCTAssert(network.publicKeyPrefix == 0x043587cf)
}

func testPrivateNet() {
let network = Network.private(chainID: 100, testUse: false)
XCTAssert(network.chainID == 100)
XCTAssert(network.coinType == 60)
XCTAssert(network.privateKeyPrefix == 0x0488ade4)
XCTAssert(network.publicKeyPrefix == 0x0488b21e)
}
}

0 comments on commit e592148

Please sign in to comment.