From 23490fc988b695fcd256546cc45e8e23592bf337 Mon Sep 17 00:00:00 2001 From: orhoj Date: Mon, 6 Nov 2023 13:54:27 +0100 Subject: [PATCH] Fix init contract serialization issue --- packages/sdk/CHANGELOG.md | 6 +++ packages/sdk/package.json | 2 +- packages/sdk/src/accountTransactions.ts | 5 +- .../sdk/test/ci/accountTransactions.test.ts | 51 +++++++++++++++++-- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index ead330ae1..1fb7d1363 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 7.0.3 + +### Fixed + +- An issue with the serialization of init contract account transactions. + ## 7.0.2 ### Fixed diff --git a/packages/sdk/package.json b/packages/sdk/package.json index b83e722cc..c0e447174 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@concordium/web-sdk", - "version": "7.0.2", + "version": "7.0.3", "license": "Apache-2.0", "engines": { "node": ">=16" diff --git a/packages/sdk/src/accountTransactions.ts b/packages/sdk/src/accountTransactions.ts index 4381e54b0..a894084be 100644 --- a/packages/sdk/src/accountTransactions.ts +++ b/packages/sdk/src/accountTransactions.ts @@ -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); diff --git a/packages/sdk/test/ci/accountTransactions.test.ts b/packages/sdk/test/ci/accountTransactions.test.ts index ad803b49f..934029c71 100644 --- a/packages/sdk/test/ci/accountTransactions.test.ts +++ b/packages/sdk/test/ci/accountTransactions.test.ts @@ -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), @@ -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'); +});