简体   繁体   中英

SECP256K1 using SawTooth Swift

I am trying to sign signature as well as verify using Sawtooth SDK from Sawtooth SDK . Looking at the documentation, i followed through but it didnt work.

import SawtoothSigning

public class Secp256k1 {

private var signer: Signer
private var privateKey: PrivateKey
private var context = Secp256k1Context() //Here it crashed with error Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeda61ff8)

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

public 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 init() {
    self.privateKey = Secp256k1().getPrivateKey()
    self.signer = Signer(context: context, privateKey: self.privateKey)
}
}

Have anyone worked with this SDK and knows why is it crashing? Or is there any simpler SDK?

You've cycled Secp256k1 constructor (ie. calls Secp256k1() within own init ), that result in crash.

Here is possible variant of solution (tested & works with Xcode 11.4):

public class Secp256k1 {

    private var signer: Signer
    private var privateKey: PrivateKey
    private static var context = Secp256k1Context()          // << shared !!

    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 {        // << shared !!
        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 init() {
        self.privateKey = Secp256k1.getPrivateKey()       // no cycle now !!
        self.signer = Signer(context: Secp256k1.context, privateKey: self.privateKey)
    }
}

backup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM