Skip to content

Commit

Permalink
feat: debugging, downloading inputs and outputs and cleansing
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed Oct 20, 2022
1 parent e2e1cad commit 347d7f4
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 239 deletions.
8 changes: 6 additions & 2 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/

public class AppletInstructions {


public AppletInstructions() {
}

Expand Down Expand Up @@ -42,13 +44,15 @@ public AppletInstructions() {
* Below are debug classes
*/
public static final short CLASS_DOWNLOAD_GLOBAL_MAP = 5; // p1 which global map, here always 0
public static final short INS_DOWNLOAD_NUM_OUTPUT = 4;
public static final short INS_DOWNLOAD_NUM_INPUT = 5;
public static final short INS_DOWNLOAD_NUM_INPUT_V0 = 5;
public static final short INS_DOWNLOAD_NUM_OUTPUT_V0 = 4;
public static final short INS_DOWNLOAD_VERSION = 6;
public static final short INS_DOWNLOAD_SIZE = 7;
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 CLASS_DOWNLOAD_INPUT_V0 = 12;
public static final short CLASS_DOWNLOAD_OUTPUT_V0 = 13;
public static final short CLASS_DOWNLOAD_GLOBAL_ALL = 8;
public static final short CLASS_DOWNLOAD_INPUT_ALL = 9;
public static final short CLASS_DOWNLOAD_OUTPUT_ALL = 10;
Expand Down
33 changes: 24 additions & 9 deletions applet/src/main/java/applet/FromApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Arrays;

import static applet.MainApplet.PSBTdata;

public class FromApplet {
/**
Expand All @@ -16,29 +15,45 @@ public class FromApplet {
*/

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

static void send_data(APDU apdu, short data) {
apdu.getBuffer()[0] = (byte) (data << 8);
apdu.getBuffer()[1] = (byte) (data);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

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.getBuffer()[2] = (byte) ((map.map_start + map.map_size) << 8);
apdu.getBuffer()[3] = (byte) (map.map_start + map.map_size);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, KeyPair keyPair) {
apdu.getBuffer()[0] = (byte) (keyPair.key.start << 8);
apdu.getBuffer()[1] = (byte) (keyPair.key.start);
apdu.getBuffer()[2] = (byte) (keyPair.getSize() << 8);
apdu.getBuffer()[3] = (byte) (keyPair.getSize());
apdu.getBuffer()[2] = (byte) ((keyPair.key.start + keyPair.getSize()) << 8);
apdu.getBuffer()[3] = (byte) (keyPair.key.start + keyPair.getSize());
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

static void send_data(APDU apdu, short data) {
apdu.getBuffer()[0] = (byte) (data << 8);
apdu.getBuffer()[1] = (byte) (data);
public static void send_data(APDU apdu, GlobalUnsignedTXInput input) {
apdu.getBuffer()[0] = (byte) (input.previous_output_start << 8);
apdu.getBuffer()[1] = (byte) (input.previous_output_start);
apdu.getBuffer()[2] = (byte) ((input.size + input.previous_output_start) << 8);
apdu.getBuffer()[3] = (byte) (input.size + input.previous_output_start);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}

public static void send_data(APDU apdu, GlobalUnsignedTXOutput output) {
apdu.getBuffer()[0] = (byte) (output.value_start << 8);
apdu.getBuffer()[1] = (byte) (output.value_start);
apdu.getBuffer()[2] = (byte) ((output.size + output.value_start) << 8);
apdu.getBuffer()[3] = (byte) (output.size + output.value_start);
apdu.setOutgoingAndSend((short) 0, (short) 4);
}
}
Expand Down
5 changes: 0 additions & 5 deletions applet/src/main/java/applet/GeneralMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ 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());
//System.out.print("arrayIndex = " + arrayIndex + System.lineSeparator());
key_pairs[current_key_pair].fill((short) (arrayIndex + map_size));
map_size += key_pairs[current_key_pair].getSize();
//System.out.print("currentKeyPairSize = " + keyPairs[currentKeyPair].getSize() + System.lineSeparator());
//System.out.print("mapsize = " + mapSize + System.lineSeparator());
//System.out.print("arrayIndex = " + arrayIndex + System.lineSeparator());
}
}
}
7 changes: 0 additions & 7 deletions applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public class GlobalMap extends GeneralMap {
Short input_maps_total = null;
Short output_maps_total = null;
Short PSBTversion = null;
//PGU_TX_keypair fancyKeyPairInfo = new PGU_TX_keypair();
GlobalUnsignedTX globalUnsignedTX = new GlobalUnsignedTX();

public void fill(short arrayIndex) {
Expand All @@ -17,11 +16,8 @@ public void fill(short arrayIndex) {
key_pairs[current_key_pair].fill((short) (arrayIndex + map_size));

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_UNSIGNED_TX) {
assert PSBTversion == null || PSBTversion != 2;
globalUnsignedTX.fill((short) (arrayIndex + map_size + 2 +
key_pairs[current_key_pair].value.value_len_bytes));
assert globalUnsignedTX.size == key_pairs[current_key_pair].value.value_len;
System.out.print("PSBTv0 parsed" + System.lineSeparator());
}

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_INPUT_COUNT) {
Expand All @@ -34,14 +30,11 @@ public void fill(short arrayIndex) {

if (key_pairs[current_key_pair].key.key_type == PSBT_GLOBAL_TX_VERSION) {
PSBTversion = (short) key_pairs[current_key_pair].value.getByte((short) 3);
assert PSBTversion != 2 || globalUnsignedTX.start == null;
}

// TODO maybe add more special key types here later on

map_size += key_pairs[current_key_pair].getSize();
}
System.out.print("input_map_total: " + input_maps_total + System.lineSeparator());
System.out.print("output_map_total: " + output_maps_total + System.lineSeparator());
}
}
14 changes: 0 additions & 14 deletions applet/src/main/java/applet/GlobalUnsignedTX.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void fill(short arrayIndex){
controlArray[0] = (byte) getCount();
size += byteSizeOfCWI(input_count);

assert (input_count) <= MAX_COUNT_OF_IO;

for (short i = 0; i < input_count; i++) {
inputs[i].fill((short) (start + size));
Expand All @@ -48,7 +47,6 @@ public void fill(short arrayIndex){

output_count = getCount();
controlArray[1] = (byte) getCount();
assert output_count <= MAX_COUNT_OF_IO;
size += byteSizeOfCWI(output_count);

for (short i = 0; i < output_count; i++) {
Expand All @@ -57,25 +55,13 @@ public void fill(short arrayIndex){
}
lockTimeStart = size;
size += 4;
print();
}
short getCount() {
return compactWeirdoInt((short) (start + size));
}

void print(){
System.out.print(("RAW TRANSACTION:" + System.lineSeparator()));
System.out.print("starts at: " + start + System.lineSeparator());
System.out.print("version: " + version + System.lineSeparator());
System.out.print("size: " + size + System.lineSeparator());
System.out.print("input count: " + input_count + System.lineSeparator());
System.out.print("output count: " + output_count + System.lineSeparator());
System.out.print("lockTimeStart: " + lockTimeStart + System.lineSeparator());
}

short ignoreInput(short bytesIgnored) {
short signature_scrip_size = compactWeirdoInt((short) (bytesIgnored + 36));
System.out.print("signature_script_size = " + signature_scrip_size + System.lineSeparator());
return (short) (signature_scrip_size + 40 + byteSizeOfCWI(signature_scrip_size));
}
}
11 changes: 1 addition & 10 deletions applet/src/main/java/applet/GlobalUnsignedTXInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static applet.Tools.compactWeirdoInt;

public class GlobalUnsignedTXInput {
Short previous_output_start = null;
short previous_output_start = -1;
Short script_size_start = null;
Short script_size = null;
Short script_sig_start = null;
Expand All @@ -19,13 +19,4 @@ void fill(short start){
sequence_start = (short) (script_sig_start + script_size);
size = (short) (36 + byteSizeOfCWI(script_size) + script_size + 4); // easier to read and understand this way
}

void print() {
System.out.print("previousOutputStart: " + previous_output_start + System.lineSeparator());
System.out.print("scriptSizeStart: " + script_sig_start + System.lineSeparator());
System.out.print("scriptSize: " + script_size + System.lineSeparator());
System.out.print("scriptSigStart: " + script_sig_start + System.lineSeparator());
System.out.print("sequenceStart: " + sequence_start + System.lineSeparator());
System.out.print("size: " + size + System.lineSeparator());
}
}
10 changes: 1 addition & 9 deletions applet/src/main/java/applet/GlobalUnsignedTXOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static applet.Tools.compactWeirdoInt;

public class GlobalUnsignedTXOutput {
Short value_start = null;
short value_start = -1;
Short script_size_tart = null;
Short script_size = null;
Short script_pub_key_start = null;
Expand All @@ -17,12 +17,4 @@ void fill(short start){
script_pub_key_start = (short) (script_size_tart + byteSizeOfCWI(script_size));
size = (short) (8 + byteSizeOfCWI(script_size) + script_size); // easier to read and understand this way
}

void print(){
System.out.print("valueStart: " + value_start + System.lineSeparator());
System.out.print("scriptSizeStart: " + script_size_tart + System.lineSeparator());
System.out.print("scriptSize: " + script_size + System.lineSeparator());
System.out.print("scriptPubKeyStart: " + script_pub_key_start + System.lineSeparator());
System.out.print(("size: " + size + System.lineSeparator()));
}
}
3 changes: 0 additions & 3 deletions applet/src/main/java/applet/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ public void fill(short arrayIndex) {
}

public short getSize() {
assert key_len != null;
return (short) (key_len_bytes + key_len);
}

public byte getByte(short index) {
assert index <= key_len;
return PSBTdata[start + key_len_bytes + index];
}

public void setByte(short index, byte newByte) {
assert index <= key_len;
PSBTdata[start + key_len_bytes + index] = newByte;
}
}
41 changes: 14 additions & 27 deletions applet/src/main/java/applet/MainApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javacard.framework.*;
import javacard.security.RandomData;

import java.util.Arrays; // this import won't be needed in applet itself

public class MainApplet extends Applet implements MultiSelectable {
public static final short MAX_SIZE_OF_PSBT = 1024 * 6;
Expand Down Expand Up @@ -63,13 +62,11 @@ public void process(APDU apdu) {
if (ins == AppletInstructions.INS_UPLOAD) {

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) {

System.out.print(Arrays.toString(PSBTdata));
try {
psbt.fill();
} catch (Exception e) {
Expand Down Expand Up @@ -109,10 +106,8 @@ public void process(APDU apdu) {
}

if (cla == AppletInstructions.CLASS_DEBUG_DOWNLOAD && ins == AppletInstructions.INS_DOWNLOAD_ARRAY) {
short from = (short) (apduBuffer[ISO7816.OFFSET_CDATA] << 8 | apduBuffer[ISO7816.OFFSET_CDATA + 1]);
short to = (short) (apduBuffer[ISO7816.OFFSET_CDATA + 2] << 8 | apduBuffer[ISO7816.OFFSET_CDATA + 3]);
System.out.print("from: " + from + System.lineSeparator());
System.out.print("to: " + to + System.lineSeparator());
short from = (short) ((apduBuffer[ISO7816.OFFSET_CDATA] & 0xff) << 8 | (apduBuffer[ISO7816.OFFSET_CDATA + 1] & 0xff));
short to = (short) ((apduBuffer[ISO7816.OFFSET_CDATA + 2] & 0xff) << 8 | (apduBuffer[ISO7816.OFFSET_CDATA + 3] & 0xff));
if (from < 0 || to > PSBTdata.length) {
try {
throw new Exception("Can't download data outside of array!");
Expand Down Expand Up @@ -142,43 +137,35 @@ public void process(APDU apdu) {
FromApplet.send_data(apdu, offset);
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_INPUT){
if (psbt.global_map.input_maps_total != null) {
FromApplet.send_data(apdu, psbt.global_map.input_maps_total);
}
if (psbt.global_map.globalUnsignedTX.input_count != null) {
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.input_count);
}
if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_INPUT_V0){
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.input_count);
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_OUTPUT){
if (psbt.global_map.input_maps_total != null) {
FromApplet.send_data(apdu, psbt.global_map.output_maps_total);
}
if (psbt.global_map.globalUnsignedTX.output_count != null) {
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.output_count);
}
if (cla == AppletInstructions.CLASS_DOWNLOAD_GLOBAL_MAP && ins == AppletInstructions.INS_DOWNLOAD_NUM_OUTPUT_V0){
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.output_count);
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_INPUT_V0){
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.inputs[p1]);
}

if (cla == AppletInstructions.CLASS_DOWNLOAD_OUTPUT_V0){
FromApplet.send_data(apdu, psbt.global_map.globalUnsignedTX.outputs[p1]);
}

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

if (cla == AppletInstructions.CLASS_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.CLASS_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.CLASS_DOWNLOAD_GLOBAL_MAP_KEYPAIR) { // TODO: same
assert p1 >= 0;
assert p1 <= psbt.global_map.current_key_pair;
FromApplet.send_data(apdu, psbt.global_map.key_pairs[p1]);
}

Expand Down
16 changes: 0 additions & 16 deletions applet/src/main/java/applet/PSBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ public PSBT() {
}

public void fill() throws Exception {
/**
assert data[0] == 0x70;
assert data[1] == 0x73;
assert data[2] == 0x62;
assert data[3] == 0x74;
assert (data[4] & 0xff) == 0xff;
TODO: delete later
**/

if (((PSBTdata[0] & 0xff) != 0x70) ||
((PSBTdata[1] & 0xff) != 0x73) ||
((PSBTdata[2] & 0xff) != 0x62) ||
Expand All @@ -39,34 +30,27 @@ public void fill() throws Exception {
throw new Exception("Unsupported (black and forbidden) magic!");
}

System.out.print("magic is ok" + System.lineSeparator());
byte_size += (short) 5;

global_map.fill((short) 5);

byte_size += global_map.map_size;

assert PSBTdata[byte_size] == 0x00;
byte_size++;

System.out.print("global map successfully filled" + System.lineSeparator());

//TODO: check that current IOMap != numOfIOMaps

while (global_map.input_maps_total != null && current_input_map < global_map.input_maps_total) {
input_maps[current_input_map].fill(byte_size);
byte_size += input_maps[current_input_map].map_size;
assert PSBTdata[byte_size] == 0x00;
System.out.print("input map with index " + current_input_map + "successfully filled" + System.lineSeparator());
byte_size++;
current_input_map++;
}

while (global_map.output_maps_total != null && current_output_map < global_map.output_maps_total) {
output_maps[current_output_map].fill(byte_size);
byte_size += output_maps[current_output_map].map_size;
assert PSBTdata[byte_size] == 0x00;
System.out.print("output map with index " + current_output_map + "successfully filled" + System.lineSeparator());
byte_size++;
current_output_map++;
}
Expand Down
13 changes: 0 additions & 13 deletions applet/src/main/java/applet/Policy.java

This file was deleted.

Loading

0 comments on commit 347d7f4

Please sign in to comment.