0

I have been looking around, I am currently using Sawtooth SDK for signing, creating private key, public key and verifying but I cant find any ways to create EC Key Pair. Anyone who worked with blockchains can give me an idea or way on how to solve this? Below is the functions i made for verify, create private key, public key.. Now i am left with creating EC Keypair

import SawtoothSigning

public class Secp256k1 {

private var signer: Signer
private var privateKey: PrivateKey
private static var context = Secp256k1Context()

public func sign(message: [UInt8]) -> String {
    var result = ""
    do {
        result = try signer.sign(data: message)
    } catch {
        print("Error signing")
    }
    return result
}

public static func getPrivateKey() -> PrivateKey {        
    if let privateKey = UserDefaults.standard.string(forKey: "privateKey") {
        return Secp256k1PrivateKey.fromHex(hexPrivKey: privateKey)
    } else {
        let privateKey = context.newRandomPrivateKey()
        UserDefaults.standard.set(privateKey.hex(), forKey: "privateKey" )
        do {
            let pubKey = try context.getPublicKey(privateKey: privateKey)
            UserDefaults.standard.set(pubKey.hex(), forKey: "publicKey" )
        } catch {
            if #available(iOS 10.0, *) {
                print("Error creating public key")
            }
        }
        return privateKey
    }
}

public static func getPublicKey() -> PublicKey {
    if let publicKey = UserDefaults.standard.string(forKey: "publicKey") {
        return Secp256k1PublicKey.fromHex(hexPubKey: publicKey)
    } else {
        let privateKey = context.newRandomPrivateKey()
        var pubKey: PublicKey?
        UserDefaults.standard.set(privateKey.hex(), forKey: "privateKey" )
        do {
            pubKey = try context.getPublicKey(privateKey: privateKey)
            UserDefaults.standard.set(pubKey?.hex(), forKey: "publicKey" )
        } catch {
            if #available(iOS 10.0, *) {
                print("Error creating public key")
            }
        }
        return pubKey!
    }
}

public static func verify(signature: String, data: [UInt8], publicKey: PublicKey) -> Bool {
    var result = false
    do {
        try result = context.verify(signature: signature, data: data, publicKey: publicKey)
    } catch {
        if #available(iOS 10.0, *) {
            print("Error verifying")
        }
    }
    return result
}

public init() {
    self.privateKey = Secp256k1.getPrivateKey()
    self.signer = Signer(context: Secp256k1.context, privateKey: self.privateKey)
  }
}

Looked around many resources online but none is for Swift

L.William
  • 382
  • 1
  • 4
  • 20
  • Keychain services can do everything you are trying to do without any external libraries, look at `SecKeyCreateRandomKey`, `SecKeyCreateWithData` and `SecKeyCreateSignature`. The only problematic part is to convert it to the correct format. – Sulthan Mar 29 '20 at 12:03
  • @Sulthan do u mean that for EC Key Pairs, I can use Keychain Services? I cant find any resources online, do u have any resources i can look at for creating EC Key Pairs? – L.William Mar 29 '20 at 12:40
  • Yes you can and I am betting that's what the library is using internally. There is nothing different from other key types, you only have to set `kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom, kSecAttrKeySizeInBits: 256`. The only complicated thing is the conversion from external bytes because you have to understand the format in which keys are stored. https://github.com/agens-no/EllipticCurveKeyPair/blob/master/Sources/EllipticCurveKeyPair.swift is a good starting point. – Sulthan Mar 29 '20 at 14:17

0 Answers0