Search code examples
iosswiftcryptographyecdsahyperledger-sawtooth

ECDSA secp256k1 keypair generation and signing on Swift


I'm making Hyperledger Sawtooth client prototype for iOS on Swift.

Before that, I was doing the same for Android on Java. In Java implementation it makes easy with SpongyCastle library: Function to generate keys looks like this:

public static KeyPair getKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "SC");
        ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256k1");
        keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    }

I need to do the same thing in Swift:

Generate a secp256k1 keypair and sign an array of bytes with it.

and use this to sign array of bytes:

        Signature signature = Signature.getInstance("ECDSA", "SC");
        signature.initSign(keyPair.getPrivate(), new SecureRandom());
        signature.update(bytes);
        byte[] signedBytes = signature.sign();

I've googled "secp256k1 swift" and found these libraries:

All of them are bindings of bitcoin-core's secp256k1 library to Swift.

Can I make something like let kp = KeyPair("secp256k1"), let signedBytes = kp.sign(bytes)? If yes then how, and if no then are there any other ways to do that?


Solution

  • i found solution. it is BitcoinKit framework. i had some issues with installation with Carthage, but Cocoapods + installation of some missing tools found in error messages works well.