Skip to content

Commit

Permalink
Merge pull request #42 from cryptape/make-message-signer-public
Browse files Browse the repository at this point in the history
Make message signer public
  • Loading branch information
XiaoLu authored Nov 30, 2018
2 parents 24d4966 + 2de6831 commit f15f39e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
2 changes: 1 addition & 1 deletion AppChainSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AppChainSwift"
s.version = "0.20.4"
s.version = "0.20.5"
s.summary = "Nervos AppChain SDK implementation in Swift for iOS"

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.20.4</string>
<string>0.20.5</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
27 changes: 5 additions & 22 deletions Source/Signer/MessageSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ public enum SignError: Error {
}

// AppChain Message Signer
struct MessageSigner {
public struct MessageSigner {
public init() {}

// TODO: AppChain sign personal message
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)
}

private func signHash(_ hash: Data, privateKey: String, useExtraEntropy: Bool = true) throws -> Data {
guard let privateKeyData = Data.fromHex(privateKey) else {
throw SignError.invalidPrivateKey
Expand All @@ -32,24 +35,4 @@ 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
}
}
}

0 comments on commit f15f39e

Please sign in to comment.