Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs: rename block types #1706

Merged
merged 6 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bindings/nodejs/examples/client/06-simple-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ async function run() {
issuerId,
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);
const signedBlock = await secretManager.signBlock(unsignedBlock, chain);
const blockId = await client.postBlock(signedBlock);
console.log('Block:', signedBlock, '\n');
const block = await secretManager.signBlock(unsignedBlock, chain);
const blockId = await client.postBlock(block);
console.log('Block:', block, '\n');

console.log(
`Empty block sent: ${process.env.EXPLORER_URL}/block/${blockId}`,
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ async function run() {
issuerId,
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);
const signedBlock = await secretManager.signBlock(unsignedBlock, chain);
const blockId = await client.postBlock(signedBlock);
const block = await secretManager.signBlock(unsignedBlock, chain);
const blockId = await client.postBlock(block);

console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`);

Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/examples/client/10-mqtt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, initLogger, parseSignedBlock } from '@iota/sdk';
import { Client, initLogger, parseBlock } from '@iota/sdk';

require('dotenv').config({ path: '.env' });

Expand Down Expand Up @@ -35,7 +35,7 @@ async function run() {

const parsed = JSON.parse(data);
if (parsed.topic == 'blocks') {
const block = parseSignedBlock(JSON.parse(parsed.payload));
const block = parseBlock(JSON.parse(parsed.payload));

if (block.isBasic()) {
const basic = block.asBasic();
Expand Down
32 changes: 16 additions & 16 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import {
UnlockCondition,
Payload,
SignedTransactionPayload,
parseSignedBlock,
SignedBlock,
parseBlock,
Block,
AccountId,
AnchorId,
NftId,
Expand Down Expand Up @@ -149,7 +149,7 @@ export class Client {
* @param block The block to post.
* @returns The block ID once the block has been posted.
*/
async postBlock(block: SignedBlock): Promise<BlockId> {
async postBlock(block: Block): Promise<BlockId> {
const response = await this.methodHandler.callMethod({
name: 'postBlock',
data: {
Expand All @@ -166,16 +166,16 @@ export class Client {
* @param blockId The corresponding block ID of the requested block.
* @returns The requested block.
*/
async getBlock(blockId: BlockId): Promise<SignedBlock> {
async getBlock(blockId: BlockId): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'getBlock',
data: {
blockId,
},
});

const parsed = JSON.parse(response) as Response<SignedBlock>;
return parseSignedBlock(parsed.payload);
const parsed = JSON.parse(response) as Response<Block>;
return parseBlock(parsed.payload);
}

/**
Expand Down Expand Up @@ -404,7 +404,7 @@ export class Client {
* @param block The block.
* @returns The ID of the posted block.
*/
async postBlockRaw(block: SignedBlock): Promise<BlockId> {
async postBlockRaw(block: Block): Promise<BlockId> {
const response = await this.methodHandler.callMethod({
name: 'postBlockRaw',
data: {
Expand Down Expand Up @@ -438,15 +438,15 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlock(transactionId: TransactionId): Promise<SignedBlock> {
async getIncludedBlock(transactionId: TransactionId): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlock',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<SignedBlock>;
return parseSignedBlock(parsed.payload);
const parsed = JSON.parse(response) as Response<Block>;
return parseBlock(parsed.payload);
}

/**
Expand All @@ -457,15 +457,15 @@ export class Client {
*/
async getIncludedBlockMetadata(
transactionId: TransactionId,
): Promise<SignedBlock> {
): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlockMetadata',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<SignedBlock>;
return parseSignedBlock(parsed.payload);
const parsed = JSON.parse(response) as Response<Block>;
return parseBlock(parsed.payload);
}

/**
Expand Down Expand Up @@ -582,15 +582,15 @@ export class Client {
* @param blockIds An array of `BlockId`s.
* @returns An array of corresponding blocks.
*/
async findBlocks(blockIds: BlockId[]): Promise<SignedBlock[]> {
async findBlocks(blockIds: BlockId[]): Promise<Block[]> {
const response = await this.methodHandler.callMethod({
name: 'findBlocks',
data: {
blockIds,
},
});
const parsed = JSON.parse(response) as Response<SignedBlock[]>;
return parsed.payload.map((p) => parseSignedBlock(p));
const parsed = JSON.parse(response) as Response<Block[]>;
return parsed.payload.map((p) => parseBlock(p));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions bindings/nodejs/lib/secret_manager/secret-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
Unlock,
Response,
UnsignedBlock,
SignedBlock,
parseSignedBlock,
Block,
parseBlock,
} from '../types';

import { plainToInstance } from 'class-transformer';
Expand Down Expand Up @@ -122,7 +122,7 @@ export class SecretManager {
async signBlock(
unsignedBlock: UnsignedBlock,
chain: Bip44,
): Promise<SignedBlock> {
): Promise<Block> {
const response = await this.methodHandler.callMethod({
name: 'signBlock',
data: {
Expand All @@ -131,8 +131,8 @@ export class SecretManager {
},
});

const parsed = JSON.parse(response) as Response<SignedBlock>;
return parseSignedBlock(parsed.payload);
const parsed = JSON.parse(response) as Response<Block>;
return parseBlock(parsed.payload);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions bindings/nodejs/lib/types/block/core/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import { Payload, PayloadDiscriminator } from '../payload';
import { Type } from 'class-transformer';
import { StrongParents, WeakParents, ShallowLikeParents } from '../parents';
import { Block } from './block';
import { BlockBody } from './block-body';
import { u64 } from '../../utils';

/**
* Basic Block layout.
* Basic Block Body layout.
*/
export class BasicBlock extends Block {
export class BasicBlockBody extends BlockBody {
/**
* Blocks that are strongly directly approved.
*/
Expand Down
67 changes: 67 additions & 0 deletions bindings/nodejs/lib/types/block/core/block-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { BasicBlockBody } from './basic';
import { ValidationBlockBody } from './validation';
/**
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
* All of the block body types.
*/
export enum BlockBodyType {
/// A Basic block body.
Basic = 0,
/// A Validation block body.
Validation = 1,
}

export abstract class BlockBody {
readonly type: BlockBodyType;

/**
* @param type The type of BlockBody.
*/
constructor(type: BlockBodyType) {
this.type = type;
}

/**
* Checks whether the block body is a `BasicBlockBody`.
* @returns true if it is, otherwise false
*/
isBasic(): boolean {
return this.type === BlockBodyType.Basic;
}

/**
* Gets the block body as an actual `BasicBlockBody`.
* NOTE: Will throw an error if the block is not a `BasicBlockBody`.
* @returns The BasicBlockBody
*/
asBasic(): BasicBlockBody {
if (this.isBasic()) {
return this as unknown as BasicBlockBody;
} else {
throw new Error('invalid downcast of non-BasicBlockBody');
}
}

/**
* Checks whether the block body is a `ValidationBlockBody`.
* @returns true if it is, otherwise false
*/
isValidation(): boolean {
return this.type === BlockBodyType.Validation;
}

/**
* Gets the block body as an actual `ValidationBlockBody`.
* NOTE: Will throw an error if the block is not a `ValidationBlockBody`.
* @returns The ValidationBlockBody
*/
asValidation(): ValidationBlockBody {
if (this.isValidation()) {
return this as unknown as ValidationBlockBody;
} else {
throw new Error('invalid downcast of non-ValidationBlockBody');
}
}
}
Loading
Loading