Safe-js tutorial website (WIP)

I’ll push my updates soon. I wasn’t handling a thrown error in my promises and the handleId isn’t returned within a payload object, it’s just response.handleId.

1 Like

OK thx without diving into it I just think it didn’t like the ‘response’ part, was there something I had to import in my function so that the response would exist?

Or maybe I’m just wrong and in that case I’ll give that a try for the ‘handle’

Readme is updated: https://github.com/hunterlester/safe-message-chat/blob/master/README.md

Source updated and running green in in browser: https://github.com/hunterlester/safe-message-chat/blob/master/index.js

I’ll take a look at your repo now.

1 Like

I need to reconfigure the rest of my error handling. I’ve learned that safe-js handles errors by throwing an exception, which will stop further program execution and pass control to the catch block.
throw doc

Take a look at the changes I’ve made to make your code work. You’re really close. I threw you off because I omitted the type_tag and I think I was malforming the data body.

https://github.com/hunterlester/safe-message-chat/blob/master/index.js#L38

1 Like

Awesome thanks so much I’ll try this out when I get home. Really appreciate the help, great to have things to work on for practice as MaidSafe chugs along

Repo source and readme updated: https://github.com/hunterlester/safe-message-chat

This covers creating and getting SD’s with safe-js.
Next, i’ll work on AppendableData and to figure out why my markdown is not rendering as expected.

Let me know if you have any issues.

