3

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?

skywinder
  • 21,291
  • 15
  • 93
  • 123
null null
  • 101
  • 1
  • 7
  • Look at the examples and tests for the projects you listed. Sounds as though you need fundamental Swift acclimation as well. It is unlikely someone will write your code for you. – Frank C. May 19 '18 at 11:31
  • There are even methods for creating keys in those libs. Please choose either one of them (the one with the most documentation or reviews would be a good idea) and then try something, come back if you get stuck and include your code in the new question. – Maarten Bodewes May 20 '18 at 12:55
  • That a good and actual question. I fixed it. So please, do not delete it. Here is an answer about the actual library, that solves it! – skywinder May 30 '18 at 21:07

2 Answers2

5

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.

null null
  • 101
  • 1
  • 7
  • Which tools did you have to install? I am getting issues with coacopods install the static library – S-K' Oct 18 '18 at 08:43
  • hi How did you managed to do this. i have been struggling to get and .r .s from a signature that is equivalent to the java version, is it possible for you to share your swift code from BitcoinKit? – RicardoDuarte Feb 18 '20 at 01:37
1

You can always use the C bindings, but then you have to work with UnsafePointers and Bytes and I assume you are looking for appropriate wrappers. Therefore, I found this sawtooth-swift-sdk, which seems to use the secp256k1 library by boilertalk, which in turn imports the widely used bitcoin-core/libsecp256k1. It allows something similar as to what you were asking (per the docs):

import SawtoothSigning

let context = Secp256k1Context()
let privateKey = context.newRandomPrivateKey()
let signer = Signer(context: context, privateKey: privateKey)

let signature = signer.sign(data: message_bytes)

I assume it will be listed as an officially endorsed sdk soon, as indicated by this Sawtooth RFC. The only thing that seems to be missing there, is a provision of generated protobuf classes.

Gigi
  • 150
  • 1
  • 8