-
Notifications
You must be signed in to change notification settings - Fork 0
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 #48 from XYOracleNetwork/feature/escrow-helpers-util
Escrow Secret Validation Helpers
- Loading branch information
Showing
33 changed files
with
242 additions
and
519 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
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,6 @@ | ||
import type { EscrowTermsFields } from './Terms.ts' | ||
|
||
/** | ||
* The party in an escrow transaction | ||
*/ | ||
export type EscrowParty = keyof Pick<EscrowTermsFields, 'buyer' | 'seller'> |
6 changes: 6 additions & 0 deletions
6
packages/payload/packages/payments/src/Escrow/Terms/PartySecret.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,6 @@ | ||
import type { EscrowTermsFields } from './Terms.ts' | ||
|
||
/** | ||
* The party's secret in an escrow transaction | ||
*/ | ||
export type EscrowPartySecret = keyof Pick<EscrowTermsFields, 'buyerSecret' | 'sellerSecret'> |
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,3 @@ | ||
export * from './Party.ts' | ||
export * from './PartySecret.ts' | ||
export * from './Terms.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,6 +1,5 @@ | ||
export * from './createEscrowIntent.ts' | ||
export * from './getEscrowSecret.ts' | ||
export * from './Outcome.ts' | ||
export * from './Schema.ts' | ||
export * from './Terms.ts' | ||
export * from './Terms/index.ts' | ||
export * from './util/index.ts' | ||
export * from './validators/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 './secret/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
47 changes: 47 additions & 0 deletions
47
packages/payload/packages/payments/src/Escrow/util/secret/findEscrowPartySecretSignatures.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,47 @@ | ||
import type { Hash } from '@xylabs/hex' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
import { isBoundWitnessWithMeta } from '@xyo-network/boundwitness-model' | ||
import type { Payload, WithMeta } from '@xyo-network/payload-model' | ||
|
||
import type { | ||
EscrowParty, EscrowPartySecret, EscrowTerms, | ||
} from '../../Terms/index.ts' | ||
/** | ||
* Returns the log prefix for the party | ||
* @param party The party | ||
* @returns The log prefix for the party | ||
*/ | ||
const getLogPrefix = (party: EscrowParty) => { | ||
const partySecret: EscrowPartySecret = party === 'seller' ? 'sellerSecret' : 'buyerSecret' | ||
return `EscrowTerms.${partySecret}` | ||
} | ||
|
||
/** | ||
* Returns an array of BoundWitnesses containing the secret signed by all the parties | ||
* @param terms The escrow terms | ||
* @param dictionary A dictionary of all the payloads associated with the escrow | ||
* @param party The party to get the secret signatures for | ||
* @returns An array of BoundWitnesses containing the secret signed by all the parties | ||
*/ | ||
export const findEscrowPartySecretSignatures = (terms: EscrowTerms, dictionary: Record<Hash, WithMeta<Payload>>, party: EscrowParty): BoundWitness[] => { | ||
const partyAddresses = terms[party] | ||
if (partyAddresses === undefined || partyAddresses.length === 0) { | ||
console.log(`${getLogPrefix(party)}: No ${party}: ${terms[party]}`) | ||
return [] | ||
} | ||
const partySecret: EscrowPartySecret = party === 'seller' ? 'sellerSecret' : 'buyerSecret' | ||
const secretHash = terms[partySecret] | ||
if (secretHash === undefined) { | ||
console.log(`${getLogPrefix(party)}: No ${partySecret}: ${terms[partySecret]}`) | ||
return [] | ||
} | ||
// BWs containing the secret signed by all the parties | ||
const partySignedBWs = Object.values(dictionary) | ||
// Find all BoundWitnesses | ||
.filter(isBoundWitnessWithMeta) | ||
// That contain the seller secret | ||
.filter(bw => bw.payload_hashes.includes(secretHash)) | ||
// That are signed by all the parties | ||
.filter(bw => partyAddresses.every(address => bw.addresses.includes(address))) | ||
return partySignedBWs | ||
} |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
packages/payload/packages/payments/src/Escrow/util/secret/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 './createEscrowIntent.ts' | ||
export * from './findEscrowPartySecretSignatures.ts' | ||
export * from './getEscrowSecret.ts' |
2 changes: 1 addition & 1 deletion
2
...ayments/src/Escrow/validators/common/ModuleInstanceValidators/moduleInstanceValidators.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
6 changes: 3 additions & 3 deletions
6
...c/Escrow/validators/common/ModuleInstanceValidators/spec/moduleInstanceValidators.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
48 changes: 48 additions & 0 deletions
48
...s/payments/src/Escrow/validators/common/SecretValidators/getPartySecretSignedValidator.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,48 @@ | ||
import type { Hash } from '@xylabs/hex' | ||
import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator' | ||
import type { | ||
AsyncPayloadValidationFunction, Payload, WithMeta, | ||
} from '@xyo-network/payload-model' | ||
|
||
import type { | ||
EscrowParty, EscrowPartySecret, EscrowTerms, | ||
} from '../../../Terms/index.ts' | ||
import { findEscrowPartySecretSignatures } from '../../../util/index.ts' | ||
|
||
/** | ||
* Returns the log prefix for the party | ||
* @param party The party | ||
* @returns The log prefix for the party | ||
*/ | ||
const getLogPrefix = (party: EscrowParty) => { | ||
const partySecret: EscrowPartySecret = party === 'seller' ? 'sellerSecret' : 'buyerSecret' | ||
return `EscrowTerms.${partySecret}` | ||
} | ||
|
||
/** | ||
* Returns a function that validates the escrow terms for the existence of the party secret signed by the party | ||
* @param dictionary Payload dictionary of the escrow terms | ||
* @returns A function that validates the escrow terms for the existence of the party secret signed by the party | ||
*/ | ||
export const getPartySecretSignedValidator = (dictionary: Record<Hash, WithMeta<Payload>>, party: EscrowParty): AsyncPayloadValidationFunction<EscrowTerms> => { | ||
const partySecret: EscrowPartySecret = party === 'seller' ? 'sellerSecret' : 'buyerSecret' | ||
return async (terms: EscrowTerms): Promise<boolean> => { | ||
// Party-signed party secret BWs | ||
const buyerSecretBWs = findEscrowPartySecretSignatures(terms, dictionary, party) | ||
|
||
// If there are no BWs, return false | ||
if (buyerSecretBWs.length === 0) { | ||
console.log(`${getLogPrefix(party)}: No BoundWitnesses supplied for ${partySecret}: ${terms[partySecret]}`) | ||
return false | ||
} | ||
|
||
// Ensure each BW supplied for the party secret is valid | ||
const errors = await Promise.all(buyerSecretBWs.map(bw => new BoundWitnessValidator(bw).validate())) | ||
const validBoundWitnesses = errors.every(errors => errors.length === 0) | ||
if (!validBoundWitnesses) { | ||
console.log(`${getLogPrefix(party)}: Invalid BoundWitnesses supplied for ${partySecret}: ${terms[partySecret]}`) | ||
return false | ||
} | ||
return true | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/payload/packages/payments/src/Escrow/validators/common/SecretValidators/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 './getPartySecretSignedValidator.ts' |
85 changes: 85 additions & 0 deletions
85
.../src/Escrow/validators/common/SecretValidators/spec/getPartySecretSignedValidator.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,85 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import { HDWallet } from '@xyo-network/account' | ||
import type { AccountInstance } from '@xyo-network/account-model' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
import type { Id } from '@xyo-network/id-payload-plugin' | ||
import { IdSchema } from '@xyo-network/id-payload-plugin' | ||
import { PayloadBuilder } from '@xyo-network/payload-builder' | ||
import type { Payload } from '@xyo-network/payload-model' | ||
|
||
import type { EscrowParty, EscrowTerms } from '../../../../Terms/index.ts' | ||
import { EscrowTermsSchema } from '../../../../Terms/index.ts' | ||
import { createEscrowIntentWithSecret } from '../../../../util/index.ts' | ||
import { getPartySecretSignedValidator } from '../getPartySecretSignedValidator.ts' | ||
|
||
describe('RegistrarSentinel', () => { | ||
let buyer: AccountInstance | ||
let seller: AccountInstance | ||
const baseTerms: EscrowTerms = { schema: EscrowTermsSchema } | ||
const buyerSecret: Id = { schema: IdSchema, salt: `${Date.now() - 10}` } | ||
let buyerSecretSignature: BoundWitness | ||
const sellerSecret: Id = { schema: IdSchema, salt: `${Date.now() + 10}` } | ||
let sellerSecretSignature: BoundWitness | ||
|
||
describe('getPartySecretSignedValidator', () => { | ||
beforeAll(async () => { | ||
buyer = await HDWallet.random() | ||
seller = await HDWallet.random() | ||
|
||
baseTerms.buyer = [buyer.address] | ||
baseTerms.buyerSecret = await PayloadBuilder.dataHash(buyerSecret) | ||
baseTerms.seller = [seller.address] | ||
baseTerms.sellerSecret = await PayloadBuilder.dataHash(sellerSecret) | ||
|
||
const buyerIntent = await createEscrowIntentWithSecret(baseTerms, buyerSecret, buyer) | ||
const sellerIntent = await createEscrowIntentWithSecret(baseTerms, sellerSecret, seller) | ||
|
||
buyerSecretSignature = buyerIntent[0] | ||
sellerSecretSignature = sellerIntent[0] | ||
}) | ||
const cases: EscrowParty[] = ['buyer', 'seller'] | ||
describe.each(cases)('for %s', (party) => { | ||
describe('returns true', () => { | ||
it('with valid escrow terms and values supplied', async () => { | ||
const payloads = party === 'buyer' ? [baseTerms, buyerSecret, buyerSecretSignature] : [baseTerms, sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(baseTerms) | ||
expect(result).toBeTrue() | ||
}) | ||
}) | ||
describe('returns false', () => { | ||
describe('with invalid escrow terms value for', () => { | ||
describe('secret', () => { | ||
it('undefined', async () => { | ||
const terms = { ...baseTerms, [`${party}Secret`]: undefined } | ||
const payloads = party === 'buyer' ? [terms, buyerSecret, buyerSecretSignature] : [terms, sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(terms) | ||
expect(result).toBeFalse() | ||
}) | ||
it('different from signature', async () => { | ||
const secret: Id = { schema: IdSchema, salt: '0' } | ||
const terms = { ...baseTerms, [`${party}Secret`]: await PayloadBuilder.dataHash(secret) } | ||
const payloads = party === 'buyer' ? [terms, buyerSecret, buyerSecretSignature] : [terms, sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(terms) | ||
expect(result).toBeFalse() | ||
}) | ||
}) | ||
}) | ||
describe('with missing value for', () => { | ||
it('boundwitness', async () => { | ||
const payloads: Payload[] = party === 'buyer' ? [baseTerms, buyerSecret] : [baseTerms, sellerSecret] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(baseTerms) | ||
expect(result).toBeFalse() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
...ayload/packages/payments/src/Escrow/validators/common/SecretValidators/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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"moduleResolution": "NodeNext", | ||
"module": "NodeNext", | ||
"sourceMap": true, | ||
"inlineSources": true | ||
}, | ||
"extends": "@xylabs/tsconfig-jest" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/payload/packages/payments/src/Escrow/validators/common/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,2 +1,3 @@ | ||
export * from './ModuleInstanceValidators/index.ts' | ||
export * from './SecretValidators/index.ts' | ||
export * from './TemporalValidators/index.ts' |
2 changes: 1 addition & 1 deletion
2
packages/payload/packages/payments/src/Escrow/validators/escrow/agent.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
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
2 changes: 1 addition & 1 deletion
2
packages/payload/packages/payments/src/Escrow/validators/escrow/assets.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
2 changes: 1 addition & 1 deletion
2
packages/payload/packages/payments/src/Escrow/validators/escrow/buyer.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
2 changes: 1 addition & 1 deletion
2
packages/payload/packages/payments/src/Escrow/validators/escrow/nbfExp.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
Oops, something went wrong.