Skip to content

Commit

Permalink
Add trustline verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanbelasich committed Feb 23, 2024
1 parent 8eb6a5f commit 7a900d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
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) {
const account = await server.loadAccount(receiver);

for (const balance of account.balances) {
if (assetCode === 'native') {
if ('asset_type' in balance && balance.asset_type === 'native') {
return true;
}
} else if ('asset_code' in balance && 'asset_issuer' in balance) {
if (balance.asset_code === assetCode && balance.asset_issuer === issuer) {
return true;
}
}
}

return false;
}

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 7a900d3

Please sign in to comment.