Skip to content

Commit

Permalink
refactor: update sign method
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Feb 13, 2024
1 parent 0db2530 commit 3d89ca1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
9 changes: 4 additions & 5 deletions packages/stacking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,17 @@ const burnBlockHeight = 2000;

// signer key
const signerPrivateKey = randomPrivKey();
const signerKey = getPublicKey(signerPrivateKey);
const signerKey = getPublicKeyFromPrivate(signerPrivateKey.data);

const signerSignature = signPox4SignatureHash({
// Refer to initialization section to create client instance
const signerSignature = client.signPoxSignature({
topic: 'stack-stx',
rewardCycle: 132, // current reward cycle
poxAddress,
period: cycles,
chainId,
privateKey,
signerPrivateKey,
});

// Refer to initialization section to create client instance
const stackingResults = await client.stack({
amountMicroStx,
poxAddress,
Expand Down
31 changes: 31 additions & 0 deletions packages/stacking/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
OptionalCV,
PrincipalCV,
ResponseErrorCV,
StacksPrivateKey,
StacksTransaction,
TupleCV,
TxBroadcastResult,
Expand All @@ -34,10 +35,12 @@ import {
} from '@stacks/transactions';
import { PoxOperationPeriod, StackingErrors } from './constants';
import {
Pox4SignatureTopic,
ensureLegacyBtcAddressForPox1,
ensurePox2Activated,
ensureSignerArgsReadiness,
poxAddressToTuple,
signPox4SignatureHash,
unwrap,
unwrapMap,
} from './utils';
Expand Down Expand Up @@ -1439,6 +1442,34 @@ export class StackingClient {

throw new Error('Stacking contract ID is malformed');
}

/**
* Generates a `signer-sig` string for the current PoX contract.
*/
signPoxSignature({
topic,
poxAddress,
rewardCycle,
period,
signerPrivateKey,
}: {
topic: `${Pox4SignatureTopic}`;
poxAddress: string;
rewardCycle: number;
period: number;
signerPrivateKey: StacksPrivateKey;
}) {
// todo: in the future add logic to determine if a later version of pox
// needs a different domain and thus use a different `signPox4SignatureHash`
return signPox4SignatureHash({
topic,
poxAddress,
rewardCycle,
period,
network: this.network,
privateKey: signerPrivateKey,
});
}
}

/** @ignore Rename `privateKey` to `senderKey`, for backwards compatibility */
Expand Down
12 changes: 7 additions & 5 deletions packages/stacking/tests/stacking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
trueCV,
tupleCV,
uintCV,
validateContractCall
validateContractCall,
} from '@stacks/transactions';
import fetchMock from 'jest-fetch-mock';
import { StackingClient } from '../src';
Expand All @@ -27,7 +27,7 @@ import {
decodeBtcAddress,
poxAddressToBtcAddress,
signPox4SignatureHash,
verifyPox4SignatureHash
verifyPox4SignatureHash,
} from '../src/utils';
import { V2_POX_REGTEST_POX_3, setApiMocks } from './apiMockingHelpers';

Expand Down Expand Up @@ -1168,16 +1168,18 @@ test('correctly signs pox-4 signer signature', () => {
const network = new StacksTestnet();
const poxAddress = 'msiYwJCvXEzjgq6hDwD9ueBka6MTfN962Z';

const privateKey = '002bc479cae71c410cf10113de8fe1611b148231eccdfb19ca779ba365cc511601';
const publicKey = getPublicKeyFromPrivate(privateKey);
const privateKey = createStacksPrivateKey(
'002bc479cae71c410cf10113de8fe1611b148231eccdfb19ca779ba365cc511601'
);
const publicKey = getPublicKeyFromPrivate(privateKey.data);

const signature = signPox4SignatureHash({
topic: 'stack-stx',
network,
period: 12,
rewardCycle: 2,
poxAddress,
privateKey: createStacksPrivateKey(privateKey),
privateKey,
});

const verified = verifyPox4SignatureHash({
Expand Down

0 comments on commit 3d89ca1

Please sign in to comment.