Skip to content

Commit

Permalink
Validators for EscrowTerms seller secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Apr 3, 2024
1 parent d8b3409 commit 3283e0e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './buyerSecret'
export * from './nbfExp'
export * from './paymentAuthorities'
export * from './seller'
export * from './sellerSecret'
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
}
}

0 comments on commit 3283e0e

Please sign in to comment.