-
Notifications
You must be signed in to change notification settings - Fork 15
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 #1228 from XYOracleNetwork/feature/bound-witness-h…
…elpers BoundWitness Helpers
- Loading branch information
Showing
14 changed files
with
310 additions
and
2 deletions.
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
1 change: 1 addition & 0 deletions
1
packages/protocol/packages/boundwitness/packages/validator/src/index.ts
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './lib/index.ts' | ||
export * from './Validator.ts' |
12 changes: 12 additions & 0 deletions
12
...ol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAddress.ts
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,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) | ||
} |
12 changes: 12 additions & 0 deletions
12
...otocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAll.ts
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,12 @@ | ||
import type { Address } from '@xylabs/hex' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
|
||
/** | ||
* 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 of the addresses | ||
*/ | ||
export const addressesContainsAll = (bw: BoundWitness, addresses: Address[]): boolean => { | ||
return addresses.every(address => bw.addresses?.includes(address)) | ||
} |
15 changes: 15 additions & 0 deletions
15
...otocol/packages/boundwitness/packages/validator/src/lib/addresses/addressesContainsAny.ts
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,15 @@ | ||
import type { Address } from '@xylabs/hex' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
|
||
/** | ||
* 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 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)) | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/protocol/packages/boundwitness/packages/validator/src/lib/addresses/index.ts
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,3 @@ | ||
export * from './addressesContainsAddress.ts' | ||
export * from './addressesContainsAll.ts' | ||
export * from './addressesContainsAny.ts' |
62 changes: 62 additions & 0 deletions
62
...s/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAddress.spec.ts
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,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('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 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() | ||
}) | ||
}) | ||
}) |
79 changes: 79 additions & 0 deletions
79
...kages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAll.spec.ts
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,79 @@ | ||
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 { addressesContainsAll } from '../addressesContainsAll.ts' | ||
|
||
describe('addressesContainsAll', () => { | ||
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], | ||
] | ||
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 all wallets as signers and empty addresses supplied', async () => { | ||
const bw = await buildBoundWitness(wallets()) | ||
expect(addressesContainsAll(bw, [])).toBeTrue() | ||
}) | ||
it('with all wallets as signers and all wallet addresses supplied', async () => { | ||
const bw = await buildBoundWitness(wallets()) | ||
expect(addressesContainsAll(bw, addresses)).toBeTrue() | ||
}) | ||
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[]][] = [ | ||
['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(addressesContainsAll(bw, addresses)).toBeFalse() | ||
}) | ||
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 and all wallet addresses supplied', async () => { | ||
const differentSigners = [await HDWallet.random(), await HDWallet.random()] | ||
const bw = await buildBoundWitness(differentSigners) | ||
expect(addressesContainsAll(bw, addresses)).toBeFalse() | ||
}) | ||
}) | ||
}) | ||
}) |
78 changes: 78 additions & 0 deletions
78
...kages/boundwitness/packages/validator/src/lib/addresses/spec/addressesContainsAny.spec.ts
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,78 @@ | ||
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('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], | ||
] | ||
it('with no signers and empty addresses supplied', async () => { | ||
const bw = await buildBoundWitness([]) | ||
expect(addressesContainsAny(bw, [])).toBeTrue() // No signers and no addresses should return true | ||
}) | ||
describe.each(cases)('%s', (_, wallets) => { | ||
let addresses: Address[] | ||
beforeAll(() => { | ||
addresses = wallets().map(x => x.address) | ||
}) | ||
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 | ||
}) | ||
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() // 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() // Should return true with one match | ||
}) | ||
}) | ||
}) | ||
describe('returns false', () => { | ||
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 all wallet addresses supplied', async () => { | ||
const [bw] = await new BoundWitnessBuilder().payload(payload).build() | ||
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) | ||
expect(addressesContainsAny(bw, addresses)).toBeFalse() // None of the supplied addresses match | ||
}) | ||
}) | ||
}) | ||
}) |
8 changes: 8 additions & 0 deletions
8
...es/protocol/packages/boundwitness/packages/validator/src/lib/addresses/spec/tsconfig.json
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
|
||
}, | ||
"extends": "@xylabs/tsconfig-jest" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/protocol/packages/boundwitness/packages/validator/src/lib/index.ts
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 @@ | ||
export * from './addresses/index.ts' |
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