From 63c039cbcb832f702b0ab582590d94cef924df74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Libert?= Date: Thu, 2 May 2024 09:33:32 +0200 Subject: [PATCH] Fix CR --- .../src/experimental/basicElements/address.ts | 4 ++-- .../experimental/basicElements/internal.ts | 7 +++---- .../basicElements/operationManager.ts | 20 ++++++++++++++----- .../src/experimental/utils/index.ts | 1 + .../src/experimental/utils/noMagic.ts | 2 ++ 5 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 packages/massa-web3/src/experimental/utils/noMagic.ts diff --git a/packages/massa-web3/src/experimental/basicElements/address.ts b/packages/massa-web3/src/experimental/basicElements/address.ts index f5f6688a..e9cbc337 100644 --- a/packages/massa-web3/src/experimental/basicElements/address.ts +++ b/packages/massa-web3/src/experimental/basicElements/address.ts @@ -2,6 +2,7 @@ import Base58 from '../crypto/base58' import Serializer from '../crypto/interfaces/serializer' import { Version, Versioner } from '../crypto/interfaces/versioner' import VarintVersioner from '../crypto/varintVersioner' +import { FIRST } from '../utils' import { PublicKey } from './keys' import varint from 'varint' @@ -230,8 +231,7 @@ export class Address { */ static extractFromBuffer( data: Uint8Array, - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - offset = 0 + offset = FIRST ): { data: Uint8Array; length: number } { // addr type varint.decode(data, offset) diff --git a/packages/massa-web3/src/experimental/basicElements/internal.ts b/packages/massa-web3/src/experimental/basicElements/internal.ts index 16953c55..2beae760 100644 --- a/packages/massa-web3/src/experimental/basicElements/internal.ts +++ b/packages/massa-web3/src/experimental/basicElements/internal.ts @@ -1,5 +1,6 @@ import Serializer from '../crypto/interfaces/serializer' import { Versioner, Version } from '../crypto/interfaces/versioner' +import { FIRST, ONE } from '../utils' /** * Deserialize a string data into non versioned bytes and checks that expected version match. @@ -31,12 +32,10 @@ export function extractData( * @returns the extracted prefix. */ export function mustExtractPrefix(str: string, ...expected: string[]): string { - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - const prefix = str.slice(0, expected[0].length) + const prefix = str.slice(FIRST, expected[FIRST].length) if (!expected.includes(prefix)) { throw new Error( - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - `invalid prefix: ${prefix}. ${expected.length > 1 ? 'one of ' : ''}${expected.join(' or ')} was expected.` + `invalid prefix: ${prefix}. ${expected.length > ONE ? 'one of ' : ''}${expected.join(' or ')} was expected.` ) } return prefix diff --git a/packages/massa-web3/src/experimental/basicElements/operationManager.ts b/packages/massa-web3/src/experimental/basicElements/operationManager.ts index d1a8142a..7bcec021 100644 --- a/packages/massa-web3/src/experimental/basicElements/operationManager.ts +++ b/packages/massa-web3/src/experimental/basicElements/operationManager.ts @@ -4,6 +4,7 @@ import { PrivateKey, PublicKey } from './keys' import { BlockchainClient } from '../client' import { Signature } from './signature' import varint from 'varint' +import { FIRST } from '../utils' // TODO: replace with a U64 helper const U64_SIZE_BYTES = 8 @@ -31,7 +32,7 @@ export enum OperationType { * * @remarks * Period to live is the number of periods the operation is valid for. - * This value must be positive and if it's too big, the node will (sliently?) reject the operation. + * This value must be positive and if it's too big, the node will (silently?) reject the operation. * * If no fee is provided, minimal fee of connected node is used. * If no periodToLive is provided, the DefaultPeriodToLive is used. @@ -117,6 +118,17 @@ export class OperationManager { operation = operation as RollOperation components.push(unsigned.encode(operation.amount)) break + case OperationType.CallSmartContractFunction: + // @see https://docs.massa.net/docs/learn/operation-format-execution#callsc-operation-payload + operation = operation as CallOperation + components.push(unsigned.encode(operation.maxGas)) + components.push(unsigned.encode(operation.coins)) + components.push(Address.fromString(operation.address).toBytes()) + components.push(varint.encode(operation.func.length)) + components.push(Buffer.from(operation.func)) + components.push(varint.encode(operation.parameter.length)) + components.push(operation.parameter) + break case OperationType.ExecuteSmartContractBytecode: operation = operation as ExecuteOperation components.push(unsigned.encode(operation.maxGas)) @@ -156,8 +168,7 @@ export class OperationManager { * @returns An new instance of OperationDetails representing the deserialized operation. */ static deserialize(data: Uint8Array): OperationDetails { - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - let offset = 0 + let offset = FIRST const nextVarInt = (): bigint => { const value = unsigned.decode(data, offset) offset += unsigned.encodingLength(value) @@ -213,8 +224,7 @@ export class OperationManager { // u64ToBytes is little endian const networkId = new Uint8Array(U64_SIZE_BYTES) const view = new DataView(networkId.buffer) - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - view.setBigUint64(0, chainId, false) + view.setBigUint64(FIRST, chainId, false) const data = OperationManager.serialize(operation) const publicKeyBytes = key.toBytes() diff --git a/packages/massa-web3/src/experimental/utils/index.ts b/packages/massa-web3/src/experimental/utils/index.ts index da81b8d3..d54d6077 100644 --- a/packages/massa-web3/src/experimental/utils/index.ts +++ b/packages/massa-web3/src/experimental/utils/index.ts @@ -1 +1,2 @@ export * from './numbers' +export * from './noMagic' diff --git a/packages/massa-web3/src/experimental/utils/noMagic.ts b/packages/massa-web3/src/experimental/utils/noMagic.ts new file mode 100644 index 00000000..4d52b56c --- /dev/null +++ b/packages/massa-web3/src/experimental/utils/noMagic.ts @@ -0,0 +1,2 @@ +export const FIRST = 0 +export const ONE = 1