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

Commit

Permalink
* Create P256 keypairs for dpop (#29)
Browse files Browse the repository at this point in the history
* * Create P256 keypairs for dpop
* Create jwt

* code clean up

* code clean up
  • Loading branch information
Ethella authored Jul 19, 2023
1 parent 744d07a commit 347be41
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 79 deletions.
4 changes: 2 additions & 2 deletions Sources/MagicSDK/Core/Provider/RpcProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class RpcProvider: NetworkClient, Web3Provider {
let newRequest = RPCRequest(method: request.method, params: request.params)

// construct message data
let eventMessage = RequestData(msgType: "\(msgType.rawValue)-\(urlBuilder.encodedParams)", payload: newRequest)
let eventMessage = MagicRequestData(msgType: "\(msgType.rawValue)-\(urlBuilder.encodedParams)", payload: newRequest, rt: nil, jwt: createJwt())

// encode to JSON
firstly {
Expand All @@ -66,7 +66,7 @@ public class RpcProvider: NetworkClient, Web3Provider {

// Decode JSON string into string
do {
let rpcResponse = try self.decoder.decode(ResponseData<RPCResponse<Result>>.self, from: jsonData)
let rpcResponse = try self.decoder.decode(MagicResponseData<RPCResponse<Result>>.self, from: jsonData)
let result = Web3Response<Result>(rpcResponse: rpcResponse.response)
response(result)
} catch {
Expand Down
7 changes: 5 additions & 2 deletions Sources/MagicSDK/Core/Relayer/Types/BasicTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ enum OutboundMessageType: String, CaseIterable {
case MAGIC_HANDLE_REQUEST
}

struct RequestData<T: Codable>: Codable {
struct MagicRequestData<T: Codable>: Codable {

let msgType: String
let payload: T
let rt: String?
let jwt: String?
}

struct ResponseData<T: Codable>: Codable {
struct MagicResponseData<T: Codable>: Codable {

let msgType: String
let response: T
let rt: String?
}
79 changes: 79 additions & 0 deletions Sources/MagicSDK/Core/Relayer/Types/DPop.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// File.swift
//
//
// Created by Wentao Liu on 7/11/23.
//

import Foundation
import CryptoKit


func base64UrlEncoded(_ data: Data) -> String {
var b64 = data.base64EncodedString()
b64 = b64.replacingOccurrences(of: "+", with: "-")
b64 = b64.replacingOccurrences(of: "/", with: "_")
b64 = b64.replacingOccurrences(of: "=", with: "")
return b64
}

func createJwt() -> String?{
var error: Unmanaged<CFError>?

do {
let privateKey = try retrieveKeyFromKeyChain()

// Get the public key.
let publicKey = privateKey.publicKey

// Get the raw representation of the public key.
let rawPublicKey = publicKey.rawRepresentation

// Extract the x and y coordinates.
let xCoordinateData = rawPublicKey[1..<33]
let yCoordinateData = rawPublicKey[33..<65]

// If you need base64-encoded strings for JWK:
let xCoordinateBase64 = base64UrlEncoded(xCoordinateData)
let yCoordinateBase64 = base64UrlEncoded(yCoordinateData)
// Convert the public key to JWK format.
// construct headers
var headers: [String: Any] = ["typ": "dpop+jwt", "alg": "ES256"]
headers["jwk"] = [
"kty": "EC",
"crv": "P-256",
"x": xCoordinateBase64,
"y": yCoordinateBase64
] as [String : Any]

let headersData = try JSONSerialization.data(withJSONObject: headers)
let headersB64 = base64UrlEncoded(headersData)


// construct claims
let iat = Int(Date().timeIntervalSince1970)
let jti = UUID().uuidString.lowercased()

let claims: [String: Any] = ["iat": iat, "jti": jti]
let claimsData = try JSONSerialization.data(withJSONObject: claims)
let claimsB64 = base64UrlEncoded(claimsData)

/// sign
let signingInput = headersB64 + "." + claimsB64
let signingInputData = signingInput.data(using: .utf8)!

let signature = try! privateKey.signature(for: signingInputData)

let signatureB64 = base64UrlEncoded(signature.rawRepresentation)

let jwt = signingInput + "." + signatureB64

return jwt

} catch {
// silently handled error
return nil
}

}

Loading

0 comments on commit 347be41

Please sign in to comment.