godprotocol 1.2.51 → 1.2.53

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.
@@ -11,6 +11,7 @@ class Account extends Utils {
11
11
 
12
12
  this.name = name;
13
13
  this.options = options;
14
+ this.init = options?.init;
14
15
  this.manager = options.manager;
15
16
  this.vm = new Virtual_machine(this);
16
17
  this.compiler = new Compiler(this);
@@ -37,6 +38,8 @@ class Account extends Utils {
37
38
 
38
39
  let res = await chain.add_config(sequence, { ...options, _id });
39
40
 
41
+ await this.manager.emit("on_load", payload);
42
+
40
43
  return res;
41
44
  };
42
45
 
@@ -78,24 +81,7 @@ class Account extends Utils {
78
81
  spawn: thread && thread._id,
79
82
  pointer: thread && thread.pointer,
80
83
  callback: async (res) => {
81
- if (typeof callback === "string") {
82
- if (callback === "store" && !Array.isArray(res)) {
83
- let store_fn = await this.vm.callable("store");
84
- await store_fn.callable(
85
- { object: await this.vm.cloth(res) },
86
- { vm: this.vm, chain: this.chain }
87
- );
88
- callback = res.value;
89
- } else {
90
- let folder = await this.ds.folder(callback);
91
- await folder.write(res, { replace: true });
92
- }
93
- } else if (typeof callback === "function") {
94
- await callback(res);
95
- }
96
- if (socket) {
97
- // handle socket
98
- }
84
+ await this.run_callback(res, { callback, socket });
99
85
  },
100
86
  });
101
87
 
@@ -103,6 +89,8 @@ class Account extends Utils {
103
89
  this.vm.envs[thread_id] = env;
104
90
  }
105
91
 
92
+ await this.manager.emit("on_run", payload);
93
+
106
94
  return { thread_id, callback };
107
95
  };
108
96
 
