Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions modules/utxo-staking/src/pox5/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export function assertPox5EarlyExitSpend(psbt: Psbt, input: pox5.Pox5InputMatch)
getMatchedTransactionInput(psbt, input);
}

export type Pox5SpendBranch = 'locktime' | 'early-exit';

/** Classify a PoX-5 spend from native principal-preimage metadata. */
export function classifyPox5Spend(psbt: Psbt, input: pox5.Pox5InputMatch): Pox5SpendBranch {
getMatchedTransactionInput(psbt, input);
const hasPrincipalPreimage = psbt
.getInputKeyValues(input.inputIndex)
.some((record) => record.type === 'known' && record.key === 'PSBT_IN_SHA256');
return hasPrincipalPreimage ? 'early-exit' : 'locktime';
}

/** Add validated principal-preimage metadata for an early-exit spend. */
export function preparePox5EarlyExit(
psbt: Psbt,
Expand Down
10 changes: 10 additions & 0 deletions modules/utxo-staking/test/unit/pox5/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getKey, getKeyTriple } from '@bitgo/wasm-utxo/testutils';
import {
assertPox5EarlyExitSpend,
assertPox5LocktimeSpend,
classifyPox5Spend,
POX5_MAX_UNLOCK_HEIGHT,
preparePox5EarlyExit,
} from '../../../src/pox5';
Expand Down Expand Up @@ -60,6 +61,15 @@ describe('PoX-5 spend policy', function () {
assert.doesNotThrow(() => assertPox5EarlyExitSpend(timestampLocktime.psbt, timestampLocktime.match));
});

it('classifies the spend from native principal-preimage metadata', function () {
const locktimeSpend = createPox5RecoveryPsbt(UNLOCK_HEIGHT);
assert.equal(classifyPox5Spend(locktimeSpend.psbt, locktimeSpend.match), 'locktime');

const earlyExitSpend = createPox5RecoveryPsbt(0);
preparePox5EarlyExit(earlyExitSpend.psbt, 0, earlyExitSpend.match, earlyExitSpend.principalPreimage);
assert.equal(classifyPox5Spend(earlyExitSpend.psbt, earlyExitSpend.match), 'early-exit');
});

it('enforces the block-height and unlock-height boundaries', function () {
const atHeight = createPox5RecoveryPsbt(UNLOCK_HEIGHT);
const aboveHeight = createPox5RecoveryPsbt(UNLOCK_HEIGHT + 1);
Expand Down
Loading