Socket
Socket
Sign inDemoInstall

@vechain/sdk-core

Package Overview
Dependencies
Maintainers
8
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vechain/sdk-core - npm Package Compare versions

Comparing version 1.0.0-beta.21 to 1.0.0-beta.22

src/address-utils/address-utils.ts

8

package.json
{
"name": "@vechain/sdk-core",
"version": "1.0.0-beta.21",
"version": "1.0.0-beta.22",
"description": "Includes modules for fundamental operations like hashing and cryptography",

@@ -44,7 +44,7 @@ "author": "vechain Foundation",

"@types/elliptic": "^6.4.18",
"@vechain/sdk-errors": "1.0.0-beta.21",
"@vechain/sdk-logging": "1.0.0-beta.21",
"@vechain/sdk-errors": "1.0.0-beta.22",
"@vechain/sdk-logging": "1.0.0-beta.22",
"bignumber.js": "^9.1.2",
"blakejs": "^1.2.1",
"elliptic": "^6.5.5",
"elliptic": "^6.5.6",
"ethers": "6.13.1",

@@ -51,0 +51,0 @@ "fast-json-stable-stringify": "^2.1.0"

import { type BytesLike, type ParamType } from './types';
import { ethers } from 'ethers';
import { fragment } from './fragment';
import { ABI, buildError } from '@vechain/sdk-errors';
import { InvalidAbiDataToEncodeOrDecode } from '@vechain/sdk-errors';

@@ -24,7 +24,11 @@ /**

return ethersCoder.encode([type], [value]);
} catch {
throw buildError(
'encode',
ABI.INVALID_DATA_TO_ENCODE,
'Encoding failed: Data must be a valid ABI type with corresponding valid data.'
} catch (e) {
throw new InvalidAbiDataToEncodeOrDecode(
'abi.encode()',
'Encoding failed: Data must be a valid ABI type with corresponding valid data.',
{
type,
value
},
e
);

@@ -55,7 +59,11 @@ }

return ethersCoder.encode(types, values);
} catch {
throw buildError(
'encodeParams',
ABI.INVALID_DATA_TO_ENCODE,
'Encoding failed: Data must be a valid ABI type with corresponding valid data.'
} catch (e) {
throw new InvalidAbiDataToEncodeOrDecode(
'abi.encodeParams()',
'Encoding failed: Data must be a valid ABI type with corresponding valid data.',
{
types,
values
},
e
);

@@ -82,7 +90,11 @@ }

return decoded[0] as ReturnType;
} catch {
throw buildError(
'decode',
ABI.INVALID_DATA_TO_DECODE,
'Decoding failed: Data must be a valid hex string that encodes a valid ABI type.'
} catch (e) {
throw new InvalidAbiDataToEncodeOrDecode(
'abi.decode()',
'Decoding failed: Data must be a valid ABI type with corresponding valid data.',
{
types,
data
},
e
);

@@ -89,0 +101,0 @@ }

@@ -10,3 +10,7 @@ import { ethers, type Fragment } from 'ethers';

} from './types';
import { ABI, assert, buildError } from '@vechain/sdk-errors';
import {
InvalidAbiDataToEncodeOrDecode,
InvalidAbiFragment,
InvalidAbiSignatureFormat
} from '@vechain/sdk-errors';
import { sanitizeValuesToEncode } from './helpers/fragment';

@@ -32,11 +36,11 @@

// If the formatType is not included in the allowed formats, throw an error.
assert(
'getSignature',
allowedSignatureFormats.includes(formatType),
ABI.INVALID_FORMAT_TYPE,
`Signature format error: '${formatType}' is invalid. Allowed formats: ${allowedSignatureFormats.join(
', '
)}`,
{ formatType }
);
if (!allowedSignatureFormats.includes(formatType)) {
throw new InvalidAbiSignatureFormat(
'getSignature()',
'Initialization failed: Cannot create Function fragment. Function format is invalid.',
{
signatureFormat: formatType
}
);
}

@@ -74,7 +78,9 @@ return fragment.format(formatType);

} catch (e) {
throw buildError(
'Function constructor',
ABI.INVALID_FUNCTION,
throw new InvalidAbiFragment(
'abi.Function constructor',
'Initialization failed: Cannot create Function fragment. Function format is invalid.',
{ source },
{
type: 'function',
fragment: source
},
e

@@ -115,5 +121,4 @@ );

} catch (e) {
throw buildError(
'decodeInput',
ABI.INVALID_DATA_TO_DECODE,
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Function.decodeInput()',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',

@@ -145,5 +150,4 @@ { data },

} catch (e) {
throw buildError(
'decodeOutput',
ABI.INVALID_DATA_TO_DECODE,
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Function.decodeOutput()',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',

@@ -167,5 +171,4 @@ { data },

} catch (e) {
throw buildError(
'encodeInput',
ABI.INVALID_DATA_TO_ENCODE,
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Function.encodeInput()',
'Encoding failed: Data format is invalid. Function data does not match the expected format for ABI type encoding.',

@@ -206,7 +209,9 @@ { dataToEncode },

} catch (e) {
throw buildError(
'Event constructor',
ABI.INVALID_EVENT,
'Initialization failed: Event fragment creation is not possible due to invalid ABI data format.',
{ source },
throw new InvalidAbiFragment(
'abi.Event constructor',
'Initialization failed: Cannot create Event fragment. Event format is invalid.',
{
type: 'event',
fragment: source
},
e

@@ -251,6 +256,5 @@ );

} catch (e) {
throw buildError(
'decodeEventLog',
ABI.INVALID_DATA_TO_DECODE,
'Decoding failed: Data and topics must be correctly formatted for ABI-compliant decoding.',
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Event.decodeEventLog()',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',
{ data },

@@ -276,6 +280,5 @@ e

} catch (e) {
throw buildError(
'encodeEventLog',
ABI.INVALID_DATA_TO_ENCODE,
'Encoding failed: Event data must be correctly formatted for ABI-compliant encoding.',
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Event.encodeEventLog()',
'Encoding failed: Data format is invalid. Event data does not match the expected format for ABI type encoding.',
{ dataToEncode },

@@ -311,6 +314,5 @@ e

} catch (e) {
throw buildError(
'encodeFilterTopics',
ABI.INVALID_DATA_TO_ENCODE,
'Encoding topics failed: Event topics values must be correctly formatted for ABI-compliant encoding.',
throw new InvalidAbiDataToEncodeOrDecode(
'abi.Event.encodeEventLog()',
'Encoding failed: Data format is invalid. Event topics values must be correctly formatted for ABI-compliant encoding.',
{ valuesToEncode },

@@ -317,0 +319,0 @@ e

import fastJsonStableStringify from 'fast-json-stable-stringify';
import { Hex, Hex0x } from '../utils';
import { addressUtils } from '../address';
import { addressUtils } from '../address-utils';
import { assert, CERTIFICATE } from '@vechain/sdk-errors';

@@ -5,0 +5,0 @@ import { blake2b256 } from '../hash';

@@ -9,4 +9,4 @@ import { abi, coder, type FunctionFragment } from '../abi';

import { ERC721_ABI, VIP180_ABI } from '../utils';
import { assert, buildError, DATA } from '@vechain/sdk-errors';
import { addressUtils } from '../address';
import { assert, DATA, InvalidDataType } from '@vechain/sdk-errors';
import { addressUtils } from '../address-utils';

@@ -120,6 +120,7 @@ /**

} catch (error) {
throw buildError(
'transferToken',
DATA.INVALID_DATA_TYPE,
`Invalid 'amount' parameter. Expected an integer but received ${amount}.`
throw new InvalidDataType(
'transferToken()',
`Invalid 'amount' parameter. Expected an integer but received ${amount}.`,
{ amount },
error
);

@@ -170,6 +171,7 @@ }

} catch (error) {
throw buildError(
'transferVET',
DATA.INVALID_DATA_TYPE,
`Invalid 'amount' parameter. Expected an integer but received ${amount}.`
throw new InvalidDataType(
'transferVET()',
`Invalid 'amount' parameter. Expected an integer but received ${amount}.`,
{ amount },
error
);

@@ -176,0 +178,0 @@ }

import { Interface as EthersInterface, type InterfaceAbi } from 'ethers';
import {
ABI,
buildError,
ERROR_CODES,
stringifyData
} from '@vechain/sdk-errors';
import { InvalidAbiDataToEncodeOrDecode } from '@vechain/sdk-errors';
import type { BytesLike, Interface, Log, Result } from '../abi';

@@ -39,10 +34,6 @@ import { abi } from '../abi';

} catch (e) {
throw buildError(
'coder.encodeFunctionInput',
ERROR_CODES.ABI.INVALID_DATA_TO_ENCODE,
`Method 'encodeFunctionInput' failed while encoding input for function '${functionName}'. ` +
`Input must match ABI specifications and be correctly formatted.\n` +
`Parameters: ${stringifyData(functionData)}.\n` +
`Ethers' error message: ${(e as Error).message}.`,
{ functionName, functionData },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.encodeFunctionInput()',
`Encoding failed: Data format is invalid. Function data does not match the expected format for ABI type encoding.`,
{ interfaceABI, functionName, functionData },
e

@@ -73,7 +64,6 @@ );

} catch (e) {
throw buildError(
'coder.decodeFunctionInput',
ERROR_CODES.ABI.INVALID_DATA_TO_DECODE,
'Decoding failed: Function input must be properly encoded per ABI specifications.',
{ functionName },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.decodeFunctionInput()',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',
{ interfaceABI, functionName, encodedFunctionInput },
e

@@ -115,7 +105,6 @@ );

} catch (e) {
throw buildError(
'coder.decodeFunctionOutput',
ERROR_CODES.ABI.INVALID_DATA_TO_DECODE,
'Decoding failed: Function output must be properly encoded per ABI specifications.',
{ functionName },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.decodeFunctionOutput()',
`Decoding failed: Data must be a valid hex string encoding a compliant ABI type.`,
{ interfaceABI, functionName, encodedFunctionOutput },
e

@@ -148,8 +137,6 @@ );

} catch (e) {
// Handle errors and throw a custom error with relevant details
throw buildError(
'coder.encodeEventLog',
ERROR_CODES.ABI.INVALID_EVENT,
'Encoding failed: Event log data must align with ABI specifications for encoding.',
{ eventName },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.encodeEventLog()',
`Encoding failed: Data format is invalid. Event data does not match the expected format for ABI type encoding.`,
{ interfaceABI, eventName, dataToEncode },
e

@@ -182,8 +169,6 @@ );

} catch (e) {
// Handle errors and throw a custom error with relevant details
throw buildError(
'coder.decodeEventLog',
ERROR_CODES.ABI.INVALID_EVENT,
'Decoding failed: Event log data must be correctly encoded per ABI specifications.',
{ eventName },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.decodeEventLog()',
`Decoding failed: Data must be a valid hex string encoding a compliant ABI type.`,
{ interfaceABI, eventName, dataToDecode },
e

@@ -217,7 +202,6 @@ );

} catch (e) {
throw buildError(
'coder.parseLog',
ABI.INVALID_DATA_TO_DECODE,
'Decoding failed: Data and topics must be correctly formatted for ABI-compliant decoding.',
{ data },
throw new InvalidAbiDataToEncodeOrDecode(
'coder.parseLog()',
`Decoding failed: Data must be a valid hex string encoding a compliant ABI type.`,
{ interfaceABI, data, topics },
e

@@ -224,0 +208,0 @@ );

@@ -5,3 +5,3 @@ import { ethers } from 'ethers';

export * from './abi';
export * from './address';
export * from './address-utils';
export * from './bloom';

@@ -22,6 +22,3 @@ export * from './certificate';

// Assertions
export * from './assertions';
// Other libraries
export { ethers as vechain_sdk_core_ethers };

@@ -1,2 +0,2 @@

import { assert, RLP_ERRORS } from '@vechain/sdk-errors';
import { InvalidRLP } from '@vechain/sdk-errors';
import { Hex0x } from '../../../utils';

@@ -7,3 +7,3 @@

*
* @throws{InvalidRLPError} - Will throw an error with a message containing the context if the buffer length is greater than the specified bytes OR if it has leading zero bytes.
* @throws{InvalidRLP} - Will throw an error with a message containing the context if the buffer length is greater than the specified bytes OR if it has leading zero bytes.
* @param buffer - The buffer to validate.

@@ -18,20 +18,29 @@ * @param context - Descriptive context for error messages, usually representing the caller's identity.

): void => {
assert(
'assertCompactFixedHexBlobBuffer',
buffer.length <= bytes,
RLP_ERRORS.INVALID_RLP,
`Validation error: Buffer in ${context} must be at most ${bytes} bytes.`,
{ buffer, context }
);
if (buffer.length > bytes) {
throw new InvalidRLP(
'assertCompactFixedHexBlobBuffer()',
`Validation error: Buffer in ${context} must be at most ${bytes} bytes.`,
{
context,
data: {
buffer,
bytes
}
}
);
}
assert(
'assertCompactFixedHexBlobBuffer',
buffer.length === 0 || buffer[0] !== 0,
RLP_ERRORS.INVALID_RLP,
`Validation error: Buffer in ${context} should not have leading zero bytes.`,
{
buffer,
context
}
);
if (buffer.length !== 0 && buffer[0] === 0) {
throw new InvalidRLP(
'assertCompactFixedHexBlobBuffer()',
`Validation error: Buffer in ${context} should not have leading zero bytes.`,
{
context,
data: {
buffer,
bytes
}
}
);
}
};

@@ -38,0 +47,0 @@

@@ -1,2 +0,2 @@

import { assert, RLP_ERRORS } from '@vechain/sdk-errors';
import { InvalidRLP } from '@vechain/sdk-errors';

@@ -6,3 +6,3 @@ /**

*
* @throws{InvalidRLPError} Will throw an error with a message containing the context if the data length is not equal to the specified bytes.
* @throws{InvalidRLP} Will throw an error with a message containing the context if the data length is not equal to the specified bytes.
* @param data - The data to validate.

@@ -17,9 +17,15 @@ * @param context - Descriptive context for error messages.

): void => {
assert(
'assertFixedHexBlobKindData',
data.length === bytes * 2 + 2,
RLP_ERRORS.INVALID_RLP,
`Validation error: Hex string in ${context} must be exactly ${bytes} bytes in length.`,
{ data, context }
);
if (data.length !== bytes * 2 + 2) {
throw new InvalidRLP(
'assertFixedHexBlobKindData()',
`Validation error: Hex string in ${context} must be exactly ${bytes} bytes in length.`,
{
context,
data: {
data,
bytes
}
}
);
}
};

@@ -30,3 +36,3 @@

*
* @throws{InvalidRLPError} - Will throw an error with a message containing the context if the buffer length is not equal to the specified bytes.
* @throws{InvalidRLP} - Will throw an error with a message containing the context if the buffer length is not equal to the specified bytes.
* @param buffer - The buffer to validate.

@@ -42,11 +48,17 @@ * @param context - Descriptive context for error messages.

): void => {
assert(
'assertFixedHexBlobKindBuffer',
buffer.length === bytes,
RLP_ERRORS.INVALID_RLP,
`Validation error: Buffer in ${context} must be exactly ${bytes} bytes in length.`,
{ buffer, context }
);
if (buffer.length !== bytes) {
throw new InvalidRLP(
'assertFixedHexBlobKindData()',
`Validation error: Hex string in ${context} must be exactly ${bytes} bytes in length.`,
{
context,
data: {
buffer,
bytes
}
}
);
}
};
export { assertFixedHexBlobKindData, assertFixedHexBlobKindBuffer };
import { Hex0x } from '../../../utils';
import { type RLPInput } from '../types';
import { assert, RLP_ERRORS } from '@vechain/sdk-errors';
import { InvalidRLP } from '@vechain/sdk-errors';

@@ -8,3 +8,3 @@ /**

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param data - The input data to validate.

@@ -14,30 +14,42 @@ * @param context - Additional context for error handling.

const assertValidHexBlobKindData = (data: RLPInput, context: string): void => {
assert(
'assertValidHexBlobKindData',
typeof data === 'string',
RLP_ERRORS.INVALID_RLP,
'Validation error: Input must be a string.',
{
data,
context
}
);
if (typeof data !== 'string') {
throw new InvalidRLP(
'assertValidHexBlobKindData()',
`Validation error: Input must be a string.`,
{
context,
data: {
data
}
}
);
}
// Check if data is a valid hex string with '0x' prefix.
assert(
'assertValidHexBlobKindData',
Hex0x.isValid(data as string),
RLP_ERRORS.INVALID_RLP,
"Validation error: Input must be a valid hex string with a '0x' prefix.",
{ data, context }
);
if (!Hex0x.isValid(data)) {
throw new InvalidRLP(
'assertValidHexBlobKindData()',
`Validation error: Input must be a valid hex string with a '0x' prefix.`,
{
context,
data: {
data
}
}
);
}
// Ensure the hex string length is even.
assert(
'assertValidHexBlobKindData',
(data as string).length % 2 === 0,
RLP_ERRORS.INVALID_RLP,
'Validation error: Hex string must have an even length.',
{ data, context }
);
if (data.length % 2 !== 0) {
throw new InvalidRLP(
'assertValidHexBlobKindData()',
`Validation error: Hex string must have an even length.`,
{
context,
data: {
data
}
}
);
}
};

@@ -48,3 +60,3 @@

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param buffer - The buffer to validate.

@@ -57,14 +69,16 @@ * @param context - Additional context for error handling.

): void => {
assert(
'assertValidHexBlobKindBuffer',
Buffer.isBuffer(buffer),
RLP_ERRORS.INVALID_RLP,
'Validation error: Input must be a valid buffer.',
{
buffer,
context
}
);
if (!Buffer.isBuffer(buffer)) {
throw new InvalidRLP(
'assertFixedHexBlobKindData()',
`Validation error: Input must be a valid buffer.`,
{
context,
data: {
buffer
}
}
);
}
};
export { assertValidHexBlobKindData, assertValidHexBlobKindBuffer };
import { dataUtils, Hex, Hex0x } from '../../../utils';
import { type RLPInput } from '../types';
import { assert, RLP_ERRORS } from '@vechain/sdk-errors';
import { InvalidRLP } from '@vechain/sdk-errors';

@@ -8,3 +8,3 @@ /**

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param data - Either a number or a string representing a non-negative integer.

@@ -17,9 +17,14 @@ * @param context - A string representing the context in which this function is used,

// Input data must be either a number or a string.
assert(
'validateNumericKindData',
typeof data === 'number' || typeof data === 'string',
RLP_ERRORS.INVALID_RLP,
`Validation error: Input in ${context} must be a string or number.`,
{ data, context }
);
if (typeof data !== 'number' && typeof data !== 'string') {
throw new InvalidRLP(
'validateNumericKindData()',
`Validation error: Input in ${context} must be a string or number.`,
{
context,
data: {
data
}
}
);
}

@@ -32,3 +37,3 @@ if (typeof data === 'number') {

return BigInt(data as string | number);
return BigInt(data);
};

@@ -44,3 +49,3 @@

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param num - The number to be validated.

@@ -50,12 +55,14 @@ * @param context - A string indicating the context, used for error messaging.

const _validateNumericKindNumber = (num: number, context: string): void => {
assert(
'validateNumericKindNumber',
Number.isSafeInteger(num) && num >= 0,
RLP_ERRORS.INVALID_RLP,
`Validation error: Number in ${context} must be a safe and non-negative integer.`,
{
num,
context
}
);
if (!Number.isSafeInteger(num) || num < 0) {
throw new InvalidRLP(
'_validateNumericKindNumber()',
`Validation error: Number in ${context} must be a safe and non-negative integer.`,
{
context,
data: {
num
}
}
);
}
};

@@ -69,3 +76,3 @@

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param str - A string expected to represent a non-negative integer.

@@ -81,18 +88,28 @@ * @param context - A string indicating the context, for creating meaningful error messages.

// Ensure the string is either a hex or decimal number.
assert(
'_validateNumericKindString',
isHex || isDecimal,
RLP_ERRORS.INVALID_RLP,
`Validation error: String in ${context} must represent a non-negative integer in hex or decimal format.`,
{ str, context }
);
if (!isHex && !isDecimal) {
throw new InvalidRLP(
'_validateNumericKindString()',
`Validation error: String in ${context} must represent a non-negative integer in hex or decimal format.`,
{
context,
data: {
str
}
}
);
}
// Ensure hex numbers are of a valid length.
assert(
'_validateNumericKindString',
!isHex || str.length > 2,
RLP_ERRORS.INVALID_RLP,
`Validation error: Hex string number in ${context} must be of valid length.`,
{ str, context }
);
if (isHex && str.length <= 2) {
throw new InvalidRLP(
'_validateNumericKindString()',
`Validation error: Hex string number in ${context} must be of valid length.`,
{
context,
data: {
str
}
}
);
}
};

@@ -104,3 +121,3 @@

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param buf - The buffer to validate.

@@ -119,18 +136,30 @@ * @param context - A string providing context for error messages.

// If maxBytes is defined, ensure buffer length is within bounds.
assert(
'assertValidNumericKindBuffer',
maxBytes === undefined || buf.length <= maxBytes,
RLP_ERRORS.INVALID_RLP,
`Validation error: Buffer in ${context} must be less than ${maxBytes} bytes.`,
{ maxBytes, context }
);
if (maxBytes !== undefined && buf.length > maxBytes) {
throw new InvalidRLP(
'assertValidNumericKindBuffer()',
`Validation error: Buffer in ${context} must be less than ${maxBytes} bytes.`,
{
context,
data: {
buf,
maxBytes
}
}
);
}
// Ensure the buffer does not have leading zeros, as it's not canonical in integer representation.
assert(
'assertValidNumericKindBuffer',
buf[0] !== 0,
RLP_ERRORS.INVALID_RLP,
`Validation error: Buffer in ${context} must represent a canonical integer (no leading zeros).`,
{ buf, context }
);
if (buf[0] === 0) {
throw new InvalidRLP(
'assertValidNumericKindBuffer()',
`Validation error: Buffer in ${context} must represent a canonical integer (no leading zeros).`,
{
context,
data: {
buf,
maxBytes
}
}
);
}
};

@@ -141,3 +170,3 @@

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param bi - BigInt instance to encode.

@@ -155,10 +184,17 @@ * @param maxBytes - Maximum byte length allowed for the encoding. If undefined, no byte size limit is imposed.

const hex = Hex.of(bi);
assert(
'encodeBigIntToBuffer',
maxBytes === undefined || hex.length <= maxBytes * 2,
RLP_ERRORS.INVALID_RLP,
`Validation error: Encoded number in ${context} must fit within ${maxBytes} bytes.`,
{ maxBytes, hex, context }
);
if (maxBytes !== undefined && hex.length > maxBytes * 2) {
throw new InvalidRLP(
'encodeBigIntToBuffer()',
`Validation error: Encoded number in ${context} must fit within ${maxBytes} bytes.`,
{
context,
data: {
hex,
maxBytes
}
}
);
}
return Buffer.from(hex, 'hex');

@@ -165,0 +201,0 @@ };

import { ScalarKind } from './scalarkind.abstract';
import { type BufferOutput, type DataOutput, type RLPInput } from '../types';
import { assertIsValidBuffer } from '../../../assertions';
import { InvalidRLP } from '@vechain/sdk-errors';

@@ -13,3 +13,3 @@ /**

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param data - The data to encode, expected to be of buffer type.

@@ -21,6 +21,16 @@ * @param context - Descriptive context for error messages

// Ensure that the data is indeed a Buffer before encoding.
assertIsValidBuffer('data', data, context);
if (!Buffer.isBuffer(data))
throw new InvalidRLP(
'BufferKind.data()',
`Validation error: Expected a Buffer type in ${context}.`,
{
context,
data: {
data
}
}
);
return {
encode: () => data as Buffer // Data is already a Buffer, so return as-is.
encode: () => data // Data is already a Buffer, so return as-is.
};

@@ -32,3 +42,3 @@ }

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param buffer - The buffer to decode, expected to be of buffer type.

@@ -39,4 +49,14 @@ * @param context - Descriptive context for error messages, usually representing the caller's identity.

public buffer(buffer: Buffer, context: string): BufferOutput {
// Ensure that the provided parameter is indeed a Buffer before decoding.
assertIsValidBuffer('buffer', buffer, context);
// Ensure that the data is indeed a Buffer before encoding.
if (!Buffer.isBuffer(buffer))
throw new InvalidRLP(
'BufferKind.buffer()',
`Validation error: Expected a Buffer type in ${context}.`,
{
context,
data: {
buffer
}
}
);

@@ -43,0 +63,0 @@ return {

@@ -10,4 +10,3 @@ import { RLP } from '@ethereumjs/rlp';

import { RLPProfiles } from '.';
import { assert, RLP_ERRORS } from '@vechain/sdk-errors';
import { assertIsArray } from '../../assertions';
import { InvalidRLP } from '@vechain/sdk-errors';

@@ -69,3 +68,3 @@ /**

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param obj - The object data to be packed.

@@ -99,3 +98,15 @@ * @param profile - RLP_CODER profile for encoding structures.

// Valid RLP array
assertIsArray('packData', obj, context);
if (!Array.isArray(obj)) {
throw new InvalidRLP(
'_packData()',
`Validation error: Expected an array in ${context}.`,
{
context,
data: {
obj,
profile
}
}
);
}

@@ -119,3 +130,3 @@ // ArrayKind: recursively pack each array item based on the shared item profile.

*
* @throws{InvalidRLPError}
* @throws{InvalidRLP}
* @param packed - The packed data to be unpacked.

@@ -139,9 +150,15 @@ * @param profile - RLP_CODER profile for decoding structures.

if (kind instanceof RLPProfiles.ScalarKind) {
assert(
'_unpackData',
Buffer.isBuffer(packed) || packed instanceof Uint8Array,
RLP_ERRORS.INVALID_RLP,
'Unpacking error: Expected data type is Buffer.',
{ context }
);
if (!Buffer.isBuffer(packed) && !(packed instanceof Uint8Array)) {
throw new InvalidRLP(
'_unpackData()',
`Unpacking error: Expected data type is Buffer.`,
{
context,
data: {
packed,
profile
}
}
);
}

@@ -157,9 +174,15 @@ if (packed instanceof Uint8Array) packed = Buffer.from(packed);

assert(
'_unpackData',
parts.length === kind.length,
RLP_ERRORS.INVALID_RLP,
`Unpacking error: Expected ${kind.length} items, but got ${parts.length}.`,
{ context }
);
if (kind.length !== parts.length) {
throw new InvalidRLP(
'_unpackData()',
`Unpacking error: Expected ${kind.length} items, but got ${parts.length}.`,
{
context,
data: {
packed,
profile
}
}
);
}

@@ -177,3 +200,15 @@ return kind.reduce(

// Valid RLP array
assertIsArray('_unpackData', packed, context);
if (!Array.isArray(packed)) {
throw new InvalidRLP(
'_unpackData()',
`Validation error: Expected an array in ${context}.`,
{
context,
data: {
packed,
profile
}
}
);
}

@@ -180,0 +215,0 @@ // ArrayKind: Recursively unpack each array item based on the shared item profile.

import { Hex, Hex0x } from '../utils';
import { assertIsValidReturnType } from '../assertions';
import { blake2b } from '@noble/hashes/blake2b';
import { buildError, DATA } from '@vechain/sdk-errors';
import { hexToBytes } from '@noble/hashes/utils';
import { txt } from '../utils/txt/txt';
import { type ReturnType } from './types';
import { InvalidDataType } from '@vechain/sdk-errors';

@@ -83,3 +82,11 @@ /* --- Overloaded functions start --- */

): Uint8Array | string {
assertIsValidReturnType('blake2b256', returnType);
// Assert that the returnType is valid
if (!['hex', 'buffer'].includes(returnType)) {
throw new InvalidDataType(
'blake2b256()',
"Validation error: Invalid return type. Return type in hash function must be 'buffer' or 'hex'.",
{ returnType }
);
}
if (data instanceof Uint8Array) {

@@ -124,3 +131,11 @@ const hash = blake2b256OfArray(data);

): string | Uint8Array {
assertIsValidReturnType('blake2b256', returnType);
// Assert that the returnType is valid
if (!['hex', 'buffer'].includes(returnType)) {
throw new InvalidDataType(
'blake2b256OfHex()',
"Validation error: Invalid return type. Return type in hash function must be 'buffer' or 'hex'.",
{ returnType }
);
}
try {

@@ -130,5 +145,4 @@ const hash = blake2b256OfArray(hexToBytes(Hex.canon(hex)));

} catch (e) {
throw buildError(
throw new InvalidDataType(
'blake2b256OfHex',
DATA.INVALID_DATA_TYPE,
(e as Error).message,

@@ -135,0 +149,0 @@ { hex },

@@ -1,5 +0,5 @@

import { assertIsValidReturnType } from '../assertions';
import { Hex0x } from '../utils';
import { keccak_256 } from '@noble/hashes/sha3';
import { type ReturnType } from './types';
import { InvalidDataType } from '@vechain/sdk-errors';

@@ -65,3 +65,10 @@ /* --- Overloaded functions start --- */

// Assert that the returnType is valid
assertIsValidReturnType('keccak256', returnType);
if (!['hex', 'buffer'].includes(returnType)) {
throw new InvalidDataType(
'keccak256()',
"Validation error: Invalid return type. Return type in hash function must be 'buffer' or 'hex'.",
{ returnType }
);
}
const hash = keccak_256(data);

@@ -68,0 +75,0 @@ return returnType === 'buffer' ? hash : Hex0x.of(hash);

import { Hex0x } from '../utils';
import { assertIsValidReturnType } from '../assertions';
import { sha256 as _sha256 } from '@noble/hashes/sha256';
import { type ReturnType } from './types';
import { InvalidDataType } from '@vechain/sdk-errors';

@@ -59,3 +59,10 @@ /* --- Overloaded functions start --- */

// Assert that the returnType is valid
assertIsValidReturnType('sha256', returnType);
if (!['hex', 'buffer'].includes(returnType)) {
throw new InvalidDataType(
'sha256()',
"Validation error: Invalid return type. Return type in hash function must be 'buffer' or 'hex'.",
{ returnType }
);
}
const hash = _sha256(data);

@@ -62,0 +69,0 @@ return returnType === 'buffer' ? hash : Hex0x.of(hash);

@@ -13,3 +13,3 @@ /**

import { secp256k1 } from '../../../secp256k1';
import { addressUtils } from '../../../address';
import { addressUtils } from '../../../address-utils';
import { Hex0x } from '../../../utils';

@@ -16,0 +16,0 @@ import { SCRYPT_PARAMS } from './const';

@@ -11,3 +11,3 @@ /**

import { type Keystore, type KeystoreAccount } from '../../types';
import { addressUtils } from '../../../address';
import { addressUtils } from '../../../address-utils';
import { Hex, Hex0x } from '../../../utils';

@@ -14,0 +14,0 @@ import { secp256k1 } from '../../../secp256k1';

import * as bip39 from '@scure/bip39';
import { HDNode } from '../hdnode';
import { MNEMONIC_WORDLIST_ALLOWED_SIZES } from '../utils';
import { addressUtils } from '../address';
import { addressUtils } from '../address-utils';
import { assert, buildError, HDNODE } from '@vechain/sdk-errors';

@@ -6,0 +6,0 @@ import { secp256k1 } from '../secp256k1';

import * as n_utils from '@noble/curves/abstract/utils';
import { Hex, SIGNATURE_LENGTH } from '../utils';
import { assert, SECP256K1 } from '@vechain/sdk-errors';
import {
assert,
InvalidSecp256k1MessageHash,
InvalidSecp256k1PrivateKey,
SECP256K1
} from '@vechain/sdk-errors';
import { randomBytes as _randomBytes } from '@noble/hashes/utils';
import { secp256k1 as n_secp256k1 } from '@noble/curves/secp256k1';
import {
assertIsValidPrivateKey,
assertIsValidSecp256k1MessageHash
} from '../assertions';

@@ -58,7 +59,10 @@ /**

): Uint8Array {
assertIsValidPrivateKey(
'secp256k1.derivePublicKey',
privateKey,
isValidPrivateKey
);
// Check if the private key is valid.
if (!isValidPrivateKey(privateKey)) {
throw new InvalidSecp256k1PrivateKey(
'secp256k1.derivePublicKey()',
'Invalid private key given as input. Ensure it is a valid 32-byte secp256k1 private key.',
undefined
);
}
return n_secp256k1.getPublicKey(privateKey, isCompressed);

@@ -172,7 +176,10 @@ }

function recover(messageHash: Uint8Array, sig: Uint8Array): Uint8Array {
assertIsValidSecp256k1MessageHash(
'secp256k1.recover',
messageHash,
isValidMessageHash
);
// Check if the message hash is valid.
if (!isValidMessageHash(messageHash)) {
throw new InvalidSecp256k1MessageHash(
'secp256k1.sign()',
'Invalid message hash given as input. Ensure it is a valid 32-byte message hash.',
{ messageHash }
);
}

@@ -220,8 +227,20 @@ assert(

function sign(messageHash: Uint8Array, privateKey: Uint8Array): Uint8Array {
assertIsValidSecp256k1MessageHash(
'secp256k1.sign',
messageHash,
isValidMessageHash
);
assertIsValidPrivateKey('secp256k1.sign', privateKey, isValidPrivateKey);
// Check if the message hash is valid.
if (!isValidMessageHash(messageHash)) {
throw new InvalidSecp256k1MessageHash(
'secp256k1.sign()',
'Invalid message hash given as input. Ensure it is a valid 32-byte message hash.',
{ messageHash }
);
}
// Check if the private key is valid.
if (!isValidPrivateKey(privateKey)) {
throw new InvalidSecp256k1PrivateKey(
'secp256k1.sign()',
'Invalid private key given as input. Ensure it is a valid 32-byte secp256k1 private key.',
undefined
);
}
const sig = n_secp256k1.sign(messageHash, privateKey);

@@ -228,0 +247,0 @@ return n_utils.concatBytes(

@@ -1,7 +0,10 @@

import { addressUtils } from '../../address';
import { addressUtils } from '../../address-utils';
import { secp256k1 } from '../../secp256k1';
import { Transaction } from '../transaction';
import { assert, TRANSACTION } from '@vechain/sdk-errors';
import {
assert,
InvalidSecp256k1PrivateKey,
TRANSACTION
} from '@vechain/sdk-errors';
import { type TransactionBody } from '../types';
import { assertIsValidTransactionSigningPrivateKey } from '../../assertions';

@@ -20,8 +23,10 @@ /**

): Transaction {
// Invalid private key
assertIsValidTransactionSigningPrivateKey(
'sign',
signerPrivateKey,
secp256k1.isValidPrivateKey
);
// Check if the private key is valid
if (!secp256k1.isValidPrivateKey(signerPrivateKey)) {
throw new InvalidSecp256k1PrivateKey(
`TransactionHandler.sign()`,
"Invalid private key used to sign the transaction. Ensure it's a valid secp256k1 private key.",
undefined
);
}

@@ -32,3 +37,3 @@ const transactionToSign = new Transaction(transactionBody);

assert(
'sign',
'sign()',
!transactionToSign.isDelegated,

@@ -65,15 +70,20 @@ TRANSACTION.INVALID_DELEGATION,

// Invalid private keys (signer and delegator)
assertIsValidTransactionSigningPrivateKey(
'signWithDelegator',
signerPrivateKey,
secp256k1.isValidPrivateKey,
'signer'
);
assertIsValidTransactionSigningPrivateKey(
'signWithDelegator',
delegatorPrivateKey,
secp256k1.isValidPrivateKey,
'delegator'
);
// Check if the private key of the signer is valid
if (!secp256k1.isValidPrivateKey(signerPrivateKey)) {
throw new InvalidSecp256k1PrivateKey(
`TransactionHandler.signWithDelegator()`,
"Invalid signer private key used to sign the transaction. Ensure it's a valid secp256k1 private key.",
undefined
);
}
// Check if the private key of the delegator is valid
if (!secp256k1.isValidPrivateKey(delegatorPrivateKey)) {
throw new InvalidSecp256k1PrivateKey(
`TransactionHandler.signWithDelegator()`,
"Invalid delegator private key used to sign the transaction. Ensure it's a valid secp256k1 private key.",
undefined
);
}
const transactionToSign = new Transaction(transactionBody);

@@ -80,0 +90,0 @@

@@ -1,2 +0,2 @@

import { addressUtils } from '../address';
import { addressUtils } from '../address-utils';
import { type RLPValidObject } from '../encoding';

@@ -15,4 +15,9 @@ import { blake2b256 } from '../hash';

import { type TransactionBody } from './types';
import { ADDRESS, assert, SECP256K1, TRANSACTION } from '@vechain/sdk-errors';
import { assertCantGetFieldOnUnsignedTransaction } from '../assertions';
import {
ADDRESS,
assert,
SECP256K1,
TRANSACTION,
UnavailableTransactionField
} from '@vechain/sdk-errors';

@@ -112,3 +117,8 @@ /**

// Unsigned transaction (@note we don't check if signature is valid or not, because we have checked it into constructor at creation time)
assertCantGetFieldOnUnsignedTransaction('delegator', this, 'delegator');
if (!this.isSigned)
throw new UnavailableTransactionField(
'Transaction.delegator()',
"Transaction is not signed. 'delegator' information is unavailable.",
{ fieldName: 'delegator' }
);

@@ -235,3 +245,8 @@ // Slice signature needed to recover public key

// Unsigned transaction (@note we don't check if signature is valid or not, because we have checked it into constructor at creation time)
assertCantGetFieldOnUnsignedTransaction('origin', this, 'origin');
if (!this.isSigned)
throw new UnavailableTransactionField(
'Transaction.origin()',
"Transaction is not signed. 'origin' information is unavailable.",
{ fieldName: 'origin' }
);

@@ -260,3 +275,8 @@ // Slice signature

// Unsigned transaction (@note we don't check if signature is valid or not, because we have checked it into constructor at creation time)
assertCantGetFieldOnUnsignedTransaction('id', this, 'id');
if (!this.isSigned)
throw new UnavailableTransactionField(
'Transaction.id()',
"Transaction is not signed. 'id' information is unavailable.",
{ fieldName: 'id' }
);

@@ -263,0 +283,0 @@ // Return transaction ID

import * as n_utils from '@noble/curves/abstract/utils';
import { ADDRESS, assert, BLOOM, DATA } from '@vechain/sdk-errors';
import { Hex, Hex0x } from '../hex';
import { addressUtils } from '../../address';
import { addressUtils } from '../../address-utils';
import { bloom } from '../../bloom';

@@ -6,0 +6,0 @@

@@ -1,2 +0,2 @@

import { addressUtils } from '../../address';
import { addressUtils } from '../../address-utils';
import { type TransactionClause } from '../../transaction';

@@ -3,0 +3,0 @@ import { TRANSACTIONS_GAS_CONSTANTS } from '../const';

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc