-
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.
Validators for EscrowTerms seller secrets
- Loading branch information
1 parent
d8b3409
commit 3283e0e
Showing
2 changed files
with
76 additions
and
0 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
75 changes: 75 additions & 0 deletions
75
packages/payload/packages/payments/src/Escrow/validators/escrow/sellerSecret.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,75 @@ | ||
import { assertEx } from '@xylabs/assert' | ||
import { Hash } from '@xylabs/hex' | ||
import { isBoundWitnessWithMeta } from '@xyo-network/boundwitness-model' | ||
import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator' | ||
import { Payload, WithMeta } from '@xyo-network/payload-model' | ||
|
||
import { EscrowTerms } from '../../Terms' | ||
import { EscrowTermsValidationFunction } from '../types' | ||
|
||
const name = 'EscrowTerms.sellerSecret' | ||
|
||
/** | ||
* Returns a function that validates the escrow terms for sellerSecret | ||
* @returns A function that validates the escrow terms for sellerSecret | ||
*/ | ||
export const sellerSecretExistsValidator: EscrowTermsValidationFunction = (terms: EscrowTerms) => { | ||
// Validate we have sellerSecret | ||
const sellerSecret = terms.sellerSecret | ||
if (!sellerSecret || sellerSecret.length === 0) { | ||
console.log(`${name}: No sellerSecret: ${terms.sellerSecret}`) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Returns a function that validates the escrow terms for the existence of the sellerSecret in the dictionary | ||
* @param dictionary Payload dictionary of the escrow terms | ||
* @returns A function that validates the escrow terms for the existence of the sellerSecret in the dictionary | ||
*/ | ||
export const getSellerSecretSuppliedValidator = (dictionary: Record<Hash, WithMeta<Payload>>): EscrowTermsValidationFunction => { | ||
return (terms: EscrowTerms) => { | ||
const sellerSecret = assertEx(terms.sellerSecret, () => `${name}: No sellerSecret: ${terms.sellerSecret}`) | ||
if (!dictionary[sellerSecret]) { | ||
console.log(`${name}: Payload not supplied for sellerSecret: ${sellerSecret}`) | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
|
||
/** | ||
* Returns a function that validates the escrow terms for the existence of the sellerSecret signed by the seller | ||
* @param dictionary Payload dictionary of the escrow terms | ||
* @returns A function that validates the escrow terms for the existence of the sellerSecret signed by the seller | ||
*/ | ||
export const getSellerSecretSignedValidator = (dictionary: Record<Hash, WithMeta<Payload>>): EscrowTermsValidationFunction => { | ||
return async (terms: EscrowTerms) => { | ||
const seller = assertEx(terms.seller, () => `${name}: No seller: ${terms.seller}`) | ||
const sellerSecret = assertEx(terms.sellerSecret, () => `${name}: No sellerSecret: ${terms.sellerSecret}`) | ||
// Seller-signed seller secrets | ||
const sellerSecretBWs = Object.values(dictionary) | ||
// Find all BoundWitnesses | ||
.filter(isBoundWitnessWithMeta) | ||
// That contain the seller secret | ||
.filter((bw) => bw.payload_hashes.includes(sellerSecret)) | ||
// That are signed by all the sellers | ||
.filter((bw) => seller.every((sellerAddress) => bw.addresses.includes(sellerAddress))) | ||
|
||
// If there are no sellerSecret BWs, return false | ||
if (sellerSecretBWs.length === 0) { | ||
console.log(`${name}: No BoundWitnesses supplied for sellerSecret: ${sellerSecret}`) | ||
return false | ||
} | ||
|
||
// Ensure each BW supplied for the sellerSecret is valid | ||
const errors = await Promise.all(sellerSecretBWs.map((bw) => new BoundWitnessValidator(bw).validate())) | ||
const validBoundWitnesses = errors.every((errors) => errors.length === 0) | ||
if (!validBoundWitnesses) { | ||
console.log(`${name}: Invalid BoundWitnesses supplied for sellerSecret: ${sellerSecret}`) | ||
return false | ||
} | ||
return true | ||
} | ||
} |