From b7f7fe318eb3dcfd32f3077e2c38226ec7751ab8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 30 Nov 2018 16:43:20 +0900 Subject: [PATCH 1/2] Delete EthereumMessageSigner AppChain should not handle Ethereum signing. --- AppChain.xcodeproj/project.pbxproj | 8 --- Source/Signer/EthereumMessageSigner.swift | 59 ------------------- Source/Signer/MessageSigner.swift | 27 ++++++++- Tests/Signer/EthereumMessageSignerTests.swift | 51 ---------------- 4 files changed, 26 insertions(+), 119 deletions(-) delete mode 100644 Source/Signer/EthereumMessageSigner.swift delete mode 100644 Tests/Signer/EthereumMessageSignerTests.swift diff --git a/AppChain.xcodeproj/project.pbxproj b/AppChain.xcodeproj/project.pbxproj index ee924ea..7c92841 100644 --- a/AppChain.xcodeproj/project.pbxproj +++ b/AppChain.xcodeproj/project.pbxproj @@ -54,8 +54,6 @@ 1AD65F9F21229AF500AA4C84 /* Signer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD65F9E21229AF500AA4C84 /* Signer.swift */; }; 1AD65FA12122A1F900AA4C84 /* TransactionSendingResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD65FA02122A1F900AA4C84 /* TransactionSendingResult.swift */; }; 1AD65FA62122AF4400AA4C84 /* SignerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD65FA42122AF2F00AA4C84 /* SignerTest.swift */; }; - 893532EE217D7D9B00404C0B /* EthereumMessageSigner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 893532ED217D7D9B00404C0B /* EthereumMessageSigner.swift */; }; - 893532F2217DA03300404C0B /* EthereumMessageSignerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 893532F1217DA03300404C0B /* EthereumMessageSignerTests.swift */; }; 89733041217EB559001B8D93 /* MessageSigner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89733040217EB559001B8D93 /* MessageSigner.swift */; }; A761B95773C2431357D1B0B9 /* Pods_AppChain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A425B554C4D9A6E8284FEAC /* Pods_AppChain.framework */; }; /* End PBXBuildFile section */ @@ -124,8 +122,6 @@ 69F270D62F7F19A30770BD03 /* Pods-AppChainTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppChainTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AppChainTests/Pods-AppChainTests.debug.xcconfig"; sourceTree = ""; }; 6A425B554C4D9A6E8284FEAC /* Pods_AppChain.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AppChain.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7FF8D9768BC7235E22EC11C8 /* Pods-AppChain.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppChain.release.xcconfig"; path = "Pods/Target Support Files/Pods-AppChain/Pods-AppChain.release.xcconfig"; sourceTree = ""; }; - 893532ED217D7D9B00404C0B /* EthereumMessageSigner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumMessageSigner.swift; sourceTree = ""; }; - 893532F1217DA03300404C0B /* EthereumMessageSignerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumMessageSignerTests.swift; sourceTree = ""; }; 89733040217EB559001B8D93 /* MessageSigner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSigner.swift; sourceTree = ""; }; 8990CDB62160682F00205129 /* Config.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Config.json; sourceTree = ""; }; D9D78017E61E65184F73D1F8 /* Pods-AppChain.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AppChain.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AppChain/Pods-AppChain.debug.xcconfig"; sourceTree = ""; }; @@ -284,7 +280,6 @@ 1AD65F9E21229AF500AA4C84 /* Signer.swift */, 1A0529BF214258EC00C847CA /* Unsigner.swift */, 89733040217EB559001B8D93 /* MessageSigner.swift */, - 893532ED217D7D9B00404C0B /* EthereumMessageSigner.swift */, ); path = Signer; sourceTree = ""; @@ -303,7 +298,6 @@ children = ( 1AD65FA42122AF2F00AA4C84 /* SignerTest.swift */, 1A0529C1214279A500C847CA /* UnsignerTests.swift */, - 893532F1217DA03300404C0B /* EthereumMessageSignerTests.swift */, ); path = Signer; sourceTree = ""; @@ -545,7 +539,6 @@ 1A0D0C072185A8EF003E680D /* Data+Extension.swift in Sources */, 1A67ED50211D11C600DCE871 /* Request.swift in Sources */, 1A0D0C052185A861003E680D /* BloomFilter.swift in Sources */, - 893532EE217D7D9B00404C0B /* EthereumMessageSigner.swift in Sources */, 1AD65F9021217AC000AA4C84 /* DecodeUtils.swift in Sources */, 1AD65F9F21229AF500AA4C84 /* Signer.swift in Sources */, 1A67ED54211D122000DCE871 /* Method.swift in Sources */, @@ -569,7 +562,6 @@ files = ( 1A67ED3D211BEAFD00DCE871 /* HTTPProviderTests.swift in Sources */, 1A58853A211DC759007043F1 /* RPCTests.swift in Sources */, - 893532F2217DA03300404C0B /* EthereumMessageSignerTests.swift in Sources */, 1A0529C2214279A500C847CA /* UnsignerTests.swift in Sources */, 1AD65FA62122AF4400AA4C84 /* SignerTest.swift in Sources */, 1A64297F211C63E000352209 /* Config.swift in Sources */, diff --git a/Source/Signer/EthereumMessageSigner.swift b/Source/Signer/EthereumMessageSigner.swift deleted file mode 100644 index d2cb2ca..0000000 --- a/Source/Signer/EthereumMessageSigner.swift +++ /dev/null @@ -1,59 +0,0 @@ -// -// EthereumMessageSigner.swift -// AppChain -// -// Created by XiaoLu on 2018/10/22. -// Copyright © 2018 Cryptape. All rights reserved. -// - -import Foundation - -public enum SignError: Error { - case invalidPrivateKey - case invalidSignature -} - -// Ethereum Message Signer -public struct EthereumMessageSigner { - public init() {} - - public func signPersonalMessage(message: Data, privateKey: String, useExtraEntropy: Bool = true) throws -> String? { - return try sign(message: appendPersonalMessagePrefix(for: message), privateKey: privateKey, useExtraEntropy: useExtraEntropy) - } - - public func sign(message: Data, privateKey: String, useExtraEntropy: Bool = true) throws -> String? { - return try signHash(hashMessage(message), privateKey: privateKey, useExtraEntropy: useExtraEntropy).toHexString().addHexPrefix() - } - - public func hashMessage(_ message: Data) -> Data { - return message.sha3(.keccak256) - } - - public func hashPersonalMessage(_ personalMessage: Data) -> Data? { - return hashMessage(appendPersonalMessagePrefix(for: personalMessage)) - } -} - -private extension EthereumMessageSigner { - func signHash(_ hash: Data, privateKey: String, useExtraEntropy: Bool) throws -> Data { - guard let privateKeyData = Data.fromHex(privateKey) else { - throw SignError.invalidPrivateKey - } - let serializedSignature = Secp256k1.signForRecovery(hash: hash, privateKey: privateKeyData, useExtraEntropy: useExtraEntropy).serializedSignature - guard var signature = serializedSignature else { - throw SignError.invalidSignature - } - signature[64] += 27 - return signature - } - - func appendPersonalMessagePrefix(for message: Data) -> Data { - let prefix = "\u{19}Ethereum Signed Message:\n\(message.count)" - let prefixData = prefix.data(using: .ascii)! - if message.count >= prefixData.count && prefixData == message[0 ..< prefixData.count] { - return message - } else { - return prefixData + message - } - } -} diff --git a/Source/Signer/MessageSigner.swift b/Source/Signer/MessageSigner.swift index 4d50f7f..439a791 100644 --- a/Source/Signer/MessageSigner.swift +++ b/Source/Signer/MessageSigner.swift @@ -8,13 +8,18 @@ import Foundation +public enum SignError: Error { + case invalidPrivateKey + case invalidSignature +} + // AppChain Message Signer struct MessageSigner { public init() {} // TODO: AppChain sign personal message public func sign(message: Data, privateKey: String, useExtraEntropy: Bool = true) throws -> String? { - return try signHash(EthereumMessageSigner().hashMessage(message), privateKey: privateKey, useExtraEntropy: useExtraEntropy).toHexString().addHexPrefix() + return try signHash(hashMessage(message), privateKey: privateKey, useExtraEntropy: useExtraEntropy).toHexString().addHexPrefix() } private func signHash(_ hash: Data, privateKey: String, useExtraEntropy: Bool = true) throws -> Data { @@ -27,4 +32,24 @@ struct MessageSigner { } return signature } + + public func hashMessage(_ message: Data) -> Data { + return message.sha3(.keccak256) + } + + public func hashPersonalMessage(_ personalMessage: Data) -> Data? { + return hashMessage(appendPersonalMessagePrefix(for: personalMessage)) + } +} + +extension MessageSigner { + func appendPersonalMessagePrefix(for message: Data) -> Data { + let prefix = "\u{19}Ethereum Signed Message:\n\(message.count)" + let prefixData = prefix.data(using: .ascii)! + if message.count >= prefixData.count && prefixData == message[0 ..< prefixData.count] { + return message + } else { + return prefixData + message + } + } } diff --git a/Tests/Signer/EthereumMessageSignerTests.swift b/Tests/Signer/EthereumMessageSignerTests.swift deleted file mode 100644 index aa1def0..0000000 --- a/Tests/Signer/EthereumMessageSignerTests.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// EthereumMessageSignerTests.swift -// AppChainTests -// -// Created by XiaoLu on 2018/10/22. -// Copyright © 2018 Cryptape. All rights reserved. -// - -import XCTest -@testable import AppChain - -class EthereumMessageSignerTest: XCTestCase { - func testSignMessage() { - let privateKey = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" - let data = "This is a sweet message".data(using: .utf8)! - guard let sign = try? EthereumMessageSigner().sign(message: data, privateKey: privateKey, useExtraEntropy: false) else { - return XCTFail("Sign message failed") - } - XCTAssertEqual(sign, "0xd3ea800844eb6e1c05b208073777dee7c33d415c770278502226e437839766d23dfc9bbbdd8bf747392feb1dd12be8a020c77c4756c7838f4f032dbcc211798c1b") - } - - func testSignPersonalMessage() { - let privateKey = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" - let data = "This is a sweet message".data(using: .utf8)! - guard let sign = try? EthereumMessageSigner().signPersonalMessage(message: data, privateKey: privateKey, useExtraEntropy: false) else { - return XCTFail("Sign personal message failed") - } - XCTAssertEqual(sign, "0x3065bbd1cf6d80e7e60faf9688b9a75e6d52104a501168eccfa7ef51228c7b727f7a17dc87902d6619a7c00475bc62370dc123e6e71a6ae528d7674a4086614b1c") - } - - func testHashMessage() { - let fixtures: [Data: String] = [ - "hello world".data(using: .utf8)!: "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad", - Data.fromHex("0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")!: "0x04cd40a3ea7972c6f30142d02fd5ddcac438fe6c59e634cecb827fbee9d385fc", - ] - for (data, hash) in fixtures { - XCTAssertEqual(hash, EthereumMessageSigner().hashMessage(data).toHexString().addHexPrefix()) - } - } - - func testHashPersonalMessage() { - let fixtures: [Data: String] = [ - "hello world".data(using: .utf8)!: "0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68", - Data.fromHex("0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")!: "0x93100cc9477ba6522a2d7d5e83d0e075b167224ed8aa0c5860cfd47fa9f22797", - // Data.fromHex("0x7f23b5eed5bc7e89f267f339561b2697faab234a2")!: "0x06c9d148d268f9a13d8f94f4ce351b0beff3b9ba69f23abbf171168202b2dd67", see https://github.com/ethers-io/ethers.js/issues/85 - ] - for (data, hash) in fixtures { - XCTAssertEqual(hash, EthereumMessageSigner().hashPersonalMessage(data)!.toHexString().addHexPrefix()) - } - } -} From df8e17dab50a3e4834ea8b85d0164a36c3a18fc9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 30 Nov 2018 16:43:52 +0900 Subject: [PATCH 2/2] Bump version --- AppChainSwift.podspec | 2 +- Source/Info.plist | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AppChainSwift.podspec b/AppChainSwift.podspec index dfa0585..de79372 100644 --- a/AppChainSwift.podspec +++ b/AppChainSwift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "AppChainSwift" - s.version = "0.20.3" + s.version = "0.20.4" s.summary = "Nervos AppChain SDK implementation in Swift for iOS" s.description = <<-DESC diff --git a/Source/Info.plist b/Source/Info.plist index 82ac08e..bc3ff04 100644 --- a/Source/Info.plist +++ b/Source/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.20.3 + 0.20.4 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass