Skip to content

Commit

Permalink
Merge pull request #2 from ravikp/openid-ios-chunker
Browse files Browse the repository at this point in the history
Openid ios chunker
  • Loading branch information
vharsh authored Jan 23, 2023
2 parents fe4b2d8 + 03d4678 commit d01f610
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 52 deletions.
6 changes: 5 additions & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ PODS:
- FlipperKit/FlipperKitNetworkPlugin
- fmt (6.2.1)
- glog (0.3.5)
- GzipSwift (5.1.1)
- hermes-engine (0.70.6)
- libevent (2.1.12)
- OpenSSL-Universal (1.1.1100)
Expand Down Expand Up @@ -303,6 +304,7 @@ PODS:
- React-logger (0.70.6):
- glog
- react-native-openid4vp-ble (0.1.0):
- GzipSwift
- React-Core
- React-perflogger (0.70.6)
- React-RCTActionSheet (0.70.6):
Expand Down Expand Up @@ -450,6 +452,7 @@ SPEC REPOS:
- Flipper-RSocket
- FlipperKit
- fmt
- GzipSwift
- libevent
- OpenSSL-Universal
- SocketRocket
Expand Down Expand Up @@ -544,6 +547,7 @@ SPEC CHECKSUMS:
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
GzipSwift: 893f3e48e597a1a4f62fafcb6514220fcf8287fa
hermes-engine: 2af7b7a59128f250adfd86f15aa1d5a2ecd39995
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
Expand All @@ -562,7 +566,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: b4a65947391c658450151275aa406f2b8263178f
React-jsinspector: 60769e5a0a6d4b32294a2456077f59d0266f9a8b
React-logger: 1623c216abaa88974afce404dc8f479406bbc3a0
react-native-openid4vp-ble: 18282d8ac2050613523bd85289ca47753afac0f4
react-native-openid4vp-ble: 8ee0f01a69c757238f97c7cc613ddd024a7e4cee
React-perflogger: 8c79399b0500a30ee8152d0f9f11beae7fc36595
React-RCTActionSheet: 7316773acabb374642b926c19aef1c115df5c466
React-RCTAnimation: 5341e288375451297057391227f691d9b2326c3d
Expand Down
8 changes: 8 additions & 0 deletions ios/Openid4vpBle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
8D31C3FC2956B20F0073B710 /* WalletCryptoBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C3FB2956B20F0073B710 /* WalletCryptoBox.swift */; };
8D31C3FE2956B2650073B710 /* WalletCryptoBoxBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C3FD2956B2650073B710 /* WalletCryptoBoxBuilder.swift */; };
8D31C4022956B3EF0073B710 /* WalletCryptoBoxImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C4012956B3EF0073B710 /* WalletCryptoBoxImpl.swift */; };
E223F0322977BE090042F919 /* TransferHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E223F0312977BE090042F919 /* TransferHandler.swift */; };
E25800A42976BF8700968EA0 /* chunker.swift in Sources */ = {isa = PBXBuildFile; fileRef = E25800A32976BF8700968EA0 /* chunker.swift */; };
E2F1AFCB297176C200AC3355 /* BLEConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F1AFCA297176C200AC3355 /* BLEConstants.swift */; };
E2F1AFCD2971883500AC3355 /* WalletViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F1AFCC2971883500AC3355 /* WalletViewModel.swift */; };
F4FF95D7245B92E800C19C63 /* Openid4vpBle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* Openid4vpBle.swift */; };
Expand Down Expand Up @@ -72,6 +74,8 @@
8D31C3FD2956B2650073B710 /* WalletCryptoBoxBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletCryptoBoxBuilder.swift; sourceTree = "<group>"; };
8D31C4012956B3EF0073B710 /* WalletCryptoBoxImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletCryptoBoxImpl.swift; sourceTree = "<group>"; };
B3E7B5891CC2AC0600A0062D /* Openid4vpBle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Openid4vpBle.m; sourceTree = "<group>"; };
E223F0312977BE090042F919 /* TransferHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransferHandler.swift; sourceTree = "<group>"; };
E25800A32976BF8700968EA0 /* chunker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = chunker.swift; sourceTree = "<group>"; };
E2F1AFCA297176C200AC3355 /* BLEConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLEConstants.swift; sourceTree = "<group>"; };
E2F1AFCC2971883500AC3355 /* WalletViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletViewModel.swift; sourceTree = "<group>"; };
F4FF95D5245B92E700C19C63 /* Openid4vpBle-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Openid4vpBle-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -233,6 +237,8 @@
isa = PBXGroup;
children = (
E2F1AFCA297176C200AC3355 /* BLEConstants.swift */,
E25800A32976BF8700968EA0 /* chunker.swift */,
E223F0312977BE090042F919 /* TransferHandler.swift */,
);
path = Utility;
sourceTree = "<group>";
Expand Down Expand Up @@ -302,6 +308,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E223F0322977BE090042F919 /* TransferHandler.swift in Sources */,
8D069F752968305B00AB61A9 /* EventEmitter.swift in Sources */,
8D31C3F82956B0B30073B710 /* SecretTranslator.swift in Sources */,
8D069F69296447D000AB61A9 /* Peripheral.swift in Sources */,
Expand All @@ -311,6 +318,7 @@
8D069F672964475A00AB61A9 /* CentralManagerDelegate.swift in Sources */,
8D069F6D2964724700AB61A9 /* Characteristics.swift in Sources */,
0A62BB712938C1EC0092E47D /* Wallet.swift in Sources */,
E25800A42976BF8700968EA0 /* chunker.swift in Sources */,
0A62BB732938C1FB0092E47D /* Verifier.swift in Sources */,
8D069F6B29644B7C00AB61A9 /* PeripheralExtension.swift in Sources */,
8D179C8F29548B3A00017EE5 /* CryptoBoxImpl.swift in Sources */,
Expand Down
9 changes: 7 additions & 2 deletions ios/Openid4vpBle/Openid4vpBle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Openid4vpBle: RCTEventEmitter {
}
return dictonary!
}

