Skip to content

Commit 54cd555

Browse files
committed
refactor(data): ♻️ move state from main.js to state/index.js
1 parent 8a8c0da commit 54cd555

File tree

3 files changed

+114
-93
lines changed

3 files changed

+114
-93
lines changed

src/helpers/wallet.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import {
1717
STOREAGE_SALT, OIDC_CLAIMS,
1818
KS_CIPHER, KS_PRF, USAGE,
1919
} from './constants.js'
20+
import {
21+
walletFunds,
22+
} from '../state/index.js'
2023

2124
// @ts-ignore
2225
import blake from 'blakejs'
@@ -831,7 +834,7 @@ export async function initWallet(
831834
// }
832835

833836
export async function updateAddrFunds(
834-
wallet, walletFunds, insightRes,
837+
wallet, insightRes,
835838
) {
836839
let updatedAt = Date.now()
837840
let { addrStr, ...res } = insightRes
@@ -889,12 +892,12 @@ export async function updateAddrFunds(
889892
if ($addr.accountIndex >= storeAcctLen) {
890893
batchGenAccts(wallet.recoveryPhrase, $addr.accountIndex)
891894
.then(() => {
892-
// updateAllFunds(wallet, walletFunds)
895+
// updateAllFunds(wallet)
893896
batchGenAcctsAddrs(wallet)
894897
.then(accts => {
895898
console.log('batchGenAcctsAddrs', { accts })
896899

897-
updateAllFunds(wallet, walletFunds)
900+
updateAllFunds(wallet)
898901
.then(funds => {
899902
console.log('updateAllFunds then funds', funds)
900903
})
@@ -910,7 +913,7 @@ export async function updateAddrFunds(
910913
return { balance: 0 }
911914
}
912915

913-
export async function updateAllFunds(wallet, walletFunds) {
916+
export async function updateAllFunds(wallet) {
914917
let funds = 0
915918
let addrKeys = await store.addresses.keys()
916919

@@ -943,7 +946,7 @@ export async function updateAllFunds(wallet, walletFunds) {
943946
if (addrIdx > -1) {
944947
addrKeys.splice(addrIdx, 1)
945948
}
946-
funds += (await updateAddrFunds(wallet, walletFunds, insightRes))?.balance || 0
949+
funds += (await updateAddrFunds(wallet, insightRes))?.balance || 0
947950
walletFunds.balance = funds
948951
}
949952

@@ -1275,7 +1278,7 @@ export async function deriveTxWallet(
12751278
let cachedAddrs = {}
12761279
let privateKeys = {}
12771280
let coreUtxos
1278-
let txs
1281+
let transactions
12791282
let tmpWallet
12801283

12811284
if (Array.isArray(fundAddrs) && fundAddrs.length > 0) {
@@ -1302,7 +1305,7 @@ export async function deriveTxWallet(
13021305
coreUtxos = await dashsight.getMultiCoreUtxos(
13031306
Object.keys(privateKeys)
13041307
)
1305-
txs = await dashsight.getAllTxs(
1308+
transactions = await dashsight.getAllTxs(
13061309
Object.keys(privateKeys)
13071310
)
13081311
} else {
@@ -1324,17 +1327,18 @@ export async function deriveTxWallet(
13241327
coreUtxos = await dashsight.getCoreUtxos(
13251328
tmpWallet.address
13261329
)
1327-
txs = await dashsight.getAllTxs(
1330+
transactions = await dashsight.getAllTxs(
13281331
[tmpWallet.address]
13291332
)
13301333
}
13311334

1332-
console.log('getAllTxs', txs)
1335+
console.log('getAllTxs', transactions)
13331336

13341337
return {
13351338
privateKeys,
13361339
cachedAddrs,
13371340
coreUtxos,
1341+
transactions,
13381342
}
13391343
}
13401344

src/main.js

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@ import { lit as html } from './helpers/lit.js'
33
import {
44
generateWalletData,
55
deriveWalletData,
6-
envoy,
7-
sortContactsByAlias,
86
getStoreData,
97
formDataEntries,
10-
getAddressIndexFromUsage,
118
} from './helpers/utils.js'
129

1310
import {
1411
DUFFS,
15-
OIDC_CLAIMS,
16-
// USAGE,
1712
} from './helpers/constants.js'
1813

1914
import {
2015
findInStore,
2116
initDashSocket,
22-
// batchAddressGenerate,
2317
batchGenAccts,
2418
batchGenAcctAddrs,
2519
batchGenAcctsAddrs,
@@ -35,13 +29,22 @@ import {
3529
getUnusedChangeAddress,
3630
getAccountWallet,
3731
} from './helpers/wallet.js'
32+
3833
import {
3934
localForageBaseCfg,
4035
importFromJson,
4136
exportWalletData,
4237
saveJsonToFile,
4338
} from './helpers/db.js'
4439

40+
import {
41+
appState,
42+
appTools,
43+
appDialogs,
44+
userInfo,
45+
walletFunds,
46+
} from './state/index.js'
47+
4548
import setupNav from './components/nav.js'
4649
import setupMainFooter from './components/main-footer.js'
4750
import setupSendRequestBtns from './components/send-request-btns.js'
@@ -75,80 +78,6 @@ let accounts
7578
let wallets
7679
let wallet
7780

78-
let appState = envoy(
79-
{
80-
phrase: null,
81-
encryptionPassword: null,
82-
selectedWallet: '',
83-
selectedAlias: '',
84-
aliasInfo: {},
85-
contacts: [],
86-
sentTransactions: {},
87-
account: {},
88-
},
89-
// async (state, oldState, prop) => {
90-
// if (prop === 'sentTransactions') {
91-
// console.log(prop, state[prop])
92-
// }
93-
// },
94-
)
95-
let appTools = envoy(
96-
{
97-
storedData: {},
98-
},
99-
)
100-
let userInfo = envoy(
101-
{
102-
...OIDC_CLAIMS,
103-
},
104-
async (state, oldState, prop) => {
105-
if (state[prop] !== oldState[prop]) {
106-
let decryptedAlias = await appTools.storedData.decryptItem(
107-
store.aliases,
108-
appState.selectedAlias,
109-
)
110-
appTools.storedData.encryptItem(
111-
store.aliases,
112-
appState.selectedAlias,
113-
{
114-
...decryptedAlias,
115-
updatedAt: (new Date()).toISOString(),
116-
info: {
117-
...decryptedAlias.info,
118-
[prop]: state[prop],
119-
},
120-
},
121-
false,
122-
)
123-
}
124-
},
125-
)
126-
127-
// rigs
128-
let appDialogs = envoy(
129-
{
130-
onboard: {},
131-
phraseBackup: {},
132-
phraseGenerate: {},
133-
phraseImport: {},
134-
walletEncrypt: {},
135-
walletDecrypt: {},
136-
addContact: {},
137-
editContact: {},
138-
editProfile: {},
139-
scanContact: {},
140-
sendOrReceive: {},
141-
sendConfirm: {},
142-
requestQr: {},
143-
},
144-
)
145-
146-
let walletFunds = envoy(
147-
{
148-
balance: 0
149-
},
150-
)
151-
15281
// element
15382
let bodyNav
15483
let dashBalance
@@ -571,7 +500,7 @@ async function main() {
571500

572501
appDialogs.addContact = await addContactRig({
573502
setupDialog, updateAllFunds,
574-
appDialogs, appState, appTools, store, walletFunds,
503+
appDialogs, appState, appTools, store,
575504
mainApp, wallet, userInfo, contactsList,
576505
})
577506

@@ -783,7 +712,7 @@ async function main() {
783712
.then(accts => {
784713
console.log('batchGenAcctsAddrs', { accts })
785714

786-
updateAllFunds(wallet, walletFunds)
715+
updateAllFunds(wallet)
787716
.then(funds => {
788717
console.log('updateAllFunds then funds', funds)
789718
})
@@ -1016,7 +945,7 @@ async function main() {
1016945
)
1017946

1018947
setTimeout(() =>
1019-
updateAllFunds(wallet, walletFunds)
948+
updateAllFunds(wallet)
1020949
.then(funds => {
1021950
console.log('updateAllFunds then funds', funds)
1022951
})
@@ -1141,7 +1070,7 @@ async function main() {
11411070
}
11421071

11431072
setTimeout(() =>
1144-
updateAllFunds(wallet, walletFunds)
1073+
updateAllFunds(wallet)
11451074
.then(funds => {
11461075
console.log('updateAllFunds then funds', funds)
11471076
})

src/state/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {
2+
OIDC_CLAIMS,
3+
} from '../helpers/constants.js'
4+
import {
5+
envoy,
6+
} from '../helpers/utils.js'
7+
import {
8+
store,
9+
} from '../helpers/wallet.js'
10+
11+
export const appDialogs = envoy(
12+
{
13+
onboard: {},
14+
phraseBackup: {},
15+
phraseGenerate: {},
16+
phraseImport: {},
17+
walletEncrypt: {},
18+
walletDecrypt: {},
19+
addContact: {},
20+
editContact: {},
21+
editProfile: {},
22+
scanContact: {},
23+
sendOrReceive: {},
24+
sendConfirm: {},
25+
requestQr: {},
26+
},
27+
)
28+
29+
export const appState = envoy(
30+
{
31+
phrase: null,
32+
encryptionPassword: null,
33+
selectedWallet: '',
34+
selectedAlias: '',
35+
aliasInfo: {},
36+
contacts: [],
37+
sentTransactions: {},
38+
transactions: [],
39+
account: {},
40+
},
41+
// async (state, oldState, prop) => {
42+
// if (prop === 'sentTransactions') {
43+
// console.log(prop, state[prop])
44+
// }
45+
// },
46+
)
47+
48+
export const appTools = envoy(
49+
{
50+
storedData: {},
51+
},
52+
)
53+
54+
export const userInfo = envoy(
55+
{
56+
...OIDC_CLAIMS,
57+
},
58+
async (state, oldState, prop) => {
59+
if (
60+
state[prop] !== oldState[prop] &&
61+
appState.selectedAlias
62+
) {
63+
let decryptedAlias = await appTools.storedData.decryptItem(
64+
store.aliases,
65+
appState.selectedAlias,
66+
)
67+
appTools.storedData.encryptItem(
68+
store.aliases,
69+
appState.selectedAlias,
70+
{
71+
...decryptedAlias,
72+
updatedAt: (new Date()).toISOString(),
73+
info: {
74+
...decryptedAlias.info,
75+
[prop]: state[prop],
76+
},
77+
},
78+
false,
79+
)
80+
}
81+
},
82+
)
83+
84+
export const walletFunds = envoy(
85+
{
86+
balance: 0
87+
},
88+
)

0 commit comments

Comments
 (0)