Skip to content

Commit

Permalink
feat: add pox-4 signer-key to StackingClient.stack
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Jan 3, 2024
1 parent d0a1a32 commit 65d2ad5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/stacking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const amountMicroStx = 100000000000n;
const privateKey = 'd48f215481c16cbe6426f8e557df9b78895661971d71735126545abddcd5377001';
// block height at which to stack
const burnBlockHeight = 2000;
// signer key hex
const signerKey = '03bae4a7...e2e2c6b7e';

// Refer to initialization section to create client instance
const stackingResults = await client.stack({
Expand All @@ -101,6 +103,7 @@ const stackingResults = await client.stack({
cycles,
privateKey,
burnBlockHeight,
signerKey,
});

// {
Expand Down
25 changes: 22 additions & 3 deletions packages/stacking/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-ignore
import { IntegerType, intToBigInt } from '@stacks/common';
import { IntegerType, hexToBytes, intToBigInt } from '@stacks/common';
import { StacksNetwork } from '@stacks/network';
import {
BurnchainRewardListResponse,
Expand All @@ -21,6 +21,7 @@ import {
TxBroadcastResult,
UIntCV,
broadcastTransaction,
bufferCV,
callReadOnlyFunction,
cvToString,
getFee,
Expand All @@ -36,6 +37,7 @@ import { PoxOperationPeriod, StackingErrors } from './constants';
import {
ensureLegacyBtcAddressForPox1,
ensurePox2Activated,
ensureSignerKeyForPox4,
poxAddressToTuple,
unwrap,
unwrapMap,
Expand Down Expand Up @@ -233,6 +235,8 @@ export interface LockStxOptions {
amountMicroStx: IntegerType;
/** the burnchain block height to begin lock */
burnBlockHeight: number;
/** hex-encoded signer key `(buff 33)`, required for >= pox-4 */
signerKey?: string;
}

/**
Expand Down Expand Up @@ -661,6 +665,8 @@ export class StackingClient {
}

/**
* `stack-stx`
*
* Generate and broadcast a stacking transaction to lock STX
* @param {LockStxOptions} options - a required lock STX options object
* @returns {Promise<string>} that resolves to a broadcasted txid if the operation succeeds
Expand All @@ -670,20 +676,23 @@ export class StackingClient {
poxAddress,
cycles,
burnBlockHeight,
signerKey,
...txOptions
}: LockStxOptions & BaseTxOptions): Promise<TxBroadcastResult> {
const poxInfo = await this.getPoxInfo();
const poxOperationInfo = await this.getPoxOperationInfo(poxInfo);

const contract = await this.getStackingContract(poxOperationInfo);
ensureLegacyBtcAddressForPox1({ contract, poxAddress });
ensureSignerKeyForPox4({ contract, signerKey });

const callOptions = this.getStackOptions({
amountMicroStx,
cycles,
poxAddress,
contract,
burnBlockHeight,
signerKey,
});
const tx = await makeContractCall({
...callOptions,
Expand Down Expand Up @@ -1006,21 +1015,31 @@ export class StackingClient {
cycles,
contract,
burnBlockHeight,
signerKey,
}: {
cycles: number;
poxAddress: string;
amountMicroStx: IntegerType;
contract: string;
burnBlockHeight: number;
signerKey?: string;
}) {
const address = poxAddressToTuple(poxAddress);
const [contractAddress, contractName] = this.parseContractId(contract);

const functionArgs = [
uintCV(amountMicroStx),
address,
uintCV(burnBlockHeight),
uintCV(cycles),
] as ClarityValue[];
if (signerKey) functionArgs.push(bufferCV(hexToBytes(signerKey)));

const callOptions: ContractCallOptions = {
contractAddress,
contractName,
functionName: 'stack-stx',
// sum of uStx, address, burn_block_height, num_cycles
functionArgs: [uintCV(amountMicroStx), address, uintCV(burnBlockHeight), uintCV(cycles)],
functionArgs,
validateWithAbi: true,
network: this.network,
anchorMode: AnchorMode.Any,
Expand Down
16 changes: 16 additions & 0 deletions packages/stacking/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,19 @@ export function ensureLegacyBtcAddressForPox1({
throw new Error('PoX-1 requires P2PKH/P2SH/P2SH-P2WPKH/P2SH-P2WSH bitcoin addresses');
}
}

/**
* @internal
* Throws unless a signerKey is given for >= PoX-4.
*/
export function ensureSignerKeyForGtePox4({
contract,
signerKey,
}: {
contract: string;
signerKey?: string;
}) {
if (signerKey) return;
if (/\.pox(-[2-3])?$/.test(contract)) return;
throw new Error('PoX-4 requires a signer-key (buff 33) to stack');
}

0 comments on commit 65d2ad5

Please sign in to comment.