mirror of
https://github.com/gosticks/iota.flash.js-java-wrapper.git
synced 2026-06-28 15:50:03 +00:00
Finished porting test run except for close (signatures count does not add up yet)
This commit is contained in:
@@ -12,7 +12,7 @@ If you have any ideas please submit a request (I am totally not a Java guy so...
|
||||
- [x] updateLeafToRoot (needs testing)
|
||||
- [x] getDigest
|
||||
|
||||
#### Transfer
|
||||
#### Model.Transfer
|
||||
- [x] prepare (needs testing)
|
||||
- [x] compose (needs testing)
|
||||
- [x] close (needs testing)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import Model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Helpers {
|
||||
public static ArrayList<Bundle> createTransaction(UserObject user, ArrayList<Transfer> transfers, boolean shouldClose) {
|
||||
CreateTransactionHelperObject toUse = IotaFlashBridge.updateLeafToRoot(user.getFlash().root);
|
||||
CreateTransactionHelperObject toUse = IotaFlashBridge.updateLeafToRoot(user.getFlash().getRoot());
|
||||
|
||||
if (toUse.getGenerate() != 0) {
|
||||
// TODO: tell the server to gen new address.
|
||||
@@ -17,31 +18,38 @@ public class Helpers {
|
||||
// newTransfers = IotaFlashBridge.close(user.getFlash().getSettlementAddresses(), user.getFlash().deposits);
|
||||
} else {
|
||||
newTransfers = IotaFlashBridge.prepare(
|
||||
user.getFlash().settlementAddresses,
|
||||
user.getFlash().deposits,
|
||||
user.getFlash().getSettlementAddresses(),
|
||||
user.getFlash().getDeposits(),
|
||||
user.getUserIndex(),
|
||||
transfers
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Bundle> bundles = IotaFlashBridge.compose(
|
||||
user.getFlash().balance,
|
||||
user.getFlash().deposits,
|
||||
user.getFlash().outputs,
|
||||
user.getFlash().getBalance(),
|
||||
user.getFlash().getDeposits(),
|
||||
user.getFlash().getOutputs(),
|
||||
toUse.getAddress(),
|
||||
user.getFlash().remainderAddress,
|
||||
user.getFlash().transfers,
|
||||
user.getFlash().getRemainderAddress(),
|
||||
user.getFlash().getTransfers(),
|
||||
newTransfers,
|
||||
shouldClose
|
||||
);
|
||||
|
||||
|
||||
signTransaction(user, bundles);
|
||||
|
||||
return null;
|
||||
return bundles;
|
||||
}
|
||||
|
||||
public static Object signTransaction(UserObject user, ArrayList<Bundle> bundles) {
|
||||
return IotaFlashBridge.sign(user.getFlash().root, user.getSeed(), bundles);
|
||||
public static ArrayList<Signature> signTransaction(UserObject user, ArrayList<Bundle> bundles) {
|
||||
return IotaFlashBridge.sign(user.getFlash().getRoot(), user.getSeed(), bundles);
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Model.*;
|
||||
import com.eclipsesource.v8.*;
|
||||
import com.eclipsesource.v8.utils.V8ObjectUtils;
|
||||
import com.sun.org.apache.xpath.internal.operations.Mult;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -111,20 +111,13 @@ public class IotaFlashBridge {
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<Transaction> 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>();
|
||||
for (Transfer t: transfers) {
|
||||
transferObj.add(t.toMap());
|
||||
}
|
||||
V8Array transferJS = V8ObjectUtils.toV8Array(engine, transferObj);
|
||||
|
||||
// Now put all params into JS ready array.
|
||||
List<Object> params = new ArrayList<Object>();
|
||||
params.add(settlementAddressesJS);
|
||||
params.add(depositJS);
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(V8ObjectUtils.toV8Array(engine, settlementAddresses));
|
||||
params.add(V8ObjectUtils.toV8Array(engine, deposits));
|
||||
params.add(index);
|
||||
params.add(transferJS);
|
||||
params.add(V8Converter.transferListToV8Array(engine, transfers));
|
||||
|
||||
// Call js function.
|
||||
V8Array ret = transfer.executeArrayFunction("prepare", V8ObjectUtils.toV8Array(engine, params));
|
||||
@@ -153,111 +146,37 @@ public class IotaFlashBridge {
|
||||
* @param root
|
||||
* @param remainderAddress
|
||||
* @param history
|
||||
* @param transfers
|
||||
* @param transactions
|
||||
* @param close
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<Bundle> compose(int balance,
|
||||
List<Integer> deposits,
|
||||
List<Bundle> outputs,
|
||||
MultisigAddress root,
|
||||
MultisigAddress remainderAddress,
|
||||
List<Bundle> history,
|
||||
List<Transaction> transfers,
|
||||
boolean close) {
|
||||
V8Array depositsJS = V8ObjectUtils.toV8Array(engine, deposits);
|
||||
// Outputs
|
||||
List<Object> outputsObj = new ArrayList<Object>();
|
||||
for (Bundle t: outputs) {
|
||||
outputsObj.add(t.toMap());
|
||||
}
|
||||
V8Array outputsJS = V8ObjectUtils.toV8Array(engine, outputsObj);
|
||||
V8Object rootJS = V8ObjectUtils.toV8Object(engine, root.toMap());
|
||||
V8Object remainderJS = V8ObjectUtils.toV8Object(engine, remainderAddress.toMap());
|
||||
|
||||
List<Object> historyObj = new ArrayList<Object>();
|
||||
for (Bundle t: history) {
|
||||
historyObj.add(t.toMap());
|
||||
}
|
||||
V8Array historyJS = V8ObjectUtils.toV8Array(engine, historyObj);
|
||||
List<Integer> deposits,
|
||||
ArrayList<Bundle> outputs,
|
||||
MultisigAddress root,
|
||||
MultisigAddress remainderAddress,
|
||||
ArrayList<Bundle> history,
|
||||
ArrayList<Transaction> transactions,
|
||||
boolean close) {
|
||||
|
||||
|
||||
List<Object> transfersObj = new ArrayList<Object>();
|
||||
for (Transaction t: transfers) {
|
||||
transfersObj.add(t.toMap());
|
||||
}
|
||||
V8Array transfersJS = V8ObjectUtils.toV8Array(engine, transfersObj);
|
||||
|
||||
// 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);
|
||||
params.add(rootJS);
|
||||
params.add(remainderJS);
|
||||
params.add(history);
|
||||
params.add(transfersJS);
|
||||
params.add(V8ObjectUtils.toV8Array(engine, deposits));
|
||||
params.add(V8Converter.bundleListToV8Array(engine, outputs));
|
||||
params.add(V8Converter.multisigToV8Object(engine, root));
|
||||
params.add(V8Converter.multisigToV8Object(engine, remainderAddress));
|
||||
params.add(V8Converter.bundleListToV8Array(engine, history));
|
||||
params.add(V8Converter.transactionListToV8Array(engine, transactions));
|
||||
|
||||
// Call js function.
|
||||
V8Array ret = transfer.executeArrayFunction("compose", V8ObjectUtils.toV8Array(engine, params));
|
||||
List<Object> transfersReturnJS = V8ObjectUtils.toList(ret);
|
||||
|
||||
// Parse return as array of bundles
|
||||
ArrayList<Bundle> returnBundles = new ArrayList<Bundle>();
|
||||
for (Object returnEntry: transfersReturnJS) {
|
||||
ArrayList<Object> b = (ArrayList<Object>) returnEntry;
|
||||
|
||||
ArrayList<Transaction> returnedTransactions = new ArrayList<>();
|
||||
|
||||
for (Object parsedObjects: b) {
|
||||
Map<String, Object> bundleData = (Map<String, Object>) parsedObjects;
|
||||
String signatureMessageFragment = (String) bundleData.get("signatureMessageFragment");
|
||||
String bundle = (String) bundleData.get("bundle");
|
||||
String address = (String) bundleData.get("address");
|
||||
String attachmentTimestampLowerBound = (String) bundleData.get("attachmentTimestampLowerBound");
|
||||
String attachmentTimestampUpperBound = (String) bundleData.get("attachmentTimestampUpperBound");
|
||||
String trunkTransaction = (String) bundleData.get("trunkTransaction");
|
||||
String attachmentTimestamp = (String) bundleData.get("attachmentTimestamp");
|
||||
Integer timestamp = (Integer) bundleData.get("timestamp");
|
||||
String tag = (String) bundleData.get("tag");
|
||||
String branchTransaction = (String) bundleData.get("branchTransaction");
|
||||
String nonce = (String) bundleData.get("nonce");
|
||||
String obsoleteTag = (String) bundleData.get("obsoleteTag");
|
||||
|
||||
Integer currentIndex = (Integer) bundleData.get("currentIndex");
|
||||
Integer value = (Integer) bundleData.get("value");
|
||||
Integer lastIndex = (Integer) bundleData.get("lastIndex");
|
||||
|
||||
Transaction parsedTransaction = new Transaction(
|
||||
address,
|
||||
bundle,
|
||||
value.intValue(),
|
||||
obsoleteTag,
|
||||
tag,
|
||||
timestamp,
|
||||
signatureMessageFragment,
|
||||
trunkTransaction,
|
||||
branchTransaction,
|
||||
|
||||
attachmentTimestamp,
|
||||
attachmentTimestampUpperBound,
|
||||
attachmentTimestampLowerBound,
|
||||
nonce
|
||||
);
|
||||
|
||||
returnedTransactions.add(parsedTransaction);
|
||||
|
||||
System.out.println("Created bundle transaction: " + parsedTransaction.toString());
|
||||
}
|
||||
|
||||
Bundle bundle = new Bundle(returnedTransactions);
|
||||
returnBundles.add(bundle);
|
||||
}
|
||||
|
||||
System.out.println("Created bundles: " + returnBundles.size());
|
||||
|
||||
return returnBundles;
|
||||
return V8Converter.bundleListFromV8Array(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,38 +186,18 @@ public class IotaFlashBridge {
|
||||
* @param bundles
|
||||
* @return
|
||||
*/
|
||||
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) {
|
||||
List<Object> transactions = new ArrayList<Object>();
|
||||
for (Transaction t: b.getBundles()) {
|
||||
transactions.add(t.toMap());
|
||||
}
|
||||
bundleTmp.add(transactions);
|
||||
}
|
||||
V8Array bundlesJS = V8ObjectUtils.toV8Array(engine, bundleTmp);
|
||||
public static ArrayList<Signature> sign(MultisigAddress root, String seed, ArrayList<Bundle> bundles) {
|
||||
|
||||
// Create params.
|
||||
// Now put all params into JS ready array.
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(rootJS);
|
||||
params.add(V8Converter.multisigToV8Object(engine, root));
|
||||
params.add(seed);
|
||||
params.add(bundlesJS);
|
||||
params.add(V8Converter.bundleListToV8Array(engine, bundles));
|
||||
|
||||
V8Array signatures = transfer.executeArrayFunction("sign", V8ObjectUtils.toV8Array(engine, params));
|
||||
V8Array returnArray = transfer.executeArrayFunction("sign", V8ObjectUtils.toV8Array(engine, params));
|
||||
|
||||
for (Object o: V8ObjectUtils.toList(signatures)) {
|
||||
Map<String, Object> returnValues = (Map<String, Object>) o;
|
||||
for (Map.Entry<String, Object> entry: returnValues.entrySet()) {
|
||||
System.out.println(entry.getKey() + " : " + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add singature object.
|
||||
return signatures;
|
||||
return V8Converter.v8ArrayToSignatureList(returnArray);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,8 +206,14 @@ public class IotaFlashBridge {
|
||||
* @param signatures
|
||||
* @return
|
||||
*/
|
||||
public static Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> signatures) {
|
||||
return null;
|
||||
public static ArrayList<Bundle> appliedSignatures(ArrayList<Bundle> bundles, ArrayList<Signature> signatures) {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(V8Converter.bundleListToV8Array(engine, bundles));
|
||||
params.add(V8Converter.signatureListToV8Array(engine, signatures));
|
||||
|
||||
V8Array returnArray = transfer.executeArrayFunction("appliedSignatures", V8ObjectUtils.toV8Array(engine, params));
|
||||
// Parse returns
|
||||
return V8Converter.bundleListFromV8Array(returnArray);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -326,15 +231,29 @@ public class IotaFlashBridge {
|
||||
/**
|
||||
*
|
||||
* @param root
|
||||
* @param deposit
|
||||
* @param deposits
|
||||
* @param outputs
|
||||
* @param remainderAddress
|
||||
* @param transfers
|
||||
* @param signedBundles
|
||||
* @return
|
||||
*/
|
||||
public static Object applayTransfers(Object root, Object deposit, Object outputs, Object remainderAddress, Object transfers, Object signedBundles) {
|
||||
return null;
|
||||
public static void applayTransfers(MultisigAddress root,
|
||||
ArrayList<Integer> deposits,
|
||||
ArrayList<Bundle> outputs,
|
||||
MultisigAddress remainderAddress,
|
||||
ArrayList<Bundle> transfers,
|
||||
ArrayList<Bundle> signedBundles) {
|
||||
// Construct Java params
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(V8Converter.multisigToV8Object(engine, root));
|
||||
params.add(V8ObjectUtils.toV8Array(engine, deposits));
|
||||
params.add(V8Converter.bundleListToV8Array(engine, outputs));
|
||||
params.add(V8Converter.multisigToV8Object(engine, remainderAddress));
|
||||
params.add(V8Converter.bundleListToV8Array(engine, transfers));
|
||||
params.add(V8Converter.bundleListToV8Array(engine, signedBundles));
|
||||
|
||||
transfer.executeFunction("applyTransfers", V8ObjectUtils.toV8Array(engine, params));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import Model.Bundle;
|
||||
import Model.Digest;
|
||||
import Model.MultisigAddress;
|
||||
import Model.Transfer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -7,7 +12,7 @@ public interface IotaFlashInterface {
|
||||
|
||||
public void updateLeafToRoot(MultisigAddress root);
|
||||
|
||||
// Transfer
|
||||
// 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);
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Mult;
|
||||
import Model.*;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -75,7 +72,7 @@ public class Main {
|
||||
|
||||
// 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() );
|
||||
System.out.println("Adding child (" + oneMultisigs.get(i).toString() + ") to root :" + oneMultisigs.get(i - 1).toString() );
|
||||
oneMultisigs.get(i - 1).push(oneMultisigs.get(i));
|
||||
}
|
||||
|
||||
@@ -93,13 +90,53 @@ public class Main {
|
||||
oneFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||
twoFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||
|
||||
// Set digest/key index
|
||||
oneFlash.setIndex(oneFlash.getPartialDigests().size());
|
||||
twoFlash.setIndex(twoFlash.getPartialDigests().size());
|
||||
|
||||
System.out.println("Channel Setup!");
|
||||
|
||||
|
||||
ArrayList<Transfer> transfers = new ArrayList<>();
|
||||
transfers.add(new Transfer(twoSettlement, 200));
|
||||
|
||||
Helpers.createTransaction(oneFlash, transfers, false);
|
||||
System.out.println("Creating a transaction");
|
||||
ArrayList<Bundle> bundles = Helpers.createTransaction(oneFlash, transfers, false);
|
||||
|
||||
System.out.println("createTransaction completed");
|
||||
for (Bundle b: bundles) {
|
||||
System.out.println(b.toString());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
// Sign bundle with your USER ONE'S signatures
|
||||
ArrayList<Bundle> signedBundles = IotaFlashBridge.appliedSignatures(bundles, oneSignatures);
|
||||
|
||||
// ADD USER TWOS'S signatures to the partially signed bundles
|
||||
signedBundles = IotaFlashBridge.appliedSignatures(signedBundles, twoSignatures);
|
||||
|
||||
/////////////////////////////////
|
||||
/// APPLY SIGNED BUNDLES
|
||||
|
||||
// Apply transfers to User ONE
|
||||
Helpers.applyTransfers(oneFlash, signedBundles);
|
||||
|
||||
// Save latest channel bundles
|
||||
oneFlash.setBundles(signedBundles);
|
||||
|
||||
// Apply transfers to User TWO
|
||||
Helpers.applyTransfers(twoFlash, signedBundles);
|
||||
// Save latest channel bundles
|
||||
twoFlash.setBundles(signedBundles);
|
||||
|
||||
System.out.println("Transaction Applied!");
|
||||
|
||||
}
|
||||
|
||||
private static void setupUser(UserObject user, int TREE_DEPTH) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package Model;
|
||||
|
||||
import com.eclipsesource.v8.V8;
|
||||
import com.eclipsesource.v8.V8Array;
|
||||
|
||||
@@ -6,13 +8,17 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class Bundle {
|
||||
public class Bundle {
|
||||
private ArrayList<Transaction> bundles;
|
||||
|
||||
public Bundle(ArrayList<Transaction> bundles) {
|
||||
this.bundles = bundles;
|
||||
}
|
||||
|
||||
public Bundle() {
|
||||
this.bundles = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
List<Object> bundleList = new ArrayList<Object>();
|
||||
19
src/main/java/Model/CreateTransactionHelperObject.java
Normal file
19
src/main/java/Model/CreateTransactionHelperObject.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package Model;
|
||||
|
||||
public class CreateTransactionHelperObject {
|
||||
private int generate = 0;
|
||||
private MultisigAddress address;
|
||||
|
||||
public CreateTransactionHelperObject(int gen, MultisigAddress addr) {
|
||||
this.generate = gen;
|
||||
this.address = addr;
|
||||
}
|
||||
|
||||
public int getGenerate() {
|
||||
return generate;
|
||||
}
|
||||
|
||||
public MultisigAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package Model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class Digest {
|
||||
public class Digest {
|
||||
private int index;
|
||||
private int security;
|
||||
private String digest;
|
||||
64
src/main/java/Model/FlashObject.java
Normal file
64
src/main/java/Model/FlashObject.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package Model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public 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<Bundle> transfers = new ArrayList<Bundle>();
|
||||
|
||||
public FlashObject(int signersCount, int balance, ArrayList<Integer> deposits) {
|
||||
this.signersCount = signersCount;
|
||||
this.balance = balance;
|
||||
this.deposits = deposits;
|
||||
}
|
||||
|
||||
public int getSignersCount() {
|
||||
return signersCount;
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public MultisigAddress getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
public ArrayList<Integer> getDeposits() {
|
||||
return deposits;
|
||||
}
|
||||
|
||||
public ArrayList<Bundle> getOutputs() {
|
||||
return outputs;
|
||||
}
|
||||
|
||||
public ArrayList<Bundle> getTransfers() {
|
||||
return transfers;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
package Model;
|
||||
|
||||
import com.eclipsesource.v8.V8;
|
||||
import com.eclipsesource.v8.V8Object;
|
||||
import com.eclipsesource.v8.utils.V8ObjectUtils;
|
||||
@@ -7,25 +9,8 @@ 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 {
|
||||
public class MultisigAddress {
|
||||
private String address;
|
||||
private int securitySum;
|
||||
private int index;
|
||||
58
src/main/java/Model/Signature.java
Normal file
58
src/main/java/Model/Signature.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package Model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class represents an Model.Signature.
|
||||
*
|
||||
* @author Adrian
|
||||
**/
|
||||
public class Signature {
|
||||
|
||||
private String address;
|
||||
private List<String> signatureFragments;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the Model.Signature class.
|
||||
*/
|
||||
public Signature() {
|
||||
this.signatureFragments = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the address.
|
||||
*
|
||||
* @return The address.
|
||||
*/
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the address.
|
||||
*
|
||||
* @param address The address.
|
||||
*/
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the signatureFragments.
|
||||
*
|
||||
* @return The signatureFragments.
|
||||
*/
|
||||
public List<String> getSignatureFragments() {
|
||||
return signatureFragments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the signatureFragments.
|
||||
*
|
||||
* @param signatureFragments The signatureFragments.
|
||||
*/
|
||||
public void setSignatureFragments(List<String> signatureFragments) {
|
||||
this.signatureFragments = signatureFragments;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
package Model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class Transaction {
|
||||
public class Transaction {
|
||||
private int timestamp;
|
||||
private String address;
|
||||
private int value;
|
||||
private String obsoleteTag;
|
||||
private String tag;
|
||||
|
||||
// Signature stuff
|
||||
// Model.Signature stuff
|
||||
private String bundle = "";
|
||||
private String signatureMessageFragment = "";
|
||||
private String trunkTransaction = "";
|
||||
@@ -1,7 +1,9 @@
|
||||
package Model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
class Transfer {
|
||||
public class Transfer {
|
||||
private String address;
|
||||
private int value;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package Model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class UserObject {
|
||||
@@ -11,7 +13,7 @@ public class UserObject {
|
||||
private ArrayList<MultisigAddress> multisigDigests = new ArrayList<MultisigAddress>();
|
||||
private FlashObject flash;
|
||||
|
||||
UserObject(int userID, String seed, int depth, FlashObject flash) {
|
||||
public UserObject(int userID, String seed, int depth, FlashObject flash) {
|
||||
this.userIndex = userID;
|
||||
this.seed = seed;
|
||||
this.depth = depth;
|
||||
@@ -27,6 +29,8 @@ public class UserObject {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* Getters and Setters
|
||||
@@ -36,6 +40,14 @@ public class UserObject {
|
||||
this.multisigDigests = multisigDigests;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void setBundles(ArrayList<Bundle> bundles) {
|
||||
this.bundles = bundles;
|
||||
}
|
||||
|
||||
public ArrayList<MultisigAddress> getMultisigDigests() {
|
||||
return multisigDigests;
|
||||
}
|
||||
@@ -70,39 +82,3 @@ public class UserObject {
|
||||
|
||||
}
|
||||
|
||||
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<Bundle> transfers = new ArrayList<Bundle>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
130
src/main/java/V8Converter.java
Normal file
130
src/main/java/V8Converter.java
Normal file
@@ -0,0 +1,130 @@
|
||||
import Model.*;
|
||||
import com.eclipsesource.v8.V8;
|
||||
import com.eclipsesource.v8.V8Array;
|
||||
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;
|
||||
|
||||
public class V8Converter {
|
||||
public static ArrayList<Signature> v8ArrayToSignatureList(V8Array array) {
|
||||
// Try parsing signature array from response.
|
||||
ArrayList<Signature> signatures = new ArrayList<>();
|
||||
for (Object o: V8ObjectUtils.toList(array)) {
|
||||
Map<String, Object> returnValues = (Map<String, Object>) o;
|
||||
String addr = (String) returnValues.get("address");
|
||||
ArrayList<String> signatureFragments = (ArrayList<String>) returnValues.get("signatureFragments");
|
||||
Signature sig = new Signature();
|
||||
sig.setAddress(addr);
|
||||
sig.setSignatureFragments(signatureFragments);
|
||||
signatures.add(sig);
|
||||
}
|
||||
|
||||
return signatures;
|
||||
}
|
||||
|
||||
public static V8Object multisigToV8Object(V8 engine, MultisigAddress multisig) {
|
||||
Map<String, Object> sigMapg = multisig.toMap();
|
||||
return V8ObjectUtils.toV8Object(engine, sigMapg);
|
||||
}
|
||||
|
||||
public static V8Array bundleListToV8Array(V8 engine, ArrayList<Bundle> bundles) {
|
||||
|
||||
List<Object> bundleTmp = new ArrayList<Object>();
|
||||
for (Bundle b: bundles) {
|
||||
List<Object> transactions = new ArrayList<Object>();
|
||||
for (Transaction t: b.getBundles()) {
|
||||
transactions.add(t.toMap());
|
||||
}
|
||||
bundleTmp.add(transactions);
|
||||
}
|
||||
return V8ObjectUtils.toV8Array(engine, bundleTmp);
|
||||
}
|
||||
|
||||
public static ArrayList<Bundle> bundleListFromV8Array(V8Array input) {
|
||||
List<Object> inputList = V8ObjectUtils.toList(input);
|
||||
// Parse return as array of bundles
|
||||
ArrayList<Bundle> returnBundles = new ArrayList<>();
|
||||
for (Object bundleItem: inputList) {
|
||||
ArrayList<Object> bundleContent = (ArrayList<Object>) bundleItem;
|
||||
|
||||
ArrayList<Transaction> returnedTransactions = new ArrayList<>();
|
||||
for (Object rawTransaction: bundleContent) {
|
||||
returnedTransactions.add(transactionFromObject(rawTransaction));
|
||||
}
|
||||
|
||||
Bundle bundle = new Bundle(returnedTransactions);
|
||||
returnBundles.add(bundle);
|
||||
}
|
||||
return returnBundles;
|
||||
}
|
||||
|
||||
public static V8Array signatureListToV8Array(V8 engine, ArrayList<Signature> signatures) {
|
||||
List<Object> returnArr = new ArrayList<>();
|
||||
for (Signature sig: signatures) {
|
||||
Map<String, Object> signatureMap = new HashMap<String, Object>();
|
||||
signatureMap.put("address", sig.getAddress());
|
||||
signatureMap.put("signatureFragments", sig.getSignatureFragments());
|
||||
returnArr.add(signatureMap);
|
||||
}
|
||||
return V8ObjectUtils.toV8Array(engine, returnArr);
|
||||
}
|
||||
|
||||
public static V8Array transferListToV8Array(V8 engine, ArrayList<Transfer> transfers) {
|
||||
List<Object> transferObj = new ArrayList<Object>();
|
||||
for (Transfer t: transfers) {
|
||||
transferObj.add(t.toMap());
|
||||
}
|
||||
return V8ObjectUtils.toV8Array(engine, transferObj);
|
||||
}
|
||||
|
||||
public static V8Array transactionListToV8Array(V8 engine, ArrayList<Transaction> transactions) {
|
||||
List<Object> transfersObj = new ArrayList<Object>();
|
||||
for (Transaction t: transactions) {
|
||||
transfersObj.add(t.toMap());
|
||||
}
|
||||
return V8ObjectUtils.toV8Array(engine, transfersObj);
|
||||
}
|
||||
|
||||
|
||||
public static Transaction transactionFromObject(Object input) {
|
||||
Map<String, Object> bundleData = (Map<String, Object>) input;
|
||||
String signatureMessageFragment = (String) bundleData.get("signatureMessageFragment");
|
||||
String bundle = (String) bundleData.get("bundle");
|
||||
String address = (String) bundleData.get("address");
|
||||
String attachmentTimestampLowerBound = (String) bundleData.get("attachmentTimestampLowerBound");
|
||||
String attachmentTimestampUpperBound = (String) bundleData.get("attachmentTimestampUpperBound");
|
||||
String trunkTransaction = (String) bundleData.get("trunkTransaction");
|
||||
String attachmentTimestamp = (String) bundleData.get("attachmentTimestamp");
|
||||
Integer timestamp = (Integer) bundleData.get("timestamp");
|
||||
String tag = (String) bundleData.get("tag");
|
||||
String branchTransaction = (String) bundleData.get("branchTransaction");
|
||||
String nonce = (String) bundleData.get("nonce");
|
||||
String obsoleteTag = (String) bundleData.get("obsoleteTag");
|
||||
|
||||
Integer currentIndex = (Integer) bundleData.get("currentIndex");
|
||||
Integer value = (Integer) bundleData.get("value");
|
||||
Integer lastIndex = (Integer) bundleData.get("lastIndex");
|
||||
|
||||
Transaction parsedTransaction = new Transaction(
|
||||
address,
|
||||
bundle,
|
||||
value.intValue(),
|
||||
obsoleteTag,
|
||||
tag,
|
||||
timestamp,
|
||||
signatureMessageFragment,
|
||||
trunkTransaction,
|
||||
branchTransaction,
|
||||
|
||||
attachmentTimestamp,
|
||||
attachmentTimestampUpperBound,
|
||||
attachmentTimestampLowerBound,
|
||||
nonce
|
||||
);
|
||||
return parsedTransaction;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user