Skip to content

Commit

Permalink
implement v2 factory reset (#51)
Browse files Browse the repository at this point in the history
implement v2 factory reset
  • Loading branch information
bitgamma authored Sep 16, 2024
1 parent 93dd647 commit 6de4b0d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'org.bouncycastle:bcprov-jdk15on:1.60'
implementation 'org.apache.commons:commons-lang3:3.9'
implementation 'com.github.status-im.status-keycard-java:android:3.1.1'
implementation 'com.github.status-im.status-keycard-java:android:3.1.2'
}
39 changes: 32 additions & 7 deletions android/src/main/java/im/status/ethereum/keycard/SmartCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,17 @@ public WritableMap getApplicationInfo() throws IOException, APDUException {
return cardInfo;
}

public WritableMap factoryReset() throws IOException, APDUException {
public WritableMap factoryResetPost() throws IOException, APDUException {
ApplicationInfo info = new ApplicationInfo(new KeycardCommandSet(this.cardChannel).select().checkOK().getData());
Log.i(TAG, "Selecting the factory reset Keycard applet succeeded");

WritableMap cardInfo = Arguments.createMap();
cardInfo.putBoolean("initialized?", info.isInitializedCard());

return cardInfo;
}

public WritableMap factoryResetFallback() throws IOException, APDUException {
GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet(this.cardChannel);
cmdSet.select().checkOK();
Log.i(TAG, "ISD selected");
Expand All @@ -287,15 +297,30 @@ public WritableMap factoryReset() throws IOException, APDUException {
Log.i(TAG, "Keycard applet instance deleted");

cmdSet.installKeycardApplet().checkOK();
Log.i(TAG, "Keycard applet instance re-installed");
Log.i(TAG, "Keycard applet instance re-installed");

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

WritableMap cardInfo = Arguments.createMap();
cardInfo.putBoolean("initialized?", info.isInitializedCard());
public WritableMap factoryReset() throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
APDUResponse resp = cmdSet.select();

return cardInfo;
if (!resp.isOK()) {
return factoryResetFallback();
}

ApplicationInfo info = new ApplicationInfo(resp.getData());

if (!info.hasFactoryResetCapability()) {
return factoryResetFallback();
}

if (!cmdSet.factoryReset().isOK()) {
return factoryResetFallback();
}

return factoryResetPost();
}

public void deriveKey(final String path, final String pin) throws IOException, APDUException {
Expand Down
41 changes: 35 additions & 6 deletions ios/SmartCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ class SmartCard {
resolve(true)
}

func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
func factoryResetPost(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let info = try ApplicationInfo(KeycardCommandSet(cardChannel: channel).select().checkOK().data)
os_log("Selecting the factory reset Keycard applet succeeded")

var cardInfo = [String: Any]()
cardInfo["initialized?"] = info.initializedCard

resolve(cardInfo)
}

func factoryResetFallback(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let cmdSet: GlobalPlatformCommandSet = GlobalPlatformCommandSet(cardChannel: channel);
try cmdSet.select().checkOK()
os_log("ISD selected")
Expand All @@ -103,13 +113,32 @@ class SmartCard {
try cmdSet.installKeycardInstance().checkOK()
os_log("Keycard applet instance re-installed")

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

var cardInfo = [String: Any]()
cardInfo["initialized?"] = info.initializedCard
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let cmdSet = KeycardCommandSet(cardChannel: channel)
var resp = try cmdSet.select()

if (resp.sw != 0x9000) {
factoryResetFallback(channel, resolve, reject)
return
}

resolve(cardInfo)
let info = try ApplicationInfo(resp.data)
if (!info.hasFactoryResetCapability) {
factoryResetFallback(channel, resolve, reject)
return
}

resp = try cmdSet.factoryReset()

if (resp.sw != 0x9000) {
factoryResetFallback(channel, resolve, reject)
return
}

factoryResetPost(channel, resolve, reject)
}

func getApplicationInfo(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-status-keycard",
"homepage": "https://keycard.status.im/",
"version": "2.5.39",
"version": "2.5.40",
"description": "React Native library to interact with Status Keycard using NFC connection",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 6de4b0d

Please sign in to comment.