godprotocol 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ release:
8
+ types: [created]
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: actions/setup-node@v3
16
+ with:
17
+ node-version: 16
18
+ - run: npm ci
19
+ - run: npm test
20
+
21
+ publish-gpr:
22
+ needs: build
23
+ runs-on: ubuntu-latest
24
+ permissions:
25
+ contents: read
26
+ packages: write
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - uses: actions/setup-node@v3
30
+ with:
31
+ node-version: 16
32
+ registry-url: https://npm.pkg.github.com/
33
+ - run: npm ci
34
+ - run: npm publish
35
+ env:
36
+ NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -1,4 +1,5 @@
1
- import { fs_buffer, hash, store } from "../utils/hash";
1
+ import fs from "fs";
2
+ import { hash, load } from "../utils/hash";
2
3
 
3
4
  class Arr {
4
5
  constructor(object, account) {
@@ -6,51 +7,26 @@ class Arr {
6
7
  this.account = account;
7
8
  }
8
9
 
9
- store = (obj) => {
10
- return store(obj, this.account);
11
- };
12
-
13
- persistent_load = (initiator) => {
14
- let sequence = [
15
- `link ${initiator.path}/Opcodes/${this.account}`,
16
- `link ${initiator.path}/Datatypes/Array`,
17
- "timestep value",
18
- ];
19
- this.object.map((o, i) => {
20
- store(o, this.account, initiator);
21
- sequence.push(`write {pending_children:-${i + 1}}`);
10
+ load = () => {
11
+ let obj = [];
12
+ this.object.map((o) => {
13
+ let blk = this.account.vm.web.blocks[o];
14
+ obj.push(load(blk.read_metadata()));
22
15
  });
23
- sequence.push(
24
- ...[
25
- "mine",
26
- "pop_link",
27
- "history -1",
28
- "timestep output",
29
- "write {pending_children:-1}",
30
- "mine",
31
- "pop_link",
32
- ]
33
- );
34
-
35
- initiator.load(sequence);
36
-
37
- return this.object;
16
+ return obj;
38
17
  };
39
18
 
40
- load = (initiator) => {
41
- if (initiator) return this.persistent_load(initiator);
19
+ store = () => {
20
+ let curr_hash = hash("[]");
21
+ this.object.map((o) => {
22
+ hash(curr_hash + hash(o));
23
+ });
24
+ this.path = `${this.account.path}/Datatypes/Array`;
25
+ curr_hash = hash(curr_hash);
42
26
 
43
- let arr = [];
44
- for (let a = 0; a < this.object.length; a++) {
45
- let item = this.object[a];
46
- arr.push(this.store(item));
47
- }
48
- let addr = `${this.account.path}/Datatypes/Array/${hash(
49
- JSON.stringify(arr)
50
- )}`;
51
- fs_buffer({ addr, data: arr });
27
+ fs.writeFileSync(`${this.path}/${curr_hash}`, JSON.stringify(this.object));
52
28
 
53
- return addr;
29
+ return this;
54
30
  };
55
31
  }
56
32
 
@@ -1,37 +1,20 @@
1
- import { fs_buffer } from "../utils/hash";
1
+ import fs from "fs";
2
2
 
3
3
  class Num {
4
4
  constructor(object, account) {
5
- this.object = object;
5
+ this.object = Number(object.join(""));
6
6
  this.account = account;
7
+ this.path = `${this.account.path}/Datatypes/Number/${this.object}`;
7
8
  }
8
9
 
9
- load = (initiator) => {
10
- initiator.load([
11
- `link ${initiator.path}/Opcodes/${this.account}`,
12
- `link ${initiator.path}/Datatypes/Number`,
13
- "timestep value",
14
- `write ${this.object}`,
15
- "mine",
16
- "pop_link",
17
- "history -1",
18
- "timestep output",
19
- "write {pending_children:-1}",
20
- "mine",
21
- "pop_link",
22
- ]);
23
-
24
- return this.object;
10
+ load = () => {
11
+ return this;
25
12
  };
26
13
 
27
- store = (initiator) => {
28
- if (initiator) return this.load(initiator);
29
-
30
- let addr = `${this.account.path}/Datatypes/Number/${this.object}`;
31
-
32
- fs_buffer({ addr, data: { type: "number", value: this.object } });
14
+ store = () => {
15
+ fs.writeFileSync(this.path, this.object.toString());
33
16
 
34
- return addr;
17
+ return this;
35
18
  };
36
19
  }
37
20
 
@@ -3,38 +3,20 @@ import { fs_buffer, hash } from "../utils/hash";
3
3
 
4
4
  class Str {
5
5
  constructor(object, account) {
6
- this.object = object;
6
+ this.object = object.join("");
7
+ this.hash = hash(this.object);
7
8
  this.account = account;
8
9
  }
9
10
 
10
- load = (initiator) => {
11
- initiator.load([
12
- `link ${initiator.path}/Opcodes/${this.account}`,
13
- `link ${initiator.path}/Datatypes/String`,
14
- "timestep value",
15
- `write ${this.object}`,
16
- "mine",
17
- "pop_link",
18
- "history -1",
19
- "timestep output",
20
- "write {pending_children:-1}",
21
- "mine",
22
- "pop_link",
23
- ]);
24
-
25
- return this.object;
11
+ load = () => {
12
+ return this;
26
13
  };
27
14
 
28
- store = (initiator) => {
29
- if (initiator) return this.load(initiator);
30
-
31
- let addr = `${this.account.path}/Datatypes/String/${hash(
32
- this.object || "[blank]"
33
- )}`;
34
-
35
- fs_buffer({ addr, data: { type: "string", value: this.object } });
15
+ store = () => {
16
+ this.path = `${this.account.path}/Datatypes/String/${this.hash}`;
17
+ fs.writeFileSync(this.path, this.object);
36
18
 
37
- return addr;
19
+ return this;
38
20
  };
39
21
  }
40
22
 
@@ -1,4 +1,6 @@
1
- import { fs_buffer, hash, store } from "../utils/hash";
1
+ import fs from "fs";
2
+ import { hash } from "../utils/hash";
3
+ import Arr from "./Array";
2
4
 
3
5
  class Twain {
4
6
  constructor(object, account) {
@@ -6,60 +8,30 @@ class Twain {
6
8
  this.account = account;
7
9
  }
8
10
 
9
- store = (obj) => {
10
- return store(obj, this.account);
11
- };
12
-
13
- persistent_load = (initiator) => {
14
- let sequence = [
15
- `link ${initiator.path}/Opcodes/${this.account}`,
16
- `link ${initiator.path}/Datatypes/Twain`,
17
- ];
18
- for (let prop in this.object) {
19
- let value = this.object[prop];
20
- sequence.push(`timestep prop`);
21
- let prop_addr = store(prop, this.account, initiator);
22
- sequence.push(`write {pending_children:-1}`);
23
- sequence.push(`timestep value`);
24
- let value_addr = store(value, this.account, initiator);
25
- sequence.push(`write {pending_children:-1}`);
26
- obj[prop_addr] = value_addr;
27
- }
28
- sequence.push(
29
- ...[
30
- "mine",
31
- "pop_link",
32
- "history -1",
33
- "timestep output",
34
- "write {pending_children:-1}",
35
- "mine",
36
- "pop_link",
37
- ]
38
- );
39
-
40
- initiator.load(sequence);
41
-
42
- return this.object;
11
+ load = () => {
12
+ let obj = {};
13
+ this.object.map((o) => {
14
+ let blk = this.account.vm.web.blocks[o];
15
+ let pair = load(blk.read_metadata());
16
+ if (!(pair instanceof Arr))
17
+ return console.log("Something aint right in Twain.load");
18
+
19
+ obj[pair.object[0]] = obj[pair.object[1]];
20
+ });
21
+ return obj;
43
22
  };
44
23
 
45
- load = (initiator) => {
46
- if (initiator) return this.persistent_load();
47
-
48
- let obj = {};
49
- for (let prop in this.object) {
50
- let value = this.object[prop];
51
- let prop_addr = this.store(prop);
52
- let value_addr = this.store(value);
53
- obj[prop_addr] = value_addr;
54
- }
55
- let addr = `${this.account.path}/Datatypes/Twain/${hash(
56
- JSON.stringify(obj)
57
- )}`;
58
- this.address = addr;
24
+ store = () => {
25
+ let curr_hash = hash("{}");
26
+ this.object.map((o) => {
27
+ hash(curr_hash + hash(o));
28
+ });
29
+ this.path = `${this.account.path}/Datatypes/Twain`;
30
+ curr_hash = hash(curr_hash);
59
31
 
60
- fs_buffer({ addr, data: obj });
32
+ fs.writeFileSync(`${this.path}/${curr_hash}`, this.stringify());
61
33
 
62
- return addr;
34
+ return this;
63
35
  };
64
36
 
65
37
  stringify = () => {
package/Index.js CHANGED
@@ -1,54 +1,32 @@
1
- import http from "http";
2
-
3
1
  import Account from "./Objects/Account";
4
2
  import framer from "./framer";
5
3
  import opcodes from "./opcodes";
6
4
  import Socket_server from "./Socket";
7
5
  import { manage_key_pair } from "./utils/gen_key_pair";
6
+ import create_server, { PORT } from "./utils/create_server";
8
7
 
9
8
  manage_key_pair();
10
9
 
11
- let PORT = 1900;
12
-
13
10
  const initiator = new Account(process.env.PUBLIC_KEY);
14
- let server = process.env.SERVER;
11
+ let cli_server = process.env.SERVER;
15
12
 
16
- if (server) {
17
- initiator.add_server(server);
13
+ if (cli_server) {
14
+ initiator.add_server(cli_server);
18
15
  } else {
19
- console.warn("set SERVER in your env.");
20
- server = `http://localhost:${PORT}`;
21
- initiator.add_server(server);
16
+ // console.warn("set SERVER in your env.");
17
+ cli_server = `http://localhost:${PORT}`;
18
+ initiator.add_server(cli_server);
22
19
  }
23
20
 
24
21
  framer(initiator);
25
22
 
26
23
  opcodes(initiator);
27
24
 
28
- http
29
- .createServer((req, res) => {
30
- let data = "";
31
-
32
- req.on("data", (chunk) => {
33
- data += chunk;
34
- });
35
- req.on("end", () => {
36
- if (req.url === "/create_account") {
37
- console.log(data);
38
- initiator.account(data);
39
- } else {
40
- initiator.load(data);
41
- }
42
-
43
- res.end();
44
- });
45
- })
46
- .listen(PORT);
25
+ let instructions = ["cursor value", "write 7"];
26
+ initiator.load({ instructions: ["add"] });
27
+
28
+ create_server(initiator);
47
29
 
48
30
  Socket_server();
49
31
 
50
32
  export { initiator };
51
-
52
- console.log(
53
- `\n...GOD PROTOCOL RUNNING :${PORT}\nYour Public_key: ${process.env.PUBLIC_KEY}`
54
- );
@@ -1,10 +1,11 @@
1
- import { Blockchain } from "./Blockchain";
1
+ import { Chain } from "./Blockchain";
2
2
  import Virtual_machine from "./Virtual_machine";
3
3
  import fs from "fs";
4
4
  import Emitter from "./Emitter";
5
5
  import Folder from "./folder";
6
6
  import Frame from "./Frame";
7
7
  import File from "./file";
8
+ import { hash } from "../utils/hash";
8
9
 
9
10
  let emitter = new Emitter();
10
11
 
@@ -23,15 +24,17 @@ class Account {
23
24
  });
24
25
  this.account = this;
25
26
 
26
- this.chain = new Blockchain(this);
27
+ this.chain = new Chain(this);
27
28
  this.vm = new Virtual_machine(this.chain);
28
29
  this.accounts = new Object();
29
30
 
30
31
  this.servers = new Array();
32
+ this.parents = new Array();
31
33
 
32
34
  this.column = 0;
33
35
  this.folders = new Array();
34
36
  this.files = new Array();
37
+ this.content_buffer = new Array();
35
38
  }
36
39
 
37
40
  add_server = (server) => {
@@ -46,26 +49,27 @@ class Account {
46
49
  this.column = value;
47
50
  };
48
51
 
49
- get_active_file = () => {
50
- let par = this.vm.get_context().hash;
51
-
52
- let file = this.files.find((f) => f.name === par);
52
+ file = (name) => {
53
+ let file = this.files.find((f) => f.name === name);
53
54
 
54
55
  if (!file) {
55
- file = new File(par, this);
56
+ file = new File(name, this);
56
57
  this.files.push(file);
57
- this.active_file = file;
58
58
  }
59
59
 
60
60
  return file;
61
61
  };
62
62
 
63
- folder = (name) => {
64
- let folder = new Folder(name, this);
65
- folder.account = this;
66
- this.folders.push(folder);
63
+ folder = (name, context) => {
64
+ let name_hash = hash(name + context.hash);
65
+ let folder = this.folders.find((f) => f.hash === name_hash);
66
+ if (!folder) {
67
+ folder = new Folder(name, this, name_hash);
68
+ folder.account = this.account;
67
69
 
68
- folder.context = this.vm.get_context();
70
+ this.folders.push(folder);
71
+ }
72
+ folder.context = context.hash;
69
73
 
70
74
  return folder;
71
75
  };
@@ -77,7 +81,8 @@ class Account {
77
81
  frame = (struct) => {
78
82
  let frame = new Frame(struct);
79
83
  frame.to_instruction();
80
- frame.load(this);
84
+
85
+ return frame;
81
86
  };
82
87
 
83
88
  load = ({ instructions, account }) => {
@@ -112,14 +117,17 @@ class Account {
112
117
  return account;
113
118
  };
114
119
 
115
- account = ({ public_key, server }) => {
120
+ create_account = ({ public_key, server }) => {
116
121
  let account = this.accounts[public_key];
117
122
  if (!account) {
118
123
  account = new Account(public_key);
119
124
 
120
125
  this.accounts[account.name] = account;
121
126
  }
122
- !account.server.includes(server) && account.server.push(server);
127
+ !account.servers.includes(server) && account.servers.push(server);
128
+
129
+ !account.parents.find((p) => p.name === this.name) &&
130
+ account.parents.push(this);
123
131
 
124
132
  return account;
125
133
  };
@@ -1,17 +1,18 @@
1
+ import fs from "fs";
1
2
  import Explorer from "./Explorer";
2
3
  import Twain from "../Datatypes/Twain";
3
- import { fs_buffer, hash } from "../utils/hash";
4
+ import { hash } from "../utils/hash";
4
5
 
5
6
  class Block {
6
- constructor(index, chain, data, previous_hash = "") {
7
- this.index = index;
7
+ constructor(chain) {
8
+ this.index = chain.blocks.length;
8
9
  this.timestamp = Date.now();
9
- this.data = data;
10
- this.previous_hash = previous_hash;
10
+ this.data = chain.txs;
11
11
  this.chain = chain;
12
12
  this.parents = new Array();
13
13
  this.children = new Array();
14
- this.path = `${this.chain.path}:${this.index}`;
14
+ this.hash = hash(hash(JSON.stringify(this.data)) + this.chain.hash);
15
+ this.path = `${this.chain.path}/${this.hash}`;
15
16
 
16
17
  this.column = 0;
17
18
  }
@@ -29,30 +30,32 @@ class Block {
29
30
  };
30
31
 
31
32
  calculate_hash() {
32
- return hash(`${JSON.stringify(this.data)}`);
33
+ return this.hash;
33
34
  }
34
35
 
35
- stringify = (not_twained) => {
36
+ read_metadata = (path) => {
37
+ let res = "";
38
+ try {
39
+ res = JSON.parse(fs.readFileSync(path, { encoding: "utf-8" }));
40
+ } catch (e) {
41
+ console.log(e.message);
42
+ }
43
+ return res;
44
+ };
45
+
46
+ stringify = (str) => {
36
47
  let obj = {
37
48
  index: this.index,
38
49
  hash: this.hash,
39
50
  previous_hash: this.previous_hash,
40
- chain: this.chain.address,
51
+ chain: this.chain.hash,
41
52
  };
42
- if (not_twained) {
43
- }
44
-
45
- let twain = new Twain(obj, this.chain.parent.account);
46
- twain.load();
47
53
 
48
- return {
49
- type: "twain",
50
- path: twain.address,
51
- };
54
+ return str ? JSON.stringify(obj) : obj;
52
55
  };
53
56
  }
54
57
 
55
- class Blockchain extends Explorer {
58
+ class Chain extends Explorer {
56
59
  constructor(parent, context) {
57
60
  super();
58
61
 
@@ -87,15 +90,15 @@ class Blockchain extends Explorer {
87
90
  return { type: "twain", path: twain.address };
88
91
  };
89
92
 
90
- store = (block) => {
91
- fs_buffer({ addr: block.address, data: block.stringify() });
93
+ store = (block, vm) => {
94
+ let metadata = this.parent.content_buffer;
92
95
 
93
- fs_buffer({
94
- addr: `${this.parent.account.path}/Objects/Blockchain/Blockchain/${this.hash}`,
95
- data: this.stringify(),
96
- });
96
+ let file = this.parent.file(block.hash);
97
+ file.content = metadata;
98
+ file.store(this.parent.account);
99
+ block.metadata = file.data_path;
97
100
 
98
- block.file && block.file.store();
101
+ vm.web.new_block(block);
99
102
  };
100
103
 
101
104
  handle_children = (block) => {
@@ -103,25 +106,16 @@ class Blockchain extends Explorer {
103
106
  this.connections.push(block);
104
107
  };
105
108
 
106
- mine = () => {
107
- let blk = new Block(this.blocks.length, this, this.txs);
109
+ mine = (vm) => {
110
+ let blk = new Block(this);
108
111
 
109
112
  let latest_blk = this.get_latest_block();
110
113
  blk.previous_hash = latest_blk && latest_blk.hash;
111
-
112
- blk.calculate_hash();
113
-
114
- let active_file = this.parent.get_active_file();
115
-
116
- blk.metadata = { content: active_file.contents.length };
117
114
  blk.timelapse = blk.timestamp - this.start_time;
118
115
 
119
- blk.file = active_file;
120
-
121
116
  this.blocks.push(blk);
122
117
 
123
- this.store(blk);
124
- active_file.reset_content();
118
+ this.store(blk, vm);
125
119
 
126
120
  this.txs = new Array();
127
121
  this.pending_children = new Array();
@@ -138,4 +132,4 @@ class Blockchain extends Explorer {
138
132
  }
139
133
  }
140
134
 
141
- export { Blockchain, Block };
135
+ export { Chain, Block };
@@ -1,11 +1,15 @@
1
- import { Blockchain } from "./Blockchain";
2
-
3
1
  class Blockweb {
4
2
  constructor(vm) {
5
3
  this.vm = vm;
6
4
  this.chains = new Object();
5
+ this.blocks = new Object();
7
6
  }
8
7
 
8
+ new_block = (block) => {
9
+ this.blocks[block.hash] = block;
10
+ this.chains[block.chain.hash] = block.chain;
11
+ };
12
+
9
13
  explore = (query) => {
10
14
  this.vm.get_context().chain.explore(query);
11
15
  };
@@ -1,7 +1,3 @@
1
- import { emitter } from "./God";
2
- import File from "./file";
3
- import Folder from "./folder";
4
-
5
1
  class File_system {
6
2
  constructor() {
7
3
  this.files = {};
package/Objects/Frame.js CHANGED
@@ -1,9 +1,13 @@
1
+ import { array_to_nested_objects } from "../utils/functions";
1
2
  import { Buffs, fs_buffer } from "../utils/hash";
2
3
 
3
4
  class Frame {
4
5
  constructor(object, parent) {
5
6
  this.parent = parent;
6
- this.object = object;
7
+
8
+ this.object = Array.isArray(object)
9
+ ? array_to_nested_objects(object)
10
+ : object;
7
11
 
8
12
  this.children = new Array();
9
13
  this.instructions = new Array();
@@ -18,37 +22,15 @@ class Frame {
18
22
  return frame;
19
23
  };
20
24
 
21
- load = (account) => {
22
- account.load({ instructions: this.instructions });
23
-
24
- setTimeout(() => {
25
- while (Buffs.length) {
26
- for (let b = 0; b < Buffs.length; b++) {
27
- let buff = Buffs[b];
28
- fs_buffer(buff.payload, b + 1);
29
- }
30
- }
31
- }, 50);
32
- };
33
-
34
25
  to_instruction = () => {
35
- if (this.object.__type === "file") {
36
- } else if (Array.isArray(this.object)) {
37
- this.object.map((s) => this.frame(s).to_instruction());
38
- } else if (["string", "number"].includes(typeof this.object)) {
39
- this.instructions.push(`chain ${this.object}`);
40
- this.instructions.push(`pop ${this.object}`);
41
- } else
42
- for (let prop in this.object) {
43
- let val = this.object[prop];
44
- this.instructions.push(`chain ${prop}`);
45
-
46
- if (val) {
47
- this.frame(val).to_instruction();
48
- }
49
-
50
- this.instructions.push(`pop ${prop}`);
51
- }
26
+ for (let prop in this.object) {
27
+ let val = this.object[prop];
28
+ this.instructions.push(`chain ${prop}`);
29
+
30
+ val && this.frame(val).to_instruction();
31
+
32
+ this.instructions.push(`pop ${prop}`);
33
+ }
52
34
  };
53
35
  }
54
36
 
@@ -1,20 +1,28 @@
1
- import { store } from "../utils/hash";
1
+ import Arr from "../Datatypes/Array";
2
+ import Num from "../Datatypes/Number";
3
+ import Str from "../Datatypes/String";
4
+ import Twain from "../Datatypes/Twain";
2
5
 
3
6
  class Opcodes {
7
+ opcodes = new Object();
8
+
4
9
  set = (name, callable) => {
5
- this[name] = callable;
10
+ this.opcodes[name] = callable;
6
11
  };
7
12
 
8
13
  prepare_operation = (opcode, context) => {
9
- let path = `${context.parent.account.path}/Opcodes/${opcode}`;
10
-
11
- let block = context.pending_children
12
- .filter((blk) => blk.chain.parent.path === path)
13
- .slice(-1)[0];
14
+ let circ = this.opcodes[opcode];
15
+ circ(context.parent.content_buffer, context);
16
+ };
14
17
 
15
- let arg = block.file.activate_content(block.metadata.content);
18
+ parse_content = (buffer) => {
19
+ let obj;
20
+ if (buffer.type === "twain") obj = new Twain(buffer.value).load();
21
+ else if (buffer.type === "array") obj = new Arr(buffer.value).load();
22
+ else if (buffer.type === "string") obj = new Str(buffer.value).load();
23
+ else if (buffer.type === "number") obj = new Num(buffer.value).load();
16
24
 
17
- store(this[opcode](arg, this), opcode, this.get_context(0).parent);
25
+ return obj;
18
26
  };
19
27
  }
20
28
 
@@ -1,7 +1,7 @@
1
1
  import { Block } from "./Blockchain";
2
2
  import Blockweb from "./Blockweb";
3
+ import File_system from "./Filesystem";
3
4
  import Frame from "./Frame";
4
- import { emitter } from "./Account";
5
5
  import Opcodes from "./Opcodes";
6
6
 
7
7
  class Virtual_machine extends Opcodes {
@@ -10,9 +10,8 @@ class Virtual_machine extends Opcodes {
10
10
 
11
11
  this.state = [chain];
12
12
  this.web = new Blockweb(this);
13
+ this.fm = new File_system(this);
13
14
 
14
- this.instruction_stack = new Array();
15
- this.running = 0;
16
15
  this.pointers = new Array();
17
16
  }
18
17
 
@@ -25,15 +24,16 @@ class Virtual_machine extends Opcodes {
25
24
 
26
25
  while (this.pointers.slice(-1)[0] < instructions.length && !this.err_) {
27
26
  let ins = instructions[this.pointers.slice(-1)[0]];
28
-
29
- let res = this.check_instruction(ins);
30
- if (res) {
31
- } else this.execute(ins);
27
+ if (ins) {
28
+ let res = this.check_instruction(ins);
29
+ if (res) {
30
+ } else this.execute(ins);
31
+ }
32
32
 
33
33
  this.pointers[this.pointers.length - 1] = this.pointers.slice(-1)[0] + 1;
34
34
  }
35
35
  this.pointers.pop();
36
- this.get_context().mine();
36
+ this.get_context().mine(this);
37
37
  };
38
38
 
39
39
  check_instruction = (instruction) => {
@@ -50,40 +50,57 @@ class Virtual_machine extends Opcodes {
50
50
  return true;
51
51
  };
52
52
 
53
+ readonly = (tag) => {
54
+ let tags = tag.split(":");
55
+ tags[0] = tags[0].slice(1);
56
+ tags[1] = tags[1].slice(0, -1);
57
+
58
+ console.log(tags);
59
+ };
60
+
61
+ parse_arg = (arg) => {
62
+ let data;
63
+ if (!arg) return;
64
+ if (arg.startsWith("{")) {
65
+ data = this.readonly();
66
+ }
67
+
68
+ return data;
69
+ };
70
+
53
71
  execute(instruction) {
54
72
  let [opcode, args] = instruction.split(" ");
55
73
 
56
74
  let context = this.get_context();
57
75
 
58
76
  context.add_tx(instruction);
77
+ args = this.parse_arg(args);
78
+
59
79
  switch (opcode) {
60
80
  case "chain":
61
81
  let folder = context.parent.folder(args, context);
62
82
  this.state.push(folder.chain);
63
83
  break;
64
84
  case "link":
65
- emitter.emit(args, {
66
- action: "parse",
67
- aft: (res) => this.state.push(res.chain),
68
- });
85
+ this.state.push(args);
69
86
  break;
70
87
  case "mine":
71
- context.mine();
88
+ context.mine(this);
72
89
  this.state.slice(-2)[0].handle_children(context.get_latest_block());
73
90
  break;
74
91
  case "pop_link":
75
92
  this.state.pop();
76
93
  break;
77
94
  case "pop":
78
- context.mine();
95
+ context.mine(this);
79
96
  this.state.pop();
80
97
  break;
98
+ case "run":
99
+ this.push(args.data);
100
+ break;
81
101
  default:
82
- if (["timestep", "history", "write"].includes(opcode)) {
83
- context.parent.get_active_file()[opcode](args, context);
84
- } else {
85
- this.prepare_operation(opcode, context);
86
- }
102
+ if (["cursor", "write"].includes(opcode)) context[opcode](args);
103
+ else this.prepare_operation(opcode, context);
87
104
  }
88
105
  }
89
106
 
package/Objects/file.js CHANGED
@@ -1,212 +1,20 @@
1
- import fs from "fs";
2
- import Twain from "../Datatypes/Twain";
3
1
  import { store } from "../utils/hash";
4
2
 
5
3
  class File {
6
- object_name = "File";
7
-
8
4
  constructor(name, folder) {
9
5
  this.name = name;
10
6
  this.parent = folder;
11
7
 
12
8
  this.path = `${this.parent.path}/${this.name}`;
13
9
 
14
- this.contents = new Array();
15
10
  this.content = null;
16
- this.set_path();
17
11
 
18
12
  this.frame_path = `${this.parent.account.path}/Objects/File`;
19
-
20
- this.r = 0;
21
- this.col = "0";
22
13
  }
23
14
 
24
- activate_content = (index) => {
25
- let content = this.contents[Number(index)];
26
-
27
- let obj = {};
28
- for (let prop in content) {
29
- let value = content[prop];
30
-
31
- obj[prop] = this.parse_content(value[0]);
32
- }
33
-
34
- return obj;
35
- };
36
-
37
- parse_content = (content) => {
38
- if (content && content.includes && content.includes("/")) {
39
- let res = this.parent.chain.connections.find((c, i) => {
40
- return c.path === content;
41
- });
42
-
43
- let obj = this.datatypes(
44
- res.file.activate_content(res.metadata.content),
45
- res
46
- );
47
-
48
- return obj;
49
- } else return content;
50
- };
51
-
52
- datatypes = (obj, block) => {
53
- if (
54
- block.chain.parent.path === `${this.parent.account.path}/Datatypes/Number`
55
- ) {
56
- return Number(obj.value);
57
- } else if (
58
- block.chain.parent.path === `${this.parent.account.path}/Datatypes/String`
59
- ) {
60
- return obj.value.toString();
61
- }
62
- return obj;
63
- };
64
-
65
- reset_content = () => {
66
- if (this.content) {
67
- if (this.r < 0) {
68
- this.contents.splice(this.r, 1, this.content);
69
- } else this.contents.push(this.content);
70
- }
71
- this.r = 0;
72
- this.col = "0";
73
-
74
- this.content = null;
75
-
76
- this.parent.active_file = null;
77
- };
78
-
79
- get = (opcode) => {
80
- return this[opcode];
81
- };
82
-
83
- timestep = (col) => {
84
- this.col = col;
85
- };
86
-
87
- history = (r) => {
88
- this.r = Number(r);
89
- let content = this.contents.slice(r)[0];
90
- this.content = content;
91
- };
92
-
93
- fetch_arg = (path) => {
94
- let block = this.chain.explore({ parents: `${this.frame_path}/${path}` });
95
- return block.file.parse(true);
96
- };
97
-
98
- readonly = (prop, index, context) => {
99
- let initiator = context.get_context(0);
100
- if (
101
- [
102
- "timelapse",
103
- "timestamp",
104
- "data",
105
- "hash",
106
- "path",
107
- "children",
108
- "parents",
109
- "chain",
110
- ].includes(prop)
111
- ) {
112
- let blk = context.pending_children.slice(index || 0)[0];
113
- store(blk[prop], "readonly", initiator);
114
- } else if (
115
- [
116
- "parent",
117
- "context",
118
- "name",
119
- "hash",
120
- "path",
121
- "connections",
122
- "pending_children",
123
- "blocks",
124
- ].includes(prop)
125
- ) {
126
- let value = context[prop];
127
- if (index && Array.isArray(value)) value.slice(index)[0];
128
- store(value, "readonly", initiator);
129
- }
130
-
131
- return `${initiator.path}/Opcodes/readonly:${
132
- initiator.pending_children.slice(-1)[0].index
133
- }`;
134
- };
135
-
136
- write = (data, context) => {
137
- if (data.startsWith("{") && data.endsWith("}")) {
138
- let offset = data.split(":");
139
- let prop = data[0].slice(1);
140
- let index = Number(offset[1].slice(0, -1));
141
-
142
- data = this.readonly(prop, index, context);
143
- }
144
-
145
- if (!this.content) this.content = new Object();
146
-
147
- let col = this.content[this.col];
148
- if (!col) {
149
- col = new Array();
150
-
151
- this.content[this.col] = col;
152
- }
153
-
154
- if (!col.includes(data)) col.unshift(data);
155
- };
156
-
157
- read = (history) => {
158
- let res = this.content[this.row];
159
- if (this.col) res = res[this.col];
160
-
161
- return history != null ? res[history] : res;
162
- };
163
-
164
- run = (args, vm) => {
165
- let block = vm.web.explore({ name: args });
166
-
167
- block.map((blk) => vm.push(blk));
168
- };
169
-
170
- parse = (content) => {
171
- content = content || this.fetch_arg(`parse/content`);
172
-
173
- let obj = content && this.content;
174
- if (!obj) {
175
- obj = this.stringify();
176
- }
177
-
178
- obj = new Twain(obj);
179
-
180
- fs.writeFileSync(`${this.frame_path}/parse/output`, obj.stringify());
181
-
182
- return obj;
183
- };
184
-
185
15
  store = () => {
186
- let str = "";
187
- this.reset_content();
188
-
189
- this.contents.map((c) => (str += `${JSON.stringify(c)}\n`));
190
- fs.writeFileSync(this.path, str);
191
- };
192
-
193
- set_path = () => {
194
- if (!fs.existsSync(this.path)) {
195
- fs.writeFileSync(this.path, "");
196
- } else {
197
- this.existed = true;
198
- let content = fs.readFileSync(this.path, { encoding: "utf-8" });
199
- try {
200
- content = content.split("\n").map((c) => JSON.parse(c));
201
- } catch (e) {
202
- content = [];
203
- }
204
- if (!Array.isArray(content)) {
205
- content = [content];
206
- }
207
-
208
- this.contents = content;
209
- }
16
+ this.content_object = store(this.content);
17
+ this.data_path = this.content_object && this.content_object.path;
210
18
  };
211
19
  }
212
20
 
package/Objects/folder.js CHANGED
@@ -1,50 +1,42 @@
1
1
  import fs from "fs";
2
2
  import { emitter } from "./Account";
3
3
  import File from "./file";
4
- import { Blockchain } from "./Blockchain";
4
+ import { Block, Chain } from "./Blockchain";
5
+ import { hash } from "../utils/hash";
5
6
 
6
7
  class Folder {
7
8
  object_name = "folder";
8
9
 
9
- constructor(name, parent) {
10
+ constructor(name, parent, hash) {
10
11
  this.name = name;
11
12
  this.parent = parent;
12
- this.path = `${parent.path}/${this.name}`;
13
+ this.hash = hash;
14
+ this.path = `${parent.path}/${this.hash}`;
13
15
 
14
- emitter.listen(this.path, (payload) => {
15
- let { aft, bfr } = payload;
16
-
17
- bfr && bfr(this, payload);
18
-
19
- aft && aft(this);
20
- });
16
+ emitter.listen(this.path, (cb) => cb && cb(this));
21
17
 
22
18
  this.active_file = null;
23
19
  this.files = new Array();
24
20
  this.folders = new Array();
25
- this.chain = new Blockchain(this);
21
+ this.chain = new Chain(this);
22
+
23
+ this.curs = -1;
24
+ this.content_buffer = new Object();
26
25
 
27
26
  this.set_path();
28
27
  }
29
28
 
30
- timestep = (col) => this.get_active_file().timestep(col);
31
-
32
- history = (r) => this.get_active_file().history(r);
33
-
34
- write = (data) => this.get_active_file().write(data);
35
-
36
- get_active_file = () => {
37
- let par = this.context.hash;
29
+ cursor = (curs) => (this.curs = curs);
38
30
 
39
- let file = this.files.find((f) => f.name === par);
31
+ write = (data) => {
32
+ let v = this.content_buffer[this.curs];
40
33
 
41
- if (!file) {
42
- file = new File(par, this);
43
- this.files.push(file);
44
- this.active_file = file;
34
+ if (!v) {
35
+ v = new Array();
36
+ this.content_buffer[this.curs] = v;
45
37
  }
46
38
 
47
- return file;
39
+ v.push(data instanceof Block ? data.hash : data);
48
40
  };
49
41
 
50
42
  file = (name) => {
@@ -53,9 +45,7 @@ class Folder {
53
45
  if (!file) {
54
46
  file = new File(name, this);
55
47
  this.files.push(file);
56
- } else file.reset_content();
57
-
58
- this.active_file = file;
48
+ }
59
49
 
60
50
  return file;
61
51
  };
@@ -76,14 +66,15 @@ class Folder {
76
66
  };
77
67
 
78
68
  folder = (name, context) => {
79
- let folder = this.folders.find((f) => f.name === name);
69
+ let name_hash = hash(name + context.hash);
70
+ let folder = this.folders.find((f) => f.hash === name_hash);
80
71
  if (!folder) {
81
- folder = new Folder(name, this);
72
+ folder = new Folder(name, this, name_hash);
82
73
  folder.account = this.account;
83
74
 
84
75
  this.folders.push(folder);
85
76
  }
86
- folder.context = context;
77
+ folder.context = context.hash;
87
78
 
88
79
  return folder;
89
80
  };
package/Socket.js CHANGED
@@ -8,6 +8,7 @@ let pendings = new Object();
8
8
 
9
9
  const Socket_server = () => {
10
10
  let server = net.createServer((socket) => {
11
+ console.log("meeeee");
11
12
  socket.on("connect", () => {
12
13
  Sockets[socket.id] = { socket };
13
14
  console.log(`New client connection - ${socket.id}`);
package/framer.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const framer = (initiator) => {
2
- initiator.frame({
2
+ let frame = initiator.frame({
3
3
  Opcodes: {
4
4
  add: null,
5
5
  equal: null,
@@ -17,7 +17,7 @@ const framer = (initiator) => {
17
17
  },
18
18
  Objects: {
19
19
  Blockchain: {
20
- Blockchain: null,
20
+ Chain: null,
21
21
  Block: null,
22
22
  },
23
23
  Folder: {
@@ -33,6 +33,8 @@ const framer = (initiator) => {
33
33
  },
34
34
  },
35
35
  });
36
+
37
+ initiator.load({ instructions: frame.instructions });
36
38
  };
37
39
 
38
40
  export default framer;
package/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export class Blockchain {
21
21
  }
22
22
 
23
23
  export class Block {
24
- constructor(index: number, chain: Blockchain, data: string[]);
24
+ constructor(chain: Blockchain);
25
25
  get(prop: string): any;
26
26
 
27
27
  set(prop: string, value: any): null;
package/opcodes/add.js CHANGED
@@ -2,9 +2,9 @@ import Num from "../Datatypes/Number";
2
2
  import { initiator } from "../Index";
3
3
 
4
4
  const add = (args) => {
5
- let result = args.op1 + args.op2;
5
+ console.log(args);
6
6
 
7
- return new Num(result, "add").load(initiator);
7
+ return;
8
8
  };
9
9
 
10
10
  const divide = (args) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "A data mediator.",
5
5
  "main": "Index.js",
6
6
  "types": "index.d.ts",
@@ -31,12 +31,9 @@
31
31
  "godprotocol"
32
32
  ],
33
33
  "dependencies": {
34
- "body-parser": "^1.20.2",
35
34
  "crypto": "^1.0.1",
36
35
  "elliptic": "^6.5.5",
37
- "express": "^4.19.2",
38
36
  "fs": "^0.0.1-security",
39
- "net": "^1.0.2",
40
- "semitter": "^1.2.1"
37
+ "net": "^1.0.2"
41
38
  }
42
39
  }
@@ -0,0 +1,43 @@
1
+ import http from "http";
2
+
3
+ let PORT = 1900;
4
+
5
+ const create_server = (initiator) => {
6
+ let server = http.createServer((req, res) => {
7
+ let data = "";
8
+
9
+ req.on("data", (chunk) => {
10
+ console.log("Received chunk");
11
+ data += chunk;
12
+ });
13
+
14
+ req.on("end", () => {
15
+ data = JSON.parse(data);
16
+ if (req.url === "/create_account" && req.method === "POST") {
17
+ initiator.create_account(data);
18
+ res.writeHead(200, { "Content-Type": "application/json" });
19
+ res.end(
20
+ JSON.stringify({ status: "success", message: "Account created" })
21
+ );
22
+ } else {
23
+ initiator.load(data);
24
+ res.writeHead(200, { "Content-Type": "application/json" });
25
+ res.end(JSON.stringify({ status: "success", message: "Data loaded" }));
26
+ }
27
+ });
28
+
29
+ req.on("error", (e) => {
30
+ res.writeHead(500, { "Content-Type": "application/json" });
31
+ res.end(JSON.stringify({ status: "error", message: e.message }));
32
+ });
33
+ });
34
+
35
+ server.listen(PORT, () => {
36
+ console.log(
37
+ `\n...GOD PROTOCOL RUNNING :${PORT}\nYour Public_key: ${process.env.PUBLIC_KEY}`
38
+ );
39
+ });
40
+ };
41
+
42
+ export default create_server;
43
+ export { PORT };
@@ -0,0 +1,8 @@
1
+ const array_to_nested_objects = (arr) => {
2
+ if (arr.length === 0) return null;
3
+
4
+ let [first, ...rest] = arr;
5
+ return { [first]: array_to_nested_objects(rest) };
6
+ };
7
+
8
+ export { array_to_nested_objects };
package/utils/hash.js CHANGED
@@ -6,7 +6,7 @@ import Twain from "../Datatypes/Twain";
6
6
  const crypto = require("crypto");
7
7
 
8
8
  const hash = (data) => {
9
- let hash = crypto.createHash("sha512");
9
+ let hash = crypto.createHash("sha256");
10
10
 
11
11
  hash.update(data);
12
12
 
@@ -27,21 +27,24 @@ const fs_buffer = (payload, buff_index) => {
27
27
  }
28
28
  };
29
29
 
30
- const store = (obj, account, initiator) => {
30
+ const store = (obj) => {
31
31
  let addr;
32
- if (typeof obj === "number") {
33
- addr = new Num(obj, account).store(initiator);
34
- } else if (typeof obj === "string") {
35
- addr = new Str(obj, account).store(initiator);
36
- } else if (typeof obj === "object") {
37
- if (Array.isArray(obj)) {
38
- addr = new Arr(obj, account).load(initiator);
39
- } else {
40
- addr = new Twain(obj, account).load(initiator);
41
- }
32
+
33
+ if (obj.type === "string") {
34
+ addr = new Str(obj.value.join("")).store();
35
+ } else if (obj.type === "number") {
36
+ addr = new Num(obj.value).store();
37
+ } else if (obj.type === "array") {
38
+ addr = new Arr(obj.value).store();
39
+ } else if (obj.type === "twain") {
40
+ addr = new Twain(obj.value).store();
42
41
  }
43
42
 
44
43
  return addr;
45
44
  };
46
45
 
47
- export { hash, fs_buffer, store, Buffs };
46
+ const load = (addr) => {
47
+ let obj;
48
+ };
49
+
50
+ export { hash, fs_buffer, store, Buffs, load };