mirror of
https://github.com/gosticks/iota.flash.js-java-wrapper.git
synced 2025-10-16 11:45:40 +00:00
Minor usability tweaks
This commit is contained in:
parent
e197850967
commit
ddb13836bd
@ -119,9 +119,9 @@ public class Helpers {
|
|||||||
public static ArrayList<Digest> getNewBranchDigests(UserObject user, int toGenerate) {
|
public static ArrayList<Digest> getNewBranchDigests(UserObject user, int toGenerate) {
|
||||||
ArrayList<Digest> digests = new ArrayList<>();
|
ArrayList<Digest> digests = new ArrayList<>();
|
||||||
for (int i = 0; i < toGenerate; i++) {
|
for (int i = 0; i < toGenerate; i++) {
|
||||||
Digest digest = IotaFlashBridge.getDigest(user.getSeed(), user.getIndex(), user.getSecurity());
|
Digest digest = IotaFlashBridge.getDigest(user.getSeed(), user.getSeedIndex(), user.getSecurity());
|
||||||
System.out.println("USING index for digest: " + user.getIndex() );
|
System.out.println("USING index for digest: " + user.getSeedIndex() );
|
||||||
user.incrementIndex();
|
user.incrementSeedIndex();
|
||||||
digests.add(digest);
|
digests.add(digest);
|
||||||
}
|
}
|
||||||
return digests;
|
return digests;
|
||||||
@ -179,10 +179,10 @@ public class Helpers {
|
|||||||
// Create new digest
|
// Create new digest
|
||||||
Digest digest = IotaFlashBridge.getDigest(
|
Digest digest = IotaFlashBridge.getDigest(
|
||||||
user.getSeed(),
|
user.getSeed(),
|
||||||
user.getIndex(),
|
user.getSeedIndex(),
|
||||||
user.getSecurity()
|
user.getSecurity()
|
||||||
);
|
);
|
||||||
user.incrementIndex();
|
user.incrementSeedIndex();
|
||||||
System.out.println("Adding digest (" + digest.toString() + ") to user " + user.getUserIndex());
|
System.out.println("Adding digest (" + digest.toString() + ") to user " + user.getUserIndex());
|
||||||
// Increment key index
|
// Increment key index
|
||||||
|
|
||||||
@ -386,10 +386,12 @@ public class Helpers {
|
|||||||
* @return amount of IOTA
|
* @return amount of IOTA
|
||||||
*/
|
*/
|
||||||
public static double getBalanceOfUser(UserObject user) {
|
public static double getBalanceOfUser(UserObject user) {
|
||||||
double balance = user.getFlash().getDeposits().get(user.getUserIndex());
|
FlashObject flash = user.getFlash();
|
||||||
Map<String, Integer> transfers = user.getFlash().getOutputs();
|
double balance = flash.getDeposits().get(user.getUserIndex());
|
||||||
|
Map<String, Integer> transfers = flash.getOutputs();
|
||||||
for (Map.Entry<String, Integer> transfer : transfers.entrySet()) {
|
for (Map.Entry<String, Integer> transfer : transfers.entrySet()) {
|
||||||
if (transfer.getKey().equals(user.getAddress())) {
|
String userSettlementAddr = flash.getSettlementAddresses().get(user.getUserIndex());
|
||||||
|
if (transfer.getKey().equals(userSettlementAddr)) {
|
||||||
balance += transfer.getValue();
|
balance += transfer.getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,39 +0,0 @@
|
|||||||
package iotaFlashWrapper;
|
|
||||||
|
|
||||||
import iotaFlashWrapper.Model.Bundle;
|
|
||||||
import iotaFlashWrapper.Model.Digest;
|
|
||||||
import iotaFlashWrapper.Model.Multisig;
|
|
||||||
import iotaFlashWrapper.Model.Transfer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface IotaFlashInterface {
|
|
||||||
// Multisig
|
|
||||||
public Multisig composeAddress(ArrayList<Digest> digests);
|
|
||||||
|
|
||||||
public void updateLeafToRoot(Multisig root);
|
|
||||||
|
|
||||||
// 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, Multisig root, String remainderAddress, ArrayList<Bundle> history, ArrayList<Transfer> transfers, boolean close);
|
|
||||||
|
|
||||||
public Digest getDigest(String seed, int index, int security);
|
|
||||||
|
|
||||||
public Object sign(Multisig root, String seed, ArrayList<Bundle> bundles);
|
|
||||||
public Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> signatures);
|
|
||||||
public Object getDiff(ArrayList<Object> root,
|
|
||||||
ArrayList<Object> remainder,
|
|
||||||
ArrayList<Object> history,
|
|
||||||
ArrayList<Object> bundles);
|
|
||||||
public Object applayTransfers(Object root,
|
|
||||||
Object deposit,
|
|
||||||
Object outputs,
|
|
||||||
Object remainderAddress,
|
|
||||||
Object transfers,
|
|
||||||
Object signedBundles);
|
|
||||||
public Object close(ArrayList<String> settlementAddresses, ArrayList<Integer> deposits);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -30,17 +30,15 @@ public class Main {
|
|||||||
|
|
||||||
// Security level
|
// Security level
|
||||||
int SECURITY = 1;
|
int SECURITY = 1;
|
||||||
// Number of parties taking signing part in the channel
|
|
||||||
int SIGNERS_COUNT = 2;
|
|
||||||
// Flash tree depth
|
// Flash tree depth
|
||||||
int TREE_DEPTH = 4;
|
int TREE_DEPTH = 4;
|
||||||
// Total channel Balance
|
|
||||||
int CHANNEL_BALANCE = 200;
|
|
||||||
// Users deposits
|
// Users deposits
|
||||||
double[] DEPOSITS = new double[]{100.0, 100.0};
|
double[] DEPOSITS = new double[]{100.0, 100.0};
|
||||||
// Setup users.
|
// Setup users.
|
||||||
UserObject oneFlash = new UserObject(0, oneSeed, TREE_DEPTH, SECURITY, oneSettlement, new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS));
|
UserObject oneFlash = new UserObject(0, oneSeed, 0, SECURITY);
|
||||||
UserObject twoFlash = new UserObject(1, twoSeed, TREE_DEPTH, SECURITY, twoSettlement, new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS));
|
oneFlash.setFlash(new FlashObject(DEPOSITS, TREE_DEPTH, SECURITY));
|
||||||
|
UserObject twoFlash = new UserObject(1, twoSeed, 0, SECURITY);
|
||||||
|
twoFlash.setFlash(new FlashObject(DEPOSITS, TREE_DEPTH, SECURITY));
|
||||||
|
|
||||||
// USER ONE
|
// USER ONE
|
||||||
ArrayList<Digest> oneDigests = Helpers.getDigestsForUser(oneFlash, TREE_DEPTH);
|
ArrayList<Digest> oneDigests = Helpers.getDigestsForUser(oneFlash, TREE_DEPTH);
|
||||||
@ -111,8 +109,8 @@ public class Main {
|
|||||||
twoFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
twoFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||||
|
|
||||||
// Set digest/key index
|
// Set digest/key index
|
||||||
oneFlash.setIndex(oneDigests.size());
|
oneFlash.setSeedIndex(oneDigests.size());
|
||||||
twoFlash.setIndex(twoDigests.size());
|
twoFlash.setSeedIndex(twoDigests.size());
|
||||||
|
|
||||||
System.out.println("Channel Setup completed!");
|
System.out.println("Channel Setup completed!");
|
||||||
|
|
||||||
@ -144,7 +142,7 @@ public class Main {
|
|||||||
ArrayList<Bundle> confirmedTransfers;
|
ArrayList<Bundle> confirmedTransfers;
|
||||||
|
|
||||||
// Try to make 10 transfers.
|
// Try to make 10 transfers.
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
|
|
||||||
// Create transaction helper and check if we need to add nodes
|
// Create transaction helper and check if we need to add nodes
|
||||||
CreateTransactionHelperObject helper = Helpers.getTransactionHelper(oneFlash.getFlash().getRoot());
|
CreateTransactionHelperObject helper = Helpers.getTransactionHelper(oneFlash.getFlash().getRoot());
|
||||||
|
|||||||
@ -1,30 +1,34 @@
|
|||||||
package iotaFlashWrapper.Model;
|
package iotaFlashWrapper.Model;
|
||||||
|
|
||||||
import com.eclipsesource.v8.utils.V8ObjectUtils;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class FlashObject {
|
public class FlashObject {
|
||||||
int signersCount = 2;
|
private int signersCount = 2;
|
||||||
int balance;
|
private int balance;
|
||||||
List<String> settlementAddresses;
|
private List<String> settlementAddresses;
|
||||||
List<Double> deposits;
|
private List<Double> deposits;
|
||||||
Map<String, Integer> outputs = new HashMap<>();
|
private Map<String, Integer> outputs = new HashMap<>();
|
||||||
List<Bundle> transfers = new ArrayList<Bundle>();
|
private List<Bundle> transfers = new ArrayList<Bundle>();
|
||||||
Multisig root;
|
private Multisig root;
|
||||||
Multisig remainderAddress;
|
private Multisig remainderAddress;
|
||||||
|
private int depth;
|
||||||
|
private int security;
|
||||||
|
|
||||||
|
public FlashObject(double[] deposits, int depth, int security) {
|
||||||
|
|
||||||
public FlashObject(int signersCount, int balance, double[] deposits) {
|
this.signersCount = deposits.length;
|
||||||
this.signersCount = signersCount;
|
for (double dep : deposits) {
|
||||||
this.balance = balance;
|
this.balance += dep;
|
||||||
|
}
|
||||||
this.deposits = new ArrayList<>();
|
this.deposits = new ArrayList<>();
|
||||||
for (double deposit : deposits){
|
for (double deposit : deposits){
|
||||||
this.deposits.add(deposit);
|
this.deposits.add(deposit);
|
||||||
}
|
}
|
||||||
|
this.depth = depth;
|
||||||
|
this.security = security;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlashObject(int signersCount, int balance, List<String> settlementAddresses, List<Double> deposits, Map<String, Integer> outputs, List<Bundle> transfers, Multisig root, Multisig remainderAddress) {
|
public FlashObject(int signersCount, int balance, List<String> settlementAddresses, List<Double> deposits, Map<String, Integer> outputs, List<Bundle> transfers, Multisig root, Multisig remainderAddress, int depth, int security) {
|
||||||
this.signersCount = signersCount;
|
this.signersCount = signersCount;
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
this.settlementAddresses = settlementAddresses;
|
this.settlementAddresses = settlementAddresses;
|
||||||
@ -33,6 +37,8 @@ public class FlashObject {
|
|||||||
this.transfers = transfers;
|
this.transfers = transfers;
|
||||||
this.root = root;
|
this.root = root;
|
||||||
this.remainderAddress = remainderAddress;
|
this.remainderAddress = remainderAddress;
|
||||||
|
this.depth = depth;
|
||||||
|
this.security = security;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
@ -42,7 +48,8 @@ public class FlashObject {
|
|||||||
objectMap.put("root", root.toMap());
|
objectMap.put("root", root.toMap());
|
||||||
objectMap.put("remainderAddress", remainderAddress.toMap());
|
objectMap.put("remainderAddress", remainderAddress.toMap());
|
||||||
objectMap.put("settlementAddresses", getSettlementAddresses());
|
objectMap.put("settlementAddresses", getSettlementAddresses());
|
||||||
|
objectMap.put("depth", getDepth());
|
||||||
|
objectMap.put("security", getSecurity());
|
||||||
// Wrap outputs inside an array.
|
// Wrap outputs inside an array.
|
||||||
objectMap.put("outputs", getOutputs());
|
objectMap.put("outputs", getOutputs());
|
||||||
|
|
||||||
@ -57,12 +64,20 @@ public class FlashObject {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getBalance() {
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
public int getSignersCount() {
|
public int getSignersCount() {
|
||||||
return signersCount;
|
return signersCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBalance() {
|
public int getDepth() {
|
||||||
return balance;
|
return depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSecurity() {
|
||||||
|
return security;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Multisig getRoot() {
|
public Multisig getRoot() {
|
||||||
@ -76,7 +91,6 @@ public class FlashObject {
|
|||||||
public Map<String, Integer> getOutputs() {
|
public Map<String, Integer> getOutputs() {
|
||||||
return outputs;
|
return outputs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Bundle> getTransfers() {
|
public List<Bundle> getTransfers() {
|
||||||
return transfers;
|
return transfers;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,31 +7,21 @@ import java.util.Map;
|
|||||||
public class UserObject {
|
public class UserObject {
|
||||||
private int userIndex;
|
private int userIndex;
|
||||||
private String seed;
|
private String seed;
|
||||||
private int index = 0;
|
private int seedIndex;
|
||||||
private int security;
|
private int security;
|
||||||
private int depth;
|
|
||||||
private String address;
|
|
||||||
private ArrayList<Bundle> bundles = new ArrayList<Bundle>();
|
|
||||||
private ArrayList<Digest> partialDigests = new ArrayList<Digest>();
|
|
||||||
private ArrayList<Multisig> multisigDigests = new ArrayList<Multisig>();
|
|
||||||
private FlashObject flash;
|
private FlashObject flash;
|
||||||
|
|
||||||
public UserObject(int userID, String seed, int depth, int security, String address, FlashObject flash) {
|
public UserObject(int userID, String seed, int seedIndex, int security) {
|
||||||
this.userIndex = userID;
|
this.userIndex = userID;
|
||||||
this.seed = seed;
|
this.seed = seed;
|
||||||
this.depth = depth;
|
this.seedIndex = seedIndex;
|
||||||
this.security = security;
|
this.security = security;
|
||||||
this.flash = flash;
|
|
||||||
this.address = address;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int incrementIndex() {
|
public int incrementSeedIndex() {
|
||||||
this.index = this.index + 1;
|
this.seedIndex = this.seedIndex + 1;
|
||||||
return index;
|
return seedIndex;
|
||||||
}
|
|
||||||
|
|
||||||
public void add(Digest digest) {
|
|
||||||
partialDigests.add(digest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,21 +29,9 @@ public class UserObject {
|
|||||||
String out = "";
|
String out = "";
|
||||||
out += "userIndex: " + userIndex + "\n";
|
out += "userIndex: " + userIndex + "\n";
|
||||||
out += "seed: " + seed + "\n";
|
out += "seed: " + seed + "\n";
|
||||||
out += "index: " + index + "\n";
|
out += "seedIndex: " + seedIndex + "\n";
|
||||||
out += "security: " + getSecurity() + "\n";
|
out += "security: " + getSecurity() + "\n";
|
||||||
out += "depth: " + depth + "\n";
|
|
||||||
out += "bundles: " + "\n";
|
out += "bundles: " + "\n";
|
||||||
for (Bundle b: bundles) {
|
|
||||||
out += "\t" + b.toString() + "\n";
|
|
||||||
}
|
|
||||||
out += "partialDigests: " + "\n";
|
|
||||||
for (Digest d: partialDigests) {
|
|
||||||
out += "\t" + d.toString() + "\n";
|
|
||||||
}
|
|
||||||
out += "multisigDigests: " + "\n";
|
|
||||||
for (Multisig m: multisigDigests) {
|
|
||||||
out += "\t" + m.toString() + "\n";
|
|
||||||
}
|
|
||||||
out += "Flash: " + "\n";
|
out += "Flash: " + "\n";
|
||||||
out += flash.toString();
|
out += flash.toString();
|
||||||
|
|
||||||
@ -64,27 +42,8 @@ public class UserObject {
|
|||||||
Map<String, Object> objectMap = new HashMap<>();
|
Map<String, Object> objectMap = new HashMap<>();
|
||||||
objectMap.put("userIndex", getUserIndex());
|
objectMap.put("userIndex", getUserIndex());
|
||||||
objectMap.put("seed", getSeed());
|
objectMap.put("seed", getSeed());
|
||||||
objectMap.put("index", getIndex());
|
objectMap.put("seedIndex", getSeedIndex());
|
||||||
objectMap.put("security", getSecurity());
|
objectMap.put("security", getSecurity());
|
||||||
objectMap.put("depth", depth);
|
|
||||||
|
|
||||||
ArrayList<Object> bundleMaps = new ArrayList<>();
|
|
||||||
for (Bundle b: bundles) {
|
|
||||||
bundleMaps.add(b.toMap());
|
|
||||||
}
|
|
||||||
objectMap.put("bundles", bundleMaps);
|
|
||||||
|
|
||||||
ArrayList<Object> partialDigestMaps = new ArrayList<>();
|
|
||||||
for (Bundle b: bundles) {
|
|
||||||
partialDigestMaps.add(b.toMap());
|
|
||||||
}
|
|
||||||
objectMap.put("partialDigests", partialDigestMaps);
|
|
||||||
|
|
||||||
ArrayList<Object> multisigDigestsMaps = new ArrayList<>();
|
|
||||||
for (Bundle b: bundles) {
|
|
||||||
partialDigestMaps.add(b.toMap());
|
|
||||||
}
|
|
||||||
objectMap.put("multisigDigests", multisigDigestsMaps);
|
|
||||||
objectMap.put("flash", flash.toMap());
|
objectMap.put("flash", flash.toMap());
|
||||||
return objectMap;
|
return objectMap;
|
||||||
}
|
}
|
||||||
@ -94,24 +53,12 @@ public class UserObject {
|
|||||||
* Getters and Setters
|
* Getters and Setters
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void setMultisigDigests(ArrayList<Multisig> multisigDigests) {
|
|
||||||
this.multisigDigests = multisigDigests;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFlash(FlashObject flash) {
|
public void setFlash(FlashObject flash) {
|
||||||
this.flash = flash;
|
this.flash = flash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIndex(int index) {
|
public void setSeedIndex(int index) {
|
||||||
this.index = index;
|
this.seedIndex = index;
|
||||||
}
|
|
||||||
|
|
||||||
public void setBundles(ArrayList<Bundle> bundles) {
|
|
||||||
this.bundles = bundles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Multisig> getMultisigDigests() {
|
|
||||||
return multisigDigests;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSecurity() {
|
public int getSecurity() {
|
||||||
@ -122,32 +69,17 @@ public class UserObject {
|
|||||||
return seed;
|
return seed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIndex() {
|
public int getSeedIndex() {
|
||||||
return index;
|
return seedIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getUserIndex() {
|
public int getUserIndex() {
|
||||||
return userIndex;
|
return userIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Bundle> getBundles() {
|
|
||||||
return bundles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Digest> getPartialDigests() {
|
|
||||||
return partialDigests;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FlashObject getFlash() {
|
public FlashObject getFlash() {
|
||||||
return flash;
|
return flash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDepth() {
|
|
||||||
return depth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAddress() {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -67,8 +67,9 @@ public class V8Converter {
|
|||||||
}
|
}
|
||||||
ArrayList<Bundle> transfers = bundleListFromArrayList((ArrayList<Object>) inputMap.get("transfers"));
|
ArrayList<Bundle> transfers = bundleListFromArrayList((ArrayList<Object>) inputMap.get("transfers"));
|
||||||
Map<String, Integer> outputs = (Map<String, Integer>) inputMap.get("outputs");
|
Map<String, Integer> outputs = (Map<String, Integer>) inputMap.get("outputs");
|
||||||
|
Integer depth = (Integer) inputMap.get("depth");
|
||||||
return new FlashObject(singersCount, balance, settlementAddresses, deposits, outputs, transfers, root, remainderAddress);
|
Integer security = (Integer) inputMap.get("security");
|
||||||
|
return new FlashObject(singersCount, balance, settlementAddresses, deposits, outputs, transfers, root, remainderAddress, depth ,security);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static V8Array bundleListToV8Array(V8 engine, List<Bundle> bundles) {
|
public static V8Array bundleListToV8Array(V8 engine, List<Bundle> bundles) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user