Skip to content

Commit

Permalink
feat: added debugging by downloading parts of psbt thats on card
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed Jul 1, 2022
1 parent 7bd9e0d commit 9c2613b
Show file tree
Hide file tree
Showing 22 changed files with 778 additions and 1,085 deletions.
503 changes: 166 additions & 337 deletions applet/build/reports/tests/test/classes/tests.AppletTest.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions applet/build/reports/tests/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h1>Test Summary</h1>
</td>
<td>
<div class="infoBox" id="duration">
<div class="counter">0.314s</div>
<div class="counter">0.171s</div>
<p>duration</p>
</div>
</td>
Expand Down Expand Up @@ -85,7 +85,7 @@ <h2>Packages</h2>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0.314s</td>
<td>0.171s</td>
<td class="success">100%</td>
</tr>
</tbody>
Expand All @@ -112,7 +112,7 @@ <h2>Classes</h2>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0.314s</td>
<td>0.171s</td>
<td class="success">100%</td>
</tr>
</tbody>
Expand All @@ -126,7 +126,7 @@ <h2>Classes</h2>
<input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
</label>
</div>Generated by
<a href="http://www.gradle.org">Gradle 6.6</a> at 28 Apr 2022, 3:02:31 AM</p>
<a href="http://www.gradle.org">Gradle 6.6</a> at 1 Jul 2022, 4:46:09 AM</p>
</div>
</div>
</body>
Expand Down
6 changes: 3 additions & 3 deletions applet/build/reports/tests/test/packages/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1>Package tests</h1>
</td>
<td>
<div class="infoBox" id="duration">
<div class="counter">0.314s</div>
<div class="counter">0.171s</div>
<p>duration</p>
</div>
</td>
Expand Down Expand Up @@ -83,7 +83,7 @@ <h2>Classes</h2>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0.314s</td>
<td>0.171s</td>
<td class="success">100%</td>
</tr>
</table>
Expand All @@ -96,7 +96,7 @@ <h2>Classes</h2>
<input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
</label>
</div>Generated by
<a href="http://www.gradle.org">Gradle 6.6</a> at 28 Apr 2022, 3:02:31 AM</p>
<a href="http://www.gradle.org">Gradle 6.6</a> at 1 Jul 2022, 4:46:09 AM</p>
</div>
</div>
</body>
Expand Down
501 changes: 165 additions & 336 deletions applet/build/test-results/test/TEST-tests.AppletTest.xml

Large diffs are not rendered by default.

497 changes: 163 additions & 334 deletions applet/build/test-results/test/binary/output.bin

Large diffs are not rendered by default.

Binary file modified applet/build/test-results/test/binary/output.bin.idx
Binary file not shown.
Binary file modified applet/build/test-results/test/binary/results.bin
Binary file not shown.
16 changes: 8 additions & 8 deletions applet/build/tmp/compileJava/source-classes-mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ applet/AppletInstructions.java
applet.AppletInstructions
applet/KeyPair.java
applet.KeyPair
applet/GlobalMap.java
applet.GlobalMap
applet/GlobalUnsignedTXOutput.java
applet.GlobalUnsignedTXOutput
applet/GlobalUnsignedTX.java
applet.GlobalUnsignedTX
applet/PSBT.java
applet.PSBT
applet/FromApplet.java
applet.FromApplet
applet/GlobalUnsignedTXInput.java
applet.GlobalUnsignedTXInput
applet/SekriptInstructions.java
Expand All @@ -26,9 +26,9 @@ applet/Stack.java
applet.Stack
applet/SecretAndTimeStorage.java
applet.SecretAndTimeStorage
applet/FromApplet.java
applet.FromApplet
applet/GlobalUnsignedTXOutput.java
applet.GlobalUnsignedTXOutput
applet/GlobalMap.java
applet.GlobalMap
applet/PSBT.java
applet.PSBT
applet/MainApplet.java
applet.MainApplet
18 changes: 12 additions & 6 deletions applet/build/tmp/compileTestJava/source-classes-mapping.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
tests/TransactionsImported.java
tests.TransactionsImported
tests/Upload.java
tests.Upload
tests/MyTests.java
tests.MyTests
tests/Download.java
tests.Download
tests/ImpTrxUnitTests.java
tests.ImpTrxUnitTests
tests/AppletComunicationTools.java
tests.AppletComunicationTools
tests/BaseTest.java
tests.BaseTest
tests/TransactionsImported.java
tests.TransactionsImported
tests/MyTests.java
tests.MyTests
tests/AppletControl.java
tests.AppletControl
tests/AppletTest.java
tests.AppletTest
tests/ImpTrxUnitTests.java
tests.ImpTrxUnitTests
26 changes: 21 additions & 5 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package applet;

