Skip to content

Commit

Permalink
IOS-2399: Make sum public
Browse files Browse the repository at this point in the history
  • Loading branch information
Andoran90 committed Oct 26, 2022
1 parent 03ab40c commit a64f9ce
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions TangemSdk/TangemSdk/Crypto/Secp256k1Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ public final class Secp256k1Utils {
return try serializeDer(&sig)
}

public func sum(compressedPubKey1: Data, compressedPubKey2: Data) throws -> Data {
var pubKey1 = try parsePublicKey(compressedPubKey1)
var pubKey2 = try parsePublicKey(compressedPubKey2)
var publicKeySecp = secp256k1_pubkey()
var result: Int32 = 0

withUnsafePointer(to: &pubKey1) { pointer1 in
withUnsafePointer(to: &pubKey2) { pointer2 in
var pubkeyPointers: [UnsafePointer<secp256k1_pubkey>?] = [pointer1, pointer2]

result = secp256k1_ec_pubkey_combine(context, &publicKeySecp, &pubkeyPointers, 2)
}
}

guard result == 1 else {
throw TangemSdkError.cryptoUtilsError("Failed to sum keys")
}

return try serializePublicKey(&publicKeySecp, compressed: true)
}

func compressKey(_ publicKey: Data) throws -> Data {
if publicKey.count == 33 {
return publicKey
Expand Down Expand Up @@ -97,27 +118,6 @@ public final class Secp256k1Utils {
return try serializePublicKey(&publicKey, compressed: compressed)
}

func sum(compressedPubKey1: Data, compressedPubKey2: Data) throws -> Data {
var pubKey1 = try parsePublicKey(compressedPubKey1)
var pubKey2 = try parsePublicKey(compressedPubKey2)
var publicKeySecp = secp256k1_pubkey()
var result: Int32 = 0

withUnsafePointer(to: &pubKey1) { pointer1 in
withUnsafePointer(to: &pubKey2) { pointer2 in
var pubkeyPointers: [UnsafePointer<secp256k1_pubkey>?] = [pointer1, pointer2]

result = secp256k1_ec_pubkey_combine(context, &publicKeySecp, &pubkeyPointers, 2)
}
}

guard result == 1 else {
throw TangemSdkError.cryptoUtilsError("Failed to sum keys")
}

return try serializePublicKey(&publicKeySecp, compressed: true)
}

func serializeDer(_ signature: inout secp256k1_ecdsa_signature) throws -> Data {
var length: Int = 128
var der = [UInt8].init(repeating: UInt8(0x0), count: Int(length))
Expand Down

0 comments on commit a64f9ce

Please sign in to comment.