From bcb076c86abccd3aefb59a2fe10925ec1d760e70 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 14:31:14 +0200 Subject: [PATCH 01/28] added parents --- .../types/block/{block.ts => basic_block.ts} | 16 +++++++++---- bindings/nodejs/lib/types/block/index.ts | 3 ++- .../nodejs/lib/types/block/parents/index.ts | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) rename bindings/nodejs/lib/types/block/{block.ts => basic_block.ts} (60%) create mode 100644 bindings/nodejs/lib/types/block/parents/index.ts diff --git a/bindings/nodejs/lib/types/block/block.ts b/bindings/nodejs/lib/types/block/basic_block.ts similarity index 60% rename from bindings/nodejs/lib/types/block/block.ts rename to bindings/nodejs/lib/types/block/basic_block.ts index a05d3223d5..e221548db7 100644 --- a/bindings/nodejs/lib/types/block/block.ts +++ b/bindings/nodejs/lib/types/block/basic_block.ts @@ -1,12 +1,12 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import type { HexEncodedString } from '../utils/hex-encoding'; import { Payload, PayloadDiscriminator } from './payload'; import { Type } from 'class-transformer'; +import { StrongParents, WeakParents, ShallowLikeParents } from './parents'; /** - * Block layout. + * Basic Block layout. */ export class Block { /** @@ -14,9 +14,17 @@ export class Block { */ protocolVersion!: number; /** - * The parent block ids. + * Blocks that are strongly directly approved. */ - parents!: HexEncodedString[]; + strongParents!: StrongParents; + /** + * Blocks that are weakly directly approved. + */ + weakParents!: WeakParents; + /** + * Blocks that are directly referenced to adjust opinion. + */ + shallowLikeParents!: ShallowLikeParents; /** * The payload contents. */ diff --git a/bindings/nodejs/lib/types/block/index.ts b/bindings/nodejs/lib/types/block/index.ts index c0f349c55c..18be76ff01 100644 --- a/bindings/nodejs/lib/types/block/index.ts +++ b/bindings/nodejs/lib/types/block/index.ts @@ -4,8 +4,9 @@ export * from './input/index'; export * from './output/index'; export * from './payload/index'; +export * from './parents/index'; export * from './address'; export * from './id'; -export * from './block'; +export * from './basic_block'; export * from './signature'; diff --git a/bindings/nodejs/lib/types/block/parents/index.ts b/bindings/nodejs/lib/types/block/parents/index.ts new file mode 100644 index 0000000000..dfc0de70c2 --- /dev/null +++ b/bindings/nodejs/lib/types/block/parents/index.ts @@ -0,0 +1,23 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { BlockId } from "../id"; + +type TupMinMax< + T, Min extends number, Max extends number, + A extends (T | undefined)[] = [], O extends boolean = false + > = O extends false ? ( + Min extends A['length'] ? TupMinMax : + TupMinMax + ) : Max extends A['length'] ? A : + TupMinMax; + +export type Parents = TupMinMax; + +// Array of Strongly referenced parents in the range of 1..8 +export type StrongParents = Parents<1, 8>; +// Array of Weakly referenced parents in the range of 0..8 +export type WeakParents = Parents<0, 8>; +// Array of Shallowly referenced parents in the range of 0..8 +export type ShallowLikeParents = Parents<0, 8>; + From 80d5a7f75d844568c58e55e6e09ff476c2a035ff Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 14:35:23 +0200 Subject: [PATCH 02/28] format --- .../nodejs/lib/types/block/parents/index.ts | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/bindings/nodejs/lib/types/block/parents/index.ts b/bindings/nodejs/lib/types/block/parents/index.ts index dfc0de70c2..27c810ceef 100644 --- a/bindings/nodejs/lib/types/block/parents/index.ts +++ b/bindings/nodejs/lib/types/block/parents/index.ts @@ -1,18 +1,27 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { BlockId } from "../id"; +import { BlockId } from '../id'; type TupMinMax< - T, Min extends number, Max extends number, - A extends (T | undefined)[] = [], O extends boolean = false - > = O extends false ? ( - Min extends A['length'] ? TupMinMax : - TupMinMax - ) : Max extends A['length'] ? A : - TupMinMax; + T, + Min extends number, + Max extends number, + A extends (T | undefined)[] = [], + O extends boolean = false, +> = O extends false + ? Min extends A['length'] + ? TupMinMax + : TupMinMax + : Max extends A['length'] + ? A + : TupMinMax; -export type Parents = TupMinMax; +export type Parents = TupMinMax< + BlockId, + Min, + Max +>; // Array of Strongly referenced parents in the range of 1..8 export type StrongParents = Parents<1, 8>; @@ -20,4 +29,3 @@ export type StrongParents = Parents<1, 8>; export type WeakParents = Parents<0, 8>; // Array of Shallowly referenced parents in the range of 0..8 export type ShallowLikeParents = Parents<0, 8>; - From e8c60c12cada801b0aa0027ae02b975b7686b2ac Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 15:12:34 +0200 Subject: [PATCH 03/28] wrapper --- .../lib/types/block/{ => core}/basic_block.ts | 11 +-- .../lib/types/block/core/block_wrapper.ts | 79 +++++++++++++++++++ bindings/nodejs/lib/types/block/core/index.ts | 21 +++++ 3 files changed, 104 insertions(+), 7 deletions(-) rename bindings/nodejs/lib/types/block/{ => core}/basic_block.ts (80%) create mode 100644 bindings/nodejs/lib/types/block/core/block_wrapper.ts create mode 100644 bindings/nodejs/lib/types/block/core/index.ts diff --git a/bindings/nodejs/lib/types/block/basic_block.ts b/bindings/nodejs/lib/types/block/core/basic_block.ts similarity index 80% rename from bindings/nodejs/lib/types/block/basic_block.ts rename to bindings/nodejs/lib/types/block/core/basic_block.ts index e221548db7..a539b55a86 100644 --- a/bindings/nodejs/lib/types/block/basic_block.ts +++ b/bindings/nodejs/lib/types/block/core/basic_block.ts @@ -1,18 +1,15 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Payload, PayloadDiscriminator } from './payload'; +import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; -import { StrongParents, WeakParents, ShallowLikeParents } from './parents'; +import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; +import { BlockWrapper } from './block_wrapper'; /** * Basic Block layout. */ -export class Block { - /** - * The protocol version under which this block operates. - */ - protocolVersion!: number; +export class BasicBlock extends BlockWrapper { /** * Blocks that are strongly directly approved. */ diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts new file mode 100644 index 0000000000..2d6e0c003c --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -0,0 +1,79 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { Ed25519Signature } from '../signature'; + +/** + * All of the block types. + */ +enum BlockType { + /// A Basic block. + Basic = 0, + /// A Verification block. + Verification = 1, +} + +abstract class BlockWrapper { + /** + * The type of block + */ + type: BlockType; + + /** + * Protocol version of the block. + */ + protocolVersion: number; + /** + * Network identifier. + */ + networkId: BigInt; + /** + * The time at which the block was issued. It is a Unix timestamp in nanoseconds. + */ + issuingTime: BigInt; + /** + * The identifier of the slot to which this block commits. + */ + //slotCommitmentId: SlotCommitmentId; + /** + * The slot index of the latest finalized slot. + */ + //latestFinalizedSlot: SlotIndex; + /** + * The identifier of the account that issued this block. + */ + //issuerId: IssuerId; + /** + * The block signature; used to validate issuance capabilities. + */ + signature: Ed25519Signature; + + constructor( + type: BlockType, + protocolVersion: number, + networkId: BigInt, + issuingTime: BigInt, + //slotCommitmentId: SlotCommitmentId, + //latestFinalizedSlot: SlotIndex, + //issuerId: IssuerId, + signature: Ed25519Signature, + ) { + this.type = type; + this.protocolVersion = protocolVersion; + this.networkId = networkId; + this.issuingTime = issuingTime; + //this.slotCommitmentId = slotCommitmentId; + //this.latestFinalizedSlot = latestFinalizedSlot; + //this.issuerId = issuerId; + this.signature = signature; + } + + /** + * The type of block. + */ + getType(): BlockType { + return this.type; + } +} + +export { BlockType, BlockWrapper }; diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts new file mode 100644 index 0000000000..228d5481e2 --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -0,0 +1,21 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { plainToInstance } from 'class-transformer'; +import { BlockType, BlockWrapper } from './block_wrapper'; +import { BasicBlock } from './basic_block'; + +export * from './block_wrapper'; +export * from './basic_block'; + +export const BlockDiscriminator = { + property: 'type', + subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }], +}; + +export function parseBlock(data: any): BlockWrapper { + if (data.type == BlockType.Basic) { + return plainToInstance(BasicBlock, data) as any as BasicBlock; + } + throw new Error('Invalid JSON'); +} From 52c04df64c37d794428b3ae5e51e005fde811798 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 15:15:11 +0200 Subject: [PATCH 04/28] bigint rename --- bindings/nodejs/lib/types/block/core/block_wrapper.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts index 2d6e0c003c..62528dd424 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -26,11 +26,11 @@ abstract class BlockWrapper { /** * Network identifier. */ - networkId: BigInt; + networkId: bigint; /** * The time at which the block was issued. It is a Unix timestamp in nanoseconds. */ - issuingTime: BigInt; + issuingTime: bigint; /** * The identifier of the slot to which this block commits. */ @@ -51,8 +51,8 @@ abstract class BlockWrapper { constructor( type: BlockType, protocolVersion: number, - networkId: BigInt, - issuingTime: BigInt, + networkId: bigint, + issuingTime: bigint, //slotCommitmentId: SlotCommitmentId, //latestFinalizedSlot: SlotIndex, //issuerId: IssuerId, From 9ac9cf221d6346106fa4c88fb363a8d6ac2a5f4d Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 16:19:05 +0200 Subject: [PATCH 05/28] wrapper imports --- bindings/nodejs/lib/client/client.ts | 63 ++++++++++--------- bindings/nodejs/lib/types/block/index.ts | 2 +- .../nodejs/lib/types/client/bridge/client.ts | 6 +- .../nodejs/lib/types/utils/bridge/utils.ts | 4 +- bindings/nodejs/lib/utils/utils.ts | 4 +- 5 files changed, 40 insertions(+), 39 deletions(-) diff --git a/bindings/nodejs/lib/client/client.ts b/bindings/nodejs/lib/client/client.ts index 77c9ccfadc..74baa5e420 100644 --- a/bindings/nodejs/lib/client/client.ts +++ b/bindings/nodejs/lib/client/client.ts @@ -28,11 +28,12 @@ import { FoundryOutput, NftOutput, Output, - Block, + BlockWrapper, BlockId, UnlockCondition, Payload, TransactionPayload, + parseBlock, } from '../types/block'; import { HexEncodedString } from '../utils'; import { @@ -138,7 +139,7 @@ export class Client { /** * Post block in JSON format, returns the block ID. */ - async postBlock(block: Block): Promise { + async postBlock(block: BlockWrapper): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlock', data: { @@ -152,7 +153,7 @@ export class Client { /** * Get block as JSON. */ - async getBlock(blockId: BlockId): Promise { + async getBlock(blockId: BlockId): Promise { const response = await this.methodHandler.callMethod({ name: 'getBlock', data: { @@ -160,8 +161,8 @@ export class Client { }, }); - const parsed = JSON.parse(response) as Response; - return plainToInstance(Block, parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** @@ -239,15 +240,15 @@ export class Client { /** * Submit a payload in a block */ - async postBlockPayload(payload: Payload): Promise<[BlockId, Block]> { + async postBlockPayload(payload: Payload): Promise<[BlockId, BlockWrapper]> { const response = await this.methodHandler.callMethod({ name: 'postBlockPayload', data: { payload, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, Block]>; - const block = plainToInstance(Block, parsed.payload[1]); + const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -345,7 +346,7 @@ export class Client { /** * Post block as raw bytes, returns the block ID. */ - async postBlockRaw(block: Block): Promise { + async postBlockRaw(block: BlockWrapper): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlockRaw', data: { @@ -373,29 +374,29 @@ export class Client { /** * Returns the included block of the transaction. */ - async getIncludedBlock(transactionId: string): Promise { + async getIncludedBlock(transactionId: string): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlock', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; - return plainToInstance(Block, parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** * Returns the metadata of the included block of the transaction. */ - async getIncludedBlockMetadata(transactionId: string): Promise { + async getIncludedBlockMetadata(transactionId: string): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlockMetadata', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; - return plainToInstance(Block, parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** @@ -574,30 +575,30 @@ export class Client { /** * Find all blocks by provided block IDs. */ - async findBlocks(blockIds: BlockId[]): Promise { + async findBlocks(blockIds: BlockId[]): Promise { const response = await this.methodHandler.callMethod({ name: 'findBlocks', data: { blockIds, }, }); - const parsed = JSON.parse(response) as Response; - return plainToInstance(Block, parsed.payload); + const parsed = JSON.parse(response) as Response; + return parsed.payload.map(p => parseBlock(p)); } /** * Retries (promotes or reattaches) a block for provided block id. Block should be * retried only if they are valid and haven't been confirmed for a while. */ - async retry(blockId: BlockId): Promise<[BlockId, Block]> { + async retry(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { const response = await this.methodHandler.callMethod({ name: 'retry', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, Block]>; - const block = plainToInstance(Block, parsed.payload[1]); + const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -610,7 +611,7 @@ export class Client { blockId: BlockId, interval?: number, maxAttempts?: number, - ): Promise<[BlockId, Block][]> { + ): Promise<[BlockId, BlockWrapper][]> { const response = await this.methodHandler.callMethod({ name: 'retryUntilIncluded', data: { @@ -619,10 +620,10 @@ export class Client { maxAttempts, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, Block][]>; - const arr: [BlockId, Block][] = []; + const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper][]>; + const arr: [BlockId, BlockWrapper][] = []; parsed.payload.forEach((payload) => { - arr.push([payload[0], plainToInstance(Block, payload[1])]); + arr.push([payload[0], parseBlock(payload[1])]); }); return arr; @@ -632,30 +633,30 @@ export class Client { * Reattaches blocks for provided block id. Blocks can be reattached only if they are valid and haven't been * confirmed for a while. */ - async reattach(blockId: BlockId): Promise<[BlockId, Block]> { + async reattach(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { const response = await this.methodHandler.callMethod({ name: 'reattach', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, Block]>; - const block = plainToInstance(Block, parsed.payload[1]); + const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } /** * Reattach a block without checking if it should be reattached */ - async reattachUnchecked(blockId: BlockId): Promise<[BlockId, Block]> { + async reattachUnchecked(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { const response = await this.methodHandler.callMethod({ name: 'reattachUnchecked', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, Block]>; - const block = plainToInstance(Block, parsed.payload[1]); + const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } diff --git a/bindings/nodejs/lib/types/block/index.ts b/bindings/nodejs/lib/types/block/index.ts index 18be76ff01..cd40d13fdd 100644 --- a/bindings/nodejs/lib/types/block/index.ts +++ b/bindings/nodejs/lib/types/block/index.ts @@ -5,8 +5,8 @@ export * from './input/index'; export * from './output/index'; export * from './payload/index'; export * from './parents/index'; +export * from './core/index'; export * from './address'; export * from './id'; -export * from './basic_block'; export * from './signature'; diff --git a/bindings/nodejs/lib/types/client/bridge/client.ts b/bindings/nodejs/lib/types/client/bridge/client.ts index 543688217a..b1ff05631a 100644 --- a/bindings/nodejs/lib/types/client/bridge/client.ts +++ b/bindings/nodejs/lib/types/client/bridge/client.ts @@ -5,7 +5,7 @@ import type { Bip44, SecretManagerType, } from '../../secret_manager/secret-manager'; -import type { Block, BlockId, Output, Payload } from '../../block'; +import type { BlockWrapper, BlockId, Output, Payload } from '../../block'; import type { PreparedTransactionData } from '../prepared-transaction-data'; import type { AliasQueryParameter, @@ -48,7 +48,7 @@ export interface __GetOutputsMethod__ { export interface __PostBlockMethod__ { name: 'postBlock'; data: { - block: Block; + block: BlockWrapper; }; } @@ -144,7 +144,7 @@ export interface __GetPeersMethod__ { export interface __PostBlockRawMethod__ { name: 'postBlockRaw'; data: { - block: Block; + block: BlockWrapper; }; } diff --git a/bindings/nodejs/lib/types/utils/bridge/utils.ts b/bindings/nodejs/lib/types/utils/bridge/utils.ts index 41e5829dbe..f3a8a04c25 100644 --- a/bindings/nodejs/lib/types/utils/bridge/utils.ts +++ b/bindings/nodejs/lib/types/utils/bridge/utils.ts @@ -1,7 +1,7 @@ import { Ed25519Signature, HexEncodedString, - Block, + BlockWrapper, TransactionEssence, TransactionPayload, TransactionId, @@ -87,7 +87,7 @@ export interface __ParseBech32AddressMethod__ { export interface __BlockIdMethod__ { name: 'blockId'; data: { - block: Block; + block: BlockWrapper; }; } diff --git a/bindings/nodejs/lib/utils/utils.ts b/bindings/nodejs/lib/utils/utils.ts index f0db4f8956..5ec67b1422 100644 --- a/bindings/nodejs/lib/utils/utils.ts +++ b/bindings/nodejs/lib/utils/utils.ts @@ -5,7 +5,7 @@ import { callUtilsMethod } from '../bindings'; import { Address, HexEncodedString, - Block, + BlockWrapper, Ed25519Signature, TransactionEssence, TransactionPayload, @@ -168,7 +168,7 @@ export class Utils { /** * Returns a block ID (Blake2b256 hash of the block bytes) */ - static blockId(block: Block): BlockId { + static blockId(block: BlockWrapper): BlockId { return callUtilsMethod({ name: 'blockId', data: { From c4ac94692c5924371ba81c468a77c341a1a99bdb Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 16:19:32 +0200 Subject: [PATCH 06/28] fmt --- bindings/nodejs/lib/client/client.ts | 30 ++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/bindings/nodejs/lib/client/client.ts b/bindings/nodejs/lib/client/client.ts index 74baa5e420..d542164b6b 100644 --- a/bindings/nodejs/lib/client/client.ts +++ b/bindings/nodejs/lib/client/client.ts @@ -247,7 +247,9 @@ export class Client { payload, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const parsed = JSON.parse(response) as Response< + [BlockId, BlockWrapper] + >; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -388,7 +390,9 @@ export class Client { /** * Returns the metadata of the included block of the transaction. */ - async getIncludedBlockMetadata(transactionId: string): Promise { + async getIncludedBlockMetadata( + transactionId: string, + ): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlockMetadata', data: { @@ -583,7 +587,7 @@ export class Client { }, }); const parsed = JSON.parse(response) as Response; - return parsed.payload.map(p => parseBlock(p)); + return parsed.payload.map((p) => parseBlock(p)); } /** @@ -597,7 +601,9 @@ export class Client { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const parsed = JSON.parse(response) as Response< + [BlockId, BlockWrapper] + >; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -620,7 +626,9 @@ export class Client { maxAttempts, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper][]>; + const parsed = JSON.parse(response) as Response< + [BlockId, BlockWrapper][] + >; const arr: [BlockId, BlockWrapper][] = []; parsed.payload.forEach((payload) => { arr.push([payload[0], parseBlock(payload[1])]); @@ -640,7 +648,9 @@ export class Client { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const parsed = JSON.parse(response) as Response< + [BlockId, BlockWrapper] + >; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -648,14 +658,18 @@ export class Client { /** * Reattach a block without checking if it should be reattached */ - async reattachUnchecked(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { + async reattachUnchecked( + blockId: BlockId, + ): Promise<[BlockId, BlockWrapper]> { const response = await this.methodHandler.callMethod({ name: 'reattachUnchecked', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response<[BlockId, BlockWrapper]>; + const parsed = JSON.parse(response) as Response< + [BlockId, BlockWrapper] + >; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } From c36647913bf0a0779ac87610fdacef1f928784ac Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 16:43:16 +0200 Subject: [PATCH 07/28] renamed --- bindings/nodejs/lib/types/block/parents/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/nodejs/lib/types/block/parents/index.ts b/bindings/nodejs/lib/types/block/parents/index.ts index 27c810ceef..66cd59eb8a 100644 --- a/bindings/nodejs/lib/types/block/parents/index.ts +++ b/bindings/nodejs/lib/types/block/parents/index.ts @@ -3,7 +3,7 @@ import { BlockId } from '../id'; -type TupMinMax< +type BoxedSlicePrefix< T, Min extends number, Max extends number, @@ -11,13 +11,13 @@ type TupMinMax< O extends boolean = false, > = O extends false ? Min extends A['length'] - ? TupMinMax - : TupMinMax + ? BoxedSlicePrefix + : BoxedSlicePrefix : Max extends A['length'] ? A - : TupMinMax; + : BoxedSlicePrefix; -export type Parents = TupMinMax< +export type Parents = BoxedSlicePrefix< BlockId, Min, Max From 23675c1f87e3f480c5624d64b661125b211a9b01 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 16:57:32 +0200 Subject: [PATCH 08/28] examples --- bindings/nodejs/examples/client/08-data-block.ts | 10 +++++++--- bindings/nodejs/examples/client/10-mqtt.ts | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/bindings/nodejs/examples/client/08-data-block.ts b/bindings/nodejs/examples/client/08-data-block.ts index f9d3fbf125..30bfecad8d 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { + BasicBlock, Client, hexToUtf8, initLogger, @@ -38,9 +39,12 @@ async function run() { const fetchedBlock = await client.getBlock(blockIdAndBlock[0]); console.log('Block data: ', fetchedBlock); - if (fetchedBlock.payload instanceof TaggedDataPayload) { - const payload = fetchedBlock.payload as TaggedDataPayload; - console.log('Decoded data:', hexToUtf8(payload.data)); + if (fetchedBlock instanceof BasicBlock) { + const basic = fetchedBlock as BasicBlock; + if (basic instanceof TaggedDataPayload) { + const payload = basic as TaggedDataPayload; + console.log('Decoded data:', hexToUtf8(payload.data)); + } } } catch (error) { console.error('Error: ', error); diff --git a/bindings/nodejs/examples/client/10-mqtt.ts b/bindings/nodejs/examples/client/10-mqtt.ts index f23ec89d3e..aeed06e9d2 100644 --- a/bindings/nodejs/examples/client/10-mqtt.ts +++ b/bindings/nodejs/examples/client/10-mqtt.ts @@ -1,7 +1,13 @@ // Copyright 2021-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Block, Client, initLogger } from '@iota/sdk'; +import { + BasicBlock, + BlockWrapper, + Client, + initLogger, + parseBlock, +} from '@iota/sdk'; import { plainToInstance } from 'class-transformer'; require('dotenv').config({ path: '.env' }); @@ -34,8 +40,12 @@ async function run() { const parsed = JSON.parse(data); if (parsed.topic == 'blocks') { - const block = plainToInstance(Block, JSON.parse(parsed.payload)); - console.log('payload:', block.payload); + const block = parseBlock(JSON.parse(parsed.payload)); + + if (block instanceof BasicBlock) { + const basic = block as BasicBlock; + console.log('payload:', basic.payload); + } } }; From ae478a751342d3aedbe2e846623d51536a0036a3 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 17:26:48 +0200 Subject: [PATCH 09/28] removed boxedslice --- .../nodejs/lib/types/block/parents/index.ts | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/bindings/nodejs/lib/types/block/parents/index.ts b/bindings/nodejs/lib/types/block/parents/index.ts index 66cd59eb8a..1a863a1fd1 100644 --- a/bindings/nodejs/lib/types/block/parents/index.ts +++ b/bindings/nodejs/lib/types/block/parents/index.ts @@ -3,29 +3,17 @@ import { BlockId } from '../id'; -type BoxedSlicePrefix< - T, - Min extends number, - Max extends number, - A extends (T | undefined)[] = [], - O extends boolean = false, -> = O extends false - ? Min extends A['length'] - ? BoxedSlicePrefix - : BoxedSlicePrefix - : Max extends A['length'] - ? A - : BoxedSlicePrefix; +export type Parents = BlockId[]; -export type Parents = BoxedSlicePrefix< - BlockId, - Min, - Max ->; - -// Array of Strongly referenced parents in the range of 1..8 -export type StrongParents = Parents<1, 8>; -// Array of Weakly referenced parents in the range of 0..8 -export type WeakParents = Parents<0, 8>; -// Array of Shallowly referenced parents in the range of 0..8 -export type ShallowLikeParents = Parents<0, 8>; +/** + * Array of Strongly referenced parents in the range of 1..8 + */ +export type StrongParents = Parents; +/** + * Array of Weakly referenced parents in the range of 0..8 + */ +export type WeakParents = Parents; +/** + * Array of Shallowly referenced parents in the range of 0..8 + */ +export type ShallowLikeParents = Parents; From 01e9a17b8ae975d905b52b888d6235b3ed709e6e Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 18:06:18 +0200 Subject: [PATCH 10/28] ids and getters --- .../lib/types/block/core/block_wrapper.ts | 78 +++++++++++++++---- bindings/nodejs/lib/types/block/id.ts | 4 + 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts index 62528dd424..619cecc869 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -1,6 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 +import { IssuerId, SlotCommitmentId } from '../id'; import { Ed25519Signature } from '../signature'; /** @@ -17,54 +18,54 @@ abstract class BlockWrapper { /** * The type of block */ - type: BlockType; + private type: BlockType; /** * Protocol version of the block. */ - protocolVersion: number; + private protocolVersion: number; /** * Network identifier. */ - networkId: bigint; + private networkId: bigint; /** * The time at which the block was issued. It is a Unix timestamp in nanoseconds. */ - issuingTime: bigint; + private issuingTime: bigint; /** * The identifier of the slot to which this block commits. */ - //slotCommitmentId: SlotCommitmentId; + private slotCommitmentId: SlotCommitmentId; /** * The slot index of the latest finalized slot. */ - //latestFinalizedSlot: SlotIndex; + private latestFinalizedSlot: bigint; /** * The identifier of the account that issued this block. */ - //issuerId: IssuerId; + private issuerId: IssuerId; /** * The block signature; used to validate issuance capabilities. */ - signature: Ed25519Signature; + private signature: Ed25519Signature; constructor( type: BlockType, protocolVersion: number, networkId: bigint, issuingTime: bigint, - //slotCommitmentId: SlotCommitmentId, - //latestFinalizedSlot: SlotIndex, - //issuerId: IssuerId, + slotCommitmentId: SlotCommitmentId, + latestFinalizedSlot: bigint, + issuerId: IssuerId, signature: Ed25519Signature, ) { this.type = type; this.protocolVersion = protocolVersion; this.networkId = networkId; this.issuingTime = issuingTime; - //this.slotCommitmentId = slotCommitmentId; - //this.latestFinalizedSlot = latestFinalizedSlot; - //this.issuerId = issuerId; + this.slotCommitmentId = slotCommitmentId; + this.latestFinalizedSlot = latestFinalizedSlot; + this.issuerId = issuerId; this.signature = signature; } @@ -74,6 +75,55 @@ abstract class BlockWrapper { getType(): BlockType { return this.type; } + + /** + * Protocol version of the block. + */ + getProtocolVersion(): number { + return this.protocolVersion; + } + + /** + * Network identifier. + */ + getNetworkId(): bigint { + return this.networkId; + } + + /** + * The time at which the block was issued. It is a Unix timestamp in nanoseconds. + */ + getIssuingTime(): bigint { + return this.issuingTime; + } + + /** + * The identifier of the slot to which this block commits. + */ + getSlotCommitmentId(): SlotCommitmentId { + return this.slotCommitmentId; + } + + /** + * The slot index of the latest finalized slot. + */ + getLatestFinalizedSlot(): bigint { + return this.latestFinalizedSlot; + } + + /** + * The identifier of the account that issued this block. + */ + getIssuerId(): IssuerId { + return this.issuerId; + } + + /** + * The block signature; used to validate issuance capabilities. + */ + getSignature(): Ed25519Signature { + return this.signature; + } } export { BlockType, BlockWrapper }; diff --git a/bindings/nodejs/lib/types/block/id.ts b/bindings/nodejs/lib/types/block/id.ts index 019c491ec4..129b17c44f 100644 --- a/bindings/nodejs/lib/types/block/id.ts +++ b/bindings/nodejs/lib/types/block/id.ts @@ -12,3 +12,7 @@ export type BlockId = HexEncodedString; export type TokenId = HexEncodedString; export type FoundryId = HexEncodedString; + +export type SlotCommitmentId = HexEncodedString; + +export type IssuerId = HexEncodedString; From fc2561fe0f75588623c32b7153b91b78f3ab8d64 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 18:08:02 +0200 Subject: [PATCH 11/28] readonly --- .../nodejs/lib/types/block/core/block_wrapper.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts index 619cecc869..a3929440be 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -18,36 +18,36 @@ abstract class BlockWrapper { /** * The type of block */ - private type: BlockType; + readonly type: BlockType; /** * Protocol version of the block. */ - private protocolVersion: number; + readonly protocolVersion: number; /** * Network identifier. */ - private networkId: bigint; + readonly networkId: bigint; /** * The time at which the block was issued. It is a Unix timestamp in nanoseconds. */ - private issuingTime: bigint; + readonly issuingTime: bigint; /** * The identifier of the slot to which this block commits. */ - private slotCommitmentId: SlotCommitmentId; + readonly slotCommitmentId: SlotCommitmentId; /** * The slot index of the latest finalized slot. */ - private latestFinalizedSlot: bigint; + readonly latestFinalizedSlot: bigint; /** * The identifier of the account that issued this block. */ - private issuerId: IssuerId; + readonly issuerId: IssuerId; /** * The block signature; used to validate issuance capabilities. */ - private signature: Ed25519Signature; + readonly signature: Ed25519Signature; constructor( type: BlockType, From 9fe511e6553c53f2baf2607a0e3c619aabc6e14a Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 23:08:49 +0200 Subject: [PATCH 12/28] removed unused deps --- bindings/nodejs/examples/client/10-mqtt.ts | 2 -- bindings/nodejs/examples/package.json | 1 - 2 files changed, 3 deletions(-) diff --git a/bindings/nodejs/examples/client/10-mqtt.ts b/bindings/nodejs/examples/client/10-mqtt.ts index aeed06e9d2..d790b2c411 100644 --- a/bindings/nodejs/examples/client/10-mqtt.ts +++ b/bindings/nodejs/examples/client/10-mqtt.ts @@ -3,12 +3,10 @@ import { BasicBlock, - BlockWrapper, Client, initLogger, parseBlock, } from '@iota/sdk'; -import { plainToInstance } from 'class-transformer'; require('dotenv').config({ path: '.env' }); diff --git a/bindings/nodejs/examples/package.json b/bindings/nodejs/examples/package.json index 0f440e4a2e..61c45c6c07 100644 --- a/bindings/nodejs/examples/package.json +++ b/bindings/nodejs/examples/package.json @@ -9,7 +9,6 @@ }, "dependencies": { "@iota/sdk": "link:../", - "class-transformer": "^0.5.1", "dotenv": "^16.0.0", "ts-node": "^10.9.1" }, From 07890646d83d81d0f06c04022165e803facf48a7 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 10 Aug 2023 23:19:33 +0200 Subject: [PATCH 13/28] fmt... --- bindings/nodejs/examples/client/10-mqtt.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bindings/nodejs/examples/client/10-mqtt.ts b/bindings/nodejs/examples/client/10-mqtt.ts index d790b2c411..921212795f 100644 --- a/bindings/nodejs/examples/client/10-mqtt.ts +++ b/bindings/nodejs/examples/client/10-mqtt.ts @@ -1,12 +1,7 @@ // Copyright 2021-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { - BasicBlock, - Client, - initLogger, - parseBlock, -} from '@iota/sdk'; +import { BasicBlock, Client, initLogger, parseBlock } from '@iota/sdk'; require('dotenv').config({ path: '.env' }); From f682ede04bfbfbe8d834a969941a87da5275d9a4 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Fri, 11 Aug 2023 14:12:41 +0200 Subject: [PATCH 14/28] readonly, removed getters --- .../lib/types/block/core/basic_block.ts | 10 ++-- .../lib/types/block/core/block_wrapper.ts | 56 ------------------- 2 files changed, 5 insertions(+), 61 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/basic_block.ts b/bindings/nodejs/lib/types/block/core/basic_block.ts index a539b55a86..97b2b80a2d 100644 --- a/bindings/nodejs/lib/types/block/core/basic_block.ts +++ b/bindings/nodejs/lib/types/block/core/basic_block.ts @@ -13,25 +13,25 @@ export class BasicBlock extends BlockWrapper { /** * Blocks that are strongly directly approved. */ - strongParents!: StrongParents; + readonly strongParents!: StrongParents; /** * Blocks that are weakly directly approved. */ - weakParents!: WeakParents; + readonly weakParents!: WeakParents; /** * Blocks that are directly referenced to adjust opinion. */ - shallowLikeParents!: ShallowLikeParents; + readonly shallowLikeParents!: ShallowLikeParents; /** * The payload contents. */ @Type(() => Payload, { discriminator: PayloadDiscriminator, }) - payload?: Payload; + readonly payload?: Payload; /** * The amount of mana the Account identified by IssuerID is at most * willing to burn for this block. */ - burnedMana!: string; + readonly burnedMana!: string; } diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts index a3929440be..6b8408f0e8 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -68,62 +68,6 @@ abstract class BlockWrapper { this.issuerId = issuerId; this.signature = signature; } - - /** - * The type of block. - */ - getType(): BlockType { - return this.type; - } - - /** - * Protocol version of the block. - */ - getProtocolVersion(): number { - return this.protocolVersion; - } - - /** - * Network identifier. - */ - getNetworkId(): bigint { - return this.networkId; - } - - /** - * The time at which the block was issued. It is a Unix timestamp in nanoseconds. - */ - getIssuingTime(): bigint { - return this.issuingTime; - } - - /** - * The identifier of the slot to which this block commits. - */ - getSlotCommitmentId(): SlotCommitmentId { - return this.slotCommitmentId; - } - - /** - * The slot index of the latest finalized slot. - */ - getLatestFinalizedSlot(): bigint { - return this.latestFinalizedSlot; - } - - /** - * The identifier of the account that issued this block. - */ - getIssuerId(): IssuerId { - return this.issuerId; - } - - /** - * The block signature; used to validate issuance capabilities. - */ - getSignature(): Ed25519Signature { - return this.signature; - } } export { BlockType, BlockWrapper }; From 2853242d356c613a7c1871f1be394606d8ac9511 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Fri, 11 Aug 2023 14:56:04 +0200 Subject: [PATCH 15/28] renamed to Block --- bindings/nodejs/lib/client/client.ts | 58 +++++++------------ .../lib/types/block/core/basic_block.ts | 4 +- .../lib/types/block/core/block_wrapper.ts | 4 +- bindings/nodejs/lib/types/block/core/index.ts | 4 +- .../nodejs/lib/types/client/bridge/client.ts | 6 +- .../nodejs/lib/types/utils/bridge/utils.ts | 4 +- bindings/nodejs/lib/utils/utils.ts | 4 +- 7 files changed, 35 insertions(+), 49 deletions(-) diff --git a/bindings/nodejs/lib/client/client.ts b/bindings/nodejs/lib/client/client.ts index d542164b6b..10999a8d97 100644 --- a/bindings/nodejs/lib/client/client.ts +++ b/bindings/nodejs/lib/client/client.ts @@ -28,7 +28,7 @@ import { FoundryOutput, NftOutput, Output, - BlockWrapper, + Block, BlockId, UnlockCondition, Payload, @@ -139,7 +139,7 @@ export class Client { /** * Post block in JSON format, returns the block ID. */ - async postBlock(block: BlockWrapper): Promise { + async postBlock(block: Block): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlock', data: { @@ -153,7 +153,7 @@ export class Client { /** * Get block as JSON. */ - async getBlock(blockId: BlockId): Promise { + async getBlock(blockId: BlockId): Promise { const response = await this.methodHandler.callMethod({ name: 'getBlock', data: { @@ -161,7 +161,7 @@ export class Client { }, }); - const parsed = JSON.parse(response) as Response; + const parsed = JSON.parse(response) as Response; return parseBlock(parsed.payload); } @@ -240,16 +240,14 @@ export class Client { /** * Submit a payload in a block */ - async postBlockPayload(payload: Payload): Promise<[BlockId, BlockWrapper]> { + async postBlockPayload(payload: Payload): Promise<[BlockId, Block]> { const response = await this.methodHandler.callMethod({ name: 'postBlockPayload', data: { payload, }, }); - const parsed = JSON.parse(response) as Response< - [BlockId, BlockWrapper] - >; + const parsed = JSON.parse(response) as Response<[BlockId, Block]>; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -348,7 +346,7 @@ export class Client { /** * Post block as raw bytes, returns the block ID. */ - async postBlockRaw(block: BlockWrapper): Promise { + async postBlockRaw(block: Block): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlockRaw', data: { @@ -376,30 +374,28 @@ export class Client { /** * Returns the included block of the transaction. */ - async getIncludedBlock(transactionId: string): Promise { + async getIncludedBlock(transactionId: string): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlock', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; + const parsed = JSON.parse(response) as Response; return parseBlock(parsed.payload); } /** * Returns the metadata of the included block of the transaction. */ - async getIncludedBlockMetadata( - transactionId: string, - ): Promise { + async getIncludedBlockMetadata(transactionId: string): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlockMetadata', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; + const parsed = JSON.parse(response) as Response; return parseBlock(parsed.payload); } @@ -579,14 +575,14 @@ export class Client { /** * Find all blocks by provided block IDs. */ - async findBlocks(blockIds: BlockId[]): Promise { + async findBlocks(blockIds: BlockId[]): Promise { const response = await this.methodHandler.callMethod({ name: 'findBlocks', data: { blockIds, }, }); - const parsed = JSON.parse(response) as Response; + const parsed = JSON.parse(response) as Response; return parsed.payload.map((p) => parseBlock(p)); } @@ -594,16 +590,14 @@ export class Client { * Retries (promotes or reattaches) a block for provided block id. Block should be * retried only if they are valid and haven't been confirmed for a while. */ - async retry(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { + async retry(blockId: BlockId): Promise<[BlockId, Block]> { const response = await this.methodHandler.callMethod({ name: 'retry', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response< - [BlockId, BlockWrapper] - >; + const parsed = JSON.parse(response) as Response<[BlockId, Block]>; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -617,7 +611,7 @@ export class Client { blockId: BlockId, interval?: number, maxAttempts?: number, - ): Promise<[BlockId, BlockWrapper][]> { + ): Promise<[BlockId, Block][]> { const response = await this.methodHandler.callMethod({ name: 'retryUntilIncluded', data: { @@ -626,10 +620,8 @@ export class Client { maxAttempts, }, }); - const parsed = JSON.parse(response) as Response< - [BlockId, BlockWrapper][] - >; - const arr: [BlockId, BlockWrapper][] = []; + const parsed = JSON.parse(response) as Response<[BlockId, Block][]>; + const arr: [BlockId, Block][] = []; parsed.payload.forEach((payload) => { arr.push([payload[0], parseBlock(payload[1])]); }); @@ -641,16 +633,14 @@ export class Client { * Reattaches blocks for provided block id. Blocks can be reattached only if they are valid and haven't been * confirmed for a while. */ - async reattach(blockId: BlockId): Promise<[BlockId, BlockWrapper]> { + async reattach(blockId: BlockId): Promise<[BlockId, Block]> { const response = await this.methodHandler.callMethod({ name: 'reattach', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response< - [BlockId, BlockWrapper] - >; + const parsed = JSON.parse(response) as Response<[BlockId, Block]>; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } @@ -658,18 +648,14 @@ export class Client { /** * Reattach a block without checking if it should be reattached */ - async reattachUnchecked( - blockId: BlockId, - ): Promise<[BlockId, BlockWrapper]> { + async reattachUnchecked(blockId: BlockId): Promise<[BlockId, Block]> { const response = await this.methodHandler.callMethod({ name: 'reattachUnchecked', data: { blockId, }, }); - const parsed = JSON.parse(response) as Response< - [BlockId, BlockWrapper] - >; + const parsed = JSON.parse(response) as Response<[BlockId, Block]>; const block = parseBlock(parsed.payload[1]); return [parsed.payload[0], block]; } diff --git a/bindings/nodejs/lib/types/block/core/basic_block.ts b/bindings/nodejs/lib/types/block/core/basic_block.ts index 97b2b80a2d..e90208caae 100644 --- a/bindings/nodejs/lib/types/block/core/basic_block.ts +++ b/bindings/nodejs/lib/types/block/core/basic_block.ts @@ -4,12 +4,12 @@ import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { BlockWrapper } from './block_wrapper'; +import { Block } from './block_wrapper'; /** * Basic Block layout. */ -export class BasicBlock extends BlockWrapper { +export class BasicBlock extends Block { /** * Blocks that are strongly directly approved. */ diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block_wrapper.ts index 6b8408f0e8..0d214c553b 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block_wrapper.ts @@ -14,7 +14,7 @@ enum BlockType { Verification = 1, } -abstract class BlockWrapper { +abstract class Block { /** * The type of block */ @@ -70,4 +70,4 @@ abstract class BlockWrapper { } } -export { BlockType, BlockWrapper }; +export { BlockType, Block }; diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index 228d5481e2..36e4d1464f 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { plainToInstance } from 'class-transformer'; -import { BlockType, BlockWrapper } from './block_wrapper'; +import { BlockType, Block } from './block_wrapper'; import { BasicBlock } from './basic_block'; export * from './block_wrapper'; @@ -13,7 +13,7 @@ export const BlockDiscriminator = { subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }], }; -export function parseBlock(data: any): BlockWrapper { +export function parseBlock(data: any): Block { if (data.type == BlockType.Basic) { return plainToInstance(BasicBlock, data) as any as BasicBlock; } diff --git a/bindings/nodejs/lib/types/client/bridge/client.ts b/bindings/nodejs/lib/types/client/bridge/client.ts index b1ff05631a..543688217a 100644 --- a/bindings/nodejs/lib/types/client/bridge/client.ts +++ b/bindings/nodejs/lib/types/client/bridge/client.ts @@ -5,7 +5,7 @@ import type { Bip44, SecretManagerType, } from '../../secret_manager/secret-manager'; -import type { BlockWrapper, BlockId, Output, Payload } from '../../block'; +import type { Block, BlockId, Output, Payload } from '../../block'; import type { PreparedTransactionData } from '../prepared-transaction-data'; import type { AliasQueryParameter, @@ -48,7 +48,7 @@ export interface __GetOutputsMethod__ { export interface __PostBlockMethod__ { name: 'postBlock'; data: { - block: BlockWrapper; + block: Block; }; } @@ -144,7 +144,7 @@ export interface __GetPeersMethod__ { export interface __PostBlockRawMethod__ { name: 'postBlockRaw'; data: { - block: BlockWrapper; + block: Block; }; } diff --git a/bindings/nodejs/lib/types/utils/bridge/utils.ts b/bindings/nodejs/lib/types/utils/bridge/utils.ts index f3a8a04c25..41e5829dbe 100644 --- a/bindings/nodejs/lib/types/utils/bridge/utils.ts +++ b/bindings/nodejs/lib/types/utils/bridge/utils.ts @@ -1,7 +1,7 @@ import { Ed25519Signature, HexEncodedString, - BlockWrapper, + Block, TransactionEssence, TransactionPayload, TransactionId, @@ -87,7 +87,7 @@ export interface __ParseBech32AddressMethod__ { export interface __BlockIdMethod__ { name: 'blockId'; data: { - block: BlockWrapper; + block: Block; }; } diff --git a/bindings/nodejs/lib/utils/utils.ts b/bindings/nodejs/lib/utils/utils.ts index 5ec67b1422..f0db4f8956 100644 --- a/bindings/nodejs/lib/utils/utils.ts +++ b/bindings/nodejs/lib/utils/utils.ts @@ -5,7 +5,7 @@ import { callUtilsMethod } from '../bindings'; import { Address, HexEncodedString, - BlockWrapper, + Block, Ed25519Signature, TransactionEssence, TransactionPayload, @@ -168,7 +168,7 @@ export class Utils { /** * Returns a block ID (Blake2b256 hash of the block bytes) */ - static blockId(block: BlockWrapper): BlockId { + static blockId(block: Block): BlockId { return callUtilsMethod({ name: 'blockId', data: { From 460791132d9e2a41077b25d1fcb3668778f9b6bb Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 14 Aug 2023 19:50:30 +0200 Subject: [PATCH 16/28] updated Slotcommit and u64 usage, renamed some files --- .../{context_input.ts => context-input.ts} | 2 +- .../lib/types/block/context_input/index.ts | 2 +- .../core/{basic_block.ts => basic-block.ts} | 2 +- .../core/{block_wrapper.ts => block-wrapper.ts} | 16 +++++++++------- bindings/nodejs/lib/types/block/core/index.ts | 8 ++++---- bindings/nodejs/lib/types/block/id.ts | 2 -- bindings/nodejs/lib/types/utils/index.ts | 2 +- .../utils/{type_aliases.ts => type-aliases.ts} | 0 8 files changed, 17 insertions(+), 17 deletions(-) rename bindings/nodejs/lib/types/block/context_input/{context_input.ts => context-input.ts} (96%) rename bindings/nodejs/lib/types/block/core/{basic_block.ts => basic-block.ts} (96%) rename bindings/nodejs/lib/types/block/core/{block_wrapper.ts => block-wrapper.ts} (82%) rename bindings/nodejs/lib/types/utils/{type_aliases.ts => type-aliases.ts} (100%) diff --git a/bindings/nodejs/lib/types/block/context_input/context_input.ts b/bindings/nodejs/lib/types/block/context_input/context-input.ts similarity index 96% rename from bindings/nodejs/lib/types/block/context_input/context_input.ts rename to bindings/nodejs/lib/types/block/context_input/context-input.ts index 93a2db90f1..08db055d42 100644 --- a/bindings/nodejs/lib/types/block/context_input/context_input.ts +++ b/bindings/nodejs/lib/types/block/context_input/context-input.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { AccountId } from '../..'; -import { u16 } from '../../utils/type_aliases'; +import { u16 } from '../../utils/type-aliases'; enum ContextInputType { // ToDo: commitment context input; diff --git a/bindings/nodejs/lib/types/block/context_input/index.ts b/bindings/nodejs/lib/types/block/context_input/index.ts index e723cf8e74..36cbdd5a00 100644 --- a/bindings/nodejs/lib/types/block/context_input/index.ts +++ b/bindings/nodejs/lib/types/block/context_input/index.ts @@ -1,4 +1,4 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -export * from './context_input'; +export * from './context-input'; diff --git a/bindings/nodejs/lib/types/block/core/basic_block.ts b/bindings/nodejs/lib/types/block/core/basic-block.ts similarity index 96% rename from bindings/nodejs/lib/types/block/core/basic_block.ts rename to bindings/nodejs/lib/types/block/core/basic-block.ts index e90208caae..9099d89850 100644 --- a/bindings/nodejs/lib/types/block/core/basic_block.ts +++ b/bindings/nodejs/lib/types/block/core/basic-block.ts @@ -4,7 +4,7 @@ import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { Block } from './block_wrapper'; +import { Block } from './block-wrapper'; /** * Basic Block layout. diff --git a/bindings/nodejs/lib/types/block/core/block_wrapper.ts b/bindings/nodejs/lib/types/block/core/block-wrapper.ts similarity index 82% rename from bindings/nodejs/lib/types/block/core/block_wrapper.ts rename to bindings/nodejs/lib/types/block/core/block-wrapper.ts index 0d214c553b..ee3635bd1c 100644 --- a/bindings/nodejs/lib/types/block/core/block_wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/block-wrapper.ts @@ -1,8 +1,10 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { IssuerId, SlotCommitmentId } from '../id'; +import { IssuerId } from '../id'; import { Ed25519Signature } from '../signature'; +import { SlotCommitmentId, SlotIndex } from '../slot'; +import { u64 } from '../../utils/type-aliases'; /** * All of the block types. @@ -27,11 +29,11 @@ abstract class Block { /** * Network identifier. */ - readonly networkId: bigint; + readonly networkId: u64; /** * The time at which the block was issued. It is a Unix timestamp in nanoseconds. */ - readonly issuingTime: bigint; + readonly issuingTime: u64; /** * The identifier of the slot to which this block commits. */ @@ -39,7 +41,7 @@ abstract class Block { /** * The slot index of the latest finalized slot. */ - readonly latestFinalizedSlot: bigint; + readonly latestFinalizedSlot: SlotIndex; /** * The identifier of the account that issued this block. */ @@ -52,10 +54,10 @@ abstract class Block { constructor( type: BlockType, protocolVersion: number, - networkId: bigint, - issuingTime: bigint, + networkId: u64, + issuingTime: u64, slotCommitmentId: SlotCommitmentId, - latestFinalizedSlot: bigint, + latestFinalizedSlot: SlotIndex, issuerId: IssuerId, signature: Ed25519Signature, ) { diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index 36e4d1464f..e3457d8627 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import { plainToInstance } from 'class-transformer'; -import { BlockType, Block } from './block_wrapper'; -import { BasicBlock } from './basic_block'; +import { BlockType, Block } from './block-wrapper'; +import { BasicBlock } from './basic-block'; -export * from './block_wrapper'; -export * from './basic_block'; +export * from './block-wrapper'; +export * from './basic-block'; export const BlockDiscriminator = { property: 'type', diff --git a/bindings/nodejs/lib/types/block/id.ts b/bindings/nodejs/lib/types/block/id.ts index 129b17c44f..b36dc73981 100644 --- a/bindings/nodejs/lib/types/block/id.ts +++ b/bindings/nodejs/lib/types/block/id.ts @@ -13,6 +13,4 @@ export type TokenId = HexEncodedString; export type FoundryId = HexEncodedString; -export type SlotCommitmentId = HexEncodedString; - export type IssuerId = HexEncodedString; diff --git a/bindings/nodejs/lib/types/utils/index.ts b/bindings/nodejs/lib/types/utils/index.ts index 308edeafd9..0685c81039 100644 --- a/bindings/nodejs/lib/types/utils/index.ts +++ b/bindings/nodejs/lib/types/utils/index.ts @@ -3,4 +3,4 @@ export * from './bridge'; export * from './hex-encoding'; -export * from './type_aliases'; +export * from './type-aliases'; diff --git a/bindings/nodejs/lib/types/utils/type_aliases.ts b/bindings/nodejs/lib/types/utils/type-aliases.ts similarity index 100% rename from bindings/nodejs/lib/types/utils/type_aliases.ts rename to bindings/nodejs/lib/types/utils/type-aliases.ts From a044d82a3f5d4cab5388415509c1b4bbbccc8574 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 14 Aug 2023 21:23:43 +0200 Subject: [PATCH 17/28] fixed file name --- bindings/nodejs/lib/types/block/core/basic-block.ts | 2 +- .../lib/types/block/core/{block-wrapper.ts => block.ts} | 0 bindings/nodejs/lib/types/block/core/index.ts | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename bindings/nodejs/lib/types/block/core/{block-wrapper.ts => block.ts} (100%) diff --git a/bindings/nodejs/lib/types/block/core/basic-block.ts b/bindings/nodejs/lib/types/block/core/basic-block.ts index 9099d89850..ebacff0de6 100644 --- a/bindings/nodejs/lib/types/block/core/basic-block.ts +++ b/bindings/nodejs/lib/types/block/core/basic-block.ts @@ -4,7 +4,7 @@ import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { Block } from './block-wrapper'; +import { Block } from './block'; /** * Basic Block layout. diff --git a/bindings/nodejs/lib/types/block/core/block-wrapper.ts b/bindings/nodejs/lib/types/block/core/block.ts similarity index 100% rename from bindings/nodejs/lib/types/block/core/block-wrapper.ts rename to bindings/nodejs/lib/types/block/core/block.ts diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index e3457d8627..fe232810f4 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -2,10 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 import { plainToInstance } from 'class-transformer'; -import { BlockType, Block } from './block-wrapper'; +import { BlockType, Block } from './block'; import { BasicBlock } from './basic-block'; -export * from './block-wrapper'; +export * from './block'; export * from './basic-block'; export const BlockDiscriminator = { From 64c7f29ff3b76c5e174b7b5243663d9116a72bc6 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Tue, 15 Aug 2023 16:08:48 +0200 Subject: [PATCH 18/28] Update bindings/nodejs/lib/types/block/core/block.ts Co-authored-by: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> --- bindings/nodejs/lib/types/block/core/block.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index ee3635bd1c..469a69ac1f 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -21,7 +21,6 @@ abstract class Block { * The type of block */ readonly type: BlockType; - /** * Protocol version of the block. */ From 14ae3431607cdad5d92e971adf0b6254ad4ec40e Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 16 Aug 2023 11:19:16 +0200 Subject: [PATCH 19/28] removed unused blokc tyle --- bindings/nodejs/lib/types/block/core/block.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index ee3635bd1c..f02eed8a5b 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -12,8 +12,6 @@ import { u64 } from '../../utils/type-aliases'; enum BlockType { /// A Basic block. Basic = 0, - /// A Verification block. - Verification = 1, } abstract class Block { From 67b95284b4f7ef88cc6bed4c88329c1d53f49990 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 21 Aug 2023 21:32:24 +0200 Subject: [PATCH 20/28] rewrote a bit --- .../nodejs/examples/client/08-data-block.ts | 7 ++-- bindings/nodejs/examples/client/10-mqtt.ts | 12 +++++-- .../lib/types/block/core/basic-block.ts | 6 ++-- bindings/nodejs/lib/types/block/core/block.ts | 36 ++++++++++++++----- bindings/nodejs/lib/types/block/core/index.ts | 15 +++++--- bindings/nodejs/lib/types/block/signature.ts | 7 +++- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/bindings/nodejs/examples/client/08-data-block.ts b/bindings/nodejs/examples/client/08-data-block.ts index 30bfecad8d..d987da2b07 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -3,6 +3,7 @@ import { BasicBlock, + BlockType, Client, hexToUtf8, initLogger, @@ -39,10 +40,10 @@ async function run() { const fetchedBlock = await client.getBlock(blockIdAndBlock[0]); console.log('Block data: ', fetchedBlock); - if (fetchedBlock instanceof BasicBlock) { + if (fetchedBlock.type === BlockType.Basic) { const basic = fetchedBlock as BasicBlock; - if (basic instanceof TaggedDataPayload) { - const payload = basic as TaggedDataPayload; + if (basic.block.payload instanceof TaggedDataPayload) { + const payload = basic.block.payload as TaggedDataPayload; console.log('Decoded data:', hexToUtf8(payload.data)); } } diff --git a/bindings/nodejs/examples/client/10-mqtt.ts b/bindings/nodejs/examples/client/10-mqtt.ts index 921212795f..5121d324be 100644 --- a/bindings/nodejs/examples/client/10-mqtt.ts +++ b/bindings/nodejs/examples/client/10-mqtt.ts @@ -1,7 +1,13 @@ // Copyright 2021-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { BasicBlock, Client, initLogger, parseBlock } from '@iota/sdk'; +import { + BasicBlock, + BlockType, + Client, + initLogger, + parseBlock, +} from '@iota/sdk'; require('dotenv').config({ path: '.env' }); @@ -35,9 +41,9 @@ async function run() { if (parsed.topic == 'blocks') { const block = parseBlock(JSON.parse(parsed.payload)); - if (block instanceof BasicBlock) { + if (block.type === BlockType.Basic) { const basic = block as BasicBlock; - console.log('payload:', basic.payload); + console.log('payload:', basic.block.payload); } } }; diff --git a/bindings/nodejs/lib/types/block/core/basic-block.ts b/bindings/nodejs/lib/types/block/core/basic-block.ts index ebacff0de6..5079c083d2 100644 --- a/bindings/nodejs/lib/types/block/core/basic-block.ts +++ b/bindings/nodejs/lib/types/block/core/basic-block.ts @@ -4,12 +4,14 @@ import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { Block } from './block'; +import { BlockWrapper } from './block'; + +export type BasicBlock = BlockWrapper; /** * Basic Block layout. */ -export class BasicBlock extends Block { +export class BasicBlockData { /** * Blocks that are strongly directly approved. */ diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 99fad412dc..b9180d7be9 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -5,6 +5,8 @@ import { IssuerId } from '../id'; import { Ed25519Signature } from '../signature'; import { SlotCommitmentId, SlotIndex } from '../slot'; import { u64 } from '../../utils/type-aliases'; +import { Type } from 'class-transformer'; +import { BasicBlockData } from './basic-block'; /** * All of the block types. @@ -14,7 +16,7 @@ enum BlockType { Basic = 0, } -abstract class Block { +class BlockWrapper { /** * The type of block */ @@ -22,31 +24,45 @@ abstract class Block { /** * Protocol version of the block. */ - readonly protocolVersion: number; + readonly protocolVersion!: number; /** * Network identifier. */ - readonly networkId: u64; + readonly networkId!: u64; /** * The time at which the block was issued. It is a Unix timestamp in nanoseconds. */ - readonly issuingTime: u64; + readonly issuingTime!: u64; /** * The identifier of the slot to which this block commits. */ - readonly slotCommitmentId: SlotCommitmentId; + readonly slotCommitmentId!: SlotCommitmentId; /** * The slot index of the latest finalized slot. */ - readonly latestFinalizedSlot: SlotIndex; + readonly latestFinalizedSlot!: SlotIndex; /** * The identifier of the account that issued this block. */ - readonly issuerId: IssuerId; + readonly issuerId!: IssuerId; + + @Type((info) => { + // Generics doesnt get covered in class-transformer out of the box + let type = info?.object['type']; + switch (type) { + case BlockType.Basic: + return BasicBlockData; + default: + throw new Error('Unknown block type: ${type}'); + } + }) + readonly block!: T; + /** * The block signature; used to validate issuance capabilities. */ - readonly signature: Ed25519Signature; + @Type(() => Ed25519Signature) + readonly signature!: Ed25519Signature; constructor( type: BlockType, @@ -56,6 +72,7 @@ abstract class Block { slotCommitmentId: SlotCommitmentId, latestFinalizedSlot: SlotIndex, issuerId: IssuerId, + block: T, signature: Ed25519Signature, ) { this.type = type; @@ -66,7 +83,8 @@ abstract class Block { this.latestFinalizedSlot = latestFinalizedSlot; this.issuerId = issuerId; this.signature = signature; + this.block = block; } } -export { BlockType, Block }; +export { BlockType, BlockWrapper }; diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index fe232810f4..75448b69cf 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -2,20 +2,27 @@ // SPDX-License-Identifier: Apache-2.0 import { plainToInstance } from 'class-transformer'; -import { BlockType, Block } from './block'; -import { BasicBlock } from './basic-block'; +import { BasicBlock, BasicBlockData } from './basic-block'; +import { BlockType, BlockWrapper } from './block'; export * from './block'; export * from './basic-block'; +export type Block = BasicBlock; + export const BlockDiscriminator = { property: 'type', - subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }], + subTypes: [ + { value: BlockWrapper, name: BlockType.Basic as any }, + ], }; export function parseBlock(data: any): Block { if (data.type == BlockType.Basic) { - return plainToInstance(BasicBlock, data) as any as BasicBlock; + return plainToInstance( + BlockWrapper, + data, + ) as any as BlockWrapper; } throw new Error('Invalid JSON'); } diff --git a/bindings/nodejs/lib/types/block/signature.ts b/bindings/nodejs/lib/types/block/signature.ts index 9f7ec48d6a..f1eee0387b 100644 --- a/bindings/nodejs/lib/types/block/signature.ts +++ b/bindings/nodejs/lib/types/block/signature.ts @@ -46,4 +46,9 @@ class Ed25519Signature extends Signature { } } -export { SignatureType, Ed25519Signature, Signature }; +const SignatureDiscriminator = { + property: 'type', + subTypes: [{ value: Ed25519Signature, name: SignatureType.Ed25519 as any }], +}; + +export { SignatureDiscriminator, SignatureType, Ed25519Signature, Signature }; From de3fa74fc68ea3f0434e309929ef395b401c6cec Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 21 Aug 2023 21:37:33 +0200 Subject: [PATCH 21/28] lint --- bindings/nodejs/lib/types/block/core/block.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index b9180d7be9..b3f8c40ae7 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -48,7 +48,7 @@ class BlockWrapper { @Type((info) => { // Generics doesnt get covered in class-transformer out of the box - let type = info?.object['type']; + const type = info?.object['type']; switch (type) { case BlockType.Basic: return BasicBlockData; From 0f6e0d2b223a9fa38523c441ebe6ac15dab0e662 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Fri, 11 Aug 2023 12:15:58 +0200 Subject: [PATCH 22/28] validationblock --- bindings/nodejs/lib/types/block/core/block.ts | 2 + bindings/nodejs/lib/types/block/core/index.ts | 12 +++--- .../lib/types/block/core/validation-block.ts | 41 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 bindings/nodejs/lib/types/block/core/validation-block.ts diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index b3f8c40ae7..62a2f38ae1 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -14,6 +14,8 @@ import { BasicBlockData } from './basic-block'; enum BlockType { /// A Basic block. Basic = 0, + /// A Validation block. + Validation = 1, } class BlockWrapper { diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index 75448b69cf..eeb5ee75b5 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -4,25 +4,27 @@ import { plainToInstance } from 'class-transformer'; import { BasicBlock, BasicBlockData } from './basic-block'; import { BlockType, BlockWrapper } from './block'; +import { ValidationBlock, ValidationBlockData } from './validation-block'; export * from './block'; export * from './basic-block'; +export * from './validation-block'; -export type Block = BasicBlock; +export type Block = BasicBlock | ValidationBlock; export const BlockDiscriminator = { property: 'type', subTypes: [ { value: BlockWrapper, name: BlockType.Basic as any }, + { value: BlockWrapper, name: BlockType.Validation as any } ], }; export function parseBlock(data: any): Block { if (data.type == BlockType.Basic) { - return plainToInstance( - BlockWrapper, - data, - ) as any as BlockWrapper; + return plainToInstance(BlockWrapper, data) as any as BasicBlock; + } else if (data.type == BlockType.Validation) { + return plainToInstance(BlockWrapper, data) as any as ValidationBlock; } throw new Error('Invalid JSON'); } diff --git a/bindings/nodejs/lib/types/block/core/validation-block.ts b/bindings/nodejs/lib/types/block/core/validation-block.ts new file mode 100644 index 0000000000..3d4ecc943a --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/validation-block.ts @@ -0,0 +1,41 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; +import { HexEncodedString } from '../../utils'; +import { BlockWrapper } from './block'; + +export type ValidationBlock = BlockWrapper; + +/** + * A Validation Block is a special type of block used by validators to secure the network. + * It is recognised by the Congestion Control of the IOTA 2.0 protocol and can be issued without + * burning Mana within the constraints of the allowed validator throughput. + * + * It is allowed to reference more parent blocks than a normal Basic Block. + * Validation Block uses the introduced Block ID as its identifier. + */ +export class ValidationBlockData { + /** + * Blocks that are strongly directly approved. + */ + strongParents!: StrongParents; + /** + * Blocks that are weakly directly approved. + */ + weakParents!: WeakParents; + /** + * Blocks that are directly referenced to adjust opinion. + */ + shallowLikeParents!: ShallowLikeParents; + + /** + * The highest supported protocol version the issuer of this block supports. + */ + highestSupportedVersion!: number; + + /** + * The hash of the protocol parameters for the Highest Supported Version. + */ + protocolParametersHash!: HexEncodedString; +} From 05a16a8322d0a90f61a9487f3c714d63a6d82718 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 14 Aug 2023 21:16:41 +0200 Subject: [PATCH 23/28] correct block import --- .../nodejs/lib/types/block/core/validation-block.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/validation-block.ts b/bindings/nodejs/lib/types/block/core/validation-block.ts index 3d4ecc943a..4a3bc31a7b 100644 --- a/bindings/nodejs/lib/types/block/core/validation-block.ts +++ b/bindings/nodejs/lib/types/block/core/validation-block.ts @@ -19,23 +19,23 @@ export class ValidationBlockData { /** * Blocks that are strongly directly approved. */ - strongParents!: StrongParents; + readonly strongParents!: StrongParents; /** * Blocks that are weakly directly approved. */ - weakParents!: WeakParents; + readonly weakParents!: WeakParents; /** * Blocks that are directly referenced to adjust opinion. */ - shallowLikeParents!: ShallowLikeParents; + readonly shallowLikeParents!: ShallowLikeParents; /** * The highest supported protocol version the issuer of this block supports. */ - highestSupportedVersion!: number; + readonly highestSupportedVersion!: number; /** * The hash of the protocol parameters for the Highest Supported Version. */ - protocolParametersHash!: HexEncodedString; + readonly protocolParametersHash!: HexEncodedString; } From 3cf42ce77332aa1459ca73a871c924c9c19de01b Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 21 Sep 2023 18:15:14 +0200 Subject: [PATCH 24/28] old and fmt --- .../nodejs/examples/client/08-data-block.ts | 2 - .../lib/types/block/core/basic-block.ts | 39 ------------------ bindings/nodejs/lib/types/block/core/block.ts | 1 + .../lib/types/block/core/validation-block.ts | 41 ------------------- 4 files changed, 1 insertion(+), 82 deletions(-) delete mode 100644 bindings/nodejs/lib/types/block/core/basic-block.ts delete mode 100644 bindings/nodejs/lib/types/block/core/validation-block.ts diff --git a/bindings/nodejs/examples/client/08-data-block.ts b/bindings/nodejs/examples/client/08-data-block.ts index 70cdc9fcbe..6ba245b7be 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -2,8 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import { - BasicBlock, - BlockType, Client, hexToUtf8, initLogger, diff --git a/bindings/nodejs/lib/types/block/core/basic-block.ts b/bindings/nodejs/lib/types/block/core/basic-block.ts deleted file mode 100644 index 5079c083d2..0000000000 --- a/bindings/nodejs/lib/types/block/core/basic-block.ts +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -import { Payload, PayloadDiscriminator } from '../payload'; -import { Type } from 'class-transformer'; -import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { BlockWrapper } from './block'; - -export type BasicBlock = BlockWrapper; - -/** - * Basic Block layout. - */ -export class BasicBlockData { - /** - * Blocks that are strongly directly approved. - */ - readonly strongParents!: StrongParents; - /** - * Blocks that are weakly directly approved. - */ - readonly weakParents!: WeakParents; - /** - * Blocks that are directly referenced to adjust opinion. - */ - readonly shallowLikeParents!: ShallowLikeParents; - /** - * The payload contents. - */ - @Type(() => Payload, { - discriminator: PayloadDiscriminator, - }) - readonly payload?: Payload; - /** - * The amount of mana the Account identified by IssuerID is at most - * willing to burn for this block. - */ - readonly burnedMana!: string; -} diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 10ed5cb99a..51cc4f8774 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { BasicBlock } from './basic'; +import { ValidationBlock } from './validation'; /** * All of the block types. */ diff --git a/bindings/nodejs/lib/types/block/core/validation-block.ts b/bindings/nodejs/lib/types/block/core/validation-block.ts deleted file mode 100644 index 4a3bc31a7b..0000000000 --- a/bindings/nodejs/lib/types/block/core/validation-block.ts +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { HexEncodedString } from '../../utils'; -import { BlockWrapper } from './block'; - -export type ValidationBlock = BlockWrapper; - -/** - * A Validation Block is a special type of block used by validators to secure the network. - * It is recognised by the Congestion Control of the IOTA 2.0 protocol and can be issued without - * burning Mana within the constraints of the allowed validator throughput. - * - * It is allowed to reference more parent blocks than a normal Basic Block. - * Validation Block uses the introduced Block ID as its identifier. - */ -export class ValidationBlockData { - /** - * Blocks that are strongly directly approved. - */ - readonly strongParents!: StrongParents; - /** - * Blocks that are weakly directly approved. - */ - readonly weakParents!: WeakParents; - /** - * Blocks that are directly referenced to adjust opinion. - */ - readonly shallowLikeParents!: ShallowLikeParents; - - /** - * The highest supported protocol version the issuer of this block supports. - */ - readonly highestSupportedVersion!: number; - - /** - * The hash of the protocol parameters for the Highest Supported Version. - */ - readonly protocolParametersHash!: HexEncodedString; -} From 99f4df42bba91ebe1389a116909df9d972c71418 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 21 Sep 2023 18:15:34 +0200 Subject: [PATCH 25/28] validation --- .../nodejs/lib/types/block/core/validation.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 bindings/nodejs/lib/types/block/core/validation.ts diff --git a/bindings/nodejs/lib/types/block/core/validation.ts b/bindings/nodejs/lib/types/block/core/validation.ts new file mode 100644 index 0000000000..713f93cec8 --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/validation.ts @@ -0,0 +1,39 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; +import { HexEncodedString } from '../../utils'; +import { Block } from './block'; + +/** + * A Validation Block is a special type of block used by validators to secure the network. + * It is recognised by the Congestion Control of the IOTA 2.0 protocol and can be issued without + * burning Mana within the constraints of the allowed validator throughput. + * + * It is allowed to reference more parent blocks than a normal Basic Block. + * Validation Block uses the introduced Block ID as its identifier. + */ +export class ValidationBlock extends Block { + /** + * Blocks that are strongly directly approved. + */ + readonly strongParents!: StrongParents; + /** + * Blocks that are weakly directly approved. + */ + readonly weakParents!: WeakParents; + /** + * Blocks that are directly referenced to adjust opinion. + */ + readonly shallowLikeParents!: ShallowLikeParents; + + /** + * The highest supported protocol version the issuer of this block supports. + */ + readonly highestSupportedVersion!: number; + + /** + * The hash of the protocol parameters for the Highest Supported Version. + */ + readonly protocolParametersHash!: HexEncodedString; +} From 4d1cd49640071d0ec6ebcf82a3c4e41c0b84d169 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 21 Sep 2023 18:17:59 +0200 Subject: [PATCH 26/28] fmt --- bindings/nodejs/lib/types/block/core/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index a646951dc6..d5bd231ea7 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -15,6 +15,6 @@ export const BlockDiscriminator = { property: 'type', subTypes: [ { value: BasicBlock, name: BlockType.Basic as any }, - { value: ValidationBlock, name: BlockType.Validation as any } + { value: ValidationBlock, name: BlockType.Validation as any }, ], }; From 1e00c48124ccbd8d707d259a90d38991e78b7e2c Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 21 Sep 2023 18:41:55 +0200 Subject: [PATCH 27/28] is/as validation --- bindings/nodejs/lib/types/block/core/block.ts | 2 +- .../nodejs/lib/types/block/core/wrapper.ts | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 51cc4f8774..42ad18b781 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -58,7 +58,7 @@ export abstract class Block { * @returns The block */ asValidation(): ValidationBlock { - if (this.isBasic()) { + if (this.isValidation()) { return this as unknown as ValidationBlock; } else { throw new Error('invalid downcast of non-ValidationBlock'); diff --git a/bindings/nodejs/lib/types/block/core/wrapper.ts b/bindings/nodejs/lib/types/block/core/wrapper.ts index d8f498802e..5445e30041 100644 --- a/bindings/nodejs/lib/types/block/core/wrapper.ts +++ b/bindings/nodejs/lib/types/block/core/wrapper.ts @@ -8,6 +8,7 @@ import { u64 } from '../../utils/type-aliases'; import { plainToInstance, Type } from 'class-transformer'; import { Block, BlockType } from './block'; import { BasicBlock } from './basic'; +import { ValidationBlock } from './validation'; import { BlockDiscriminator } from './'; /** @@ -92,6 +93,27 @@ class BlockWrapper { throw new Error('invalid downcast of non-BasicBlock'); } } + + /** + * Checks whether the block is a `ValidationBlock`. + * @returns true if it is, otherwise false + */ + isValidation(): boolean { + return this.block.type === BlockType.Validation; + } + + /** + * Gets the block as an actual `ValidationBlock`. + * NOTE: Will throw an error if the block is not a `ValidationBlock`. + * @returns The block + */ + asValidation(): ValidationBlock { + if (this.isValidation()) { + return this.block as unknown as ValidationBlock; + } else { + throw new Error('invalid downcast of non-ValidationBlock'); + } + } } function parseBlockWrapper(data: any): BlockWrapper { From e485042bd8d292dac3f284c339529e8e95bf8619 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Thu, 21 Sep 2023 18:59:51 +0200 Subject: [PATCH 28/28] comment --- bindings/nodejs/lib/types/block/core/validation.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/bindings/nodejs/lib/types/block/core/validation.ts b/bindings/nodejs/lib/types/block/core/validation.ts index 713f93cec8..7a00fc87f6 100644 --- a/bindings/nodejs/lib/types/block/core/validation.ts +++ b/bindings/nodejs/lib/types/block/core/validation.ts @@ -11,7 +11,6 @@ import { Block } from './block'; * burning Mana within the constraints of the allowed validator throughput. * * It is allowed to reference more parent blocks than a normal Basic Block. - * Validation Block uses the introduced Block ID as its identifier. */ export class ValidationBlock extends Block { /**