This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace secp256k1_swift with secp256k1.swift and implement Secp…
…256k1.privateToPublic
- Loading branch information
Showing
6 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |