mirror of
https://github.com/gosticks/iota.flash.js-java-wrapper.git
synced 2026-08-12 04:50:20 +00:00
started to implement a test flash channel transaction
This commit is contained in:
@@ -12,5 +12,6 @@
|
||||
<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" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -8,12 +8,24 @@
|
||||
<artifactId>iota.flash.lib.js.wrapper</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.eclipsesource.j2v8</groupId>
|
||||
<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>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- https://maven.apache.org/settings.html#Properties -->
|
||||
|
||||
@@ -4,21 +4,25 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class Bundle {
|
||||
private ArrayList<Bundle> bundles;
|
||||
private ArrayList<Transaction> bundles;
|
||||
|
||||
public Bundle(ArrayList<Bundle> bundles) {
|
||||
public Bundle(ArrayList<Transaction> bundles) {
|
||||
this.bundles = bundles;
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
List<Object> bundleList = new ArrayList<Object>();
|
||||
for (Bundle b: bundles) {
|
||||
for (Transaction b: bundles) {
|
||||
bundleList.add(b.toMap());
|
||||
}
|
||||
map.put("bundles", bundleList);
|
||||
return map;
|
||||
}
|
||||
|
||||
public ArrayList<Transaction> getBundles() {
|
||||
return bundles;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,4 +35,5 @@ class Digest {
|
||||
map.put("security", getSecurity());
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Helpers {
|
||||
public static ArrayList<Bundle> createTransaction(UserObject user, ArrayList<Transfer> transfers, boolean shouldClose) {
|
||||
CreateTransactionHelperObject toUse = IotaFlashBridge.updateLeafToRoot(user.getFlash().root);
|
||||
|
||||
if (toUse.getGenerate() != 0) {
|
||||
// TODO: tell the server to gen new address.
|
||||
System.out.println("No more addresses in channel.");
|
||||
}
|
||||
|
||||
ArrayList<Transfer> newTransfers;
|
||||
|
||||
if (shouldClose) {
|
||||
// newTransfers = IotaFlashBridge.close(user.getFlash().getSettlementAddresses(), user.getFlash().deposits);
|
||||
} else {
|
||||
List<Object> test = IotaFlashBridge.prepare(
|
||||
user.getFlash().settlementAddresses,
|
||||
user.getFlash().deposits,
|
||||
user.getUserIndex(),
|
||||
transfers
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -12,28 +12,20 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class IotaFlashBridge implements IotaFlashInterface {
|
||||
private String iotaLibPath;
|
||||
private V8 engine;
|
||||
private V8Object transfer;
|
||||
private V8Object multisig;
|
||||
public class IotaFlashBridge {
|
||||
private static String iotaLibPath = "res/iota.flash.js";
|
||||
private static V8 engine;
|
||||
private static V8Object transfer;
|
||||
private static V8Object multisig;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param path
|
||||
* @throws IOException
|
||||
*/
|
||||
IotaFlashBridge(String path) throws IOException {
|
||||
this.iotaLibPath = path;
|
||||
public static void boot() throws IOException {
|
||||
String file = readFile(iotaLibPath, Charset.defaultCharset());
|
||||
|
||||
String file = readFile(path, Charset.defaultCharset());
|
||||
|
||||
this.engine = V8.createV8Runtime();
|
||||
engine = V8.createV8Runtime();
|
||||
// Eval lib into current v8 context.
|
||||
engine.executeVoidScript(file);
|
||||
multisig = (V8Object) engine.executeScript("iotaFlash.multisig");
|
||||
transfer = (V8Object) engine.executeScript("iotaFlash.transfer");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,9 +33,8 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param digests
|
||||
* @return
|
||||
*/
|
||||
public MultisigAddress composeAddress(ArrayList<Digest> digests) {
|
||||
public static MultisigAddress composeAddress(ArrayList<Digest> digests) {
|
||||
// Create js object for digest
|
||||
// TODO: find more clean way to do thi s.
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
for (Digest digest: digests) {
|
||||
list.add(digest.toMap());
|
||||
@@ -73,7 +64,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param security
|
||||
* @return
|
||||
*/
|
||||
public Digest getDigest(String seed, int index, int security) {
|
||||
public static Digest getDigest(String seed, int index, int security) {
|
||||
if (seed.length() < 81) {
|
||||
System.out.println("Seed is too short");
|
||||
return null;
|
||||
@@ -94,14 +85,21 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
public void updateLeafToRoot(MultisigAddress root) {
|
||||
public static CreateTransactionHelperObject updateLeafToRoot(MultisigAddress root) {
|
||||
Map<String, Object> map = root.toMap();
|
||||
// Create param list
|
||||
List<Object> paramsObj = new ArrayList<Object>();
|
||||
paramsObj.add(map);
|
||||
V8Array params = V8ObjectUtils.toV8Array(engine, paramsObj);
|
||||
|
||||
multisig.executeFunction("updateLeafToRoot", params);
|
||||
V8Object ret = multisig.executeObjectFunction("updateLeafToRoot", params);
|
||||
int generate = ret.getInteger("generate");
|
||||
Map<String, ? super Object> multiSigMap = V8ObjectUtils.toMap((V8Object) ret.getObject("multisig"));
|
||||
// Parse result into Java Obj.
|
||||
String addr = (String) multiSigMap.get("address");
|
||||
int secSum = (Integer) multiSigMap.get("securitySum");
|
||||
MultisigAddress multisig = new MultisigAddress(addr, secSum);
|
||||
return new CreateTransactionHelperObject(generate, multisig);
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +111,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param transfers array of all transfers (value, address) pairs
|
||||
* @return
|
||||
*/
|
||||
public Object prepare(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits, int index, ArrayList<Transfer> transfers) {
|
||||
public static List<Object> prepare(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits, int index, ArrayList<Transfer> transfers) {
|
||||
V8Array settlementAddressesJS = V8ObjectUtils.toV8Array(engine, settlementAddresses);
|
||||
V8Array depositJS = V8ObjectUtils.toV8Array(engine, deposits);
|
||||
List<Object> transferObj = new ArrayList<Object>();
|
||||
@@ -133,6 +131,11 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
V8Array ret = transfer.executeArrayFunction("prepare", V8ObjectUtils.toV8Array(engine, params));
|
||||
List<Object> transfersReturnJS = V8ObjectUtils.toList(ret);
|
||||
|
||||
for (Object b: transfersReturnJS) {
|
||||
Map<String, Object> test = V8ObjectUtils.toMap((V8Object) b);
|
||||
|
||||
}
|
||||
|
||||
// Call js.
|
||||
return transfersReturnJS;
|
||||
}
|
||||
@@ -142,14 +145,14 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param balance
|
||||
* @param deposits
|
||||
* @param outputs
|
||||
* @param multisig
|
||||
* @param root
|
||||
* @param remainderAddress
|
||||
* @param history
|
||||
* @param transfers
|
||||
* @param close
|
||||
* @return
|
||||
*/
|
||||
public List<Object> compose(int balance, ArrayList<Integer> deposits, ArrayList<Transfer> outputs, Object multisig, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close) {
|
||||
public static List<Object> compose(int balance, ArrayList<Integer> deposits, ArrayList<Transfer> outputs, MultisigAddress root, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close) {
|
||||
V8Array depositsJS = V8ObjectUtils.toV8Array(engine, deposits);
|
||||
// Outputs
|
||||
List<Object> outputsObj = new ArrayList<Object>();
|
||||
@@ -158,26 +161,60 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
}
|
||||
V8Array outputsJS = V8ObjectUtils.toV8Array(engine, outputsObj);
|
||||
|
||||
|
||||
|
||||
List<Object> transfersObj = new ArrayList<Object>();
|
||||
for (Transfer t: transfers) {
|
||||
transfersObj.add(t.toMap());
|
||||
}
|
||||
V8Array transfersJS = V8ObjectUtils.toV8Array(engine, transfersObj);
|
||||
|
||||
List<Object> trs = V8ObjectUtils.toList(transfersJS);
|
||||
// Create params.
|
||||
// Now put all params into JS ready array.
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
params.add(balance);
|
||||
params.add(depositsJS);
|
||||
params.add(outputsJS);
|
||||
|
||||
return trs;
|
||||
|
||||
// Call js function.
|
||||
V8Array ret = transfer.executeArrayFunction("compose", V8ObjectUtils.toV8Array(engine, params));
|
||||
List<Object> transfersReturnJS = V8ObjectUtils.toList(ret);
|
||||
|
||||
// Parse return as array of bundles
|
||||
|
||||
|
||||
return transfersReturnJS;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param multisig
|
||||
* @param root
|
||||
* @param seed
|
||||
* @param bundles
|
||||
* @return
|
||||
*/
|
||||
public Object sign(Object multisig, String seed, ArrayList<Object> bundles) {
|
||||
return null;
|
||||
public static Object sign(MultisigAddress root, String seed, ArrayList<Bundle> bundles) {
|
||||
Map<String, Object> multisig = root.toMap();
|
||||
V8Object rootJS = V8ObjectUtils.toV8Object(engine, multisig);
|
||||
|
||||
List<Object> bundleTmp = new ArrayList<Object>();
|
||||
for (Bundle b: bundles) {
|
||||
bundleTmp.add(b.toMap());
|
||||
}
|
||||
V8Array bundlesJS = V8ObjectUtils.toV8Array(engine, bundleTmp);
|
||||
|
||||
// Create params.
|
||||
// Now put all params into JS ready array.
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
params.add(rootJS);
|
||||
params.add(seed);
|
||||
params.add(bundles);
|
||||
|
||||
V8Object signatures = transfer.executeObjectFunction("sign", V8ObjectUtils.toV8Array(engine, params));
|
||||
|
||||
// TODO: add singature object.
|
||||
return signatures;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,7 +223,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param signatures
|
||||
* @return
|
||||
*/
|
||||
public Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> signatures) {
|
||||
public static Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> signatures) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -198,7 +235,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param bundles
|
||||
* @return
|
||||
*/
|
||||
public Object getDiff(ArrayList<Object> root, ArrayList<Object> remainder, ArrayList<Object> history, ArrayList<Object> bundles) {
|
||||
public static Object getDiff(ArrayList<Object> root, ArrayList<Object> remainder, ArrayList<Object> history, ArrayList<Object> bundles) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -212,7 +249,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param signedBundles
|
||||
* @return
|
||||
*/
|
||||
public Object applayTransfers(Object root, Object deposit, Object outputs, Object remainderAddress, Object transfers, Object signedBundles) {
|
||||
public static Object applayTransfers(Object root, Object deposit, Object outputs, Object remainderAddress, Object transfers, Object signedBundles) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -222,7 +259,7 @@ public class IotaFlashBridge implements IotaFlashInterface {
|
||||
* @param deposits
|
||||
* @return
|
||||
*/
|
||||
public Object close(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits) {
|
||||
public static Object close(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits) {
|
||||
V8Array saJS = V8ObjectUtils.toV8Array(engine, settlementAddresses);
|
||||
// Deposits
|
||||
V8Array depositsJS = V8ObjectUtils.toV8Array(engine, deposits);
|
||||
|
||||
@@ -9,11 +9,11 @@ public interface IotaFlashInterface {
|
||||
|
||||
// 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, Object multisig, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close);
|
||||
public List<Object> compose(int balance, ArrayList<Integer> deposits, ArrayList<Transfer> outputs, MultisigAddress root, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close);
|
||||
|
||||
public Digest getDigest(String seed, int index, int security);
|
||||
|
||||
public Object sign(Object multisig, String seed, ArrayList<Object> bundles);
|
||||
public Object sign(MultisigAddress root, String seed, ArrayList<Bundle> bundles);
|
||||
public Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> signatures);
|
||||
public Object getDiff(ArrayList<Object> root,
|
||||
ArrayList<Object> remainder,
|
||||
|
||||
+167
-8
@@ -1,23 +1,182 @@
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Mult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] argv) throws Exception {
|
||||
// Create v8 engine and load lib.
|
||||
IotaFlashBridge.boot();
|
||||
|
||||
// Run a test based on the flash example
|
||||
// Link: https://github.com/iotaledger/iota.flash.js/blob/master/examples/flash.js
|
||||
|
||||
String oneSeed = "USERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSER";
|
||||
String oneSettlement = "USERONE9ADDRESS9USERONE9ADDRESS9USERONE9ADDRESS9USERONE9ADDRESS9USERONE9ADDRESS9U";
|
||||
String twoSeed = "USERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSERTWOUSER";
|
||||
String twoSettlement = "USERTWO9ADDRESS9USERTWO9ADDRESS9USERTWO9ADDRESS9USERTWO9ADDRESS9USERTWO9ADDRESS9U";
|
||||
|
||||
//////////////////////////////////
|
||||
// INITIAL CHANNEL CONDITIONS
|
||||
|
||||
// Security level
|
||||
int SECURITY = 2;
|
||||
// Number of parties taking signing part in the channel
|
||||
int SIGNERS_COUNT = 2;
|
||||
// Flash tree depth
|
||||
int TREE_DEPTH = 4;
|
||||
// Total channel Balance
|
||||
int CHANNEL_BALANCE = 2000;
|
||||
// Users deposits
|
||||
ArrayList<Integer> DEPOSITS = new ArrayList<>();
|
||||
DEPOSITS.add(1000);
|
||||
DEPOSITS.add(1000);
|
||||
// Setup users.
|
||||
FlashObject oneFlashObj = new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS);
|
||||
UserObject oneFlash = new UserObject(0, oneSeed, TREE_DEPTH, oneFlashObj);
|
||||
|
||||
FlashObject twoFlashObj = new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS);
|
||||
UserObject twoFlash = new UserObject(1, twoSeed, TREE_DEPTH, twoFlashObj);
|
||||
|
||||
// USER ONE
|
||||
setupUser(oneFlash, TREE_DEPTH);
|
||||
|
||||
// USER TWO
|
||||
setupUser(twoFlash, TREE_DEPTH);
|
||||
|
||||
//////////////////////////////////
|
||||
// INITAL MULTISIG
|
||||
|
||||
// Make an array of digests
|
||||
ArrayList<UserObject> allUsers = new ArrayList<UserObject>();
|
||||
allUsers.add(oneFlash);
|
||||
allUsers.add(twoFlash);
|
||||
|
||||
// Create partial digests for users.
|
||||
createInitialPartialDigests(allUsers, oneFlash);
|
||||
createInitialPartialDigests(allUsers, twoFlash);
|
||||
|
||||
ArrayList<MultisigAddress> oneMultisigs = oneFlash.getMultisigDigests();
|
||||
ArrayList<MultisigAddress> twoMultisigs = twoFlash.getMultisigDigests();
|
||||
|
||||
// Set renainder address.
|
||||
MultisigAddress oneRemainderAddr = oneMultisigs.remove(0); //shiftCopyArray();
|
||||
oneFlash.getFlash().setRemainderAddress(oneRemainderAddr);
|
||||
|
||||
MultisigAddress twoRemainderAddr = twoMultisigs.remove(0);
|
||||
twoFlash.getFlash().setRemainderAddress(twoRemainderAddr);
|
||||
|
||||
// Build flash trees
|
||||
for (int i = 1; i < oneMultisigs.size(); i++) {
|
||||
System.out.println("Adding child (" + oneMultisigs.get(0).toString() + ") to root :" + oneMultisigs.get(i - 1).toString() );
|
||||
oneMultisigs.get(i - 1).push(oneMultisigs.get(i));
|
||||
}
|
||||
|
||||
// Build flash trees
|
||||
for (int i = 1; i < twoMultisigs.size(); i++) {
|
||||
twoMultisigs.get(i - 1).push(twoMultisigs.get(i));
|
||||
}
|
||||
|
||||
oneFlash.getFlash().setRoot(oneMultisigs.remove(0));
|
||||
twoFlash.getFlash().setRoot(twoMultisigs.remove(0));
|
||||
|
||||
ArrayList<String> settlementAddresses = new ArrayList<>();
|
||||
settlementAddresses.add(oneSettlement);
|
||||
settlementAddresses.add(twoSettlement);
|
||||
oneFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||
twoFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||
|
||||
System.out.println("Channel Setup!");
|
||||
|
||||
|
||||
ArrayList<Transfer> transfers = new ArrayList<>();
|
||||
transfers.add(new Transfer(twoSettlement, 200));
|
||||
|
||||
Helpers.createTransaction(oneFlash, transfers, false);
|
||||
}
|
||||
|
||||
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++) {
|
||||
// Create new digest
|
||||
Digest digest = IotaFlashBridge.getDigest(
|
||||
user.getSeed(),
|
||||
user.getIndex(),
|
||||
user.getSecurity()
|
||||
);
|
||||
System.out.println("Adding digest (" + digest.toString() + ") to user " + user.getUserIndex());
|
||||
// Increment key index
|
||||
user.incrementIndex();
|
||||
user.add(digest);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createInitialPartialDigests(ArrayList<UserObject> allUsers, UserObject currentUser) {
|
||||
|
||||
// Generate the first addresses
|
||||
ArrayList<MultisigAddress> oneMultisigs = new ArrayList<MultisigAddress>();
|
||||
|
||||
|
||||
System.out.println("_________________________________________________________________");
|
||||
System.out.println("Creating multisigs on user: " + currentUser.getUserIndex());
|
||||
int index = 0;
|
||||
// Create address
|
||||
for (Digest digest: allUsers.get(index).getPartialDigests()) {
|
||||
int i = index;
|
||||
|
||||
MultisigAddress addy = IotaFlashBridge.composeAddress(
|
||||
allUsers.stream().map(u -> u.getPartialDigests().get(i)).collect(Collectors.toCollection(ArrayList::new))
|
||||
);
|
||||
|
||||
System.out.println("Multisig: " + addy.toString());
|
||||
|
||||
// Add key index in
|
||||
addy.setIndex(digest.getIndex());
|
||||
// Add the signing index to the object IMPORTANT
|
||||
addy.setSigningIndex(currentUser.getUserIndex() * digest.getSecurity());
|
||||
// Get the sum of all digest security to get address security sum
|
||||
addy.setSecuritySum(allUsers.stream()
|
||||
.map(u -> u.getPartialDigests().get(i))
|
||||
.mapToInt(Digest::getSecurity)
|
||||
.sum()
|
||||
);
|
||||
addy.setSecurity(digest.getSecurity());
|
||||
oneMultisigs.add(addy);
|
||||
index++;
|
||||
}
|
||||
currentUser.setMultisigDigests(oneMultisigs);
|
||||
}
|
||||
|
||||
private static ArrayList<MultisigAddress> shiftCopyArray(ArrayList<MultisigAddress> input) {
|
||||
ArrayList<MultisigAddress> output = new ArrayList<>();
|
||||
|
||||
for (int i = 1; i < input.size(); i++) {
|
||||
output.add(input.get(i));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private static void test() throws IOException {
|
||||
|
||||
System.out.println("IOTA Flash channel tester");
|
||||
|
||||
String pathToLib = "res/iota.flash.js";
|
||||
|
||||
System.out.println("Loading lib into V8 engine");
|
||||
IotaFlashInterface lib = new IotaFlashBridge(pathToLib);
|
||||
System.out.println("Lib imported");
|
||||
|
||||
|
||||
System.out.println("Testing getDigest(seed, index, security):");
|
||||
Digest digest1 = lib.getDigest("USERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSER", 0, 2);
|
||||
Digest digest2 = lib.getDigest("USERTWOUSERTWOUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSER", 0, 2);
|
||||
Digest digest1 = IotaFlashBridge.getDigest("USERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSER", 0, 2);
|
||||
Digest digest2 = IotaFlashBridge.getDigest("USERTWOUSERTWOUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSERONEUSER", 0, 2);
|
||||
System.out.println("Digest1: " + digest1.toString());
|
||||
|
||||
|
||||
@@ -25,13 +184,13 @@ public class Main {
|
||||
ArrayList<Digest> digests = new ArrayList<Digest>();
|
||||
digests.add(digest1);
|
||||
digests.add(digest2);
|
||||
MultisigAddress composedAddr = lib.composeAddress(digests);
|
||||
MultisigAddress composedAddr = IotaFlashBridge.composeAddress(digests);
|
||||
System.out.println("Got multisig addr for digests: " + composedAddr.getAddress() + ", securitySum: " + composedAddr.getSecuritySum());
|
||||
|
||||
testPrepare(lib);
|
||||
testPrepare();
|
||||
}
|
||||
|
||||
private static void testPrepare(IotaFlashInterface lib) {
|
||||
private static void testPrepare() {
|
||||
|
||||
System.out.println("Testing prepare()");
|
||||
ArrayList<String> settlementAddr = new ArrayList<String>();
|
||||
@@ -39,6 +198,6 @@ public class Main {
|
||||
ArrayList<Integer> depositsPrep = new ArrayList<Integer>();
|
||||
ArrayList<Transfer> transfers = new ArrayList<Transfer>();
|
||||
|
||||
lib.prepare(settlementAddr, depositsPrep, 0, transfers);
|
||||
IotaFlashBridge.prepare(settlementAddr, depositsPrep, 0, transfers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,36 @@
|
||||
import com.eclipsesource.v8.V8;
|
||||
import com.eclipsesource.v8.V8Object;
|
||||
import com.eclipsesource.v8.utils.V8ObjectUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class CreateTransactionHelperObject {
|
||||
private int generate = 0;
|
||||
private MultisigAddress address;
|
||||
|
||||
CreateTransactionHelperObject(int gen, MultisigAddress addr) {
|
||||
this.generate = gen;
|
||||
this.address = addr;
|
||||
}
|
||||
|
||||
public int getGenerate() {
|
||||
return generate;
|
||||
}
|
||||
|
||||
public MultisigAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
|
||||
class MultisigAddress {
|
||||
private String address;
|
||||
private int securitySum;
|
||||
private int index;
|
||||
private int signingIndex;
|
||||
private int security;
|
||||
private ArrayList<MultisigAddress> children;
|
||||
private ArrayList<Bundle> bundles;
|
||||
|
||||
@@ -17,6 +42,10 @@ class MultisigAddress {
|
||||
|
||||
}
|
||||
|
||||
public void push(MultisigAddress addr) {
|
||||
children.add(addr);
|
||||
}
|
||||
|
||||
public ArrayList<MultisigAddress> getChildren() {
|
||||
return children;
|
||||
}
|
||||
@@ -28,6 +57,33 @@ class MultisigAddress {
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public int getSigningIndex() {
|
||||
return signingIndex;
|
||||
}
|
||||
|
||||
public int getSecurity() {
|
||||
return security;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void setSecurity(int security) {
|
||||
this.security = security;
|
||||
}
|
||||
|
||||
public void setSecuritySum(int securitySum) {
|
||||
this.securitySum = securitySum;
|
||||
}
|
||||
|
||||
public void setSigningIndex(int signingIndex) {
|
||||
this.signingIndex = signingIndex;
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
@@ -41,11 +97,20 @@ class MultisigAddress {
|
||||
map.put("children", childrenList);
|
||||
|
||||
List<Object> bundleList = new ArrayList<Object>();
|
||||
for (MultisigAddress ma: children) {
|
||||
bundleList.add(ma.toMap());
|
||||
for (Bundle b: bundles) {
|
||||
bundleList.add(b.getBundles());
|
||||
}
|
||||
map.put("bundle", bundleList);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public V8Object toV8Object(V8 engine) {
|
||||
return V8ObjectUtils.toV8Object(engine, this.toMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{'address':'" + address + "', securitySum:" + securitySum + ", signingIndex: " + signingIndex + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UserObject {
|
||||
private int userIndex = 1;
|
||||
private String seed;
|
||||
private int index = 0;
|
||||
private int security = 2;
|
||||
private int depth = 4;
|
||||
private ArrayList<Bundle> bundles = new ArrayList<Bundle>();
|
||||
private ArrayList<Digest> partialDigests = new ArrayList<Digest>();
|
||||
private ArrayList<MultisigAddress> multisigDigests = new ArrayList<MultisigAddress>();
|
||||
private FlashObject flash;
|
||||
|
||||
UserObject(int userID, String seed, int depth, FlashObject flash) {
|
||||
this.userIndex = userID;
|
||||
this.seed = seed;
|
||||
this.depth = depth;
|
||||
this.flash = flash;
|
||||
}
|
||||
|
||||
public void incrementIndex() {
|
||||
index++;
|
||||
}
|
||||
|
||||
public void add(Digest digest) {
|
||||
partialDigests.add(digest);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Getters and Setters
|
||||
*/
|
||||
|
||||
public void setMultisigDigests(ArrayList<MultisigAddress> multisigDigests) {
|
||||
this.multisigDigests = multisigDigests;
|
||||
}
|
||||
|
||||
public ArrayList<MultisigAddress> getMultisigDigests() {
|
||||
return multisigDigests;
|
||||
}
|
||||
|
||||
public int getSecurity() {
|
||||
return security;
|
||||
}
|
||||
|
||||
public String getSeed() {
|
||||
return seed;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public int getUserIndex() {
|
||||
return userIndex;
|
||||
}
|
||||
|
||||
public ArrayList<Bundle> getBundles() {
|
||||
return bundles;
|
||||
}
|
||||
|
||||
public ArrayList<Digest> getPartialDigests() {
|
||||
return partialDigests;
|
||||
}
|
||||
|
||||
public FlashObject getFlash() {
|
||||
return flash;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FlashObject {
|
||||
int signersCount = 2;
|
||||
int balance;
|
||||
ArrayList<String> settlementAddresses;
|
||||
MultisigAddress root;
|
||||
MultisigAddress remainderAddress;
|
||||
ArrayList<Integer> deposits; // Clone correctly
|
||||
ArrayList<Bundle> outputs = new ArrayList<Bundle>();
|
||||
ArrayList<Transfer> transfers = new ArrayList<Transfer>();
|
||||
|
||||
FlashObject(int signersCount, int balance, ArrayList<Integer> deposits) {
|
||||
this.signersCount = signersCount;
|
||||
this.balance = balance;
|
||||
this.deposits = deposits;
|
||||
}
|
||||
|
||||
public void setRemainderAddress(MultisigAddress remainderAddress) {
|
||||
this.remainderAddress = remainderAddress;
|
||||
}
|
||||
|
||||
public MultisigAddress getRemainderAddress() {
|
||||
return remainderAddress;
|
||||
}
|
||||
|
||||
public void setRoot(MultisigAddress root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public void setSettlementAddresses(ArrayList<String> settlementAddresses) {
|
||||
this.settlementAddresses = settlementAddresses;
|
||||
}
|
||||
|
||||
public ArrayList<String> getSettlementAddresses() {
|
||||
return settlementAddresses;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user