Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
refactor: rename StarkKey generateKeyPair method
Browse files Browse the repository at this point in the history
This keypair generation is specific to ImmutableX's Link and should be
used only for compatibility reasons.

Remove StarkKey's generateKeyPairFromRawSignature method from public
interface.
  • Loading branch information
CassiusPacheco committed Nov 21, 2022
1 parent 16b8316 commit 49af7ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Ropsten has been deprecated and won't work anymore. Sandbox is the preferred tes
- (Breaking): rename PrivateKey, PublicKey, KeyPair and CurvePoint to ECPrivateKey, ECPublicKey, ECKeyPair and ECCurvePoint respectively
The previous names were too generic and would easily conflict with other classes/structs. These have then been prefixed with EC for Elliptic Curve.

- (Breaking): rename StarkKey's generateKeyPair to generateLegacyKeyPair
This keypair generation is specific to ImmutableX's Link and should be used only for compatibility reasons.

### Fixed

- re-include macos as a Cocoapods target
Expand All @@ -50,6 +53,8 @@ The Core SDK is generic enough that it should work on macOS. It had accidentally

### Removed

- (Breaking): remove StarkKey's generateKeyPairFromRawSignature method from public interface

- version file

### Changed
Expand Down
7 changes: 5 additions & 2 deletions Sources/ImmutableXCore/Crypto/Stark/StarkKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension StarkKey {
/// - Parameter signer: the signer that the key pair will be derived from
/// - Returns: Stark key pair as ``ECKeyPair``
/// - Throws: ``ImmutableXError``
static func generateKeyPair(from signer: Signer) async throws -> ECKeyPair {
static func generateLegacyKeyPair(from signer: Signer) async throws -> ECKeyPair {
let address = try await signer.getAddress()
let signature = try await signer.signMessage(Constants.starkMessage)
return try generateKeyPairFromRawSignature(signature, ethereumAddress: address)
Expand All @@ -26,7 +26,10 @@ public extension StarkKey {
/// - Parameter ethereumAddress: the connected wallet address
/// - Returns: Stark key pair as ``ECKeyPair``
/// - Throws: ``ImmutableXError``
static func generateKeyPairFromRawSignature(_ signature: String, ethereumAddress: String) throws -> ECKeyPair {
internal static func generateKeyPairFromRawSignature(
_ signature: String,
ethereumAddress: String
) throws -> ECKeyPair {
// swiftlint:disable:next line_length
// https://github.com/ethers-io/ethers.js/blob/3de1b815014b10d223a42e524fe9c25f9087293b/packages/bytes/src.ts/index.ts#L347
let seed = signature.dropHexPrefix[64 ..< 128]
Expand Down
4 changes: 2 additions & 2 deletions Tests/ImmutableXCoreTests/Crypto/Stark/StarkKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class StarkKeyTests: XCTestCase {
signer.signMessageReturnValue = "0x5a263fad6f17f23e7c7ea833d058f3656d3fe464baf13f6f5ccba9a246" +
"6ba2ce4c4a250231bcac7beb165aec4c9b049b4ba40ad8dd287dc79b92b1ffcf20cdcf1b"

let pair = try await StarkKey.generateKeyPair(from: signer)
let pair = try await StarkKey.generateLegacyKeyPair(from: signer)
XCTAssertEqual(
pair.publicKey.asStarkPublicKey,
"0x02a4c7332c55d6c1c510d24272d1db82878f2302f05b53bcc38695ed5f78fffd"
Expand All @@ -67,7 +67,7 @@ final class StarkKeyTests: XCTestCase {
signer.signMessageThrowableError = ImmutableXError.invalidKeyData

do {
_ = try await StarkKey.generateKeyPair(from: signer)
_ = try await StarkKey.generateLegacyKeyPair(from: signer)
XCTFail("Expected to throw while awaiting, but succeeded")
} catch {
XCTAssertTrue(error is ImmutableXError)
Expand Down

0 comments on commit 49af7ed

Please sign in to comment.