Skip to content

Commit

Permalink
Merge pull request #305 from Concordium/fix-init-contract-serialization
Browse files Browse the repository at this point in the history
Fix init contract serialization issue
  • Loading branch information
orhoj authored Nov 6, 2023
2 parents 35d6ed9 + 23490fc commit 5258133
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 7.0.3

### Fixed

- An issue with the serialization of init contract account transactions.

## 7.0.2

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@concordium/web-sdk",
"version": "7.0.2",
"version": "7.0.3",
"license": "Apache-2.0",
"engines": {
"node": ">=16"
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/accountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export class InitContractHandler

serialize(payload: InitContractPayload): Buffer {
const serializedAmount = encodeWord64(payload.amount.microCcdAmount);
const initNameBuffer = Buffer.from('init_' + payload.initName, 'utf8');
const initNameBuffer = Buffer.from(
'init_' + payload.initName.value,
'utf8'
);
const serializedInitName = packBufferWithWord16Length(initNameBuffer);
const serializedModuleRef = payload.moduleRef.decodedModuleRef;
const parameterBuffer = Parameter.toBuffer(payload.param);
Expand Down
51 changes: 46 additions & 5 deletions packages/sdk/test/ci/accountTransactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ import {
getAccountTransactionSignDigest,
AccountTransactionHeader,
SequenceNumber,
InitContractPayload,
ContractName,
Energy,
ModuleReference,
Parameter,
serializeAccountTransactionPayload,
} from '../../src/index.js';

test('configureBaker is serialized correctly', async () => {
const senderAccountAddress =
'4ZJBYQbVp3zVZyjCXfZAAYBVkJMyVj8UKUNj9ox5YqTCBdBq2M';
const senderAccountAddress =
'4ZJBYQbVp3zVZyjCXfZAAYBVkJMyVj8UKUNj9ox5YqTCBdBq2M';
const expiry = TransactionExpiry.fromDate(new Date(1675872215));

test('configureBaker is serialized correctly', async () => {
const expectedDigest =
'dcfb92b6e57b1d3e252c52cb8b838f44a33bf8d67301e89753101912f299dffb';

const expiry = TransactionExpiry.fromDate(new Date(1675872215));

const header: AccountTransactionHeader = {
expiry,
nonce: SequenceNumber.create(1),
Expand Down Expand Up @@ -60,3 +65,39 @@ test('configureBaker is serialized correctly', async () => {

expect(signDigest.toString('hex')).toBe(expectedDigest);
});

test('Init contract serializes init name correctly', async () => {
const header: AccountTransactionHeader = {
expiry,
nonce: SequenceNumber.create(1),
sender: AccountAddress.fromBase58(senderAccountAddress),
};

const initNameBase = 'credential_registry';

const payload: InitContractPayload = {
amount: CcdAmount.fromMicroCcd(0),
initName: ContractName.fromString(initNameBase),
maxContractExecutionEnergy: Energy.create(30000),
moduleRef: ModuleReference.fromHexString(
'aabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccdd'
),
param: Parameter.empty(),
};

const transaction: AccountTransaction = {
header,
payload,
type: AccountTransactionType.InitContract,
};

const serializedTransaction =
serializeAccountTransactionPayload(transaction);

// Slice out the init name part of the serialized transaction.
const serializedInitName = serializedTransaction
.slice(43, serializedTransaction.length - 2)
.toString('utf8');

expect(serializedInitName).toEqual('init_credential_registry');
});

0 comments on commit 5258133

Please sign in to comment.