@@ -70,47 +70,11 @@ abstract class CardManager {
7070 }
7171
7272 open suspend fun readRecordsFromCard (options : ReadableArray , promise : Promise ) {
73- val readableArray = Arguments .createArray()
74- val readableMap = Arguments .createMap()
75- readableMap.putString(" cardId" , cardId)
76- readableMap.putString(" samId" , samId)
77-
7873 try {
7974 waitForCard()
8075 connectCard()
8176
82- options.toArrayList().forEach {
83- it as HashMap <* , * >
84- val application = it[" application" ] as ArrayList <Int >
85- val sfi = (it[" sfi" ] as Double ).toInt()
86- val offset = (it[" offset" ] as Double ).toInt()
87- val readMode = when ((it[" readMode" ] as Double ).toInt()) {
88- 1 -> CardReadRecordsBuilder .ReadMode .MULTIPLE_RECORD
89- else -> CardReadRecordsBuilder .ReadMode .ONE_RECORD
90- }
91-
92- selectApplication(application.map { it.toByte() }.toByteArray())
93- selectFileBuilder()
94- val readRecordsParser = readRecordsFromSfi(sfi, offset, readMode)
95-
96- val records: Map <Int , ByteArray > = readRecordsParser.records
97-
98- val resultMap = Arguments .createMap()
99- val recordsMap = Arguments .createMap()
100-
101- for ((key, record) in records) {
102- val array = ByteConvertReactNativeUtil .byteArrayToReadableArray(record)
103- recordsMap.putArray(key.toString(), array)
104- }
105-
106- resultMap.putInt(" sfi" , sfi)
107- resultMap.putMap(" records" , recordsMap)
108-
109- readableArray.pushMap(resultMap)
110- }
111-
112- readableMap.putArray(" data" , readableArray)
113- promise.resolve(readableMap)
77+ read(options, promise)
11478 } catch (e: Throwable ) {
11579 e.printStackTrace()
11680 promise.reject(
@@ -131,29 +95,7 @@ abstract class CardManager {
13195 waitForCard()
13296 connectCard()
13397
134- options.toArrayList().forEach {
135- it as HashMap <* , * >
136- val apdu = it[" apdu" ] as ArrayList <Int >
137- val application = it[" application" ] as ArrayList <Int >
138- val sfi = (it[" sfi" ] as Double ).toInt()
139- val offset = (it[" offset" ] as Double ).toInt()
140- val samUnlockString = it[" samUnlockString" ] as String
141-
142- val newRecord = apdu.map { it.toByte() }.toByteArray();
143-
144- val selectApplicationParser = selectApplication(ByteConvertReactNativeUtil .arrayListToByteArray(application))
145- unlockSam(samUnlockString)
146- selectSamDiversifier(selectApplicationParser)
147- val samChallengeParser = samChallenge()
148- val openSession3Parser = openSession3(samChallengeParser, sfi, offset)
149- samDigestInit(openSession3Parser)
150- updateRecord(sfi, offset, newRecord)
151- val samDigestCloseParser = samDigestClose()
152- val closeSession3Parser = closeSession3(samDigestCloseParser)
153- samDigestAuthenticate(closeSession3Parser)
154- }
155-
156- promise.resolve(true )
98+ write(options, promise)
15799 } catch (e: Exception ) {
158100 when (e) {
159101 is PosException -> promise.reject(e.code, e.message)
@@ -165,6 +107,104 @@ abstract class CardManager {
165107 }
166108 }
167109
110+ // region Unsafe methods for custom workflows
111+
112+ protected fun read (options : ReadableArray , promise : Promise ) {
113+ val readableArray = Arguments .createArray()
114+ val readableMap = Arguments .createMap()
115+ readableMap.putString(" cardId" , cardId)
116+ readableMap.putString(" samId" , samId)
117+
118+ options.toArrayList().forEach {
119+ it as HashMap <* , * >
120+ val application = it[" application" ] as ArrayList <Int >
121+ val sfi = (it[" sfi" ] as Double ).toInt()
122+ val offset = (it[" offset" ] as Double ).toInt()
123+ val readMode = when ((it[" readMode" ] as Double ).toInt()) {
124+ 1 -> CardReadRecordsBuilder .ReadMode .MULTIPLE_RECORD
125+ else -> CardReadRecordsBuilder .ReadMode .ONE_RECORD
126+ }
127+
128+ selectApplication(application.map { it.toByte() }.toByteArray())
129+ selectFileBuilder()
130+ val readRecordsParser = readRecordsFromSfi(sfi, offset, readMode)
131+
132+ val records: Map <Int , ByteArray > = readRecordsParser.records
133+
134+ val resultMap = Arguments .createMap()
135+ val recordsMap = Arguments .createMap()
136+
137+ for ((key, record) in records) {
138+ val array = ByteConvertReactNativeUtil .byteArrayToReadableArray(record)
139+ recordsMap.putArray(key.toString(), array)
140+ }
141+
142+ resultMap.putInt(" sfi" , sfi)
143+ resultMap.putMap(" records" , recordsMap)
144+
145+ readableArray.pushMap(resultMap)
146+ }
147+
148+ readableMap.putArray(" data" , readableArray)
149+ promise.resolve(readableMap)
150+ }
151+
152+ protected fun write (options : ReadableArray , promise : Promise ) {
153+ options.toArrayList().forEach {
154+ it as HashMap <* , * >
155+ val apdu = it[" apdu" ] as ArrayList <Int >
156+ val application = it[" application" ] as ArrayList <Int >
157+ val sfi = (it[" sfi" ] as Double ).toInt()
158+ val offset = (it[" offset" ] as Double ).toInt()
159+ val samUnlockString = it[" samUnlockString" ] as String
160+
161+ val newRecord = apdu.map { it.toByte() }.toByteArray();
162+
163+ val selectApplicationParser = selectApplication(ByteConvertReactNativeUtil .arrayListToByteArray(application))
164+ unlockSam(samUnlockString)
165+ selectSamDiversifier(selectApplicationParser)
166+ val samChallengeParser = samChallenge()
167+ val openSession3Parser = openSession3(samChallengeParser, sfi, offset)
168+ samDigestInit(openSession3Parser)
169+ updateRecord(sfi, offset, newRecord)
170+ val samDigestCloseParser = samDigestClose()
171+ val closeSession3Parser = closeSession3(samDigestCloseParser)
172+ samDigestAuthenticate(closeSession3Parser)
173+ }
174+
175+ promise.resolve(true )
176+ }
177+
178+ fun unsafeConnectSam () {
179+ connectSam()
180+ }
181+
182+ open suspend fun unsafeWaitForCard (promise : Promise ) {
183+ waitForCard()
184+ }
185+
186+ fun unsafeConnectCard () {
187+ connectCard()
188+ }
189+
190+ fun unsafeRead (options : ReadableArray , promise : Promise ) {
191+ read(options, promise)
192+ }
193+
194+ fun unsafeWrite (options : ReadableArray , promise : Promise ) {
195+ write(options, promise)
196+ }
197+
198+ fun unsafeDisconnectSam () {
199+ disconnectSam()
200+ }
201+
202+ fun unsafeDisconnectCard () {
203+ disconnectCard()
204+ }
205+
206+ // endregion
207+
168208 private fun selectApplication (application : ByteArray ): SelectApplicationParser {
169209 val selectApplicationBuilder = SelectApplicationBuilder (
170210 SelectApplicationBuilder .SELECT_FIRST_OCCURRENCE_RETURN_FCI , application)
0 commit comments