Skip to content

Commit

Permalink
fixed transactions not updating flash channel state
Browse files Browse the repository at this point in the history
  • Loading branch information
gosticks committed Jan 15, 2018
1 parent a874805 commit 9191ac1
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 101 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you have any ideas please submit a request (I am totally not a Java guy so...
- [x] updateLeafToRoot
- [x] getDigest

#### Model.Transfer
#### com.flashwifi.flashwrapper.Model.Transfer
- [x] prepare
- [x] compose
- [x] close (needs testing)
Expand All @@ -28,7 +28,7 @@ If you have any ideas please submit a request (I am totally not a Java guy so...
1. Clone repo
2. Update maven ressources
3. That's it.
4. You can run a test transaction by running the main func in the Main Class.
4. You can run a test transaction by running the main func in the com.flashwifi.flashwrapper.Main Class.



Expand Down
2 changes: 1 addition & 1 deletion iotaflashlibjswrapper.iml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.eclipsesource.j2v8:j2v8_macosx_x86_64:4.6.0" level="project" />
<orderEntry type="library" name="Maven: com.github.iotaledger:iota~lib~java:v0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.github.iotaledger:iota~lib~java:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.2" level="project" />
</component>
</module>
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
<artifactId>j2v8_macosx_x86_64</artifactId>
<version>4.6.0</version>
</dependency>

<dependency>
<groupId>com.github.iotaledger</groupId>
<artifactId>iota~lib~java</artifactId>
<version>v0.9.10</version>
<version>0.9.10</version>
</dependency>

<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -37,13 +39,10 @@

<!-- https://maven.apache.org/settings.html#Properties -->
<properties>

<!-- <encoding>UTF-8</encoding> -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

</properties>
</project>
13 changes: 13 additions & 0 deletions res/iota.flash.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var Helper = {
applyTransfers: function(flash, bundles) {
iotaFlash.transfer.applyTransfers(
flash.root,
flash.deposits,
flash.outputs,
flash.remainderAddress,
flash.transfers,
bundles
)
return flash
}
}
1 change: 1 addition & 0 deletions res/iota.flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -10297,6 +10297,7 @@ function close(settlement, deposits) {
* @param {array} transfers
*/
function applyTransfers(root, deposit, outputs, remainder, history, transfers) {

if (transfers.filter(transfer =>
transfer.filter(tx => tx.value < 0)
.filter(tx => !IOTACrypto.utils.validateSignatures(transfer, tx.address))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Model.*;
package com.flashwifi.flashwrapper;

import com.flashwifi.flashwrapper.Model.*;


import java.util.ArrayList;
import java.util.List;
Expand All @@ -7,7 +10,6 @@
public class Helpers {
public static ArrayList<Bundle> createTransaction(UserObject user, ArrayList<Transfer> transfers, boolean shouldClose) {
CreateTransactionHelperObject toUse = IotaFlashBridge.updateLeafToRoot(user.getFlash().getRoot());

if (toUse.getGenerate() != 0) {
// TODO: tell the server to gen new address.
System.out.println("No more addresses in channel.");
Expand Down Expand Up @@ -72,14 +74,16 @@ public static ArrayList<Bundle> appliedSignatures(ArrayList<Bundle> bundles, Arr
}

public static void applyTransfers(UserObject user, ArrayList<Bundle> bundles) {
IotaFlashBridge.applayTransfers(
user.getFlash().getRoot(),
user.getFlash().getDeposits(),
user.getFlash().getOutputs(),
user.getFlash().getRemainderAddress(),
user.getFlash().getTransfers(),
bundles
);
FlashObject flash = IotaFlashBridge.applyTransfersToUser(user, bundles);
user.setFlash(flash);
// com.flashwifi.flashwrapper.IotaFlashBridge.applyTransfers(
// user.getFlash().getRoot(),
// user.getFlash().getDeposits(),
// user.getFlash().getOutputs(),
// user.getFlash().getRemainderAddress(),
// user.getFlash().getTransfers(),
// bundles
// );
}

public static ArrayList<Bundle> clone(ArrayList<Bundle> bundles) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Model.*;
package com.flashwifi.flashwrapper;

import com.flashwifi.flashwrapper.Model.*;
import com.eclipsesource.v8.*;
import com.eclipsesource.v8.utils.V8ObjectUtils;

Expand All @@ -13,20 +15,24 @@

public class IotaFlashBridge {
private static String iotaLibPath = "res/iota.flash.js";
private static String iotaHelperLibPath = "res/iota.flash.helper.js";
private static V8 engine;
private static V8Object transfer;
private static V8Object multisig;
private static V8Object helper;

public static void boot() throws IOException {
String file = readFile(iotaLibPath, Charset.defaultCharset());

engine = V8.createV8Runtime();
// Eval lib into current v8 context.
engine.executeVoidScript(file);
engine.executeVoidScript(readFile(iotaHelperLibPath, Charset.defaultCharset()));
multisig = (V8Object) engine.executeScript("iotaFlash.multisig");
transfer = (V8Object) engine.executeScript("iotaFlash.transfer");
helper = (V8Object) engine.executeScript("Helper");

Model.Console console = new Model.Console();
com.flashwifi.flashwrapper.Model.Console console = new com.flashwifi.flashwrapper.Model.Console();
V8Object v8Console = new V8Object(engine);
engine.add("console", v8Console);
v8Console.registerJavaMethod(console, "log", "log", new Class<?>[] { String.class });
Expand Down Expand Up @@ -61,6 +67,8 @@ public static MultisigAddress composeAddress(ArrayList<Digest> digests) {
int secSum = (Integer) resultMap.get("securitySum");
MultisigAddress ret = new MultisigAddress(addr, secSum);

params.release();
retV8.release();
return ret;
}

Expand Down Expand Up @@ -250,7 +258,7 @@ public static Object getDiff(ArrayList<Object> root, ArrayList<Object> remainder
* @param signedBundles
* @return
*/
public static void applayTransfers(MultisigAddress root,
public static void applyTransfers(MultisigAddress root,
ArrayList<Integer> deposits,
ArrayList<Bundle> outputs,
MultisigAddress remainderAddress,
Expand All @@ -268,6 +276,20 @@ public static void applayTransfers(MultisigAddress root,
transfer.executeFunction("applyTransfers", V8ObjectUtils.toV8Array(engine, params));
}

public static FlashObject applyTransfersToUser(UserObject user, ArrayList<Bundle> signedBundles) {
List<Object> params = new ArrayList<>();
params.add(V8Converter.flashObjectToV8Object(engine, user.getFlash()));
params.add(V8Converter.bundleListToV8Array(engine, signedBundles));
V8Array paramV8 = V8ObjectUtils.toV8Array(engine, params);
V8Object ret = helper.executeObjectFunction("applyTransfers", paramV8);
Map<String, Object> obj = V8ObjectUtils.toMap(ret);

paramV8.release();
FlashObject flash = V8Converter.flashObjectFromV8Object(ret);
ret.release();
return flash;
}

/// Utils

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Model.Bundle;
import Model.Digest;
import Model.MultisigAddress;
import Model.Transfer;
package com.flashwifi.flashwrapper;

import com.flashwifi.flashwrapper.Model.Bundle;
import com.flashwifi.flashwrapper.Model.Digest;
import com.flashwifi.flashwrapper.Model.MultisigAddress;
import com.flashwifi.flashwrapper.Model.Transfer;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,7 +14,7 @@ public interface IotaFlashInterface {

public void updateLeafToRoot(MultisigAddress root);

// Model.Transfer
// com.flashwifi.flashwrapper.Model.Transfer
public Object prepare(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits, int index, ArrayList<Transfer> transfers);
public List<Object> compose(int balance, ArrayList<Integer> deposits, ArrayList<Transfer> outputs, MultisigAddress root, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flashwifi.flashwrapper;

import Model.*;
import com.flashwifi.flashwrapper.Model.*;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -96,38 +97,17 @@ public static void main(String[] argv) throws Exception {

System.out.println("Channel Setup!");


ArrayList<Transfer> transfers = new ArrayList<>();
transfers.add(new Transfer(twoSettlement, 200));
transfers.add(new Transfer(twoSettlement, 1));
transfers.add(new Transfer(twoSettlement, 400));

System.out.println(oneFlash);

System.out.println("Creating a transaction: 200 to " + twoSettlement);
ArrayList<Bundle> bundles = Helpers.createTransaction(oneFlash, transfers, false);

System.out.println("[SUCCESS] createTransaction completed");

// Sign the bundles.
// Get signatures for the bundles
ArrayList<Signature> oneSignatures = Helpers.signTransaction(oneFlash, bundles);

// Generate USER TWO'S Singatures
ArrayList<Signature> twoSignatures = Helpers.signTransaction(twoFlash, bundles);

System.out.println("[SUCCESS] Created signatures for users.");

// Sign bundle with your USER ONE'S signatures
ArrayList<Bundle> signedBundles = IotaFlashBridge.appliedSignatures(bundles, oneSignatures);

System.out.println("[SUCCESS] Parial applied Signature for User one on transfer bundle");


// ADD USER TWOS'S signatures to the partially signed bundles
signedBundles = IotaFlashBridge.appliedSignatures(signedBundles, twoSignatures);

System.out.println("[SUCCESS] Signed bundle bu second user. Bundle ready.");


ArrayList<Bundle> partialSignedBundles = signTransfer(bundles, oneFlash);
ArrayList<Bundle> signedBundles = signTransfer(partialSignedBundles, twoFlash);
/////////////////////////////////
/// APPLY SIGNED BUNDLES

Expand All @@ -154,7 +134,7 @@ public static void main(String[] argv) throws Exception {
/*
// Supplying the CORRECT varibles to create a closing bundle
bundles = Helpers.createTransaction(
bundles = com.flashwifi.flashwrapper.Helpers.createTransaction(
oneFlash,
oneFlash.getFlash().getSettlementAddresses(),
true
Expand All @@ -164,10 +144,10 @@ public static void main(String[] argv) throws Exception {
/// SIGN BUNDLES
// Get signatures for the bundles
oneSignatures = Helpers.signTransaction(oneFlash, bundles)
oneSignatures = com.flashwifi.flashwrapper.Helpers.signTransaction(oneFlash, bundles)
// Generate USER TWO'S Singatures
twoSignatures = Helpers.signTransaction(twoFlash, bundles)
twoSignatures = com.flashwifi.flashwrapper.Helpers.signTransaction(twoFlash, bundles)
// Sign bundle with your USER ONE'S signatures
signedBundles = transfer.appliedSignatures(bundles, oneSignatures)
Expand All @@ -179,19 +159,30 @@ public static void main(String[] argv) throws Exception {
/// APPLY SIGNED BUNDLES
// Apply transfers to User ONE
oneFlash = Helpers.applyTransfers(oneFlash, signedBundles)
oneFlash = com.flashwifi.flashwrapper.Helpers.applyTransfers(oneFlash, signedBundles)
// Save latest channel bundles
oneFlash.bundles = signedBundles
// Apply transfers to User TWO
twoFlash = Helpers.applyTransfers(twoFlash, signedBundles)
twoFlash = com.flashwifi.flashwrapper.Helpers.applyTransfers(twoFlash, signedBundles)
// Save latest channel bundles
twoFlash.bundles = signedBundles
console.log("Channel Closed")
console.log("Final Bundle to be attached: ")*/
}

private static ArrayList<Bundle> signTransfer(ArrayList<Bundle> bundles, UserObject user) {
System.out.println("[SUCCESS] Created signatures for users.");
ArrayList<Signature> oneSignatures = Helpers.signTransaction(user, bundles);

System.out.println("[SUCCESS] Parial applied Signature for User one on transfer bundle");
// Sign bundle with your USER ONE'S signatures
ArrayList<Bundle> signedBundles = IotaFlashBridge.appliedSignatures(bundles, oneSignatures);

return signedBundles;
}

private static void setupUser(UserObject user, int TREE_DEPTH) {
// Create digests for the start of the channel
for (int i = 0; i < TREE_DEPTH + 1; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package Model;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Array;
package com.flashwifi.flashwrapper.Model;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package com.flashwifi.flashwrapper.Model;


public class Console {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package com.flashwifi.flashwrapper.Model;

public class CreateTransactionHelperObject {
private int generate = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Model;
package com.flashwifi.flashwrapper.Model;

import java.util.HashMap;
import java.util.Map;
Expand Down
Loading

0 comments on commit 9191ac1

Please sign in to comment.