Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add error throwing on too large fungible post-condition #1612

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/transactions/src/postcondition.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { IntegerType, intToBigInt } from '@stacks/common';
import { ClarityValue } from './clarity';
import {
PostConditionType,
FungibleConditionCode,
NonFungibleConditionCode,
PostConditionType,
StacksMessageType,
} from './constants';

import {
AssetInfo,
FungiblePostCondition,
NonFungiblePostCondition,
PostConditionPrincipal,
STXPostCondition,
parseAssetInfoString,
parsePrincipalString,
STXPostCondition,
FungiblePostCondition,
NonFungiblePostCondition,
} from './postcondition-types';

import { ClarityValue } from './clarity';

export function createSTXPostCondition(
principal: string | PostConditionPrincipal,
conditionCode: FungibleConditionCode,
Expand Down
5 changes: 4 additions & 1 deletion packages/transactions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
createLPString,
} from './postcondition-types';
import { Payload, deserializePayload, serializePayload } from './payload';
import { DeserializationError } from './errors';
import { DeserializationError, SerializationError } from './errors';
import {
deserializeTransactionAuthField,
deserializeMessageSignature,
Expand Down Expand Up @@ -379,6 +379,9 @@ export function serializePostCondition(postCondition: PostCondition): Uint8Array
postCondition.conditionType === PostConditionType.STX ||
postCondition.conditionType === PostConditionType.Fungible
) {
// SIP-005: Maximal length of amount is 8 bytes
if (postCondition.amount > BigInt('0xffffffffffffffff'))
throw new SerializationError('The post-condition amount may not be larger than 8 bytes');
bytesArray.push(intToBytes(postCondition.amount, false, 8));
}

Expand Down
30 changes: 30 additions & 0 deletions packages/transactions/tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ import { createTransactionAuthField } from '../src/signature';
import { TransactionSigner } from '../src/signer';
import { deserializeTransaction, StacksTransaction } from '../src/transaction';
import { cloneDeep } from '../src/utils';
import {
createFungiblePostCondition,
createSTXPostCondition,
serializePostCondition,
} from '../src';

function setSignature(
unsignedTransaction: StacksTransaction,
Expand Down Expand Up @@ -2157,3 +2162,28 @@ test('Get contract map entry - no match', async () => {
expect(result).toEqual(mockResult);
expect(result.type).toBe(ClarityType.OptionalNone);
});

test('Post-conditions with amount larger than 8 bytes throw an error', () => {
const amount = BigInt('0xffffffffffffffff') + 1n;

const stxPc = createSTXPostCondition(
'SP34EBMKMRR6SXX65GRKJ1FHEXV7AGHJ2D8ASQ5M3',
FungibleConditionCode.Equal,
amount
);

const fungiblePc = createFungiblePostCondition(
'SP34EBMKMRR6SXX65GRKJ1FHEXV7AGHJ2D8ASQ5M3',
FungibleConditionCode.Equal,
amount,
'SP34EBMKMRR6SXX65GRKJ1FHEXV7AGHJ2D8ASQ5M3.token::frank'
);

expect(() => {
serializePostCondition(stxPc);
}).toThrowError('The post-condition amount may not be larger than 8 bytes');

expect(() => {
serializePostCondition(fungiblePc);
}).toThrowError('The post-condition amount may not be larger than 8 bytes');
});
Loading