Skip to content

Commit

Permalink
refactor: transaction sign permission count amount spend instead of a…
Browse files Browse the repository at this point in the history
…mount left
  • Loading branch information
peronczyk committed Oct 23, 2023
1 parent 1d929cd commit 53cae92
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 43 deletions.
31 changes: 13 additions & 18 deletions src/composables/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { METHODS } from '@aeternity/aepp-sdk';

import type {
IAppData,
IPermission,
Expand All @@ -22,9 +23,10 @@ import { useStorageRef } from './storageRef';
import { IModalProps, useModals } from './modals';

interface ITransactionSignPermissionOptions {
amount?: number;
fee?: number;
nameFee?: number;
amount?: number | string;
fee?: number | string;
nameFee?: number | string;
[x: string]: any;
}

const permissions = useStorageRef<PermissionRegistry>(
Expand Down Expand Up @@ -61,17 +63,10 @@ const modalAndPopupTypes: Partial<Record<METHODS, { modal: string, popup: PopupT
export function usePermissions() {
const { openModal } = useModals();

function setTransactionSignLimitLeft(host: string, value: number) {
function resetTransactionSignSpent(host: string) {
permissions.value[host] = {
...permissions.value[host],
transactionSignLimitLeft: value,
};
}

function resetTransactionSignLimitLeft(host: string) {
permissions.value[host] = {
...permissions.value[host],
transactionSignLimitLeft: permissions.value[host].transactionSignLimit,
transactionSignSpent: 0,
transactionSignFirstAskedOn: (new Date()).toISOString(),
};
}
Expand All @@ -98,19 +93,19 @@ export function usePermissions() {
} = permissions.value[host] || {};

if (!transactionSignLimit) {
return false; // Always ask for permission if no limit is set
return false; // Always ask for permission if limit is not set
}
if (!transactionSignFirstAskedOn || hasDayPassed(transactionSignFirstAskedOn)) {
resetTransactionSignLimitLeft(host);
resetTransactionSignSpent(host);
}

const totalCost = +aettosToAe(+amount + +fee + +nameFee);
const futureLimitLeft = permissions.value[host].transactionSignLimitLeft - totalCost;
if (futureLimitLeft < 0) {
return false;
const currentAmountSpent = (permissions.value[host].transactionSignSpent || 0) + totalCost;
if (currentAmountSpent > transactionSignLimit) {
return false; // Transaction is out of the limit
}

setTransactionSignLimitLeft(host, futureLimitLeft);
permissions.value[host].transactionSignSpent = currentAmountSpent;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export const PERMISSION_DEFAULTS: IPermission = {
messageSign: false,
dailySpendLimit: false,
transactionSignLimit: 0,
transactionSignLimitLeft: 0,
transactionSignSpent: 0,
transactionSignFirstAskedOn: null,
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/accounts/AccountSuperhero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class AccountSuperhero extends AccountBase {
if (IS_EXTENSION_BACKGROUND && aeppOrigin) {
const txObject = unpackTx(txBase64);
if (
(checkPermission(new URL(aeppOrigin).host, METHODS.sign, txObject as any))
(checkPermission(new URL(aeppOrigin).host, METHODS.sign, txObject))
|| (await openPopup(
POPUP_TYPE_SIGN,
aeppOrigin,
Expand Down
4 changes: 2 additions & 2 deletions src/popup/pages/Names/AuctionHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="item">
<AccountItem
:address="highestBid.accountId"
:name="getName(highestBid.accountId).value"
:name="getName(highestBid.accountId)"
:protocol="PROTOCOL_AETERNITY"
/>
<AuctionOverview :name="name" />
Expand All @@ -26,7 +26,7 @@
<AccountItem
:protocol="PROTOCOL_AETERNITY"
:address="bid.accountId"
:name="getName(bid.accountId).value"
:name="getName(bid.accountId)"
/>
</div>
</div>
Expand Down
24 changes: 4 additions & 20 deletions src/popup/pages/PermissionManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@
<div class="limit-info">
<TokenAmount
:label="$t('pages.permissions.spent-today')"
:amount="permission.transactionSignLimit - permission.transactionSignLimitLeft"
:amount="permission.transactionSignSpent"
:protocol="PROTOCOL_AETERNITY"
/>
</div>
<div class="limit-info">
<TokenAmount
:label="$t('pages.permissions.left-today')"
:amount="permission.transactionSignLimitLeft"
:amount="permission.transactionSignLimit - permission.transactionSignSpent"
:protocol="PROTOCOL_AETERNITY"
/>
</div>
Expand Down Expand Up @@ -210,7 +210,6 @@ export default defineComponent({
const permission = ref<IPermission>({ ...PERMISSION_DEFAULTS });
const permissionChanged = ref(false);
const originalTransactionSignLimit = ref<number>(0);
const selectedAsset = computed(() => ({
contractId: AE_CONTRACT_ID,
Expand Down Expand Up @@ -240,10 +239,6 @@ export default defineComponent({
if (!permission.value.dailySpendLimit) {
permission.value.transactionSignLimit = 0;
} else if (originalTransactionSignLimit.value !== permission.value.transactionSignLimit) {
permission.value.transactionSignLimitLeft += (
permission.value.transactionSignLimit - originalTransactionSignLimit.value
);
}
addPermission({ ...permission.value, host });
Expand All @@ -256,22 +251,11 @@ export default defineComponent({
router.replace({ name: ROUTE_NOT_FOUND });
} else {
if (typeof savedPermission.transactionSignLimit === 'string') {
savedPermission.transactionSignLimit = parseInt(
savedPermission.transactionSignLimit,
10,
);
originalTransactionSignLimit.value = savedPermission.transactionSignLimit;
}
if (typeof savedPermission.transactionSignLimitLeft === 'string') {
savedPermission.transactionSignLimitLeft = parseInt(
savedPermission.transactionSignLimitLeft,
10,
);
savedPermission.transactionSignLimit = +savedPermission.transactionSignLimit;
}
setValues({ url: savedPermission.host, ...savedPermission });
permission.value = savedPermission;
permission.value = { ...PERMISSION_DEFAULTS, ...savedPermission };
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,14 @@ export interface IPermission {
name: string;
dailySpendLimit: boolean;
transactionSignFirstAskedOn: string | null;
/**
* Daily limit for transactions
*/
transactionSignLimit: number;
transactionSignLimitLeft: number;
/**
* Amount spent during that day
*/
transactionSignSpent: number;
}

/**
Expand Down

0 comments on commit 53cae92

Please sign in to comment.