From 4b76f4224296bc4922dbf4945f42b84225aa0e7e Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 19:52:31 -0500 Subject: [PATCH 01/21] Update import syntax --- .../boundwitness/packages/validator/src/spec/Validator.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/spec/Validator.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/spec/Validator.spec.ts index 2160947e5c..ca928a2c3f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/spec/Validator.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/spec/Validator.spec.ts @@ -5,7 +5,7 @@ import type { Payload } from '@xyo-network/payload' import { PayloadBuilder } from '@xyo-network/payload' import { SchemaNameValidator } from '@xyo-network/schema-name-validator' -import { BoundWitnessValidator } from '../Validator' +import { BoundWitnessValidator } from '../Validator.ts' const dumpErrors = (errors: Error[]) => { for (const error of errors) { From 3d3c36a1c8facca69e96caeb7c3f4edd163c7b0c Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:11:05 -0500 Subject: [PATCH 02/21] Add hex lib --- .../packages/boundwitness/packages/validator/package.json | 1 + yarn.lock | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/packages/protocol/packages/boundwitness/packages/validator/package.json b/packages/protocol/packages/boundwitness/packages/validator/package.json index 56707a1f04..cbfd1a5516 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/package.json +++ b/packages/protocol/packages/boundwitness/packages/validator/package.json @@ -30,6 +30,7 @@ "types": "dist/neutral/index.d.ts", "dependencies": { "@xylabs/arraybuffer": "^4.0.9", + "@xylabs/hex": "^4.0.10", "@xylabs/lodash": "^4.0.9", "@xylabs/typeof": "^4.0.9", "@xyo-network/account": "workspace:^", diff --git a/yarn.lock b/yarn.lock index bce93dac09..55a7edcae0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3745,6 +3745,13 @@ __metadata: languageName: node linkType: hard +"@xylabs/hex@npm:^4.0.10": + version: 4.0.10 + resolution: "@xylabs/hex@npm:4.0.10" + checksum: 10/cad87986ced0e46e66c305d88dfd753772bd99701fe3b1fdbe97dee4d3ad05f7505dabbb1f5db9506c90caa69eeb8984b1031ebb5205c40839beb269982a5e73 + languageName: node + linkType: hard + "@xylabs/hex@npm:^4.0.9": version: 4.0.9 resolution: "@xylabs/hex@npm:4.0.9" @@ -4414,6 +4421,7 @@ __metadata: resolution: "@xyo-network/boundwitness-validator@workspace:packages/protocol/packages/boundwitness/packages/validator" dependencies: "@xylabs/arraybuffer": "npm:^4.0.9" + "@xylabs/hex": "npm:^4.0.10" "@xylabs/lodash": "npm:^4.0.9" "@xylabs/ts-scripts-yarn3": "npm:^4.0.7" "@xylabs/tsconfig": "npm:^4.0.7" From 58201d11a2db8dc26478ef60db4bfcdd41018961 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:11:27 -0500 Subject: [PATCH 03/21] Add addresses helpers --- .../packages/boundwitness/packages/validator/src/index.ts | 1 + .../validator/src/lib/addresses/addressesContainsAll.ts | 8 ++++++++ .../packages/validator/src/lib/addresses/index.ts | 1 + .../boundwitness/packages/validator/src/lib/index.ts | 1 + 4 files changed, 11 insertions(+) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/index.ts diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/index.ts b/packages/protocol/packages/boundwitness/packages/validator/src/index.ts index 18cc9f468d..39e3eff932 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/index.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/index.ts @@ -1 +1,2 @@ +export * from './lib/index.ts' export * from './Validator.ts' diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts new file mode 100644 index 0000000000..09504be11a --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts @@ -0,0 +1,8 @@ +import type { Address } from '@xylabs/hex' +import type { BoundWitnessFields } from '@xyo-network/boundwitness-model' +import { BoundWitness } from '@xyo-network/boundwitness-model' + +export const addressesContainsAll = (boundwitness: Partial, addresses: Address[]): boolean => { + // TODO + return false +} diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts new file mode 100644 index 0000000000..d63361bbc2 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts @@ -0,0 +1 @@ +export * from './addressesContainsAll.ts' diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/index.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/index.ts new file mode 100644 index 0000000000..6fd49f3c74 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/index.ts @@ -0,0 +1 @@ +export * from './addresses/index.ts' From 3b1d6e180f47760dcaa2c9ca253a61f8ed7c05d8 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:11:34 -0500 Subject: [PATCH 04/21] Address contains all helper tests --- .../spec/addressesContainsAll.spec.ts | 39 +++++++++++++++++++ .../src/lib/addresses/spec/tsconfig.json | 8 ++++ 2 files changed, 47 insertions(+) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/tsconfig.json diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts new file mode 100644 index 0000000000..9d35d7313a --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -0,0 +1,39 @@ +import type { WalletInstance } from '@xyo-network/account' +import { HDWallet } from '@xyo-network/account' +import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' + +import { addressesContainsAll } from '../addressesContainsAll.ts' + +describe('BoundWitnessValidator', () => { + const payload = { schema: 'network.xyo.test', value: Date.now() } + const cases: [string, WalletInstance[]][] = [ + ['with no signer', [] as WalletInstance[]], + ['with single signer', [] as WalletInstance[]], + ['with multiple signers', [] as WalletInstance[]], + ] + beforeAll(async () => { + cases[1][1] = [await HDWallet.random()] + cases[2][1] = [await HDWallet.random(), await HDWallet.random()] + }) + describe.each(cases)('%s', (_, signers) => { + describe('returns true', () => { + it('when all addresses present', async () => { + const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() + expect(addressesContainsAll(bw, signers)).toBeTrue() + }) + it('when extra addresses present', async () => { + const extra = [...signers, await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() + expect(addressesContainsAll(bw, signers)).toBeTrue() + }) + }) + describe('returns false', () => { + it('when single address missing', async () => { + // + }) + it('when all addresses missing', async () => { + // + }) + }) + }) +}) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/tsconfig.json b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/tsconfig.json new file mode 100644 index 0000000000..8035369776 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + + }, + "extends": "@xylabs/tsconfig-jest" +} \ No newline at end of file From d7f4eaa71a84e95045c213f8c04ca85bf660fd50 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:20:22 -0500 Subject: [PATCH 05/21] Add vite debug --- .vscode/launch.json | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 5024061d58..7ee84db66c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,12 +10,40 @@ "--forceExit", "--runInBand", "--testTimeout=120000", - "packages/modules/packages/diviner/packages/payloadpointer/packages/memory/src/spec/Diviner.payloadPointer.schema.spec.ts" + "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts" ], "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "cwd": "${workspaceFolder}", "envFile": "${workspaceFolder}/.env" + }, + { + "name": "Vite", + "request": "launch", + "type": "node", + "program": "${workspaceFolder}/node_modules/vitest/vitest.mjs", + "args": [ + "--run", + "--inspect-brk", + "--no-file-parallelism", + "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts" + ], + "sourceMaps": true, + "resolveSourceMapLocations": [ + "${workspaceFolder}/**", + "**/node_modules/**" + ], + "outFiles": [ + "${workspaceFolder}/dist/**/*.*", + "${workspaceFolder}/node_modules/**/*.*" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "cwd": "${workspaceRoot}", + "env": { + "NODE_ENV": "test" + }, + "envFile": "${workspaceRoot}/.env", } ] } From 9fde747cf88d7e838699ed4b309a3832fa9212fd Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:33:12 -0500 Subject: [PATCH 06/21] Implement helper and tests --- .../src/lib/addresses/addressesContainsAll.ts | 14 ++++--- .../spec/addressesContainsAll.spec.ts | 42 ++++++++++++------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts index 09504be11a..72a4a907f2 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts @@ -1,8 +1,12 @@ import type { Address } from '@xylabs/hex' -import type { BoundWitnessFields } from '@xyo-network/boundwitness-model' -import { BoundWitness } from '@xyo-network/boundwitness-model' +import type { BoundWitness } from '@xyo-network/boundwitness-model' -export const addressesContainsAll = (boundwitness: Partial, addresses: Address[]): boolean => { - // TODO - return false +/** + * Checks if the boundwitness contains all the addresses + * @param bw The boundwitness to check + * @param addresses The addresses to check for + * @returns True if the boundwitness contains all the addresses + */ +export const addressesContainsAll = (bw: BoundWitness, addresses: Address[]): boolean => { + return addresses.every(address => bw.addresses?.includes(address)) } diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 9d35d7313a..4fbf5f5e5c 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -1,3 +1,4 @@ +import type { Address } from '@xylabs/hex' import type { WalletInstance } from '@xyo-network/account' import { HDWallet } from '@xyo-network/account' import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' @@ -6,33 +7,42 @@ import { addressesContainsAll } from '../addressesContainsAll.ts' describe('BoundWitnessValidator', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } - const cases: [string, WalletInstance[]][] = [ - ['with no signer', [] as WalletInstance[]], - ['with single signer', [] as WalletInstance[]], - ['with multiple signers', [] as WalletInstance[]], + const oneWallet: WalletInstance[] = [] + const twoWallets: WalletInstance[] = [] + const cases: [string, () => WalletInstance[]][] = [ + ['with single signer', () => oneWallet], + ['with multiple signers', () => twoWallets], ] beforeAll(async () => { - cases[1][1] = [await HDWallet.random()] - cases[2][1] = [await HDWallet.random(), await HDWallet.random()] + oneWallet.push(await HDWallet.random()) + twoWallets.push(await HDWallet.random(), await HDWallet.random()) }) describe.each(cases)('%s', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) describe('returns true', () => { - it('when all addresses present', async () => { - const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() - expect(addressesContainsAll(bw, signers)).toBeTrue() + it('when all addresses present in boundwitness', async () => { + const [bw] = await new BoundWitnessBuilder().signers(signers()).payload(payload).build() + expect(addressesContainsAll(bw, addresses)).toBeTrue() }) - it('when extra addresses present', async () => { - const extra = [...signers, await HDWallet.random()] + it('when extra addresses present in boundwitness', async () => { + const extra = [...signers(), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() - expect(addressesContainsAll(bw, signers)).toBeTrue() + expect(addressesContainsAll(bw, addresses)).toBeTrue() }) }) describe('returns false', () => { - it('when single address missing', async () => { - // + it('when one address not present in boundwitness', async () => { + const less = [...signers().slice(0, -1), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() + expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('when all addresses missing', async () => { - // + it('when all addresses not present in boundwitness', async () => { + const none = [await HDWallet.random(), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() + expect(addressesContainsAll(bw, addresses)).toBeFalse() }) }) }) From e3fce1e3cf5469e83ef54408e13f38f371d2a04e Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 20:40:43 -0500 Subject: [PATCH 07/21] Updated test cases --- .../spec/addressesContainsAll.spec.ts | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 4fbf5f5e5c..624a338bc0 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -23,23 +23,33 @@ describe('BoundWitnessValidator', () => { addresses = signers().map(x => x.address) }) describe('returns true', () => { - it('when all addresses present in boundwitness', async () => { - const [bw] = await new BoundWitnessBuilder().signers(signers()).payload(payload).build() + it('when addresses empty', async () => { + const all = signers() + const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + expect(addressesContainsAll(bw, [])).toBeTrue() + }) + it('when all addresses present in boundwitness addresses', async () => { + const all = signers() + const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeTrue() }) - it('when extra addresses present in boundwitness', async () => { + it('when all addresses present in boundwitness addresses with extra addresses', async () => { const extra = [...signers(), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeTrue() }) }) describe('returns false', () => { - it('when one address not present in boundwitness', async () => { + it('when no addresses in boundwitness addresses', async () => { + const [bw] = await new BoundWitnessBuilder().payload(payload).build() + expect(addressesContainsAll(bw, addresses)).toBeFalse() + }) + it('when missing one address in boundwitness addresses', async () => { const less = [...signers().slice(0, -1), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('when all addresses not present in boundwitness', async () => { + it('when missing all addresses in boundwitness addresses', async () => { const none = [await HDWallet.random(), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() From 9626a38079f8572d009f9ba6b3c8c174ef9d711b Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:03:36 -0500 Subject: [PATCH 08/21] Update descriptions --- .../spec/addressesContainsAll.spec.ts | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 624a338bc0..2c370133b8 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -9,47 +9,59 @@ describe('BoundWitnessValidator', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } const oneWallet: WalletInstance[] = [] const twoWallets: WalletInstance[] = [] - const cases: [string, () => WalletInstance[]][] = [ - ['with single signer', () => oneWallet], - ['with multiple signers', () => twoWallets], - ] beforeAll(async () => { oneWallet.push(await HDWallet.random()) twoWallets.push(await HDWallet.random(), await HDWallet.random()) }) - describe.each(cases)('%s', (_, signers) => { - let addresses: Address[] - beforeAll(() => { - addresses = signers().map(x => x.address) - }) - describe('returns true', () => { - it('when addresses empty', async () => { + describe('returns true', () => { + const cases: [string, () => WalletInstance[]][] = [ + ['for no wallet', () => []], + ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s when all sign', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + it('addresses empty', async () => { const all = signers() const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, [])).toBeTrue() }) - it('when all addresses present in boundwitness addresses', async () => { + it('all addresses present in boundwitness addresses', async () => { const all = signers() const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeTrue() }) - it('when all addresses present in boundwitness addresses with extra addresses', async () => { + it('with extra signers and all addresses present in boundwitness addresses', async () => { const extra = [...signers(), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeTrue() }) }) - describe('returns false', () => { - it('when no addresses in boundwitness addresses', async () => { + }) + describe('returns false', () => { + const cases: [string, () => WalletInstance[]][] = [ + // ['for no wallet', () => []], // This case intentionally skipped because empty addresses will always return true + ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + it('with no signers', async () => { const [bw] = await new BoundWitnessBuilder().payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('when missing one address in boundwitness addresses', async () => { + it('with all signers but one', async () => { const less = [...signers().slice(0, -1), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('when missing all addresses in boundwitness addresses', async () => { + it('with all different signers', async () => { const none = [await HDWallet.random(), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() From 9fa28eb4a7133b2fe6c91d1f08ee8b82e17da7f7 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:06:29 -0500 Subject: [PATCH 09/21] addressesContainsAny --- .../src/lib/addresses/addressesContainsAll.ts | 4 ++-- .../src/lib/addresses/addressesContainsAny.ts | 12 ++++++++++++ .../packages/validator/src/lib/addresses/index.ts | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts index 72a4a907f2..5d44fe81bc 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts @@ -2,10 +2,10 @@ import type { Address } from '@xylabs/hex' import type { BoundWitness } from '@xyo-network/boundwitness-model' /** - * Checks if the boundwitness contains all the addresses + * Checks if the boundwitness contains all of the addresses * @param bw The boundwitness to check * @param addresses The addresses to check for - * @returns True if the boundwitness contains all the addresses + * @returns True if the boundwitness contains all of the addresses */ export const addressesContainsAll = (bw: BoundWitness, addresses: Address[]): boolean => { return addresses.every(address => bw.addresses?.includes(address)) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts new file mode 100644 index 0000000000..134a9e55b4 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts @@ -0,0 +1,12 @@ +import type { Address } from '@xylabs/hex' +import type { BoundWitness } from '@xyo-network/boundwitness-model' + +/** + * Checks if the boundwitness contains any of the addresses + * @param bw The boundwitness to check + * @param addresses The addresses to check for + * @returns True if the boundwitness contains all the addresses + */ +export const addressesContainsAny = (bw: BoundWitness, addresses: Address[]): boolean => { + return addresses.some(address => bw.addresses?.includes(address)) +} diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts index d63361bbc2..818d460193 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts @@ -1 +1,2 @@ export * from './addressesContainsAll.ts' +export * from './addressesContainsAny.ts' From 3deb5ebf41804a51a197ed3791635bf572b8208f Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:19:26 -0500 Subject: [PATCH 10/21] Add tests --- .vscode/launch.json | 2 +- .../spec/addressesContainsAll.spec.ts | 2 +- .../spec/addressesContainsAny.spec.ts | 71 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts diff --git a/.vscode/launch.json b/.vscode/launch.json index 7ee84db66c..d3f4591213 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,7 +26,7 @@ "--run", "--inspect-brk", "--no-file-parallelism", - "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts" + "packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts" ], "sourceMaps": true, "resolveSourceMapLocations": [ diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 2c370133b8..4307177e9f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -24,7 +24,7 @@ describe('BoundWitnessValidator', () => { beforeAll(() => { addresses = signers().map(x => x.address) }) - it('addresses empty', async () => { + it.skip('addresses empty', async () => { const all = signers() const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, [])).toBeTrue() diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts new file mode 100644 index 0000000000..5c41231a05 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -0,0 +1,71 @@ +import type { Address } from '@xylabs/hex' +import type { WalletInstance } from '@xyo-network/account' +import { HDWallet } from '@xyo-network/account' +import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' + +import { addressesContainsAny } from '../addressesContainsAny.ts' + +describe('BoundWitnessValidator', () => { + const payload = { schema: 'network.xyo.test', value: Date.now() } + const oneWallet: WalletInstance[] = [] + const twoWallets: WalletInstance[] = [] + beforeAll(async () => { + oneWallet.push(await HDWallet.random()) + twoWallets.push(await HDWallet.random(), await HDWallet.random()) + }) + describe('returns true', () => { + const cases: [string, () => WalletInstance[]][] = [ + // ['for no wallet', () => []], // This case intentionally skipped because empty addresses will always return false + ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s when all sign', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + // it('addresses empty', async () => { + // const all = signers() + // const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + // expect(addressesContainsAny(bw, [])).toBeTrue() + // }) + it('all addresses present in boundwitness addresses', async () => { + const all = signers() + const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeTrue() + }) + it('with extra signers and all addresses present in boundwitness addresses', async () => { + const extra = [...signers(), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeTrue() + }) + }) + }) + describe('returns false', () => { + const cases: [string, () => WalletInstance[]][] = [ + // ['for no wallet', () => []], + // ['for single wallet', () => oneWallet], + ['for multiple wallets', () => twoWallets], + ] + describe.each(cases)('%s', (_, signers) => { + let addresses: Address[] + beforeAll(() => { + addresses = signers().map(x => x.address) + }) + it('with no signers', async () => { + const [bw] = await new BoundWitnessBuilder().payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + it.only('with all signers but one', async () => { + const less = [...signers().slice(0, -1), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + it('with all different signers', async () => { + const none = [await HDWallet.random(), await HDWallet.random()] + const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() + expect(addressesContainsAny(bw, addresses)).toBeFalse() + }) + }) + }) +}) From b4a2c099719f6b4cd7fe15049cba65575e095a5d Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:25:04 -0500 Subject: [PATCH 11/21] Unskip tests --- .../src/lib/addresses/spec/addressesContainsAll.spec.ts | 8 ++++---- .../src/lib/addresses/spec/addressesContainsAny.spec.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 4307177e9f..75915ace2f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -15,16 +15,16 @@ describe('BoundWitnessValidator', () => { }) describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ - ['for no wallet', () => []], - ['for single wallet', () => oneWallet], - ['for multiple wallets', () => twoWallets], + ['with no signer', () => []], + ['with single signer', () => oneWallet], + ['with multiple signer', () => twoWallets], ] describe.each(cases)('%s when all sign', (_, signers) => { let addresses: Address[] beforeAll(() => { addresses = signers().map(x => x.address) }) - it.skip('addresses empty', async () => { + it('addresses empty', async () => { const all = signers() const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() expect(addressesContainsAll(bw, [])).toBeTrue() diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index 5c41231a05..0917ae5dde 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -56,7 +56,7 @@ describe('BoundWitnessValidator', () => { const [bw] = await new BoundWitnessBuilder().payload(payload).build() expect(addressesContainsAny(bw, addresses)).toBeFalse() }) - it.only('with all signers but one', async () => { + it('with all signers but one', async () => { const less = [...signers().slice(0, -1), await HDWallet.random()] const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() expect(addressesContainsAny(bw, addresses)).toBeFalse() From c3768bb104abfd067b0cd05f5cba29f6d537c248 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:33:55 -0500 Subject: [PATCH 12/21] Update test readability --- .../spec/addressesContainsAll.spec.ts | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 75915ace2f..65ce1b5066 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -5,65 +5,69 @@ import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' import { addressesContainsAll } from '../addressesContainsAll.ts' -describe('BoundWitnessValidator', () => { +describe('addressesContainsAll', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } - const oneWallet: WalletInstance[] = [] - const twoWallets: WalletInstance[] = [] + let oneWallet: WalletInstance[] = [] + let twoWallets: WalletInstance[] = [] + beforeAll(async () => { - oneWallet.push(await HDWallet.random()) - twoWallets.push(await HDWallet.random(), await HDWallet.random()) + oneWallet = [await HDWallet.random()] + twoWallets = [await HDWallet.random(), await HDWallet.random()] }) + + const buildBoundWitness = async (signers: WalletInstance[]) => { + const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() + return bw + } + describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ - ['with no signer', () => []], - ['with single signer', () => oneWallet], - ['with multiple signer', () => twoWallets], + ['with no wallets', () => []], + ['with single wallet', () => oneWallet], + ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s when all sign', (_, signers) => { + describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { - addresses = signers().map(x => x.address) + addresses = wallets().map(x => x.address) }) - it('addresses empty', async () => { - const all = signers() - const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + it('with no signers and empty addresses supplied', async () => { + const bw = await buildBoundWitness(wallets()) expect(addressesContainsAll(bw, [])).toBeTrue() }) - it('all addresses present in boundwitness addresses', async () => { - const all = signers() - const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + it('with all wallets as signers and all wallet addresses supplied', async () => { + const bw = await buildBoundWitness(wallets()) expect(addressesContainsAll(bw, addresses)).toBeTrue() }) - it('with extra signers and all addresses present in boundwitness addresses', async () => { - const extra = [...signers(), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() + it('with all wallets (and extra wallets) as signers and all wallet addresses supplied', async () => { + const extraSigners = [...wallets(), await HDWallet.random()] + const bw = await buildBoundWitness(extraSigners) expect(addressesContainsAll(bw, addresses)).toBeTrue() }) }) }) describe('returns false', () => { const cases: [string, () => WalletInstance[]][] = [ - // ['for no wallet', () => []], // This case intentionally skipped because empty addresses will always return true - ['for single wallet', () => oneWallet], - ['for multiple wallets', () => twoWallets], + ['with single wallet', () => oneWallet], + ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s', (_, signers) => { + describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { - addresses = signers().map(x => x.address) + addresses = wallets().map(x => x.address) }) - it('with no signers', async () => { + it('with no signers and all wallet addresses supplied', async () => { const [bw] = await new BoundWitnessBuilder().payload(payload).build() expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('with all signers but one', async () => { - const less = [...signers().slice(0, -1), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() + it('with all signers except one and all wallet addresses supplied', async () => { + const lessSigners = [...wallets().slice(0, -1), await HDWallet.random()] + const bw = await buildBoundWitness(lessSigners) expect(addressesContainsAll(bw, addresses)).toBeFalse() }) - it('with all different signers', async () => { - const none = [await HDWallet.random(), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() + it('with all different signers and all wallet addresses supplied', async () => { + const differentSigners = [await HDWallet.random(), await HDWallet.random()] + const bw = await buildBoundWitness(differentSigners) expect(addressesContainsAll(bw, addresses)).toBeFalse() }) }) From 6141c7c1b8584c8ef377f5bd547fa2e75492c716 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:38:02 -0500 Subject: [PATCH 13/21] Tests for containsAny --- .../spec/addressesContainsAny.spec.ts | 91 +++++++++++-------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index 0917ae5dde..2b20bc61e7 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -5,66 +5,85 @@ import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' import { addressesContainsAny } from '../addressesContainsAny.ts' -describe('BoundWitnessValidator', () => { +describe('addressesContainsAny', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } - const oneWallet: WalletInstance[] = [] - const twoWallets: WalletInstance[] = [] + let oneWallet: WalletInstance[] = [] + let twoWallets: WalletInstance[] = [] + beforeAll(async () => { - oneWallet.push(await HDWallet.random()) - twoWallets.push(await HDWallet.random(), await HDWallet.random()) + oneWallet = [await HDWallet.random()] + twoWallets = [await HDWallet.random(), await HDWallet.random()] }) + + const buildBoundWitness = async (signers: WalletInstance[]) => { + const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() + return bw + } + describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ - // ['for no wallet', () => []], // This case intentionally skipped because empty addresses will always return false - ['for single wallet', () => oneWallet], - ['for multiple wallets', () => twoWallets], + ['with no wallets', () => []], + ['with single wallet', () => oneWallet], + ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s when all sign', (_, signers) => { + + describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { - addresses = signers().map(x => x.address) + addresses = wallets().map(x => x.address) }) - // it('addresses empty', async () => { - // const all = signers() - // const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() - // expect(addressesContainsAny(bw, [])).toBeTrue() - // }) - it('all addresses present in boundwitness addresses', async () => { - const all = signers() - const [bw] = await new BoundWitnessBuilder().signers(all).payload(payload).build() + + it('with no signers and empty addresses supplied', async () => { + const bw = await buildBoundWitness(wallets()) + expect(addressesContainsAny(bw, [])).toBeTrue() + }) + + it('with all wallets as signers and all wallet addresses supplied', async () => { + const bw = await buildBoundWitness(wallets()) expect(addressesContainsAny(bw, addresses)).toBeTrue() }) - it('with extra signers and all addresses present in boundwitness addresses', async () => { - const extra = [...signers(), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(extra).payload(payload).build() + + it('with all wallets (and extra wallets) as signers and all wallet addresses supplied', async () => { + const extraSigners = [...wallets(), await HDWallet.random()] + const bw = await buildBoundWitness(extraSigners) expect(addressesContainsAny(bw, addresses)).toBeTrue() }) + + it('with only one matching signer', async () => { + const extraSigners = [await HDWallet.random(), ...wallets()] + const bw = await buildBoundWitness(extraSigners) + expect(addressesContainsAny(bw, addresses)).toBeTrue() // Only one wallet needs to match + }) }) }) + describe('returns false', () => { const cases: [string, () => WalletInstance[]][] = [ - // ['for no wallet', () => []], - // ['for single wallet', () => oneWallet], - ['for multiple wallets', () => twoWallets], + ['with single wallet', () => oneWallet], + ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s', (_, signers) => { + + describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { - addresses = signers().map(x => x.address) + addresses = wallets().map(x => x.address) }) - it('with no signers', async () => { + + it('with no signers and all wallet addresses supplied', async () => { const [bw] = await new BoundWitnessBuilder().payload(payload).build() - expect(addressesContainsAny(bw, addresses)).toBeFalse() + expect(addressesContainsAny(bw, addresses)).toBeFalse() // No signers at all }) - it('with all signers but one', async () => { - const less = [...signers().slice(0, -1), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(less).payload(payload).build() - expect(addressesContainsAny(bw, addresses)).toBeFalse() + + it('with signers that do not include any of the wallet addresses supplied', async () => { + const differentSigners = [await HDWallet.random(), await HDWallet.random()] + const bw = await buildBoundWitness(differentSigners) + expect(addressesContainsAny(bw, addresses)).toBeFalse() // None of the wallet addresses are in the bound witness }) - it('with all different signers', async () => { - const none = [await HDWallet.random(), await HDWallet.random()] - const [bw] = await new BoundWitnessBuilder().signers(none).payload(payload).build() - expect(addressesContainsAny(bw, addresses)).toBeFalse() + + it('with extra signers and no wallet addresses supplied', async () => { + const extraSigners = [await HDWallet.random(), await HDWallet.random()] + const bw = await buildBoundWitness(extraSigners) + expect(addressesContainsAny(bw, addresses)).toBeFalse() // None of the supplied addresses match }) }) }) From 1516e0f073383f8a60b0408c231c1a12585000e1 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:41:04 -0500 Subject: [PATCH 14/21] Alternative test flow --- .../spec/addressesContainsAny.spec.ts | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index 2b20bc61e7..e445e56f7f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -9,77 +9,64 @@ describe('addressesContainsAny', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } let oneWallet: WalletInstance[] = [] let twoWallets: WalletInstance[] = [] - beforeAll(async () => { oneWallet = [await HDWallet.random()] twoWallets = [await HDWallet.random(), await HDWallet.random()] }) - const buildBoundWitness = async (signers: WalletInstance[]) => { const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() return bw } - describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ ['with no wallets', () => []], ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { addresses = wallets().map(x => x.address) }) - it('with no signers and empty addresses supplied', async () => { - const bw = await buildBoundWitness(wallets()) - expect(addressesContainsAny(bw, [])).toBeTrue() + const bw = await buildBoundWitness([]) + expect(addressesContainsAny(bw, [])).toBeFalse() // No signers and no addresses should return false }) - it('with all wallets as signers and all wallet addresses supplied', async () => { const bw = await buildBoundWitness(wallets()) - expect(addressesContainsAny(bw, addresses)).toBeTrue() + expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should be true if wallets exist }) - it('with all wallets (and extra wallets) as signers and all wallet addresses supplied', async () => { const extraSigners = [...wallets(), await HDWallet.random()] const bw = await buildBoundWitness(extraSigners) - expect(addressesContainsAny(bw, addresses)).toBeTrue() + expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should still be true with extras }) - it('with only one matching signer', async () => { const extraSigners = [await HDWallet.random(), ...wallets()] const bw = await buildBoundWitness(extraSigners) - expect(addressesContainsAny(bw, addresses)).toBeTrue() // Only one wallet needs to match + expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should return true with one match }) }) }) - describe('returns false', () => { const cases: [string, () => WalletInstance[]][] = [ ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] - describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { addresses = wallets().map(x => x.address) }) - it('with no signers and all wallet addresses supplied', async () => { const [bw] = await new BoundWitnessBuilder().payload(payload).build() - expect(addressesContainsAny(bw, addresses)).toBeFalse() // No signers at all + expect(addressesContainsAny(bw, addresses)).toBeFalse() // No signers, no match }) - it('with signers that do not include any of the wallet addresses supplied', async () => { const differentSigners = [await HDWallet.random(), await HDWallet.random()] const bw = await buildBoundWitness(differentSigners) expect(addressesContainsAny(bw, addresses)).toBeFalse() // None of the wallet addresses are in the bound witness }) - it('with extra signers and no wallet addresses supplied', async () => { const extraSigners = [await HDWallet.random(), await HDWallet.random()] const bw = await buildBoundWitness(extraSigners) From eab65ce0bd1e6d2ee8c064c311d9b70ff84c6338 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:42:33 -0500 Subject: [PATCH 15/21] Update tests for any contains BW addresses --- .../src/lib/addresses/spec/addressesContainsAny.spec.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index e445e56f7f..609a5f8e4f 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -19,7 +19,7 @@ describe('addressesContainsAny', () => { } describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ - ['with no wallets', () => []], + // ['with no wallets', () => []], ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] @@ -34,22 +34,23 @@ describe('addressesContainsAny', () => { }) it('with all wallets as signers and all wallet addresses supplied', async () => { const bw = await buildBoundWitness(wallets()) - expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should be true if wallets exist + expect(addressesContainsAny(bw, addresses)).toBeTrue() // Should be true if wallets exist }) it('with all wallets (and extra wallets) as signers and all wallet addresses supplied', async () => { const extraSigners = [...wallets(), await HDWallet.random()] const bw = await buildBoundWitness(extraSigners) - expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should still be true with extras + expect(addressesContainsAny(bw, addresses)).toBeTrue() // Should still be true with extras }) it('with only one matching signer', async () => { const extraSigners = [await HDWallet.random(), ...wallets()] const bw = await buildBoundWitness(extraSigners) - expect(addressesContainsAny(bw, addresses)).toBe(wallets().length > 0) // Should return true with one match + expect(addressesContainsAny(bw, addresses)).toBeTrue() // Should return true with one match }) }) }) describe('returns false', () => { const cases: [string, () => WalletInstance[]][] = [ + ['with no wallets', () => []], ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] From 036d15c448b17f8d6801f81938066cf5d7b5f748 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:44:48 -0500 Subject: [PATCH 16/21] Add addressesContainsAddress helper --- .../src/lib/addresses/addressesContainsAddress.ts | 12 ++++++++++++ .../packages/validator/src/lib/addresses/index.ts | 1 + 2 files changed, 13 insertions(+) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAddress.ts diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAddress.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAddress.ts new file mode 100644 index 0000000000..033cd929c7 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAddress.ts @@ -0,0 +1,12 @@ +import type { Address } from '@xylabs/hex' +import type { BoundWitness } from '@xyo-network/boundwitness-model' + +/** + * Checks if the boundwitness contains the addresses + * @param bw The boundwitness to check + * @param addresses The address to check for + * @returns True if the boundwitness contains the addresses + */ +export const addressesContainsAddress = (bw: BoundWitness, address: Address): boolean => { + return bw.addresses?.includes(address) +} diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts index 818d460193..270d5b9cd4 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts @@ -1,2 +1,3 @@ +export * from './addressesContainsAddress.ts' export * from './addressesContainsAll.ts' export * from './addressesContainsAny.ts' From 59db5533fbf0e467f95c95dd21ea56302011e031 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 23 Sep 2024 21:46:00 -0500 Subject: [PATCH 17/21] import type addressesContainsAddress --- .../spec/addressesContainsAddress.spec.ts | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts new file mode 100644 index 0000000000..95d21cdf35 --- /dev/null +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts @@ -0,0 +1,62 @@ +import type { WalletInstance } from '@xyo-network/account' +import { HDWallet } from '@xyo-network/account' +import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' + +import { addressesContainsAddress } from '../addressesContainsAddress.ts' + +describe('BoundWitnessValidator - addressesContainsAddress', () => { + const payload = { schema: 'network.xyo.test', value: Date.now() } + let oneWallet: WalletInstance[] = [] + let twoWallets: WalletInstance[] = [] + + beforeAll(async () => { + oneWallet = [await HDWallet.random()] + twoWallets = [await HDWallet.random(), await HDWallet.random()] + }) + + const buildBoundWitness = async (signers: WalletInstance[]) => { + const [bw] = await new BoundWitnessBuilder().signers(signers).payload(payload).build() + return bw + } + + describe('returns true', () => { + it('with a single wallet and the address is present', async () => { + const bw = await buildBoundWitness(oneWallet) + const address = oneWallet[0].address + expect(addressesContainsAddress(bw, address)).toBeTrue() + }) + + it('with multiple wallets and the address is present', async () => { + const bw = await buildBoundWitness(twoWallets) + const address = twoWallets[0].address + expect(addressesContainsAddress(bw, address)).toBeTrue() + }) + + it('with extra signers and the address is still present', async () => { + const extraSigners = [...twoWallets, await HDWallet.random()] + const bw = await buildBoundWitness(extraSigners) + const address = twoWallets[1].address + expect(addressesContainsAddress(bw, address)).toBeTrue() + }) + }) + + describe('returns false', () => { + it('with no signers', async () => { + const [bw] = await new BoundWitnessBuilder().payload(payload).build() + const address = oneWallet[0].address + expect(addressesContainsAddress(bw, address)).toBeFalse() + }) + + it('with a single wallet but a different address is checked', async () => { + const bw = await buildBoundWitness(oneWallet) + const randomAddress = (await HDWallet.random()).address + expect(addressesContainsAddress(bw, randomAddress)).toBeFalse() + }) + + it('with multiple wallets but a non-existing address is checked', async () => { + const bw = await buildBoundWitness(twoWallets) + const randomAddress = (await HDWallet.random()).address + expect(addressesContainsAddress(bw, randomAddress)).toBeFalse() + }) + }) +}) From 7538b998dcab24bbd1127a7be3c9c940e85f2cb8 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Tue, 24 Sep 2024 07:05:23 -0500 Subject: [PATCH 18/21] Update test name --- .../src/lib/addresses/spec/addressesContainsAddress.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts index 95d21cdf35..df72648231 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts @@ -4,7 +4,7 @@ import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder' import { addressesContainsAddress } from '../addressesContainsAddress.ts' -describe('BoundWitnessValidator - addressesContainsAddress', () => { +describe('addressesContainsAddress', () => { const payload = { schema: 'network.xyo.test', value: Date.now() } let oneWallet: WalletInstance[] = [] let twoWallets: WalletInstance[] = [] From cf6b84a947a23ed6b284d6b7819d00385c7e2d88 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Tue, 24 Sep 2024 10:07:27 -0500 Subject: [PATCH 19/21] Better test cases for edge cases --- .../src/lib/addresses/spec/addressesContainsAll.spec.ts | 6 +++++- .../src/lib/addresses/spec/addressesContainsAny.spec.ts | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts index 65ce1b5066..33d66b1a30 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts @@ -26,12 +26,16 @@ describe('addressesContainsAll', () => { ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] + it('with no signers and empty addresses supplied', async () => { + const bw = await buildBoundWitness([]) + expect(addressesContainsAll(bw, [])).toBeTrue() + }) describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { addresses = wallets().map(x => x.address) }) - it('with no signers and empty addresses supplied', async () => { + it('with all wallets as signers and empty addresses supplied', async () => { const bw = await buildBoundWitness(wallets()) expect(addressesContainsAll(bw, [])).toBeTrue() }) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index 609a5f8e4f..31e0e9830d 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -23,15 +23,15 @@ describe('addressesContainsAny', () => { ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] + it('with no signers and empty addresses supplied', async () => { + const bw = await buildBoundWitness([]) + expect(addressesContainsAny(bw, [])).toBeFalse() // No signers and no addresses should return false + }) describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] beforeAll(() => { addresses = wallets().map(x => x.address) }) - it('with no signers and empty addresses supplied', async () => { - const bw = await buildBoundWitness([]) - expect(addressesContainsAny(bw, [])).toBeFalse() // No signers and no addresses should return false - }) it('with all wallets as signers and all wallet addresses supplied', async () => { const bw = await buildBoundWitness(wallets()) expect(addressesContainsAny(bw, addresses)).toBeTrue() // Should be true if wallets exist From ab701893d899f935bb63fe6325d9e42f0676bddf Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Tue, 24 Sep 2024 10:08:14 -0500 Subject: [PATCH 20/21] test title --- .../src/lib/addresses/spec/addressesContainsAddress.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts index df72648231..efe2e6c798 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts @@ -32,7 +32,7 @@ describe('addressesContainsAddress', () => { expect(addressesContainsAddress(bw, address)).toBeTrue() }) - it('with extra signers and the address is still present', async () => { + it('with extra signers and the address is present', async () => { const extraSigners = [...twoWallets, await HDWallet.random()] const bw = await buildBoundWitness(extraSigners) const address = twoWallets[1].address From cad35265a056dc3d426e27c2abac5880cf06ccf5 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Tue, 24 Sep 2024 10:15:14 -0500 Subject: [PATCH 21/21] Update illogical edge cases to return logical results --- .../validator/src/lib/addresses/addressesContainsAny.ts | 7 +++++-- .../src/lib/addresses/spec/addressesContainsAny.spec.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts index 134a9e55b4..1b8338e14e 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts @@ -2,11 +2,14 @@ import type { Address } from '@xylabs/hex' import type { BoundWitness } from '@xyo-network/boundwitness-model' /** - * Checks if the boundwitness contains any of the addresses + * Checks if the boundwitness contains any of the addresses. If the addresses array + * is empty, it will return true. This is to match the behavior or addressesContainsAll + * which will return true if the addresses array is empty. * @param bw The boundwitness to check * @param addresses The addresses to check for - * @returns True if the boundwitness contains all the addresses + * @returns True if the boundwitness contains any of the addresses */ export const addressesContainsAny = (bw: BoundWitness, addresses: Address[]): boolean => { + if (addresses.length === 0) return true return addresses.some(address => bw.addresses?.includes(address)) } diff --git a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts index 31e0e9830d..6e3039d3cb 100644 --- a/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts +++ b/packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts @@ -19,13 +19,13 @@ describe('addressesContainsAny', () => { } describe('returns true', () => { const cases: [string, () => WalletInstance[]][] = [ - // ['with no wallets', () => []], + ['with no wallets', () => []], ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ] it('with no signers and empty addresses supplied', async () => { const bw = await buildBoundWitness([]) - expect(addressesContainsAny(bw, [])).toBeFalse() // No signers and no addresses should return false + expect(addressesContainsAny(bw, [])).toBeTrue() // No signers and no addresses should return true }) describe.each(cases)('%s', (_, wallets) => { let addresses: Address[] @@ -50,7 +50,7 @@ describe('addressesContainsAny', () => { }) describe('returns false', () => { const cases: [string, () => WalletInstance[]][] = [ - ['with no wallets', () => []], + // ['with no wallets', () => []], ['with single wallet', () => oneWallet], ['with multiple wallets', () => twoWallets], ]