Skip to content

Commit 6de4b0d

Browse files
authored
implement v2 factory reset (#51)
implement v2 factory reset
1 parent 93dd647 commit 6de4b0d

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ dependencies {
4646
implementation 'com.facebook.react:react-native:+'
4747
implementation 'org.bouncycastle:bcprov-jdk15on:1.60'
4848
implementation 'org.apache.commons:commons-lang3:3.9'
49-
implementation 'com.github.status-im.status-keycard-java:android:3.1.1'
49+
implementation 'com.github.status-im.status-keycard-java:android:3.1.2'
5050
}

android/src/main/java/im/status/ethereum/keycard/SmartCard.java

+32-7
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,17 @@ public WritableMap getApplicationInfo() throws IOException, APDUException {
275275
return cardInfo;
276276
}
277277

278-
public WritableMap factoryReset() throws IOException, APDUException {
278+
public WritableMap factoryResetPost() throws IOException, APDUException {
279+
ApplicationInfo info = new ApplicationInfo(new KeycardCommandSet(this.cardChannel).select().checkOK().getData());
280+
Log.i(TAG, "Selecting the factory reset Keycard applet succeeded");
281+
282+
WritableMap cardInfo = Arguments.createMap();
283+
cardInfo.putBoolean("initialized?", info.isInitializedCard());
284+
285+
return cardInfo;
286+
}
287+
288+
public WritableMap factoryResetFallback() throws IOException, APDUException {
279289
GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet(this.cardChannel);
280290
cmdSet.select().checkOK();
281291
Log.i(TAG, "ISD selected");
@@ -287,15 +297,30 @@ public WritableMap factoryReset() throws IOException, APDUException {
287297
Log.i(TAG, "Keycard applet instance deleted");
288298

289299
cmdSet.installKeycardApplet().checkOK();
290-
Log.i(TAG, "Keycard applet instance re-installed");
300+
Log.i(TAG, "Keycard applet instance re-installed");
291301

292-
ApplicationInfo info = new ApplicationInfo(new KeycardCommandSet(this.cardChannel).select().checkOK().getData());
293-
Log.i(TAG, "Selecting the newly installed Keycard applet succeeded");
302+
return factoryResetPost();
303+
}
294304

295-
WritableMap cardInfo = Arguments.createMap();
296-
cardInfo.putBoolean("initialized?", info.isInitializedCard());
305+
public WritableMap factoryReset() throws IOException, APDUException {
306+
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
307+
APDUResponse resp = cmdSet.select();
297308

298-
return cardInfo;
309+
if (!resp.isOK()) {
310+
return factoryResetFallback();
311+
}
312+
313+
ApplicationInfo info = new ApplicationInfo(resp.getData());
314+
315+
if (!info.hasFactoryResetCapability()) {
316+
return factoryResetFallback();
317+
}
318+
319+
if (!cmdSet.factoryReset().isOK()) {
320+
return factoryResetFallback();
321+
}
322+
323+
return factoryResetPost();
299324
}
300325

301326
public void deriveKey(final String path, final String pin) throws IOException, APDUException {

ios/SmartCard.swift

+35-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ class SmartCard {
8989
resolve(true)
9090
}
9191

92-
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
92+
func factoryResetPost(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
93+
let info = try ApplicationInfo(KeycardCommandSet(cardChannel: channel).select().checkOK().data)
94+
os_log("Selecting the factory reset Keycard applet succeeded")
95+
96+
var cardInfo = [String: Any]()
97+
cardInfo["initialized?"] = info.initializedCard
98+
99+
resolve(cardInfo)
100+
}
101+
102+
func factoryResetFallback(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
93103
let cmdSet: GlobalPlatformCommandSet = GlobalPlatformCommandSet(cardChannel: channel);
94104
try cmdSet.select().checkOK()
95105
os_log("ISD selected")
@@ -103,13 +113,32 @@ class SmartCard {
103113
try cmdSet.installKeycardInstance().checkOK()
104114
os_log("Keycard applet instance re-installed")
105115

106-
let info = try ApplicationInfo(KeycardCommandSet(cardChannel: channel).select().checkOK().data)
107-
os_log("Selecting the newly installed Keycard applet succeeded")
116+
factoryResetPost(channel, resolve, reject)
117+
}
108118

109-
var cardInfo = [String: Any]()
110-
cardInfo["initialized?"] = info.initializedCard
119+
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
120+
let cmdSet = KeycardCommandSet(cardChannel: channel)
121+
var resp = try cmdSet.select()
122+
123+
if (resp.sw != 0x9000) {
124+
factoryResetFallback(channel, resolve, reject)
125+
return
126+
}
111127

112-
resolve(cardInfo)
128+
let info = try ApplicationInfo(resp.data)
129+
if (!info.hasFactoryResetCapability) {
130+
factoryResetFallback(channel, resolve, reject)
131+
return
132+
}
133+
134+
resp = try cmdSet.factoryReset()
135+
136+
if (resp.sw != 0x9000) {
137+
factoryResetFallback(channel, resolve, reject)
138+
return
139+
}
140+
141+
factoryResetPost(channel, resolve, reject)
113142
}
114143

115144
func getApplicationInfo(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-status-keycard",
33
"homepage": "https://keycard.status.im/",
4-
"version": "2.5.39",
4+
"version": "2.5.40",
55
"description": "React Native library to interact with Status Keycard using NFC connection",
66
"main": "index.js",
77
"scripts": {

0 commit comments

Comments
 (0)