@objc
func getConnectionParametersDebug() -> String {
return "GetConnectionParametersDebug"
Expand Down Expand Up @@ -66,8 +67,11 @@ class Openid4vpBle: RCTEventEmitter {
case "exchange-sender-info":
print("EXCHANGE-SENDER-INFO")
callback([])
// Wallet.shared.registerCallbackForEvent(event: "EXCHANGE-SENDER-INFO", callback: callback)
//Wallet.shared.registerCallbackForEvent(event: "EXCHANGE-SENDER-INFO", callback: callback)
Wallet.shared.writeIdentity()
case "send-vc":
callback([])
Wallet.shared.sendData(data: messageComponents[1])
default:
print("DEFAULT SEND: MESSAGE:: ", message)
}
Expand All @@ -80,7 +84,8 @@ class Openid4vpBle: RCTEventEmitter {
print("Advertiser")
case "discoverer":
print("Discoverer")
Wallet.shared.central = Central()
Central.shared.initialize()
Wallet.shared.central = Central.shared
Wallet.shared.registerCallbackForEvent(event: "CREATE_CONNECTION", callback: callback)
default:
print("DEFAULT CASE: MESSAGE:: ", mode)
Expand Down
48 changes: 32 additions & 16 deletions ios/Wallet/Wallet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Gzip

@objc(Wallet)
@available(iOS 13.0, *)
Expand All @@ -23,7 +24,6 @@ class Wallet: NSObject {
}

func registerCallbackForEvent(event: String, callback: @escaping RCTResponseSenderBlock) {

NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: event), object: nil, queue: nil) { [unowned self] notification in
print("Handling notification for \(notification.name.rawValue)")
callback([])
Expand Down Expand Up @@ -61,21 +61,37 @@ class Wallet: NSObject {
}
return data
}

@available(iOS 13.0, *)
func writeIdentity() {
print("::: write idendity called ::: ")
let publicKey = WalletCryptoBoxImpl().getPublicKey()
print("verifier pub key:::", self.verifierPublicKey)
guard let verifierPublicKey = self.verifierPublicKey else {
print("Write Identity - Found NO KEY")
return

func sendData(data: String){
var dataInBytes = Data(data.utf8)
var compressedBytes = try! dataInBytes.gzipped()
var encryptedData = secretTranslator?.encryptToSend(data: compressedBytes)
if (encryptedData != nil) {
DispatchQueue.main.async {
let transferHandler = TransferHandler.shared
transferHandler.initialize(initdData: encryptedData!)
let imsgBuilder = imessage(msgType: .INIT_RESPONSE_TRANSFER, data: encryptedData!)
transferHandler.sendMessage(message: imsgBuilder)
}
} else {

}
}
@available(iOS 13.0, *)
func writeIdentity() {
print("::: write idendity called ::: ")
let publicKey = WalletCryptoBoxImpl().getPublicKey()
print("verifier pub key:::", self.verifierPublicKey)
guard let verifierPublicKey = self.verifierPublicKey else {
print("Write Identity - Found NO KEY")
return
}
self.secretTranslator = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: verifierPublicKey)
var iv = (self.secretTranslator?.initializationVector())!
central?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.identifyRequestCharacteristic, data: iv + publicKey)
NotificationCenter.default.post(name: Notification.Name(rawValue: "EXCHANGE-SENDER-INFO"), object: nil)
}
self.secretTranslator = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: verifierPublicKey)
var iv = (self.secretTranslator?.initializationVector())!
central?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: TransferService.identifyRequestCharacteristic, data: iv + publicKey)
NotificationCenter.default.post(name: Notification.Name(rawValue: "EXCHANGE-SENDER-INFO"), object: nil)
}
}

}


