Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3b1b44d

Browse files
committedMar 9, 2023
fix(castor): prism did was not being resolved correctly because of a change in the protos
1 parent c8b7ded commit 3b1b44d

File tree

6 files changed

+83
-8
lines changed

6 files changed

+83
-8
lines changed
 

‎AtalaPrismSDK/Apollo/Sources/ApolloImpl+Public.swift

+32
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ returns random mnemonics nerver returns invalid mnemonics
105105
)
106106
}
107107

108+
public func publicKeyFrom(x: Data, y: Data) -> PublicKey {
109+
PublicKey(
110+
curve: KeyCurve.secp256k1().name,
111+
value: LockPublicKey(x: x, y: y).data
112+
)
113+
}
114+
115+
public func publicKeyPointCurve(publicKey: PublicKey) throws -> (x: Data, y: Data) {
116+
let points = try LockPublicKey(bytes: publicKey.value).pointCurve()
117+
return (points.x.data, points.y.data)
118+
}
119+
108120
/// signMessage signs a message using a given private key, returning the signature.
109121
///
110122
/// - Parameters:
@@ -191,6 +203,26 @@ returns random mnemonics nerver returns invalid mnemonics
191203
return jwk
192204
}
193205
}
206+
207+
public func keyDataToPEMString(_ keyData: PrivateKey) -> String? {
208+
let keyBase64 = keyData.value.base64EncodedString(options: .lineLength64Characters)
209+
let pemString = """
210+
-----BEGIN PRIVATE KEY-----
211+
\(keyBase64)
212+
-----END PRIVATE KEY-----
213+
"""
214+
return pemString
215+
}
216+
217+
public func keyDataToPEMString(_ keyData: PublicKey) -> String? {
218+
let keyBase64 = keyData.value.base64EncodedString(options: .lineLength64Characters)
219+
let pemString = """
220+
-----BEGIN PUBLIC KEY-----
221+
\(keyBase64)
222+
-----END PUBLIC KEY-----
223+
"""
224+
return pemString
225+
}
194226
}
195227

196228
struct MyClaims: Claims {

‎AtalaPrismSDK/Apollo/Sources/BIPs/BIP32_44/LockPublicKey.swift

+32
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,45 @@ struct LockPublicKey {
4141
self.isCompressed = (header == 0x02 || header == 0x03)
4242
}
4343

44+
/**
45+
* Guarantees to init a public key uncompressed with 65 bytes in the following form:
46+
*
47+
* 0x04 ++ xBytes ++ yBytes
48+
*
49+
* Where `xBytes` and `yBytes` represent a 32-byte coordinates of a point
50+
* on the secp256k1 elliptic curve, which follow the formula below:
51+
*
52+
* y^2 == x^3 + 7
53+
*
54+
* @return uncompressed public key
55+
*/
56+
init(x: Data, y: Data) {
57+
let header: UInt8 = 0x04
58+
self.data = [header] + x + y
59+
self.isCompressed = false
60+
}
61+
4462
func compressedPublicKey() -> LockPublicKey {
4563
LockPublicKey(bytes: KeyHelpers.compressPublicKey(fromPublicKey: data))
4664
}
4765

4866
func uncompressedPublicKey() -> LockPublicKey {
4967
LockPublicKey(bytes: KeyHelpers.uncompressPublicKey(fromPublicKey: data))
5068
}
69+
70+
func pointCurve() throws -> PointOnCurve {
71+
let selfUncompressed = uncompressedPublicKey()
72+
var xAndY = selfUncompressed.data
73+
xAndY.removeFirst() // Remove the header
74+
let expectedLengthOfScalar = Scalar32Bytes.expectedByteCount
75+
let expectedLengthOfKey = expectedLengthOfScalar * 2
76+
guard xAndY.count == expectedLengthOfKey else {
77+
fatalError("expected length of key is \(expectedLengthOfKey) bytes, but got: \(xAndY.count)")
78+
}
79+
let x = xAndY.prefix(expectedLengthOfScalar)
80+
let y = xAndY.suffix(expectedLengthOfScalar)
81+
return try PointOnCurve(x: x, y: y)
82+
}
5183
}
5284

