Skip to content

Commit d01f610

Browse files
authored
Merge pull request #2 from ravikp/openid-ios-chunker
Openid ios chunker
2 parents fe4b2d8 + 03d4678 commit d01f610

13 files changed

+407
-52
lines changed

example/ios/Podfile.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ PODS:
7373
- FlipperKit/FlipperKitNetworkPlugin
7474
- fmt (6.2.1)
7575
- glog (0.3.5)
76+
- GzipSwift (5.1.1)
7677
- hermes-engine (0.70.6)
7778
- libevent (2.1.12)
7879
- OpenSSL-Universal (1.1.1100)
@@ -303,6 +304,7 @@ PODS:
303304
- React-logger (0.70.6):
304305
- glog
305306
- react-native-openid4vp-ble (0.1.0):
307+
- GzipSwift
306308
- React-Core
307309
- React-perflogger (0.70.6)
308310
- React-RCTActionSheet (0.70.6):
@@ -450,6 +452,7 @@ SPEC REPOS:
450452
- Flipper-RSocket
451453
- FlipperKit
452454
- fmt
455+
- GzipSwift
453456
- libevent
454457
- OpenSSL-Universal
455458
- SocketRocket
@@ -544,6 +547,7 @@ SPEC CHECKSUMS:
544547
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
545548
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
546549
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
550+
GzipSwift: 893f3e48e597a1a4f62fafcb6514220fcf8287fa
547551
hermes-engine: 2af7b7a59128f250adfd86f15aa1d5a2ecd39995
548552
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
549553
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
@@ -562,7 +566,7 @@ SPEC CHECKSUMS:
562566
React-jsiexecutor: b4a65947391c658450151275aa406f2b8263178f
563567
React-jsinspector: 60769e5a0a6d4b32294a2456077f59d0266f9a8b
564568
React-logger: 1623c216abaa88974afce404dc8f479406bbc3a0
565-
react-native-openid4vp-ble: 18282d8ac2050613523bd85289ca47753afac0f4
569+
react-native-openid4vp-ble: 8ee0f01a69c757238f97c7cc613ddd024a7e4cee
566570
React-perflogger: 8c79399b0500a30ee8152d0f9f11beae7fc36595
567571
React-RCTActionSheet: 7316773acabb374642b926c19aef1c115df5c466
568572
React-RCTAnimation: 5341e288375451297057391227f691d9b2326c3d

