Skip to content

Commit

Permalink
feat: all thats left is signed time and polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-sekanina committed Apr 18, 2023
1 parent 02ed072 commit 52036a0
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 152 deletions.
5 changes: 5 additions & 0 deletions applet/src/main/java/applet/AppletInstructions.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public AppletInstructions() {
public static final byte PSBT_GLOBAL_SIGHASH_SINGLE_INPUTS = 0x07;
public static final byte PSBT_GLOBAL_VERSION = (byte) 0xFB;
public static final byte PSBT_GLOBAL_PROPRIETARY = (byte) 0xFC;


public static final byte PSBT_OUT_AMOUNT = (byte) 0x03;
public static final byte PSBT_OUT_SCRIPT = (byte) 0x04;

}
15 changes: 15 additions & 0 deletions applet/src/main/java/applet/GlobalMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ public void fill(short arrayIndex) {
map_size += key_pairs[current_key_pair].getSize();
}
}

public void reset() {
short i = 0;
while (i < NUM_OF_KEYPAIR) {
key_pairs[i].reset();
i++;
}
input_maps_total = -1;
output_maps_total = -1;
PSBTversion = 0;
globalUnsignedTX.reset();
map_start = -1;
current_key_pair = -1;
map_size = 0;
}
}
12 changes: 12 additions & 0 deletions applet/src/main/java/applet/GlobalUnsignedTX.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ short ignoreInput(short bytesIgnored) {
short signature_scrip_size = compactWeirdoInt((short) (bytesIgnored + 36));
return (short) (signature_scrip_size + 40 + byteSizeOfCWI(signature_scrip_size));
}

