Skip to content

Commit 7d3444b

Browse files
authored
feat: add signer_address to pox signer endpoints (#1975)
1 parent 9648ac8 commit 7d3444b

File tree

8 files changed

+37
-6
lines changed

8 files changed

+37
-6
lines changed

docs/api/stacking/get-pox-cycle-signers.example.json

+3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
"results": [
66
{
77
"signing_key": "0x038e3c4529395611be9abf6fa3b6987e81d402385e3d605a073f42f407565a4a3d",
8+
"signer_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP",
89
"stacked_amount": "686251350000000000",
910
"stacked_amount_percent": 50,
1011
"weight": 5,
1112
"weight_percent": 55.55555555555556
1213
},
1314
{
1415
"signing_key": "0x029874497a7952483aa23890e9d0898696f33864d3df90939930a1f45421fe3b09",
16+
"signer_address": "STF9B75ADQAVXQHNEQ6KGHXTG7JP305J2GRWF3A2",
1517
"stacked_amount": "457500900000000000",
1618
"stacked_amount_percent": 33.333333333333336,
1719
"weight": 3,
1820
"weight_percent": 33.33333333333333
1921
},
2022
{
2123
"signing_key": "0x02dcde79b38787b72d8e5e0af81cffa802f0a3c8452d6b46e08859165f49a72736",
24+
"signer_address": "ST18MDW2PDTBSCR1ACXYRJP2JX70FWNM6YY2VX4SS",
2225
"stacked_amount": "228750450000000000",
2326
"stacked_amount_percent": 16.666666666666668,
2427
"weight": 1,

docs/entities/stacking/signer.example.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"signing_key": "0x038e3c4529395611be9abf6fa3b6987e81d402385e3d605a073f42f407565a4a3d",
3+
"signer_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP",
34
"stacked_amount": "686251350000000000",
45
"stacked_amount_percent": 50,
56
"weight": 5,

docs/entities/stacking/signer.schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"additionalProperties": false,
55
"required": [
66
"signing_key",
7+
"signer_address",
78
"weight",
89
"stacked_amount",
910
"weight_percent",
@@ -13,6 +14,10 @@
1314
"signing_key": {
1415
"type": "string"
1516
},
17+
"signer_address": {
18+
"type": "string",
19+
"description": "The Stacks address derived from the signing_key."
20+
},
1621
"weight": {
1722
"type": "integer"
1823
},

docs/generated.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3393,6 +3393,10 @@ export interface PoxCycleSignersListResponse {
33933393
}
33943394
export interface PoxSigner {
33953395
signing_key: string;
3396+
/**
3397+
* The Stacks address derived from the signing_key.
3398+
*/
3399+
signer_address: string;
33963400
weight: number;
33973401
stacked_amount: string;
33983402
weight_percent: number;

src/api/init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export async function startApiServer(opts: {
242242
v2.use('/smart-contracts', createV2SmartContractsRouter(datastore));
243243
v2.use('/mempool', createMempoolRouter(datastore));
244244
v2.use('/addresses', createV2AddressesRouter(datastore));
245-
v2.use('/pox', createPoxRouter(datastore));
245+
v2.use('/pox', createPoxRouter(datastore, chainId));
246246
return v2;
247247
})()
248248
);

src/api/routes/v2/helpers.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
parseDbTx,
2828
} from '../../../api/controllers/db-controller';
2929
import { decodeClarityValueToRepr } from 'stacks-encoding-native-js';
30+
import { TransactionVersion, getAddressFromPublicKey } from '@stacks/transactions';
3031

3132
export function parseDbNakamotoBlock(block: DbBlock): NakamotoBlock {
3233
const apiBlock: NakamotoBlock = {
@@ -175,9 +176,14 @@ export function parseDbPoxCycle(cycle: DbPoxCycle): PoxCycle {
175176
return result;
176177
}
177178

178-
export function parseDbPoxSigner(signer: DbPoxCycleSigner): PoxSigner {
179+
export function parseDbPoxSigner(signer: DbPoxCycleSigner, isMainnet: boolean): PoxSigner {
180+
const signerAddress = getAddressFromPublicKey(
181+
Buffer.from(signer.signing_key.slice(2), 'hex'),
182+
isMainnet ? TransactionVersion.Mainnet : TransactionVersion.Testnet
183+
);
179184
const result: PoxSigner = {
180185
signing_key: signer.signing_key,
186+
signer_address: signerAddress,
181187
weight: signer.weight,
182188
stacked_amount: signer.stacked_amount,
183189
weight_percent: signer.weight_percent,

src/api/routes/v2/pox.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ import {
1818
PoxCycleListResponse,
1919
PoxCycleSignerStackersListResponse,
2020
PoxCycleSignersListResponse,
21-
} from 'docs/generated';
21+
PoxSigner,
22+
} from '@stacks/stacks-blockchain-api-types';
2223
import { parseDbPoxCycle, parseDbPoxSigner, parseDbPoxSignerStacker } from './helpers';
2324
import { InvalidRequestError } from '../../../errors';
25+
import { ChainID, getChainIDNetwork } from '../../../helpers';
2426

25-
export function createPoxRouter(db: PgStore): express.Router {
27+
export function createPoxRouter(db: PgStore, chainId: ChainID): express.Router {
2628
const router = express.Router();
2729
const cacheHandler = getETagCacheHandler(db);
30+
const isMainnet = getChainIDNetwork(chainId) === 'mainnet';
2831

2932
router.get(
3033
'/cycles',
@@ -83,7 +86,7 @@ export function createPoxRouter(db: PgStore): express.Router {
8386
limit,
8487
offset,
8588
total,
86-
results: results.map(r => parseDbPoxSigner(r)),
89+
results: results.map(r => parseDbPoxSigner(r, isMainnet)),
8790
};
8891
setETagCacheHeaders(res);
8992
res.json(response);
@@ -110,8 +113,9 @@ export function createPoxRouter(db: PgStore): express.Router {
110113
res.status(404).json({ error: `Not found` });
111114
return;
112115
}
116+
const response: PoxSigner = parseDbPoxSigner(signer, isMainnet);
113117
setETagCacheHeaders(res);
114-
res.json(parseDbPoxSigner(signer));
118+
res.json(response);
115119
} catch (error) {
116120
if (error instanceof InvalidRequestError) {
117121
res.status(404).json({ errors: error.message });

src/tests/pox-tests.ts

+8
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,23 @@ describe('PoX tests', () => {
106106
results: [
107107
{
108108
signing_key: '0x038e3c4529395611be9abf6fa3b6987e81d402385e3d605a073f42f407565a4a3d',
109+
signer_address: 'STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP',
109110
stacked_amount: '686251350000000000',
110111
stacked_amount_percent: 50,
111112
weight: 5,
112113
weight_percent: 55.55555555555556,
113114
},
114115
{
115116
signing_key: '0x029874497a7952483aa23890e9d0898696f33864d3df90939930a1f45421fe3b09',
117+
signer_address: 'STF9B75ADQAVXQHNEQ6KGHXTG7JP305J2GRWF3A2',
116118
stacked_amount: '457500900000000000',
117119
stacked_amount_percent: 33.333333333333336,
118120
weight: 3,
119121
weight_percent: 33.33333333333333,
120122
},
121123
{
122124
signing_key: '0x02dcde79b38787b72d8e5e0af81cffa802f0a3c8452d6b46e08859165f49a72736',
125+
signer_address: 'ST18MDW2PDTBSCR1ACXYRJP2JX70FWNM6YY2VX4SS',
123126
stacked_amount: '228750450000000000',
124127
stacked_amount_percent: 16.666666666666668,
125128
weight: 1,
@@ -135,6 +138,7 @@ describe('PoX tests', () => {
135138
expect(signer.type).toBe('application/json');
136139
expect(JSON.parse(signer.text)).toStrictEqual({
137140
signing_key: '0x038e3c4529395611be9abf6fa3b6987e81d402385e3d605a073f42f407565a4a3d',
141+
signer_address: 'STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP',
138142
stacked_amount: '686251350000000000',
139143
stacked_amount_percent: 50,
140144
weight: 5,
@@ -253,13 +257,15 @@ describe('PoX tests', () => {
253257
results: [
254258
{
255259
signing_key: '0x028efa20fa5706567008ebaf48f7ae891342eeb944d96392f719c505c89f84ed8d',
260+
signer_address: 'ST3AM1A56AK2C1XAFJ4115ZSV26EB49BVQ10MGCS0',
256261
stacked_amount: '7500510000000000',
257262
stacked_amount_percent: 42.857142857142854,
258263
weight: 9,
259264
weight_percent: 42.857142857142854,
260265
},
261266
{
262267
signing_key: '0x023f19d77c842b675bd8c858e9ac8b0ca2efa566f17accf8ef9ceb5a992dc67836',
268+
signer_address: 'ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ',
263269
stacked_amount: '5000340000000000',
264270
stacked_amount_percent: 28.571428571428573,
265271
weight: 6,
@@ -268,6 +274,7 @@ describe('PoX tests', () => {
268274
{
269275
// steph doubled the weight of this signer
270276
signing_key: '0x029fb154a570a1645af3dd43c3c668a979b59d21a46dd717fd799b13be3b2a0dc7',
277+
signer_address: 'ST3NBRSFKX28FQ2ZJ1MAKX58HKHSDGNV5N7R21XCP',
271278
stacked_amount: '5000340000000000',
272279
stacked_amount_percent: 28.571428571428573,
273280
weight: 6,
@@ -284,6 +291,7 @@ describe('PoX tests', () => {
284291
expect(signer.type).toBe('application/json');
285292
expect(JSON.parse(signer.text)).toStrictEqual({
286293
signing_key: '0x029fb154a570a1645af3dd43c3c668a979b59d21a46dd717fd799b13be3b2a0dc7',
294+
signer_address: 'ST3NBRSFKX28FQ2ZJ1MAKX58HKHSDGNV5N7R21XCP',
287295
stacked_amount: '5000340000000000',
288296
stacked_amount_percent: 28.571428571428573,
289297
weight: 6,

0 commit comments

Comments
 (0)