ios/Openid4vpBle.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
8D31C3FC2956B20F0073B710 /* WalletCryptoBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C3FB2956B20F0073B710 /* WalletCryptoBox.swift */; };
3030
8D31C3FE2956B2650073B710 /* WalletCryptoBoxBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C3FD2956B2650073B710 /* WalletCryptoBoxBuilder.swift */; };
3131
8D31C4022956B3EF0073B710 /* WalletCryptoBoxImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D31C4012956B3EF0073B710 /* WalletCryptoBoxImpl.swift */; };
32+
E223F0322977BE090042F919 /* TransferHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E223F0312977BE090042F919 /* TransferHandler.swift */; };
33+
E25800A42976BF8700968EA0 /* chunker.swift in Sources */ = {isa = PBXBuildFile; fileRef = E25800A32976BF8700968EA0 /* chunker.swift */; };
3234
E2F1AFCB297176C200AC3355 /* BLEConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F1AFCA297176C200AC3355 /* BLEConstants.swift */; };
3335
E2F1AFCD2971883500AC3355 /* WalletViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2F1AFCC2971883500AC3355 /* WalletViewModel.swift */; };
3436
F4FF95D7245B92E800C19C63 /* Openid4vpBle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* Openid4vpBle.swift */; };
@@ -72,6 +74,8 @@
7274
8D31C3FD2956B2650073B710 /* WalletCryptoBoxBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletCryptoBoxBuilder.swift; sourceTree = "<group>"; };
7375
8D31C4012956B3EF0073B710 /* WalletCryptoBoxImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletCryptoBoxImpl.swift; sourceTree = "<group>"; };
7476
B3E7B5891CC2AC0600A0062D /* Openid4vpBle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Openid4vpBle.m; sourceTree = "<group>"; };
77+
E223F0312977BE090042F919 /* TransferHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransferHandler.swift; sourceTree = "<group>"; };
78+
E25800A32976BF8700968EA0 /* chunker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = chunker.swift; sourceTree = "<group>"; };
7579
E2F1AFCA297176C200AC3355 /* BLEConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLEConstants.swift; sourceTree = "<group>"; };
7680
E2F1AFCC2971883500AC3355 /* WalletViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletViewModel.swift; sourceTree = "<group>"; };
7781
F4FF95D5245B92E700C19C63 /* Openid4vpBle-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Openid4vpBle-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -233,6 +237,8 @@
233237
isa = PBXGroup;
234238
children = (
235239
E2F1AFCA297176C200AC3355 /* BLEConstants.swift */,
240+
E25800A32976BF8700968EA0 /* chunker.swift */,
241+
E223F0312977BE090042F919 /* TransferHandler.swift */,
236242
);
237243
path = Utility;
238244
sourceTree = "<group>";
@@ -302,6 +308,7 @@
302308
isa = PBXSourcesBuildPhase;
303309
buildActionMask = 2147483647;
304310
files = (
311+
E223F0322977BE090042F919 /* TransferHandler.swift in Sources */,
305312
8D069F752968305B00AB61A9 /* EventEmitter.swift in Sources */,
306313
8D31C3F82956B0B30073B710 /* SecretTranslator.swift in Sources */,
307314
8D069F69296447D000AB61A9 /* Peripheral.swift in Sources */,
@@ -311,6 +318,7 @@
311318
8D069F672964475A00AB61A9 /* CentralManagerDelegate.swift in Sources */,
312319
8D069F6D2964724700AB61A9 /* Characteristics.swift in Sources */,
313320
0A62BB712938C1EC0092E47D /* Wallet.swift in Sources */,
321+
E25800A42976BF8700968EA0 /* chunker.swift in Sources */,
314322
0A62BB732938C1FB0092E47D /* Verifier.swift in Sources */,
315323
8D069F6B29644B7C00AB61A9 /* PeripheralExtension.swift in Sources */,
316324
8D179C8F29548B3A00017EE5 /* CryptoBoxImpl.swift in Sources */,

ios/Openid4vpBle/Openid4vpBle.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Openid4vpBle: RCTEventEmitter {
3838
}
3939
return dictonary!
4040
}
41+
4142
@objc
4243
func getConnectionParametersDebug() -> String {
4344
return "GetConnectionParametersDebug"
@@ -66,8 +67,11 @@ class Openid4vpBle: RCTEventEmitter {
6667
case "exchange-sender-info":
6768
print("EXCHANGE-SENDER-INFO")
6869
callback([])
69-
// Wallet.shared.registerCallbackForEvent(event: "EXCHANGE-SENDER-INFO", callback: callback)
70+
//Wallet.shared.registerCallbackForEvent(event: "EXCHANGE-SENDER-INFO", callback: callback)
7071
Wallet.shared.writeIdentity()
72+
case "send-vc":
73+
callback([])
74+
Wallet.shared.sendData(data: messageComponents[1])
7175
default:
7276
print("DEFAULT SEND: MESSAGE:: ", message)
7377
}
@@ -80,7 +84,8 @@ class Openid4vpBle: RCTEventEmitter {
8084
print("Advertiser")
8185
case "discoverer":
8286
print("Discoverer")
83-
Wallet.shared.central = Central()
87+
Central.shared.initialize()
88+
Wallet.shared.central = Central.shared
8489
Wallet.shared.registerCallbackForEvent(event: "CREATE_CONNECTION", callback: callback)
8590
default:
8691
print("DEFAULT CASE: MESSAGE:: ", mode)

ios/Wallet/Wallet.swift

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Gzip
23

34
@objc(Wallet)
45
@available(iOS 13.0, *)
@@ -23,7 +24,6 @@ class Wallet: NSObject {
2324
}
2425

2526
func registerCallbackForEvent(event: String, callback: @escaping RCTResponseSenderBlock) {
26-
2727
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: event), object: nil, queue: nil) { [unowned self] notification in
2828
print("Handling notification for \(notification.name.rawValue)")
2929
callback([])
@@ -61,21 +61,37 @@ class Wallet: NSObject {
6161
}
6262
return data
6363
}
64-
65-
@available(iOS 13.0, *)
66-
func writeIdentity() {
67-
print("::: write idendity called ::: ")
68-
let publicKey = WalletCryptoBoxImpl().getPublicKey()
69-
print("verifier pub key:::", self.verifierPublicKey)
70-
guard let verifierPublicKey = self.verifierPublicKey else {
71-
print("Write Identity - Found NO KEY")
72-
return
64+
65+
func sendData(data: String){
66+
var dataInBytes = Data(data.utf8)
67+
var compressedBytes = try! dataInBytes.gzipped()
68+
var encryptedData = secretTranslator?.encryptToSend(data: compressedBytes)
69+
if (encryptedData != nil) {
70+
DispatchQueue.main.async {
71+
let transferHandler = TransferHandler.shared
72+
transferHandler.initialize(initdData: encryptedData!)
73+
let imsgBuilder = imessage(msgType: .INIT_RESPONSE_TRANSFER, data: encryptedData!)
74+
transferHandler.sendMessage(message: imsgBuilder)
75+
}
76+
} else {
77+
78+
}
79+
}
80+
@available(iOS 13.0, *)
81+
func writeIdentity() {
82+
print("::: write idendity called ::: ")
83+
let publicKey = WalletCryptoBoxImpl().getPublicKey()
84+
print("verifier pub key:::", self.verifierPublicKey)
85+
guard let verifierPublicKey = self.verifierPublicKey else {
86+
print("Write Identity - Found NO KEY")
87+
return
88+
}
89+
self.secretTranslator = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: verifierPublicKey)
90+
var iv = (self.secretTranslator?.initializationVector())!
91+
central?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.identifyRequestCharacteristic, data: iv + publicKey)
92+
NotificationCenter.default.post(name: Notification.Name(rawValue: "EXCHANGE-SENDER-INFO"), object: nil)
7393
}
74-
self.secretTranslator = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: verifierPublicKey)
75-
var iv = (self.secretTranslator?.initializationVector())!
76-
central?.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: TransferService.identifyRequestCharacteristic, data: iv + publicKey)
77-
NotificationCenter.default.post(name: Notification.Name(rawValue: "EXCHANGE-SENDER-INFO"), object: nil)
78-
}
94+
}
7995

