Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Commit

Permalink
feat: replace secp256k1_swift with secp256k1.swift and implement Secp…
Browse files Browse the repository at this point in the history
…256k1.privateToPublic
  • Loading branch information
ashchan authored and XiaoLu committed Mar 3, 2019
1 parent fff6257 commit f1911b8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
28 changes: 26 additions & 2 deletions CKB.xcodeproj/project.pbxproj
Expand Up @@ -30,6 +30,8 @@
1A414AA021C770DD00B28C09 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A414A9F21C770DD00B28C09 /* Utils.swift */; };
1A414AA521C7713800B28C09 /* UtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A414AA421C7713800B28C09 /* UtilsTests.swift */; };
1A8CAEC021F0282E00E99A1C /* bitcoin_unlock.rb in Resources */ = {isa = PBXBuildFile; fileRef = 1A8CAEBF21F0282E00E99A1C /* bitcoin_unlock.rb */; };
1AB45ED7222AA558009B395B /* Secp256k1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB45ED6222AA558009B395B /* Secp256k1.swift */; };
1AB45EDA222AA644009B395B /* Secp256k1Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB45ED9222AA644009B395B /* Secp256k1Tests.swift */; };
2D6EA000E9B8936CB81356CC /* Pods_CKB.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6F4BD554CBEED5D384A1C737 /* Pods_CKB.framework */; };
591DADB7D11BC8F71DBF4902 /* Pods_CKBTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FD2179DE52A415B08B8D134 /* Pods_CKBTests.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -71,6 +73,8 @@
1A414A9F21C770DD00B28C09 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
1A414AA421C7713800B28C09 /* UtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilsTests.swift; sourceTree = "<group>"; };
1A8CAEBF21F0282E00E99A1C /* bitcoin_unlock.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; name = bitcoin_unlock.rb; path = "ckb-ruby-scripts/bitcoin_unlock.rb"; sourceTree = SOURCE_ROOT; };
1AB45ED6222AA558009B395B /* Secp256k1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secp256k1.swift; sourceTree = "<group>"; };
1AB45ED9222AA644009B395B /* Secp256k1Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secp256k1Tests.swift; sourceTree = "<group>"; };
3280C4848C7A8236CA5F4CD8 /* Pods-CKBTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKBTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CKBTests/Pods-CKBTests.debug.xcconfig"; sourceTree = "<group>"; };
5CAEE4335089142E8F3ECC55 /* Pods-CKBTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKBTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-CKBTests/Pods-CKBTests.release.xcconfig"; sourceTree = "<group>"; };
5CD9DB4E09DDB6450D81BC6F /* Pods-CKB.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CKB.debug.xcconfig"; path = "Pods/Target Support Files/Pods-CKB/Pods-CKB.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -132,6 +136,7 @@
isa = PBXGroup;
children = (
1A8CAEBE21F027D300E99A1C /* RubyScripts */,
1AB45ED5222AA53F009B395B /* Crypto */,
1A414A8621C36FAA00B28C09 /* Types */,
1A414A7E21C32D1D00B28C09 /* API */,
1A414A9E21C770CF00B28C09 /* Utils */,
Expand All @@ -144,6 +149,7 @@
1A414A6D21C2226E00B28C09 /* Tests */ = {
isa = PBXGroup;
children = (
1AB45ED8222AA624009B395B /* Crypto */,
1A28119521D3804600E7CEC5 /* Types */,
1A414A7F21C32D2500B28C09 /* API */,
1A414AA121C7711200B28C09 /* Utils */,
Expand Down Expand Up @@ -214,6 +220,22 @@
path = RubyScripts;
sourceTree = "<group>";
};
1AB45ED5222AA53F009B395B /* Crypto */ = {
isa = PBXGroup;
children = (
1AB45ED6222AA558009B395B /* Secp256k1.swift */,
);
path = Crypto;
sourceTree = "<group>";
};
1AB45ED8222AA624009B395B /* Crypto */ = {
isa = PBXGroup;
children = (
1AB45ED9222AA644009B395B /* Secp256k1Tests.swift */,
);
path = Crypto;
sourceTree = "<group>";
};
4F766494AD6DB6539413DFD4 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -416,14 +438,14 @@
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-CKBTests/Pods-CKBTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/CryptoSwift/CryptoSwift.framework",
"${BUILT_PRODUCTS_DIR}/secp256k1_swift/secp256k1_swift.framework",
"${BUILT_PRODUCTS_DIR}/secp256k1.swift/secp256k1.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CryptoSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/secp256k1_swift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/secp256k1.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand All @@ -443,6 +465,7 @@
1A1134802229063F00840EE8 /* TxTrace.swift in Sources */,
1A11347E2228D14300840EE8 /* LocalNode.swift in Sources */,
1A414A7B21C22A9300B28C09 /* APIClient.swift in Sources */,
1AB45ED7222AA558009B395B /* Secp256k1.swift in Sources */,
1A414A8E21C3722300B28C09 /* CellInput.swift in Sources */,
1A414A9621C37AA000B28C09 /* Script.swift in Sources */,
1A414AA021C770DD00B28C09 /* Utils.swift in Sources */,
Expand All @@ -463,6 +486,7 @@
1A28119721D3805800E7CEC5 /* ScriptTests.swift in Sources */,
1A414A7D21C23A3900B28C09 /* APIClientTests.swift in Sources */,
1A414AA521C7713800B28C09 /* UtilsTests.swift in Sources */,
1AB45EDA222AA644009B395B /* Secp256k1Tests.swift in Sources */,
1A3B97A321C9CD2700FC1C54 /* UnlockScriptTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Expand Up @@ -7,7 +7,7 @@ target 'CKB' do
inhibit_all_warnings!

pod "CryptoSwift", "~> 0.14.0"
pod "secp256k1_swift", modular_headers: true, git: "https://github.com/cryptape/secp256k1_swift.git", branch: "swift-4.2", submodules: true
pod "secp256k1.swift", "~> 0.1.4"
pod "SwiftLint"
end

Expand Down
21 changes: 5 additions & 16 deletions Podfile.lock
@@ -1,35 +1,24 @@
PODS:
- CryptoSwift (0.14.0)
- secp256k1_swift (1.0.3)
- secp256k1.swift (0.1.4)
- SwiftLint (0.29.1)

DEPENDENCIES:
- CryptoSwift (~> 0.14.0)
- secp256k1_swift (from `https://github.com/cryptape/secp256k1_swift.git`, branch `swift-4.2`)
- secp256k1.swift (~> 0.1.4)
- SwiftLint

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- CryptoSwift
- secp256k1.swift
- SwiftLint

EXTERNAL SOURCES:
secp256k1_swift:
:branch: swift-4.2
:git: https://github.com/cryptape/secp256k1_swift.git
:submodules: true

CHECKOUT OPTIONS:
secp256k1_swift:
:commit: db0f48a853f5c4240194853d8d87601d47852f7f
:git: https://github.com/cryptape/secp256k1_swift.git
:submodules: true

SPEC CHECKSUMS:
CryptoSwift: 35a10419f4dbf0619c13f8c1c180037e115323fa
secp256k1_swift: 4fc5c4b2d2c6d21ee8ccb868cdc92da12f38bed9
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftLint: 6772320e40b52049053a518c17db9b0634a0b45a

PODFILE CHECKSUM: 0bba1951d87fd9729c71d9aa3ca10a413779652e
PODFILE CHECKSUM: 51fd394a2435f43eaa833dbcd8cecd9893e4a85b

COCOAPODS: 1.6.1
31 changes: 31 additions & 0 deletions Source/Crypto/Secp256k1.swift
@@ -0,0 +1,31 @@
//
// Secp256k1.swift
// CKB
//
// Created by James Chen on 2019/03/02.
// Copyright © 2019 Nervos Foundation. All rights reserved.
//

import Foundation
import secp256k1

final class Secp256k1 {
static func privateToPublic(privateKey: Data, compressed: Bool = true) -> Data {
let context = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_SIGN))!
defer {
secp256k1_context_destroy(context)
}

let seckeyData = Array(privateKey)
var publicKey = secp256k1_pubkey()
_ = secp256k1_ec_pubkey_create(context, &publicKey, seckeyData)

var length = compressed ? 33 : 65
var data = Data(count: length)
data.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
let flag = compressed ? UInt32(SECP256K1_EC_COMPRESSED) : UInt32(SECP256K1_EC_UNCOMPRESSED)
_ = secp256k1_ec_pubkey_serialize(context, bytes, &length, &publicKey, flag)
}
return data
}
}
3 changes: 1 addition & 2 deletions Source/Utils/Utils.swift
Expand Up @@ -7,12 +7,11 @@
//

import Foundation
import secp256k1_swift
import CryptoSwift

public struct Utils {
public static func privateToPublic(_ privateKey: Data) -> Data {
return SECP256K1.privateToPublic(privateKey: privateKey, compressed: true)!
return Secp256k1.privateToPublic(privateKey: privateKey)
}

/// Generate compressed public key from private key.
Expand Down
18 changes: 18 additions & 0 deletions Tests/Crypto/Secp256k1Tests.swift
@@ -0,0 +1,18 @@
//
// Secp256k1Tests.swift
// CKBTests
//
// Created by James Chen on 2019/03/02.
// Copyright © 2019 Nervos Foundation. All rights reserved.
//

import XCTest
@testable import CKB

class Secp256k1Tests: XCTestCase {
func testPrivateToPublic() {
let privateKey = Data(hex: "e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3")
let publicKey = Data(hex: "024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01")
XCTAssertEqual(publicKey, Secp256k1.privateToPublic(privateKey: privateKey))
}
}

0 comments on commit f1911b8

Please sign in to comment.