import org.graalvm.compiler.graph.Node;

/**
* Class of applet instructions to make testing and changing instructions easier
* both MainApplet and MyTests are getting instructions from this class
Expand All @@ -25,18 +27,30 @@ public AppletInstructions() {
* when I plan to create different upload class for PSBT transaction and different class for
* applet initialization
*/
public static final short APPLET_INSTRUCTION_SUCC_MORE = (short) 0x9001;

public static final short INSTRUCTION_VERSION = 1;
public static final short INSTRUCTION_VERSION = 0;


public static final short CLASS_PSBT_UPLOAD = 0;
// one array of size of AppletInstruction.APDU_PACKET_SIZE is sent back as control data

public static final short CLASS_POLICY_UPLOAD = 1;
// simple scenario where applet returns "HAND SHAKE" in bytes
public static final short HAND_SHAKE = 3;
public static final short CLASS_SECRETandTIME_UPLOAD = 2; // p1 will determine reference, where to store it
public static final short CLASS_DEBUG_DOWNLOAD = 4;

/**
* Below are debug classes
*/
public static final short CLASS_DOWNLOAD_GLOBAL_MAP = 5; // p1 which global map, here always 0
public static final short CLASS_DOWNLOAD_INPUT_MAP = 6; // p1 is position of map, map must be present, first is 0
public static final short CLASS_DOWNLOAD_OUTPUT_MAP = 7; // --above--

public static final short CLASSS_DOWNLOAD_GLOBAL_ALL = 8;
public static final short CLASSS_DOWNLOAD_INPUT_ALL = 9;
public static final short CLASSS_DOWNLOAD_OUTPUT_ALL = 10;

public static final short HAND_SHAKE = 3;
// simple scenario where applet returns "HAND SHAKE" in bytes


