import { taproot } from '@scrow/tapscript/sighash';
import { parse_sequence } from '@scrow/tapscript/tx';
import * as assert from '../assert.js';
import * as schema from '../schema/index.js';
export function validate_registration(template) {
    schema.deposit.register.parse(template);
}
export function validate_deposit(deposit) {
    schema.deposit.data.parse(deposit);
}
export function verify_deposit(deposit_ctx, return_ctx, txout) {
    const { sequence, tap_data } = deposit_ctx;
    const { pubkey, tapkey, tx } = return_ctx;
    const { txid, vout, value, scriptkey } = txout;
    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.');
    assert.ok(tap_data.tapkey === tapkey, 'Deposit tapkey does not match return tapkey!');
    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 };
    taproot.verify_tx(tx, opt);
}
