Skip to content

Commit

Permalink
Merge pull request #145 from D-Technologies/sig-message
Browse files Browse the repository at this point in the history
Add method to sign a message normally (not eth_personal message)
  • Loading branch information
yuzushioh authored Aug 2, 2018
2 parents 0b0d84f + 05963ec commit 9e6c758
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
50 changes: 40 additions & 10 deletions EthereumKit/Wallet/Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,31 @@ extension Wallet {
}
}

// MARK :- Sign personal message
// MARK :- Sign message

extension Wallet {

/// Sign a provided hex
///
/// - Parameter hex: hex value to sign (hex format)
/// - Returns: signature in string format
/// - Throws: EthereumKitError.failedToEncode when failed to encode
public func sign(hex: String) throws -> String {
let hash = Crypto.hashSHA3_256(Data(hex: hex.stripHexPrefix()))
return try key.sign(hash: hash).toHexString()
}

/// Sign a provided message
///
/// - Parameter message: message to sign (string format)
/// - Returns: signature in string format
/// - Throws: EthereumKitError.failedToEncode when failed to encode
public func sign(message: String) throws -> String {
return try sign(hex: message.toHexString())
}
}

// MARK :- Personal-sign message

extension Wallet {

Expand All @@ -127,7 +151,7 @@ extension Wallet {
/// - Parameter hex: message in hex format to sign
/// - Returns: signiture in hex format
/// - Throws: EthereumKitError.failedToEncode when failed to encode
public func sign(hex: String) throws -> String {
public func personalSign(hex: String) throws -> String {
let hash = generatePersonalMessageHash(hex: hex)
var signiture = try key.sign(hash: hash)

Expand All @@ -153,8 +177,8 @@ extension Wallet {
/// - Parameter hex: message to sign
/// - Returns: signiture in hex format
/// - Throws: EthereumKitError.failedToEncode when failed to encode
public func sign(message: String) throws -> String {
return try sign(hex: message.toHexString())
public func personalSign(message: String) throws -> String {
return try personalSign(hex: message.toHexString())
}
}

Expand All @@ -166,29 +190,35 @@ extension Wallet {
///
/// - Parameters:
/// - signature: signature in string format, must be signed with eth_personal_sign
/// - originalMessage: original message you signed
/// - message: message you signed
/// - compressed: whether a public key is compressed
/// - Returns: whether a signature is valid or not
public func verifySignature(personalSigned signature: String, originalMessage: String, compressed: Bool = false) -> Bool {
public func verify(personalSigned signature: String, message: String, compressed: Bool = false) -> Bool {
var sig = Data(hex: signature)

if sig[64] != 27 && sig[64] != 28 {
fatalError()
}

sig[64] = sig[64] - 27
return verifySignature(signature: sig, originalMessage: originalMessage, compressed: compressed)

let hash = generatePersonalMessageHash(hex: message.toHexString())
return verifySignature(signature: sig, hash: hash, compressed: compressed)
}

/// Verify a signature
///
/// - Parameters:
/// - signature: signature in data format
/// - originalMessage: original message you signed
/// - hash: hash of an message you signed
/// - compressed: whether a public key is compressed
/// - Returns: whether a signature is valid or not
public func verifySignature(signature: Data, originalMessage: String, compressed: Bool) -> Bool {
let hash = generatePersonalMessageHash(hex: originalMessage.toHexString())
public func verify(normalSigned signature: String, message: String, compressed: Bool = false) -> Bool {
let hash = Crypto.hashSHA3_256(Data(hex: message.toHexString().stripHexPrefix()))
return verifySignature(signature: Data(hex: signature), hash: hash, compressed: compressed)
}

private func verifySignature(signature: Data, hash: Data, compressed: Bool) -> Bool {
return Crypto.isValid(signature: signature, of: hash, publicKey: publicKey(), compressed: compressed)
}
}
12 changes: 6 additions & 6 deletions EthereumKitTests/MessageSigningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ final class MessageSigningTests: XCTestCase {

func testMessageSigning() {
XCTAssertEqual(
try! wallet.sign(message: "Ethereum"),
try! wallet.personalSign(message: "Ethereum"),
"0x0ec487cc67649c87f6ef059d21079c6e3023cd6b31b1e9b6ac82d1bff53f67e63a811b3e70f52459897b0bc03e5cdb3d482c982b2d4a4f68f17fd35c973473521c"
)

XCTAssertEqual(
try! wallet.sign(message: "Bitcoin"),
try! wallet.personalSign(message: "Bitcoin"),
"0xe3f0a3588aae404b6096e91b412387b7d9c775482fa5f980cc0f96cc11d37a00259f44bb4c38193b01b5179cd5a5a032be9a0143df9bd070f12416f09b05bab21c"
)

XCTAssertEqual(
try! wallet.sign(message: "hello world!"),
try! wallet.personalSign(message: "hello world!"),
"0xbf573bdc43a9d0f19e9035896609527cd0c8bf753b139c3f25c72751e5e0476243258d1945b952a2b7282d4e09ebe90ae1c0948325f1262262e4ee31bb5cd6851c"
)

XCTAssertEqual(
try! wallet.sign(message: "LLVM Profile Error Failed to write file default profraw Permission denie"),
try! wallet.personalSign(message: "LLVM Profile Error Failed to write file default profraw Permission denie"),
"0xf6d4779fe9cfd6217c65d8f65b583004f38787fc3faa3eb8f2683fe8d8c555844c83ddaaa82e09f23c822e6927d4987d140cfa3c8a4bb230007daaf438efe8401b"
)

XCTAssertEqual(
try! wallet.sign(hex: "0x57656c636f6d6520746f205765692077616c6c657421"),
try! wallet.personalSign(hex: "0x57656c636f6d6520746f205765692077616c6c657421"),
"0x1cdd3bc8ac888bb608d1951e803d6b255a8500dd857a2dee2b02dfd98df3a0df2db1c3199a86be6bfdd60a8c2182b70df76026dc1278f9dab3268bbc04fa660a1c"
)

XCTAssertEqual(
try! wallet.sign(hex: "0x4d6573736167655369676e696e675465737473"),
try! wallet.personalSign(hex: "0x4d6573736167655369676e696e675465737473"),
"0xeb2cd3bc3d704d4206f84e604b7c8fc20f7ed54eb5784c79d70e99993c7b6e6251d36e95b764a65f64a1f992e75e79f6fc750743a17fa6d508ac5d5f0b1e3df81c"
)
}
Expand Down

0 comments on commit 9e6c758

Please sign in to comment.