Skip to content

Commit 188dfd2

Browse files
1. Accounts list show spark addresses
2. Fix state init 3. In QR now shows Firo:{address}
1 parent ab10fd5 commit 188dfd2

8 files changed

Lines changed: 121 additions & 115 deletions

File tree

packages/extension/src/providers/bitcoin/libs/firo-wallet/base-firo-wallet.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ import {
1616
import { firoElectrum } from '../electrum-client/electrum-client';
1717
import configs from './configs';
1818
import { getSparkState } from '@/libs/spark-handler/generateSparkWallet';
19-
import {
20-
LOCAL_STORAGE_KEYS,
21-
LOCAL_STORAGE_PREFIXES,
22-
LocalStorageHelper,
23-
} from '@action/db/localStorage';
2419
import BrowserStorage from '@/libs/common/browser-storage';
2520
import { InternalStorageNamespace } from '@/types/provider';
2621

@@ -63,6 +58,7 @@ export class BaseFiroWallet {
6358
seed: string = '';
6459
network = NETWORK;
6560
balance: number = 0;
61+
private addresses2Check: string[] | null = null;
6662

6763
next_free_address_index = 0;
6864
next_free_change_address_index = 0;
@@ -229,33 +225,34 @@ export class BaseFiroWallet {
229225
return this._xPub;
230226
}
231227

232-
async getSparkAddressAsync(): Promise<string | undefined> {
233-
const localStorage = new LocalStorageHelper(LOCAL_STORAGE_PREFIXES.wallet);
234-
235-
if (!localStorage.exists(LOCAL_STORAGE_KEYS.sparkIndex)) {
236-
localStorage.set(LOCAL_STORAGE_KEYS.sparkIndex, 1);
228+
private initAddresses2Check = async () => {
229+
if (this.addresses2Check?.length) {
230+
return this.addresses2Check;
237231
}
232+
const address2Check = await this.getTransactionsAddresses();
233+
this.addresses2Check = address2Check;
234+
return address2Check;
235+
};
238236

239-
const currentIndex = localStorage.get<number>(
240-
LOCAL_STORAGE_KEYS.sparkIndex,
241-
);
242-
243-
const sparkState = await getSparkState(currentIndex);
244-
return sparkState?.defaultAddress;
237+
async getAddressIndex(address: string) {
238+
const address2Check = await this.initAddresses2Check();
239+
return address2Check.indexOf(address);
245240
}
246241

247-
skipAddress(): void {
248-
const localStorage = new LocalStorageHelper(LOCAL_STORAGE_PREFIXES.wallet);
242+
async getSparkAddressAsync(
243+
currentAddress: string,
244+
): Promise<string | undefined> {
245+
const currentIndex = await this.getAddressIndex(currentAddress);
249246

250-
if (!localStorage.exists(LOCAL_STORAGE_KEYS.sparkIndex)) {
251-
localStorage.set(LOCAL_STORAGE_KEYS.sparkIndex, 1);
247+
if (currentIndex === -1) {
248+
console.error(
249+
`🚨 Current address not found in address2Check: ${currentAddress}`,
250+
);
251+
return undefined;
252252
}
253253

254-
const currentIndex = localStorage.get<number>(
255-
LOCAL_STORAGE_KEYS.sparkIndex,
256-
);
257-
258-
localStorage.set(LOCAL_STORAGE_KEYS.sparkIndex, currentIndex! + 1);
254+
const sparkState = await getSparkState(currentIndex);
255+
return sparkState?.defaultAddress;
259256
}
260257

261258
async _getInternalAddressByIndex(index: number): Promise<string | undefined> {

packages/extension/src/ui/action/App.vue

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ import { useRateStore } from './store/rate-store';
150150
import { isGeoRestricted, isWalletRestricted } from '@/libs/utils/screening';
151151
import { useUpdateActivityState } from '@action/composables/update-activity-state';
152152
import { partition } from '@action/utils/array-utils';
153+
import { getSparkAddress } from '@action/composables/get-spark-address';
153154
154155
const wallet = new BaseFiroWallet();
155156
const db = new IndexedDBHelper();
@@ -352,7 +353,15 @@ onMounted(async () => {
352353
353354
const getSparkAccountState = async (network: BaseNetwork) => {
354355
if (network.name === NetworkNames.Firo) {
355-
const defaultAddress = await wallet.getSparkAddressAsync();
356+
// TODO: check maybe redundand
357+
const selectedAccount = accountHeaderData.value.selectedAccount;
358+
const currentSelectedAddress = selectedAccount?.address;
359+
360+
if (!currentSelectedAddress) {
361+
return null;
362+
}
363+
const displayAddress = network.displayAddress(currentSelectedAddress);
364+
const defaultAddress = await wallet.getSparkAddressAsync(displayAddress);
356365
await db.waitInit();
357366
const availableBalance = await db.readData<string>(
358367
DB_DATA_KEYS.sparkBalance,
@@ -375,18 +384,40 @@ const setNetwork = async (network: BaseNetwork) => {
375384
currentSubNetwork.value = '';
376385
}
377386
378-
const activeAccounts = await getAccountsByNetworkName(network.name);
379-
const inactiveAccounts = await kr.getAccounts(
387+
let activeAccounts = await getAccountsByNetworkName(network.name);
388+
let inactiveAccounts = await kr.getAccounts(
380389
getOtherSigners(network.signer),
381390
);
382391
383392
const sparkAccount = {} as SparkAccount;
384393
394+
const selectedAddress = await domainState.getSelectedAddress();
395+
385396
if (network.name === NetworkNames.Firo) {
397+
/**
398+
* Filter Firo accounts by base path
399+
*/
400+
const firoBasePath = "m/44'/136'/0'/0";
401+
402+
const [activeAccs, inactiveAccs] = partition(
403+
activeAccounts,
404+
account => account.basePath === firoBasePath,
405+
);
406+
407+
activeAccounts = activeAccs;
408+
inactiveAccounts = inactiveAccs;
409+
386410
/**
387411
* Init spark account info
388412
*/
389-
const defaultAddress = await wallet.getSparkAddressAsync();
413+
414+
let selectedAccount = activeAccounts[0];
415+
if (selectedAddress) {
416+
const found = activeAccounts.find(acc => acc.address === selectedAddress);
417+
if (found) selectedAccount = found;
418+
}
419+
420+
const defaultAddress = await getSparkAddress(network, selectedAccount.address);
390421
391422
await db.waitInit();
392423
const availableBalance =
@@ -396,23 +427,8 @@ const setNetwork = async (network: BaseNetwork) => {
396427
sparkAccount['defaultAddress'] = defaultAddress;
397428
}
398429
sparkAccount['sparkBalance'] = { availableBalance };
399-
400-
/**
401-
* Filter Firo accounts by base path
402-
*/
403-
// const firoBasePath = "m/44'/136'/0'/0";
404-
//
405-
// const [activeAccs, inactiveAccs] = partition(
406-
// activeAccounts,
407-
// account => account.basePath === firoBasePath,
408-
// );
409-
//
410-
// activeAccounts = activeAccs;
411-
// inactiveAccounts = inactiveAccs;
412430
}
413431
414-
const selectedAddress = await domainState.getSelectedAddress();
415-
416432
let selectedAccount = activeAccounts[0];
417433
if (selectedAddress) {
418434
const found = activeAccounts.find(acc => acc.address === selectedAddress);
@@ -536,7 +552,17 @@ const onSelectedSubnetworkChange = async (id: string) => {
536552
};
537553
538554
const onSelectedAddressChanged = async (newAccount: EnkryptAccount) => {
555+
const defaultAddress = await getSparkAddress(
556+
currentNetwork,
557+
newAccount.address,
558+
);
559+
560+
if (defaultAddress && accountHeaderData.value.sparkAccount) {
561+
accountHeaderData.value.sparkAccount['defaultAddress'] = defaultAddress;
562+
}
563+
539564
accountHeaderData.value.selectedAccount = newAccount;
565+
540566
await checkAddress(newAccount);
541567
if (!isAddressRestricted.value.isRestricted) {
542568
const accountStates = {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { unref, type MaybeRef } from 'vue';
2+
import { BaseNetwork } from '@/types/base-network';
3+
import { BaseFiroWallet } from '@/providers/bitcoin/libs/firo-wallet/base-firo-wallet';
4+
import { NetworkNames } from '@enkryptcom/types/dist';
5+
6+
const wallet = new BaseFiroWallet();
7+
8+
export async function getSparkAddress(
9+
network: MaybeRef<BaseNetwork>,
10+
selectedAddress: MaybeRef<string>,
11+
) {
12+
const selectedNetwork = unref(network);
13+
const selectedAddressData = unref(selectedAddress);
14+
15+
if (selectedNetwork?.name !== NetworkNames.Firo) {
16+
return null;
17+
}
18+
19+
if (!selectedAddressData) {
20+
return null;
21+
}
22+
23+
const displayAddress = selectedNetwork.displayAddress(selectedAddressData);
24+
25+
return wallet.getSparkAddressAsync(displayAddress);
26+
}

packages/extension/src/ui/action/db/localStorage.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

packages/extension/src/ui/action/views/accounts/components/accounts-list-item.vue

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
</p>
1212
<p class="accounts-item__info-amount">
1313
{{ $filters.formatFloatingPointValue(amount).value }} {{ symbol }}
14-
<span>{{ $filters.replaceWithEllipsis(address, 6, 4) }}</span>
14+
<span>{{
15+
$filters.replaceWithEllipsis(
16+
network.name === NetworkNames.Firo ? sparkDisplayAddress : address,
17+
6,
18+
4,
19+
)
20+
}}</span>
1521
</p>
1622
</div>
1723
<done-icon
@@ -41,13 +47,16 @@ import DoneIcon from '@action/icons/common/done_icon.vue';
4147
import MoreIcon from '@action/icons/common/more-icon.vue';
4248
import AccountsListItemMenu from './accounts-list-item-menu.vue';
4349
import { PropType, ref } from 'vue';
44-
import { onClickOutside } from '@vueuse/core';
50+
import { computedAsync, onClickOutside } from '@vueuse/core';
51+
import { getSparkAddress } from '@action/composables/get-spark-address';
52+
import { BaseNetwork } from '@/types/base-network';
53+
import { NetworkNames } from '@enkryptcom/types/dist';
4554
4655
const openEdit = ref(false);
4756
const dropdown = ref(null);
4857
const toggle = ref(null);
4958
50-
defineProps({
59+
const props = defineProps({
5160
name: {
5261
type: String,
5362
default: '',
@@ -71,6 +80,10 @@ defineProps({
7180
return {};
7281
},
7382
},
83+
network: {
84+
type: Object as PropType<BaseNetwork>,
85+
default: () => ({}),
86+
},
7487
identiconElement: {
7588
type: Function as PropType<(address: string, options?: any) => string>,
7689
default: () => ({}),
@@ -80,6 +93,11 @@ defineProps({
8093
deletable: Boolean,
8194
});
8295
96+
const sparkDisplayAddress = computedAsync(async () => {
97+
if (props.network.name !== NetworkNames.Firo) return null;
98+
return await getSparkAddress(props.network, props.address);
99+
});
100+
83101
const toggleEdit = () => {
84102
openEdit.value = !openEdit.value;
85103
};

packages/extension/src/ui/action/views/accounts/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
:is-checked="accountInfo.selectedAccount?.address == account.address"
2121
:select="selectAccount"
2222
:active="true"
23+
:network="network"
2324
:identicon-element="network.identicon"
2425
:show-edit="true"
2526
:deletable="account.walletType !== WalletType.mnemonic"
@@ -39,6 +40,7 @@
3940
:is-checked="false"
4041
:active="false"
4142
:identicon-element="network.identicon"
43+
:network="network"
4244
/>
4345
<div
4446
v-if="displayInactive.length === 0 && displayActive.length === 0"

packages/extension/src/ui/action/views/deposit/index.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
<div class="deposit__code">
3434
<qrcode-vue
3535
:value="
36-
$props.network?.provider == ProviderName.kadena
37-
? network.displayAddress(account.address)
38-
: network.provider +
39-
':' +
40-
network.displayAddress(account.address)
36+
NetworkNames.Firo +
37+
':' +
38+
network.displayAddress(account.address)
4139
"
4240
:size="150"
4341
level="H"
@@ -73,7 +71,7 @@
7371
<div v-if="activeTab === 'spark' && !!sparkAccount">
7472
<div class="deposit__code">
7573
<qrcode-vue
76-
:value="network.provider + ':' + sparkAccount?.defaultAddress"
74+
:value="NetworkNames.Firo + ':' + sparkAccount?.defaultAddress"
7775
:size="150"
7876
level="H"
7977
/>

packages/extension/src/ui/action/views/modal-accounts/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
:active="true"
2727
:identicon-element="network.identicon"
2828
:show-edit="showEdit"
29+
:network="network"
2930
/>
3031
</custom-scrollbar>
3132
</div>

0 commit comments

Comments
 (0)