Skip to content

Commit 6cbbdb7

Browse files
fix: Throw on post-condition amounts not in valid range (#1839)
1 parent f7523b6 commit 6cbbdb7

4 files changed

Lines changed: 140 additions & 11 deletions

File tree

‎packages/transactions/src/pc.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IntegerType, intToBigInt } from '@stacks/common';
1+
import { IntegerType } from '@stacks/common';
22
import { ClarityValue } from './clarity';
33
import {
44
FungibleComparator,
@@ -11,7 +11,7 @@ import {
1111
import { AddressString, AssetString, ContractIdString } from './types';
1212
import { parseContractId, validateStacksAddress } from './utils';
1313
import { deserializePostConditionWire } from './wire';
14-
import { wireToPostCondition } from './postcondition';
14+
import { parsePostConditionAmount, wireToPostCondition } from './postcondition';
1515

1616
/// `Pc.` Post Condition Builder
1717
//
@@ -188,7 +188,7 @@ class PartialPcFtWithCode {
188188
type: 'stx-postcondition',
189189
address: this.address,
190190
condition: this.code,
191-
amount: intToBigInt(this.amount).toString(),
191+
amount: parsePostConditionAmount(this.amount).toString(),
192192
};
193193
}
194194

@@ -208,7 +208,7 @@ class PartialPcFtWithCode {
208208
type: 'ft-postcondition',
209209
address: this.address,
210210
condition: this.code,
211-
amount: intToBigInt(this.amount).toString(),
211+
amount: parsePostConditionAmount(this.amount).toString(),
212212
asset: `${contractId}::${tokenName}`,
213213
};
214214
}

‎packages/transactions/src/postcondition.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IntegerType, intToBigInt } from '@stacks/common';
12
import {
23
FungibleConditionCode,
34
NonFungibleConditionCode,
@@ -23,6 +24,19 @@ import {
2324
serializePostConditionWire,
2425
} from './wire';
2526

27+
const MAX_U64 = BigInt('18446744073709551615'); // 2^64 - 1
28+
29+
/** @internal Parses a post-condition amount to bigint, throws if outside u64 range. */
30+
export function parsePostConditionAmount(amount: IntegerType): bigint {
31+
const value = intToBigInt(amount);
32+
if (value < 0n || value > MAX_U64) {
33+
throw new RangeError(
34+
`Post-condition amount must be between 0 and ${MAX_U64} (u64 max), received: ${value}`
35+
);
36+
}
37+
return value;
38+
}
39+
2640
/** @internal */
2741
enum PostConditionCodeWireType {
2842
eq = FungibleConditionCode.Equal,
@@ -46,7 +60,7 @@ export function postConditionToWire(postcondition: PostCondition): PostCondition
4660
? { type: StacksWireType.Principal, prefix: PostConditionPrincipalId.Origin }
4761
: parsePrincipalString(postcondition.address),
4862
conditionCode: conditionTypeToByte(postcondition.condition) as FungibleConditionCode,
49-
amount: BigInt(postcondition.amount),
63+
amount: parsePostConditionAmount(postcondition.amount),
5064
};
5165
case 'ft-postcondition':
5266
return {
@@ -57,7 +71,7 @@ export function postConditionToWire(postcondition: PostCondition): PostCondition
5771
? { type: StacksWireType.Principal, prefix: PostConditionPrincipalId.Origin }
5872
: parsePrincipalString(postcondition.address),
5973
conditionCode: conditionTypeToByte(postcondition.condition) as FungibleConditionCode,
60-
amount: BigInt(postcondition.amount),
74+
amount: parsePostConditionAmount(postcondition.amount),
6175
asset: parseAssetString(postcondition.asset),
6276
};
6377
case 'nft-postcondition':

‎packages/transactions/tests/builder.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {
4343
publicKeyIsCompressed,
4444
publicKeyToHex,
4545
serializePayloadBytes,
46-
serializePostConditionWireBytes,
4746
serializePublicKeyBytes,
4847
} from '../src';
4948
import { BytesReader } from '../src/BytesReader';
@@ -2465,12 +2464,12 @@ test('Post-conditions with amount larger than 8 bytes throw an error', () => {
24652464
};
24662465

24672466
expect(() => {
2468-
serializePostConditionWireBytes(postConditionToWire(stxPc));
2469-
}).toThrowError('The post-condition amount may not be larger than 8 bytes');
2467+
postConditionToWire(stxPc);
2468+
}).toThrowError(/Post-condition amount must be between 0 and 18446744073709551615/);
24702469

