Skip to content

Commit

Permalink
Merge pull request #49 from XYOracleNetwork/feature/escrow-helpers
Browse files Browse the repository at this point in the history
Escrow helpers
  • Loading branch information
JoelBCarter authored Sep 27, 2024
2 parents 203fc55 + 91a896b commit 896cec4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ import type { IdPayload } from '@xyo-network/id-payload-plugin'

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

/**
* Creates an escrow intent
* @deprecated Use createEscrowIntentWithSecret instead
* @param terms The payloads describing the terms for the escrow
* @param account The account to create the escrow intent with
* @returns The escrow intent
*/
export const createEscrowIntent = async (terms: EscrowTerms[], account: AccountInstance) => {
const result = await new BoundWitnessBuilder({ accounts: [account] }).payloads([...terms]).build()
return result
}

/**
* Creates an escrow intent (for a buyer or seller) using the supplied secret
* @param terms The payload describing the terms for the escrow
Expand Down
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 896cec4

Please sign in to comment.