Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SW-1183] feat: adjust invite links #2274

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
CedrikNikita marked this conversation as resolved.
Show resolved Hide resolved
const secretKey = (encode(Buffer.from(props.secretKey), Encoding.Name)).slice(3);
CedrikNikita marked this conversation as resolved.
Show resolved Hide resolved
return new URL(
router
.resolve({ name: ROUTE_INVITE_CLAIM, params: { secretKey } })
.href.replace(/^#/, ''),
`${router
.resolve({ name: ROUTE_INVITE_CLAIM })
.href.replace(/^#/, '')}#${secretKey}`,
CedrikNikita marked this conversation as resolved.
Show resolved Hide resolved
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}
CedrikNikita marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 11 additions & 0 deletions src/store/migrations/07-change-invite-links-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (state) => {
const newState = { ...state };
if (newState.invites.invites?.length) {
newState.invites.invites = newState.invites.invites
.map((invite) => ({
...invite,
secretKey: Buffer.from(invite.secretKey, 'hex').slice(0, 32),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe store serialized the same was as in invite links 🤷‍♀️

}));
}
return newState;
};
2 changes: 2 additions & 0 deletions src/store/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import addFungibleTokensNewFields from './03-add-fungible-tokens-new-fields';
import addNamesNewFields from './04-add-names-new-fields';
import changeTransactionsStructure from './05-change-transactions-structure';
import changeTransactionsStructure2 from './06-change-transactions-structure-2';
import changeInviteLinksFormat from './07-change-invite-links-format';

registerMigration(collectState);
registerMigration(setDefaultNames);
Expand All @@ -14,5 +15,6 @@ registerMigration(addFungibleTokensNewFields);
registerMigration(addNamesNewFields);
registerMigration(changeTransactionsStructure);
registerMigration(changeTransactionsStructure2);
registerMigration(changeInviteLinksFormat);

export default runMigrations;
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