[Edit: implemented SAFE style guide: safe://chat-demo.kiparis.safenet/]

3 Likes

I would but I don’t understand SAFE-JS or the SAFE API well enough to translate. All I can say is perhaps annotate the code more. And perhaps cite or give a few examples. I’m wondering when you give suboptions for example how they’d be used.

hoping to change that soon!

(preview of the safe-js tutorial safe:// site I’m working on)

1 Like

i found that I needed to change it to auth.handleId to work with the variables in code (which is here to see)

so now I think I’m closer than ever. Still giving me this error though when I run the function (but at least it’s recognizing auth.handleId I think!)

and this is my function:

		function createSD() {
        window.safeStructuredData.create(auth.token, 'eyJhbGciOiJIUzInniJ9.eyJpZCI6Im5RT1poRFJ2VUFLRlVZMzNiRTlnQ25VbVVJSkV0Q2lmYk4zYjE1dXZ2TlU9In0.OTKcHQ9VUKYzBXH_MqeWR4UcHFJV-xlllR68UM9l0b4', 'testdatafilledhere', auth.handleId);
		}

safe-js SD functions, create reference

Your create arguments:
1 Auth token looks good
2 This looks like a JsonWebToken. It needs to be a 32-byte identifier.
3 Currently a string. Needs to be a type tag integer, such as 500 or 501
4 Currently auth.handleId, which I’m not sure is previously assigned. This needs to be your data encoded as a base64 string
5 Need a fifth arg to pass the Cipher Options handle

The error that you are seeing, although it’s not very helpful, is telling you that there is no program logic to deal with an error that has been thrown. In this case, there is need for a catch block to handle the error.

Make another pass at the updated readme. Keep me updated, let’s work through this.

3 Likes

Thanks very much again! Will try when I get home. Turns out cipher encryption options aren’t as easy as they sound lol but once I finally learn it I’ll break it down for everyone!

2 Likes

Hmm ok let me have another go at this, just trying to get a working version first then I’ll try to understand the parts after it works (that’s usually how I function).

  1. Auth looked good so I left it
  2. Didn’t really know what else so I used this site to create a 32bit decimal ID
  3. Chose 500. Don’t know the difference
  4. Used this website to encode my ‘testdatafilledhere’ text and pasted the result
  5. What is the fifth argument?

Newest attempt:

		function createSD() {
        window.safeStructuredData.create(auth.token, '81671021001952541564167282331305022261491608687461560961793937214422214812286', '500', 'dGVzdGRhdGFmaWxsZWRoZXJl');
		}

#IT WORKED!!!?? :smiley:

lol that very strange shot in the dark attempt worked!!

Now a few questions so I understand what happened:

  • what are some good tools / code to automate creation of IDs (question #2 above)
  • also some tools / code to automate conversion of text data to 64bit encoding? (question #4 above)
  • what’s the difference between ‘500’ and ‘501’ (question #3 above) and is there a list somewhere to break these down? are they type tags?
  • how do I GET / view / display my SD now? like if I want to use JS to decrypt my ‘testdatafilledhere’ mesage and display that in a web page?
  • and what is the fifth argument? was it optional? because my code worked without it. Visible at safe://structdata.wom5/ to see it working
2 Likes

what are some good tools / code to automate creation of IDs (question #2 above)

I’m playing around with different ways. Right now using:

let crypto = require('crypto-js');
let generateStructuredDataId = () => {
    return crypto.lib.WordArray.random(32).toString(crypto.enc.Base64);
  };

Would otherwise use the nodejs native crypto library, but I’m just building on front-end, so using crypto-js module.

also some tools / code to automate conversion of text data to 64bit encoding?

In my example, I’m using:

const data = new Buffer(JSON.stringify({
        id: ''
      })).toString('base64')

what’s the difference between ‘500’ and ‘501’ (question #3 above) and is there a list somewhere to break these down? are they type tags?

Yes, type tags
500 is an unversioned SD
501 is an versioned SD
see docs

how do I GET / view / display my SD now? like if I want to use JS to decrypt my ‘testdatafilledhere’ mesage and display that in a web page?

See my readme at Getting an SD
This is will take you through the process

Notice though that the SD 32-byte name is being written and saved to a config file when SD is first created, where the program later retrieves it when accessing that SD: https://github.com/hunterlester/safe-message-chat/blob/master/index.js#L59

and what is the fifth argument? was it optional? because my code worked without it. Visible at safe://structdata.wom5/ to see it working

The fifth argument is only necessary if encrypting an SD with symmetric or asymmetric cipher option. Otherwise by default, as you’ve discovered, the SD is created with a PLAIN cipher option

4 Likes

@whiteoutmashups good to see you and @hunterlester have got this going some.

I never had a chance to dive into SD as I wanted to while working on the browser. And I’ve been slammed personally the last weeks. (That’s all coming to an end and come next week I’ll have some more time to be more active on here and finish some more safe stuff I’ve been playing with :champagne: ). So I’ve never really gotten into using those APIs on the network yet :expressionless:

I’ve been trying to get something going, but how I’ve also found AD/SD confusing and haven’t had time to get any deeper.

From next week I’ll be diving in more again, but i’m also hoping to see some of the newer mutable data APIs, which I believe should be somewhat clearer/cleaner.

2 Likes

OMG very excited to see that!!

Glad I’m not the only one!

It doesn’t seem so bad though, it’s mostly just creating a chunk of encrypted space to put things into, if I understand correctly.

Yes and the FFI’s. I hope we can play with those real soon!

In the meantime we’re still having fun and finishing our little localhost-based safe-js projects, even though they may be obsolete soon. :slight_smile: for practice

1 Like

Tried using this; is ‘crypto-js’ included in the safe-js browser? or do I have to npm install it on my pc?

Getting closer! Got DataID successfully!!

now I’m just trying to display it in a < span> area with my jQuery code here, but it didn’t quite work. didn’t display anything:

		function getSD() {
		window.safeDataId.getStructuredDataHandle(auth.token, '81671121001952541564167282331305022261491608687461560961793937214422214812286', '500').then((res) => {
			got = JSON.parse(res.toString());
		    $('span.poo').text(got);
		 })}

so basically I don’t know how to use or display the info I got from that function ^ above, which it looks like is necessary to use this next function: window.safeStructuredData.getHandle

this is the error that keeps me from displaying the data, I think:

JS can be sooo confusing. T.T

Thanks for helping the community out. I’m still struggling with conversion from Email example client to Email Browser client.

1 Like

Yes, you need to npm install crypto-js to use it.

When in doubt console.log your values for confirmation.

getStructuredDataHandle, if successful, returns an already parsed object with a handleId property.

All you need to do is make got equal to res.handleId

Hit me up if you run into major JS issues that need help sorting.