From 33220bb5b4f0cd54519477bfb515087d4edf407b Mon Sep 17 00:00:00 2001 From: cmd Date: Sun, 28 Jan 2024 02:53:52 -0600 Subject: [PATCH] update --- demo/06_request_account.ts | 9 +++++---- demo/07_deposit_funds.ts | 8 ++++---- src/client/api/deposit.ts | 3 ++- src/client/api/depositor.ts | 22 ++++++++++------------ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/demo/06_request_account.ts b/demo/06_request_account.ts index ceff13d0..55eb2036 100644 --- a/demo/06_request_account.ts +++ b/demo/06_request_account.ts @@ -2,15 +2,16 @@ import { print_banner } from '@scrow/test' import { client } from './01_create_client.js' import { signers } from './02_create_signer.js' -const pubkey = signers[0].pubkey const locktime = 60 * 60 // 1 hour locktime -const res = await client.deposit.request({ pubkey, locktime }) +const funder = signers[0] +const acct_req = funder.deposit.request_account(locktime) +const acct_res = await client.deposit.request(acct_req) // Check the response is valid. -if (!res.ok) throw new Error(res.error) +if (!acct_res.ok) throw new Error(acct_res.error) // Unpack some of the terms. -export const account = res.data.account +export const { account } = acct_res.data print_banner('deposit account') console.dir(account, { depth : null }) diff --git a/demo/07_deposit_funds.ts b/demo/07_deposit_funds.ts index 654ea019..33e35755 100644 --- a/demo/07_deposit_funds.ts +++ b/demo/07_deposit_funds.ts @@ -7,7 +7,7 @@ import { signers } from './02_create_signer.js' import { contract } from './05_create_contract.js' import { account } from './06_request_account.js' -const { address, agent_id } = account +const { address } = account const vin_fee = contract.feerate * 65 const amt_total = contract.total + vin_fee @@ -38,11 +38,11 @@ console.log('\nutxo:', utxos[0]) // Request the member to sign const signer = signers[0] const utxo = utxos[0].txspend -const return_tx = await signer.deposit.register_utxo(account, utxo) -const covenant = await signer.deposit.commit_utxo(account, contract, utxo) +const covenant = signer.deposit.commit_utxo(account, contract, utxo) +const reg_req = { ...account, covenant, utxo } // Fund the contract -const res = await client.deposit.fund(agent_id, return_tx, covenant) +const res = await client.deposit.fund(reg_req) // Check the response is valid. if (!res.ok) throw new Error('failed') diff --git a/src/client/api/deposit.ts b/src/client/api/deposit.ts index 079c1b3f..4281e52d 100644 --- a/src/client/api/deposit.ts +++ b/src/client/api/deposit.ts @@ -1,6 +1,6 @@ import { EscrowClient } from '../class/client.js' -import { validate_register_req } from '@/validators/index.js' +import { validate_account_req, validate_register_req } from '@/validators/index.js' import { CovenantData, @@ -23,6 +23,7 @@ function request_account_api (client : EscrowClient) { return async ( request : AccountRequest ) : Promise> => { + validate_account_req(request) // Formulate the request. const url = `${client.host}/api/deposit/request` // Formulate the request. diff --git a/src/client/api/depositor.ts b/src/client/api/depositor.ts index bdf86642..1f449b32 100644 --- a/src/client/api/depositor.ts +++ b/src/client/api/depositor.ts @@ -10,8 +10,7 @@ import { } from '@/lib/session.js' import { - AccountDataResponse, - ApiResponse, + AccountRequest, ContractData, CovenantData, DepositAccount, @@ -20,14 +19,13 @@ import { } from '@/types/index.js' export function request_account_api (signer : EscrowSigner) { - return async ( + return ( locktime : number, index ?: number - ) : Promise> => { + ) : AccountRequest => { const deposit_pk = signer.pubkey const spend_xpub = signer.get_account(index).xpub - const req = { deposit_pk, locktime, spend_xpub } - return signer.client.deposit.request(req) + return { deposit_pk, locktime, spend_xpub } } } @@ -38,11 +36,11 @@ export function verify_account_api (signer : EscrowSigner) { } export function commit_utxo_api (signer : EscrowSigner) { - return async ( + return ( account : DepositAccount, contract : ContractData, utxo : TxOutput - ) : Promise => { + ) : CovenantData => { // Unpack the deposit object. const { agent_pk, sequence, spend_xpub } = account // Check if account xpub is valid. @@ -61,10 +59,10 @@ export function commit_utxo_api (signer : EscrowSigner) { } export function commit_deposit_api (signer : EscrowSigner) { - return async ( + return ( contract : ContractData, deposit : DepositData - ) : Promise => { + ) : CovenantData => { // Unpack the deposit object. const { agent_pk, sequence, txid, vout, @@ -88,11 +86,11 @@ export function commit_deposit_api (signer : EscrowSigner) { } export function close_deposit_api (signer : EscrowSigner) { - return async ( + return ( deposit : DepositData, txfee : number, address ?: string - ) : Promise => { + ) : string => { // Unpack signer object. const { txid } = deposit if (address === undefined) {