public static final short CLASS_PSBT_UPLOAD_AND_BACK = 4;
Expand All @@ -46,9 +60,11 @@ public AppletInstructions() {
public static final short INS_REQUEST = 0;
public static final short INS_UPLOAD = 1;
public static final short INS_FINISH = 2;
public static final short INS_DOWNLOAD_ARRAY = 3;


//global keytype bytes below
public static final byte PSBT_GLOBAL_UNSIGNED_TX = 0x00;
public static final byte PSBT_GLOBAL_UNSIGNED_TX = 0x00; // p2
public static final byte PSBT_GLOBAL_XPUB = 0x01;
public static final byte PSBT_GLOBAL_TX_VERSION = 0x02;
public static final byte PSBT_GLOBAL_FALLBACK_LOCKTIME = 0x03;
Expand Down
23 changes: 19 additions & 4 deletions applet/src/main/java/applet/FromApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import javacard.framework.APDU;
import javacard.framework.Util;

import java.util.Arrays;

import static applet.MainApplet.PSBTdata;

public class FromApplet {
Expand All @@ -10,10 +13,22 @@ public class FromApplet {
* @param apdu apdu used for communication
* @param array array of data to send
*/
static void send_data(APDU apdu, byte[] array)
{
Util.arrayCopyNonAtomic(array, (short) 0, apdu.getBuffer(), (short) 0, (short) array.length);
apdu.setOutgoingAndSend((short) 0, (short) array.length);

static void send_data(APDU apdu, byte[] array, short from, short to) {
Util.arrayCopyNonAtomic(array, (short) 0, apdu.getBuffer(), (short) 0, (short) (to - from));
apdu.setOutgoingAndSend((short) 0, (short) (to - from));
}

static void send_data(APDU apdu, GeneralMap map) {
apdu.getBuffer()[0] = (byte) (map.map_start << 8);
apdu.getBuffer()[1] = (byte) (map.map_start);
apdu.getBuffer()[2] = (byte) (map.map_size << 8);
apdu.getBuffer()[3] = (byte) (map.map_size);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, KeyPair key_pair) {
//TODO rewrite this to take a byte of KeyPair type instead of itself
send_data(apdu, PSBTdata, key_pair.start, key_pair.getSize());
}
}
5 changes: 4 additions & 1 deletion applet/src/main/java/applet/GeneralMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static applet.MainApplet.PSBTdata;

public class GeneralMap {

public short map_start = -1;
public short NUM_OF_KEYPAIR = 8;
public short current_key_pair = -1;
public KeyPair[] key_pairs = new KeyPair[NUM_OF_KEYPAIR];
Expand All @@ -16,7 +18,8 @@ public GeneralMap() {
}
}

public void fillUp(short arrayIndex) {
public void fill(short arrayIndex) {
map_start = (short) (arrayIndex + 1);
while ((PSBTdata[arrayIndex + map_size] & 0xff) != 0x00 && current_key_pair < NUM_OF_KEYPAIR - 1) {
current_key_pair++;
//System.out.print("mapsize = " + mapSize + System.lineSeparator());
Expand Down
3 changes: 2 additions & 1 deletion applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class GlobalMap extends GeneralMap {
//PGU_TX_keypair fancyKeyPairInfo = new PGU_TX_keypair();
GlobalUnsignedTX globalUnsignedTX = new GlobalUnsignedTX();

public void fillUp(short arrayIndex) {
public void fill(short arrayIndex) {
map_start = (short) (arrayIndex + 1);
while ((PSBTdata[arrayIndex + map_size] & 0xff) != 0x00 && current_key_pair < NUM_OF_KEYPAIR - 1) {
current_key_pair++;
key_pairs[current_key_pair].fill((short) (arrayIndex + map_size));
Expand Down
2 changes: 2 additions & 0 deletions applet/src/main/java/applet/KeyPair.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package applet;

public class KeyPair {
public short start = -1;
public Key key = new Key();
public Value value = new Value();

public void fill(short arrayIndex) {
start = arrayIndex;
key.fill(arrayIndex);
value.fill((short) (arrayIndex + key.getSize()));
}
Expand Down
75 changes: 56 additions & 19 deletions applet/src/main/java/applet/MainApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public MainApplet(byte[] buffer, short offset, byte length) {
psbt = new PSBT();
PSBTdata = new byte[MAX_SIZE_OF_PSBT]; // to change PSBT max size change this constant
controlArray = new byte[AppletInstructions.PACKET_BUFFER_SIZE]; // array that is sent back to computer as confirmation
controlArray[0] = 0;
controlArray[1] = 1;
controlArray[2] = 2;
controlArray[3] = 3;
controlArray[4] = 4;
controlArray[5] = 5;
controlArray[6] = 6;
controlArray[7] = 7;
this.offset = 0;

random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
Expand All @@ -50,30 +58,23 @@ public void process(APDU apdu) {
*/
if (cla == AppletInstructions.CLASS_PSBT_UPLOAD) {
if (ins == AppletInstructions.INS_REQUEST) {
//good
// TODO: return array size
}
if (ins == AppletInstructions.INS_UPLOAD){
if (ins == AppletInstructions.INS_UPLOAD) {

Util.arrayCopyNonAtomic(apduBuffer, (short) 5, PSBTdata, offset, (short) (lc & 0xff));
Util.arrayCopyNonAtomic(apduBuffer, ISO7816.OFFSET_CDATA, PSBTdata, offset, (short) (lc & 0xff));
System.out.print(Arrays.toString(PSBTdata) + System.lineSeparator());
offset += (short) (lc & 0xff);
}
if (ins == AppletInstructions.INS_FINISH){

if (ins == AppletInstructions.INS_FINISH) {

System.out.print(Arrays.toString(PSBTdata));
try {
psbt.fill();
} catch (Exception e) {
e.printStackTrace();
}

//controlArray[0] = (byte) 42;
//controlArray[1] = (byte) 69;
controlArray[2] = (byte) 42;
controlArray[3] = (byte) 69;

FromApplet.send_data(apdu, controlArray);
// sends control array back to computer
}
}
//ISOException.throwIt((short) 0x9000); how it looks
Expand All @@ -82,36 +83,72 @@ public void process(APDU apdu) {
* this uploads Policy represented as array of bytes
*/
if (cla == AppletInstructions.CLASS_POLICY_UPLOAD) {
if (ins == AppletInstructions.INS_REQUEST){
if (ins == AppletInstructions.INS_REQUEST) {
//do smth here
}
if (ins == AppletInstructions.INS_UPLOAD){
if (ins == AppletInstructions.INS_UPLOAD) {
//do smth here
}
if (ins == AppletInstructions.INS_FINISH){
if (ins == AppletInstructions.INS_FINISH) {
//do smth here
}
}
/**
* this uploads data(secrets and time signed by authority) for Policy
*/
if (cla == AppletInstructions.CLASS_SECRETandTIME_UPLOAD) {
if (ins == AppletInstructions.INS_REQUEST){
if (ins == AppletInstructions.INS_REQUEST) {
//do smth here
}
if (ins == AppletInstructions.INS_UPLOAD){
if (ins == AppletInstructions.INS_UPLOAD) {
//do smth here
}
if (ins == AppletInstructions.INS_FINISH){
if (ins == AppletInstructions.INS_FINISH) {
//do smth here
}
}

if (cla == AppletInstructions.CLASS_DEBUG_DOWNLOAD && ins == AppletInstructions.INS_DOWNLOAD_ARRAY) {
short from = Tools.toShort(apduBuffer[ISO7816.OFFSET_CDATA], apduBuffer[ISO7816.OFFSET_CDATA+ 1]);
short to = Tools.toShort(apduBuffer[ISO7816.OFFSET_CDATA + 2], apduBuffer[ISO7816.OFFSET_CDATA + 3]);
System.out.print("from: " + from + System.lineSeparator());
System.out.print("to: " + to + System.lineSeparator());
if (from < 0 || to > PSBTdata.length) {
try {
throw new Exception("Can't download data outside of array!");
} catch (Exception e) {
e.printStackTrace();
}
} else if (to - from < 0) {
try {
throw new Exception("Negate amount of bytes can't be transferred!");
} catch (Exception e) {
e.printStackTrace();
}
}
FromApplet.send_data(apdu, PSBTdata, from, to);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_GLOBAL_ALL) {
FromApplet.send_data(apdu, psbt.global_map);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_INPUT_ALL) { // TODO: change asserts to exception warning
assert p1 >= 0;
assert p1 <= psbt.current_input_map;
FromApplet.send_data(apdu, psbt.input_maps[p1]);
}

if (cla == AppletInstructions.CLASSS_DOWNLOAD_OUTPUT_ALL) { // TODO: same
assert p1 >= 0;
assert p1 <= psbt.current_output_map;
FromApplet.send_data(apdu, psbt.output_maps[p1]);
}

if (cla == AppletInstructions.HAND_SHAKE) {
byte[] HAND_SHAKE = {'H', 'A', 'N', 'D', ' ', 'S', 'H', 'A', 'K', 'E'};
Util.arrayCopyNonAtomic(HAND_SHAKE, (short) 0, apduBuffer, (short) 0, (short) HAND_SHAKE.length);
apdu.setOutgoingAndSend((short) 0, (short) HAND_SHAKE.length);
return;
}
}

Expand Down
Loading

0 comments on commit 9c2613b

Please sign in to comment.