Skip to content

Commit

Permalink
fix(hdwallet): use last aeternity account for signing when other prot…
Browse files Browse the repository at this point in the history
…ocol account is active
  • Loading branch information
subhod-i authored and CedrikNikita committed Aug 29, 2023
1 parent d6f23ac commit b1d9d87
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
24 changes: 19 additions & 5 deletions src/lib/accounts/AccountSuperhero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export class AccountSuperhero extends AccountBase {
{ ...options, origin: options.aeppOrigin },
);
}
return this.store.dispatch('accounts/signTransaction', { txBase64, options });
return this.store.dispatch('accounts/signTransaction', {
txBase64,
options: {
fromAccount: this.address,
...options,
},
});
}

async signMessage(message: string, options: any): Promise<Uint8Array> {
Expand All @@ -59,14 +65,20 @@ export class AccountSuperhero extends AccountBase {
{ ...options, origin: options.aeppOrigin },
);
}
return this.sign(messageToHash(message), options);
return this.sign(messageToHash(message), { fromAccount: this.address, ...options });
}

sign(data: string | Uint8Array, options: any): Promise<Uint8Array> {
const { getLastActiveProtocolAccount } = useAccounts({ store: this.store });
return IS_EXTENSION_BACKGROUND
? sign(data, getLastActiveProtocolAccount(PROTOCOL_AETERNITY)!.secretKey) as any
: this.store.dispatch('accounts/sign', data, options);
: this.store.dispatch('accounts/sign', {
data,
options: {
fromAccount: this.address,
...options,
},
});
}

async fgPermissionCheckAndSign(method: any, payload: any, options: any, origin?: string) {
Expand All @@ -91,12 +103,13 @@ export class AccountSuperhero extends AccountBase {
},
});
}
return this.sign(messageToHash(payload), options);
return this.sign(messageToHash(payload), { fromAccount: this.address, ...options });
}

return this.store.dispatch('accounts/signTransaction', {
txBase64: payload,
options: {
fromAccunt: this.address,
...options,
modal: !permission,
app,
Expand Down Expand Up @@ -135,11 +148,12 @@ export class AccountSuperhero extends AccountBase {
))
) {
if (method === METHODS.signMessage) {
return this.store.dispatch('accounts/sign', messageToHash(payload));
return this.store.dispatch('accounts/sign', { data: messageToHash(payload), options: { fromAccount: this.address } });
}
return this.store.dispatch('accounts/signTransaction', {
txBase64: payload,
options: {
fromAccount: this.address,
...options,
modal: false,
host: options.origin,
Expand Down
5 changes: 4 additions & 1 deletion src/popup/components/Modals/ConfirmRawSign.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<script lang="ts">
import { computed, defineComponent, onUnmounted } from 'vue';
import { useStore } from 'vuex';
import { PROTOCOL_AETERNITY } from '@/constants';
import { RejectedByUserError } from '../../../lib/errors';
import { useAccounts, usePopupProps } from '../../../composables';
Expand All @@ -81,7 +82,9 @@ export default defineComponent({
setup() {
const { popupProps, sender, setPopupProps } = usePopupProps();
const store = useStore();
const { activeAccount } = useAccounts({ store });
const { getLastActiveProtocolAccount } = useAccounts({ store });
const activeAccount = getLastActiveProtocolAccount(PROTOCOL_AETERNITY);
const dataAsString = computed(
(): string => (typeof popupProps.value?.data === 'string')
Expand Down
8 changes: 5 additions & 3 deletions src/popup/components/Modals/ConfirmTransactionSign.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import type {
} from '@/types';
import { tg } from '@/store/plugins/languages';
import { RejectedByUserError } from '@/lib/errors';
import { TX_DIRECTION } from '@/constants';
import { PROTOCOL_AETERNITY, TX_DIRECTION } from '@/constants';
import {
fetchJson,
handleUnknownError,
Expand Down Expand Up @@ -210,7 +210,9 @@ export default defineComponent({
const { aeActiveNetworkSettings } = useAeNetworkSettings();
const { getAeSdk } = useAeSdk({ store });
const { activeAccount } = useAccounts({ store });
const { getLastActiveProtocolAccount } = useAccounts({ store });
const activeAccount = getLastActiveProtocolAccount(PROTOCOL_AETERNITY);
const { popupProps, setPopupProps } = usePopupProps();
Expand Down Expand Up @@ -360,7 +362,7 @@ export default defineComponent({
try {
verifying.value = true;
const sdk = await getAeSdk();
const balance = await sdk.getBalance(activeAccount.value.address).catch((err) => {
const balance = await sdk.getBalance(activeAccount!.address).catch((err) => {
if (!isNotFoundError(err)) {
handleUnknownError(err);
}
Expand Down
9 changes: 6 additions & 3 deletions src/store/modules/accounts/hdWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ export default {
await openModal(MODAL_CONFIRM_TRANSACTION_SIGN, { tx: txObject, txBase64 });
}
},
sign({ dispatch }, data) {
return dispatch('signWithoutConfirmation', { data });
sign({ dispatch }, { data, options }) {
return dispatch('signWithoutConfirmation', { data, options });
},
async signTransaction({ dispatch, rootState, rootGetters }, {
txBase64,
options: { modal = true, app = null },
options: { modal = true, app = null, fromAccount },
}) {
const { nodeNetworkId } = useAeSdk({
store: { state: rootState, getters: rootGetters },
Expand All @@ -109,6 +109,9 @@ export default {
Buffer.from(nodeNetworkId.value),
Buffer.from(encodedTx),
]),
options: {
fromAccount,
},
},
);
return buildTx({ tag: Tag.SignedTx, encodedTx, signatures: [signature] });
Expand Down
4 changes: 2 additions & 2 deletions src/store/modules/accounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export default {
},

actions: {
sign({ getters: { active, getModule }, dispatch }, data) {
return dispatch(`${getModule(active).name}/sign`, data);
sign({ getters: { active, getModule }, dispatch }, { data, options }) {
return dispatch(`${getModule(active).name}/sign`, { data, options });
},

signTransaction({ getters: { active, getModule }, dispatch }, { txBase64, options }) {
Expand Down

0 comments on commit b1d9d87

Please sign in to comment.