fetchConstructLockupOutputScript
Calls the pox-5 construct-lockup-output-script read-only and returns the 34-byte P2WSH scriptPubKey that an L1 lockup output must pay to. Use it to check an output script built locally with buildLockOutputScript before you fund it.
Usage
import { bytesToHex } from '@stacks/common';
import {
buildLockOutputScript,
buildUnlockScript,
fetchBond,
fetchBondL1UnlockHeight,
fetchConstructLockupOutputScript,
} from '@stacks/bitcoin-staking';
const bond = await fetchBond({ bondIndex: 1, network: 'mainnet' });
if (!bond) throw new Error('bond 1 is not set up');
const params = {
stxAddress: 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7',
unlockHeight: await fetchBondL1UnlockHeight({ bondIndex: 1, network: 'mainnet' }),
unlockBytes: buildUnlockScript('0316e35d38b52d4886e40065e4952a49535ce914e02294be58e252d1998f129b19'), // staker's compressed public key
earlyUnlockBytes: bond.earlyUnlockBytes,
};
const onchain = await fetchConstructLockupOutputScript({ ...params, network: 'mainnet' });
const local = buildLockOutputScript(params);
if (bytesToHex(onchain) !== bytesToHex(local)) {
throw new Error('Local output script does not match pox-5. Do not fund it.');
}Notes
Wraps
construct-lockup-output-script, which returns0x0020followed by the SHA-256 of the script from fetchConstructLockupScript.register-for-bondcalls the same read-only for every L1 lockup output and fails withERR_INVALID_LOCKUP_SCRIPT (u42)if the output's script differs (L2061-L2064, L2080-L2082).An
unlockHeightof 2^39 (549,755,813,888) or more makes the contract returnERR_INVALID_UNLOCK_HEIGHT (u52). The SDK then throws anErrorwhose message starts withconstruct-lockup-output-script returned (err u52), followed by the error's name and description.This read-only does not check the bond's minimum unlock height or the 500,000,000 locktime threshold.
register-for-bonddoes (L2074-L2079), so check the height against fetchBondL1UnlockHeight as well.Throws an
Errorif the node returns a non-2xx status (the message starts withError calling read-only function.) or answersokay: false(the message is the node'scause).
Signature
opts is a ConstructLockupParams plus a NetworkClientParam.
Returns
Promise<Uint8Array>
The 34-byte P2WSH scriptPubKey.
Parameters
opts.stxAddress (required)
Type:
string
Stacks address of the staker. A contract principal is also accepted.
opts.unlockHeight (required)
Type:
IntegerType
Bitcoin block height for the OP_CHECKLOCKTIMEVERIFY branch. Must be below 2^39.
opts.unlockBytes (required)
Type:
Uint8Array | string
Staker-signature subscript, as raw bytes or hex, at most 683 bytes. buildUnlockScript builds the single-key form.
opts.earlyUnlockBytes (required)
Type:
Uint8Array | string
The bond's early-unlock subscript, earlyUnlockBytes from fetchBond, as raw bytes or hex, at most 683 bytes.
opts.network (optional)
Type:
StacksNetworkName | StacksNetwork
The network to query: a name such as 'mainnet' or 'testnet', or a StacksNetwork object. Defaults to 'mainnet'.
opts.client (optional)
Type:
{ baseUrl?: string; fetch?: FetchFn }
Overrides the API base URL or the fetch function, for example to query your own node. Fields you set replace the network's defaults.
Was this helpful?