Skip to content

Commit

Permalink
Add isHex function and sign function support
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Mar 13, 2019
1 parent 3600c0b commit 4f99685
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Sources/Utils/Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public final class Hex {
return isHex(string.tk_substring(from: 2))
}

if string.count % 2 != 0 {
return false
}

let regex = "^[A-Fa-f0-9]+$"
let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
return predicate.evaluate(with: string)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Wallet/WalletManager+EOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public extension WalletManager {
return try EOSTransactionSigner(txs: txs, keystore: wallet.keystore, password: password).sign()
}

public static func eosEcSign(walletID: String, data: String, publicKey: String?, password: String) throws -> String {
public static func eosEcSign(walletID: String, data: String, isHex: Bool, publicKey: String?, password: String) throws -> String {
guard let wallet = Identity.currentIdentity?.findWalletByWalletID(walletID) else {
throw GenericError.walletNotFound
}
Expand All @@ -93,15 +93,15 @@ public extension WalletManager {
throw "Only EOS wallet can invoke the eosEcSign"
}

guard let hashedData = data.tk_data()?.sha256() else {
guard let hashedData = (isHex ? data.tk_dataFromHexString(): data.data(using: .utf8)?.sha256()) else {
throw "Data shoud be string or hex"
}

return EOSTransaction.signatureBase58(data: eosKey.sign(data: hashedData))
}

public static func eosEcRecover(data: String, signature: String) throws -> String {
guard let hashedData = data.tk_data()?.sha256() else {
public static func eosEcRecover(data: String, isHex: Bool, signature: String) throws -> String {
guard let hashedData = (isHex ? data.tk_dataFromHexString(): data.data(using: .utf8)?.sha256()) else {
throw "Data shoud be string or hex"
}

Expand Down
19 changes: 16 additions & 3 deletions Tests/Foundation/EOS/EOSKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,30 @@ class EOSKeyTests: TestCase {
do {
let meta = WalletMeta(chain: .eos, source: .wif)
let eosWallet = try WalletManager.importFromPrivateKey(TestData.eosPrivateKey, encryptedBy: TestData.password, metadata: meta, accountName: "imtoken1")
let signedData = try WalletManager.eosEcSign(walletID: eosWallet.walletID, data: "imToken2017", publicKey: TestData.eosPublicKey, password: TestData.password)
let signedData = try WalletManager.eosEcSign(walletID: eosWallet.walletID, data: "imToken2017", isHex: false, publicKey: TestData.eosPublicKey, password: TestData.password)
XCTAssertEqual("SIG_K1_JuVsfsNmB3JgvsnxUcmuw5m27gH9xTGuU4yN9BMoRLeLVYhA4Bfypdm8DDg5cUTXSLArDLc3gtRFkFHMm3rmZyZxD5FE7k", signedData)
let rightPubKey = try WalletManager.eosEcRecover(data: "imToken2017", signature: signedData)
let rightPubKey = try WalletManager.eosEcRecover(data: "imToken2017", isHex: false, signature: signedData)
XCTAssertEqual(TestData.eosPublicKey, rightPubKey)

let wrongPubKey = try WalletManager.eosEcRecover(data: "imToken2016", signature: signedData)
let wrongPubKey = try WalletManager.eosEcRecover(data: "imToken2016", isHex: false, signature: signedData)
XCTAssertNotEqual(TestData.eosPublicKey, wrongPubKey)
} catch {
XCTFail(error.localizedDescription)
}

}

func testEcSignHexTest() {
do {
let meta = WalletMeta(chain: .eos, source: .mnemonic)
let eosWallet = try WalletManager.importEOS(from: TestData.mnemonic, accountName: "blobblobblob", permissions: [], metadata: meta, encryptBy: TestData.password, at: BIP44.eosLedger)
let signedData = try WalletManager.eosEcSign(walletID: eosWallet.walletID, data: "1546396453811", isHex: false, publicKey: "EOS88XhiiP7Cu5TmAUJqHbyuhyYgd6sei68AU266PyetDDAtjmYWF", password: TestData.password)
XCTAssertEqual("SIG_K1_KYKWN3wf2jVsd2MP279ds6YvoPCKx4YHHisfenaEZWwwZEq2bodf8LRBepMm33PEBjYp3STceR5AeD94V6DEWEkEZWA5sq", signedData)

} catch {
XCTFail(error.localizedDescription)
}

}

}
2 changes: 2 additions & 0 deletions Tests/Utils/HexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class HexTests: XCTestCase {
XCTAssertFalse(Hex.isHex("imToken"))
XCTAssertFalse("hijk".tk_isHex())
XCTAssertFalse("imToken".tk_isHex())
XCTAssertFalse("12345".tk_isHex())
XCTAssert("123456".tk_isHex())
}

func testPrefix() {
Expand Down

0 comments on commit 4f99685

Please sign in to comment.