Skip to content

Commit

Permalink
feat: adjust invite links
Browse files Browse the repository at this point in the history
  • Loading branch information
CedrikNikita committed Aug 26, 2023
1 parent 987e72e commit 8675ccb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"satoshi-bitcoin": "^1.0.5",
"swagger-client": "3.18.4",
"swiper": "^6.8.4",
"tweetnacl": "^1.0.3",
"uuid": "^8.3.2",
"validator": "^13.7.0",
"vee-validate": "^4.5.8",
Expand Down
17 changes: 10 additions & 7 deletions src/popup/components/InviteItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</template>

<script lang="ts">
import nacl from 'tweetnacl';
import {
computed,
defineComponent,
Expand Down Expand Up @@ -116,7 +117,7 @@ export default defineComponent({
Field,
},
props: {
secretKey: { type: String, required: true },
secretKey: { type: Buffer, required: true },
createdAt: { type: Number, required: true },
},
setup(props, { emit }) {
Expand All @@ -139,17 +140,19 @@ export default defineComponent({
const inviteLinkBalance = ref(0);
const link = computed(() => {
// sg_ prefix was chosen as a dummy to decode from base58Check
const secretKey = (encode(Buffer.from(props.secretKey, 'hex'), Encoding.Signature)).slice(3);
// nm_ prefix was chosen as a dummy to decode from base58Check
const secretKey = (encode(Buffer.from(props.secretKey), Encoding.Name)).slice(3);
return new URL(
router
.resolve({ name: ROUTE_INVITE_CLAIM, params: { secretKey } })
.href.replace(/^#/, ''),
`${router
.resolve({ name: ROUTE_INVITE_CLAIM })
.href.replace(/^#/, '')}#${secretKey}`,
APP_LINK_WEB,
);
});
const address = computed(() => getAddressFromPriv(props.secretKey));
const address = computed(() => getAddressFromPriv(
nacl.sign.keyPair.fromSeed(Buffer.from(props.secretKey)).secretKey,
));
function deleteItem() {
store.commit('invites/delete', props.secretKey);
Expand Down
2 changes: 1 addition & 1 deletion src/popup/pages/Invite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default defineComponent({
loading.value = false;
}
store.commit('invites/add', secretKey);
store.commit('invites/add', Buffer.from(secretKey, 'hex').slice(0, 32));
formModel.value.amount = '';
}
Expand Down
18 changes: 11 additions & 7 deletions src/popup/pages/InviteClaim.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
import { defineComponent, onMounted } from 'vue';
import { decode } from '@aeternity/aepp-sdk';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { useRouter, useRoute } from 'vue-router';
import { useModals, useAeSdk } from '../../composables';
import { ROUTE_ACCOUNT } from '../router/routeNames';
export default defineComponent({
props: {
secretKey: { type: String, required: true },
},
setup(props) {
setup() {
const store = useStore();
const router = useRouter();
const route = useRoute();
const { getAeSdk } = useAeSdk({ store });
const { openDefaultModal } = useModals();
onMounted(async () => {
await getAeSdk();
try {
// sg_ prefix was chosen as a dummy to decode from base58Check
await store.dispatch('invites/claim', decode(`sg_${props.secretKey}`));
// nm_ prefix was chosen as a dummy to decode from base58Check
// The secretKey can be retrieved from the URL in two different ways:
// current: /invite#${secretKey}
// legacy: /invite/${secretKey}
await store.dispatch(
'invites/claim',
decode(`nm_${route.hash ? route.hash.replace('#', '') : route.fullPath.split('/').at(-1)}`),
);
await openDefaultModal({
msg: 'You have successfully claimed tokens by the invite link',
});
Expand Down
2 changes: 1 addition & 1 deletion src/popup/router/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ export const routes: WalletAppRouteConfig[] = [
},
{
name: ROUTE_INVITE_CLAIM,
path: '/invite/:secretKey',
path: '/invite/:secretKey*',
component: InviteClaim,
props: true,
meta: {
Expand Down
7 changes: 6 additions & 1 deletion src/store/modules/invites.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import nacl from 'tweetnacl';
import { AeSdk, MemoryAccount, Node } from '@aeternity/aepp-sdk';
import { useAccounts, useModals, useNetworks } from '@/composables';
import { tg } from '../plugins/languages';

const SEED_LENGTH = 32;

export default {
namespaced: true,
state: {
Expand All @@ -24,7 +27,9 @@ export default {
name: activeNetwork.value.name,
instance: new Node(activeNetwork.value.protocols.aeternity.nodeUrl),
}],
accounts: [new MemoryAccount(secretKey)],
// `secretKey` variable can be either seed or seed + public key (legacy)
accounts: [new MemoryAccount(secretKey.length === SEED_LENGTH
? nacl.sign.keyPair.fromSeed(secretKey).secretKey : secretKey)],
});
await aeSdk.transferFunds(1, activeAccount.value.address, { verify: false });
},
Expand Down

0 comments on commit 8675ccb

Please sign in to comment.