@@ -117,9 +105,13 @@ class Account extends Utils {
117
105
  let phy = `${this.physical_address}/${address}`;
118
106
 
119
107
  if (from === ".codes") {
120
- let res = await this.manager.oracle.read(`.codes/${phy}.air`);
108
+ let res = await (
109
+ await this.manager.ds.folder(`.codes/${phy}.air`, {
110
+ account: this.physical_address,
111
+ })
112
+ ).readone(query?.query, query?.options);
121
113
  if (res) {
122
- return { result: res };
114
+ return { result: res.sequence, _id: res._id };
123
115
  } else return { message: "Code not found" };
124
116
  } else if (from === "thread") {
125
117
  // is thread_id;
@@ -171,7 +163,7 @@ class Account extends Utils {
171
163
  return result;
172
164
  };
173
165
 
174
- verify = async (password) => {
166
+ verify = async (password, init) => {
175
167
  let passes = await this.ds.folder(`${this.physical_address}/__passwords`, {
176
168
  account: this.physical_address,
177
169
  });
@@ -181,14 +173,18 @@ class Account extends Utils {
181
173
  passed = false;
182
174
 
183
175
  if (pass) {
184
- passed = pass && pass.key === hash(password);
185
- } else {
176
+ passed = pass.key === hash(password);
177
+ } else if (init) {
186
178
  passed = await passes.write({ key: hash(password) });
187
179
  }
188
180
 
189
- this.servers =
190
- passed &&
191
- (await this.manager.oracle.update_servers(this.physical_address));
181
+ if (init)
182
+ this.servers =
183
+ passed &&
184
+ (await this.manager.oracle.update_servers(
185
+ this.physical_address,
186
+ this.servers
187
+ ));
192
188
 
193
189
  return passed;
194
190
  };
@@ -202,7 +198,7 @@ class Account extends Utils {
202
198
  res = true;
203
199
 
204
200
  if (pass) {
205
- res = await this.verify(pass);
201
+ res = await this.verify(pass, this.is_init);
206
202
  }
207
203
 
208
204
  await register(this);
@@ -0,0 +1,9 @@
1
+ import Events from "./Events.js";
2
+
3
+ class Clock extends Events {
4
+ constructor() {
5
+ super();
6
+ }
7
+ }
8
+
9
+ export default Clock;
@@ -0,0 +1,47 @@
1
+ import { query_ } from "../callables/functions/query.js";
2
+
3
+ class Events {
4
+ constructor() {
5
+ this.events_listeners = new Object();
6
+ }
7
+
8
+ add_listener = async (name, handler, filter) => {
9
+ let listeners = this.events_listeners[name];
10
+ if (!listeners) {
11
+ listeners = new Array();
12
+ this.events_listeners[name] = listeners;
13
+ }
14
+ if (
15
+ !listeners.find(
16
+ (li) =>
17
+ li.handler === handler &&
18
+ JSON.stringify(li.filter) === JSON.stringify(filter)
19
+ )
20
+ )
21
+ listeners.push({ handler, filter });
22
+ };
23
+
24
+ emit = async (name, payload, cb) => {
25
+ let listeners = this.events_listeners[name];
26
+ if (!listeners) return;
27
+
28
+ let total = 0;
29
+ for (let l = 0; l < listeners.length; l++) {
30
+ let { filter, handler } = listeners[l];
31
+
32
+ if (await query_(payload, filter, await this.ds.get_folder())) {
33
+ total++;
34
+ handler(payload)
35
+ .then((res) => {
36
+ total--;
37
+ cb && cb(res, { done: !total, event: { name, handler, filter } });
38
+ })
39
+ .catch((e) => {
40
+ console.log(e.message);
41
+ });
42
+ }
43
+ }
44
+ };
45
+ }
46
+
47
+ export default Events;
@@ -3,9 +3,12 @@ import Account from "./Account.js";
3
3
  import GDS from "generalised-datastore";
4
4
  import { hash } from "godprotocol/utils/hash.js";
5
5
  import { copy_object } from "generalised-datastore/utils/functions.js";
6
+ import Clock from "./Clock.js";
6
7
 
7
- class Manager {
8
+ class Manager extends Clock {
8
9
  constructor(name, options) {
10
+ super();
11
+
9
12
  this.name = name;
10
13
  this.options = options;
11
14
  this.trinity = options.trinity;
@@ -360,7 +363,7 @@ class Manager {
360
363
 
361
364
  this.ds = new GDS(this);
362
365
  await this.ds.sync();
363
- let create_serv;
366
+
364
367
  if (this.options.server) {
365
368
  this.server = this.options.server;
366
369
  if (this.options.server.port)
@@ -383,18 +386,16 @@ class Manager {
383
386
  "Initiate manager account...",
384
387
  this.options.init_account.name
385
388
  );
386
- // await this.add_account(this.options.init_account.name, {
387
- // password: this.options.init_account.password,
388
- // init: true,
389
- // });
389
+ await this.add_account(this.options.init_account.name, {
390
+ password: this.options.init_account.password,
391
+ init: true,
392
+ });
390
393
 
391
394
  cb && cb();
392
395
  }
393
396
  }
394
397
  };
395
398
  }
396
-
397
- return create_serv;
398
399
  };
399
400
 
400
401
  get_init = async () => {
@@ -421,14 +422,18 @@ class Manager {
421
422
  return acc;
422
423
  };
423
424
 
424
- get_account = async (name) => {
425
+ get_account = async (name, password) => {
425
426
  let acc = this.accounts[name];
426
427
  if (!acc) {
427
428
  let servers = await this.oracle.read(`Accounts/${name}/.servers`);
428
429
  servers = servers ? JSON.parse(servers) : [];
429
430
  if (servers.length) {
430
- acc = await this.add_account(name, { servers });
431
- }
431
+ acc = await this.add_account(name, { servers, password });
432
+ if (!acc) return password ? "Invalid Password" : null;
433
+ } else return password ? "Account not found." : null;
434
+ } else if (password) {
435
+ let res = await acc.verify(password);
436
+ if (!res) return "Invalid Password";
432
437
  }
433
438
 
434
439
  return acc;
package/Objects/Oracle.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import { post_request } from "../utils/services.js";
2
+ import {
3
+ encrypt,
4
+ decrypt,
5
+ } from "@godprotocol/repositories/utils/cryptography.js";
2
6
 
3
7
  class Oracle_client {
4
8
  constructor(server, options = {}) {
@@ -6,16 +10,16 @@ class Oracle_client {
6
10
  this.manager_key = options.manager_key;
7
11
 
8
12
  this.repo_cache = new Object();
9
- this.repos = [];
13
+ this.repos = new Array();
10
14
  }
11
15
 
12
16
  add_repo = async (repo) => {
13
- let repo_ = await this.Repos.cloth_repo(repo.repo);
17
+ let repo_ = await this.cloth_repo(repo.repo);
14
18
  this.repos.push(repo_);
15
19
 
16
20
  await this.fetch({
17
21
  method: "add_repo",
18
- args: repo,
22
+ args: { repo: encrypt(JSON.stringify(repo), this.manager_key) },
19
23
  });
20
24
 
21
25
  return repo_;
@@ -59,7 +63,9 @@ class Oracle_client {
59
63
  content = response && response.content;
60
64
  if (content) {
61
65
  repo = response.repo;
62
- repo = await this.cloth_repo(repo);
66
+ repo = await this.cloth_repo(
67
+ JSON.parse(decrypt(repo, this.manager_key))
68
+ );
63
69
  if (repo) this.repo_cache[path] = repo;
64
70
  }
65
71
  }
@@ -68,11 +74,18 @@ class Oracle_client {
68
74
 
69
75
  server_match = (s1, s2) => s1.hostname === s2.hostname && s1.port === s2.port;
70
76
 
71
- update_servers = async (account) => {
77
+ get_servers = async (account) => {
72
78
  let addr = `${account}/.servers`;
73
79
 
74
80
  let servers = await this.read(addr);
75
81
  servers = servers ? JSON.parse(servers) : [];
82
+
83
+ return servers;
84
+ };
85
+
86
+ update_servers = async (account) => {
87
+ let servers = await this.get_servers(account);
88
+
76
89
  if (!servers.find((s) => this.server_match(s, this.client))) {
77
90
  servers.push(this.client);
78
91
  await this.write(addr, JSON.stringify(servers));
@@ -131,6 +144,7 @@ class Oracle_client {
131
144
  method: "authenticate",
132
145
  args: {
133
146
  client: this.client,
147
+ key: encrypt(this.manager_key, this.manager_key),
134
148
  },
135
149
  });
136
150
  if (!auth) return;
package/Objects/Utils.js CHANGED
@@ -1,44 +1,82 @@
1
- class Utils{
2
- on = async(event_name, filter, handler) => {
3
-
4
- }
1
+ class Utils {
2
+ on = async (event_name, filter, handler) => {};
5
3
 
6
- reload = async(payload)=>{
7
- let {address} = payload;
4
+ reload = async (payload) => {
5
+ let { address } = payload;
8
6
 
9
- let res = await this.manager.oracle.read(`${address}/air`, {address, prefix: '.codes', account: this.physical_address})
10
-
11
- await this.load({address: address.slice(this.physical_address.length+1), sequence: res, compiler: true, from_reload: true})
12
- }
7
+ let res = await this.manager.oracle.read(`${address}/air`, {
8
+ address,
9
+ prefix: ".codes",
10
+ account: this.physical_address,
11
+ });
13
12
 
14
- get_from_repo = async(address, options) =>{
15
- address = `.storage/${this.physical_address}/${address}/__literal__`
16
- let content = await this.manager.oracle.get_from_repos(address, {count: 1, args: options})
13
+ await this.load({
14
+ address: address.slice(this.physical_address.length + 1),
15
+ sequence: res,
16
+ compiler: true,
17
+ from_reload: true,
18
+ });
19
+ };
17
20
 
18
- return content && JSON.parse(content)
19
- }
21
+ run_callback = async (res, { callback, socket }) => {
22
+ if (typeof callback === "string") {
23
+ if (callback === "store" && !Array.isArray(res)) {
24
+ let store_fn = await this.vm.callable("store");
25
+ await store_fn.callable(
26
+ { object: await this.vm.cloth(res) },
27
+ { vm: this.vm, chain: this.chain }
28
+ );
29
+ callback = res.value;
30
+ } else {
31
+ let net_fn = await this.vm.callable("net");
32
+ let url = await this.parse_net_url(callback);
33
+ if (url.path) return;
34
+ await net_fn.callable(
35
+ {
36
+ payload: {
37
+ header: { method: "POST" },
38
+ body: await this.vm.cloth(res),
39
+ },
40
+ account: url.account,
41
+ path: url.path,
42
+ server: url.server,
43
+ },
44
+ { vm: this.vm, chain: this.chain, no_then: true }
45
+ );
46
+ }
47
+ } else if (typeof callback === "function") {
48
+ await callback(res);
49
+ }
50
+ if (socket) {
51
+ // handle socket
52
+ }
53
+ };
20
54
 
21
- set_to_repo = async(address, content, options) =>{
22
- address = `.storage/${this.physical_address}/${address}/__literal__`
23
- let response = await this.manager.oracle.set_to_repos(address, JSON.stringify(content), {count: 1, args: options})
55
+ // acc+Arena://send_mail
56
+ parse_net_url = async (url) => {
57
+ url = url.split("://");
58
+ let protocol = url[0];
59
+ let payload = {};
24
60
 
25
- return response
26
- }
61
+ if (protocol.startsWith("acc+")) {
62
+ payload.account = protocol.split("+")[1];
63
+ payload.path = url[1];
64
+ } else if (protocol.startsWith("http")) {
65
+ let addr = url[1].split("/");
66
+ addr[1] = addr.slice(1).join("/");
67
+ addr[0] = addr.split(":");
27
68
 
28
- read_file = async(address) =>{
29
- let content = await this.manager.oracle.read(address, {account: this.physical_address})
69
+ payload.server = {
70
+ hostname: addr[0][0],
71
+ port: Number(addr[0][1]) || null,
72
+ };
73
+ payload.path = addr[1];
74
+ }
30
75
 
31
- return content;
32
- }
76
+ return payload;
77
+ };
33
78
 
34
- write_file = async(address, content)=>{
35
- let res = await this.manager.oracle.write(address, JSON.stringify(content), {account: this.physical_address})
36
- }
37
-
38
- read = async(address, query)=>{
39
-
40
- }
79
+ read = async (address, query) => {};
41
80
  }
42
81
 
43
-
44
- export default Utils
82
+ export default Utils;
@@ -1,20 +1,22 @@
1
1
  import { gen_random_int } from "generalised-datastore/utils/functions.js";
2
2
  import { post_request } from "../../utils/services.js";
3
3
 
4
- const net = async(args, {vm, chain, call_config, thread})=>{
5
- let {server, path, account, payload} = args;
4
+ const net = async (args, { vm, chain, no_then, call_config, thread }) => {
5
+ let { server, path, account, payload } = args;
6
6
 
7
- thread.status = 'waiting'
8
- path = await path.literal()
9
- payload = await payload.literal()
10
- let serv = server && [await server.literal()];
11
- account = account ? await account.literal() : vm.account.name
12
- if (!serv){
13
- serv = await vm.account.manager.oracle.get_servers(`Accounts/${account}`)
7
+ thread.status = "waiting";
8
+ path = await path.literal();
9
+ if (no_then) {
10
+ payload.body = await payload.body.config;
11
+ } else payload = await payload.literal();
12
+ let serv = server && [no_then ? server : await server.literal()];
13
+ account = account ? await account.literal() : vm.account.name;
14
+ if (!serv && account) {
15
+ serv = await vm.account.manager.oracle.get_servers(`Accounts/${account}`);
14
16
  }
15
17
 
16
- let s = gen_random_int(serv.length-1)
17
- let servr = serv[s]
18
+ let s = gen_random_int(serv.length - 1);
19
+ let servr = serv[s];
18
20
 
19
21
  post_request({
20
22
  options: {
@@ -23,20 +25,23 @@ const net = async(args, {vm, chain, call_config, thread})=>{
23
25
  path: `/${path}`,
24
26
  method: payload.header.method.toUpperCase(),
25
27
  headers: {
26
- ...payload.header
27
- }
28
+ ...payload.header,
29
+ },
28
30
  },
29
- data: payload.body && JSON.stringify(payload.body)
30
- }).then(async res=>{
31
- let adr = await vm.parse_aircode(res, {chain})
32
- thread.results[thread.pointer-1] = adr;
33
- thread.status = 'active'
34
- }).catch(e=>{
35
- console.log(e)
31
+ data: payload.body && JSON.stringify(payload.body),
36
32
  })
33
+ .then(async (res) => {
34
+ if (no_then) return;
37
35
 
38
- return;
39
- }
36
+ let adr = await vm.parse_aircode(res, { chain });
37
+ thread.results[thread.pointer - 1] = adr;
38
+ thread.status = "active";
39
+ })
40
+ .catch((e) => {
41
+ console.log(e);
42
+ });
40
43
 
44
+ return;
45
+ };
41
46
 
42
- export default net;
47
+ export default net;
@@ -1,12 +1,17 @@
1
- const query = async(args, {vm, thread}) => {
2
- let {object, filter} = args;
1
+ const query_ = async (object, filter, folder) => {
2
+ let res = filter ? await folder.pass(object, filter) : true;
3
3
 
4
- object = await object.literal()
5
- filter = await filter.literal()
6
-
7
- let res = await vm.account.chain.folder().pass(object, filter)
8
-
9
4
  return res;
10
- }
5
+ };
6
+
7
+ const query = async (args, { vm, thread }) => {
8
+ let { object, filter } = args;
9
+
10
+ object = await object.literal();
11
+ filter = await filter.literal();
12
+
13
+ return await query_(object, filter, await vm.account.chain.folder());
14
+ };
11
15
 
12
- export default query
16
+ export default query;
17
+ export { query_ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "1.2.51",
3
+ "version": "1.2.53",
4
4
  "description": "A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.",
5
5
  "main": "Index.js",
6
6
  "type": "module",
@@ -93,26 +93,13 @@ export const handle_routes = async (req, res, manager, app) => {
93
93
 
94
94
  switch (req.url) {
95
95
  case "/create_account": {
96
- let acc = await manager.get_account(data.name);
97
- if (acc) {
98
- if (!(await acc.verify(data.password))) {
99
- result = { message: "Incorrect password." };
100
- } else {
101
- const uid = generate_random_string(16, "alnum");
102
- await set_account_uid(uid, data.name, manager);
103
- result = { uid, message: "Account signed-in successfully." };
104
- }
96
+ let acc = await manager.get_account(data.name, data.password);
97
+ if (acc?.physical_address) {
98
+ const uid = generate_random_string(16, "alnum");
99
+ await set_account_uid(uid, data.name, manager);
100
+ result = { uid, message: "Account signed-in successfully." };
105
101
  } else {
106
- let new_acc = await manager.add_account(data.name, {
107
- password: data.password,
108
- });
109
- if (new_acc) {
110
- const uid = generate_random_string(16, "alnum");
111
- await set_account_uid(uid, data.name, manager);
112
- result = { uid, message: "Account created successfully." };
113
- } else {
114
- result = { message: "Account creation failed." };
115
- }
102
+ result = { message: acc };
116
103
  }
117
104
  break;
118
105
  }