80-
}
8196

97+

ios/Wallet/WalletViewModel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct WalletViewModel {
3838
print("verifier pub key:::", BLEConstants.verifierPublicKey)
3939
secretTranslators = WalletCryptoBoxImpl().buildSecretsTranslator(verifierPublicKey: BLEConstants.verifierPublicKey)
4040
var iv = (secretTranslators?.initializationVector())!
41-
central.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: TransferService.identifyRequestCharacteristic, data: iv + publicKey)
41+
central.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: NetworkCharNums.identifyRequestCharacteristic, data: iv + publicKey)
4242
}
4343

4444
mutating func setAdvIdentifier(advIdentifier: String) {
@@ -51,3 +51,4 @@ struct WalletViewModel {
5151
BLEConstants.verifierPublicKey = publicKeyData
5252
}
5353
}
54+

ios/ble/Utility/BLEConstants.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ struct BLEConstants {
77
static let SCAN_RESPONSE_SERVICE_UUID = CBUUID(string: "0000AB2A-0000-1000-8000-00805f9b34fb")
88
static var ADV_IDENTIFIER = ""
99
static var verifierPublicKey: Data = Data()
10+
static var DEFAULT_CHUNK_SIZE = 185
11+
static var seqNumberReservedByteSize = 2
12+
static var mtuReservedByteSize = 2
1013
}

