Skip to content

Commit

Permalink
Merge pull request #199 from ScaleMote/update/add-trustline-check
Browse files Browse the repository at this point in the history
Add trustline verification
  • Loading branch information
ivanbelasich authored Feb 28, 2024
2 parents 4fe8e0d + fffa75c commit 062f076
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cypress/fixtures/payment.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"destinationAccount": "GCECUYXE32PPYKPVYOSILACOCUGMEZSDO7GYERC2GKAG2HM34BLMMOMB",
"destinationAccount": "GCAGLYWX5SWMKOE537BN3PNCCENL2QAG5H35P55GH5BPQW7QGYIRV6NZ",
"amountToSend": 100,
"assetCode": "native",
"issuer": "none"
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/ITranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface ITranslation {
NETWORK_FEE: string;
NETWORK: string;
NOT_AUTHORIZED_TO_TRANSACT: string;
NO_TRUSTLINE: string;
OFFER_ID: string;
OPERATION_ACCOUNT_MERGE: string;
OPERATION_ACCOUNT_TRUST: string;
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/languages/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"NETWORK_FEE": "Network Fee:",
"NETWORK": "Network",
"NOT_AUTHORIZED_TO_TRANSACT": "Authorization: The account is not authorized to transact with the asset",
"NO_TRUSTLINE": "The recipient hasn't established a trustline with the asset.",
"OFFER_ID": "Offer ID:",
"OPERATION_ACCOUNT_MERGE": "Account Merge",
"OPERATION_ACCOUNT_TRUST": "Trust",
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/languages/spanish.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"NETWORK_FEE": "Comisión de la red:",
"NETWORK": "Red",
"NOT_AUTHORIZED_TO_TRANSACT": "Autorización: La cuenta no está autorizada para realizar transacciones con el activo",
"NO_TRUSTLINE": "El destinatario no estableció una línea de confianza con el activo.",
"OFFER_ID": "ID de la Oferta:",
"OPERATION_ACCOUNT_MERGE": "Combinar cuentas",
"OPERATION_ACCOUNT_TRUST": "Establecer línea de confianza",
Expand Down
18 changes: 18 additions & 0 deletions src/lib/stellar/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ import { Asset, BASE_FEE, Operation, TransactionBuilder } from 'stellar-sdk';
import { CURRENT_NETWORK_PASSPHRASE } from './StellarNetwork';
import { server } from './utils';

export async function checkTrustline(receiver: string, assetCode: string, issuer: string) {
try {
const account = await server.loadAccount(receiver);

for (const balance of account.balances) {
if (
(assetCode === 'native' && balance.asset_type === 'native') ||
('asset_code' in balance && balance.asset_code === assetCode && balance.asset_issuer === issuer)
) {
return true;
}
}
return false;
} catch (error) {
throw new Error(JSON.stringify(error));
}
}

export async function createPaymentTransaction(
publicKey: string,
receiver: string,
Expand Down
22 changes: 18 additions & 4 deletions src/routes/payment/Payment.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Bridge, { SimpleSignerPageType } from '../../lib/bridge/Bridge';
import { setMinimumPopUpSize } from '../../lib/components/helpers/popUpSizeHelper';
import type { WalletConnectService } from '../../lib/service/walletConnect';
import { createPaymentTransaction } from '../../lib/stellar/Payment';
import { checkTrustline, createPaymentTransaction } from '../../lib/stellar/Payment';
import { CURRENT_STELLAR_NETWORK } from '../../lib/stellar/StellarNetwork';
import { server } from '../../lib/stellar/utils';
import LocalStorage from '../../lib/storage/storage';
Expand Down Expand Up @@ -39,14 +39,24 @@
if (urlParams) {
({ receiver, amount, assetCode, issuer } = urlParams);
checkTrustlineAndSetMessage();
} else {
bridge.addPaymentMessageHandler((message) => {
({ receiver, amount, assetCode, issuer } = message);
checkTrustlineAndSetMessage();
});
}
let isPaymentInProgress = false;
let paymentResultMessage = '';
let trustlineMessage = '';
async function checkTrustlineAndSetMessage() {
const hasTrustline = await checkTrustline(receiver, assetCode, issuer);
if (!hasTrustline) {
trustlineMessage = $language.NO_TRUSTLINE;
}
}
async function handlePayment() {
if (isPaymentInProgress) return;
Expand Down Expand Up @@ -102,10 +112,14 @@
</div>
{:else}
<div class="simple-signer tx-data-container">
{#if !receiver || !amount || !assetCode || !issuer}
{#if !receiver || !amount || !assetCode || !issuer || trustlineMessage === $language.NO_TRUSTLINE}
<h1 class="simple-signer error-title">{$language.ERROR}</h1>
<div class="simple-signer information-container">
<p class="simple-signer">{$language.ERROR_MISSING_RECEIVER_DATA}</p>
<p class="simple-signer">
{trustlineMessage === $language.NO_TRUSTLINE
? trustlineMessage
: $language.ERROR_MISSING_RECEIVER_DATA}
</p>
<button class="simple-signer accept-button" on:click={handlePopupClose}>{$language.CLOSE}</button>
</div>
{:else}
Expand All @@ -119,7 +133,7 @@
<div class="simple-signer receiver">
{$language.YOU_ARE_PAYING}
<strong>{amount}</strong>
<strong>{assetCode === 'native' ? 'XLM' : { assetCode }}</strong>
<strong>{assetCode === 'native' ? 'XLM' : assetCode}</strong>
{$language.TO_THE_ACCOUNT}
<br />
<strong>{receiver}.</strong>
Expand Down

0 comments on commit 062f076

Please sign in to comment.