void reset() {
short i = 0;
while (i < MAX_COUNT_OF_IO) {
inputs[i].reset();
outputs[i].reset();
i++;
}
start = -1;
version = -1;
size = 0;
}
}
9 changes: 9 additions & 0 deletions applet/src/main/java/applet/GlobalUnsignedTXInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ 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 reset(){
previous_output_start = -1;
script_size_start = -1;
script_size = -1;
script_sig_start = -1;
sequence_start = -1;
size = 0;
}
}
10 changes: 9 additions & 1 deletion applet/src/main/java/applet/GlobalUnsignedTXOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ public class GlobalUnsignedTXOutput {
short size = 0;

void fill(short start){
value_start = start;
value_start = start; // value has static size of 8 bytes
script_size_start = (short) (start + 8);
script_size = compactWeirdoInt(script_size_start);
script_pub_key_start = (short) (script_size_start + byteSizeOfCWI(script_size));
size = (short) (8 + byteSizeOfCWI(script_size) + script_size); // easier to read and understand this way
}

void reset() {
value_start = -1;
script_size_start = -1;
script_size = -1;
script_pub_key_start = -1;
size = 0;
}
}
2 changes: 1 addition & 1 deletion applet/src/main/java/applet/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Key {
public short start = -1;
public short key_len = -1;
public short key_len_bytes = 1;
public short key_type = -1;
public short key_type = -1; // in bips there is difference

public void fill(short arrayIndex) {
start = arrayIndex;
Expand Down
98 changes: 61 additions & 37 deletions applet/src/main/java/applet/MainApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ public class MainApplet extends Applet implements MultiSelectable {
* class of all instructions and other hardcoded information
*/

public PSBT psbt;
public static PSBT psbt;
public static byte[][] additionalDataStorage;
public static short[] dataStorageOffsets;
public static byte[][] checkAgainstDataStorage;
public static short[] checkAgainstDataStorageOffsets;
public static byte[] PSBTdata;
public static byte[] controlArray;
public static byte [] policy;
short transactionOffset;
short policyOffset;
short policyUploadLocked; // 0 locked, 1 - opened
public static byte[] policy;
public static byte[] totalOutput;
static short transactionOffset;
static short policyOffset;
static short policyUploadLocked; // 0 locked, 1 - opened

//private byte[] data = JCSystem.makeTransientByteArray((short) (1024 * 10),
// JCSystem.CLEAR_ON_DESELECT);
Expand All @@ -43,6 +44,7 @@ public MainApplet(byte[] buffer, short offset, byte length) {
PSBTdata = new byte[MAX_SIZE_OF_PSBT]; // to change PSBT max size change this constant
policy = new byte[MAX_SIZE_OF_POLICY];
controlArray = new byte[AppletInstructions.PACKET_BUFFER_SIZE]; // historical array that is sent back to computer as confirmation
totalOutput = new byte[8];
controlArray[0] = 0;
controlArray[1] = 1;
controlArray[2] = 2;
Expand All @@ -51,9 +53,9 @@ public MainApplet(byte[] buffer, short offset, byte length) {
controlArray[5] = 5;
controlArray[6] = 6;
controlArray[7] = 7;
this.transactionOffset = 0;
this.policyOffset = 0;
this.policyUploadLocked = 0;
transactionOffset = 0;
policyOffset = 0;
policyUploadLocked = 0;

random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);

Expand All @@ -76,16 +78,13 @@ public void process(APDU apdu) {
if (ins == AppletInstructions.INS_REQUEST) {
// TODO: return array size
transactionOffset = 0;
psbt.reset(); // maybe arbitrary
}
if (ins == AppletInstructions.INS_UPLOAD) {

Util.arrayCopyNonAtomic(apduBuffer, ISO7816.OFFSET_CDATA, PSBTdata, transactionOffset, (short) (lc & 0xff));
transactionOffset += (short) (lc & 0xff);
}

if (ins == AppletInstructions.INS_FINISH) {

try {
psbt.reset();
psbt.fill();
Expand Down Expand Up @@ -253,11 +252,35 @@ short validatePolicy(){
System.out.print("policy[stepcounter]: " + policy[stepCounter] + System.lineSeparator());
System.out.print("orSection: " + orSection + System.lineSeparator());
switch (policy[stepCounter]) {
case PolicyInstruction.minTotalOutput:
case PolicyInstruction.maxTotalOutput:
case PolicyInstruction.minOutputToTargetAddress:
case PolicyInstruction.maxOutputToTargetAddress:
case PolicyInstruction.minAmountofInputs:
case Policy.minTotalOutput:
if (Policy.validateTotalOutput(policy[stepCounter + 1], (short) 1)){
orSection = 1;
}
stepCounter += 2;
break;

case Policy.maxTotalOutput:
if (Policy.validateTotalOutput(policy[stepCounter + 1], (short) -1)){
orSection = 1;
}
stepCounter += 2;
break;

case Policy.minOutputWithSigScr:
if (Policy.validateValueOwSS((policy[stepCounter + 1]), (policy[stepCounter + 2]), (short) 1)) { //validate Value Output with Sign Script
orSection = 1;
}
stepCounter += 3;
break;

case Policy.maxOutputWithSigScr:
if (Policy.validateValueOwSS((policy[stepCounter + 1]), (policy[stepCounter + 2]), (short) -1)) {
orSection = 1;
}
stepCounter += 3;
break;

case Policy.minNumberofInputs:
if (GlobalMap.PSBTversion == 0) {
if (policy[stepCounter + 1] <= psbt.global_map.globalUnsignedTX.input_count) {
orSection = 1;
Expand All @@ -272,7 +295,7 @@ short validatePolicy(){
stepCounter += 2;
break;

case PolicyInstruction.maxAmountofInputs:
case Policy.maxNumberofInputs:
if (GlobalMap.PSBTversion == 0) {
if (policy[stepCounter + 1] >= psbt.global_map.globalUnsignedTX.input_count) {
orSection = 1;
Expand All @@ -287,7 +310,7 @@ short validatePolicy(){
stepCounter += 2;
break;

case PolicyInstruction.minAmountofOutputs:
case Policy.minNumberofOutputs:
if (GlobalMap.PSBTversion == 0) {
if (policy[stepCounter + 1] <= psbt.global_map.globalUnsignedTX.output_count) {
orSection = 1;
Expand All @@ -302,7 +325,7 @@ short validatePolicy(){
stepCounter += 2;
break;

case PolicyInstruction.maxAmountofOutputs:
case Policy.maxNumberofOutputs:
if (GlobalMap.PSBTversion == 0) {
if (policy[stepCounter + 1] >= psbt.global_map.globalUnsignedTX.output_count) {
orSection = 1;
Expand All @@ -317,23 +340,24 @@ short validatePolicy(){
stepCounter += 2;
break;

case PolicyInstruction.naiveTimeLapsed: // TODO delete
case PolicyInstruction.signedTmeLapse:
case PolicyInstruction.checkSecret:
case Policy.naiveTimeLapsed: // TODO delete
case Policy.signedTmeLapse:
case Policy.checkSecret:
if ((Util.arrayCompare(additionalDataStorage[policy[stepCounter + 1]],(short) 0,checkAgainstDataStorage[policy[stepCounter + 1]],(short) 0, STORAGE_SIZE)) == 0) {
//does this compare little or big endian
orSection = 1;
}
stepCounter += 2;
break;

case PolicyInstruction.transactionVersion:
case Policy.transactionVersion:
if (policy[stepCounter + 1] == GlobalMap.PSBTversion) {
orSection = 1;
}
stepCounter += 2;
break;

case PolicyInstruction.policyAnd:
case Policy.policyAnd:
if (orSection == 0) {
return validationReturnProcedure((short) 0);
}
Expand All @@ -355,7 +379,7 @@ boolean checkStorageUsage() {
while (stepCounter < policyOffset) {
System.out.print("policy[stepcounter]: " + policy[stepCounter] + System.lineSeparator());
switch (policy[stepCounter]) {
case PolicyInstruction.minTotalOutput:
case Policy.minTotalOutput:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0){
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -364,7 +388,7 @@ boolean checkStorageUsage() {
stepCounter += 2;
break;

case PolicyInstruction.maxTotalOutput:
case Policy.maxTotalOutput:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0){
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -373,7 +397,7 @@ boolean checkStorageUsage() {
stepCounter += 2;
break;

case PolicyInstruction.minOutputToTargetAddress:
case Policy.minOutputWithSigScr:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0 || dataStorageOffsets[policy[stepCounter + 2]] == 0){
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -382,7 +406,7 @@ boolean checkStorageUsage() {
stepCounter += 3;
break;

case PolicyInstruction.maxOutputToTargetAddress:
case Policy.maxOutputWithSigScr:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0 || dataStorageOffsets[policy[stepCounter + 2]] == 0){
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -391,20 +415,20 @@ boolean checkStorageUsage() {
stepCounter += 3;
break;

case PolicyInstruction.minAmountofInputs:
case Policy.minNumberofInputs:
stepCounter += 2;
break;
case PolicyInstruction.maxAmountofInputs:
case Policy.maxNumberofInputs:
stepCounter += 2;
break;
case PolicyInstruction.minAmountofOutputs:
case Policy.minNumberofOutputs:
stepCounter += 2;
break;
case PolicyInstruction.maxAmountofOutputs:
case Policy.maxNumberofOutputs:
stepCounter += 2;
break;

case PolicyInstruction.naiveTimeLapsed:
case Policy.naiveTimeLapsed:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0){
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -413,7 +437,7 @@ boolean checkStorageUsage() {
stepCounter += 2;
break;

case PolicyInstruction.signedTmeLapse:
case Policy.signedTmeLapse:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0 || dataStorageOffsets[policy[stepCounter + 2]] == 0) {
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -422,7 +446,7 @@ boolean checkStorageUsage() {
stepCounter += 3;
break;

case PolicyInstruction.checkSecret:
case Policy.checkSecret:
if (dataStorageOffsets[policy[stepCounter + 1]] == 0) {
System.out.println("seems like unused storage in memory with pointer: " + policy[stepCounter + 1]);
System.out.println("with PolicyInstruction: " + policy[stepCounter]);
Expand All @@ -431,11 +455,11 @@ boolean checkStorageUsage() {
stepCounter += 2;
break;

case PolicyInstruction.transactionVersion:
case Policy.transactionVersion:
stepCounter += 2;
break;

case PolicyInstruction.policyAnd:
case Policy.policyAnd:
stepCounter++;
break;
default:
Expand Down
20 changes: 19 additions & 1 deletion applet/src/main/java/applet/PSBT.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package applet;

import java.util.Arrays;

import static applet.MainApplet.PSBTdata;
import static applet.MainApplet.totalOutput;

public class PSBT {
public static final short MAX_OF_IO_MAPS = 8;
Expand Down Expand Up @@ -54,11 +57,26 @@ public void fill() throws Exception {
byte_size++;
current_output_map++;
}

Tools.getTotalOutput();
}

public void reset() {
short i = 0;
global_map.reset();
while (i < MAX_OF_IO_MAPS) {
input_maps[i].reset();
output_maps[i].reset();
i++;
}

i = 0;
while (i < 8) {
MainApplet.totalOutput[i] = 0;
i++;
}

current_input_map = 0;
current_output_map = 0;
byte_size = 0;
}
}
Loading

0 comments on commit 52036a0

Please sign in to comment.