Skip to content

Commit

Permalink
fix: update rosetta to use signer-key
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Jan 23, 2024
1 parent 2c4d1e7 commit af463b0
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"pox_addr": {
"type": "string",
"description": "The reward address for stacking transaction. It should be a valid Bitcoin address"
},
"signer_key": {
"type": "string",
"description": "The hex-encoded signer key (buff 33) for PoX."
}
}
}
18 changes: 9 additions & 9 deletions src/api/routes/rosetta/construction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import {
} from '../../../rosetta/rosetta-helpers';
import { makeRosettaError, rosettaValidateRequest, ValidSchema } from './../../rosetta-validate';
import { has0xPrefix, hexToBuffer } from '@hirosystems/api-toolkit';
import { hexToBytes } from '@stacks/common';

export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID): express.Router {
const router = express.Router();
Expand Down Expand Up @@ -215,13 +216,12 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
break;
case RosettaOperationType.StackStx: {
const poxAddr = options.pox_addr;
if (!options.number_of_cycles || !poxAddr) {
if (!options.number_of_cycles || !options.signer_key || !poxAddr) {
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}

if (doesThrow(() => decodeBtcAddress(poxAddr))) {
// todo: add error type specifically for this?
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
Expand All @@ -241,6 +241,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
poxAddressToTuple(poxAddr),
uintCV(0),
uintCV(options.number_of_cycles),
bufferCV(hexToBytes(options.signer_key)),
],
validateWithAbi: false,
network: getStacksNetwork(),
Expand All @@ -252,11 +253,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
break;
}
case RosettaOperationType.DelegateStx: {
if (!options.amount) {
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
if (!options.delegate_to) {
if (!options.amount || !options.delegate_to || !options.signer_key) {
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
Expand All @@ -276,6 +273,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
standardPrincipalCV(options.delegate_to),
noneCV(),
poxAddrOptionalCV,
bufferCV(hexToBytes(options.signer_key)),
],
validateWithAbi: false,
network: getStacksNetwork(),
Expand Down Expand Up @@ -656,7 +654,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
if (!options.number_of_cycles || !options.amount) {
if (!options.number_of_cycles || !options.amount || !options.signer_key) {
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
Expand All @@ -671,6 +669,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
poxAddressCV,
uintCV(req.body.metadata.burn_block_height),
uintCV(options.number_of_cycles),
bufferCV(hexToBytes(options.signer_key)),
],
fee: txFee,
nonce: nonce,
Expand Down Expand Up @@ -712,7 +711,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
if (typeof burn_block_height !== 'number' || typeof burn_block_height !== 'string')
expire_burn_block_heightCV = someCV(uintCV(burn_block_height));
}
if (!options.delegate_to || !options.amount) {
if (!options.delegate_to || !options.amount || !options.signer_key) {
res.status(400).json(RosettaErrors[RosettaErrorsTypes.invalidOperation]);
return;
}
Expand All @@ -727,6 +726,7 @@ export function createRosettaConstructionRouter(db: PgStore, chainId: ChainID):
standardPrincipalCV(options.delegate_to),
expire_burn_block_heightCV,
poxAddressCV,
bufferCV(hexToBytes(options.signer_key)),
],
fee: txFee,
nonce: nonce,
Expand Down
9 changes: 8 additions & 1 deletion src/rosetta/rosetta-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,13 @@ export function getOptionsFromOperations(operations: RosettaOperation[]): Rosett
if (!operation.metadata || typeof operation.metadata.number_of_cycles !== 'number') {
return null;
}

if (!operation.metadata || typeof operation.metadata.signer_key !== 'string') {
return null;
}
options.sender_address = operation.account?.address;
options.type = operation.type;
options.number_of_cycles = operation.metadata.number_of_cycles;
options.signer_key = operation.metadata.signer_key;
options.amount = operation.amount?.value.replace('-', '');
options.symbol = operation.amount?.currency.symbol;
options.decimals = operation.amount?.currency.decimals;
Expand All @@ -790,13 +793,17 @@ export function getOptionsFromOperations(operations: RosettaOperation[]): Rosett
if (!operation.metadata || typeof operation.metadata.delegate_to !== 'string') {
return null;
}
if (!operation.metadata || typeof operation.metadata.signer_key !== 'string') {
return null;
}
options.sender_address = operation.account?.address;
options.type = operation.type;
options.delegate_to = operation.metadata?.delegate_to;
options.amount = operation.amount?.value.replace('-', '');
options.symbol = operation.amount?.currency.symbol;
options.decimals = operation.amount?.currency.decimals;
options.pox_addr = operation.metadata?.pox_addr as string;
options.signer_key = operation.metadata.signer_key;
break;
default:
return null;
Expand Down
2 changes: 2 additions & 0 deletions src/test-utils/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export async function stackStxWithRosetta(opts: {
privateKey: string;
cycleCount: number;
ustxAmount: bigint;
signerKey: string;
}) {
const rosettaNetwork: NetworkIdentifier = {
blockchain: RosettaConstants.blockchain,
Expand All @@ -535,6 +536,7 @@ export async function stackStxWithRosetta(opts: {
metadata: {
number_of_cycles: opts.cycleCount,
pox_addr: opts.btcAddr,
signer_key: opts.signerKey,
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/tests-2.5/pox-4-burnchain-delegate-stx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async function createPox2DelegateStx(args: {
};
}

describe('PoX-4 - Stack using Bitcoin-chain ops', () => {
describe('PoX-4 - Stack using Bitcoin-chain delegate ops', () => {
const seedAccount = testnetKeys[0];

let db: PgWriteStore;
Expand Down
4 changes: 2 additions & 2 deletions src/tests-2.5/pox-4-burnchain-stack-stx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function createPox2StackStx(args: {
};
}

describe('PoX-4 - Stack using Bitcoin-chain ops', () => {
describe('PoX-4 - Stack using Bitcoin-chain stack ops', () => {
const seedAccount = testnetKeys[0];

let db: PgWriteStore;
Expand Down Expand Up @@ -186,7 +186,7 @@ describe('PoX-4 - Stack using Bitcoin-chain ops', () => {

// transfer pox "min_amount_ustx" from seed to test account
const poxInfo = await client.getPox();
testAccountBalance = BigInt(Math.round(Number(poxInfo.min_amount_ustx) * 2.1).toString());
testAccountBalance = BigInt(poxInfo.min_amount_ustx) * 2n;
const stxXfer1 = await makeSTXTokenTransfer({
senderKey: seedAccount.secretKey,
recipient: account.stxAddr,
Expand Down
3 changes: 3 additions & 0 deletions src/tests-2.5/pox-4-rosetta-btc-addr-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { CoreRpcPoxInfo } from '../core-rpc/client';
import { DbTxStatus } from '../datastore/common';
import { BurnchainRewardSlotHolderListResponse } from '@stacks/stacks-blockchain-api-types';
import { bytesToHex } from '@stacks/common';
import { randomBytes } from '@stacks/transactions';

const BTC_ADDRESS_CASES = [
{ addressFormat: 'p2pkh' },
Expand Down Expand Up @@ -55,6 +57,7 @@ describe.each(BTC_ADDRESS_CASES)(
privateKey: account.secretKey,
cycleCount,
ustxAmount,
signerKey: bytesToHex(randomBytes(33)),
});
expect(rosettaStackStx.tx.status).toBe(DbTxStatus.Success);
expect(rosettaStackStx.constructionMetadata.metadata.contract_name).toBe('pox-4');
Expand Down
3 changes: 3 additions & 0 deletions src/tests-2.5/pox-4-rosetta-cycle-phases.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { bytesToHex } from '@stacks/common';
import { randomBytes } from '@stacks/transactions';
import { testnetKeys } from '../api/routes/debug';
import { stackStxWithRosetta, standByUntilBurnBlock, testEnv } from '../test-utils/test-helpers';

Expand Down Expand Up @@ -41,6 +43,7 @@ describe.each(BLOCK_SHIFT_COUNT)(
cycleCount: 1,
ustxAmount,
btcAddr,
signerKey: bytesToHex(randomBytes(33)),
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/tests-2.5/pox-4-rosetta-segwit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AnchorMode,
getAddressFromPrivateKey,
makeSTXTokenTransfer,
randomBytes,
TransactionVersion,
} from '@stacks/transactions';
import bignumber from 'bignumber.js';
Expand All @@ -26,6 +27,7 @@ import {
testEnv,
} from '../test-utils/test-helpers';
import { hexToBuffer } from '@hirosystems/api-toolkit';
import { bytesToHex } from '@stacks/common';

describe('PoX-4 - Rosetta - Stacking with segwit', () => {
let btcAddr: string;
Expand Down Expand Up @@ -129,6 +131,7 @@ describe('PoX-4 - Rosetta - Stacking with segwit', () => {
privateKey: account.secretKey,
cycleCount: cycleCount,
ustxAmount: ustxAmount,
signerKey: bytesToHex(randomBytes(33)),
});

expect(stackingResult.constructionMetadata.metadata.contract_name).toBe('pox-4');
Expand Down Expand Up @@ -247,6 +250,7 @@ describe('PoX-4 - Rosetta - Stacking with segwit', () => {
privateKey: account.secretKey,
cycleCount,
ustxAmount,
signerKey: bytesToHex(randomBytes(33)),
});

expect(rosettaStackStx.constructionMetadata.metadata.contract_name).toBe('pox-4');
Expand Down

0 comments on commit af463b0

Please sign in to comment.