Memo encoding decoding

This commit is contained in:
Henry Varona 2016-12-08 22:27:20 -04:00
commit 9aacdc0e15
11 changed files with 283 additions and 74 deletions

View file

@ -6,8 +6,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
/**
* Class used to encapsulate operations related to the account_update_operation.

View file

@ -38,7 +38,7 @@ public abstract class FileBin {
public static String getBrainkeyFromByte(byte[] input, String password) {
try {
byte[] publicKey = new byte[33];
byte[] rawDataEncripted = new byte[input.length-33];
byte[] rawDataEncripted = new byte[input.length - 33];
System.arraycopy(input, 0, publicKey, 0, publicKey.length);
System.arraycopy(input, 33, rawDataEncripted, 0, rawDataEncripted.length);
@ -73,8 +73,8 @@ public abstract class FileBin {
System.arraycopy(encKey, 0, temp, 0, temp.length);
byte[] encBrain = new BigInteger(wallet.get("encrypted_brainkey").getAsString(), 16).toByteArray();
while(encBrain[0] == 0){
byte[]temp2 = new byte[encBrain.length-1];
while (encBrain[0] == 0) {
byte[] temp2 = new byte[encBrain.length - 1];
System.arraycopy(encBrain, 1, temp2, 0, temp2.length);
encBrain = temp2;
}

View file

@ -50,14 +50,14 @@ public class Main {
// test.testGetDynamicParams();
// test.testGetRequiredFeesSerialization();
// test.testRequiredFeesResponse();
// test.testTransactionBroadcastSequence();
test.testTransactionBroadcastSequence();
// test.testAccountLookupDeserialization();
// test.testPrivateKeyManipulations();
// test.testPublicKeyManipulations();
// test.testGetAccountByName();
// test.testGetRequiredFees();
// test.testRandomNumberGeneration();
test.testBrainKeyOperations(false);
//test.testBrainKeyOperations(false);
// test.testBip39Opertion();
// test.testAccountNamebyAddress();
// test.testAccountNameById();
@ -68,5 +68,6 @@ public class Main {
// test.testAccountUpdateOperationBroadcast();
// test.testCreateBinFile();
// test.testImportBinFile();
//test.testLookupAccounts();
}
}

View file

@ -1,57 +1,59 @@
package com.luminiasoft.bitshares;
import com.google.common.primitives.Bytes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.luminiasoft.bitshares.crypto.SecureRandomStrengthener;
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
/**
* Created by nelson on 11/9/16.
*/
public class Memo implements ByteSerializable {
public class Memo implements ByteSerializable, JsonSerializable {
public static final String KEY_FROM = "from";
public static final String KEY_TO = "to";
public static final String KEY_NONCE = "nonce";
public static final String KEY_MESSAGE = "message";
//TODO: Give this class a proper implementation
private byte[] from;
private byte[] to;
private PublicKey from;
private PublicKey to;
private byte[] nonce = new byte[8];
private byte[] message;
@Override
public byte[] toBytes() {
if ((this.from == null) || (this.to == null) || (this.nonce == null) ||(this.message == null)){
return new byte[] { (byte) 0 };
if ((this.from == null) || (this.to == null) || (this.nonce == null) || (this.message == null)) {
return new byte[]{(byte) 0};
}
byte[] result = new byte[this.from.length+this.to.length+this.nonce.length+this.message.length];
System.arraycopy(this.from, 0, result, 0, this.from.length);
System.arraycopy(this.to, 0, result, this.from.length, this.to.length);
System.arraycopy(this.nonce, 0, result, this.from.length+this.to.length, this.nonce.length);
System.arraycopy(this.message, 0, result, this.from.length+this.to.length+this.nonce.length, this.message.length);
return result;
return Bytes.concat(this.from.toBytes(), this.to.toBytes(), this.nonce, this.message);
}
public Memo(){
public Memo() {
this.from = null;
this.nonce = null;
this.to = null;
this.message = null;
}
public void encodeMessage(byte[] private_key, byte[] public_key, byte[] msg){
this.encodeMessage(private_key,public_key,msg,0);
public void encodeMessage(ECKey fromKey, ECKey toKey, byte[] msg) {
this.encodeMessage(fromKey, toKey, msg, 0);
}
public void encodeMessage(byte[] private_key, byte[] public_key, byte[] msg, long custom_nonce){
public void encodeMessage(ECKey fromKey, ECKey toKey, byte[] msg, long custom_nonce) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
ECKey privateECKey = ECKey.fromPrivate(md.digest(private_key));
this.from = privateECKey.getPubKey();
this.to = public_key;
this.from = new PublicKey(fromKey);
this.to = new PublicKey(toKey);
if (custom_nonce == 0){
if (custom_nonce == 0) {
SecureRandomStrengthener randomStrengthener = SecureRandomStrengthener.getInstance();
//randomStrengthener.addEntropySource(new AndroidRandomSource());
SecureRandom secureRandom = randomStrengthener.generateAndSeedRandomNumberGenerator();
@ -59,24 +61,22 @@ public class Memo implements ByteSerializable {
long time = System.currentTimeMillis();
for (int i = 7;i >=1; i--){
this.nonce[i] = (byte)(time&0xff);
time = time/0x100;
for (int i = 7; i >= 1; i--) {
this.nonce[i] = (byte) (time & 0xff);
time = time / 0x100;
}
} else {
for (int i = 7;i >=0; i--){
this.nonce[i] = (byte)(custom_nonce&0xff);
custom_nonce = custom_nonce/0x100;
for (int i = 7; i >= 0; i--) {
this.nonce[i] = (byte) (custom_nonce & 0xff);
custom_nonce = custom_nonce / 0x100;
}
}
byte[] secret = privateECKey.getPubKeyPoint().multiply(ECKey.fromPublicOnly(md.digest(public_key)).getPrivKey()).normalize().getXCoord().getEncoded();
byte[] secret = fromKey.getPubKeyPoint().multiply(toKey.getPrivKey()).normalize().getXCoord().getEncoded();
byte[] finalKey = new byte[secret.length + this.nonce.length];
System.arraycopy(secret, 0, finalKey, 0, secret.length);
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
byte[] sha256Msg = md.digest(msg);
byte[] serialChecksum = new byte[4];
System.arraycopy(sha256Msg, 0, serialChecksum, 0, 4);
@ -85,30 +85,45 @@ public class Memo implements ByteSerializable {
System.arraycopy(msg, 0, msgFinal, serialChecksum.length, msg.length);
this.message = Util.encryptAES(msgFinal, finalKey);
} catch (NoSuchAlgorithmException ex){
} catch (NoSuchAlgorithmException ex) {
}
}
public void decodeMessage(byte[] private_key, byte[] public_key, byte[] msg, byte[] nonce){
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
ECKey privateECKey = ECKey.fromPrivate(md.digest(private_key));
this.to = privateECKey.getPubKey();
this.from = public_key;
this.nonce = nonce;
public void decodeMessage(ECKey toKey, ECKey fromKey, byte[] msg, byte[] nonce) {
this.to = new PublicKey(toKey);
this.from = new PublicKey(fromKey);
this.nonce = nonce;
byte[] secret = privateECKey.getPubKeyPoint().multiply(ECKey.fromPublicOnly(md.digest(public_key)).getPrivKey()).normalize().getXCoord().getEncoded();
byte[] finalKey = new byte[secret.length + this.nonce.length];
System.arraycopy(secret, 0, finalKey, 0, secret.length);
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
byte[] secret = toKey.getPubKeyPoint().multiply(fromKey.getPrivKey()).normalize().getXCoord().getEncoded();
byte[] finalKey = new byte[secret.length + this.nonce.length];
System.arraycopy(secret, 0, finalKey, 0, secret.length);
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
byte[] msgFinal = Util.decryptAES(msg, finalKey);
byte[] decodedMsg = new byte[msgFinal.length-4];
System.arraycopy(msgFinal, 4, decodedMsg, 0, decodedMsg.length);
this.message = decodedMsg;
} catch (NoSuchAlgorithmException ex){
}
byte[] msgFinal = Util.decryptAES(msg, finalKey);
byte[] decodedMsg = new byte[msgFinal.length - 4];
//TODO verify checksum for integrity
System.arraycopy(msgFinal, 4, decodedMsg, 0, decodedMsg.length);
this.message = decodedMsg;
}
@Override
public String toJsonString() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public JsonElement toJsonObject() {
if ((this.from == null) || (this.to == null) || (this.nonce == null) || (this.message == null)) {
return null;
}
JsonObject memoObject = new JsonObject();
memoObject.addProperty(KEY_FROM, new Address(this.from.getKey()).toString());
memoObject.addProperty(KEY_TO, new Address(this.to.getKey()).toString());
//memoObject.addProperty(KEY_NONCE, new BigInteger(1,this.nonce).toString(10));
memoObject.addProperty(KEY_NONCE, new BigInteger(1,this.nonce).toString(10));
memoObject.addProperty(KEY_MESSAGE, new BigInteger(1,this.message).toString(16));
return memoObject;
}
}

View file

@ -15,4 +15,5 @@ public class RPC {
public static final String CALL_GET_ACCOUNTS = "get_accounts";
public static final String CALL_GET_KEY_REFERENCES = "get_key_references";
public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
public static final String CALL_LOOKUP_ACCOUNTS = "lookup_accounts";
}

File diff suppressed because one or more lines are too long

View file

@ -4,6 +4,7 @@ import com.google.common.primitives.Bytes;
import com.google.gson.*;
import java.lang.reflect.Type;
import org.bitcoinj.core.ECKey;
/**
* Class used to encapsulate the TransferOperation operation related functionalities.
@ -15,6 +16,7 @@ public class TransferOperation extends BaseOperation {
public static final String KEY_EXTENSIONS = "extensions";
public static final String KEY_FROM = "from";
public static final String KEY_TO = "to";
public static final String KEY_MEMO = "memo";
private AssetAmount fee;
private AssetAmount amount;
@ -86,6 +88,7 @@ public class TransferOperation extends BaseOperation {
JsonObject jsonObject = new JsonObject();
jsonObject.add(KEY_FEE, fee.toJsonObject());
jsonObject.add(KEY_AMOUNT, amount.toJsonObject());
//jsonObject.add(KEY_MEMO, memo.toJsonObject());
jsonObject.add(KEY_EXTENSIONS, new JsonArray());
jsonObject.addProperty(KEY_FROM, from.toJsonString());
jsonObject.addProperty(KEY_TO, to.toJsonString());
@ -93,6 +96,10 @@ public class TransferOperation extends BaseOperation {
return array;
}
public void setMemo(ECKey fromKey, ECKey toKey, byte[] memo) {
this.memo.encodeMessage(fromKey, toKey, memo);
}
public static class TransferSerializer implements JsonSerializer<TransferOperation> {
@Override

View file

@ -15,6 +15,8 @@ public class TransferTransactionBuilder extends TransactionBuilder {
private UserAccount destinationAccount;
private AssetAmount transferAmount;
private AssetAmount feeAmount;
private String memo;
private ECKey memoPublicKey;
public TransferTransactionBuilder(){}
@ -52,6 +54,12 @@ public class TransferTransactionBuilder extends TransactionBuilder {
return this;
}
public TransferTransactionBuilder setMemo(String memo,ECKey publicKey){
this.memo = memo;
this.memoPublicKey = publicKey;
return this;
}
//TODO: Add support for multiple transfer operations in a single transaction
public TransferTransactionBuilder addOperation(TransferOperation transferOperation){
if(operations == null){
@ -84,6 +92,10 @@ public class TransferTransactionBuilder extends TransactionBuilder {
}else{
transferOperation = new TransferOperation(sourceAccount, destinationAccount, transferAmount, feeAmount);
}
if(memo != null){
transferOperation.setMemo(this.privateKey,this.memoPublicKey,memo.getBytes());
}
operations.add(transferOperation);
}
return new Transaction(privateKey, blockData, operations);

View file

@ -1,6 +1,6 @@
package com.luminiasoft.bitshares;
import com.google.gson.JsonObject;
import com.google.gson.*;
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
/**
* Class tha represents a graphene user account.
@ -17,15 +18,35 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
public static final String PROXY_TO_SELF = "1.2.5";
private String accountName;
/**
* Constructor that expects a user account in the string representation.
* That is in the 1.2.x format.
* @param id: The string representing the account apiId.
* @param id: The string representing the user account.
*/
public UserAccount(String id) {
super(id);
}
/**
* Constructor that expects a user account withe the proper graphene object id and an account name.
* @param id: The string representing the user account.
* @param name: The name of this user account.
*/
public UserAccount(String id, String name){
super(id);
this.accountName = name;
}
/**
* Getter for the account name field.
* @return: The name of this account.
*/
public String getAccountName() {
return accountName;
}
@Override
public byte[] toBytes() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
@ -52,4 +73,24 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
public String toString() {
return this.toJsonString();
}
/**
* Custom deserializer used to deserialize user accounts provided as response from the 'lookup_accounts' api call.
* This response contains serialized user accounts in the form [[{id1},{name1}][{id1},{name1}]].
*
* For instance:
* [["bilthon-1","1.2.139205"],["bilthon-2","1.2.139207"],["bilthon-2016","1.2.139262"]]
*
* So this class will pick up this data and turn it into a UserAccount object.
*/
public static class UserAccountDeserializer implements JsonDeserializer<UserAccount> {
@Override
public UserAccount deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
String name = array.get(0).getAsString();
String id = array.get(1).getAsString();
return new UserAccount(id, name);
}
}
}

View file

@ -1,6 +1,7 @@
package com.luminiasoft.bitshares;
import org.tukaani.xz.FinishableOutputStream;
import com.google.common.primitives.Bytes;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.LZMAInputStream;
import org.tukaani.xz.LZMAOutputStream;

View file

@ -0,0 +1,88 @@
package com.luminiasoft.bitshares.ws;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.luminiasoft.bitshares.RPC;
import com.luminiasoft.bitshares.UserAccount;
import com.luminiasoft.bitshares.interfaces.WitnessResponseListener;
import com.luminiasoft.bitshares.models.ApiCall;
import com.luminiasoft.bitshares.models.BaseResponse;
import com.luminiasoft.bitshares.models.WitnessResponse;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by henry on 07/12/16.
*/
public class LookupAccounts extends WebSocketAdapter {
public static final int DEFAULT_MAX = 1000;
private final String accountName;
private int maxAccounts = DEFAULT_MAX;
private final WitnessResponseListener mListener;
public LookupAccounts(String accountName, WitnessResponseListener listener){
this.accountName = accountName;
this.maxAccounts = DEFAULT_MAX;
this.mListener = listener;
}
public LookupAccounts(String accountName, int maxAccounts, WitnessResponseListener listener){
this.accountName = accountName;
this.maxAccounts = maxAccounts;
this.mListener = listener;
}
@Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
ArrayList<Serializable> accountParams = new ArrayList<>();
accountParams.add(this.accountName);
accountParams.add(this.maxAccounts);
ApiCall getAccountByName = new ApiCall(0, RPC.CALL_LOOKUP_ACCOUNTS, accountParams, RPC.VERSION, 1);
websocket.sendText(getAccountByName.toJsonString());
}
@Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
System.out.println("<<< "+frame.getPayloadText());
String response = frame.getPayloadText();
Type LookupAccountsResponse = new TypeToken<WitnessResponse<List<UserAccount>>>(){}.getType();
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountDeserializer());
WitnessResponse<List<UserAccount>> witnessResponse = builder.create().fromJson(response, LookupAccountsResponse);
if(witnessResponse.error != null){
this.mListener.onError(witnessResponse.error);
}else{
this.mListener.onSuccess(witnessResponse);
}
websocket.disconnect();
}
@Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
if(frame.isTextFrame())
System.out.println(">>> "+frame.getPayloadText());
}
@Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
@Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
mListener.onError(new BaseResponse.Error(cause.getMessage()));
websocket.disconnect();
}
}