3 changes: 2 additions & 1 deletion ios/Wallet/WalletViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct WalletViewModel {
print("verifier pub key:::", BLEConstants.verifierPublicKey)
secretTranslators = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: BLEConstants.verifierPublicKey)
var iv = (secretTranslators?.initializationVector())!
central.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: TransferService.identifyRequestCharacteristic, data: iv + publicKey)
central.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: NetworkCharNums.identifyRequestCharacteristic, data: iv + publicKey)
}

mutating func setAdvIdentifier(advIdentifier: String) {
Expand All @@ -51,3 +51,4 @@ struct WalletViewModel {
BLEConstants.verifierPublicKey = publicKeyData
}
}

3 changes: 3 additions & 0 deletions ios/ble/Utility/BLEConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ struct BLEConstants {
static let SCAN_RESPONSE_SERVICE_UUID = CBUUID(string: "0000AB2A-0000-1000-8000-00805f9b34fb")
static var ADV_IDENTIFIER = ""
static var verifierPublicKey: Data = Data()
static var DEFAULT_CHUNK_SIZE = 185
static var seqNumberReservedByteSize = 2
static var mtuReservedByteSize = 2
}
158 changes: 158 additions & 0 deletions ios/ble/Utility/TransferHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

import Foundation

@available(iOS 13.0, *)
class TransferHandler {
var data: Data?
private var currentState: States = States.UnInitialised
private var responseStartTimeInMillis: UInt64 = 0
var chunker: Chunker?

public static var shared = TransferHandler()

func initialize(initdData: Data) {
data = initdData
}

func sendMessage(message: imessage) {
handleMessage(msg: message)
}
deinit{
print("deinit happend in transferh")
}
private func handleMessage(msg: imessage){
if msg.msgType == .INIT_RESPONSE_TRANSFER {
var responseData = msg.data!
print("Total response size of data",responseData.count)
chunker = Chunker(chunkData: responseData, mtuSize: BLEConstants.DEFAULT_CHUNK_SIZE)
print("MTU found to be", BLEConstants.DEFAULT_CHUNK_SIZE)
currentState = States.ResponseSizeWritePending
sendMessage(message: imessage(msgType: .ResponseSizeWritePendingMessage, data: responseData, dataSize: responseData.count))
}
else if msg.msgType == .ResponseSizeWritePendingMessage {
sendResponseSize(size: msg.dataSize!)
}
else if msg.msgType == .RESPONSE_SIZE_WRITE_SUCCESS {
responseStartTimeInMillis = Utils.currentTimeInMilliSeconds()
currentState = States.ResponseSizeWriteSuccess
initResponseChunkSend()
} else if msg.msgType == .INIT_RESPONSE_CHUNK_TRANSFER {
currentState = .ResponseWritePending
sendResponseChunk()
}
else if msg.msgType == .READ_TRANSMISSION_REPORT {
currentState = States.WaitingForTransferReport
requestTransmissionReport()
}
else if msg.msgType == .HANDLE_TRANSMISSION_REPORT {
currentState = States.HandlingTransferReport
var handleTransmissionReportMessage = msg.data
handleTransmissionReport(report: handleTransmissionReportMessage!)
}
else {
print("out of scope")
}
}

private func requestTransmissionReport() {
var notifyObj: Data = Data()
Central.shared.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: NetworkCharNums.semaphoreCharacteristic, data: withUnsafeBytes(of: 1.bigEndian) { Data($0) })
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "HANDLE_TRANSMISSION_REPORT"), object: nil, queue: nil) { [unowned self] notification in
print("Handling notification for \(notification.name.rawValue)")
notifyObj = notification.object as! Data
}
sendMessage(message: imessage(msgType: .HANDLE_TRANSMISSION_REPORT, data: notifyObj))
}

