Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Jan 28, 2024
1 parent 9d158ab commit dfd618a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
34 changes: 24 additions & 10 deletions src/client/api/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@

import { EscrowClient } from '../class/client.js'
import { validate_account_req, validate_register_req } from '@/validators/index.js'
import { EscrowClient } from '../class/client.js'

import {
validate_account_req,
validate_covenant,
validate_register_req,
validate_spend_req
} from '@/validators/index.js'

import {
CovenantData,
ReturnData,
ApiResponse,
AccountRequest,
AccountDataResponse,
DepositDataResponse,
DepositListResponse,
FundingDataResponse,
RegisterRequest
RegisterRequest,
SpendRequest
} from '@/types/index.js'

import * as assert from '@/assert.js'
Expand Down Expand Up @@ -104,6 +110,8 @@ function list_deposit_api (client : EscrowClient) {
pubkey : string,
token : string
) : Promise<ApiResponse<DepositListResponse>> => {
// Validate the pubkey.
assert.is_hash(pubkey)
// Formulate the request.
const url = `${client.host}/api/deposit/list/${pubkey}`
// Return the response.
Expand All @@ -116,26 +124,32 @@ function commit_funds_api (client : EscrowClient) {
dpid : string,
covenant : CovenantData
) : Promise<ApiResponse<FundingDataResponse>> => {
// Validate the deposit id.
assert.is_hash(dpid)
// Validate the covenant.
validate_covenant(covenant)
// Create the request url.
const url = `${client.host}/api/deposit/${dpid}/commit`
const body = JSON.stringify(covenant)
// Create the request object.
const init = {
body,
body : JSON.stringify(covenant),
method : 'POST',
headers : { 'content-type' : 'application/json' }
}
// Fetch and return a response.
return client.fetcher<FundingDataResponse>({ url, init })
}
}

function close_deposit_api (client : EscrowClient) {
return async (
req : ReturnData
dpid : string,
req : SpendRequest
) : Promise<ApiResponse<DepositDataResponse>> => {
const dpid = req.dpid
validate_spend_req(req)
const url = `${client._host}/api/deposit/${dpid}/close`
const body = JSON.stringify(req)
const init = {
body,
body : JSON.stringify(req),
headers : { 'content-type': 'application/json' },
method : 'POST'
}
Expand Down
24 changes: 23 additions & 1 deletion src/lib/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { get_return_script } from './return.js'
import { parse_extkey } from '@cmdcode/crypto-tools/hd'
import { validate_deposit } from '@/validators/deposit.js'
import { get_return_script } from './return.js'
import { RETURN_TX_VSIZE } from '@/config.js'

import {
get_object_id,
Expand All @@ -12,6 +14,7 @@ import {
} from '@cmdcode/musig2'

import {
create_tx_template,
get_address,
get_tapkey,
parse_timelock
Expand Down Expand Up @@ -160,3 +163,22 @@ export function get_spend_state (
// Return the spend state.
return state
}

export function get_ext_pubkey (xpub : string) {
return parse_extkey(xpub).pubkey
}

export function get_close_tx_template (
value : number,
xpub : string,
feerate : number
) {
// Parse the return pubkey.
const return_pk = get_ext_pubkey(xpub)
// Create locking script.
const script = [ 'OP_1', return_pk ]
// Calculate the transaction fee.
const txfee = Math.ceil(RETURN_TX_VSIZE * feerate)
// Return a transaction using the provided params.
return create_tx_template(script, value - txfee)
}
8 changes: 7 additions & 1 deletion src/schema/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const reg_req = z.object({
utxo : txspend
})

const spend_req = z.object({
pnonce : nonce,
psig : hex,
txfee : num
})

const data = z.object({
status,
agent_id : hash,
Expand All @@ -73,4 +79,4 @@ const data = z.object({
updated_at : stamp
}).and(state).and(spend_state).and(close_state).and(txspend)

export default { account, covenant, data, state, acct_req, reg_req, status }
export default { account, covenant, data, state, acct_req, reg_req, spend_req, status }
6 changes: 6 additions & 0 deletions src/types/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export interface RegisterRequest {
utxo : TxOutput
}

export interface SpendRequest {
feerate : number
pnonce : string
psig : string
}

export interface ExtendedKey {
prefix : number
depth : number
Expand Down
16 changes: 9 additions & 7 deletions src/validators/covenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { parse_txout } from '@/lib/tx.js'
import { parse_covenant } from '@/lib/parse.js'
import { get_entry } from '../lib/util.js'

import { get_close_tx_template } from '@/lib/deposit.js'

import {
get_path_mutexes,
get_return_mutex,
Expand All @@ -15,9 +17,9 @@ import {
CovenantData,
DepositContext,
DepositData,
ReturnData,
MutexEntry,
SignerAPI
SignerAPI,
SpendRequest
} from '../types/index.js'

import * as assert from '../assert.js'
Expand Down Expand Up @@ -52,16 +54,16 @@ export function verify_covenant (
check_deposit_psigs(entries, covenant.psigs)
}

export function verify_refund (
export function verify_spend_req (
dp_agent : SignerAPI,
deposit : DepositData,
refund : ReturnData
request : SpendRequest
) {
const { dpid, agent_pn } = deposit
const { pnonce, psig, txhex } = refund
assert.ok(dpid === refund.dpid, 'deposit_id does not match')
const { agent_pn, spend_xpub, value } = deposit
const { feerate, pnonce, psig } = request
check_deposit_agent(dp_agent, deposit)
const pnonces = [ pnonce, agent_pn ]
const txhex = get_close_tx_template(value, spend_xpub, feerate)
const mutex = get_return_mutex(deposit, pnonces, txhex)
verify_mutex_psig(mutex, psig)
}
Expand Down
7 changes: 7 additions & 0 deletions src/validators/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DepositContext,
DepositData,
RegisterRequest,
SpendRequest,
TxOutput
} from '../types/index.js'

Expand All @@ -23,6 +24,12 @@ export function validate_register_req (
schema.deposit.reg_req.parse(template)
}

export function validate_spend_req (
template : unknown
) : asserts template is SpendRequest {
schema.deposit.spend_req.parse(template)
}

export function validate_deposit (
deposit : Record<string, any>
) : asserts deposit is DepositData {
Expand Down

0 comments on commit dfd618a

Please sign in to comment.