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) {
|
||||
ArrayList<Digest> digests = new ArrayList<>();
|
||||
for (int i = 0; i < toGenerate; i++) {
|
||||
Digest digest = IotaFlashBridge.getDigest(user.getSeed(), user.getIndex(), user.getSecurity());
|
||||
System.out.println("USING index for digest: " + user.getIndex() );
|
||||
user.incrementIndex();
|
||||
Digest digest = IotaFlashBridge.getDigest(user.getSeed(), user.getSeedIndex(), user.getSecurity());
|
||||
System.out.println("USING index for digest: " + user.getSeedIndex() );
|
||||
user.incrementSeedIndex();
|
||||
digests.add(digest);
|
||||
}
|
||||
return digests;
|
||||
@ -179,10 +179,10 @@ public class Helpers {
|
||||
// Create new digest
|
||||
Digest digest = IotaFlashBridge.getDigest(
|
||||
user.getSeed(),
|
||||
user.getIndex(),
|
||||
user.getSeedIndex(),
|
||||
user.getSecurity()
|
||||
);
|
||||
user.incrementIndex();
|
||||
user.incrementSeedIndex();
|
||||
System.out.println("Adding digest (" + digest.toString() + ") to user " + user.getUserIndex());
|
||||
// Increment key index
|
||||
|
||||
@ -386,10 +386,12 @@ public class Helpers {
|
||||
* @return amount of IOTA
|
||||
*/
|
||||
public static double getBalanceOfUser(UserObject user) {
|
||||
double balance = user.getFlash().getDeposits().get(user.getUserIndex());
|
||||
Map<String, Integer> transfers = user.getFlash().getOutputs();
|
||||
FlashObject flash = user.getFlash();
|
||||
double balance = flash.getDeposits().get(user.getUserIndex());
|
||||
Map<String, Integer> transfers = flash.getOutputs();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
int SECURITY = 1;
|
||||
// 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 = 200;
|
||||
// Users deposits
|
||||
double[] DEPOSITS = new double[]{100.0, 100.0};
|
||||
// Setup users.
|
||||
UserObject oneFlash = new UserObject(0, oneSeed, TREE_DEPTH, SECURITY, oneSettlement, new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS));
|
||||
UserObject twoFlash = new UserObject(1, twoSeed, TREE_DEPTH, SECURITY, twoSettlement, new FlashObject(SIGNERS_COUNT, CHANNEL_BALANCE, DEPOSITS));
|
||||
UserObject oneFlash = new UserObject(0, oneSeed, 0, SECURITY);
|
||||
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
|
||||
ArrayList<Digest> oneDigests = Helpers.getDigestsForUser(oneFlash, TREE_DEPTH);
|
||||
@ -111,8 +109,8 @@ public class Main {
|
||||
twoFlash.getFlash().setSettlementAddresses(settlementAddresses);
|
||||
|
||||
// Set digest/key index
|
||||
oneFlash.setIndex(oneDigests.size());
|
||||
twoFlash.setIndex(twoDigests.size());
|
||||
oneFlash.setSeedIndex(oneDigests.size());
|
||||
twoFlash.setSeedIndex(twoDigests.size());
|
||||
|
||||
System.out.println("Channel Setup completed!");
|
||||
|
||||
@ -144,7 +142,7 @@ public class Main {
|
||||
ArrayList<Bundle> confirmedTransfers;
|
||||
|
||||
// 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
|
||||
CreateTransactionHelperObject helper = Helpers.getTransactionHelper(oneFlash.getFlash().getRoot());
|
||||
|
||||
@ -1,30 +1,34 @@
|
||||
package iotaFlashWrapper.Model;
|
||||
|
||||
import com.eclipsesource.v8.utils.V8ObjectUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class FlashObject {
|
||||
int signersCount = 2;
|
||||
int balance;
|
||||
List<String> settlementAddresses;
|
||||
List<Double> deposits;
|
||||
Map<String, Integer> outputs = new HashMap<>();
|
||||
List<Bundle> transfers = new ArrayList<Bundle>();
|
||||
Multisig root;
|
||||
Multisig remainderAddress;
|
||||
private int signersCount = 2;
|
||||
private int balance;
|
||||
private List<String> settlementAddresses;
|
||||
private List<Double> deposits;
|
||||
private Map<String, Integer> outputs = new HashMap<>();
|
||||
private List<Bundle> transfers = new ArrayList<Bundle>();
|
||||
private Multisig root;
|
||||
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 = signersCount;
|
||||
this.balance = balance;
|
||||
this.signersCount = deposits.length;
|
||||
for (double dep : deposits) {
|
||||
this.balance += dep;
|
||||
}
|
||||
this.deposits = new ArrayList<>();
|
||||
for (double deposit : deposits){
|
||||
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.balance = balance;
|
||||
this.settlementAddresses = settlementAddresses;
|
||||
@ -33,6 +37,8 @@ public class FlashObject {
|
||||
this.transfers = transfers;
|
||||
this.root = root;
|
||||
this.remainderAddress = remainderAddress;
|
||||
this.depth = depth;
|
||||
this.security = security;
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap() {
|
||||
@ -42,7 +48,8 @@ public class FlashObject {
|
||||
objectMap.put("root", root.toMap());
|
||||
objectMap.put("remainderAddress", remainderAddress.toMap());
|
||||
objectMap.put("settlementAddresses", getSettlementAddresses());
|
||||
|
||||
objectMap.put("depth", getDepth());
|
||||
objectMap.put("security", getSecurity());
|
||||
// Wrap outputs inside an array.
|
||||
objectMap.put("outputs", getOutputs());
|
||||
|
||||
@ -57,12 +64,20 @@ public class FlashObject {
|
||||
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
}
|
||||
|
||||
public int getSignersCount() {
|
||||
return signersCount;
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
return balance;
|
||||
public int getDepth() {
|
||||
return depth;
|
||||
}
|
||||
|
||||
public int getSecurity() {
|
||||
return security;
|
||||
}
|
||||
|
||||
public Multisig getRoot() {
|
||||
@ -76,7 +91,6 @@ public class FlashObject {
|
||||
public Map<String, Integer> getOutputs() {
|
||||
return outputs;
|
||||
}
|
||||
|
||||
public List<Bundle> getTransfers() {
|
||||
return transfers;
|
||||
}
|
||||
|
||||
@ -7,31 +7,21 @@ import java.util.Map;
|
||||
public class UserObject {
|
||||
private int userIndex;
|
||||
private String seed;
|
||||
private int index = 0;
|
||||
private int seedIndex;
|
||||
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;
|
||||
|
||||
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.seed = seed;
|
||||
this.depth = depth;
|
||||
this.seedIndex = seedIndex;
|
||||
this.security = security;
|
||||
this.flash = flash;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int incrementIndex() {
|
||||
this.index = this.index + 1;
|
||||
return index;
|
||||
}
|
||||
|
||||
public void add(Digest digest) {
|
||||
partialDigests.add(digest);
|
||||
public int incrementSeedIndex() {
|
||||
this.seedIndex = this.seedIndex + 1;
|
||||
return seedIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,21 +29,9 @@ public class UserObject {
|
||||
String out = "";
|
||||
out += "userIndex: " + userIndex + "\n";
|
||||
out += "seed: " + seed + "\n";
|
||||
out += "index: " + index + "\n";
|
||||
out += "seedIndex: " + seedIndex + "\n";
|
||||
out += "security: " + getSecurity() + "\n";
|
||||
out += "depth: " + depth + "\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.toString();
|
||||
|
||||
@ -64,27 +42,8 @@ public class UserObject {
|
||||
Map<String, Object> objectMap = new HashMap<>();
|
||||
objectMap.put("userIndex", getUserIndex());
|
||||
objectMap.put("seed", getSeed());
|
||||
objectMap.put("index", getIndex());
|
||||
objectMap.put("seedIndex", getSeedIndex());
|
||||
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());
|
||||
return objectMap;
|
||||
}
|
||||
@ -94,24 +53,12 @@ public class UserObject {
|
||||
* Getters and Setters
|
||||
*/
|
||||
|
||||
public void setMultisigDigests(ArrayList<Multisig> multisigDigests) {
|
||||
this.multisigDigests = multisigDigests;
|
||||
}
|
||||
|
||||
public void setFlash(FlashObject flash) {
|
||||
this.flash = flash;
|
||||
}
|
||||
|
||||
public void setIndex(int index) {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public void setBundles(ArrayList<Bundle> bundles) {
|
||||
this.bundles = bundles;
|
||||
}
|
||||
|
||||
public ArrayList<Multisig> getMultisigDigests() {
|
||||
return multisigDigests;
|
||||
public void setSeedIndex(int index) {
|
||||
this.seedIndex = index;
|
||||
}
|
||||
|
||||
public int getSecurity() {
|
||||
@ -122,32 +69,17 @@ public class UserObject {
|
||||
return seed;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
public int getSeedIndex() {
|
||||
return seedIndex;
|
||||
}
|
||||
|
||||
public int getUserIndex() {
|
||||
return userIndex;
|
||||
}
|
||||
|
||||
public ArrayList<Bundle> getBundles() {
|
||||
return bundles;
|
||||
}
|
||||
|
||||
public ArrayList<Digest> getPartialDigests() {
|
||||
return partialDigests;
|
||||
}
|
||||
|
||||
public FlashObject getFlash() {
|
||||
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"));
|
||||
Map<String, Integer> outputs = (Map<String, Integer>) inputMap.get("outputs");
|
||||
|
||||
return new FlashObject(singersCount, balance, settlementAddresses, deposits, outputs, transfers, root, remainderAddress);
|
||||
Integer depth = (Integer) inputMap.get("depth");
|
||||
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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user