Advertisement
Guest User

safenetwork.js

a guest
Oct 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let safeApp;
  2.  
  3. async function authoriseAndConnect() {
  4.   let appInfo = {
  5.         name: 'Warz Test webapp',
  6.         id: 'net.maidsafe.tutorials.web-app',
  7.         version: '1.0.0',
  8.         vendor: 'MaidSafe.net Ltd.'
  9.     };
  10.   safeApp = await window.safe.initialiseApp(appInfo);
  11.   console.log('Authorising SAFE application...');
  12.   const authReqUri = await safeApp.auth.genAuthUri();
  13.   const authUri = await window.safe.authorise(authReqUri);
  14.   console.log('SAFE application authorised by user');
  15.   await safeApp.auth.loginFromUri(authUri);
  16.   console.log("Application connected to the network");
  17. }
  18.  
  19. let md;
  20. async function createMutableData() {
  21.   console.log("Creating MutableData with initial dataset...");
  22.   const typeTag = 15000;
  23.   md = await safeApp.mutableData.newRandomPublic(typeTag);
  24.   const initialData = {
  25.     "random_key_1": JSON.stringify({
  26.         text: 'Scotland to try Scotch whisky',
  27.         made: false
  28.     }),
  29.     "random_key_2": JSON.stringify({
  30.         text: 'Patagonia before I\'m too old',
  31.         made: false
  32.     })
  33.   };
  34.   await md.quickSetup(initialData);
  35.  
  36. }
  37.  
  38. async function getItems() {
  39.   const entries = await md.getEntries();
  40.   const entriesList = await entries.listEntries();
  41.   const items = [];
  42.   entriesList.forEach((entry) => {
  43.     const value = entry.value;
  44.     if (value.buf.length == 0) return;
  45.     const parsedValue = JSON.parse(value.buf);
  46.     items.push({ key: entry.key, value: parsedValue, version: value.version });
  47.   });
  48.   return items;
  49. };
  50.  
  51. async function insertItem(key, value) {
  52. };
  53.  
  54. async function updateItem(key, value, version) {
  55. };
  56.  
  57. async function deleteItems(items) {
  58. };
  59.  
  60. module.exports = {
  61.   authoriseAndConnect,
  62.   createMutableData,
  63.   getItems,
  64.   insertItem,
  65.   updateItem,
  66.   deleteItems
  67. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement