-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2752 from dusk-network/feature-2750
web-wallet: Add a check for spent notes during the sync
- Loading branch information
Showing
6 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
web-wallet/src/lib/wallet/__tests__/nullifiersDifference.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { pluck } from "lamb"; | ||
|
||
import { cacheUnspentNotes } from "$lib/mock-data"; | ||
|
||
import { nullifiersDifference } from ".."; | ||
|
||
/** @type {(source: WalletCacheNote[]) => Uint8Array[]} */ | ||
const getNullifiers = pluck("nullifier"); | ||
|
||
describe("nullifiersDifference", () => { | ||
it("should return the array of unique nullifiers contained only in the first of the two given sets of nullifiers", () => { | ||
const a = getNullifiers(cacheUnspentNotes); | ||
const b = getNullifiers(cacheUnspentNotes.slice(0, a.length - 2)); | ||
|
||
// ensure we have meaningful data for the test | ||
expect(a.length).toBeGreaterThan(0); | ||
expect(b.length).toBeGreaterThan(1); | ||
|
||
expect(nullifiersDifference(a, b)).toStrictEqual(a.slice(-2)); | ||
expect(nullifiersDifference(b, a)).toStrictEqual([]); | ||
expect(nullifiersDifference(a, [])).toStrictEqual(a); | ||
expect(nullifiersDifference([], b)).toStrictEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { mapWith } from "lamb"; | ||
|
||
const nullifiersToString = mapWith(String); | ||
|
||
/** | ||
* Returns the array of unique nullifiers contained only | ||
* in the first of the two given nullifiers arrays. | ||
* | ||
* @see {@link https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement} | ||
* | ||
* @param {Uint8Array[]} a | ||
* @param {Uint8Array[]} b | ||
* @returns {Uint8Array[]} | ||
*/ | ||
function nullifiersDifference(a, b) { | ||
if (a.length === 0 || b.length === 0) { | ||
return a; | ||
} | ||
|
||
const result = []; | ||
const lookup = new Set(nullifiersToString(b)); | ||
|
||
for (const entry of a) { | ||
if (!lookup.has(entry.toString())) { | ||
result.push(entry); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export default nullifiersDifference; |