Skip to content

Commit

Permalink
Remove Hex.swift and use Sodium's hex decoding functions instead
Browse files Browse the repository at this point in the history
  • Loading branch information
qtbeee committed Nov 6, 2019
1 parent bb7a590 commit ec5b533
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 65 deletions.
1 change: 1 addition & 0 deletions Sources/Hedera/account/CryptoTransferTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class CryptoTransferTransaction: TransactionBuilder {
add(account: recipient, amount: Int64(amount))
}

@discardableResult
public func add(account: AccountId, amount: Int64) -> Self {
var accountAmount = Proto_AccountAmount()
accountAmount.accountID = account.toProto()
Expand Down
31 changes: 0 additions & 31 deletions Sources/Hedera/crypto/Hex.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Sources/Hedera/crypto/ed25519/Ed25519PrivateKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct Ed25519PrivateKey {

extension Ed25519PrivateKey: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
hexEncode(bytes: inner.secretKey, prefixed: ed25519PrivateKeyPrefix)
"\(ed25519PrivateKeyPrefix)\(sodium.utils.bin2hex(bytes)!)"
}

public var debugDescription: String {
Expand All @@ -58,14 +58,14 @@ extension Ed25519PrivateKey: LosslessStringConvertible {
public init?(_ description: String) {
switch description.count {
case ed25519PrivateKeyLength * 2, combinedEd25519KeyLength * 2: // lone key, or combined key
guard let decoded = try? hexDecode(description) else { return nil }
guard let decoded = sodium.utils.hex2bin(description) else { return nil }
self = Ed25519PrivateKey(bytes: decoded)!

case ed25519PrivateKeyLength * 2 + ed25519PrivateKeyPrefix.count: // DER encoded key
guard description.hasPrefix(ed25519PrivateKeyPrefix) else { return nil }

let range = description.index(description.startIndex, offsetBy: ed25519PrivateKeyPrefix.count)...
guard let decoded = try? hexDecode(description[range]) else { return nil }
guard let decoded = sodium.utils.hex2bin(String(description[range])) else { return nil }
self = Ed25519PrivateKey(bytes: decoded)!

default:
Expand Down
6 changes: 3 additions & 3 deletions Sources/Hedera/crypto/ed25519/Ed25519PublicKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Ed25519PublicKey {

extension Ed25519PublicKey: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
hexEncode(bytes: inner, prefixed: ed25519PublicKeyPrefix)
"\(ed25519PublicKeyPrefix)\(sodium.utils.bin2hex(bytes)!)"
}

public var debugDescription: String {
Expand All @@ -39,14 +39,14 @@ extension Ed25519PublicKey: LosslessStringConvertible {
public init?(_ description: String) {
switch description.count {
case ed25519PublicKeyLength * 2:
guard let decoded = try? hexDecode(description) else { return nil }
guard let decoded = sodium.utils.hex2bin(description) else { return nil }
self.init(bytes: decoded)

case ed25519PublicKeyLength * 2 + ed25519PublicKeyPrefix.count:
guard description.hasPrefix(ed25519PublicKeyPrefix) else { return nil }

let start = description.index(description.startIndex, offsetBy: ed25519PublicKeyPrefix.count)
guard let decoded = try? hexDecode(description[start...]) else { return nil }
guard let decoded = sodium.utils.hex2bin(String(description[start...])) else { return nil }
self.init(bytes: decoded)

default:
Expand Down
1 change: 0 additions & 1 deletion Tests/HederaTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public func allTests() -> [XCTestCaseEntry] {
return [
testCase(Ed25519PrivateKeyTests.allTests),
testCase(Ed25519PublicKeyTests.allTests),
testCase(HexTests.allTests),
testCase(EntityIdTests.allTests),
testCase(DateTests.allTests),
]
Expand Down
25 changes: 0 additions & 25 deletions Tests/HederaTests/crypto/HexTests.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class Ed25519PrivateKeyTests: XCTestCase {
let key = Ed25519PrivateKey(privateKeyString)!
let sig = key.sign(message: message.bytes)

XCTAssertEqual(hexEncode(bytes: sig), signature)
XCTAssertEqual(sodium.utils.bin2hex(sig), signature)
}

static var allTests = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class Ed25519PublicKeyTests: XCTestCase {

func testVerify() {
let key = Ed25519PublicKey(publicKeyString)!
let verified = key.verify(signature: try! hexDecode(signature), of: message.bytes)
let verified = key.verify(signature: sodium.utils.hex2bin(signature)!, of: message.bytes)
XCTAssertTrue(verified)
}

Expand Down

0 comments on commit ec5b533

Please sign in to comment.