From 37e9ba0d05cfb24d8394ba86c89b0bce50d84bc6 Mon Sep 17 00:00:00 2001 From: minibits-cash Date: Wed, 30 Jul 2025 00:57:42 +0200 Subject: [PATCH] Prevent keysetId collision - more fixes --- src/models/Mint.ts | 22 +++++++++++++--------- src/models/MintsStore.ts | 10 +++++----- src/screens/MintsScreen.tsx | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/models/Mint.ts b/src/models/Mint.ts index b5311e75..29a8751b 100644 --- a/src/models/Mint.ts +++ b/src/models/Mint.ts @@ -99,10 +99,7 @@ export const MintModel = types }) .actions(withSetPropAction) // TODO? start to use across app to avoid pure setter methods, e.g. mint.setProp('color', '#ccc') .views(self => ({ - getAllMintsKeysetIds(): string[] { - const mintsStore = getRootStore(self).mintsStore - return mintsStore.allKeysetIds - } + })) .actions(self => ({ addKeyset(keyset: CashuMintKeyset) { @@ -203,7 +200,7 @@ export const MintModel = types }, keysetExists(keyset: CashuMintKeyset): boolean { return self.keysets.some(k => k.id === keyset.id) - }, + } })) .actions(self => ({ createProofsCounter(keyset: CashuMintKeyset) { @@ -224,7 +221,7 @@ export const MintModel = types } })) .actions(self => ({ - initKeyset(keyset: CashuMintKeyset) { + initKeyset(keyset: CashuMintKeyset, allKeysetIds: string[]) { // Do not add unit the wallet does not have configured try { if(!self.isUnitSupported(keyset.unit as MintUnit)) { @@ -250,7 +247,7 @@ export const MintModel = types } // Prevent keysetId collision with other mints - if(CashuUtils.isCollidingKeysetId(keyset.id, self.getAllMintsKeysetIds())) { + if(CashuUtils.isCollidingKeysetId(keyset.id, allKeysetIds)) { throw new AppError( Err.VALIDATION_ERROR, `KeysetId validation failed, collision detected for ${keyset.id}`, @@ -300,10 +297,15 @@ export const MintModel = types }, })) .actions(self => ({ - refreshKeysets(freshKeysets: CashuMintKeyset[]) { + refreshKeysets(freshKeysets: CashuMintKeyset[]) { + const mintsStore = getRootStore(self).mintsStore + const allKeysetIds = mintsStore.allKeysetIds + + log.trace('[refreshKeysets]', {freshKeysets, allKeysetIds}) + // add new keyset if not exists for (const keyset of freshKeysets) { - self.initKeyset(keyset) + self.initKeyset(keyset, allKeysetIds) self.setIsActive(keyset) } }, @@ -335,6 +337,8 @@ export const MintModel = types if(self.validateURL(url)) { const mintsStore = getRootStore(self).mintsStore + //log.trace('[setMintUrl]', {mintsStore}) + if(!mintsStore.alreadyExists(url)) { const proofsStore = getRootStore(self).proofsStore diff --git a/src/models/MintsStore.ts b/src/models/MintsStore.ts index 0048ef54..1c93785f 100644 --- a/src/models/MintsStore.ts +++ b/src/models/MintsStore.ts @@ -56,6 +56,9 @@ export const MintsStoreModel = types const mint = self.mints.find(m => m.mintUrl === mintUrl) return mint ? mint : undefined }, + get allKeysetIds() { + return self.mints.flatMap(m => m.keysetIds) + }, })) .actions(withSetPropAction) .actions(self => ({ @@ -144,7 +147,7 @@ export const MintsStoreModel = types continue } - mintInstance.initKeyset(keyset) + mintInstance.initKeyset(keyset, self.allKeysetIds) } for(const key of keys) { @@ -196,7 +199,7 @@ export const MintsStoreModel = types continue } - mintInstance.initKeyset(keyset) + mintInstance.initKeyset(keyset, self.allKeysetIds) } for(const key of keys) { @@ -287,9 +290,6 @@ export const MintsStoreModel = types return Object.values(groupedByUnit) as MintsByUnit[] }, - get allKeysetIds() { - return self.mints.flatMap(m => m.keysetIds) - }, alreadyExists(mintUrl: string) { return self.mints.some(m => m.mintUrl === mintUrl) ? true : false }, diff --git a/src/screens/MintsScreen.tsx b/src/screens/MintsScreen.tsx index dc157fa5..0ec03567 100644 --- a/src/screens/MintsScreen.tsx +++ b/src/screens/MintsScreen.tsx @@ -166,7 +166,7 @@ export const MintsScreen = observer(function MintsScreen({ route }: Props) { const matchingKeyset = keysets.find(keyset => selectedMint.keysets?.some(k => k.id === keyset.id)) if(!matchingKeyset) { - throw new AppError(Err.VALIDATION_ERROR, 'No keyset match, provided URL likely points to different mint.') + throw new AppError(Err.VALIDATION_ERROR, 'No keyset match, provided URL likely points to a different mint.') } selectedMint.setMintUrl!(mintUrl)