Memo encoding decoding
This commit is contained in:
commit
9aacdc0e15
11 changed files with 283 additions and 74 deletions
|
@ -6,8 +6,6 @@ import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
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.
|
* Class used to encapsulate operations related to the account_update_operation.
|
||||||
|
|
|
@ -50,14 +50,14 @@ public class Main {
|
||||||
// test.testGetDynamicParams();
|
// test.testGetDynamicParams();
|
||||||
// test.testGetRequiredFeesSerialization();
|
// test.testGetRequiredFeesSerialization();
|
||||||
// test.testRequiredFeesResponse();
|
// test.testRequiredFeesResponse();
|
||||||
// test.testTransactionBroadcastSequence();
|
test.testTransactionBroadcastSequence();
|
||||||
// test.testAccountLookupDeserialization();
|
// test.testAccountLookupDeserialization();
|
||||||
// test.testPrivateKeyManipulations();
|
// test.testPrivateKeyManipulations();
|
||||||
// test.testPublicKeyManipulations();
|
// test.testPublicKeyManipulations();
|
||||||
// test.testGetAccountByName();
|
// test.testGetAccountByName();
|
||||||
// test.testGetRequiredFees();
|
// test.testGetRequiredFees();
|
||||||
// test.testRandomNumberGeneration();
|
// test.testRandomNumberGeneration();
|
||||||
test.testBrainKeyOperations(false);
|
//test.testBrainKeyOperations(false);
|
||||||
// test.testBip39Opertion();
|
// test.testBip39Opertion();
|
||||||
// test.testAccountNamebyAddress();
|
// test.testAccountNamebyAddress();
|
||||||
// test.testAccountNameById();
|
// test.testAccountNameById();
|
||||||
|
@ -68,5 +68,6 @@ public class Main {
|
||||||
// test.testAccountUpdateOperationBroadcast();
|
// test.testAccountUpdateOperationBroadcast();
|
||||||
// test.testCreateBinFile();
|
// test.testCreateBinFile();
|
||||||
// test.testImportBinFile();
|
// test.testImportBinFile();
|
||||||
|
//test.testLookupAccounts();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,30 @@
|
||||||
package com.luminiasoft.bitshares;
|
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.crypto.SecureRandomStrengthener;
|
||||||
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
||||||
|
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
import org.bitcoinj.core.Base58;
|
||||||
import org.bitcoinj.core.ECKey;
|
import org.bitcoinj.core.ECKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by nelson on 11/9/16.
|
* 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
|
//TODO: Give this class a proper implementation
|
||||||
private byte[] from;
|
private PublicKey from;
|
||||||
private byte[] to;
|
private PublicKey to;
|
||||||
private byte[] nonce = new byte[8];
|
private byte[] nonce = new byte[8];
|
||||||
private byte[] message;
|
private byte[] message;
|
||||||
|
|
||||||
|
@ -22,34 +33,25 @@ public class Memo implements ByteSerializable {
|
||||||
if ((this.from == null) || (this.to == null) || (this.nonce == null) || (this.message == null)) {
|
if ((this.from == null) || (this.to == null) || (this.nonce == null) || (this.message == null)) {
|
||||||
return new byte[]{(byte) 0};
|
return new byte[]{(byte) 0};
|
||||||
}
|
}
|
||||||
|
return Bytes.concat(this.from.toBytes(), this.to.toBytes(), this.nonce, this.message);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Memo() {
|
public Memo() {
|
||||||
this.from = null;
|
this.from = null;
|
||||||
this.nonce = null;
|
|
||||||
this.to = null;
|
this.to = null;
|
||||||
this.message = null;
|
this.message = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void encodeMessage(byte[] private_key, byte[] public_key, byte[] msg){
|
public void encodeMessage(ECKey fromKey, ECKey toKey, byte[] msg) {
|
||||||
this.encodeMessage(private_key,public_key,msg,0);
|
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 {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||||
ECKey privateECKey = ECKey.fromPrivate(md.digest(private_key));
|
|
||||||
|
|
||||||
this.from = privateECKey.getPubKey();
|
this.from = new PublicKey(fromKey);
|
||||||
this.to = public_key;
|
this.to = new PublicKey(toKey);
|
||||||
|
|
||||||
if (custom_nonce == 0) {
|
if (custom_nonce == 0) {
|
||||||
SecureRandomStrengthener randomStrengthener = SecureRandomStrengthener.getInstance();
|
SecureRandomStrengthener randomStrengthener = SecureRandomStrengthener.getInstance();
|
||||||
|
@ -70,13 +72,11 @@ public class Memo implements ByteSerializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] secret = fromKey.getPubKeyPoint().multiply(toKey.getPrivKey()).normalize().getXCoord().getEncoded();
|
||||||
byte[] secret = privateECKey.getPubKeyPoint().multiply(ECKey.fromPublicOnly(md.digest(public_key)).getPrivKey()).normalize().getXCoord().getEncoded();
|
|
||||||
byte[] finalKey = new byte[secret.length + this.nonce.length];
|
byte[] finalKey = new byte[secret.length + this.nonce.length];
|
||||||
System.arraycopy(secret, 0, finalKey, 0, secret.length);
|
System.arraycopy(secret, 0, finalKey, 0, secret.length);
|
||||||
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
|
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
|
||||||
|
|
||||||
|
|
||||||
byte[] sha256Msg = md.digest(msg);
|
byte[] sha256Msg = md.digest(msg);
|
||||||
byte[] serialChecksum = new byte[4];
|
byte[] serialChecksum = new byte[4];
|
||||||
System.arraycopy(sha256Msg, 0, serialChecksum, 0, 4);
|
System.arraycopy(sha256Msg, 0, serialChecksum, 0, 4);
|
||||||
|
@ -90,25 +90,40 @@ public class Memo implements ByteSerializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void decodeMessage(byte[] private_key, byte[] public_key, byte[] msg, byte[] nonce){
|
public void decodeMessage(ECKey toKey, ECKey fromKey, byte[] msg, byte[] nonce) {
|
||||||
try {
|
this.to = new PublicKey(toKey);
|
||||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
this.from = new PublicKey(fromKey);
|
||||||
ECKey privateECKey = ECKey.fromPrivate(md.digest(private_key));
|
|
||||||
this.to = privateECKey.getPubKey();
|
|
||||||
this.from = public_key;
|
|
||||||
this.nonce = nonce;
|
this.nonce = nonce;
|
||||||
|
|
||||||
byte[] secret = privateECKey.getPubKeyPoint().multiply(ECKey.fromPublicOnly(md.digest(public_key)).getPrivKey()).normalize().getXCoord().getEncoded();
|
byte[] secret = toKey.getPubKeyPoint().multiply(fromKey.getPrivKey()).normalize().getXCoord().getEncoded();
|
||||||
byte[] finalKey = new byte[secret.length + this.nonce.length];
|
byte[] finalKey = new byte[secret.length + this.nonce.length];
|
||||||
System.arraycopy(secret, 0, finalKey, 0, secret.length);
|
System.arraycopy(secret, 0, finalKey, 0, secret.length);
|
||||||
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
|
System.arraycopy(this.nonce, 0, finalKey, secret.length, this.nonce.length);
|
||||||
|
|
||||||
byte[] msgFinal = Util.decryptAES(msg, finalKey);
|
byte[] msgFinal = Util.decryptAES(msg, finalKey);
|
||||||
byte[] decodedMsg = new byte[msgFinal.length - 4];
|
byte[] decodedMsg = new byte[msgFinal.length - 4];
|
||||||
|
//TODO verify checksum for integrity
|
||||||
System.arraycopy(msgFinal, 4, decodedMsg, 0, decodedMsg.length);
|
System.arraycopy(msgFinal, 4, decodedMsg, 0, decodedMsg.length);
|
||||||
this.message = decodedMsg;
|
this.message = decodedMsg;
|
||||||
} catch (NoSuchAlgorithmException ex){
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,4 +15,5 @@ public class RPC {
|
||||||
public static final String CALL_GET_ACCOUNTS = "get_accounts";
|
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_KEY_REFERENCES = "get_key_references";
|
||||||
public static final String CALL_GET_RELATIVE_ACCOUNT_HISTORY = "get_relative_account_history";
|
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
|
@ -4,6 +4,7 @@ import com.google.common.primitives.Bytes;
|
||||||
import com.google.gson.*;
|
import com.google.gson.*;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import org.bitcoinj.core.ECKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class used to encapsulate the TransferOperation operation related functionalities.
|
* 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_EXTENSIONS = "extensions";
|
||||||
public static final String KEY_FROM = "from";
|
public static final String KEY_FROM = "from";
|
||||||
public static final String KEY_TO = "to";
|
public static final String KEY_TO = "to";
|
||||||
|
public static final String KEY_MEMO = "memo";
|
||||||
|
|
||||||
private AssetAmount fee;
|
private AssetAmount fee;
|
||||||
private AssetAmount amount;
|
private AssetAmount amount;
|
||||||
|
@ -86,6 +88,7 @@ public class TransferOperation extends BaseOperation {
|
||||||
JsonObject jsonObject = new JsonObject();
|
JsonObject jsonObject = new JsonObject();
|
||||||
jsonObject.add(KEY_FEE, fee.toJsonObject());
|
jsonObject.add(KEY_FEE, fee.toJsonObject());
|
||||||
jsonObject.add(KEY_AMOUNT, amount.toJsonObject());
|
jsonObject.add(KEY_AMOUNT, amount.toJsonObject());
|
||||||
|
//jsonObject.add(KEY_MEMO, memo.toJsonObject());
|
||||||
jsonObject.add(KEY_EXTENSIONS, new JsonArray());
|
jsonObject.add(KEY_EXTENSIONS, new JsonArray());
|
||||||
jsonObject.addProperty(KEY_FROM, from.toJsonString());
|
jsonObject.addProperty(KEY_FROM, from.toJsonString());
|
||||||
jsonObject.addProperty(KEY_TO, to.toJsonString());
|
jsonObject.addProperty(KEY_TO, to.toJsonString());
|
||||||
|
@ -93,6 +96,10 @@ public class TransferOperation extends BaseOperation {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMemo(ECKey fromKey, ECKey toKey, byte[] memo) {
|
||||||
|
this.memo.encodeMessage(fromKey, toKey, memo);
|
||||||
|
}
|
||||||
|
|
||||||
public static class TransferSerializer implements JsonSerializer<TransferOperation> {
|
public static class TransferSerializer implements JsonSerializer<TransferOperation> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -15,6 +15,8 @@ public class TransferTransactionBuilder extends TransactionBuilder {
|
||||||
private UserAccount destinationAccount;
|
private UserAccount destinationAccount;
|
||||||
private AssetAmount transferAmount;
|
private AssetAmount transferAmount;
|
||||||
private AssetAmount feeAmount;
|
private AssetAmount feeAmount;
|
||||||
|
private String memo;
|
||||||
|
private ECKey memoPublicKey;
|
||||||
|
|
||||||
public TransferTransactionBuilder(){}
|
public TransferTransactionBuilder(){}
|
||||||
|
|
||||||
|
@ -52,6 +54,12 @@ public class TransferTransactionBuilder extends TransactionBuilder {
|
||||||
return this;
|
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
|
//TODO: Add support for multiple transfer operations in a single transaction
|
||||||
public TransferTransactionBuilder addOperation(TransferOperation transferOperation){
|
public TransferTransactionBuilder addOperation(TransferOperation transferOperation){
|
||||||
if(operations == null){
|
if(operations == null){
|
||||||
|
@ -84,6 +92,10 @@ public class TransferTransactionBuilder extends TransactionBuilder {
|
||||||
}else{
|
}else{
|
||||||
transferOperation = new TransferOperation(sourceAccount, destinationAccount, transferAmount, feeAmount);
|
transferOperation = new TransferOperation(sourceAccount, destinationAccount, transferAmount, feeAmount);
|
||||||
}
|
}
|
||||||
|
if(memo != null){
|
||||||
|
|
||||||
|
transferOperation.setMemo(this.privateKey,this.memoPublicKey,memo.getBytes());
|
||||||
|
}
|
||||||
operations.add(transferOperation);
|
operations.add(transferOperation);
|
||||||
}
|
}
|
||||||
return new Transaction(privateKey, blockData, operations);
|
return new Transaction(privateKey, blockData, operations);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.luminiasoft.bitshares;
|
package com.luminiasoft.bitshares;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.*;
|
||||||
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
import com.luminiasoft.bitshares.interfaces.ByteSerializable;
|
||||||
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
import com.luminiasoft.bitshares.interfaces.JsonSerializable;
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutput;
|
import java.io.DataOutput;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class tha represents a graphene user account.
|
* 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";
|
public static final String PROXY_TO_SELF = "1.2.5";
|
||||||
|
|
||||||
|
private String accountName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor that expects a user account in the string representation.
|
* Constructor that expects a user account in the string representation.
|
||||||
* That is in the 1.2.x format.
|
* 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) {
|
public UserAccount(String id) {
|
||||||
super(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
|
@Override
|
||||||
public byte[] toBytes() {
|
public byte[] toBytes() {
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
@ -52,4 +73,24 @@ public class UserAccount extends GrapheneObject implements ByteSerializable, Jso
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.toJsonString();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.luminiasoft.bitshares;
|
package com.luminiasoft.bitshares;
|
||||||
|
|
||||||
import org.tukaani.xz.FinishableOutputStream;
|
import org.tukaani.xz.FinishableOutputStream;
|
||||||
|
import com.google.common.primitives.Bytes;
|
||||||
import org.tukaani.xz.LZMA2Options;
|
import org.tukaani.xz.LZMA2Options;
|
||||||
import org.tukaani.xz.LZMAInputStream;
|
import org.tukaani.xz.LZMAInputStream;
|
||||||
import org.tukaani.xz.LZMAOutputStream;
|
import org.tukaani.xz.LZMAOutputStream;
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue