Skip to content

Commit

Permalink
Use common helper for finding party secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Sep 20, 2024
1 parent 86c5aea commit 4217de9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 56 deletions.
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './getPartySecretSignedValidator.ts'
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'
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { assertEx } from '@xylabs/assert'
import type { Hash } from '@xylabs/hex'
import { isBoundWitnessWithMeta } from '@xyo-network/boundwitness-model'
import { BoundWitnessValidator } from '@xyo-network/boundwitness-validator'
import type {
AsyncPayloadValidationFunction, Payload, SyncPayloadValidationFunction, WithMeta,
} from '@xyo-network/payload-model'

import type { EscrowTerms } from '../../Terms/index.ts'
import { getPartySecretSignedValidator } from '../common/index.ts'

const name = 'EscrowTerms.buyerSecret'

Expand Down Expand Up @@ -46,31 +45,5 @@ export const getBuyerSecretSuppliedValidator = (dictionary: Record<Hash, WithMet
* @returns A function that validates the escrow terms for the existence of the buyerSecret signed by the buyer
*/
export const getBuyerSecretSignedValidator = (dictionary: Record<Hash, WithMeta<Payload>>): AsyncPayloadValidationFunction<EscrowTerms> => {
return async (terms: EscrowTerms): Promise<boolean> => {
const buyer = assertEx(terms.buyer, () => `${name}: No buyer: ${terms.buyer}`)
const buyerSecret = assertEx(terms.buyerSecret, () => `${name}: No buyerSecret: ${terms.buyerSecret}`)
// Buyer-signed buyer secrets
const buyerSecretBWs = Object.values(dictionary)
// Find all BoundWitnesses
.filter(isBoundWitnessWithMeta)
// That contain the buyer secret
.filter(bw => bw.payload_hashes.includes(buyerSecret))
// That are signed by all the buyers
.filter(bw => buyer.every(buyerAddress => bw.addresses.includes(buyerAddress)))

// If there are no buyerSecret BWs, return false
if (buyerSecretBWs.length === 0) {
console.log(`${name}: No BoundWitnesses supplied for buyerSecret: ${buyerSecret}`)
return false
}

// Ensure each BW supplied for the buyerSecret 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(`${name}: Invalid BoundWitnesses supplied for buyerSecret: ${buyerSecret}`)
return false
}
return true
}
return getPartySecretSignedValidator(dictionary, 'buyer')
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from '@xyo-network/payload-model'

import type { EscrowTerms } from '../../Terms/index.ts'
import { getPartySecretSignedValidator } from '../common/index.ts'

const name = 'EscrowTerms.sellerSecret'

Expand Down Expand Up @@ -46,31 +47,5 @@ export const getSellerSecretSuppliedValidator = (dictionary: Record<Hash, WithMe
* @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>>): AsyncPayloadValidationFunction<EscrowTerms> => {
return async (terms: EscrowTerms): Promise<boolean> => {
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
}
return getPartySecretSignedValidator(dictionary, 'seller')
}

0 comments on commit 4217de9

Please sign in to comment.