5385
extension LockPublicKey: Equatable {

‎AtalaPrismSDK/Castor/Sources/DID/PrismDID/PrismDIDPublicKey.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,24 @@ struct PrismDIDPublicKey {
7676
id = proto.id
7777
usage = proto.usage.fromProto()
7878
switch proto.keyData {
79+
case let .ecKeyData(value):
80+
keyData = apollo.publicKeyFrom(x: value.x, y: value.y)
7981
case let .compressedEcKeyData(value):
8082
keyData = apollo.uncompressedPublicKey(compressedData: value.data)
8183
default:
8284
throw CastorError.invalidPublicKeyCoding(didMethod: "prism", curve: "secp256k1")
8385
}
8486
}
8587

86-
func toProto() -> Io_Iohk_Atala_Prism_Protos_PublicKey {
88+
func toProto() throws -> Io_Iohk_Atala_Prism_Protos_PublicKey {
8789
var protoKey = Io_Iohk_Atala_Prism_Protos_PublicKey()
8890
protoKey.id = id
8991
protoKey.usage = usage.toProto()
90-
let compressed = apollo.compressedPublicKey(publicKey: keyData)
91-
protoKey.keyData = .compressedEcKeyData(compressed.toProto())
92+
let points = try apollo.publicKeyPointCurve(publicKey: keyData)
93+
var protoEC = Io_Iohk_Atala_Prism_Protos_ECKeyData()
94+
protoEC.x = points.x
95+
protoEC.y = points.y
96+
protoKey.keyData = .ecKeyData(protoEC)
9297
return protoKey
9398
}
9499
}

‎AtalaPrismSDK/Castor/Sources/Operations/CreatePrismDIDOperation.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct CreatePrismDIDOperation {
1010

1111
func compute() throws -> DID {
1212
var operation = Io_Iohk_Atala_Prism_Protos_AtalaOperation()
13-
operation.createDid = createDIDAtalaOperation(
13+
operation.createDid = try createDIDAtalaOperation(
1414
publicKeys: [PrismDIDPublicKey(
1515
apollo: apollo,
1616
id: PrismDIDPublicKey.Usage.masterKey.defaultId,
@@ -25,9 +25,9 @@ struct CreatePrismDIDOperation {
2525
private func createDIDAtalaOperation(
2626
publicKeys: [PrismDIDPublicKey],
2727
services: [DIDDocument.Service]
28-
) -> Io_Iohk_Atala_Prism_Protos_CreateDIDOperation {
28+
) throws -> Io_Iohk_Atala_Prism_Protos_CreateDIDOperation {
2929
var didData = Io_Iohk_Atala_Prism_Protos_CreateDIDOperation.DIDCreationData()
30-
didData.publicKeys = publicKeys.map { $0.toProto() }
30+
didData.publicKeys = try publicKeys.map { try $0.toProto() }
3131
didData.services = services.map {
3232
var service = Io_Iohk_Atala_Prism_Protos_Service()
3333
service.id = $0.id

‎AtalaPrismSDK/Domain/Sources/BBs/Apollo.swift

+8
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ public protocol Apollo {
9898
/// - Returns: The public key pair in JWK format, as a string
9999
/// - Throws: An error if the key pair is invalid
100100
func getPublicJWKJson(id: String, keyPair: KeyPair) throws -> String
101+
102+
func keyDataToPEMString(_ keyData: PrivateKey) -> String?
103+
104+
func keyDataToPEMString(_ keyData: PublicKey) -> String?
105+
106+
func publicKeyFrom(x: Data, y: Data) -> PublicKey
107+
108+
func publicKeyPointCurve(publicKey: PublicKey) throws -> (x: Data, y: Data)
101109
}

‎README.md

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ To get started with the Atala PRISM Swift SDK, you can set up the SDK and start
3636
- MacOS 12 or later
3737
- iOS 15 or later
3838

39-
> ⚠️ **Currently you need to always open the XCode in ROSETTA mode**
40-
4139
### Integrating the SDK in an existing project
4240

4341
To integrate the SDK into an existing project, you can use the Swift Package Manager, which is distributed with Xcode.

0 commit comments

Comments
 (0)
Please sign in to comment.