Stream filter to check the keys

+1 vote

I want to use stream filter to validate my input key and reject incase it fails.

My key should be in format 111-22-3. Any other key like 11111-22222-333 should fail.

Below is the snippet to create a filter to check and match above regex for key.

create streamfilter testfilter '{}' 'function filterstreamitem() {  var item=getfilterstreamitem(); if (item.keys.length<8) { key =; var ncheck= "[0-9]{3}[-][0-9]{2}[-][0-9]{1}"; var ncheck1= new RegExp(ncheck); return ncheck1.test(key)} }'

I am able to create the filter successfully and approved by admin, but still the wrong keys are passing the stream.

Is there anything i am missing?

I think this will be an interesting feature and will be helpful..! Expecting MC to grow more and more with new features everyday :)
asked Feb 21, 2019 by blkchain_enthu

1 Answer

0 votes

There are various mistakes in the smart filter code – for example you're checking the length of the keys array rather than the length of each key within. And you're not returning an error string if a key is wrong.

The basic structure of the code you need is this:

function filterstreamitem()
  var item=getfilterstreamitem();
  for (var k=0; k<item.keys.length; k++) {
    var key=item.keys[k];
    if ([some condition on key])
      return 'Key '+key+' is invalid because ...';

answered Feb 24, 2019 by MultiChain
I am able to create the required filter now and it works.!!
Thanks for the help.!

And just to undertand better, the stream filter will reject any transactions that does not pass the filter right?

Is it the case or the transaction will still be accepted by nodes and created in blocks and will not be available during retrieval as mentioned below?

"Once a filter is approved for a stream, any subsequent items in that stream must pass the filter in order to be considered valid. Note that, unlike transaction filters, stream filters cannot prevent transactions or blocks from being accepted by nodes. Instead, stream filters are checked when retrieving items from a stream using APIs such as liststreamitems. In the responses to these APIs, invalid items show up with "available":false and "data":null and include an error field containing the filter’s rejection message"

Also in my case, I am not able to see the transactions which does not pass the filter when i use liststreamitems.
How can i verify the transactions which does not pass the filter is still being accepted by nodes.

Can you pls throw some lights to understand better.
Any MultiChain node that has not been modified at the source code level will refuse to publish a stream item that is rejected by an active stream filter.

But if the source code of MultiChain is modified, it is possible for a node to publish stream items that should be rejected. In this case, any node subscribed to the stream will reject that item at the time of retrieval. The rejected item will appear as an item in liststreamitems and other APIs, but (as is said above) it will be shown as "available":false and "data":null and include an error field containing the filter’s rejection message".

If you want to simulate this behavior without changing MultiChain's source code, you can do a little trick with the "pause" command.

Let's say node A has admin permissions for stream S and there is another node W that has write permissions for that stream. Do this:

1. Run "pause incoming" on node W.
2. Create/approve the stream filter for stream S on node A.
3. Publish the bad item from node W (which it allows because it didn't see the stream filter approval transaction, because of "pause incoming" earlier).
4. Subscribe to the stream S on node A and list its items.