Skip to content

Commit

Permalink
modify proxy role possible
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Dec 12, 2024
1 parent 5036d34 commit f44eff4
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 40 deletions.
10 changes: 8 additions & 2 deletions components/overlays/ChooseWalletOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@
<p class="text-sm text-gray-400 wrap-text">
your currently selected account is {{ accountStore.getAddress }}
</p>
<div class="mt-10">
<div
v-if="
accountStore.hasSessionProxyForRole(SessionProxyRole.ReadBalance)
"
class="mt-10"
>
<button
@click="changeSessionAuthorization"
class="incognitee-bg btn btn_gradient rounded-md px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-400"
Expand Down Expand Up @@ -115,9 +120,10 @@ import {
extensionAccounts,
} from "~/lib/signerExtensionUtils";
import OverlayDialog from "~/components/overlays/OverlayDialog.vue";
import { defineProps, computed, ref, watch } from "vue";
import { computed, defineProps, ref, watch } from "vue";
import { useAccount } from "~/store/account.ts";
import { encodeAddress } from "@polkadot/util-crypto";
import { SessionProxyRole } from "~/lib/sessionProxyStorage";
const accountStore = useAccount();
const currentExtensionAccount = ref("");
Expand Down
125 changes: 115 additions & 10 deletions components/overlays/SessionProxiesOverlay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<div class="flex flex-col mt-5">
<form @submit.prevent="updateAuthorization">
<label>Select an option:</label>
<!-- removing proxies not yet supported
<div v-if="bestSessionProxyRole !== null" class="radio-group">
<div>
<input
Expand All @@ -27,7 +28,7 @@
/>
</div>
<label for="noProxy">remove all authorizations</label>
</div>
</div> -->
<div class="radio-group">
<div v-if="'readBalance' !== bestSessionProxyRole">
<input
Expand Down Expand Up @@ -138,28 +139,53 @@ import {
INCOGNITEE_TX_FEE,
INCOGNITEE_SESSION_PROXY_DEPOSIT,
} from "~/configs/incognitee";
import { useIncognitee } from "~/store/incognitee";
import { Health, useSystemHealth } from "~/store/systemHealth";
import { TypeRegistry, u32 } from "@polkadot/types";
import { incogniteeSidechain } from "~/lib/environmentConfig";
const accountStore = useAccount();
const incogniteeStore = useIncognitee();
const systemHealth = useSystemHealth();
const selectedSessionProxyRole = ref("NonTransfer");
const persistSessionProxy = ref(false);
const bestSessionProxy = ref(null);
const bestSessionProxyRole = ref(null);
const isSignerBusy = ref(false);
const updateAuthorization = async () => {
if (systemHealth.getSidechainSystemHealth.overall() !== Health.Healthy) {
alert(
"Sidechain health currently can't be assessed. Please wait for a green health indicator and try again",
);
return;
}
if (!props?.enableActions) {
console.error("network not live");
return;
}
console.log(
"updating authorization from ",
bestSessionProxyRole,
" to ",
selectedSessionProxyRole.value,
);
createSessionProxy();
if (bestSessionProxyRole.value === null) {
props?.close();
await createSessionProxy();
} else if (bestSessionProxyRole.value !== selectedSessionProxyRole.value) {
props?.close();
await modifySessionProxyRole(
bestSessionProxy.value,
selectedSessionProxyRole.value,
);
} else {
console.log("ignoring authorization update request: no change in role");
}
};
const createSessionProxy = async () => {
if (!enableActions.value) {
console.error("network not live");
return;
}
txStatus.value = "creating session proxy....";
openStatusOverlay();
await cryptoWaitReady();
Expand All @@ -177,6 +203,10 @@ const createSessionProxy = async () => {
" localStorage: " +
persistSessionProxy.value,
);
await addSessionProxyFromSeed(seed);
};
const addSessionProxyFromSeed = async (seed: Uint8Array) => {
const injector = accountStore.hasInjector ? accountStore.injector : null;
const role = incogniteeStore.api.createType(
"SessionProxyRole",
Expand All @@ -185,6 +215,18 @@ const createSessionProxy = async () => {
const now = new Date();
const expiryDate = new Date(now.getTime() + 40 * 24 * 60 * 60 * 1000);
const expiry = Math.floor(expiryDate.getTime());
console.log(
"create session proxy " +
proxy.address +
" with role: " +
role +
" expiry update to: " +
expiry,
);
const nonce = new u32(
new TypeRegistry(),
accountStore.nonce[incogniteeSidechain.value],
);
await incogniteeStore.api
.trustedAddSessionProxy(
accountStore.account,
Expand All @@ -194,25 +236,74 @@ const createSessionProxy = async () => {
sessionProxy.address,
expiry,
seed,
{ signer: injector?.signer },
{ signer: injector?.signer, nonce: nonce },
)
.then((result) =>
handleTopResult(result, "😀 session proxy registration successful"),
)
.catch((err) => handleTopError(err));
};
const modifySessionProxyRole = async (
proxy: AddressOrPair,
role: SessionProxyRole,
) => {
if (isSignerBusy.value) {
// fixme! this is a hack. don't know why extension pops up twice without this
console.log("signer busy. aborting repeated attempt...");
return;
}
isSignerBusy.value = true;
txStatus.value = "modifying session proxy role....";
openStatusOverlay();
const injector = accountStore.hasInjector ? accountStore.injector : null;
const seed = accountStore.sessionProxySeed(proxy);
const now = new Date();
const expiryDate = new Date(now.getTime() + 40 * 24 * 60 * 60 * 1000);
const expiry = Math.floor(expiryDate.getTime());
console.log(
"modify session proxy " +
proxy.address +
" to role: " +
role +
" expiry update to: " +
expiry,
);
const nonce = new u32(
new TypeRegistry(),
accountStore.nonce[incogniteeSidechain.value],
);
await incogniteeStore.api
.trustedAddSessionProxy(
accountStore.account,
incogniteeStore.shard,
incogniteeStore.fingerprint,
role,
proxy.address,
expiry,
seed,
{ signer: injector?.signer, nonce: nonce },
)
.then((result) =>
handleTopResult(result, "😀 session proxy registration successful"),
)
.then((result) => handleTopResult(result, "😀 session proxy r successful"))
.catch((err) => handleTopError(err));
};
const txStatus = ref("");
const handleTopResult = (result, successMsg?) => {
console.log("TOP result: " + result);
if (result) {
isSignerBusy.value = false;
if (result.status.isInSidechainBlock) {
if (successMsg) {
txStatus.value = successMsg;
} else {
txStatus.value =
"😀 included in sidechain block: " + result.status.asInSidechainBlock;
}
//update history to see successfuly action immediately
updateNotes();
//update history to see successful action immediately
props?.updateNotes();
return;
}
if (result.status.isInvalid) {
Expand Down Expand Up @@ -248,14 +339,28 @@ const props = defineProps({
type: Function,
required: true,
},
enableActions: {
type: Boolean,
required: true,
},
updateNotes: {
type: Function,
required: true,
},
});
watch(
() => props.show,
(show) => {
if (show) {
isSignerBusy.value = false;
[bestSessionProxy.value, bestSessionProxyRole.value] =
accountStore.sessionProxyBest();
if (bestSessionProxyRole.value !== null) {
selectedSessionProxyRole.value = bestSessionProxyRole.value;
} else {
selectedSessionProxyRole.value = "NonTransfer";
}
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion components/tabs/MessagingTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ const handleTopResult = (result, successMsg?) => {
txStatus.value =
"😀 included in sidechain block: " + result.status.asInSidechainBlock;
}
//update history to see successfuly action immediately
//update history to see successful action immediately
props.updateNotes();
return;
}
Expand Down
34 changes: 14 additions & 20 deletions components/tabs/WalletTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div v-if="show">
<WarningBanner
v-if="
isProd &&
accountStore.getSymbol === 'TEER' &&
accountStore.getAddress !== 'none' &&
!accountStore.hasInjector
"
Expand All @@ -27,13 +27,6 @@
textDesktop="This page is not yet live for mainnet. please visit <a href='https://try.incognitee.io'>try.incognitee.io</a> for the latest version of our paseo testnet wallet"
/>

<InfoBanner
v-if="!enableActions"
:isMobile="isMobile"
textMobile="Looking for <a href='/teerdays'>TEERdays</a>?"
textDesktop="If you are looking for our TEERDAYS page, please follow <a href='/teerdays'>this link</a>"
/>

<div class="mt-4"></div>

<NetworkSelector
Expand Down Expand Up @@ -1061,6 +1054,7 @@ import { QrcodeStream } from "vue-qrcode-reader";
import { ApiPromise } from "@polkadot/api";
import { formatMoment } from "~/helpers/date";
import { SessionProxyRole } from "~/lib/sessionProxyStorage";
import SessionProxiesOverlay from "~/components/overlays/SessionProxiesOverlay.vue";
const accountStore = useAccount();
const incogniteeStore = useIncognitee();
Expand Down Expand Up @@ -1220,7 +1214,7 @@ const handleTopResult = (result, successMsg?) => {
txStatus.value =
"😀 included in sidechain block: " + result.status.asInSidechainBlock;
}
//update history to see successfuly action immediately
//update history to see successful action immediately
props.updateNotes();
return;
}
Expand Down Expand Up @@ -1437,7 +1431,7 @@ const closePrivacyInfo = () => {
const showObtainTokenOverlay = ref(false);
const openObtainTokenOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1449,7 +1443,7 @@ const closeObtainTokenOverlay = () => {
const showShieldOverlay = ref(false);
const openShieldOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1464,7 +1458,7 @@ const closeShieldOverlay = () => {
const showFaucetOverlay = ref(false);
const openFaucetOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1476,7 +1470,7 @@ const closeFaucetOverlay = () => {
const showUnshieldOverlay = ref(false);
const openUnshieldOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1490,7 +1484,7 @@ const closeUnshieldOverlay = () => {
};
const showReceiveOverlay = ref(false);
const openReceiveOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1501,7 +1495,7 @@ const closeReceiveOverlay = () => {
};
const showPrivateSendOverlay = ref(false);
const openPrivateSendOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand All @@ -1523,7 +1517,7 @@ const closePrivateSendOverlay = () => {
const showGuessTheNumberOverlay = ref(false);
const openGuessTheNumberOverlay = () => {
if (!enableActions.value) {
if (!props?.enableActions) {
console.error("network not live");
return;
}
Expand Down Expand Up @@ -1605,10 +1599,10 @@ const props = defineProps({
type: Function,
required: true,
},
});
const enableActions = computed(() => {
return isLive.value || forceLive.value;
enableActions: {
type: Boolean,
required: true,
},
});
</script>

Expand Down
Loading

0 comments on commit f44eff4

Please sign in to comment.