Giter Site home page Giter Site logo

mam.js's Introduction

mam.js

We strongly advise that you update your apps to use IOTA Streams - this package is unlikely to be maintained.

Implementation of Masked Authentication Messaging v0 for IOTA in JavaScript, for use with IOTA network.

Installing

Install this package using the following commands:

npm install @iota/mam.js

If you want to use this module in a browser <script> tag see the example Browser ./examples/browser/index.html

Example Usage

const { channelRoot, createChannel, createMessage, parseMessage, mamAttach, mamFetch, mamFetchAll } = require('@iota/mam.js');

// Setup the details for the channel.
const seed = 'ENTER A SEED';
const mode = 'restricted';
const sideKey = 'MYKEY';

// Create a new channel using the details
// You could also load the state from persistence.
const channelState = createChannel(seed, 2, mode, sideKey);

// Create a MAM message using the channel state.
// The returned mamMessage will contain address, root, nextRoot and payload.
// The channel state is also updated, so you should persist it if you want
// to add further messages in the same channel.
// The payload should be attached to the tangle.
const mamMessage = createMessage(channelState, 'MY9MESSAGE');

// Decode the message using the root and sideKey.
// The decodedMessage will contain nextRoot and message.
const decodedMessage = parseMessage(mamMessage.payload, mamMessage.root, sideKey);

// If we want to attach the message to the tangle we first compose the API
const node = "https://chrysalis-nodes.iota.org";
// And then attach the message, tagging it if required.
// Attaching will return the actual transactions attached to the tangle if you need them.
await mamAttach(node, mamMessage, "MY9MAM");

// We can also fetch a message given its root and channel details.
// The fetched data will contain the nextRoot and the message.
const fetched = await mamFetch(node, mamMessage.root, mode, sideKey)

// If you want to fetch multiple messages from a channel
// you need either its initial root (or start from another root).
const channelState = createChannel(seed, 2, mode, sideKey);
const initialRoot = channelRoot(channelState);
const chunkSize = 4;
const chunk = await mamFetchAll(node, initialRoot, mode, sideKey, chunkSize);

// If you want to fetch the next message from a list of channels
const channels = [
  { root: initialRoot1, mode: "restricted", sideKey: "MYKEY" },
  { root: initialRoot2, mode: "public" },
];
// One message for each channel will be retrieved
const fetched = await mamFetchCombined(node, channels);

API Reference

See the API reference for the JavaScript implementation here.

Examples

To see the MAMv0 in action demonstration code using this library can be found in the ./examples/ folder.

License

Apache License Version 2.0 - Copyright (c) 2021 IOTA Stiftung

mam.js's People

Contributors

jmcanterafonseca avatar obany avatar sobolev-alexey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mam.js's Issues

mamFetch doesnt work anymore

Hello

Bug description

The mamFetch Function seems to not be working anymore. Opening a channel still works fine, but fetching the Message from the Channel doesn't work anymore and the mamFetch() returns undefined. (I know it returns that, when the Message can not be found, but the message is visible in the tangle explorer)

I am using the Iota Testnet and the same node to open and fetch the channel.

examples/listen/index.js/line number 23

Bug description

examples/listen/index.js/line number 23

const currentState = fs.readFileSync('../simple/channelState.json');

Need to change, as

const currentState = fs.readFileSync('./channelState.json');

Multi-Leaf Merkle Tree

@obany great work on this repo, I've been checking it out.
I do see at the reference that the channel is hardcoded with count = 1.

count: 1,

Later this is used in the Merkle Tree creation, giving only 1 leaf:

channelState.count,

If we are interested in making many channels with many messages in each channel, as far as I can tell we need to be able to set this count to be much higher (like 2^n).

Thoughts on making this hardcoded 1 a optional variable? Should we make channelState.count a non-hardcoded option to implement the following?

image

mamFetch/mamFetchAll function mismatch parameter

In the API description (here https://github.com/iotaledger/mam.js/blob/chrysalis/docs/modules/mam_client.md#mamfetch)

both mamFetch and mamFetchAll api require a client parameter in format iclient or string.

both functions do not work if the client is passed as iclient, obtained from a SingleNodeClient(iotanode) function;

Functions work only if the client is passed as string containing the IOTA node api's url

I suggest to amend the documentation.

Slow execution of the mamAttach function

This issue is related to the Chrysalis version of the MAM.js lib

I've done many test with the new lib and I've noted that the mamAttach is 3 time slower than the previous version in mainnet. Considering that I've used the testnet, it is a very negative behaviour.

I'd like to move to Streams, but I've lot of code written in node.js and at the moment the stream.js lib is not available.

I've analyzed the cause of the slowdown and I suppose it is caused by the message enconding.

The encoding is done in the client.ts: https://github.com/iotaledger/mam.js/blob/chrysalis/src/mam/client.ts

at line 36 the message payload is converted in byte: data.set(Converter.utf8ToBytes(mamMessage.payload), 1 + tagLength);

This cause the us of une byte of the transaction for each character of the payload leading to a higher MWM required to the pow process.

I guess that the binary encoding will save lot of space reducing the required pow.

Hello,

Bug description

Briefly describe the bug.

Version

Which version of the library are you running?

  • Version:

IOTA network

Which node are you connected to and which IOTA network is it in?

  • Node URL:
  • Network:

Hardware specification

What hardware are you using?

  • Operating system:
  • RAM:
  • Number of cores:
  • Device type:

Steps To reproduce the bug

Explain how the maintainer can reproduce the bug.

Expected behaviour

Describe what you expect to happen.

Actual behaviour

Describe what actually happens.

Errors

Paste any errors that you see.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.