ios/ble/Utility/TransferHandler.swift

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
import Foundation
3+
4+
@available(iOS 13.0, *)
5+
class TransferHandler {
6+
var data: Data?
7+
private var currentState: States = States.UnInitialised
8+
private var responseStartTimeInMillis: UInt64 = 0
9+
var chunker: Chunker?
10+
11+
public static var shared = TransferHandler()
12+
13+
func initialize(initdData: Data) {
14+
data = initdData
15+
}
16+
17+
func sendMessage(message: imessage) {
18+
handleMessage(msg: message)
19+
}
20+
deinit{
21+
print("deinit happend in transferh")
22+
}
23+
private func handleMessage(msg: imessage){
24+
if msg.msgType == .INIT_RESPONSE_TRANSFER {
25+
var responseData = msg.data!
26+
print("Total response size of data",responseData.count)
27+
chunker = Chunker(chunkData: responseData, mtuSize: BLEConstants.DEFAULT_CHUNK_SIZE)
28+
print("MTU found to be", BLEConstants.DEFAULT_CHUNK_SIZE)
29+
currentState = States.ResponseSizeWritePending
30+
sendMessage(message: imessage(msgType: .ResponseSizeWritePendingMessage, data: responseData, dataSize: responseData.count))
31+
}
32+
else if msg.msgType == .ResponseSizeWritePendingMessage {
33+
sendResponseSize(size: msg.dataSize!)
34+
}
35+
else if msg.msgType == .RESPONSE_SIZE_WRITE_SUCCESS {
36+
responseStartTimeInMillis = Utils.currentTimeInMilliSeconds()
37+
currentState = States.ResponseSizeWriteSuccess
38+
initResponseChunkSend()
39+
} else if msg.msgType == .INIT_RESPONSE_CHUNK_TRANSFER {
40+
currentState = .ResponseWritePending
41+
sendResponseChunk()
42+
}
43+
else if msg.msgType == .READ_TRANSMISSION_REPORT {
44+
currentState = States.WaitingForTransferReport
45+
requestTransmissionReport()
46+
}
47+
else if msg.msgType == .HANDLE_TRANSMISSION_REPORT {
48+
currentState = States.HandlingTransferReport
49+
var handleTransmissionReportMessage = msg.data
50+
handleTransmissionReport(report: handleTransmissionReportMessage!)
51+
}
52+
else {
53+
print("out of scope")
54+
}
55+
}
56+
57+
private func requestTransmissionReport() {
58+
var notifyObj: Data = Data()
59+
Central.shared.write(serviceUuid: BLEConstants.SERVICE_UUID, charUUID: NetworkCharNums.semaphoreCharacteristic, data: withUnsafeBytes(of: 1.bigEndian) { Data($0) })
60+
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "HANDLE_TRANSMISSION_REPORT"), object: nil, queue: nil) { [unowned self] notification in
61+
print("Handling notification for \(notification.name.rawValue)")
62+
notifyObj = notification.object as! Data
63+
}
64+
sendMessage(message: imessage(msgType: .HANDLE_TRANSMISSION_REPORT, data: notifyObj))
65+
}
66+
67+
private func handleTransmissionReport(report: Data) {
68+
// if (report.type == TransferReport.ReportType.SUCCESS) {
69+
// currentState = States.TransferVerified
70+
// transferListener.onResponseSent()
71+
// print(logTag, "handleMessage: Successfully transferred vc in ${System.currentTimeMillis() - responseStartTimeInMillis}ms")
72+
// } else if(report.type == TransferReport.ReportType.MISSING_CHUNKS && report.missingSequences != null && !isRetryFrame) {
73+
// currentState = States.PartiallyTransferred
74+
// this.sendMessage(InitRetryTransferMessage(report.missingSequences))
75+
// } else {
76+
// this.sendMessage(ResponseTransferFailureMessage("Invalid Report"))
77+
// }
78+
print("report is :::", String(data: report, encoding: .utf8))
79+
}
80+
81+
private func sendResponseSize(size: Int) {
82+
// TODO: Send a stringified number in a byte array
83+
let decimalString = String(size)
84+
let d = decimalString.data(using: .utf8)
85+
print(d!)
86+
Central.shared.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.responseSizeCharacteristic, data: d!)
87+
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "RESPONSE_SIZE_WRITE_SUCCESS"), object: nil, queue: nil) { [unowned self] notification in
88+
print("Handling notification for \(notification.name.rawValue)")
89+
sendMessage(message: imessage(msgType: .RESPONSE_SIZE_WRITE_SUCCESS, data: data))
90+
}
91+
}
92+
93+
private func initResponseChunkSend() {
94+
print("initResponseChunkSend")
95+
sendMessage(message: imessage(msgType: .INIT_RESPONSE_CHUNK_TRANSFER, data: data, dataSize: data?.count))
96+
}
97+
98+
private func sendResponseChunk() {
99+
if let chunker = chunker {
100+
if chunker.isComplete() {
101+
print("Data send complete")
102+
sendMessage(message: imessage(msgType: .READ_TRANSMISSION_REPORT))
103+
return
104+
}
105+
106+
var done = false
107+
while !done {
108+
sleep(10)
109+
let chunk = chunker.next()
110+
if chunk.isEmpty {
111+
done = true
112+
sendMessage(message: imessage(msgType: .INIT_RESPONSE_CHUNK_TRANSFER, data: data, dataSize: data?.count))
113+
}
114+
else {
115+
Central.shared.write(serviceUuid: Peripheral.SERVICE_UUID, charUUID: NetworkCharNums.responseCharacteristic, data: chunk)
116+
}
117+
118+
}
119+
}
120+
}
121+
}
122+
123+
enum TransferMessageTypes {
124+
case INIT_RESPONSE_TRANSFER
125+
case ResponseSizeWritePendingMessage
126+
case RESPONSE_SIZE_WRITE_SUCCESS
127+
case INIT_RESPONSE_CHUNK_TRANSFER
128+
case RESPONSE_TRANSFER_COMPLETE
129+
case READ_TRANSMISSION_REPORT
130+
case HANDLE_TRANSMISSION_REPORT
131+
}
132+
133+
struct imessage {
134+
var msgType: TransferMessageTypes
135+
var data: Data?
136+
var dataSize: Int?
137+
}
138+
139+
enum States {
140+
case UnInitialised
141+
case ResponseSizeWritePending
142+
case ResponseSizeWriteSuccess
143+
case ResponseSizeWriteFailed
144+
case ResponseWritePending
145+
case ResponseWriteFailed
146+
case TransferComplete
147+
case WaitingForTransferReport
148+
case HandlingTransferReport
149+
case TransferVerified
150+
case PartiallyTransferred
151+
}
152+
153+
enum SemaphoreMarker: Int {
154+
case UnInitialised = 0
155+
case RequestReport = 1
156+
case Error = 2
157+
}
158+

0 commit comments

Comments
 (0)