@concordium/common-sdk
Advanced tools
Comparing version
@@ -49,2 +49,16 @@ /// <reference types="node" /> | ||
/** | ||
* Magic prefix for versioned schemas, used to distinguish between a versioned | ||
* schema and the older format without versioning as part of the schema. | ||
* | ||
* The constant is corresponding to two maxed-out bytes, interpreted as a 16-bit unsigned integer. | ||
*/ | ||
export declare const VERSIONED_SCHEMA_PREFIX = 65535; | ||
/** | ||
* Reads a versioned schema for a contract module from the given{@link Readable}. | ||
* | ||
* @param source input stream | ||
* @returns schema (Versioned) of a module (contract map) | ||
*/ | ||
export declare function deserialVersionedModule(source: Readable): VersionedModule; | ||
/** | ||
* schema (V1) for a contract. | ||
@@ -101,3 +115,3 @@ */ | ||
typeTag: ParameterType.Unit | ParameterType.Bool | ParameterType.U8 | ParameterType.U16 | ParameterType.U32 | ParameterType.U64 | ParameterType.U128 | ParameterType.I8 | ParameterType.I16 | ParameterType.I32 | ParameterType.I64 | ParameterType.I128 | ParameterType.Amount | ParameterType.AccountAddress | ParameterType.ContractAddress | ParameterType.Timestamp | ParameterType.Duration; | ||
} | PairType | ListType | MapType | ArrayType | StructType | EnumType | StringType; | ||
} | PairType | ListType | MapType | ArrayType | StructType | EnumType | StringType | ULeb128Type | ILeb128Type | ByteListType | ByteArrayType; | ||
export declare type ContractFunction = { | ||
@@ -159,2 +173,30 @@ parameter?: Type; | ||
/** | ||
* LEB128 for unsigned integers type. | ||
*/ | ||
export declare type ULeb128Type = { | ||
typeTag: ParameterType.ULeb128; | ||
constraint: number; | ||
}; | ||
/** | ||
* LEB128 for signed integers type. | ||
*/ | ||
export declare type ILeb128Type = { | ||
typeTag: ParameterType.ILeb128; | ||
constraint: number; | ||
}; | ||
/** | ||
* List of bytes type. | ||
*/ | ||
export declare type ByteListType = { | ||
typeTag: ParameterType.ByteList; | ||
sizeLength: SizeLength; | ||
}; | ||
/** | ||
* Array of bytes type. | ||
*/ | ||
export declare type ByteArrayType = { | ||
typeTag: ParameterType.ByteArray; | ||
size: number; | ||
}; | ||
/** | ||
* Reads {@link ContractFunction} from the given {@link Readable}. | ||
@@ -299,7 +341,14 @@ * | ||
/** | ||
* @param buffer wasm file buffer | ||
* @param moduleVersion the version of the module | ||
* Reads an unsigned 16-bit integer from the given {@link Readable}. | ||
* | ||
* @param source input stream | ||
* @returns number from 0 to 65535 | ||
*/ | ||
export declare function deserialUint16(source: Readable): number; | ||
/** | ||
* @param buffer Schema buffer | ||
* @param moduleVersion the version of the module (only needed for older versions of the schema). | ||
* @returns deserialized module of wasm file | ||
*/ | ||
export declare function deserialModuleFromBuffer(buffer: Buffer, schemaVersion: SchemaVersion): VersionedModule; | ||
export declare function deserialModuleFromBuffer(buffer: Buffer, schemaVersion?: SchemaVersion): VersionedModule; | ||
export declare function getParameterType(schema: ContractFunction | Type | null, schemaVersion: SchemaVersion): Type | null; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getParameterType = exports.deserialModuleFromBuffer = exports.deserialUint32 = exports.deserialUint8 = exports.deserialTupleFn = exports.deserialOptionFn = exports.OptionTag = exports.deserialMapFn = exports.deserialArrayFn = exports.deserialString = exports.deserialFields = exports.FieldsTag = exports.deserialType = exports.deserialFunction = exports.SizeLength = exports.deserialContractV2 = exports.deserialContractV1 = exports.deserialModuleV2 = exports.deserialModuleV1 = void 0; | ||
exports.getParameterType = exports.deserialModuleFromBuffer = exports.deserialUint16 = exports.deserialUint32 = exports.deserialUint8 = exports.deserialTupleFn = exports.deserialOptionFn = exports.OptionTag = exports.deserialMapFn = exports.deserialArrayFn = exports.deserialString = exports.deserialFields = exports.FieldsTag = exports.deserialType = exports.deserialFunction = exports.SizeLength = exports.deserialContractV2 = exports.deserialContractV1 = exports.deserialVersionedModule = exports.VERSIONED_SCHEMA_PREFIX = exports.deserialModuleV2 = exports.deserialModuleV1 = void 0; | ||
const stream_1 = require("stream"); | ||
@@ -28,2 +28,37 @@ const types_1 = require("./types"); | ||
/** | ||
* Magic prefix for versioned schemas, used to distinguish between a versioned | ||
* schema and the older format without versioning as part of the schema. | ||
* | ||
* The constant is corresponding to two maxed-out bytes, interpreted as a 16-bit unsigned integer. | ||
*/ | ||
exports.VERSIONED_SCHEMA_PREFIX = 65535; | ||
/** | ||
* Reads a versioned schema for a contract module from the given{@link Readable}. | ||
* | ||
* @param source input stream | ||
* @returns schema (Versioned) of a module (contract map) | ||
*/ | ||
function deserialVersionedModule(source) { | ||
const prefix = deserialUint16(source); | ||
if (prefix != exports.VERSIONED_SCHEMA_PREFIX) { | ||
throw new Error('Versioned schema module must be prefixed with two maxed-out bytes'); | ||
} | ||
const version = deserialUint8(source); | ||
switch (version) { | ||
case types_1.SchemaVersion.V1: | ||
return { | ||
v: version, | ||
value: deserialModuleV1(source), | ||
}; | ||
case types_1.SchemaVersion.V2: | ||
return { | ||
v: version, | ||
value: deserialModuleV2(source), | ||
}; | ||
default: | ||
throw new Error('Unsupported schema version'); | ||
} | ||
} | ||
exports.deserialVersionedModule = deserialVersionedModule; | ||
/** | ||
* Reads {@link ContractV1} from the given {@link Readable}. | ||
@@ -175,2 +210,22 @@ * | ||
}; | ||
case types_1.ParameterType.ULeb128: | ||
return { | ||
typeTag: tag, | ||
constraint: deserialUint32(source), | ||
}; | ||
case types_1.ParameterType.ILeb128: | ||
return { | ||
typeTag: tag, | ||
constraint: deserialUint32(source), | ||
}; | ||
case types_1.ParameterType.ByteList: | ||
return { | ||
typeTag: tag, | ||
sizeLength: deserialUint8(source), | ||
}; | ||
case types_1.ParameterType.ByteArray: | ||
return { | ||
typeTag: tag, | ||
size: deserialUint32(source), | ||
}; | ||
default: | ||
@@ -360,4 +415,14 @@ throw new Error(`unsupported type tag: ${tag}`); | ||
/** | ||
* @param buffer wasm file buffer | ||
* @param moduleVersion the version of the module | ||
* Reads an unsigned 16-bit integer from the given {@link Readable}. | ||
* | ||
* @param source input stream | ||
* @returns number from 0 to 65535 | ||
*/ | ||
function deserialUint16(source) { | ||
return source.read(2).readUInt16LE(0); | ||
} | ||
exports.deserialUint16 = deserialUint16; | ||
/** | ||
* @param buffer Schema buffer | ||
* @param moduleVersion the version of the module (only needed for older versions of the schema). | ||
* @returns deserialized module of wasm file | ||
@@ -368,2 +433,8 @@ */ | ||
bufferStream.end(buffer); | ||
if (buffer.readUInt16LE(0) === exports.VERSIONED_SCHEMA_PREFIX) { | ||
return deserialVersionedModule(bufferStream); | ||
} | ||
if (schemaVersion === undefined) { | ||
throw new Error('Supply a schema version to deserialize an unversioned schema'); | ||
} | ||
switch (schemaVersion) { | ||
@@ -370,0 +441,0 @@ case types_1.SchemaVersion.V1: |
import { Buffer } from 'buffer/'; | ||
import { VerifyKey } from '.'; | ||
import { ConfigureDelegationPayload } from './types'; | ||
import { ArrayType, Type, StructType, ListType, PairType, MapType, EnumType, SizeLength, Fields } from './deserializeSchema'; | ||
import { ArrayType, Type, StructType, ListType, PairType, MapType, EnumType, SizeLength, Fields, ULeb128Type, ILeb128Type, ByteListType, ByteArrayType } from './deserializeSchema'; | ||
import { DataBlob } from './types/DataBlob'; | ||
@@ -136,2 +136,16 @@ export declare function serializeMap<K extends string | number | symbol, T>(map: Record<K, T>, encodeSize: (size: number) => Buffer, encodeKey: (k: string) => Buffer, encodeValue: (t: T) => Buffer): Buffer; | ||
/** | ||
* Serialize fixed sized hex string to Buffer | ||
* @param schema Schema for the input | ||
* @param data user input | ||
* @returns Buffer containing serialization of the byte array | ||
*/ | ||
export declare function serializeByteArray(schema: ByteArrayType, data: any): Buffer; | ||
/** | ||
* Serialize hex string to Buffer | ||
* @param schema Schema for the input | ||
* @param data user input | ||
* @returns Buffer containing serialization of the byte list | ||
*/ | ||
export declare function serializeByteList(schema: ByteListType, data: any): Buffer; | ||
/** | ||
* Serialize struct of parameters to Buffer | ||
@@ -179,2 +193,16 @@ * @param structType type of struct | ||
/** | ||
* Serialize a string with an unsigned integer using LEB128 to a Buffer. | ||
* @param uleb128Type type of parameters | ||
* @param data user input | ||
* @returns Buffer containing serialization of unsigned integer | ||
*/ | ||
export declare function serializeULeb128(uleb128Type: ULeb128Type, data: any): Buffer; | ||
/** | ||
* Serialize a string with a signed integer using LEB128 to a Buffer. | ||
* @param ileb128Type type of parameters | ||
* @param data user input | ||
* @returns Buffer containing serialization of unsigned integer | ||
*/ | ||
export declare function serializeILeb128(ileb128Type: ILeb128Type, data: any): Buffer; | ||
/** | ||
* | ||
@@ -181,0 +209,0 @@ * @param length length of the values provided by the user |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.serializeConfigureDelegationPayload = exports.getSerializedConfigureDelegationBitmap = exports.configureDelegationSerializationSpec = exports.orUndefined = exports.isDefined = exports.serializeLength = exports.serializeSchemaFields = exports.serializeEnumType = exports.serializeMapType = exports.serializePairType = exports.serializeListOrSet = exports.serializeStruct = exports.serializeArray = exports.serializeParameters = exports.serializeYearMonth = exports.serializeVerifyKey = exports.encodeStringToByteArray = exports.packBufferWithWord16Length = exports.packBufferWithWord32Length = exports.encodeDataBlob = exports.encodeWord16FromString = exports.encodeWord8FromString = exports.encodeWord8 = exports.encodeInt8 = exports.encodeWord16 = exports.encodeInt16 = exports.encodeWord32 = exports.encodeInt32 = exports.encodeWord64 = exports.encodeBool = exports.encodeInt64 = exports.encodeWord128LE = exports.encodeInt128LE = exports.serializeList = exports.serializeMap = void 0; | ||
exports.serializeConfigureDelegationPayload = exports.getSerializedConfigureDelegationBitmap = exports.configureDelegationSerializationSpec = exports.orUndefined = exports.isDefined = exports.serializeLength = exports.serializeILeb128 = exports.serializeULeb128 = exports.serializeSchemaFields = exports.serializeEnumType = exports.serializeMapType = exports.serializePairType = exports.serializeListOrSet = exports.serializeStruct = exports.serializeByteList = exports.serializeByteArray = exports.serializeArray = exports.serializeParameters = exports.serializeYearMonth = exports.serializeVerifyKey = exports.encodeStringToByteArray = exports.packBufferWithWord16Length = exports.packBufferWithWord32Length = exports.encodeDataBlob = exports.encodeWord16FromString = exports.encodeWord8FromString = exports.encodeWord8 = exports.encodeInt8 = exports.encodeWord16 = exports.encodeInt16 = exports.encodeWord32 = exports.encodeInt32 = exports.encodeWord64 = exports.encodeBool = exports.encodeInt64 = exports.encodeWord128LE = exports.encodeInt128LE = exports.serializeList = exports.serializeMap = void 0; | ||
const buffer_1 = require("buffer/"); | ||
@@ -499,2 +499,10 @@ const types_1 = require("./types"); | ||
return serializeEnumType(paramSchema, userInput); | ||
case types_1.ParameterType.ULeb128: | ||
return serializeULeb128(paramSchema, userInput); | ||
case types_1.ParameterType.ILeb128: | ||
return serializeILeb128(paramSchema, userInput); | ||
case types_1.ParameterType.ByteList: | ||
return serializeByteList(paramSchema, userInput); | ||
case types_1.ParameterType.ByteArray: | ||
return serializeByteArray(paramSchema, userInput); | ||
default: | ||
@@ -533,2 +541,38 @@ throw new Error('Type is not supported currently.'); | ||
/** | ||
* Serialize fixed sized hex string to Buffer | ||
* @param schema Schema for the input | ||
* @param data user input | ||
* @returns Buffer containing serialization of the byte array | ||
*/ | ||
function serializeByteArray(schema, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
data) { | ||
if (typeof data !== 'string' && !(data instanceof String)) { | ||
throw new Error(`Invalid input for type ByteArray, must be a string containing a lowercase hex encoding of ${schema.size} bytes.`); | ||
} | ||
const buffer = buffer_1.Buffer.from(data.toString(), 'hex'); | ||
if (buffer.length !== schema.size) { | ||
throw new Error(`Invalid input for type ByteArray, must be a string containing a lowercase hex encoding of ${schema.size} bytes.`); | ||
} | ||
return buffer; | ||
} | ||
exports.serializeByteArray = serializeByteArray; | ||
/** | ||
* Serialize hex string to Buffer | ||
* @param schema Schema for the input | ||
* @param data user input | ||
* @returns Buffer containing serialization of the byte list | ||
*/ | ||
function serializeByteList(schema, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
data) { | ||
if (typeof data !== 'string' && !(data instanceof String)) { | ||
throw new Error('Invalid input for type ByteArray, must be a string containing a lowercase hex encoding of bytes.'); | ||
} | ||
const bytes = buffer_1.Buffer.from(data.toString(), 'hex'); | ||
const listLengthBuffer = serializeLength(bytes.length, schema.sizeLength); | ||
return buffer_1.Buffer.from([listLengthBuffer, bytes]); | ||
} | ||
exports.serializeByteList = serializeByteList; | ||
/** | ||
* Serialize struct of parameters to Buffer | ||
@@ -714,2 +758,72 @@ * @param structType type of struct | ||
/** | ||
* Serialize a string with an unsigned integer using LEB128 to a Buffer. | ||
* @param uleb128Type type of parameters | ||
* @param data user input | ||
* @returns Buffer containing serialization of unsigned integer | ||
*/ | ||
function serializeULeb128(uleb128Type, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
data) { | ||
if (typeof data !== 'string' && !(data instanceof String)) { | ||
throw new Error('Invalid input for type ULeb128, must be a string containing an unsigned integer'); | ||
} | ||
let value = BigInt(data.toString()); | ||
if (value < 0n) { | ||
throw new Error('Invalid input for type ULeb128, must contain a positive value'); | ||
} | ||
const buffer = []; | ||
for (let i = 0; i < uleb128Type.constraint; i++) { | ||
const byte = Number(BigInt.asUintN(7, value)); | ||
value = value / 128n; // Note: this is integer division | ||
const lastByte = value === 0n; | ||
buffer.push(encodeWord8(lastByte ? byte : byte | 128)); | ||
if (lastByte) { | ||
return buffer_1.Buffer.concat(buffer); | ||
} | ||
} | ||
throw new Error('Invalid LEB128 unsigned integer encoding'); | ||
} | ||
exports.serializeULeb128 = serializeULeb128; | ||
/** | ||
* Serialize a string with a signed integer using LEB128 to a Buffer. | ||
* @param ileb128Type type of parameters | ||
* @param data user input | ||
* @returns Buffer containing serialization of unsigned integer | ||
*/ | ||
function serializeILeb128(ileb128Type, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
data) { | ||
if (typeof data !== 'string' && !(data instanceof String)) { | ||
throw new Error('Invalid input for type ULeb128, must be a string containing a signed integer'); | ||
} | ||
// Since BigInt does not support bitwise right shifting, the current workaround is to convert | ||
// the bigint to a string with the two-complement binary representation and split this into | ||
// chunks for the leb128 encoding. | ||
const value = BigInt(data.toString()); | ||
const isNegative = value < 0; | ||
const unsignedBitString = value.toString(2); | ||
const totalBits = unsignedBitString.length; | ||
const totalBytes = Math.ceil(totalBits / 7); | ||
const binaryString = isNegative | ||
? BigInt.asUintN(totalBytes * 7, value).toString(2) | ||
: unsignedBitString.padStart(totalBytes * 7, '0'); | ||
if (totalBytes > ileb128Type.constraint) { | ||
throw new Error(`'Invalid LEB128 signed integer encoding, the encoding is constraint to at most ${ileb128Type.constraint} bytes`); | ||
} | ||
const buffer = []; | ||
for (let i = 0; i < totalBytes; i++) { | ||
const startIndex = (totalBytes - i - 1) * 7; | ||
const valueBits = binaryString.substring(startIndex, startIndex + 7); | ||
const byte = parseInt(valueBits, 2); | ||
const isLastByte = !(i + 1 < totalBytes); | ||
buffer.push(encodeWord8(!isLastByte | ||
? byte | 128 | ||
: isNegative | ||
? byte | 64 | ||
: byte)); | ||
} | ||
return buffer_1.Buffer.concat(buffer); | ||
} | ||
exports.serializeILeb128 = serializeILeb128; | ||
/** | ||
* | ||
@@ -716,0 +830,0 @@ * @param length length of the values provided by the user |
@@ -962,3 +962,11 @@ import { AccountAddress } from './types/accountAddress'; | ||
/** Receive function name. */ | ||
ReceiveName = 26 | ||
ReceiveName = 26, | ||
/** LEB128 encoding of an unsigned integer */ | ||
ULeb128 = 27, | ||
/** LEB128 encoding of a signed integer */ | ||
ILeb128 = 28, | ||
/** Variable size list of bytes */ | ||
ByteList = 29, | ||
/** Fixed size list of bytes */ | ||
ByteArray = 30 | ||
} | ||
@@ -965,0 +973,0 @@ export interface InstanceInfoCommon { |
@@ -222,2 +222,10 @@ "use strict"; | ||
ParameterType[ParameterType["ReceiveName"] = 26] = "ReceiveName"; | ||
/** LEB128 encoding of an unsigned integer */ | ||
ParameterType[ParameterType["ULeb128"] = 27] = "ULeb128"; | ||
/** LEB128 encoding of a signed integer */ | ||
ParameterType[ParameterType["ILeb128"] = 28] = "ILeb128"; | ||
/** Variable size list of bytes */ | ||
ParameterType[ParameterType["ByteList"] = 29] = "ByteList"; | ||
/** Fixed size list of bytes */ | ||
ParameterType[ParameterType["ByteArray"] = 30] = "ByteArray"; | ||
})(ParameterType = exports.ParameterType || (exports.ParameterType = {})); | ||
@@ -224,0 +232,0 @@ const isInstanceInfoV1 = (info) => info.version === 1; |
{ | ||
"name": "@concordium/common-sdk", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "engines": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
269421
4.31%5315
5.56%1
Infinity%