private func handleTransmissionReport(report: Data) {
// if (report.type == TransferReport.ReportType.SUCCESS) {
// currentState = States.TransferVerified
// transferListener.onResponseSent()
// print(logTag, "handleMessage: Successfully transferred vc in ${System.currentTimeMillis() - responseStartTimeInMillis}ms")
// } else if(report.type == TransferReport.ReportType.MISSING_CHUNKS && report.missingSequences != null && !isRetryFrame) {
// currentState = States.PartiallyTransferred
// this.sendMessage(InitRetryTransferMessage(report.missingSequences))
// } else {
// this.sendMessage(ResponseTransferFailureMessage("Invalid Report"))
// }
print("report is :::", String(data: report, encoding: .utf8))
}

private func sendResponseSize(size: Int) {
// TODO: Send a stringified number in a byte array
let decimalString = String(size)
let d = decimalString.data(using: .utf8)
print(d!)
Central.shared.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.responseSizeCharacteristic, data: d!)
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "RESPONSE_SIZE_WRITE_SUCCESS"), object: nil, queue: nil) { [unowned self] notification in
print("Handling notification for \(notification.name.rawValue)")
sendMessage(message: imessage(msgType: .RESPONSE_SIZE_WRITE_SUCCESS, data: data))
}
}

private func initResponseChunkSend() {
print("initResponseChunkSend")
sendMessage(message: imessage(msgType: .INIT_RESPONSE_CHUNK_TRANSFER, data: data, dataSize: data?.count))
}

private func sendResponseChunk() {
if let chunker = chunker {
if chunker.isComplete() {
print("Data send complete")
sendMessage(message: imessage(msgType: .READ_TRANSMISSION_REPORT))
return
}

var done = false
while !done {
sleep(10)
let chunk = chunker.next()
if chunk.isEmpty {
done = true
sendMessage(message: imessage(msgType: .INIT_RESPONSE_CHUNK_TRANSFER, data: data, dataSize: data?.count))
}
else {
Central.shared.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.responseCharacteristic, data: chunk)
}

}
}
}
}

enum TransferMessageTypes {
case INIT_RESPONSE_TRANSFER
case ResponseSizeWritePendingMessage
case RESPONSE_SIZE_WRITE_SUCCESS
case INIT_RESPONSE_CHUNK_TRANSFER
case RESPONSE_TRANSFER_COMPLETE
case READ_TRANSMISSION_REPORT
case HANDLE_TRANSMISSION_REPORT
}

struct imessage {
var msgType: TransferMessageTypes
var data: Data?
var dataSize: Int?
}

enum States {
case UnInitialised
case ResponseSizeWritePending
case ResponseSizeWriteSuccess
case ResponseSizeWriteFailed
case ResponseWritePending
case ResponseWriteFailed
case TransferComplete
case WaitingForTransferReport
case HandlingTransferReport
case TransferVerified
case PartiallyTransferred
}

enum SemaphoreMarker: Int {
case UnInitialised = 0
case RequestReport = 1
case Error = 2
}

Loading

0 comments on commit d01f610

Please sign in to comment.