/
Construction.java
339 lines (254 loc) · 11.9 KB
/
Construction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package io.swagger.controllers;
import io.swagger.oas.inflector.models.RequestContext;
import io.swagger.oas.inflector.models.ResponseContext;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import snowblossom.lib.ValidationException;
import java.io.File;
import java.util.List;
import io.swagger.model.*;
import io.swagger.model.ConstructionCombineRequest;
import io.swagger.model.ConstructionCombineResponse;
import java.util.LinkedList;
import io.swagger.model.ConstructionDeriveRequest;
import io.swagger.model.ConstructionDeriveResponse;
import io.swagger.model.ConstructionHashRequest;
import io.swagger.model.ConstructionMetadataRequest;
import io.swagger.model.ConstructionMetadataResponse;
import io.swagger.model.ConstructionParseRequest;
import io.swagger.model.ConstructionParseResponse;
import io.swagger.model.ConstructionPayloadsRequest;
import io.swagger.model.ConstructionPayloadsResponse;
import io.swagger.model.ConstructionPreprocessRequest;
import io.swagger.model.ConstructionPreprocessResponse;
import io.swagger.model.ConstructionSubmitRequest;
import io.swagger.model.Error;
import io.swagger.model.TransactionIdentifierResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.snowblossom.rosesnow.RoseSnow;
import org.snowblossom.rosesnow.RoseUtil;
import snowblossom.lib.ChainHash;
import snowblossom.lib.Globals;
import snowblossom.node.SnowBlossomNode;
import snowblossom.proto.BlockHeader;
import snowblossom.proto.TransactionInner;
import snowblossom.proto.TransactionInput;
import snowblossom.proto.TransactionOutput;
import snowblossom.lib.NetworkParams;
import snowblossom.lib.HexUtil;
import snowblossom.lib.AddressUtil;
import snowblossom.lib.AddressSpecHash;
import snowblossom.lib.SignatureUtil;
import com.google.protobuf.ByteString;
import java.util.HashSet;
import com.google.common.collect.ImmutableList;
import snowblossom.lib.DigestUtil;
import snowblossom.proto.AddressSpec;
import snowblossom.lib.TransactionUtil;
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaInflectorServerCodegen", date = "2020-10-18T05:48:04.106Z[GMT]")
public class Construction {
public ResponseContext constructionCombine(RequestContext request , JsonNode body)
throws Exception
{
ConstructionCombineRequest req = new ObjectMapper().readValue(body.toString(), ConstructionCombineRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
snowblossom.proto.Transaction.Builder s_tx = snowblossom.proto.Transaction.newBuilder().mergeFrom(
snowblossom.proto.Transaction.parseFrom( HexUtil.hexStringToBytes( req.getUnsignedTransaction() ) ));
TransactionInner inner = TransactionUtil.getInner(s_tx.build());
for(Signature sig : req.getSignatures())
{
ByteString sig_bytes = RoseUtil.checkSignature(sig, s_tx.getTxHash());
AddressSpec spec = RoseUtil.getSpecForPublicKey(sig.getPublicKey());
int claim_idx = 0;
for(AddressSpec claim_spec : inner.getClaimsList())
{
if (claim_spec.equals(spec)) break;
claim_idx++;
}
s_tx.addSignatures( snowblossom.proto.SignatureEntry.newBuilder()
.setClaimIdx(claim_idx)
.setKeyIdx(0)
.setSignature( sig_bytes )
.build()
);
}
ConstructionCombineResponse resp = new ConstructionCombineResponse();
resp.setSignedTransaction( HexUtil.getHexString( s_tx.build().toByteString() ) );
return new ResponseContext().entity( resp );
}
public ResponseContext constructionDerive(RequestContext request , JsonNode body)
throws Exception
{
ConstructionDeriveRequest req = new ObjectMapper().readValue(body.toString(), ConstructionDeriveRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
PublicKey pk = req.getPublicKey();
AddressSpecHash hash = RoseUtil.getSpecHashForPublicKey(pk);
ConstructionDeriveResponse resp = new ConstructionDeriveResponse();
resp.setAccountIdentifier( new AccountIdentifier().address(hash.toAddressString(params)) );
return new ResponseContext().entity(resp);
}
public ResponseContext constructionHash(RequestContext request , JsonNode body)
throws Exception
{
ConstructionHashRequest req = new ObjectMapper().readValue(body.toString(), ConstructionHashRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
String tx_hex = req.getSignedTransaction();
snowblossom.proto.Transaction s_tx = snowblossom.proto.Transaction.parseFrom( HexUtil.hexStringToBytes(tx_hex) );
TransactionIdentifierResponse resp = new TransactionIdentifierResponse();
resp.setTransactionIdentifier( new TransactionIdentifier().hash(HexUtil.getHexString( s_tx.getTxHash() ) ));
return new ResponseContext().entity(resp);
}
public ResponseContext constructionMetadata(RequestContext request , JsonNode body)
throws Exception
{
ConstructionMetadataRequest req = new ObjectMapper().readValue(body.toString(), ConstructionMetadataRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
ConstructionMetadataResponse resp = new ConstructionMetadataResponse();
long suggested_fee = Math.round(Globals.BASIC_FEE * 500);
resp.setSuggestedFee( ImmutableList.of( RoseUtil.getSnowAmount(suggested_fee, params)));
resp.setMetadata(body);
return new ResponseContext().entity(resp);
}
public ResponseContext constructionParse(RequestContext request , JsonNode body)
throws Exception
{
ConstructionParseRequest req = new ObjectMapper().readValue(body.toString(), ConstructionParseRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
String tx_hex = req.getTransaction();
snowblossom.proto.Transaction s_tx = snowblossom.proto.Transaction.parseFrom( HexUtil.hexStringToBytes(tx_hex) );
NetworkIdentifier id = req.getNetworkIdentifier();
// TODO - this operation should not be online
SnowBlossomNode node = RoseSnow.getNode(id);
Transaction tx = RoseUtil.protoToModel(s_tx, node);
ConstructionParseResponse resp = new ConstructionParseResponse();
for(Operation op : tx.getOperations())
{
op.setStatus(null);
resp.getOperations().add(op);
}
TransactionInner inner = TransactionUtil.getInner(s_tx);
LinkedList<AccountIdentifier> signers = new LinkedList<>();
for(snowblossom.proto.SignatureEntry sig : s_tx.getSignaturesList())
{
int claim_idx = sig.getClaimIdx();
AddressSpec spec = inner.getClaims(claim_idx);
AddressSpecHash hash = AddressUtil.getHashForSpec(spec);
signers.add( new AccountIdentifier().address(hash.toAddressString(params)));
}
resp.setAccountIdentifierSigners(signers);
return new ResponseContext().entity(resp);
}
public ResponseContext constructionPayloads(RequestContext request, JsonNode body)
throws Exception
{
ConstructionPayloadsRequest req = new ObjectMapper().readValue(body.toString(), ConstructionPayloadsRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
TransactionInner.Builder inner = TransactionInner.newBuilder();
inner.setVersion(1);
long input_value=0;
long output_value=0;
HashSet<AddressSpecHash> needed_claims = new HashSet<>();
for(Operation op : req.getOperations())
{
if (op.getType().equals("INPUT"))
{
AddressSpecHash hs = new AddressSpecHash( op.getAccount().getAddress(), params );
long v = Long.parseLong(op.getAmount().getValue());
v=-v; // We think in positive terms for this
input_value += v;
String coin_id = op.getCoinChange().getCoinIdentifier().getIdentifier();
String src_tx_str = coin_id.split(":")[0];
int idx = Integer.parseInt(coin_id.split(":")[1]);
needed_claims.add(hs);
TransactionInput in = TransactionInput.newBuilder()
.setSpecHash(hs.getBytes())
.setSrcTxId( HexUtil.hexStringToBytes( src_tx_str ) )
.setSrcTxOutIdx( idx )
.build();
inner.addInputs(in);
}
else if (op.getType().equals("OUTPUT"))
{
AddressSpecHash hs = new AddressSpecHash( op.getAccount().getAddress(), params );
long v = Long.parseLong(op.getAmount().getValue());
output_value += v;
TransactionOutput out = TransactionOutput.newBuilder()
.setValue(v)
.setRecipientSpecHash(hs.getBytes())
.build();
inner.addOutputs(out);
}
else
{
throw new ValidationException("Unexpected op type: " + op.getType());
}
}
inner.setFee(input_value - output_value);
HashSet<PublicKey> needed_signers = new HashSet<>();
for(PublicKey pk : req.getPublicKeys())
{
AddressSpec spec = RoseUtil.getSpecForPublicKey(pk);
AddressSpecHash hash = RoseUtil.getSpecHashForPublicKey(pk);
if (needed_claims.contains(hash))
{
inner.addClaims(spec);
needed_claims.remove(hash);
needed_signers.add(pk);
}
}
snowblossom.proto.Transaction.Builder tx = snowblossom.proto.Transaction.newBuilder();
ByteString inner_data= inner.build().toByteString();
tx.setInnerData(inner_data);
tx.setTxHash(ByteString.copyFrom(DigestUtil.getMD().digest(inner_data.toByteArray())));
ConstructionPayloadsResponse resp = new ConstructionPayloadsResponse();
resp.setUnsignedTransaction( HexUtil.getHexString(tx.build().toByteString()));
for(PublicKey pk : needed_signers)
{
AddressSpecHash hash = RoseUtil.getSpecHashForPublicKey(pk);
SigningPayload load = new SigningPayload();
ByteString hash_data = RoseUtil.hashSha1( tx.getTxHash() );
//ByteString hash_data = tx.getTxHash();
System.out.println("LORK payload: " + HexUtil.getHexString( hash_data ) );
load.setHexBytes( HexUtil.getHexString( hash_data ));
load.setAccountIdentifier( new AccountIdentifier().address( hash.toAddressString(params) ) );
load.setSignatureType( SignatureType.ECDSA );
resp.getPayloads().add(load);
}
return new ResponseContext().entity(resp);
}
public ResponseContext constructionPreprocess(RequestContext request , JsonNode body)
throws Exception
{
ConstructionPreprocessRequest req = new ObjectMapper().readValue(body.toString(), ConstructionPreprocessRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
ConstructionPreprocessResponse resp = new ConstructionPreprocessResponse();
HashSet<AccountIdentifier> needed_keys = new HashSet<>();
for(Operation op : req.getOperations())
{
if (op.getType().equals("INPUT"))
{
needed_keys.add(op.getAccount());
}
}
resp.setRequiredPublicKeys(ImmutableList.copyOf(needed_keys));
return new ResponseContext().entity(resp);
}
public ResponseContext constructionSubmit(RequestContext request , JsonNode body)
throws Exception
{
ConstructionSubmitRequest req = new ObjectMapper().readValue(body.toString(), ConstructionSubmitRequest.class);
NetworkParams params = RoseUtil.getParams(req.getNetworkIdentifier());
String tx_hex = req.getSignedTransaction();
snowblossom.proto.Transaction s_tx = snowblossom.proto.Transaction.parseFrom( HexUtil.hexStringToBytes(tx_hex) );
snowblossom.proto.SubmitReply reply = RoseSnow.getClient( req.getNetworkIdentifier() ).getBlockingStub().submitTransaction(s_tx);
if (reply.getSuccess() != true)
{
throw new ValidationException(reply.toString());
}
TransactionIdentifierResponse resp = new TransactionIdentifierResponse();
resp.setTransactionIdentifier( new TransactionIdentifier().hash(HexUtil.getHexString( s_tx.getTxHash() ) ));
return new ResponseContext().entity(resp);
}
}