Skip to content

Commit

Permalink
cleanup (#2331)
Browse files Browse the repository at this point in the history
* cleanup

* format
  • Loading branch information
wolverineks committed Feb 10, 2023
1 parent 9679f07 commit ee0b03a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 240 deletions.
1 change: 1 addition & 0 deletions src/Dashboard/StakePoolInfos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const useStakingTx = (
if (poolId == null) throw new Error('invalid state')
const accountStates = await wallet.fetchAccountState()
const accountState = accountStates[wallet.rewardAddressHex]
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!accountState) throw new Error('Account state not found')

const stakingUtxos = await wallet.getAllUtxosForKey()
Expand Down
39 changes: 13 additions & 26 deletions src/yoroi-wallets/cardano/ShelleyWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,7 @@ import {makeMemosManager, MemosManager} from './memosManager'
import {filterAddressesByStakingKey, getDelegationStatus} from './shelley/delegationUtils'
import {yoroiSignedTx} from './signedTx'
import {TransactionManager} from './transactionManager'
import {
isYoroiWallet,
NetworkId,
WalletEvent,
WalletImplementationId,
WalletInterface,
WalletSubscription,
YoroiWallet,
} from './types'
import {isYoroiWallet, NetworkId, WalletEvent, WalletImplementationId, WalletSubscription, YoroiWallet} from './types'
import {yoroiUnsignedTx} from './unsignedTx'
import {makeUtxoManager, UtxoManager} from './utxoManager'

Expand Down Expand Up @@ -110,7 +102,7 @@ export type ByronWalletJSON = Omit<ShelleyWalletJSON, 'account'>
export type WalletJSON = ShelleyWalletJSON | ByronWalletJSON

export default ShelleyWallet
export class ShelleyWallet implements WalletInterface {
export class ShelleyWallet implements YoroiWallet {
readonly primaryToken: DefaultAsset
readonly primaryTokenInfo: TokenInfo
readonly id: string
Expand All @@ -122,7 +114,7 @@ export class ShelleyWallet implements WalletInterface {
readonly internalChain: AddressChain
readonly externalChain: AddressChain
readonly publicKeyHex: string
readonly rewardAddressHex: null | string = null
readonly rewardAddressHex: string
readonly version: string
readonly checksum: CardanoTypes.WalletChecksum
readonly encryptedStorage: WalletEncryptedStorage
Expand Down Expand Up @@ -170,7 +162,7 @@ export class ShelleyWallet implements WalletInterface {
isEasyConfirmationEnabled: false,
})

await wallet.encryptAndSaveRootKey(rootKey, password)
await encryptAndSaveRootKey(wallet, rootKey, password)

return wallet
}
Expand All @@ -190,7 +182,6 @@ export class ShelleyWallet implements WalletInterface {
id: string
implementationId: WalletImplementationId
networkId: NetworkId

isReadOnly: boolean
storage: YoroiStorage
}): Promise<YoroiWallet> {
Expand Down Expand Up @@ -601,7 +592,6 @@ export class ShelleyWallet implements WalletInterface {
}

getDelegationStatus() {
if (this.rewardAddressHex == null) throw new Error('reward address is null')
const certsForKey = this.transactionManager.perRewardAddressCertificates[this.rewardAddressHex]
return Promise.resolve(getDelegationStatus(this.rewardAddressHex, certsForKey))
}
Expand All @@ -625,7 +615,7 @@ export class ShelleyWallet implements WalletInterface {
return this.generateNewReceiveAddress()
}

generateNewReceiveAddress(): boolean {
generateNewReceiveAddress() {
if (!this.canGenerateNewReceiveAddress()) return false

this.updateState({
Expand Down Expand Up @@ -870,7 +860,6 @@ export class ShelleyWallet implements WalletInterface {
}

async createWithdrawalTx(shouldDeregister: boolean): Promise<YoroiUnsignedTx> {
if (this.rewardAddressHex == null) throw new Error('reward address is null')
const timeToSlotFn = genTimeToSlot(getCardanoBaseConfig(this.getNetworkConfig()))

const time = await this.checkServerStatus()
Expand Down Expand Up @@ -966,7 +955,6 @@ export class ShelleyWallet implements WalletInterface {
}

async fetchAccountState(): Promise<AccountStateResponse> {
if (this.rewardAddressHex == null) throw new Error('reward address is null')
return api.bulkGetAccountState([this.rewardAddressHex], this.getBackendConfig())
}

Expand Down Expand Up @@ -1001,19 +989,19 @@ export class ShelleyWallet implements WalletInterface {
return api.fetchCurrentPrice(symbol, this.getBackendConfig())
}

state: WalletState = {
private state: WalletState = {
lastGeneratedAddressIndex: 0,
}

private isInitialized = false

_doFullSyncMutex: any = {name: 'doFullSyncMutex', lock: null}
private _doFullSyncMutex: any = {name: 'doFullSyncMutex', lock: null}

private subscriptions: Array<WalletSubscription> = []

_onTxHistoryUpdateSubscriptions: Array<(Wallet) => void> = []
private _onTxHistoryUpdateSubscriptions: Array<(Wallet) => void> = []

_isUsedAddressIndexSelector = defaultMemoize((perAddressTxs) =>
private _isUsedAddressIndexSelector = defaultMemoize((perAddressTxs) =>
_.mapValues(perAddressTxs, (txs) => {
assert.assert(!!txs, 'perAddressTxs cointains false-ish value')
return txs.length > 0
Expand Down Expand Up @@ -1058,9 +1046,6 @@ export class ShelleyWallet implements WalletInterface {
}

// ============ security & key management ============ //
async encryptAndSaveRootKey(rootKey: string, password: string) {
return this.encryptedStorage.rootKey.write(rootKey, password)
}

async getDecryptedRootKey(password: string) {
return this.encryptedStorage.rootKey.read(password)
Expand Down Expand Up @@ -1208,8 +1193,7 @@ export class ShelleyWallet implements WalletInterface {

// ========== persistence ============= //

// TODO: move to specific child class?
toJSON(): WalletJSON {
private toJSON(): WalletJSON {
return {
lastGeneratedAddressIndex: this.state.lastGeneratedAddressIndex,
publicKeyHex: this.publicKeyHex,
Expand Down Expand Up @@ -1322,3 +1306,6 @@ export const primaryTokenInfo = {
ticker: 'TADA',
} as TokenInfo,
}

const encryptAndSaveRootKey = (wallet: YoroiWallet, rootKey: string, password: string) =>
wallet.encryptedStorage.rootKey.write(rootKey, password)

0 comments on commit ee0b03a

Please sign in to comment.