Skip to content

Commit

Permalink
fix: add error throwing on too large fungible post-condition
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Jan 2, 2024
1 parent 2eabcf7 commit d0a1a32
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
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');
});

1 comment on commit d0a1a32

@vercel
Copy link

@vercel vercel bot commented on d0a1a32 Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.