12
12
import java .util .List ;
13
13
import java .util .Map ;
14
14
15
- public class IotaFlashBridge implements IotaFlashInterface {
16
- private String iotaLibPath ;
17
- private V8 engine ;
18
- private V8Object transfer ;
19
- private V8Object multisig ;
15
+ public class IotaFlashBridge {
16
+ private static String iotaLibPath = "res/iota.flash.js" ;
17
+ private static V8 engine ;
18
+ private static V8Object transfer ;
19
+ private static V8Object multisig ;
20
20
21
- /**
22
- *
23
- * @param path
24
- * @throws IOException
25
- */
26
- IotaFlashBridge (String path ) throws IOException {
27
- this .iotaLibPath = path ;
28
-
29
- String file = readFile (path , Charset .defaultCharset ());
21
+ public static void boot () throws IOException {
22
+ String file = readFile (iotaLibPath , Charset .defaultCharset ());
30
23
31
- this . engine = V8 .createV8Runtime ();
24
+ engine = V8 .createV8Runtime ();
32
25
// Eval lib into current v8 context.
33
26
engine .executeVoidScript (file );
34
27
multisig = (V8Object ) engine .executeScript ("iotaFlash.multisig" );
35
28
transfer = (V8Object ) engine .executeScript ("iotaFlash.transfer" );
36
-
37
29
}
38
30
39
31
/**
40
32
*
41
33
* @param digests
42
34
* @return
43
35
*/
44
- public MultisigAddress composeAddress (ArrayList <Digest > digests ) {
36
+ public static MultisigAddress composeAddress (ArrayList <Digest > digests ) {
45
37
// Create js object for digest
46
- // TODO: find more clean way to do thi s.
47
38
List <Object > list = new ArrayList <Object >();
48
39
for (Digest digest : digests ) {
49
40
list .add (digest .toMap ());
@@ -73,7 +64,7 @@ public MultisigAddress composeAddress(ArrayList<Digest> digests) {
73
64
* @param security
74
65
* @return
75
66
*/
76
- public Digest getDigest (String seed , int index , int security ) {
67
+ public static Digest getDigest (String seed , int index , int security ) {
77
68
if (seed .length () < 81 ) {
78
69
System .out .println ("Seed is too short" );
79
70
return null ;
@@ -94,14 +85,21 @@ public Digest getDigest(String seed, int index, int security) {
94
85
*
95
86
* @param root
96
87
*/
97
- public void updateLeafToRoot (MultisigAddress root ) {
88
+ public static CreateTransactionHelperObject updateLeafToRoot (MultisigAddress root ) {
98
89
Map <String , Object > map = root .toMap ();
99
90
// Create param list
100
91
List <Object > paramsObj = new ArrayList <Object >();
101
92
paramsObj .add (map );
102
93
V8Array params = V8ObjectUtils .toV8Array (engine , paramsObj );
103
94
104
- multisig .executeFunction ("updateLeafToRoot" , params );
95
+ V8Object ret = multisig .executeObjectFunction ("updateLeafToRoot" , params );
96
+ int generate = ret .getInteger ("generate" );
97
+ Map <String , ? super Object > multiSigMap = V8ObjectUtils .toMap ((V8Object ) ret .getObject ("multisig" ));
98
+ // Parse result into Java Obj.
99
+ String addr = (String ) multiSigMap .get ("address" );
100
+ int secSum = (Integer ) multiSigMap .get ("securitySum" );
101
+ MultisigAddress multisig = new MultisigAddress (addr , secSum );
102
+ return new CreateTransactionHelperObject (generate , multisig );
105
103
}
106
104
107
105
@@ -113,7 +111,7 @@ public void updateLeafToRoot(MultisigAddress root) {
113
111
* @param transfers array of all transfers (value, address) pairs
114
112
* @return
115
113
*/
116
- public Object prepare (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits , int index , ArrayList <Transfer > transfers ) {
114
+ public static List < Object > prepare (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits , int index , ArrayList <Transfer > transfers ) {
117
115
V8Array settlementAddressesJS = V8ObjectUtils .toV8Array (engine , settlementAddresses );
118
116
V8Array depositJS = V8ObjectUtils .toV8Array (engine , deposits );
119
117
List <Object > transferObj = new ArrayList <Object >();
@@ -133,6 +131,11 @@ public Object prepare(ArrayList<String> settlementAddresses, ArrayList<Integer>
133
131
V8Array ret = transfer .executeArrayFunction ("prepare" , V8ObjectUtils .toV8Array (engine , params ));
134
132
List <Object > transfersReturnJS = V8ObjectUtils .toList (ret );
135
133
134
+ for (Object b : transfersReturnJS ) {
135
+ Map <String , Object > test = V8ObjectUtils .toMap ((V8Object ) b );
136
+
137
+ }
138
+
136
139
// Call js.
137
140
return transfersReturnJS ;
138
141
}
@@ -142,14 +145,14 @@ public Object prepare(ArrayList<String> settlementAddresses, ArrayList<Integer>
142
145
* @param balance
143
146
* @param deposits
144
147
* @param outputs
145
- * @param multisig
148
+ * @param root
146
149
* @param remainderAddress
147
150
* @param history
148
151
* @param transfers
149
152
* @param close
150
153
* @return
151
154
*/
152
- public List <Object > compose (int balance , ArrayList <Integer > deposits , ArrayList <Transfer > outputs , Object multisig , String remainderAddress , ArrayList <Bundle > history , ArrayList <Transfer > transfers , boolean close ) {
155
+ 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 ) {
153
156
V8Array depositsJS = V8ObjectUtils .toV8Array (engine , deposits );
154
157
// Outputs
155
158
List <Object > outputsObj = new ArrayList <Object >();
@@ -158,26 +161,60 @@ public List<Object> compose(int balance, ArrayList<Integer> deposits, ArrayList<
158
161
}
159
162
V8Array outputsJS = V8ObjectUtils .toV8Array (engine , outputsObj );
160
163
164
+
165
+
161
166
List <Object > transfersObj = new ArrayList <Object >();
162
167
for (Transfer t : transfers ) {
163
168
transfersObj .add (t .toMap ());
164
169
}
165
170
V8Array transfersJS = V8ObjectUtils .toV8Array (engine , transfersObj );
166
171
167
- List <Object > trs = V8ObjectUtils .toList (transfersJS );
172
+ // Create params.
173
+ // Now put all params into JS ready array.
174
+ List <Object > params = new ArrayList <Object >();
175
+ params .add (balance );
176
+ params .add (depositsJS );
177
+ params .add (outputsJS );
178
+
179
+
180
+ // Call js function.
181
+ V8Array ret = transfer .executeArrayFunction ("compose" , V8ObjectUtils .toV8Array (engine , params ));
182
+ List <Object > transfersReturnJS = V8ObjectUtils .toList (ret );
183
+
184
+ // Parse return as array of bundles
185
+
168
186
169
- return trs ;
187
+ return transfersReturnJS ;
170
188
}
171
189
172
190
/**
173
191
*
174
- * @param multisig
192
+ * @param root
175
193
* @param seed
176
194
* @param bundles
177
195
* @return
178
196
*/
179
- public Object sign (Object multisig , String seed , ArrayList <Object > bundles ) {
180
- return null ;
197
+ public static Object sign (MultisigAddress root , String seed , ArrayList <Bundle > bundles ) {
198
+ Map <String , Object > multisig = root .toMap ();
199
+ V8Object rootJS = V8ObjectUtils .toV8Object (engine , multisig );
200
+
201
+ List <Object > bundleTmp = new ArrayList <Object >();
202
+ for (Bundle b : bundles ) {
203
+ bundleTmp .add (b .toMap ());
204
+ }
205
+ V8Array bundlesJS = V8ObjectUtils .toV8Array (engine , bundleTmp );
206
+
207
+ // Create params.
208
+ // Now put all params into JS ready array.
209
+ List <Object > params = new ArrayList <Object >();
210
+ params .add (rootJS );
211
+ params .add (seed );
212
+ params .add (bundles );
213
+
214
+ V8Object signatures = transfer .executeObjectFunction ("sign" , V8ObjectUtils .toV8Array (engine , params ));
215
+
216
+ // TODO: add singature object.
217
+ return signatures ;
181
218
}
182
219
183
220
/**
@@ -186,7 +223,7 @@ public Object sign(Object multisig, String seed, ArrayList<Object> bundles) {
186
223
* @param signatures
187
224
* @return
188
225
*/
189
- public Object appliedSignatures (ArrayList <Object > bundles , ArrayList <Object > signatures ) {
226
+ public static Object appliedSignatures (ArrayList <Object > bundles , ArrayList <Object > signatures ) {
190
227
return null ;
191
228
}
192
229
@@ -198,7 +235,7 @@ public Object appliedSignatures(ArrayList<Object> bundles, ArrayList<Object> sig
198
235
* @param bundles
199
236
* @return
200
237
*/
201
- public Object getDiff (ArrayList <Object > root , ArrayList <Object > remainder , ArrayList <Object > history , ArrayList <Object > bundles ) {
238
+ public static Object getDiff (ArrayList <Object > root , ArrayList <Object > remainder , ArrayList <Object > history , ArrayList <Object > bundles ) {
202
239
return null ;
203
240
}
204
241
@@ -212,7 +249,7 @@ public Object getDiff(ArrayList<Object> root, ArrayList<Object> remainder, Array
212
249
* @param signedBundles
213
250
* @return
214
251
*/
215
- public Object applayTransfers (Object root , Object deposit , Object outputs , Object remainderAddress , Object transfers , Object signedBundles ) {
252
+ public static Object applayTransfers (Object root , Object deposit , Object outputs , Object remainderAddress , Object transfers , Object signedBundles ) {
216
253
return null ;
217
254
}
218
255
@@ -222,7 +259,7 @@ public Object applayTransfers(Object root, Object deposit, Object outputs, Objec
222
259
* @param deposits
223
260
* @return
224
261
*/
225
- public Object close (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits ) {
262
+ public static Object close (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits ) {
226
263
V8Array saJS = V8ObjectUtils .toV8Array (engine , settlementAddresses );
227
264
// Deposits
228
265
V8Array depositsJS = V8ObjectUtils .toV8Array (engine , deposits );
0 commit comments