Skip to content

Commit

Permalink
feat: added hex representation for comfort
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed Nov 20, 2022
1 parent 3538dbf commit 67cadca
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public void fill(short arrayIndex) {
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_INPUT_COUNT) {
input_maps_total = (short) key_pairs[current_key_pair].value.getByte((short) 0);
input_maps_total = key_pairs[current_key_pair].value.getByte((short) 0);
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_OUTPUT_COUNT) {
output_maps_total = (short) key_pairs[current_key_pair].value.getByte((short) 0);
output_maps_total = key_pairs[current_key_pair].value.getByte((short) 0);
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_TX_VERSION) {
PSBTversion = (short) key_pairs[current_key_pair].value.getByte((short) 3);
PSBTversion = key_pairs[current_key_pair].value.getByte((short) 3);
}

// TODO maybe add more special key types here later on
Expand Down
16 changes: 16 additions & 0 deletions applet/src/main/java/main/AppletControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import applet.MainApplet;
import com.licel.jcardsim.base.CardManager;
import com.licel.jcardsim.base.Simulator;
import com.licel.jcardsim.bouncycastle.util.encoders.Hex;
import com.licel.jcardsim.smartcardio.CardSimulator;
import com.licel.jcardsim.utils.AIDUtil;
import javacard.framework.AID;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class AppletControl {
Expand Down Expand Up @@ -39,10 +41,12 @@ public void DownloadDebugV0() throws Exception {
for (i = 0; i < inps; i++){
System.out.print("Input with index: " + i + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadInputV0(simulator, (byte) i)) + System.lineSeparator());
System.out.print(bytesToHex((md.downloadInputV0(simulator, (byte) i))) + System.lineSeparator());
}
for (i = 0; i < outs; i++){
System.out.print("Output with index: " + i + System.lineSeparator());
System.out.print(Arrays.toString(md.downloadOutputV0(simulator, (byte) i)) + System.lineSeparator());
System.out.print(bytesToHex((md.downloadOutputV0(simulator, (byte) i))) + System.lineSeparator());
}
}

Expand Down Expand Up @@ -99,6 +103,18 @@ public static byte[] fromHex(String hex){

return res;
}
public String bytesToHex(byte[] bytes) {
// nemam tucha jestli to funguje dobře
StringBuilder res = new StringBuilder();
int cb;

for (byte aByte : bytes) {
cb = aByte & 0xFF;
res.append(Integer.toHexString(cb / 16));
res.append(Integer.toHexString(cb % 16));
}
return res.toString();
}
}


Expand Down
19 changes: 6 additions & 13 deletions applet/src/main/java/main/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@
public class Run {
public static void main(String[] args) throws Exception {
// ./gradlew run --args="arg1 arg2 arg3"
System.out.print("Enter a PSBT transaction: " + System.lineSeparator());
String psbt = TransactionsImported.validTransaction8;
if (args.length < 1) {
System.out.print("No transaction detected." + System.lineSeparator());
System.out.print("run template: `./gradlew run --args=\"your_transaction\"`" + System.lineSeparator());
return;
}
System.out.print("You entered a following transaction:" + System.lineSeparator());
System.out.print(psbt + System.lineSeparator());
System.out.print(args[0] + System.lineSeparator());
System.out.print(System.lineSeparator());

// 1. create simulator
CardSimulator simulator = new CardSimulator();
/**

// 2. install applet
AID appletAID = AIDUtil.create("F000000001");
simulator.installApplet(appletAID, MainApplet.class);
// 3. select applet
simulator.selectApplet(appletAID);*/

// 4. send APDU
AppletControl ap = new AppletControl(simulator, args[0]);
ap.UploadTransaction();
ap.DownloadDebugV0();
Expand Down
1 change: 0 additions & 1 deletion applet/src/main/java/main/Upload.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.nio.charset.StandardCharsets;

/**
* class of my tests
Expand Down
10 changes: 10 additions & 0 deletions applet/src/test/java/tests/AppletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void transaction1() throws Exception {
download.downloadInputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction1: passed" + System.lineSeparator());
}

@Test
Expand All @@ -115,6 +116,7 @@ public void transaction2() throws Exception {
download.downloadInputV0(simulator, (byte) 1);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction2: passed" + System.lineSeparator());
}

@Test
Expand All @@ -130,6 +132,7 @@ public void transaction3() throws Exception {
download.downloadInputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction3: passed" + System.lineSeparator());
}

@Test
Expand All @@ -146,6 +149,7 @@ public void transaction4() throws Exception {
download.downloadInputV0(simulator, (byte) 1);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction4: passed" + System.lineSeparator());
}

@Test
Expand All @@ -160,6 +164,7 @@ public void transaction5() throws Exception {
download.downloadSize(simulator);
download.downloadInputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 0);
System.out.print("Test transaction5: passed" + System.lineSeparator());
}

@Test
Expand All @@ -174,6 +179,7 @@ public void transaction6() throws Exception {
download.downloadSize(simulator);
download.downloadInputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 0);
System.out.print("Test transaction6: passed" + System.lineSeparator());
}

@Test
Expand All @@ -188,6 +194,7 @@ public void transaction7() throws Exception {
download.downloadSize(simulator);
download.downloadInputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 0);
System.out.print("Test transaction7: passed" + System.lineSeparator());
}

@Test
Expand All @@ -204,6 +211,7 @@ public void transaction8() throws Exception {
download.downloadInputV0(simulator, (byte) 1);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction8: passed" + System.lineSeparator());
}

@Test
Expand All @@ -216,6 +224,7 @@ public void transaction9() throws Exception {
assert download.downloadNumOfInp(simulator) == 0;
assert download.downloadNumOfOut(simulator) == 0;
download.downloadSize(simulator);
System.out.print("Test transaction9: passed" + System.lineSeparator());
}

@Test
Expand All @@ -230,6 +239,7 @@ public void transaction10() throws Exception {
download.downloadSize(simulator);
download.downloadOutputV0(simulator, (byte) 0);
download.downloadOutputV0(simulator, (byte) 1);
System.out.print("Test transaction10: passed" + System.lineSeparator());
}

//@Test
Expand Down

0 comments on commit 67cadca

Please sign in to comment.