Merge origin/master

master
Henry Varona 2016-11-27 19:40:18 -04:00
commit f539e8951d
4 changed files with 84 additions and 42 deletions

View File

@ -72,9 +72,9 @@ public class Main {
// test.testBip39Opertion();
// test.testAccountNamebyAddress();
test.testAccountNamebyAddress();
test.testAccountNameById();
// test.testAccountNameById();
// test.testRelativeAccountHistory();
}

View File

@ -25,6 +25,7 @@ import java.lang.reflect.Type;
import java.net.URL;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
@ -179,7 +180,7 @@ public class Test {
// return new String(Base64.encode(sigData), Charset.forName("UTF-8"));
}
public byte[] testTransactionSerialization(long head_block_number, String head_block_id, long relative_expiration) {
public void testTransactionSerialization(long head_block_number, String head_block_id, long relative_expiration) {
BlockData blockData = new BlockData(head_block_number, head_block_id, relative_expiration);
ArrayList<BaseOperation> operations = new ArrayList<BaseOperation>();
@ -192,7 +193,6 @@ public class Test {
byte[] serializedTransaction = this.transaction.toBytes();
System.out.println("Serialized transaction");
System.out.println(Util.bytesToHex(serializedTransaction));
return serializedTransaction;
}
public void testWebSocketTransfer() throws IOException {
@ -340,12 +340,15 @@ public class Test {
.setDestination(new UserAccount("1.2.129848"))
.setAmount(new AssetAmount(UnsignedLong.valueOf(100), new Asset("1.3.120")))
.setFee(new AssetAmount(UnsignedLong.valueOf(264174), new Asset("1.3.0")))
.setBlockData(new BlockData(43408, 1430521623, 1479231969))
.setBlockData(new BlockData(Main.REF_BLOCK_NUM, Main.REF_BLOCK_PREFIX, Main.RELATIVE_EXPIRATION))
.setPrivateKey(DumpedPrivateKey.fromBase58(null, Main.WIF).getKey())
.build();
ArrayList<Serializable> transactionList = new ArrayList<>();
transactionList.add(transaction);
byte[] signature = transaction.getGrapheneSignature();
System.out.println(Util.bytesToHex(signature));
ApiCall call = new ApiCall(4, "call", "broadcast_transaction", transactionList, "2.0", 1);
String jsonCall = call.toJsonString();
System.out.println("json call");
@ -402,7 +405,7 @@ public class Test {
}
public void testTransactionBroadcastSequence() {
String url = "ws://api.devling.xyz:8088";
String url = Test.OPENLEDGER_WITNESS_URL;
WitnessResponseListener listener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
@ -428,20 +431,29 @@ public class Test {
ArrayList<Serializable> transactionList = new ArrayList<>();
transactionList.add(transaction);
ApiCall call = new ApiCall(4, "call", "broadcast_transaction", transactionList, "2.0", 1);
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000);
try {
WebSocket mWebSocket = factory.createSocket(url);
mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener));
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
}
transactionList.add(transaction);
SSLContext context = null;
context = NaiveSSLContext.getInstance("TLS");
WebSocketFactory factory = new WebSocketFactory();
// Set the custom SSL context.
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener));
mWebSocket.connect();
} catch (MalformedTransactionException e) {
System.out.println("MalformedTransactionException. Msg: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());
} catch (WebSocketException e) {
System.out.println("WebSocketException. Msg: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgoritmException. Msg: "+e.getMessage());
}
}
@ -614,6 +626,18 @@ public class Test {
}
public void testAccountNamebyAddress() {
WitnessResponseListener listener = new WitnessResponseListener() {
@Override
public void onSuccess(WitnessResponse response) {
System.out.println("onSuccess");
}
@Override
public void onError(BaseResponse.Error error) {
System.out.println("onError");
}
};
BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0);
Address address = new Address(brainKey.getPrivateKey());
try {
@ -626,7 +650,7 @@ public class Test {
factory.setSSLContext(context);
WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL);
mWebSocket.addListener(new GetAccountsByAddress(address, mListener));
mWebSocket.addListener(new GetAccountsByAddress(address, listener));
mWebSocket.connect();
} catch (IOException e) {
System.out.println("IOException. Msg: " + e.getMessage());

View File

@ -73,24 +73,24 @@ public class Transaction implements ByteSerializable, JsonSerializable {
public List<BaseOperation> getOperations(){ return this.operations; }
/**
* Obtains a signature of this transaction.
* Obtains a signature of this transaction. Please note that due to the current reliance on
* bitcoinj to generate the signatures, and due to the fact that it uses deterministic
* ecdsa signatures, we are slightly modifying the expiration time of the transaction while
* we look for a signature that will be accepted by the graphene network.
*
* This should then be called before any other serialization method.
* @return: A valid signature of the current transaction.
*/
public byte[] getSignature(){
byte[] serializedTransaction = this.toBytes();
Sha256Hash hash = Sha256Hash.wrap(Sha256Hash.hash(serializedTransaction));
boolean isCanonical = false;
int recId = -1;
ECKey.ECDSASignature sig = null;
while(!isCanonical) {
sig = privateKey.sign(hash);
if(!sig.isCanonical()){
// Signature was not canonical, retrying
continue;
}else{
// Signature is canonical
isCanonical = true;
}
public byte[] getGrapheneSignature(){
boolean isGrapheneCanonical = false;
byte[] sigData = null;
while(!isGrapheneCanonical) {
byte[] serializedTransaction = this.toBytes();
Sha256Hash hash = Sha256Hash.wrap(Sha256Hash.hash(serializedTransaction));
int recId = -1;
ECKey.ECDSASignature sig = privateKey.sign(hash);
// Now we have to work backwards to figure out the recId needed to recover the signature.
for (int i = 0; i < 4; i++) {
ECKey k = ECKey.recoverFromSignature(i, sig, hash, privateKey.isCompressed());
@ -99,15 +99,24 @@ public class Transaction implements ByteSerializable, JsonSerializable {
break;
}
}
sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S
int headerByte = recId + 27 + (privateKey.isCompressed() ? 4 : 0);
sigData[0] = (byte) headerByte;
System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
// Further "canonicality" tests
if(((sigData[0] & 0x80) != 0) || (sigData[0] == 0) ||
((sigData[1] & 0x80) != 0) || ((sigData[32] & 0x80) != 0) ||
(sigData[32] == 0) || ((sigData[33] & 0x80) != 0)){
this.blockData.setRelativeExpiration(this.blockData.getRelativeExpiration() + 1);
}else{
isGrapheneCanonical = true;
}
}
int headerByte = recId + 27 + (privateKey.isCompressed() ? 4 : 0);
byte[] sigData = new byte[65]; // 1 header + 32 bytes for R + 32 bytes for S
sigData[0] = (byte)headerByte;
System.arraycopy(Utils.bigIntegerToBytes(sig.r, 32), 0, sigData, 1, 32);
System.arraycopy(Utils.bigIntegerToBytes(sig.s, 32), 0, sigData, 33, 32);
return sigData;
}
/**
* Method that creates a serialized byte array with compact information about this transaction
* that is needed for the creation of a signature.
@ -156,6 +165,10 @@ public class Transaction implements ByteSerializable, JsonSerializable {
public JsonObject toJsonObject() {
JsonObject obj = new JsonObject();
// Getting the signature before anything else,
// since this might change the transaction expiration data slightly
byte[] signature = getGrapheneSignature();
// Formatting expiration time
Date expirationTime = new Date(blockData.getRelativeExpiration() * 1000);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@ -166,7 +179,7 @@ public class Transaction implements ByteSerializable, JsonSerializable {
// Adding signatures
JsonArray signatureArray = new JsonArray();
signatureArray.add(Util.bytesToHex(getSignature()));
signatureArray.add(Util.bytesToHex(signature));
obj.add(KEY_SIGNATURES, signatureArray);
JsonArray operationsArray = new JsonArray();

View File

@ -64,7 +64,6 @@ public class GetAccountsByAddress extends WebSocketAdapter {
Type GetAccountByAddressResponse = new TypeToken<WitnessResponse<List<List<String>>>>(){}.getType();
WitnessResponse<WitnessResponse<List<List<String>>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse);
if (witnessResponse.error != null) {
this.mListener.onError(witnessResponse.error);
} else {
@ -73,6 +72,12 @@ public class GetAccountsByAddress extends WebSocketAdapter {
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()));