Skip to content

Commit

Permalink
nodejs add getMessageId
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed May 25, 2021
1 parent 0923e15 commit b1935f3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/getMessageId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nodejs-binding": minor
---

Added getMessageId function.
10 changes: 10 additions & 0 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ Checks if a given address is valid.

**Returns** A boolean.

#### getMessageId(message: string): boolean

Returns the message id from a message.

| Param | Type | Description |
| ------- | ------------------- | -------------- |
| message | <code>string</code> | The message id |

**Returns** the message id.

#### retry(messageId: string): Promise<Message>

Retries (promotes or reattaches) the message associated with the given id.
Expand Down
11 changes: 10 additions & 1 deletion bindings/nodejs/examples/10_mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ async function run() {
.node('https://api.hornet-0.testnet.chrysalis2.com')
.build();

client.subscriber().topics(['milestones/confirmed', 'messages']).subscribe((err, data) => {
client.subscriber().topics(['messages']).subscribe((err, data) => {
console.log(client.getMessageId(data.payload));
console.log(data);
})

const message = await client.message()
.index('IOTA.RS BINDING - NODE.JS')
.data('some utf based data')
.submit();
console.log(message.message);
console.log(client.getMessageId(JSON.stringify(message.message)));
console.log(message);
}

run()
1 change: 1 addition & 0 deletions bindings/nodejs/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export declare class Client {
bech32ToHex(address: string): string
hexToBech32(address: string, bech32_hrp?: string): Promise<string>
isAddressValid(address: string): boolean
getMessageId(message: string): string
getMilestone(index: number): Promise<MilestoneMetadata>
getMilestoneUtxoChanges(index: number): Promise<MilestoneUTXOChanges>
getReceipts(): Promise<Receipts[]>
Expand Down
16 changes: 14 additions & 2 deletions bindings/nodejs/native/src/classes/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

#![allow(clippy::unnecessary_wraps)]
use iota_client::{
bee_message::prelude::{Address, MessageId, TransactionId, UtxoInput},
bee_message::prelude::{Address, Message, MessageId, TransactionId, UtxoInput},
bee_rest_api::types::dtos::MessageDto as BeeMessageDto,
AddressOutputsOptions, Client, OutputType, Seed,
};
use neon::prelude::*;
use serde::Deserialize;

use std::str::FromStr;
use std::{convert::TryFrom, str::FromStr};

mod builder;
pub use builder::*;
Expand Down Expand Up @@ -665,5 +666,16 @@ declare_types! {
let is_valid = Client::is_address_valid(address.as_str());
Ok(cx.boolean(is_valid).upcast())
}

method getMessageId(mut cx) {
let message_string = cx.argument::<JsString>(0)?.value();
// Try BeeMessageDto and if it fails Message
let message = match serde_json::from_str::<BeeMessageDto>(&message_string){
Ok(message_dto) => Message::try_from(&message_dto).expect("invalid message"),
Err(_) => serde_json::from_str::<Message>(&message_string).expect("invalid message"),
};
let message_id = message.id().0.to_string();
Ok(cx.string(message_id).upcast())
}
}
}

0 comments on commit b1935f3

Please sign in to comment.