Skip to content

Commit

Permalink
Export escrow secret helper based on escrow party
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Sep 27, 2024
1 parent 55b98d8 commit 91a896b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './createEscrowIntent.ts'
export * from './findEscrowPartySecretSignatures.ts'
export * from './getEscrowSecret.ts'
export * from './updateEscrowTermsWithSecret.ts'
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { AccountInstance } from '@xyo-network/account-model'
import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import type { IdPayload } from '@xyo-network/id-payload-plugin'
import { PayloadBuilder } from '@xyo-network/payload-builder'

import type {
EscrowParty, EscrowPartySecret, EscrowTerms,
} from '../../Terms/index.ts'
import { getEscrowSecret } from './getEscrowSecret.ts'

/**
* Creates an escrow intent (for a buyer or seller) using the supplied secret
* @param terms The payload describing the terms for the escrow
* @param escrowParty The party in the escrow transaction
* @param account The account(s) to create the escrow intent with
* @param secret The secret for the escrow principal party to use for the escrow. If
* not provided, a cryptographically random secret will be generated.
* @returns The escrow intent
*/
export const updateEscrowTermsWithSecret = async (
terms: EscrowTerms,
escrowParty: EscrowParty,
account: AccountInstance | AccountInstance[],
secret?: IdPayload,
) => {
if (!secret) secret = getEscrowSecret()
const signers = Array.isArray(account) ? account : [account]
// Add party addresses to escrow terms
terms[escrowParty] = signers.map(signer => signer.address)
// Add secret hash to terms
const secretType: EscrowPartySecret = escrowParty === 'buyer' ? 'buyerSecret' : 'sellerSecret'
const secretHash = await PayloadBuilder.dataHash(secret)
terms[secretType] = secretHash
// Have the parties sign the secret and terms
const result = await new BoundWitnessBuilder().signers(signers).payloads([terms, secret]).build()
return result
}

0 comments on commit 91a896b

Please sign in to comment.