-
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.
Merge pull request #49 from XYOracleNetwork/feature/escrow-helpers
Escrow helpers
- Loading branch information
Showing
3 changed files
with
38 additions
and
12 deletions.
There are no files selected for viewing
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
1 change: 1 addition & 0 deletions
1
packages/payload/packages/payments/src/Escrow/util/secret/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,3 +1,4 @@ | ||
export * from './createEscrowIntent.ts' | ||
export * from './findEscrowPartySecretSignatures.ts' | ||
export * from './getEscrowSecret.ts' | ||
export * from './updateEscrowTermsWithSecret.ts' |
37 changes: 37 additions & 0 deletions
37
packages/payload/packages/payments/src/Escrow/util/secret/updateEscrowTermsWithSecret.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,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 | ||
} |