Skip to content

Commit b38db26

Browse files
committed
When getting stealthKeys via subgraph StealthKeyChanged event, save block number of event in storage as starting point of announcements scan
1 parent 6d831ac commit b38db26

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

frontend/src/pages/AccountReceive.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,16 @@ function useScan() {
153153
const mostRecentBlockNumber = ref<number>(0);
154154
155155
// Start and end blocks for advanced mode settings
156-
const { advancedMode, startBlock, endBlock, setScanBlocks, setScanPrivateKey, scanPrivateKey, resetScanSettings } =
157-
useSettingsStore();
156+
const {
157+
advancedMode,
158+
startBlock,
159+
endBlock,
160+
setScanBlocks,
161+
setScanPrivateKey,
162+
scanPrivateKey,
163+
resetScanSettings,
164+
getRegisteredBlockNumber,
165+
} = useSettingsStore();
158166
const { signer, userAddress: userWalletAddress, isAccountSetup, provider } = useWalletStore();
159167
const startBlockLocal = ref<number>();
160168
const endBlockLocal = ref<number>();
@@ -320,6 +328,7 @@ function useScan() {
320328
mostRecentBlockTimestamp.value = latestBlock.timestamp;
321329
// Default scan behavior
322330
for await (const announcementsBatch of umbra.value.fetchSomeAnnouncements(
331+
getRegisteredBlockNumber(),
323332
signer.value,
324333
userWalletAddress.value,
325334
overrides

frontend/src/store/settings.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const settings = {
1212
language: 'language',
1313
sendHistorySave: 'send-history-save',
1414
UmbraApiVersion: 'umbra-api-version',
15+
registeredBlockNumber: 'registered-block-number',
1516
};
1617

1718
// Shared state between instances
@@ -27,6 +28,7 @@ const startBlock = ref<number | undefined>(undefined); // block number to start
2728
const endBlock = ref<number | undefined>(undefined); // block number to scan through
2829
const scanPrivateKey = ref<string>(); // private key entered when scanning
2930
const lastWallet = ref<string>(); // name of last wallet used
31+
const registeredBlockNumber = ref<number | undefined>(undefined); // block number of the when the user registered
3032
const params = new URLSearchParams(window.location.search);
3133
const paramLocale = params.get('locale') || undefined;
3234

@@ -41,6 +43,9 @@ export default function useSettingsStore() {
4143
lastWallet.value = LocalStorage.getItem(settings.lastWallet)
4244
? String(LocalStorage.getItem(settings.lastWallet))
4345
: undefined;
46+
registeredBlockNumber.value = LocalStorage.getItem(settings.registeredBlockNumber)
47+
? Number(LocalStorage.getItem(settings.registeredBlockNumber))
48+
: undefined;
4449
});
4550
setLanguage(
4651
paramLocale
@@ -137,6 +142,15 @@ export default function useSettingsStore() {
137142
LocalStorage.remove(settings.UmbraApiVersion);
138143
}
139144

145+
function getRegisteredBlockNumber() {
146+
return registeredBlockNumber.value;
147+
}
148+
149+
function setRegisteredBlockNumber(blockNumber: number) {
150+
registeredBlockNumber.value = blockNumber;
151+
LocalStorage.set(settings.registeredBlockNumber, blockNumber);
152+
}
153+
140154
return {
141155
toggleDarkMode,
142156
toggleAdvancedMode,
@@ -158,5 +172,7 @@ export default function useSettingsStore() {
158172
getUmbraApiVersion,
159173
setUmbraApiVersion,
160174
clearUmbraApiVersion,
175+
getRegisteredBlockNumber,
176+
setRegisteredBlockNumber,
161177
};
162178
}

frontend/src/store/wallet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,11 @@ const hasSetPublicKeysLegacy = async (name: string, provider: Provider) => {
631631

632632
// Helper method to check if user has registered public keys in the StealthKeyRegistry
633633
async function getRegisteredStealthKeys(account: string, provider: Provider) {
634+
const { setRegisteredBlockNumber } = useSettingsStore();
634635
try {
635-
const stealthPubKeys = await utils.lookupRecipient(account, provider); // throws if no keys found
636-
return stealthPubKeys;
636+
const registrationInfo = await utils.lookupRecipient(account, provider); // throws if no keys found
637+
setRegisteredBlockNumber(Number(registrationInfo.block));
638+
return registrationInfo;
637639
} catch (err) {
638640
window.logger.warn(err);
639641
return null;

umbra-js/src/classes/Umbra.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import {
3030
invalidStealthAddresses,
3131
getEthSweepGasInfo,
3232
lookupRecipient,
33+
getBlockNumberUserRegistered,
3334
assertSupportedAddress,
3435
checkSupportedAddresses,
35-
getBlockNumberUserRegistered,
3636
recursiveGraphFetch,
3737
} from '../utils/utils';
3838
import { Umbra as UmbraContract, Umbra__factory, ERC20__factory } from '../typechain';
@@ -419,16 +419,22 @@ export class Umbra {
419419
/**
420420
* @notice Fetches Umbra event logs starting from the block user registered their stealth keys in using
421421
* a subgraph, if available, falling back to RPC if not
422+
* @param possibleRegisteredBlockNumber Block number when user registered their stealth keys (if known)
423+
* @param Signer Signer with provider to use for fetching the block number (if not known) from the StealthKeyRegistry contract
424+
* @param address Address of the user for fetching the block number (if not known) from the subgraph or StealthKeyRegistry contract
422425
* @param overrides Override the start and end block used for scanning;
423426
* @returns A list of Announcement events supplemented with additional metadata, such as the sender, block,
424427
* timestamp, and txhash
428+
* @dev If the registered block number is not known, it will be fetched from the subgraph or the StealthKeyRegistry contract
425429
*/
426430
async *fetchSomeAnnouncements(
431+
possibleRegisteredBlockNumber: number | undefined,
427432
Signer: JsonRpcSigner,
428433
address: string,
429434
overrides: ScanOverrides = {}
430435
): AsyncGenerator<AnnouncementDetail[]> {
431-
const registeredBlockNumber = await getBlockNumberUserRegistered(address, Signer.provider, this.chainConfig);
436+
const registeredBlockNumber =
437+
possibleRegisteredBlockNumber || (await getBlockNumberUserRegistered(address, Signer.provider, this.chainConfig));
432438
// Get start and end blocks to scan events for
433439
const startBlock = overrides.startBlock || registeredBlockNumber || this.chainConfig.startBlock;
434440
const endBlock = overrides.endBlock || 'latest';

umbra-js/src/utils/utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ export async function toAddress(name: string, provider: EthersProvider) {
207207
/**
208208
* @notice Returns public keys from the recipientId
209209
* @dev When providing a public key, transaction hash, or address with advanced mode, the spending and viewing
210-
* public keys will be the same. Only keys retrieved from the StealthKeyRegistry will have different spending
211-
* and viewing keys
210+
* public keys will be the same. Only keys retrieved from the StealthKeyRegistry (or the subgraph) will have different spending
211+
* and viewing keys. Additionally, the block number when the user registered will be returned.
212212
* @param id Recipient identifier, must be an ENS name, CNS name, address, transaction hash, or public key
213213
* @param provider ethers provider to use
214214
* @param options Object containing lookup options:
@@ -230,15 +230,15 @@ export async function lookupRecipient(
230230
const isPublicKey = id.length === 132 && isHexString(id);
231231
if (supportPubKey && isPublicKey) {
232232
assertValidPoint(id);
233-
return { spendingPublicKey: id, viewingPublicKey: id };
233+
return { spendingPublicKey: id, viewingPublicKey: id, block: undefined };
234234
}
235235

236236
// Check if identifier is a transaction hash. If so, we recover the sender's public keys from the transaction
237237
const isTxHash = id.length === 66 && isHexString(id);
238238
if (supportTxHash && isTxHash) {
239239
const publicKey = await recoverPublicKeyFromTransaction(id, provider);
240240
assertValidPoint(publicKey);
241-
return { spendingPublicKey: publicKey, viewingPublicKey: publicKey };
241+
return { spendingPublicKey: publicKey, viewingPublicKey: publicKey, block: undefined };
242242
}
243243

244244
// The remaining checks are dependent on the advanced mode option. The provided identifier is now either an
@@ -259,7 +259,11 @@ export async function lookupRecipient(
259259
stealthKeyChangedEvent.viewingPubKey,
260260
stealthKeyChangedEvent.viewingPubKeyPrefix.toString()
261261
);
262-
return { spendingPublicKey: spendingPublicKey, viewingPublicKey: viewingPublicKey };
262+
return {
263+
spendingPublicKey: spendingPublicKey,
264+
viewingPublicKey: viewingPublicKey,
265+
block: stealthKeyChangedEvent.block,
266+
};
263267
} catch (error) {
264268
if (error instanceof Error) {
265269
console.log('Public key subgraph fetch error: ', error.message);
@@ -268,7 +272,8 @@ export async function lookupRecipient(
268272
}
269273
console.log('Error using subgraph to lookup receipient stealth keys, will query registry contract');
270274
const registry = new StealthKeyRegistry(provider);
271-
return registry.getStealthKeys(address);
275+
const { spendingPublicKey, viewingPublicKey } = await registry.getStealthKeys(address);
276+
return { spendingPublicKey, viewingPublicKey, block: undefined };
272277
}
273278
}
274279

@@ -277,7 +282,7 @@ export async function lookupRecipient(
277282
if (!txHash) throw new Error('Could not get public key because the provided account has not sent any transactions');
278283
const publicKey = await recoverPublicKeyFromTransaction(txHash, provider);
279284
assertValidPoint(publicKey);
280-
return { spendingPublicKey: publicKey, viewingPublicKey: publicKey };
285+
return { spendingPublicKey: publicKey, viewingPublicKey: publicKey, block: undefined };
281286
}
282287

283288
export async function getBlockNumberUserRegistered(

0 commit comments

Comments
 (0)