2
2
import com .eclipsesource .v8 .utils .V8ObjectUtils ;
3
3
import com .sun .org .apache .xpath .internal .operations .Mult ;
4
4
5
- import javax . jws . Oneway ;
5
+
6
6
import java .io .IOException ;
7
7
import java .nio .charset .Charset ;
8
8
import java .nio .file .Files ;
9
9
import java .nio .file .Paths ;
10
10
import java .util .ArrayList ;
11
- import java .util .HashMap ;
12
11
import java .util .List ;
13
12
import java .util .Map ;
14
13
@@ -111,7 +110,7 @@ public static CreateTransactionHelperObject updateLeafToRoot(MultisigAddress roo
111
110
* @param transfers array of all transfers (value, address) pairs
112
111
* @return
113
112
*/
114
- public static List < Object > prepare (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits , int index , ArrayList <Transfer > transfers ) {
113
+ public static ArrayList < Transaction > prepare (ArrayList <String > settlementAddresses , ArrayList <Integer > deposits , int index , ArrayList <Transfer > transfers ) {
115
114
V8Array settlementAddressesJS = V8ObjectUtils .toV8Array (engine , settlementAddresses );
116
115
V8Array depositJS = V8ObjectUtils .toV8Array (engine , deposits );
117
116
List <Object > transferObj = new ArrayList <Object >();
@@ -131,13 +130,19 @@ public static List<Object> prepare(ArrayList<String> settlementAddresses, ArrayL
131
130
V8Array ret = transfer .executeArrayFunction ("prepare" , V8ObjectUtils .toV8Array (engine , params ));
132
131
List <Object > transfersReturnJS = V8ObjectUtils .toList (ret );
133
132
133
+ ArrayList <Transaction > returnTransfers = new ArrayList <>();
134
+
134
135
for (Object b : transfersReturnJS ) {
135
- Map <String , Object > test = V8ObjectUtils .toMap ((V8Object ) b );
136
+ Map <String , ? super Object > values = (Map <String , ? super Object >) b ;
137
+ String obsoleteTag = (String ) values .get ("obsoleteTag" );
138
+ String address = (String ) values .get ("address" );
139
+ Integer value = (Integer ) values .get ("value" );
136
140
141
+ returnTransfers .add (new Transaction (address , value , "" , "" , 0 ));
137
142
}
138
143
139
144
// Call js.
140
- return transfersReturnJS ;
145
+ return returnTransfers ;
141
146
}
142
147
143
148
/**
@@ -152,19 +157,33 @@ public static List<Object> prepare(ArrayList<String> settlementAddresses, ArrayL
152
157
* @param close
153
158
* @return
154
159
*/
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 ) {
160
+ public static ArrayList <Bundle > compose (int balance ,
161
+ List <Integer > deposits ,
162
+ List <Bundle > outputs ,
163
+ MultisigAddress root ,
164
+ MultisigAddress remainderAddress ,
165
+ List <Bundle > history ,
166
+ List <Transaction > transfers ,
167
+ boolean close ) {
156
168
V8Array depositsJS = V8ObjectUtils .toV8Array (engine , deposits );
157
169
// Outputs
158
170
List <Object > outputsObj = new ArrayList <Object >();
159
- for (Transfer t : outputs ) {
171
+ for (Bundle t : outputs ) {
160
172
outputsObj .add (t .toMap ());
161
173
}
162
174
V8Array outputsJS = V8ObjectUtils .toV8Array (engine , outputsObj );
175
+ V8Object rootJS = V8ObjectUtils .toV8Object (engine , root .toMap ());
176
+ V8Object remainderJS = V8ObjectUtils .toV8Object (engine , remainderAddress .toMap ());
163
177
178
+ List <Object > historyObj = new ArrayList <Object >();
179
+ for (Bundle t : history ) {
180
+ historyObj .add (t .toMap ());
181
+ }
182
+ V8Array historyJS = V8ObjectUtils .toV8Array (engine , historyObj );
164
183
165
184
166
185
List <Object > transfersObj = new ArrayList <Object >();
167
- for (Transfer t : transfers ) {
186
+ for (Transaction t : transfers ) {
168
187
transfersObj .add (t .toMap ());
169
188
}
170
189
V8Array transfersJS = V8ObjectUtils .toV8Array (engine , transfersObj );
@@ -175,16 +194,70 @@ public static List<Object> compose(int balance, ArrayList<Integer> deposits, Arr
175
194
params .add (balance );
176
195
params .add (depositsJS );
177
196
params .add (outputsJS );
178
-
197
+ params .add (rootJS );
198
+ params .add (remainderJS );
199
+ params .add (history );
200
+ params .add (transfersJS );
179
201
180
202
// Call js function.
181
203
V8Array ret = transfer .executeArrayFunction ("compose" , V8ObjectUtils .toV8Array (engine , params ));
182
204
List <Object > transfersReturnJS = V8ObjectUtils .toList (ret );
183
205
184
206
// Parse return as array of bundles
207
+ ArrayList <Bundle > returnBundles = new ArrayList <Bundle >();
208
+ for (Object returnEntry : transfersReturnJS ) {
209
+ ArrayList <Object > b = (ArrayList <Object >) returnEntry ;
210
+
211
+ ArrayList <Transaction > returnedTransactions = new ArrayList <>();
212
+
213
+ for (Object parsedObjects : b ) {
214
+ Map <String , Object > bundleData = (Map <String , Object >) parsedObjects ;
215
+ String signatureMessageFragment = (String ) bundleData .get ("signatureMessageFragment" );
216
+ String bundle = (String ) bundleData .get ("bundle" );
217
+ String address = (String ) bundleData .get ("address" );
218
+ String attachmentTimestampLowerBound = (String ) bundleData .get ("attachmentTimestampLowerBound" );
219
+ String attachmentTimestampUpperBound = (String ) bundleData .get ("attachmentTimestampUpperBound" );
220
+ String trunkTransaction = (String ) bundleData .get ("trunkTransaction" );
221
+ String attachmentTimestamp = (String ) bundleData .get ("attachmentTimestamp" );
222
+ Integer timestamp = (Integer ) bundleData .get ("timestamp" );
223
+ String tag = (String ) bundleData .get ("tag" );
224
+ String branchTransaction = (String ) bundleData .get ("branchTransaction" );
225
+ String nonce = (String ) bundleData .get ("nonce" );
226
+ String obsoleteTag = (String ) bundleData .get ("obsoleteTag" );
227
+
228
+ Integer currentIndex = (Integer ) bundleData .get ("currentIndex" );
229
+ Integer value = (Integer ) bundleData .get ("value" );
230
+ Integer lastIndex = (Integer ) bundleData .get ("lastIndex" );
231
+
232
+ Transaction parsedTransaction = new Transaction (
233
+ address ,
234
+ bundle ,
235
+ value .intValue (),
236
+ obsoleteTag ,
237
+ tag ,
238
+ timestamp ,
239
+ signatureMessageFragment ,
240
+ trunkTransaction ,
241
+ branchTransaction ,
242
+
243
+ attachmentTimestamp ,
244
+ attachmentTimestampUpperBound ,
245
+ attachmentTimestampLowerBound ,
246
+ nonce
247
+ );
248
+
249
+ returnedTransactions .add (parsedTransaction );
250
+
251
+ System .out .println ("Created bundle transaction: " + parsedTransaction .toString ());
252
+ }
253
+
254
+ Bundle bundle = new Bundle (returnedTransactions );
255
+ returnBundles .add (bundle );
256
+ }
185
257
258
+ System .out .println ("Created bundles: " + returnBundles .size ());
186
259
187
- return transfersReturnJS ;
260
+ return returnBundles ;
188
261
}
189
262
190
263
/**
@@ -200,18 +273,29 @@ public static Object sign(MultisigAddress root, String seed, ArrayList<Bundle> b
200
273
201
274
List <Object > bundleTmp = new ArrayList <Object >();
202
275
for (Bundle b : bundles ) {
203
- bundleTmp .add (b .toMap ());
276
+ List <Object > transactions = new ArrayList <Object >();
277
+ for (Transaction t : b .getBundles ()) {
278
+ transactions .add (t .toMap ());
279
+ }
280
+ bundleTmp .add (transactions );
204
281
}
205
282
V8Array bundlesJS = V8ObjectUtils .toV8Array (engine , bundleTmp );
206
283
207
284
// Create params.
208
285
// Now put all params into JS ready array.
209
- List <Object > params = new ArrayList <Object >();
286
+ List <Object > params = new ArrayList <>();
210
287
params .add (rootJS );
211
288
params .add (seed );
212
- params .add (bundles );
289
+ params .add (bundlesJS );
213
290
214
- V8Object signatures = transfer .executeObjectFunction ("sign" , V8ObjectUtils .toV8Array (engine , params ));
291
+ V8Array signatures = transfer .executeArrayFunction ("sign" , V8ObjectUtils .toV8Array (engine , params ));
292
+
293
+ for (Object o : V8ObjectUtils .toList (signatures )) {
294
+ Map <String , Object > returnValues = (Map <String , Object >) o ;
295
+ for (Map .Entry <String , Object > entry : returnValues .entrySet ()) {
296
+ System .out .println (entry .getKey () + " : " + entry .getValue ());
297
+ }
298
+ }
215
299
216
300
// TODO: add singature object.
217
301
return signatures ;
0 commit comments