From dd5cd50fbd53624ffab9ea8bb048c7540aa5c99c Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Thu, 8 Feb 2024 15:19:23 -0800 Subject: [PATCH] feat: helper function to make pox-4 signer signature --- packages/stacking/src/utils.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/stacking/src/utils.ts b/packages/stacking/src/utils.ts index 83ce61b4c..1f5c44d3c 100644 --- a/packages/stacking/src/utils.ts +++ b/packages/stacking/src/utils.ts @@ -1,5 +1,5 @@ import { bech32, bech32m } from '@scure/base'; -import { bigIntToBytes } from '@stacks/common'; +import { ChainID, bigIntToBytes } from '@stacks/common'; import { base58CheckDecode, base58CheckEncode } from '@stacks/encryption'; import { bufferCV, @@ -7,8 +7,12 @@ import { ClarityType, ClarityValue, OptionalCV, + signStructuredData, + StacksPrivateKey, + stringAsciiCV, tupleCV, TupleCV, + uintCV, } from '@stacks/transactions'; import { PoxOperationInfo } from '.'; import { @@ -369,3 +373,31 @@ export function ensureSignerKeyReadiness({ if (!signerKey) throw new Error('PoX-4 or later requires a signer-key (buff 33) to stack'); } } + +export enum Pox4SignatureTopics { + StackStx = 'stack-stx', + AggregateCommit = 'agg-commit', + StackExtend = 'stack-extend', +} + +export function signPox4SignatureHash( + topic: Pox4SignatureTopics, + rewardCycle: number, + poxAddress: string, + period: number, + chainId: ChainID, + privateKey: StacksPrivateKey +) { + const domain = tupleCV({ + name: stringAsciiCV('pox-4-signer'), + version: stringAsciiCV('1.0.0'), + 'chain-id': uintCV(chainId), + }); + const message = tupleCV({ + poxAddress: poxAddressToTuple(poxAddress), + rewardCycle: uintCV(rewardCycle), + period: uintCV(period), + topic: stringAsciiCV(topic), + }); + return signStructuredData({ message, domain, privateKey }); +}