Update abstract-hd-electrum-wallet.ts

REF: cache masterfingerprint

wip

Update abstract-wallet.ts
This commit is contained in:
Marcos Rodriguez Velez 2024-10-12 17:20:49 -04:00
parent a9c4aaaf63
commit fc6c558ef0
4 changed files with 34 additions and 27 deletions

View File

@ -1538,8 +1538,14 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
* @returns {string} Hex string of fingerprint derived from wallet mnemonics. Always has length of 8 chars and correct leading zeroes
*/
getMasterFingerprintHex() {
const seed = this._getSeed();
return AbstractHDElectrumWallet.seedToFingerprint(seed);
if (this.masterFingerprint) {
return this.masterFingerprint;
} else {
const seed = this._getSeed();
const value = AbstractHDElectrumWallet.seedToFingerprint(seed);
this.masterFingerprint = value as unknown as number;
return value;
}
}
/**

View File

@ -46,6 +46,7 @@ export class AbstractWallet {
_hideTransactionsInWalletsList: boolean;
_utxoMetadata: Record<string, UtxoMetadata>;
use_with_hardware_wallet: boolean;
masterFingerprint: number;
constructor() {
@ -502,12 +503,15 @@ export class AbstractWallet {
}
getMasterFingerprintFromHex(hexValue: string): number {
if (this.masterFingerprint !== 0) return this.masterFingerprint as number;
if (hexValue.length < 8) hexValue = '0' + hexValue;
const b = Buffer.from(hexValue, 'hex');
if (b.length !== 4) throw new Error('invalid fingerprint hex');
hexValue = hexValue[6] + hexValue[7] + hexValue[4] + hexValue[5] + hexValue[2] + hexValue[3] + hexValue[0] + hexValue[1];
const value = parseInt(hexValue, 16);
return parseInt(hexValue, 16);
this.masterFingerprint = value;
return value;
}
}

View File

@ -216,7 +216,8 @@ export class WatchOnlyWallet extends LegacyWallet {
if (masterFingerprintHex.length < 8) masterFingerprintHex = '0' + masterFingerprintHex; // conversion without explicit zero might result in lost byte
// poor man's little-endian conversion:
// ¯\_(ツ)_/¯
return (
const value =
masterFingerprintHex[6] +
masterFingerprintHex[7] +
masterFingerprintHex[4] +
@ -224,8 +225,9 @@ export class WatchOnlyWallet extends LegacyWallet {
masterFingerprintHex[2] +
masterFingerprintHex[3] +
masterFingerprintHex[0] +
masterFingerprintHex[1]
);
masterFingerprintHex[1];
this.masterFingerprint = value as unknown as number;
return value;
}
isHd() {

View File

@ -4,7 +4,6 @@ import {
Alert,
I18nManager,
InteractionManager,
LayoutAnimation,
ScrollView,
StyleSheet,
Switch,
@ -66,6 +65,7 @@ const WalletDetails: React.FC = () => {
const [hideTransactionsInWalletsList, setHideTransactionsInWalletsList] = useState<boolean>(
wallet.getHideTransactionsInWalletsList ? !wallet.getHideTransactionsInWalletsList() : true,
);
const { setOptions, navigate, addListener } = useExtendedNavigation();
const { colors } = useTheme();
const [walletName, setWalletName] = useState<string>(wallet.getLabel());
@ -86,7 +86,6 @@ const WalletDetails: React.FC = () => {
}
}, [wallet]);
const [isToolTipMenuVisible, setIsToolTipMenuVisible] = useState<boolean>(false);
const [isMasterFingerPrintVisible, setIsMasterFingerPrintVisible] = useState<boolean>(false);
const onMenuWillShow = () => setIsToolTipMenuVisible(true);
const onMenuWillHide = () => setIsToolTipMenuVisible(false);
@ -98,19 +97,24 @@ const WalletDetails: React.FC = () => {
useFocusEffect(
useCallback(() => {
const task = InteractionManager.runAfterInteractions(() => {
if (isMasterFingerPrintVisible && wallet.allowMasterFingerprint && wallet.allowMasterFingerprint()) {
if (wallet.allowMasterFingerprint && wallet.allowMasterFingerprint()) {
// @ts-expect-error: Need to fix later
if (wallet.getMasterFingerprintHex) {
// @ts-expect-error: Need to fix later
setMasterFingerprint(wallet.getMasterFingerprintHex());
const newMasterFingerprintValue = wallet.getMasterFingerprintHex();
setMasterFingerprint(newMasterFingerprintValue);
if (!(newMasterFingerprintValue === 0 || newMasterFingerprintValue === '00000000')) {
// this means a new master fingerprint was set. lets cache it
saveToDisk();
}
} else {
setMasterFingerprint(undefined);
}
} else {
setMasterFingerprint(undefined);
}
});
return () => task.cancel();
}, [isMasterFingerPrintVisible, wallet]),
}, [saveToDisk, wallet]),
);
const stylesHook = StyleSheet.create({
@ -379,11 +383,6 @@ const WalletDetails: React.FC = () => {
return `${label}-history.csv`;
}, [wallet]);
const onViewMasterFingerPrintPress = () => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
setIsMasterFingerPrintVisible(true);
};
return (
<ScrollView
automaticallyAdjustKeyboardInsets
@ -490,10 +489,11 @@ const WalletDetails: React.FC = () => {
wallet.setHideTransactionsInWalletsList(!value);
setHideTransactionsInWalletsList(!wallet.getHideTransactionsInWalletsList());
}
try {
await saveToDisk();
} catch (error: any) {
console.log(error.message);
} catch (error: unknown) {
console.error((error as Error).message);
}
}}
/>
@ -558,13 +558,7 @@ const WalletDetails: React.FC = () => {
{wallet.allowMasterFingerprint && wallet.allowMasterFingerprint() && (
<View style={styles.marginRight16}>
<Text style={[styles.textLabel2, stylesHook.textLabel2]}>{loc.wallets.details_master_fingerprint.toLowerCase()}</Text>
{isMasterFingerPrintVisible ? (
<BlueText selectable>{masterFingerprint ?? <ActivityIndicator />}</BlueText>
) : (
<TouchableOpacity onPress={onViewMasterFingerPrintPress}>
<BlueText>{loc.multisig.view}</BlueText>
</TouchableOpacity>
)}
<BlueText selectable>{masterFingerprint ?? <ActivityIndicator />}</BlueText>
</View>
)}
@ -642,6 +636,7 @@ const WalletDetails: React.FC = () => {
/>
</>
)}
{wallet.allowSignVerifyMessage && wallet.allowSignVerifyMessage() && (
<>
<BlueSpacing20 />