Added conversion from hex string to hex array for the private key.

pull/2/head
Philipp Rieger 7 years ago
parent ee2d76027d
commit c155e2b819
  1. 36
      Trust/EtherClient/EtherKeystore.swift

@ -207,7 +207,7 @@ class EtherKeystore: Keystore {
}
func convertPrivateKeyToKeystoreFile(privateKey: String) -> Result<[String: Any], KeyStoreError> {
let password: [UInt8] = Array(privateKey.utf8)
let password: [UInt8] = Array(Data(fromHexEncodedString: privateKey)!)
let numberOfIterations = 262144
do {
// derive key
@ -268,3 +268,37 @@ extension Account {
)
}
}
extension Data {
init?(fromHexEncodedString string: String) {
// Convert 0 ... 9, a ... f, A ...F to their decimal value,
// return nil for all other input characters
func decodeNibble(u: UInt16) -> UInt8? {
switch(u) {
case 0x30 ... 0x39:
return UInt8(u - 0x30)
case 0x41 ... 0x46:
return UInt8(u - 0x41 + 10)
case 0x61 ... 0x66:
return UInt8(u - 0x61 + 10)
default:
return nil
}
}
self.init(capacity: string.utf16.count/2)
var even = true
var byte: UInt8 = 0
for c in string.utf16 {
guard let val = decodeNibble(u: c) else { return nil }
if even {
byte = val << 4
} else {
byte += val
self.append(byte)
}
even = !even
}
guard even else { return nil }
}
}

Loading…
Cancel
Save