24712470
expect(() => {
2472-
serializePostConditionWireBytes(postConditionToWire(fungiblePc));
2473-
}).toThrowError('The post-condition amount may not be larger than 8 bytes');
2471+
postConditionToWire(fungiblePc);
2472+
}).toThrowError(/Post-condition amount must be between 0 and 18446744073709551615/);
24742473
});
24752474

24762475
test('StacksTransaction serialize/deserialize equality with an empty memo', async () => {

‎packages/transactions/tests/postcondition.test.ts‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,119 @@ describe('Pc.fromHex', () => {
445445
expect(() => Pc.fromHex(invalidHex)).toThrow();
446446
});
447447
});
448+
449+
describe('post-condition amount u64 validation', () => {
450+
const MAX_U64 = BigInt('18446744073709551615'); // 2^64 - 1
451+
const OVER_U64 = MAX_U64 + 1n;
452+
const address = 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6';
453+
const ftContract = 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6.token';
454+
const ftAsset = `${ftContract}::tok`;
455+
456+
describe('Pc builder rejects amounts exceeding u64', () => {
457+
test('ustx rejects amount > u64 max', () => {
458+
expect(() => Pc.principal(address).willSendEq(OVER_U64).ustx()).toThrow();
459+
});
460+
461+
test('ft rejects amount > u64 max', () => {
462+
expect(() => Pc.principal(address).willSendEq(OVER_U64).ft(ftContract, 'tok')).toThrow();
463+
});
464+
465+
test('ustx rejects negative amount', () => {
466+
expect(() => Pc.principal(address).willSendEq(-1n).ustx()).toThrow();
467+
});
468+
469+
test('ft rejects negative amount', () => {
470+
expect(() => Pc.principal(address).willSendEq(-1n).ft(ftContract, 'tok')).toThrow();
471+
});
472+
473+
test('ustx accepts u64 max exactly', () => {
474+
const pc = Pc.principal(address).willSendEq(MAX_U64).ustx();
475+
expect(pc.amount).toBe(MAX_U64.toString());
476+
});
477+
478+
test('ft accepts u64 max exactly', () => {
479+
const pc = Pc.principal(address).willSendEq(MAX_U64).ft(ftContract, 'tok');
480+
expect(pc.amount).toBe(MAX_U64.toString());
481+
});
482+
});
483+
484+
describe('postConditionToWire rejects amounts exceeding u64', () => {
485+
test('STX post condition rejects amount > u64 max', () => {
486+
expect(() =>
487+
postConditionToWire({
488+
type: 'stx-postcondition',
489+
address,
490+
condition: 'eq',
491+
amount: OVER_U64,
492+
})
493+
).toThrow();
494+
});
495+
496+
test('FT post condition rejects amount > u64 max', () => {
497+
expect(() =>
498+
postConditionToWire({
499+
type: 'ft-postcondition',
500+
address,
501+
condition: 'eq',
502+
amount: OVER_U64,
503+
asset: ftAsset as any,
504+
})
505+
).toThrow();
506+
});
507+
508+
test('STX post condition rejects negative amount', () => {
509+
expect(() =>
510+
postConditionToWire({
511+
type: 'stx-postcondition',
512+
address,
513+
condition: 'eq',
514+
amount: -1n,
515+
})
516+
).toThrow();
517+
});
518+
519+
test('FT post condition rejects negative amount', () => {
520+
expect(() =>
521+
postConditionToWire({
522+
type: 'ft-postcondition',
523+
address,
524+
condition: 'eq',
525+
amount: -1n,
526+
asset: ftAsset as any,
527+
})
528+
).toThrow();
529+
});
530+
531+
test('STX post condition accepts u64 max exactly', () => {
532+
const wire = postConditionToWire({
533+
type: 'stx-postcondition',
534+
address,
535+
condition: 'eq',
536+
amount: MAX_U64,
537+
}) as STXPostConditionWire;
538+
expect(wire.amount).toBe(MAX_U64);
539+
});
540+
541+
test('FT post condition accepts u64 max exactly', () => {
542+
const wire = postConditionToWire({
543+
type: 'ft-postcondition',
544+
address,
545+
condition: 'eq',
546+
amount: MAX_U64,
547+
asset: ftAsset as any,
548+
}) as FungiblePostConditionWire;
549+
expect(wire.amount).toBe(MAX_U64);
550+
});
551+
552+
test('STX post condition rejects string amount > u64 max', () => {
553+
expect(() =>
554+
postConditionToWire({
555+
type: 'stx-postcondition',
556+
address,
557+
condition: 'eq',
558+
amount: '18446744073709551616',
559+
})
560+
).toThrow();
561+
});
562+
});
563+
});

0 commit comments

Comments
 (0)