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 efe93de commit ac10ac9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 39 deletions.
9 changes: 5 additions & 4 deletions src/schema/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ const covenant = z.object({
psigs : z.tuple([ str, psig ]).array()
})

const account_req = z.object({
const acct_req = z.object({
deposit_pk : hash,
locktime : locktime.optional(),
spend_xpub : str
})

const register_req = z.object({
const reg_req = z.object({
covenant : covenant.optional(),
deposit_pk : hash,
return_psig : hex.optional(),
sequence : num,
spend_xpub : str
spend_xpub : str,
utxo : txspend
})

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

export default { account, covenant, data, state, account_req, register_req, status }
export default { account, covenant, data, state, acct_req, reg_req, status }
1 change: 1 addition & 0 deletions src/types/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface RegisterRequest {
return_psig ?: string
sequence : number
spend_xpub : string
utxo : TxOutput
}

export interface ExtendedKey {
Expand Down
32 changes: 10 additions & 22 deletions src/validators/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { taproot } from '@scrow/tapscript/sighash'
import { parse_script } from '@scrow/tapscript/script'
import { parse_sequence } from '@scrow/tapscript/tx'

import {
DepositContext,
DepositData,
RegisterRequest,
ReturnContext,
TxOutput
} from '../types/index.js'

Expand All @@ -15,13 +14,13 @@ import * as schema from '../schema/index.js'
export function validate_account_req (
template : unknown
) : asserts template is RegisterRequest {
schema.deposit.account_req.parse(template)
schema.deposit.acct_req.parse(template)
}

export function validate_register_req (
template : unknown
) : asserts template is RegisterRequest {
schema.deposit.register_req.parse(template)
schema.deposit.reg_req.parse(template)
}

export function validate_deposit (
Expand All @@ -32,25 +31,14 @@ export function validate_deposit (

export function verify_deposit (
deposit_ctx : DepositContext,
return_ctx : ReturnContext,
txout : TxOutput
utxo : TxOutput
) {
// Unpack our transaction template.
const { sequence, tap_data } = deposit_ctx
const { pubkey, tapkey, tx } = return_ctx
const { txid, vout, value, scriptkey } = txout
// Assert that the sequence value is valid.
const sdata = parse_sequence(sequence)
assert.ok(sdata.enabled, 'Sequence field timelock is not enabled.')
assert.ok(sdata.type === 'stamp', 'Sequence field is not configured for timelock.')
// Get the deposit context.
assert.ok(tap_data.tapkey === tapkey, 'Deposit tapkey does not match return tapkey!')
// Prepare recovery tx for signature verification.
const opt = { pubkey, txindex : 0, throws : true }
const txin = tx.vin[0]
assert.ok(txin.txid === txid, 'recovery txid does not match utxo')
assert.ok(txin.vout === vout, 'recovery vout does not match utxo')
tx.vin[0].prevout = { value : BigInt(value), scriptPubKey : scriptkey }
// Assert that the recovery tx is fully valid for broadcast.
taproot.verify_tx(tx, opt)
const { scriptkey } = utxo
const sdata = parse_sequence(sequence)
const tapkey = parse_script(scriptkey).asm[1]
assert.ok(sdata.enabled, 'sequence field timelock is not enabled.')
assert.ok(sdata.type === 'stamp', 'sequence field is not configured for timelock.')
assert.ok(tapkey === tap_data.tapkey, 'utxo scriptkey does not match tapkey')
}
23 changes: 11 additions & 12 deletions test/src/tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
verify_covenant,
validate_register_req,
verify_witness,
validate_witness
validate_witness,
verify_deposit
} from '@scrow/core/validate'

import { PaymentEntry } from '@/types/index.js'
Expand Down Expand Up @@ -92,23 +93,21 @@ export default async function (client : CoreClient, tape : Test) {
const registrations = await register_funds(contract, members)

const promises = registrations.map(async tmpl => {
const { utxo, ...template } = tmpl
const { deposit_pk, sequence, spend_xpub } = template
const { covenant, deposit_pk, sequence, spend_xpub, utxo } = tmpl
const { txid, vout } = utxo
validate_register_req(template)
const agent_id = agent.signer.id
validate_register_req(tmpl)
const agent_pk = agent.signer.pubkey
const return_pk = parse_extkey(spend_xpub).pubkey
const dep_ctx = get_deposit_ctx(agent_pk, deposit_pk, return_pk, sequence)
const tx_data = await client.get_txinput(txid, vout)
assert.ok(tx_data !== null, 'there is no tx data')
const spendout = prevout_to_txspend(tx_data.txinput)
// verify_deposit(dep_ctx, return_ctx, spendout)
const dpid = Buff.random(32).hex
const state = get_spend_state(sequence, tx_data.status)
const session = create_session(agent.signer, dpid)
const agent_pn = session.agent_pn
const deposit = create_deposit({ ...dep_ctx, dpid, agent_id, agent_pn, ...template, ...spendout, ...state })
const spendout = prevout_to_txspend(tx_data.txinput)
verify_deposit(dep_ctx, utxo)
const dpid = Buff.random(32).hex
const state = get_spend_state(sequence, tx_data.status)
const session = create_session(agent.signer, dpid)
const full_ctx = { ...dep_ctx, ...session, ...spendout, ...state }
const deposit = create_deposit({ ...full_ctx, covenant, dpid, spend_xpub })
// const deposit = register_deposit(dep_ctx, dpid, pnonce, tmpl, spendout, state)
verify_covenant(dep_ctx, contract, deposit, agent.signer, agent.signer)
return deposit
Expand Down
2 changes: 1 addition & 1 deletion test/tape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import e2e_test from './src/tests/e2e.test.js'
import vm_test from './src/vm/vm.test.js'

tape('Escrow Core Test Suite', async t => {
vm_test(t)
// vm_test(t)
const core = get_daemon()
const client = await core.startup()
await e2e_test(client, t)
Expand Down

0 comments on commit ac10ac9

Please sign in to comment.