-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
protocol MessageSignatureVerifying { | ||
func verify(signature: String, message: String, address: String) throws | ||
} | ||
|
||
protocol MessageSignatureSigning { | ||
func sign(message: String, privateKey: Data) throws -> String | ||
} | ||
|
||
struct MessageSigner: MessageSignatureVerifying & MessageSignatureSigning { | ||
|
||
enum Errors: Error { | ||
case signatureValidationFailed | ||
case utf8EncodingFailed | ||
} | ||
|
||
private let signer: Signer | ||
|
||
init(signer: Signer) { | ||
self.signer = signer | ||
} | ||
|
||
func sign(message: String, privateKey: Data) throws -> String { | ||
guard let messageData = message.data(using: .utf8) else { throw Errors.utf8EncodingFailed } | ||
let signature = try signer.sign(message: messageData, with: privateKey) | ||
return signature.toHexString() | ||
} | ||
|
||
func verify(signature: String, message: String, address: String) throws { | ||
guard let messageData = message.data(using: .utf8) else { throw Errors.utf8EncodingFailed } | ||
let signatureData = Data(hex: signature) | ||
guard try signer.isValid(signature: signatureData, message: messageData, address: address) | ||
else { throw Errors.signatureValidationFailed } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters