- Reverted the Memo#nonce attribute to the original long type

- Properly fixed the deserialization problem and added a specific test case at MemoTest#shouldDeserializeFromString
develop
Nelson R. Perez 2017-10-17 23:19:55 -05:00
parent b305faa662
commit 69ccbe8cd9
4 changed files with 42 additions and 25 deletions

View File

@ -1,7 +1,6 @@
package cy.agorise.graphenej.objects;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedLong;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@ -37,7 +36,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
private Address from;
private Address to;
private UnsignedLong nonce;
private long nonce;
private byte[] message;
private String plaintextMessage;
@ -68,7 +67,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
* @param nonce: Nonce used in the encryption.
* @param message: Message in ciphertext.
*/
public Memo(Address from, Address to, UnsignedLong nonce, byte[] message){
public Memo(Address from, Address to, long nonce, byte[] message){
this.from = from;
this.to = to;
this.nonce = nonce;
@ -91,7 +90,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
return this.to;
}
public UnsignedLong getNonce(){
public long getNonce(){
return this.nonce;
}
@ -237,7 +236,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
new byte[]{(byte) this.message.length},
this.message);
} else {
byte[] nonceBytes = Util.revertUnsignedLong(nonce);
byte[] nonceBytes = Util.revertLong(nonce);
ECPoint senderPoint = ECKey.compressPoint(from.getPublicKey().getKey().getPubKeyPoint());
PublicKey senderPublicKey = new PublicKey(ECKey.fromPublicOnly(senderPoint));
@ -273,7 +272,7 @@ public class Memo implements ByteSerializable, JsonSerializable {
}else{
memoObject.addProperty(KEY_FROM, this.from.toString());
memoObject.addProperty(KEY_TO, this.to.toString());
memoObject.addProperty(KEY_NONCE, String.format("%d", this.nonce));
memoObject.addProperty(KEY_NONCE, String.format("%x", this.nonce));
memoObject.addProperty(KEY_MESSAGE, Util.bytesToHex(this.message));
}
return memoObject;
@ -289,9 +288,8 @@ public class Memo implements ByteSerializable, JsonSerializable {
JsonObject jsonObject = json.getAsJsonObject();
String fromAddress = jsonObject.get(KEY_FROM).getAsString();
String toAddress = jsonObject.get(KEY_TO).getAsString();
UnsignedLong nonce = UnsignedLong.valueOf(jsonObject.get(KEY_NONCE).getAsString());
long nonce = Long.parseLong(jsonObject.get(KEY_NONCE).getAsString(), 16);
String msg = jsonObject.get(KEY_MESSAGE).getAsString();
Memo memo = null;
try{
Address from = new Address(fromAddress);

View File

@ -131,10 +131,11 @@ public class TransferOperation extends BaseOperation {
@Override
public JsonElement serialize(TransferOperation transfer, Type type, JsonSerializationContext jsonSerializationContext) {
JsonArray arrayRep = new JsonArray();
arrayRep.add(transfer.getId());
arrayRep.add(transfer.toJsonObject());
return arrayRep;
// JsonArray arrayRep = new JsonArray();
// arrayRep.add(transfer.getId());
// arrayRep.add(transfer.toJsonObject());
// return arrayRep;
return transfer.toJsonObject();
}
}

View File

@ -144,7 +144,7 @@ public class TransactionTest {
// Creating memo
long nonce = 1;
byte[] encryptedMessage = Memo.encryptMessage(sourcePrivateKey, to1, nonce, "another message");
Memo memo = new Memo(new Address(ECKey.fromPublicOnly(sourcePrivateKey.getPubKey())), new Address(to1.getKey()), UnsignedLong.valueOf(nonce), encryptedMessage);
Memo memo = new Memo(new Address(ECKey.fromPublicOnly(sourcePrivateKey.getPubKey())), new Address(to1.getKey()), nonce, encryptedMessage);
// Creating operation 1
TransferOperation transferOperation1 = new TransferOperationBuilder()

View File

@ -1,11 +1,12 @@
package cy.agorise.graphenej.objects;
import com.google.common.primitives.UnsignedLong;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.core.ECKey;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -31,7 +32,7 @@ public class MemoTest {
private long nonce;
private String sourceWIF = System.getenv("SOURCE_WIF");
private String destinationWIF = System.getenv("DESTINATION_WIF");
private byte[] memoEncryptedEnvMessage = Util.hexToBytes(System.getenv("MEMO_MESSAGE"));
private byte[] memoEncryptedEnvMessage;
//private String sourceWIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM";
//private String destinationWIF = "5HuGQT8qwHScBgD4XsGbQUmXQF18MrbzxaQDiGGXFNRrCtqgT5Q";
private String shortMessage = "test";
@ -44,15 +45,21 @@ public class MemoTest {
@Before
public void setUp() throws Exception {
//Source
sourcePrivate = DumpedPrivateKey.fromBase58(null, sourceWIF).getKey();
PublicKey publicKey = new PublicKey(ECKey.fromPublicOnly(sourcePrivate.getPubKey()));
sourceAddress = new Address(publicKey.getKey());
if(System.getenv("MEMO_MESSAGE") != null){
memoEncryptedEnvMessage = Util.hexToBytes(System.getenv("MEMO_MESSAGE"));
}
//Destination
destinationPrivate = DumpedPrivateKey.fromBase58(null, destinationWIF).getKey();
publicKey = new PublicKey(ECKey.fromPublicOnly(destinationPrivate.getPubKey()));
destinationAddress = new Address(publicKey.getKey());
if(sourceWIF != null && destinationWIF != null){
//Source
sourcePrivate = DumpedPrivateKey.fromBase58(null, sourceWIF).getKey();
PublicKey publicKey = new PublicKey(ECKey.fromPublicOnly(sourcePrivate.getPubKey()));
sourceAddress = new Address(publicKey.getKey());
//Destination
destinationPrivate = DumpedPrivateKey.fromBase58(null, destinationWIF).getKey();
publicKey = new PublicKey(ECKey.fromPublicOnly(destinationPrivate.getPubKey()));
destinationAddress = new Address(publicKey.getKey());
}
//memo.getNonce()
nonce = 5;
@ -134,7 +141,7 @@ public class MemoTest {
@Test
public void shouldBeJsonObjectSerializable(){
byte[] encrypted = Memo.encryptMessage(sourcePrivate, destinationAddress, 1, shortMessage);
Memo memo = new Memo(sourceAddress, destinationAddress, UnsignedLong.ONE, encrypted);
Memo memo = new Memo(sourceAddress, destinationAddress, 1, encrypted);
JsonElement jsonObject = memo.toJsonObject();
JsonObject reference = new JsonObject();
reference.addProperty("from", "BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
@ -148,8 +155,19 @@ public class MemoTest {
public void shouldBeByteSerializable(){
String byteReference = "0103d1fb8c7421db64d46fba7e36f428854ca06eff65698b293f37c7ffaa54e2c2b203aece7c31616c02fcc96b50d3397c0e8d33d6384655d477c300d9196c728a5ee20100000000000000104c81c2db6ebc61e3f9e0ead65c0559dd";
byte[] encrypted = Memo.encryptMessage(sourcePrivate, destinationAddress, 1, shortMessage);
Memo memo = new Memo(sourceAddress, destinationAddress, UnsignedLong.ONE, encrypted);
Memo memo = new Memo(sourceAddress, destinationAddress, 1, encrypted);
byte[] memoBytes = memo.toBytes();
assertEquals("Memo instance should generate a valid byte array", byteReference, Util.bytesToHex(memoBytes));
}
@Test
public void shouldDeserializeFromString(){
String jsonMemo = "{\"from\": \"BTS6nB7gw1EawYXRofLvuivLsboVmh2inXroQgSQqYfAc5Bamk4Vq\",\"to\": \"BTS4xAQGg2ePLeDGZvQFpsh9CjMhQvRnVkPp6jPoE6neVPotRfZX9\",\"nonce\": \"15f2d8ee4ec23\",\"message\": \"b9aeb7632f1f4281eedcf28a684828a42d02de71254fb88e13ddcb9a79adf51d9770c58d7e7efcdbb1515f1136c3be3e\"}";
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Memo.class, new Memo.MemoDeserializer());
Memo memo = gsonBuilder.create().fromJson(jsonMemo, Memo.class);
Assert.assertEquals("Source address should match the serialized one", "BTS6nB7gw1EawYXRofLvuivLsboVmh2inXroQgSQqYfAc5Bamk4Vq", memo.getSource().toString());
Assert.assertEquals("Destination address should match the serialized one", "BTS4xAQGg2ePLeDGZvQFpsh9CjMhQvRnVkPp6jPoE6neVPotRfZX9", memo.getDestination().toString());
Assert.assertEquals("Nonce should match serialized one", Long.parseLong("15f2d8ee4ec23", 16), memo.getNonce());
Assert.assertArrayEquals(Util.hexToBytes("b9aeb7632f1f4281eedcf28a684828a42d02de71254fb88e13ddcb9a79adf51d9770c58d7e7efcdbb1515f1136c3be3e"), memo.getByteMessage());
}
}