godprotocol 1.2.26 → 1.2.27
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.
- package/Objects/Account.js +162 -132
- package/Objects/Manager.js +9 -11
- package/Objects/Oracle.js +29 -9
- package/README.md +0 -0
- package/package.json +1 -1
package/Objects/Account.js
CHANGED
|
@@ -5,185 +5,215 @@ import Virtual_machine from "./Virtual_machine.js";
|
|
|
5
5
|
import register from "godprotocol/callables/register.js";
|
|
6
6
|
import { hash } from "godprotocol/utils/hash.js";
|
|
7
7
|
|
|
8
|
-
class Account extends Utils{
|
|
9
|
-
constructor(name, options){
|
|
10
|
-
super()
|
|
11
|
-
|
|
12
|
-
this.name = name
|
|
13
|
-
this.options = options
|
|
14
|
-
this.manager = options.manager
|
|
15
|
-
this.vm = new Virtual_machine(this)
|
|
16
|
-
this.compiler = new Compiler(this)
|
|
8
|
+
class Account extends Utils {
|
|
9
|
+
constructor(name, options) {
|
|
10
|
+
super();
|
|
11
|
+
|
|
12
|
+
this.name = name;
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.manager = options.manager;
|
|
15
|
+
this.vm = new Virtual_machine(this);
|
|
16
|
+
this.compiler = new Compiler(this);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
load = async(payload, options)=>{
|
|
20
|
-
let {address, sequence, compiler, from_reload, _id} = payload
|
|
19
|
+
load = async (payload, options) => {
|
|
20
|
+
let { address, sequence, compiler, from_reload, _id } = payload;
|
|
21
21
|
|
|
22
|
-
let phy = `${this.physical_address}/${address}
|
|
23
|
-
let chain = await this.chain.chain(phy)
|
|
22
|
+
let phy = `${this.physical_address}/${address}`;
|
|
23
|
+
let chain = await this.chain.chain(phy);
|
|
24
24
|
|
|
25
|
-
if (compiler){
|
|
26
|
-
!from_reload &&
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
if (compiler) {
|
|
26
|
+
!from_reload &&
|
|
27
|
+
(await this.manager.oracle.write(`${phy}.air`, sequence, {
|
|
28
|
+
account: this.physical_address,
|
|
29
|
+
prefix: ".codes",
|
|
30
|
+
address: phy,
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
sequence = await this.compiler.to_sequence(sequence, address);
|
|
34
|
+
sequence = sequence[0];
|
|
30
35
|
}
|
|
31
|
-
|
|
32
|
-
let res = await chain.add_config(sequence, {...options, _id})
|
|
36
|
+
|
|
37
|
+
let res = await chain.add_config(sequence, { ...options, _id });
|
|
33
38
|
|
|
34
39
|
return res;
|
|
35
|
-
}
|
|
40
|
+
};
|
|
36
41
|
|
|
37
|
-
run = async(payload, socket)=>{
|
|
38
|
-
let {address, _id, env, config, thread, callback}= payload;
|
|
42
|
+
run = async (payload, socket) => {
|
|
43
|
+
let { address, _id, env, config, thread, callback } = payload;
|
|
39
44
|
|
|
40
|
-
if(!config){
|
|
41
|
-
let phy = `${this.physical_address}/${address}
|
|
42
|
-
let chain = await this.chain.chain(phy)
|
|
45
|
+
if (!config) {
|
|
46
|
+
let phy = `${this.physical_address}/${address}`;
|
|
47
|
+
let chain = await this.chain.chain(phy);
|
|
43
48
|
|
|
44
|
-
let folder = await chain.folder(
|
|
45
|
-
config = await folder.readone(_id? {_id}: null)
|
|
49
|
+
let folder = await chain.folder(".configs");
|
|
50
|
+
config = await folder.readone(_id ? { _id } : null);
|
|
46
51
|
|
|
47
|
-
if (!config){
|
|
48
|
-
let code = await this.manager.oracle.read(`${phy}.air`, {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
if (!config) {
|
|
53
|
+
let code = await this.manager.oracle.read(`${phy}.air`, {
|
|
54
|
+
account: this.physical_address,
|
|
55
|
+
prefix: ".codes",
|
|
56
|
+
address: phy,
|
|
57
|
+
});
|
|
58
|
+
if (code) {
|
|
59
|
+
code = await this.compiler.to_sequence(code, address);
|
|
60
|
+
code = code[0];
|
|
52
61
|
|
|
53
62
|
config = code;
|
|
54
63
|
|
|
55
|
-
await this.load({address, sequence: config, _id})
|
|
64
|
+
await this.load({ address, sequence: config, _id });
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
|
-
|
|
60
|
-
if (!config){
|
|
61
|
-
return {message:
|
|
68
|
+
|
|
69
|
+
if (!config) {
|
|
70
|
+
return { message: "Code not found!" };
|
|
62
71
|
}
|
|
63
72
|
|
|
64
|
-
let result = await this.vm.nodelist(config)
|
|
65
|
-
|
|
66
|
-
let thread_id = await this.manager.load(result, this, {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
callback
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
let result = await this.vm.nodelist(config);
|
|
74
|
+
|
|
75
|
+
let thread_id = await this.manager.load(result, this, {
|
|
76
|
+
spawn: thread && thread._id,
|
|
77
|
+
pointer: thread && thread.pointer,
|
|
78
|
+
callback: async (res) => {
|
|
79
|
+
if (typeof callback === "string") {
|
|
80
|
+
if (callback === "store" && !Array.isArray(res)) {
|
|
81
|
+
let store_fn = await this.vm.callable("store");
|
|
82
|
+
await store_fn.callable(
|
|
83
|
+
{ object: await this.vm.cloth(res) },
|
|
84
|
+
{ vm: this.vm, chain: this.chain }
|
|
85
|
+
);
|
|
86
|
+
callback = res.value;
|
|
87
|
+
} else {
|
|
88
|
+
let folder = await this.ds.folder(callback);
|
|
89
|
+
await folder.write(res, { replace: true });
|
|
90
|
+
}
|
|
91
|
+
} else if (typeof callback === "function") {
|
|
92
|
+
await callback(res);
|
|
75
93
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}})
|
|
94
|
+
if (socket) {
|
|
95
|
+
// handle socket
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
});
|
|
83
99
|
|
|
84
|
-
if (env){
|
|
85
|
-
this.vm.envs[thread_id] = env
|
|
100
|
+
if (env) {
|
|
101
|
+
this.vm.envs[thread_id] = env;
|
|
86
102
|
}
|
|
87
103
|
|
|
88
|
-
return {thread_id, callback}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
parse = async (payload)=>{
|
|
92
|
-
let {address, query, retrieve, from} = payload
|
|
104
|
+
return { thread_id, callback };
|
|
105
|
+
};
|
|
93
106
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
message: 'Invalid payload.'
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
let phy = `${this.physical_address}/${address}`
|
|
107
|
+
parse = async (payload) => {
|
|
108
|
+
let { address, query, retrieve, from } = payload;
|
|
100
109
|
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}else return {message: 'Code not found'}
|
|
110
|
+
if (!address) {
|
|
111
|
+
return {
|
|
112
|
+
message: "Invalid payload.",
|
|
113
|
+
};
|
|
106
114
|
}
|
|
107
|
-
|
|
115
|
+
let phy = `${this.physical_address}/${address}`;
|
|
116
|
+
|
|
117
|
+
if (from === ".codes") {
|
|
118
|
+
let res = await this.manager.oracle.read(`${phy}.air`, {
|
|
119
|
+
account: this.physical_address,
|
|
120
|
+
address: phy,
|
|
121
|
+
prefix: from,
|
|
122
|
+
});
|
|
123
|
+
if (res) {
|
|
124
|
+
return { result: res };
|
|
125
|
+
} else return { message: "Code not found" };
|
|
126
|
+
} else if (from === "thread") {
|
|
108
127
|
// is thread_id;
|
|
109
|
-
let thread_info = this.manager.accounts_pairs[this.name]
|
|
110
|
-
let thread = thread_info.threads[address]
|
|
111
|
-
|
|
112
|
-
return thread
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
128
|
+
let thread_info = this.manager.accounts_pairs[this.name];
|
|
129
|
+
let thread = thread_info.threads[address];
|
|
130
|
+
|
|
131
|
+
return thread
|
|
132
|
+
? {
|
|
133
|
+
_id: thread._id,
|
|
134
|
+
status: thread.status,
|
|
135
|
+
spawn: thread.spawn,
|
|
136
|
+
callback: thread.callback,
|
|
137
|
+
pointer: thread.pointer,
|
|
138
|
+
total: thread.bullets.length,
|
|
139
|
+
}
|
|
140
|
+
: {};
|
|
120
141
|
}
|
|
121
142
|
|
|
122
|
-
let folder = await this.ds.folder(
|
|
123
|
-
|
|
124
|
-
|
|
143
|
+
let folder = await this.ds.folder(
|
|
144
|
+
phy,
|
|
145
|
+
from === "ram" ? null : { account: this.physical_address }
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
let result = await folder.readone(query.query, { ...query.options });
|
|
125
149
|
if (!result) return result;
|
|
126
|
-
|
|
127
|
-
if (from !==
|
|
128
|
-
if (this.vm.datatypes.includes(result.type)){
|
|
129
|
-
let obj = await this.vm.retrieve(result, true)
|
|
130
|
-
if (retrieve){
|
|
131
|
-
result = await obj.to_address()
|
|
132
|
-
} else result.literal = await
|
|
133
|
-
}else if (retrieve){
|
|
134
|
-
result = await this.vm.retrieve(result)
|
|
150
|
+
|
|
151
|
+
if (from !== "ram") {
|
|
152
|
+
if (this.vm.datatypes.includes(result.type)) {
|
|
153
|
+
let obj = await this.vm.retrieve(result, true);
|
|
154
|
+
if (retrieve) {
|
|
155
|
+
result = await obj.to_address();
|
|
156
|
+
} else result.literal = await obj.literal();
|
|
157
|
+
} else if (retrieve) {
|
|
158
|
+
result = await this.vm.retrieve(result);
|
|
135
159
|
}
|
|
136
160
|
} else {
|
|
137
|
-
if (this.vm.datatypes.includes(result.type)){
|
|
138
|
-
if (retrieve){
|
|
139
|
-
result = await (await this.vm.cloth_content(result)).to_address()
|
|
140
|
-
}else
|
|
141
|
-
|
|
142
|
-
|
|
161
|
+
if (this.vm.datatypes.includes(result.type)) {
|
|
162
|
+
if (retrieve) {
|
|
163
|
+
result = await (await this.vm.cloth_content(result)).to_address();
|
|
164
|
+
} else
|
|
165
|
+
result.literal = await (
|
|
166
|
+
await this.vm.cloth_content(result)
|
|
167
|
+
).literal();
|
|
168
|
+
} else if (retrieve) {
|
|
169
|
+
result = await this.vm.cloth_content(result);
|
|
143
170
|
}
|
|
144
171
|
}
|
|
145
172
|
|
|
146
|
-
return result
|
|
147
|
-
}
|
|
173
|
+
return result;
|
|
174
|
+
};
|
|
148
175
|
|
|
149
|
-
verify = async(password)=>{
|
|
150
|
-
let
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
verify = async (password) => {
|
|
177
|
+
let passes = await this.ds.folder(`${this.physical_address}/__passwords`, {
|
|
178
|
+
account: this.physical_address,
|
|
179
|
+
});
|
|
153
180
|
|
|
154
|
-
let
|
|
181
|
+
let pass = await passes.readone(),
|
|
182
|
+
passed = false;
|
|
155
183
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
passed && await this.manager.oracle.update_servers(this.physical_address)
|
|
162
|
-
return passed
|
|
163
|
-
}else await passes.write({key: hash(password)})
|
|
184
|
+
if (pass) {
|
|
185
|
+
passed = pass && pass.key === hash(password);
|
|
186
|
+
} else {
|
|
187
|
+
passed = await passes.write({ key: hash(password) });
|
|
188
|
+
}
|
|
164
189
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.ds = this.manager.ds
|
|
190
|
+
this.servers =
|
|
191
|
+
passed &&
|
|
192
|
+
(await this.manager.oracle.update_servers(this.physical_address));
|
|
193
|
+
|
|
194
|
+
return passed;
|
|
195
|
+
};
|
|
172
196
|
|
|
173
|
-
|
|
197
|
+
sync = async () => {
|
|
198
|
+
this.physical_address = `${"Accounts"}/${this.name}`;
|
|
199
|
+
this.chain = new Chain(`${this.physical_address}`, this);
|
|
200
|
+
this.ds = this.manager.ds;
|
|
201
|
+
|
|
202
|
+
let pass = this.options.password,
|
|
203
|
+
res = true;
|
|
174
204
|
|
|
175
205
|
if (pass) {
|
|
176
|
-
res = await this.verify(pass)
|
|
206
|
+
res = await this.verify(pass);
|
|
177
207
|
}
|
|
178
208
|
|
|
179
|
-
await register(this)
|
|
209
|
+
await register(this);
|
|
180
210
|
|
|
181
|
-
if (res){
|
|
182
|
-
console.log(`Account:${this.name} initiation successful!`)
|
|
183
|
-
} else console.log(`Account:${this.name} initiation failed!`)
|
|
211
|
+
if (res) {
|
|
212
|
+
console.log(`Account:${this.name} initiation successful!`);
|
|
213
|
+
} else console.log(`Account:${this.name} initiation failed!`);
|
|
184
214
|
|
|
185
215
|
return res;
|
|
186
|
-
}
|
|
216
|
+
};
|
|
187
217
|
}
|
|
188
218
|
|
|
189
219
|
export default Account;
|
package/Objects/Manager.js
CHANGED
|
@@ -372,22 +372,20 @@ class Manager {
|
|
|
372
372
|
app: this.options.app,
|
|
373
373
|
});
|
|
374
374
|
|
|
375
|
-
this.handler = (req, res, cb) => {
|
|
375
|
+
this.handler = async (req, res, cb) => {
|
|
376
376
|
handle(req, res);
|
|
377
377
|
|
|
378
378
|
if (this.options.oracle && !this.served) {
|
|
379
379
|
this.served = true;
|
|
380
380
|
this.oracle = this.options.oracle;
|
|
381
|
-
this.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
});
|
|
381
|
+
if (this.options.init_account) {
|
|
382
|
+
await this.add_account(this.options.init_account.name, {
|
|
383
|
+
password: this.options.init_account.password,
|
|
384
|
+
init: true,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
cb && cb();
|
|
388
|
+
}
|
|
391
389
|
}
|
|
392
390
|
};
|
|
393
391
|
}
|
package/Objects/Oracle.js
CHANGED
|
@@ -3,15 +3,14 @@ import { post_request } from "../utils/services.js";
|
|
|
3
3
|
class Oracle_client {
|
|
4
4
|
constructor(server) {
|
|
5
5
|
this.server = server;
|
|
6
|
+
|
|
7
|
+
this.repo_cache = new Object();
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
add_repo = async (repo
|
|
10
|
+
add_repo = async (repo) => {
|
|
9
11
|
await this.fetch({
|
|
10
12
|
method: "add_repo",
|
|
11
|
-
args:
|
|
12
|
-
repo,
|
|
13
|
-
filter,
|
|
14
|
-
},
|
|
13
|
+
args: repo,
|
|
15
14
|
});
|
|
16
15
|
};
|
|
17
16
|
|
|
@@ -23,7 +22,9 @@ class Oracle_client {
|
|
|
23
22
|
};
|
|
24
23
|
|
|
25
24
|
read = async (path) => {
|
|
26
|
-
let repo = this.repo_cache[path]
|
|
25
|
+
let repo = this.repo_cache[path],
|
|
26
|
+
content;
|
|
27
|
+
|
|
27
28
|
if (repo) {
|
|
28
29
|
content = await repo.read(path);
|
|
29
30
|
}
|
|
@@ -42,21 +43,40 @@ class Oracle_client {
|
|
|
42
43
|
return content;
|
|
43
44
|
};
|
|
44
45
|
|
|
46
|
+
server_match = (s1, s2) => s1.hostname === s2.hostname && s1.port === s2.port;
|
|
47
|
+
|
|
48
|
+
update_servers = async (account) => {
|
|
49
|
+
let addr = `${account}/.servers`;
|
|
50
|
+
|
|
51
|
+
let servers = await this.read(addr);
|
|
52
|
+
servers = servers ? JSON.parse(servers) : [];
|
|
53
|
+
if (!servers.find((s) => this.server_match(s, this.client))) {
|
|
54
|
+
servers.push(this.client);
|
|
55
|
+
await this.write(addr, JSON.stringify(servers));
|
|
56
|
+
}
|
|
57
|
+
return servers;
|
|
58
|
+
};
|
|
59
|
+
|
|
45
60
|
fetch = async (payload) => {
|
|
61
|
+
let headers;
|
|
46
62
|
if (payload.method !== "authenticate" && !this.auth_token) {
|
|
47
63
|
await this.sync(this.client);
|
|
64
|
+
} else if (payload.method !== "authenticate") {
|
|
65
|
+
headers = {
|
|
66
|
+
authorisation: this.auth_token,
|
|
67
|
+
};
|
|
48
68
|
}
|
|
69
|
+
|
|
49
70
|
try {
|
|
50
71
|
let resp = await post_request({
|
|
51
72
|
options: {
|
|
52
73
|
...this.server,
|
|
53
74
|
path: `/oracle/${payload.method}`,
|
|
54
|
-
headers
|
|
55
|
-
authorisation: this.auth_token,
|
|
56
|
-
},
|
|
75
|
+
headers,
|
|
57
76
|
},
|
|
58
77
|
data: JSON.stringify(payload.args),
|
|
59
78
|
});
|
|
79
|
+
|
|
60
80
|
return resp || null;
|
|
61
81
|
} catch (e) {
|
|
62
82
|
console.warn("fetch error:", e.message || e);
|
package/README.md
CHANGED
|
Binary file
|
package/package.json
CHANGED