Skip to content

Commit

Permalink
[#7] Pass both signer account & sender address params from UI. Use se…
Browse files Browse the repository at this point in the history
…nder address for export unsigned transaction.
  • Loading branch information
satran004 committed Feb 28, 2021
1 parent 2757898 commit 837627c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,25 @@ public Result destroyAsset(Account sender, AssetTxnParameters finalAssetTxnPrame
return processAssetTransaction(finalAssetTxnPrameters, requestMode, txn, signTxn);
}

public Result assetTransfer(Account sender, String receiver, AccountAsset asset, BigInteger amount,
public Result assetTransfer(Account signer, Address sender, String receiver, AccountAsset asset, BigInteger amount,
TxnDetailsParameters txnDetailsParameters, RequestMode requestMode) throws Exception {
if(sender == null) {
if(signer == null && sender == null) {
logListener.error("Sender cannot be null");
return Result.error();
}

if(signer == null && !requestMode.equals(RequestMode.EXPORT_UNSIGNED)) {
logListener.error("Signing account cannot be null");
return Result.error();
}

if(StringUtil.isEmpty(receiver)) {
logListener.error("Receiver cannot be null");
return Result.error();
}

try {
logListener.info("From Address : " + sender.getAddress().toString());
logListener.info("From Address : " + sender.toString());
logListener.info("Receiver Address : " + receiver);
logListener.info(String.format("Amount : %s %s ( %d )\n",
AlgoConversionUtil.assetToDecimal(amount, asset.getDecimals()), asset.getAssetUnit(), amount));
Expand All @@ -255,12 +260,12 @@ public Result assetTransfer(Account sender, String receiver, AccountAsset asset,
}

AssetTransferTransactionBuilder builder = Transaction.AssetTransferTransactionBuilder();
builder = (AssetTransferTransactionBuilder) populateBaseTransactionDetails(builder, sender.getAddress(), txnDetailsParameters);
builder = (AssetTransferTransactionBuilder) populateBaseTransactionDetails(builder, sender, txnDetailsParameters);

builder.assetIndex(asset.getAssetId())
.assetAmount(amount)
.assetReceiver(receiver)
.sender(sender.getAddress());
.sender(sender);

if (builder == null) {
logListener.error("Transaction could not be built");
Expand All @@ -274,11 +279,11 @@ public Result assetTransfer(Account sender, String receiver, AccountAsset asset,
return Result.error();
}

SignedTransaction signedTransaction = signTransaction(sender, txn);

if (requestMode == null || requestMode.equals(RequestMode.TRANSACTION)) {
return postApplicationTransaction(sender, signedTransaction);
SignedTransaction signedTransaction = signTransaction(signer, txn);
return postApplicationTransaction(signer, signedTransaction);
} else if (requestMode.equals(RequestMode.EXPORT_SIGNED)) {
SignedTransaction signedTransaction = signTransaction(signer, txn);
return Result.success(JsonUtil.getPrettyJson(signedTransaction));
} else if(requestMode.equals(RequestMode.EXPORT_UNSIGNED)) {
return Result.success(JsonUtil.getPrettyJson(txn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@ public TransactionService(Project project, LogListener logListener) throws Deplo
super(project, logListener);
}

public Result transfer(Account sender, String receiver, Long amount, Address closeReminderTo, TxnDetailsParameters txnDetailsParameters, RequestMode requestMode) throws Exception {
if (sender == null) {
public Result transfer(Account signer, Address sender, String receiver, Long amount, Address closeReminderTo, TxnDetailsParameters txnDetailsParameters, RequestMode requestMode) throws Exception {
if (signer == null && sender == null) {
logListener.error("Sender cannot be null");
return Result.error();
}

if(signer == null && !requestMode.equals(RequestMode.EXPORT_UNSIGNED)) {
logListener.error("Signing account cannot be null");
return Result.error();
}

PaymentTransactionBuilder paymentTransactionBuilder = Transaction.PaymentTransactionBuilder();
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, sender.getAddress(), receiver, amount, closeReminderTo, txnDetailsParameters);
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, sender, receiver, amount, closeReminderTo, txnDetailsParameters);

if (txn == null) {
logListener.error("Transaction could not be built");
return Result.error();
}

SignedTransaction stxn = signTransaction(sender, txn);

if (requestMode == null || requestMode.equals(RequestMode.TRANSACTION)) {
return postApplicationTransaction(sender, stxn);
SignedTransaction stxn = signTransaction(signer, txn);
return postApplicationTransaction(signer, stxn);
} else if (requestMode.equals(RequestMode.EXPORT_SIGNED)) {
SignedTransaction stxn = signTransaction(signer, txn);
return Result.success(JsonUtil.getPrettyJson(stxn));
} else if(requestMode.equals(RequestMode.EXPORT_UNSIGNED)) {
return Result.success(JsonUtil.getPrettyJson(txn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public void actionPerformed(@NotNull AnActionEvent e) {
}

TransferTxnParamEntryForm txnEntryForm = transferDialog.getTransferTxnEntryForm();
Account fromAccount = txnEntryForm.getFromAccount();
Account signerAccount = txnEntryForm.getFromAccount();
Address fromAddress = txnEntryForm.getFromAddress();

Address toAddress = txnEntryForm.getToAccount();
Tuple<BigDecimal, BigInteger> amountTuple = txnEntryForm.getAmount();
if (amountTuple == null) {
Expand All @@ -82,7 +84,6 @@ public void actionPerformed(@NotNull AnActionEvent e) {
AssetTransactionService assetTransactionService = new AssetTransactionService(project, logListener);

RequestMode requestMode = transferDialog.getRequestMode();

Task.Backgroundable task = new Task.Backgroundable(project, getTxnCommand()) {

@Override
Expand All @@ -103,11 +104,11 @@ public void run(@NotNull ProgressIndicator indicator) {

}

if (transferDialog.isAlgoTransfer()) {
result = transactionService.transfer(fromAccount, toAddress.toString(), amountTuple._2().longValue(),
if (transferDialog.isAlgoTransfer()) { //SignerAccount and fromAddress should be same
result = transactionService.transfer(signerAccount, fromAddress, toAddress.toString(), amountTuple._2().longValue(),
closeReminderTo, txnDetailsParameters, requestMode);
} else { //asset transfer
result = assetTransactionService.assetTransfer(fromAccount, toAddress.toString(), asset, amountTuple._2(), txnDetailsParameters, requestMode);
result = assetTransactionService.assetTransfer(signerAccount, fromAddress, toAddress.toString(), asset, amountTuple._2(), txnDetailsParameters, requestMode);
}

processResult(project, module, result, requestMode, logListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,13 @@ public void run() {
try {
assetIdComboBoxModel.removeAllElements();

Account account = getFromAccount();
Address fromAddress = getFromAddress();
if(fromAddress == null) {
console.showErrorMessage("Invalid From Account");
return;
}

List<AccountAsset> accountAssets = algoAccountService.getAccountAssets(account.getAddress().toString());
List<AccountAsset> accountAssets = algoAccountService.getAccountAssets(fromAddress.toString());
if (accountAssets == null || accountAssets.size() == 0)
return;

Expand Down Expand Up @@ -314,6 +318,21 @@ public Account getFromAccount() {
}
}

//Only use for read-only account where mnemonic is empty
public Address getFromAddress() {
Account fromAccount = getFromAccount();
if(fromAccount != null) {
return fromAccount.getAddress();
} else {
String fromAddress = fromAccountTf.getText().trim();
try {
return new Address(fromAddress);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}

public Address getToAccount() {
String acc = toAccountTf.getText().trim();
try {
Expand Down

0 comments on commit 837627c

Please sign in to comment.