forked from AlphaWallet/ERC875-Example-Implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TradeImplementationExample.java
127 lines (108 loc) · 4.22 KB
/
TradeImplementationExample.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
package tapi.utils;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Sign;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sangalli on 12/2/18.
*/
class TransactionDetails
{
BigInteger value;
List<BigInteger> ticketIndices;
Sign.SignatureData signatureData;
}
public class TradeImplementationExample
{
private static final String contractAddress = "fFAB5Ce7C012bc942F5CA0cd42c3C2e1AE5F0005";
public static void main(String[] args)
{
short[] ticketPlaces = new short[]{3, 4};
//zero timestamp means unlimited
byte[] msg = encodeMessageForTrade(BigInteger.ONE, BigInteger.ZERO, ticketPlaces);
List<BigInteger> indices = new ArrayList<>();
for(int i = 0; i < ticketPlaces.length; i++)
{
indices.add(BigInteger.valueOf(ticketPlaces[i]));
}
TransactionDetails td = createTrade(msg, indices, BigInteger.ONE);
System.out.println("Price: 1 ");
System.out.println("expiry: 0 (does not expiry)");
System.out.println("Signature v value: " + td.signatureData.getV());
System.out.println("Signature r value: 0x" + bytesToHex(td.signatureData.getR()));
System.out.println("Signature s value: 0x" + bytesToHex(td.signatureData.getS()));
System.out.println("Ticket indices: 3, 4");
}
public static byte[] encodeMessageForTrade(BigInteger price, BigInteger expiryTimestamp, short[] tickets)
{
byte[] priceInWei = price.toByteArray();
byte[] expiry = expiryTimestamp.toByteArray();
ByteBuffer message = ByteBuffer.allocate(96 + tickets.length * 2);
byte[] leadingZeros = new byte[32 - priceInWei.length];
message.put(leadingZeros);
message.put(priceInWei);
byte[] leadingZerosExpiry = new byte[32 - expiry.length];
message.put(leadingZerosExpiry);
message.put(expiry);
byte[] prefix = "ERC800-CNID1".getBytes();
byte[] contract = hexStringToBytes(contractAddress);
message.put(prefix);
message.put(contract);
ShortBuffer shortBuffer = message.slice().asShortBuffer();
shortBuffer.put(tickets);
return message.array();
}
private static String bytesToHex(byte[] bytes)
{
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ )
{
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
String finalHex = new String(hexChars);
return finalHex;
}
private static byte[] hexStringToBytes(String s)
{
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2)
{
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static TransactionDetails createTrade(byte[] message, List<BigInteger> indices, BigInteger price)
{
try
{
//Note: you can replace with your own key instead of using same key generated from BigInteger.ONE
Sign.SignatureData sigData = Sign.signMessage(message,
ECKeyPair.create(BigInteger.ONE));
TransactionDetails TransactionDetails = new TransactionDetails();
TransactionDetails.ticketIndices = indices;
TransactionDetails.value = price;
byte v = sigData.getV();
String hexR = bytesToHex(sigData.getR());
String hexS = bytesToHex(sigData.getS());
byte[] rBytes = hexStringToBytes(hexR);
byte[] sBytes = hexStringToBytes(hexS);
BigInteger r = new BigInteger(rBytes);
BigInteger s = new BigInteger(sBytes);
TransactionDetails.signatureData = new Sign.SignatureData(v, r.toByteArray(), s.toByteArray());
return TransactionDetails;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}