Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privateKeyMalformed #7

Open
loekTheDreamer opened this issue Mar 24, 2022 · 2 comments
Open

privateKeyMalformed #7

loekTheDreamer opened this issue Mar 24, 2022 · 2 comments

Comments

@loekTheDreamer
Copy link

cant create a keystore from private key: error is private key malformed

code used:

    let privateKey = "583e8853014aa2c53e031f213a4075efb59cb4d0541674d87bdf49d170f46eb4"
    let privateKeyByteArray: [UInt8] = Array(privateKey.utf8)
    let keystore = try! Keystore(privateKey: privateKeyByteArray, password: password)
    // Fatal error: 'try!' expression unexpectedly raised an error: Keystore.KeystoreFactory.Error.privateKeyMalformed

please assist.

@koraykoska
Copy link
Member

@loekTheDreamer You need to use something like this to convert a hex encoded private key to a UInt8 Array. If you replace the second line with the implementation I linked or any other hex to UInt8 Array conversion, it should work out smoothly. If not, let me know.

@loekTheDreamer
Copy link
Author

Good afternoon.

Thank you so much for assisting me, ive been struggling with this problem for a couple of weeks now. I had a look at the link you provided. Unfortunately it seems that there are some errors i dont know how to solve:
IntegerLiteralType' is not a member type of type 'Element
Cannot find type 'Integer' in scope

Screenshot 2022-03-25 at 14 46 29

I then searched online and opted for this solution:

extension StringProtocol {
    var hexaData: Data { .init(hexa) }
    var hexaBytes: [UInt8] { .init(hexa) }
    private var hexa: UnfoldSequence<UInt8, Index> {
        sequence(state: startIndex) { startIndex in
            guard startIndex < self.endIndex else { return nil }
            let endIndex = self.index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
            defer { startIndex = endIndex }
            return UInt8(self[startIndex..<endIndex], radix: 16)
        }
    }
}

usage:

    let privateKey = "583e8853014aa2c53e031f213a4075efb59cb4d0541674d87bdf49d170f46eb4"
    let privateKeyByteArray = privateKey.hexaBytes
    let keystore = try! Keystore(privateKey: privateKeyByteArray, password: password)

It then produces a keystore shape and there are no errors in xcode. But unfortunately when I try unlock it on 3rd party wallet provider I get the following error:
Expected private key to be an Uint8Array with length 32
website used: https://www.myetherwallet.com/wallet/access/software?type=keystore
Screenshot 2022-03-25 at 15 05 16

Is there something wrong with the unit8 array passed to the keystore object or could there be something else that I am not doing correctly?

Thank you for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants