From 7eb6a15c7e47370bbaed66be46a15227eda677c3 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Sun, 4 Dec 2016 23:25:42 -0500 Subject: [PATCH] Successfully changing owner and account keys information --- .../bitshares/AccountUpdateOperation.java | 1 + .../luminiasoft/bitshares/BaseOperation.java | 2 + .../java/com/luminiasoft/bitshares/Main.java | 8 ++- .../java/com/luminiasoft/bitshares/Test.java | 23 +++---- .../luminiasoft/bitshares/Transaction.java | 7 ++- .../bitshares/TransferOperation.java | 1 + .../ws/TransactionBroadcastSequence.java | 60 ++++++++++--------- 7 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/luminiasoft/bitshares/AccountUpdateOperation.java b/src/main/java/com/luminiasoft/bitshares/AccountUpdateOperation.java index 0eee65f..8e7adb3 100644 --- a/src/main/java/com/luminiasoft/bitshares/AccountUpdateOperation.java +++ b/src/main/java/com/luminiasoft/bitshares/AccountUpdateOperation.java @@ -36,6 +36,7 @@ public class AccountUpdateOperation extends BaseOperation { this(account, owner, active, new AssetAmount(UnsignedLong.valueOf(0), new Asset("1.3.0"))); } + @Override public void setFee(AssetAmount fee){ this.fee = fee; } diff --git a/src/main/java/com/luminiasoft/bitshares/BaseOperation.java b/src/main/java/com/luminiasoft/bitshares/BaseOperation.java index 414a88a..89616f2 100644 --- a/src/main/java/com/luminiasoft/bitshares/BaseOperation.java +++ b/src/main/java/com/luminiasoft/bitshares/BaseOperation.java @@ -18,5 +18,7 @@ public abstract class BaseOperation implements ByteSerializable, JsonSerializabl return (byte) this.type.ordinal(); } + public abstract void setFee(AssetAmount assetAmount); + public abstract byte[] toBytes(); } diff --git a/src/main/java/com/luminiasoft/bitshares/Main.java b/src/main/java/com/luminiasoft/bitshares/Main.java index 8696230..2fb7685 100644 --- a/src/main/java/com/luminiasoft/bitshares/Main.java +++ b/src/main/java/com/luminiasoft/bitshares/Main.java @@ -9,6 +9,8 @@ public class Main { // Brain key from Nelson's app referencing the bilthon-83 account public static final String BRAIN_KEY = "PUMPER ISOTOME SERE STAINER CLINGER MOONLIT CHAETA UPBRIM AEDILIC BERTHER NIT SHAP SAID SHADING JUNCOUS CHOUGH"; + public static final String BILTHON_5_BRAIN_KEY = "UNMATE AURIGAL NAVET WAVICLE REWOVE ABBOTCY COWHERB OUTKICK STOPPER JUSSORY BEAMLET WIRY"; + //public static final String BRAIN_KEY = "TWIXT SERMO TRILLI AUDIO PARDED PLUMET BIWA REHUNG MAUDLE VALVULA OUTBURN FEWNESS ALIENER UNTRACE PRICH TROKER"; //public static final String BRAIN_KEY = "SIVER TIKKER FOGO HOMINAL PRAYER LUTEIN SMALLY ACARID MEROPIA TRANCE BOGONG IDDAT HICKORY SOUTANE MOOD DOWSER"; public static final String BIP39_KEY = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; @@ -17,7 +19,7 @@ public class Main { // public static final String WIF = "5J96pne45qWM1WpektoeazN6k9Mt93jQ7LyueRxFfEMTiy6yxjM"; // Brain key from an empty account created by the cli_wallet // public static final String BRAIN_KEY = "TWIXT SERMO TRILLI AUDIO PARDED PLUMET BIWA REHUNG MAUDLE VALVULA OUTBURN FEWNESS ALIENER UNTRACE PRICH TROKER"; - // WIF from an emty account created by the cli_wallet + // WIF from an empty account created by the cli_wallet public static final String WIF = "5KMzB2GqGhnh7ufhgddmz1eKPHS72uTLeL9hHjSvPb1UywWknF5"; public static final String EXTERNAL_SIGNATURE = "1f36c41acb774fcbc9c231b5895ec9701d6872729098d8ea56d78dda72a6b54252694db85d7591de5751b7aea06871da15d63a1028758421607ffc143e53ef3306"; @@ -61,7 +63,7 @@ public class Main { // test.testingInvoiceGeneration(); // test.testCompression(); // test.testCreateBinFile(); - test.testAccountUpdateSerialization(); -// test.testAccountUpdateOperationSerialization(); +// test.testAccountUpdateSerialization(); + test.testAccountUpdateOperationBroadcast(); } } diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index f19efb8..afedc93 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -184,7 +184,8 @@ public class Test { AssetAmount amount = new AssetAmount(UnsignedLong.valueOf(100), new Asset("1.3.120")); AssetAmount fee = new AssetAmount(UnsignedLong.valueOf(264174), new Asset("1.3.0")); operations.add(new TransferOperation(from, to, amount, fee)); - this.transaction = new Transaction(Main.WIF, blockData, operations); + BrainKey brainKey = new BrainKey(Main.BRAIN_KEY, 0); + this.transaction = new Transaction(brainKey.getWalletImportFormat(), blockData, operations); byte[] serializedTransaction = this.transaction.toBytes(); System.out.println("Serialized transaction"); System.out.println(Util.bytesToHex(serializedTransaction)); @@ -426,7 +427,7 @@ public class Test { .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)) - .setPrivateKey(DumpedPrivateKey.fromBase58(null, Main.WIF).getKey()) + .setPrivateKey(DumpedPrivateKey.fromBase58(null, Main.BILTHON_5_BRAIN_KEY).getKey()) .build(); ArrayList transactionList = new ArrayList<>(); @@ -441,7 +442,7 @@ public class Test { WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); - mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener)); + mWebSocket.addListener(new TransactionBroadcastSequence(transaction, new Asset("1.3.0"), listener)); mWebSocket.connect(); } catch (MalformedTransactionException e) { @@ -591,7 +592,7 @@ public class Test { String suggestion = BrainKey.suggest(words); brainKey = new BrainKey(suggestion, 0); } else { - brainKey = new BrainKey(Main.BRAIN_KEY, 0); + brainKey = new BrainKey(Main.BILTHON_5_BRAIN_KEY, 0); } ECKey key = brainKey.getPrivateKey(); System.out.println("Private key"); @@ -755,8 +756,6 @@ public class Test { ArrayList operations = new ArrayList(); operations.add(operation); Transaction transaction = new Transaction(Main.WIF, blockData, operations); - System.out.println("Json format of transaction"); - System.out.println(transaction.toJsonString()); } catch(MalformedAddressException e){ System.out.println("MalformedAddressException. Msg: "+e.getMessage()); } @@ -776,7 +775,7 @@ public class Test { } }; - UserAccount account = new UserAccount("1.2.138632"); + UserAccount account = new UserAccount("1.2.139313"); AssetAmount fee = new AssetAmount(UnsignedLong.valueOf("200"), new Asset("1.3.0")); HashMap keyAuths = new HashMap<>(); keyAuths.put("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY", 1); @@ -786,12 +785,8 @@ public class Test { AccountUpdateOperation operation = new AccountUpdateOperation(account, owner, active, fee); ArrayList operations = new ArrayList(); operations.add(operation); - Transaction transaction = new Transaction(Main.WIF, null, operations); - - ArrayList transactionList = new ArrayList<>(); - transactionList.add(transaction); - - ApiCall call = new ApiCall(4, "call", "broadcast_transaction", transactionList, RPC.VERSION, 1); + BrainKey brainKey = new BrainKey(Main.BILTHON_5_BRAIN_KEY, 0); + Transaction transaction = new Transaction(brainKey.getWalletImportFormat(), null, operations); SSLContext context = null; context = NaiveSSLContext.getInstance("TLS"); @@ -802,7 +797,7 @@ public class Test { WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); - mWebSocket.addListener(new TransactionBroadcastSequence(transaction, listener)); + mWebSocket.addListener(new TransactionBroadcastSequence(transaction, new Asset("1.3.0"), listener)); mWebSocket.connect(); } catch (MalformedAddressException e) { System.out.println("MalformedAddressException. Msg: "+e.getMessage()); diff --git a/src/main/java/com/luminiasoft/bitshares/Transaction.java b/src/main/java/com/luminiasoft/bitshares/Transaction.java index b80d4f2..967d5a4 100644 --- a/src/main/java/com/luminiasoft/bitshares/Transaction.java +++ b/src/main/java/com/luminiasoft/bitshares/Transaction.java @@ -69,6 +69,11 @@ public class Transaction implements ByteSerializable, JsonSerializable { this.blockData = blockData; } + public void setFees(List fees){ + for(int i = 0; i < operations.size(); i++) + operations.get(i).setFee(fees.get(i)); + } + public ECKey getPrivateKey(){ return this.privateKey; } @@ -90,8 +95,6 @@ public class Transaction implements ByteSerializable, JsonSerializable { while(!isGrapheneCanonical) { byte[] serializedTransaction = this.toBytes(); - System.out.println("Signing serialized transaction"); - System.out.println(Util.bytesToHex(serializedTransaction)); Sha256Hash hash = Sha256Hash.wrap(Sha256Hash.hash(serializedTransaction)); int recId = -1; ECKey.ECDSASignature sig = privateKey.sign(hash); diff --git a/src/main/java/com/luminiasoft/bitshares/TransferOperation.java b/src/main/java/com/luminiasoft/bitshares/TransferOperation.java index 87692a2..4f12f70 100644 --- a/src/main/java/com/luminiasoft/bitshares/TransferOperation.java +++ b/src/main/java/com/luminiasoft/bitshares/TransferOperation.java @@ -40,6 +40,7 @@ public class TransferOperation extends BaseOperation { this.memo = new Memo(); } + @Override public void setFee(AssetAmount newFee){ this.fee = newFee; } diff --git a/src/main/java/com/luminiasoft/bitshares/ws/TransactionBroadcastSequence.java b/src/main/java/com/luminiasoft/bitshares/ws/TransactionBroadcastSequence.java index 51be6ba..71202ba 100644 --- a/src/main/java/com/luminiasoft/bitshares/ws/TransactionBroadcastSequence.java +++ b/src/main/java/com/luminiasoft/bitshares/ws/TransactionBroadcastSequence.java @@ -1,10 +1,9 @@ package com.luminiasoft.bitshares.ws; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; -import com.luminiasoft.bitshares.BlockData; -import com.luminiasoft.bitshares.RPC; -import com.luminiasoft.bitshares.Transaction; +import com.luminiasoft.bitshares.*; import com.luminiasoft.bitshares.interfaces.WitnessResponseListener; import com.luminiasoft.bitshares.models.ApiCall; import com.luminiasoft.bitshares.models.BaseResponse; @@ -18,11 +17,7 @@ import com.neovisionaries.ws.client.WebSocketFrame; import java.io.Serializable; import java.lang.reflect.Type; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; +import java.util.*; /** * Class that will handle the transaction publication procedure. @@ -33,8 +28,12 @@ public class TransactionBroadcastSequence extends WebSocketAdapter { private final static int LOGIN_ID = 1; private final static int GET_NETWORK_BROADCAST_ID = 2; private final static int GET_NETWORK_DYNAMIC_PARAMETERS = 3; - private final static int BROADCAST_TRANSACTION = 4; + private final static int GET_REQUIRED_FEES = 4; + private final static int BROADCAST_TRANSACTION = 5; +// private Transaction transaction; +// private ArrayList transactions; + private Asset feeAsset; private Transaction transaction; private WitnessResponseListener mListener; @@ -48,8 +47,9 @@ public class TransactionBroadcastSequence extends WebSocketAdapter { * be implemented by the party interested in being notified about the success/failure * of the transaction broadcast operation. */ - public TransactionBroadcastSequence(Transaction transaction, WitnessResponseListener listener){ + public TransactionBroadcastSequence(Transaction transaction, Asset feeAsset, WitnessResponseListener listener){ this.transaction = transaction; + this.feeAsset = feeAsset; this.mListener = listener; } @@ -94,30 +94,36 @@ public class TransactionBroadcastSequence extends WebSocketAdapter { dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); Date date = dateFormat.parse(dynamicProperties.time); - // Obtained block data + // Adjusting dynamic block data to every transaction long expirationTime = (date.getTime() / 1000) + Transaction.DEFAULT_EXPIRATION_TIME; String headBlockId = dynamicProperties.head_block_id; long headBlockNumber = dynamicProperties.head_block_number; transaction.setBlockData(new BlockData(headBlockNumber, headBlockId, expirationTime)); - ArrayList transactionList = new ArrayList<>(); - transactionList.add(transaction); - ApiCall call = new ApiCall(broadcastApiId, - RPC.CALL_BROADCAST_TRANSACTION, - transactionList, - RPC.VERSION, - currentId); - - System.out.println("Json of transaction"); - System.out.println(transaction.toJsonString()); - - //TODO: Remove this debug code - String jsonCall = call.toJsonString(); - System.out.println("json call"); - System.out.println(jsonCall); + ArrayList accountParams = new ArrayList<>(); + accountParams.add((Serializable) transaction.getOperations()); + accountParams.add(this.feeAsset.getObjectId()); + ApiCall getRequiredFees = new ApiCall(0, RPC.CALL_GET_REQUIRED_FEES, accountParams, RPC.VERSION, currentId); // Finally sending transaction -// websocket.sendText(call.toJsonString()); + websocket.sendText(getRequiredFees.toJsonString()); + }else if(baseResponse.id == GET_REQUIRED_FEES){ + Type GetRequiredFeesResponse = new TypeToken>>(){}.getType(); + GsonBuilder gsonBuilder = new GsonBuilder(); + gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetDeserializer()); + WitnessResponse> requiredFeesResponse = gsonBuilder.create().fromJson(response, GetRequiredFeesResponse); + + transaction.setFees(requiredFeesResponse.result); + ArrayList transactions = new ArrayList<>(); + transactions.add(transaction); + + ApiCall call = new ApiCall(broadcastApiId, + RPC.CALL_BROADCAST_TRANSACTION, + transactions, + RPC.VERSION, + currentId); + websocket.sendText(call.toJsonString()); +// websocket.disconnect(); }else if(baseResponse.id >= BROADCAST_TRANSACTION){ Type WitnessResponseType = new TypeToken>(){}.getType(); WitnessResponse> witnessResponse = gson.fromJson(response, WitnessResponseType);