-
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.
Use common helper for finding party secrets
- Loading branch information
1 parent
86c5aea
commit 4217de9
Showing
5 changed files
with
54 additions
and
56 deletions.
There are no files selected for viewing
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' |
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' |
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