Joseph's Safe Websites Project

I’m trying it out now on Safe browser 0.4.0 on Linux Mint, but all I see after several minutes is [screenshot]

did you paste the code in the console

If by “console” you mean where it says to enter your message at the bottom then yes, I did.

right click on the website
you will get inspect element
you will see a window called dev tools
click on the console tab and paste my code in to it

1 Like

i saw your message “Hello from Sascha!” still fixing the UI will be done in hopefully today

2 Likes

This is no longer needed, just head to safe://safechat.jam

Instructions

  1. To use website go to safe://safechat.jam

  2. Wait for the website to load

  3. Right click on the website and click on inspect element

  4. This will bring up a new window called dev tools

  5. Click the tab that says console there

  6. Copy and paste(ctrl-c ctrl-v) this code in to there

    //Intial function
     "Use strict";
    
     $('#sendmessage').click(function() {
       authorise();
     });
     $('#refresh').click(function() {
       getMessages();
     });
    
     //initialises and authorises with the network
     (function() {
       var app = {
     name: "Safe Chat",
     id: "joe",
     version: "1",
     vendor: "joe",
       };
    
    
       window.safeApp.initialise(app)
     .then((appHandle) => {
       console.log("Initialise Token: " + appHandle);
       window.safeApp.connect(appHandle)
     .then((appHandle) => {
       //returns app token
       auth = appHandle;
       authorised = false;
       Materialize.toast(" App Token: " + auth, 3000, 'rounded');
       getMutableDataHandle();
     });
     }, (err) => {
       console.error(err);
       Materialize.toast(err, 3000, 'rounded');
     });
     })();
    
     function authorise() {
       if (authorised === false) {
     window.safeApp.free(auth);
     auth = "";
     var app = {
       name: "Safe Chat",
       id: "joe",
       version: "1",
       vendor: "joe",
     };
    
     var permissions = {
       '_public': [
     'Read'
       ]
     };
    
     window.safeApp.initialise(app)
       .then((appHandle) => {
     console.log("Initialise Token: " + appHandle);
     window.safeApp.authorise(appHandle, permissions)
       .then((authURI) => {
     // console.log(auth);
     window.safeApp.connectAuthorised(appHandle, authURI)
       .then((authorisedAppHandle) => {
         //returns authorised app token
         auth = authorisedAppHandle;
         authorised = true;
         Materialize.toast("Authorised App Token: " + auth, 3000, 'rounded');
         getMutableDataHandle();
         sendMessage();
       });
       });
       }, (err) => {
     console.error(err);
     Materialize.toast(err, 3000, 'rounded');
       });
       } else {
     sendMessage();
       }
     }
    
     function getMutableDataHandle() {
       var name = "safechat";
       window.safeCrypto.sha3Hash(auth, name)
     .then((hash) =>
       window.safeMutableData.newPublic(auth, hash, 3000))
     .then((safeChatHandle) => {
       mdHandle = safeChatHandle;
       getMessages();
     });
     }
    
     function getMessages() {
       window.safeMutableData.getEntries(mdHandle)
     .then((entriesHandle) => {
       messages.innerHTML = "";
       var date = new Date();
       var time = date.getTime();
       window.safeMutableDataEntries.forEach(entriesHandle,
     (key, value) => {
    
       if (uintToString(value.buf).length < 300 &&
     uintToString(value.buf) !== "" &&
     parseInt(uintToString(key)) < time &&
     parseInt(uintToString(key)).toString().length === 13 &&
     uintToString(key).length === 13 &&
     uintToString(key).substring(0, 4) == 1502) {
    
     console.log('Key: ', uintToString(key));
     console.log('Value: ', uintToString(value.buf));
     $("#messages").append('<div class="row"><div class="card-panel yellow"><span class="blue-text">' + uintToString(value.buf) +
       '</span></div></div>');
    
       }
       window.scrollTo(0, document.body.scrollHeight);
     });
       window.safeMutableDataEntries.free(entriesHandle);
     }, (err) => {
       console.error(err);
       // Materialize.toast(err, 3000, 'rounded');
     });
     }
    
     function sendMessage() {
       window.safeMutableData.newMutation(auth)
     .then((mutationHandle) => {
       var date = new Date();
       var time = date.getTime();
       window.safeMutableDataMutation.insert(mutationHandle, time.toString(), textarea.value)
     .then(_ =>
       window.safeMutableData.applyEntriesMutation(mdHandle, mutationHandle))
     .then(_ => {
       Materialize.toast('Message has been sent to the network', 3000, 'rounded');
       window.safeMutableDataMutation.free(mutationHandle);
       getMessages();
     });
       textarea.value = "";
    
     });
     }
    
     function uintToString(uintArray) {
       return new TextDecoder("utf-8").decode(uintArray)
     .replace(/&/g, "&amp;")
     .replace(/</g, "&lt;")
     .replace(/>/g, "&gt;")
     .replace(/"/g, "&quot;")
     .replace(/'/g, "&#039;");
        }
    
  7. Press enter and everything should work then

If you have any issues please let me know.

please come by and leave a message

7 Likes

I can’t get anything to work on this now.

Authorise: OK
Check Authentication: error

I also tried copying and pasting your code into the console but that didn’t work

1 Like

I think this is a problem with safe browser, what you can do is get the website up and wait for it to load ( the indicator on the tab will change to the refresh icon) then everything should be ok

No, same thing. Left for ten mins, same error.

are you sure? everything is working on my end, maybe close the browser down and load up the website again (don’t click on the buttons while it is loading)

OK -that worked with Check Authentication. But then I get an error when trying to upload a file.

Seems to be working again now. Strange. One thing worth noting though. If you upload a file that’s too large then it seems to break the whole system. You basically have to shut down the browser and start again or you get a ‘could not connect to the SAFE network’ error on authenticating.

1 Like

I think that has to do with the safe network and its current limits, not too much i can do about that, sorry

Today was not a good day for this…

But I do wonder if that is something we will have to fix, because we will have these situations when someone is uploading and run out of safecoin. The user decides to not spend more uploading the file or simply has no more coin.

@Viv Is this simply a problem with the testnet restrictions or is it a problem that will be there when the user hos no more coin in the middle of an upload.

3 Likes

This just a testnet restriction on the network, I would say the future safe API will check whether you have enough safe coin to upload a file from its filesize

2 Likes

With safecoin & test safecoin users will be warned well in advance they are running low. There are several ways to do this, a safety buffer (i.e. upload last file but stop) is one, but an app should warn users their balance is getting very low well before they lose space. There is also the possibility of local cache for “unfinished” transactions (I dislike local anything mind you).

Now though would be a good time to test perhaps, so warn, warn again then stop. Problem I see is uploading a ton of files to a directory or something. The client app may need to guess the number of put requests etc.

4 Likes

But of course human nature is to ignore warnings and try to fit it in.

So the question is “Will SAFE (nodes or clients) have problems if a file is partly uploaded and stops because of no more coins?

It will happen and we should have a graceful exit rather than having to restart clients/APPs/browsers

4 Likes

I agree, but it will really be the apps that need to not allow uploads where there is not enough credit. If somebody does a partial transaction for instance they will leave that data in an unknown state until they charge up their account. The apps though will need to inform users and possibly prevent a partial transaction I feel. If an app does not then I think it would be seen as a bad app.

An area we must make app devs very aware of as well. There is several ways an app can do that, but perhaps we should provide a “recommended approach” for this.

8 Likes

@dirvine, I gather then that this is a fault of the demo APPs/browser and not of SAFE itself.

So the simple answer is that its the job of the APP to be graceful.

Excuse my conflating demo apps with SAFE, my excuse is morning coffee is only just now kicking in.

3 Likes