wao 0.32.1 → 0.32.2

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/esm/hb3.js DELETED
@@ -1,418 +0,0 @@
1
- import { connect, createSigner } from "@permaweb/aoconnect"
2
- import { isEmpty, last, isNotNil, mergeLeft, clone } from "ramda"
3
- import { rsaid, hmacid, toAddr, buildTags } from "./utils.js"
4
- import { sign, signer } from "./signer.js"
5
- import { send as _send } from "./send.js"
6
- import hyper_aos from "./lua/hyper-aos.js"
7
- import aos_wamr from "./lua/aos_wamr.js"
8
- import { from } from "./httpsig.js"
9
-
10
- const seed = num => {
11
- const array = new Array(num)
12
- for (let i = 0; i < num; i++) array[i] = Math.floor(Math.random() * 256)
13
- return Buffer.from(array).toString("base64")
14
- }
15
-
16
- class HB {
17
- constructor({
18
- url = "http://localhost:10001",
19
- cu = "http://localhost:6363",
20
- jwk,
21
- } = {}) {
22
- this.cu = cu
23
- this.url = url
24
- if (jwk) this._init(jwk)
25
- }
26
- async signEncoded(encoded) {
27
- const { path, ...msg } = encoded
28
- return await sign({
29
- jwk: this.jwk,
30
- msg,
31
- path,
32
- url: this.url,
33
- })
34
- }
35
-
36
- _init(jwk) {
37
- this.jwk = jwk
38
- this.signer = createSigner(jwk, this.url)
39
- this.addr = toAddr(jwk.n)
40
- this.sign = signer({ signer: this.signer, url: this.url })
41
-
42
- const { request } = connect({
43
- MODE: "mainnet",
44
- URL: this.url,
45
- device: "",
46
- signer: this.signer,
47
- })
48
-
49
- this._request = request
50
- }
51
-
52
- async setInfo() {
53
- if (!this._info) {
54
- try {
55
- this._info = await this.g("/~meta@1.0/info")
56
- } catch (e) {}
57
- }
58
- }
59
-
60
- async init(jwk) {
61
- this._init(jwk)
62
- await this.setInfo()
63
- return this
64
- }
65
-
66
- async send(msg) {
67
- return await _send(msg)
68
- }
69
-
70
- async getImage() {
71
- const wasm = Buffer.from(aos_wamr, "base64")
72
- const id = await this.cacheBinary(wasm, "application/wasm")
73
- this.image ??= id
74
- return id
75
- }
76
-
77
- async getLua() {
78
- const lua = Buffer.from(hyper_aos, "base64")
79
- const id = await this.cacheScript(lua, "application/lua")
80
- this.lua ??= id
81
- return id
82
- }
83
-
84
- async scheduleAOS({ pid, action = "Eval", tags = {}, data }) {
85
- pid ??= this.pid
86
- let _tags = mergeLeft(tags, {
87
- device: "process@1.0",
88
- method: "POST",
89
- path: `/${pid}~process@1.0/schedule`,
90
- scheduler: this.scheduler,
91
- type: "Message",
92
- Action: action,
93
- target: pid,
94
- })
95
- if (data) _tags.data = data
96
- let res = await this.post(_tags)
97
- const slot = res.headers.slot
98
- return { slot, res, pid }
99
- }
100
-
101
- async messageAOS(args) {
102
- const { slot, pid } = await this.scheduleAOS(args)
103
- return { slot, outbox: await this.computeAOS({ pid, slot }) }
104
- }
105
-
106
- async messageLegacy(args) {
107
- const { slot, pid } = await this.scheduleLegacy(args)
108
- return { slot, res: await this.computeLegacy({ pid, slot }) }
109
- }
110
-
111
- async computeAOS({ pid, slot }) {
112
- return await this.getJSON({ path: `/${pid}/compute/results/outbox`, slot })
113
- }
114
-
115
- async computeLua({ pid, slot }) {
116
- return await this.getJSON({ path: `/${pid}/compute/results`, slot })
117
- }
118
-
119
- async compute({ pid, slot, path = "" }) {
120
- if (path && !/^\//.test(path)) path = "/" + path
121
- return await this.getJSON({ path: `/${pid}/compute${path}`, slot })
122
- }
123
-
124
- async computeLegacy({ pid, slot }) {
125
- const json = await this.compute({ pid, slot })
126
- return JSON.parse(json.results.json.body)
127
- }
128
-
129
- async spawn(tags = {}) {
130
- const addr = await this.g("/~meta@1.0/info/address")
131
- this.scheduler ??= addr
132
- const _tags = mergeLeft(tags, {
133
- device: "process@1.0",
134
- path: "/schedule",
135
- scheduler: this.scheduler,
136
- "random-seed": seed(16),
137
- type: "Process",
138
- "execution-device": "test-device@1.0",
139
- })
140
- const res = await this.post(_tags)
141
- return { res, pid: res.headers.process }
142
- }
143
-
144
- async cacheScript(data, type = "application/lua") {
145
- if (!this.cache) {
146
- const { pid } = await this.spawn({})
147
- this.cache = pid
148
- }
149
- const { slot } = await this.schedule({
150
- data,
151
- pid: this.cache,
152
- "content-type": type,
153
- })
154
- const msgs = await this.messages({ pid: this.cache, from: slot, limit: 1 })
155
- return msgs.edges[0].node.message.Id
156
- }
157
-
158
- async cacheBinary(data, type) {
159
- const res = await this.post({ path: "/~wao@1.0/cache_module", data, type })
160
- return res.headers.id
161
- }
162
-
163
- async message(args) {
164
- const pid = args.pid
165
- const { slot } = await this.schedule(args)
166
- const res = await this.compute({ pid, slot })
167
- return { slot, pid, res }
168
- }
169
-
170
- async scheduleLegacy({
171
- pid,
172
- action = "Eval",
173
- tags = {},
174
- data,
175
- scheduler,
176
- } = {}) {
177
- if (action) tags.Action = action
178
- return await this.schedule({ pid, tags, data, scheduler })
179
- }
180
-
181
- async scheduleLua(...args) {
182
- return await this.scheduleLegacy(...args)
183
- }
184
-
185
- async schedule({ pid, tags = {}, data } = {}) {
186
- pid ??= this.pid
187
- let _tags = mergeLeft(tags, {
188
- method: "POST",
189
- path: `/${pid}/schedule`,
190
- type: "Message",
191
- target: pid,
192
- })
193
- if (data) _tags.data = data
194
- let res = await this.post(_tags)
195
- return { slot: res.headers.slot, res, pid }
196
- }
197
-
198
- async spawnAOS(image) {
199
- const addr = await this.g("/~meta@1.0/info/address")
200
- this.scheduler ??= addr
201
- image ??= this.image ?? (await this.getImage())
202
- const res = await this.post({
203
- device: "process@1.0",
204
- path: "/schedule",
205
- scheduler: this.scheduler,
206
- "data-protocol": "ao",
207
- variant: "ao.N.1",
208
- "scheduler-location": this.scheduler,
209
- authority: this.scheduler,
210
- "random-seed": seed(16),
211
- type: "Process",
212
- image,
213
- "execution-device": "stack@1.0",
214
- "push-device": "push@1.0",
215
- "device-stack": [
216
- "wasi@1.0",
217
- "json-iface@1.0",
218
- "wasm-64@1.0",
219
- "patch@1.0",
220
- "multipass@1.0",
221
- ],
222
- "output-prefix": "wasm",
223
- "patch-from": "/results/outbox",
224
- "patch-mode": "patches",
225
- "stack-keys": ["init", "compute", "snapshot", "normalize"],
226
- passes: 2,
227
- })
228
- const pid = res.headers.process
229
- this.pid ??= pid
230
- return { pid, res }
231
- }
232
-
233
- async spawnLua(lua) {
234
- const addr = await this.g("/~meta@1.0/info/address")
235
- this.scheduler ??= addr
236
- lua ??= this.lua ?? (await this.getLua())
237
- const res = await this.post({
238
- device: "process@1.0",
239
- path: "/schedule",
240
- scheduler: this.scheduler,
241
- "data-protocol": "ao",
242
- variant: "ao.N.1",
243
- "scheduler-location": this.scheduler,
244
- authority: this.scheduler,
245
- "random-seed": seed(16),
246
- type: "Process",
247
- module: lua,
248
- "execution-device": "lua@5.3a",
249
- "push-device": "push@1.0",
250
- "patch-from": "/results/outbox",
251
- })
252
- const pid = res.headers.process
253
- this.pid ??= pid
254
- return { pid, res }
255
- }
256
-
257
- async now({ pid, path = "" }) {
258
- if (path && !/^\//.test(path)) path = "/" + path
259
- return await this.getJSON({ path: `/${pid}/now${path}` })
260
- }
261
-
262
- async slot({ pid, path = "" }) {
263
- if (path && !/^\//.test(path)) path = "/" + path
264
- return await this.getJSON({ path: `/${pid}/slot${path}` })
265
- }
266
-
267
- async messages({ pid, from, to } = {}) {
268
- let params = `target=${pid}`
269
- if (isNotNil(from)) params += `&from=${from}`
270
- if (isNotNil(to)) params += `&to=${to}`
271
- params += `&accept=application/aos-2`
272
- let res = await fetch(`${this.url}/~scheduler@1.0/schedule?${params}`).then(
273
- r => r.json()
274
- )
275
- if (res.page_info.has_next_page) {
276
- res.next = async () => {
277
- const from2 = last(res.edges).cursor + 1
278
- return await this.messages({ pid, from: from2, to })
279
- }
280
- }
281
- return res
282
- }
283
-
284
- async spawnLegacy({ module, tags = {}, data } = {}) {
285
- await this.setInfo()
286
- let t = {
287
- type: "Process",
288
- "data-protocol": "ao",
289
- variant: "ao.TN.1",
290
- scheduler: this._info.address,
291
- authority: this._info.address,
292
- "scheduler-location": this._info.address,
293
- "random-seed": seed(16),
294
- module: module ?? "ISShJH1ij-hPPt9St5UFFr_8Ys3Kj5cyg7zrMGt7H9s",
295
- device: "process@1.0",
296
- "scheduler-device": "scheduler@1.0",
297
- "execution-device": "stack@1.0",
298
- "push-device": "push@1.0",
299
- "device-stack": ["genesis-wasm@1.0", "patch@1.0"],
300
- "patch-from": "/results/outbox",
301
- "stack-keys": ["init", "compute", "snapshot", "normalize"],
302
- }
303
- if (data) t.data = data
304
- tags = mergeLeft(tags, t)
305
- return await this.spawn(tags)
306
- }
307
-
308
- async results({ process, limit, sort = "DESC", from, to } = {}) {
309
- let params = ""
310
- const addParam = (key, val) => {
311
- params += params === "" ? "?" : "&"
312
- params += `${key}=${val}`
313
- }
314
- if (limit) addParam("limit", limit)
315
- if (sort) addParam("sort", sort)
316
- if (from) addParam("from", from)
317
- if (to) addParam("to", to)
318
- const res = await this.post({
319
- path: "/~relay@1.0/call",
320
- method: "GET",
321
- "relay-path": `${this.cu}/results/${process}${params}`,
322
- "content-type": "application/json",
323
- })
324
- return JSON.parse(res.body)
325
- }
326
-
327
- async dryrun({ tags = {}, pid, action, data } = {}) {
328
- if (typeof action === "string") tags.Action = action
329
- let json = { Tags: buildTags({ ...tags }), Owner: this.addr }
330
- if (data) json.Data = data
331
- const res = await this.post({
332
- path: "/~relay@1.0/call",
333
- method: "POST",
334
- "relay-path": `${this.cu}/dry-run?process-id=${pid}`,
335
- "content-type": "application/json",
336
- "relay-body": JSON.stringify(json),
337
- })
338
- return JSON.parse(res.body)
339
- }
340
-
341
- async commit(obj, opts) {
342
- const msg = await this.sign(obj, opts)
343
- const hmacId = hmacid(msg.headers)
344
- const rsaId = rsaid(msg.headers)
345
- const committer = this.addr
346
- const meta = { alg: "rsa-pss-sha512", "commitment-device": "httpsig@1.0" }
347
- const meta2 = { alg: "hmac-sha256", "commitment-device": "httpsig@1.0" }
348
- const sigs = {
349
- signature: msg.headers.signature,
350
- "signature-input": msg.headers["signature-input"],
351
- }
352
- return {
353
- commitments: {
354
- [rsaId]: { ...meta, committer, ...sigs },
355
- [hmacId]: { ...meta2, ...sigs },
356
- },
357
- ...obj,
358
- }
359
- }
360
-
361
- async p(path, ...args) {
362
- let _args = clone(args)
363
- _args[0] ??= {}
364
- _args[0].path ??= path
365
- return (await this.post(..._args))?.out ?? null
366
- }
367
-
368
- async post(obj, json) {
369
- const _json = json ? "/~json@1.0/serialize" : ""
370
- obj.path += _json
371
- return await this.send(await this.sign(obj))
372
- }
373
-
374
- async g(path, ...args) {
375
- let _args = clone(args)
376
- _args[0] ??= {}
377
- _args[0].path ??= path
378
- return (await this.get(..._args))?.out ?? null
379
- }
380
-
381
- async get({ path, ...params }, json = false) {
382
- const _json = json ? "/~json@1.0/serialize" : ""
383
- path ??= "/~message@1.0"
384
- if (!/^\//.test(path)) path = "/" + path
385
- let _params = ""
386
- if (!isEmpty(params)) {
387
- let i = 0
388
- for (const k in params) {
389
- _params += `${i === 0 ? "?" : "&"}${k}=${params[k]}`
390
- i++
391
- }
392
- }
393
- const response = await fetch(`${this.url}${path}${_json}${_params}`)
394
- let headers = {}
395
- response.headers.forEach((v, k) => (headers[k] = v))
396
- const http = {
397
- headers,
398
- body: await response.text(),
399
- status: response.status,
400
- }
401
- return {
402
- ...from(http),
403
- ...http,
404
- }
405
- }
406
-
407
- async postJSON(args) {
408
- const res = await this.post(args, true)
409
- return JSON.parse(res.body)
410
- }
411
-
412
- async getJSON(args) {
413
- const res = await this.get(args, true)
414
- return JSON.parse(res.body)
415
- }
416
- }
417
-
418
- export default HB