Zach Babb
Zach Babb

Reputation: 668

secp256k1 public key parsing in Swift

I am using the bindings here to do secp256k1 functions in Swift. I have the following code:

        let pubkeyTxt = "036c1495224d8b6245ca35df958127dc3d587ff7e9d8e1e5f964b312dc5ea3aac9"
        let pubArray: [UInt8] = Array(pubkeyTxt.utf8)
        var pubkey = secp256k1_pubkey()
        let pubBool = secp256k1_ec_pubkey_parse(ctx!, &pubkey, pubArray, pubArray.count)
        if pubBool == 0 {
            print("Could not parse the public key")
            return
        }

which keeps printing that it could not parse the public key. I can't figure out what's wrong. My hunch was the pubArray.count was the wrong length for the function so I tried some other values, but with no luck.

Upvotes: 0

Views: 490

Answers (1)

Rob Napier
Rob Napier

Reputation: 299275

Your key is not valid. I believe you're expecting the first byte of the key to be 0x03, but the first byte of your key is the character "0" which is 0x30. The second byte of your key is the character "3" (0x33).

I believe you intended this to be a hex-encoded version of your actual key. If so, you need to hex-decode it first. You've treated it as a UTF-8 encoded version, which it isn't.

Upvotes: 1

Related Questions