wao 0.9.0 → 0.9.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/lfdb.js ADDED
@@ -0,0 +1,68 @@
1
+ import lf from "localforage"
2
+ import { is, keys } from "ramda"
3
+
4
+ export default _this => {
5
+ return {
6
+ put: async (key, val) => {
7
+ let _val = null
8
+ if (typeof val === "object" && !is(Array, val)) {
9
+ _val = {}
10
+ for (let k in val) {
11
+ if (typeof val[k] !== "function") {
12
+ if (is(Array, val[k])) {
13
+ let _arr = []
14
+ for (let v of val[k]) {
15
+ if (
16
+ typeof v === "object" &&
17
+ !is(Array, v) &&
18
+ !(v instanceof Uint8Array) &&
19
+ !(v instanceof ArrayBuffer)
20
+ ) {
21
+ let __val = {}
22
+ for (let k2 in v) {
23
+ if (typeof v[k2] !== "function") __val[k2] = v[k2]
24
+ }
25
+ _arr.push(__val)
26
+ } else {
27
+ _arr.push(v)
28
+ }
29
+ }
30
+ _val[k] = _arr
31
+ } else if (
32
+ typeof val[k] === "object" &&
33
+ !(val[k] instanceof Uint8Array) &&
34
+ !(val[k] instanceof ArrayBuffer)
35
+ ) {
36
+ let __val = {}
37
+ for (let k2 in val[k]) {
38
+ if (typeof val[k][k2] !== "function") __val[k2] = val[k][k2]
39
+ }
40
+ _val[k] = __val
41
+ } else {
42
+ _val[k] = val[k]
43
+ }
44
+ }
45
+ }
46
+ } else _val = val
47
+ await lf.setItem(key, _val)
48
+ _this.keys[key] = true
49
+ await lf.setItem("keys", keys(_this.keys))
50
+ },
51
+ get: async key => {
52
+ return await lf.getItem(key)
53
+ },
54
+ getKeys: async ({ start, end }) => {
55
+ if (!_this.keyInit) {
56
+ const _keys = (await lf.getItem("keys")) ?? []
57
+ _this.keys = {}
58
+ for (const v of _keys) _this.keys[v] = true
59
+ }
60
+ let __keys = []
61
+ for (const k in _this.keys) {
62
+ const sp = k.split(".")
63
+ if (sp[0] === start) __keys.push(k)
64
+ }
65
+ return __keys
66
+ },
67
+ }
68
+ }
package/esm/tao.js CHANGED
@@ -1,127 +1,10 @@
1
- import MAO from "./ao.js"
2
- import { createDataItemSigner } from "@permaweb/aoconnect"
3
- import { srcs, buildTags } from "./utils.js"
4
- import AR from "./tar.js"
1
+ import BAO from "./bao.js"
5
2
  import { connect } from "./aoconnect.js"
6
- import { acc, mu } from "./test.js"
7
- import { mergeLeft, is, map } from "ramda"
8
-
9
- let log = `
10
- local json = require("json")
11
- ao = ao or {}
12
-
13
- function ao.log(...)
14
- local args = {...}
15
- local txt = json.encode(args)
16
- if type(ao.outbox.Output) == 'string' then
17
- ao.outbox.Output = {ao.outbox.Output}
18
- end
19
- table.insert(ao.outbox.Output, txt)
20
- ao.outbox.Logs = ao.outbox.Logs or {}
21
- table.insert(ao.outbox.Logs, txt)
22
- end
23
-
24
- function ao.result(result)
25
- if ao.outbox.Error or result.Error then
26
- return { Error = result.Error or ao.outbox.Error, Output = ao.outbox.Logs }
27
- end
28
- return {
29
- Output = ao.outbox.Logs or result.Output or ao.outbox.Output,
30
- Messages = ao.outbox.Messages,
31
- Spawns = ao.outbox.Spawns,
32
- Assignments = ao.outbox.Assignments,
33
- }
34
- end
35
- `
36
-
37
- const renderLogs = logs => {
38
- if (is(Array, logs) && logs.length > 0) {
39
- for (const v of logs) {
40
- let l = v
41
- try {
42
- const p = JSON.parse(v)
43
- if (JSON.stringify(p) === v) l = p
44
- console.log(...l)
45
- } catch (e) {
46
- console.log(l)
47
- }
48
- }
49
- }
50
- }
3
+ import AR from "./tar.js"
51
4
 
52
- class AO extends MAO {
5
+ class AO extends BAO {
53
6
  constructor(opt = {}) {
54
- super({ ...opt, in_memory: true })
55
- this.in_memory = true
56
- const {
57
- modules,
58
- results,
59
- assign,
60
- result,
61
- message,
62
- spawn,
63
- dryrun,
64
- monitor,
65
- unmonitor,
66
- mem,
67
- } = connect(opt.mem, {
68
- extensions: opt.extensions,
69
- cache: opt.cache,
70
- reset: opt.reset,
71
- })
72
- this.assign = assign
73
- this.result = async (...opt) => {
74
- const res = await result(...opt)
75
- renderLogs(res.Output)
76
- return res
77
- }
78
-
79
- this.results = results
80
- this.message = message
81
- this.spawn = async (...opt) => {
82
- const res = await spawn(...opt)
83
- await this.load({ data: log, pid: res })
84
- return res
85
- }
86
- this.dryrun = async (...opt) => {
87
- const res = await dryrun(...opt)
88
- renderLogs(res.Output)
89
- return res
90
- }
91
- this.monitor = monitor
92
- this.unmonitor = unmonitor
93
- this.mem = mem
94
- this.ar = new AR({ mem })
95
- }
96
-
97
- async init(jwk) {
98
- if (!jwk && this.acc[0]) jwk = this.acc[0].jwk
99
- await this.ar.init(jwk)
100
- return this
101
- }
102
-
103
- async postModule({ data, tags = {}, jwk }) {
104
- let err = null
105
- ;({ jwk, err } = await this.ar.checkWallet({ jwk }))
106
- if (err) return { err }
107
- const t = mergeLeft(tags, {
108
- "Data-Protocol": "ao",
109
- Variant: "ao.TN.1",
110
- Type: "Module",
111
- "Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
112
- "Input-Encoding": "JSON-V1",
113
- "Output-Encoding": "JSON-V1",
114
- "Memory-Limit": "1-gb",
115
- "Compute-Limit": "9000000000000",
116
- })
117
- const signer = createDataItemSigner(jwk)
118
- const { id, owner, item } = await this.ar.dataitem({
119
- tags: t,
120
- data,
121
- signer,
122
- })
123
- await this.ar.postItems(item, jwk)
124
- return { id }
7
+ super({ ...opt, connect, AR })
125
8
  }
126
9
  }
127
10
 
package/esm/tar.js CHANGED
@@ -1,215 +1,9 @@
1
- import MAR from "./ar.js"
2
- import { buildTags, tags as t } from "./utils.js"
3
- import * as WarpArBundles from "warp-arbundles"
4
- const pkg = WarpArBundles.default ?? WarpArBundles
5
- const { DataItem } = pkg
6
- import { bundleAndSignData, ArweaveSigner } from "arbundles"
7
- import base64url from "base64url"
1
+ import BAR from "./ar-base.js"
8
2
  import ArMem from "./armem.js"
9
- import GQL from "./tgql.js"
10
- import { last, is, includes, isNil } from "ramda"
11
3
 
12
- class AR extends MAR {
4
+ class AR extends BAR {
13
5
  constructor(opt = {}) {
14
- super({ ...opt, in_memory: true })
15
- this.log = opt.log === true
16
- this.in_memory = true
17
- this.mem = opt.mem ?? new ArMem()
18
- this.gql = new GQL({ mem: this.mem })
19
- this.arweave = this.mem.arweave
20
- }
21
- async owner(di) {
22
- const raw_owner = di.rawOwner
23
- const hashBuffer = Buffer.from(
24
- await crypto.subtle.digest("SHA-256", raw_owner),
25
- )
26
- return base64url.encode(hashBuffer)
27
- }
28
- async dataitem({ target = "", data = "1984", tags = {}, signer, item }) {
29
- let di = item
30
- if (!di) {
31
- const _tags = buildTags(tags)
32
- const _item = await signer({ data, tags: _tags, target })
33
- di = new DataItem(_item.raw)
34
- } else {
35
- tags = t(di.tags)
36
- }
37
- const owner = await this.owner(di)
38
- return { id: await di.id, owner, item: di, tags }
39
- }
40
-
41
- async post({ data = "1984", tags = {}, jwk }) {
42
- let err = null
43
- ;({ err, jwk } = await this.checkWallet({ jwk }))
44
- if (err) return { err }
45
- let tx = await this.arweave.createTransaction({ data: data })
46
- let _tags = buildTags(null, tags)
47
- for (const v of _tags) tx.addTag(v.name, v.value)
48
- return await this.postTx(tx, jwk)
49
- }
50
-
51
- async postItems(items, jwk) {
52
- let err = null
53
- ;({ err, jwk } = await this.checkWallet({ jwk }))
54
- if (err) return { err }
55
- if (!is(Array, items)) items = [items]
56
- let _items = []
57
- for (const di of items) {
58
- di._id = await di.id
59
- const data_size = Buffer.byteLength(di.rawData).toString()
60
- let data_type = ""
61
- for (const t of di.tags)
62
- if (t.name === "Content-Type") data_type = t.value
63
- const owner = await this.owner(di)
64
- await this.mem.set({ key: di.owner, address: owner }, "addrmap", owner)
65
- let data = di.data
66
- let _item = {
67
- _data: { size: data_size, type: data_type },
68
- anchor: di.anchor,
69
- signature: di.signature,
70
- recipient: di.target,
71
- id: await di.id,
72
- item: di,
73
- owner,
74
- tags: di.tags,
75
- data,
76
- }
77
- await this.mem.set(_item, "txs", await di.id)
78
- _items.push(_item)
79
- }
80
- const bundle = await bundleAndSignData(items, new ArweaveSigner(jwk))
81
- const tx = await this.mem.arweave.createTransaction(
82
- { data: bundle.binary },
83
- jwk,
84
- )
85
- tx.addTag("Bundle-Format", "binary")
86
- tx.addTag("Bundle-Version", "2.0.0")
87
- return await this.postTx(tx, jwk, _items)
88
- }
89
-
90
- async postTx(tx, jwk, items = []) {
91
- let err = null
92
- ;({ err, jwk } = await this.checkWallet({ jwk }))
93
- if (err) return { err }
94
-
95
- let res = null
96
- if (!tx.id) await this.mem.arweave.transactions.sign(tx, jwk)
97
- let height = (await this.mem.get("height")) + 1
98
- await this.mem.set(height, "height")
99
- let block = {
100
- id: tx.id,
101
- timestamp: Date.now(),
102
- height,
103
- previous: last(await this.mem.get("blocks")) ?? "",
104
- txs: [],
105
- }
106
- let msg = null
107
- if (items) {
108
- for (const item of items) {
109
- let _txs = item
110
- _txs.parent = { id: tx.id }
111
- _txs.bundledIn = { id: tx.id }
112
- _txs.anchor = ""
113
- const _tags = t(item.tags)
114
- if (
115
- includes(_tags.Type, [
116
- "Message",
117
- "Process",
118
- "Module",
119
- "Scheduler-Location",
120
- "Attestation",
121
- "Available",
122
- ])
123
- ) {
124
- msg = { id: item.id, type: _tags.Type }
125
- if (msg.type === "Process") msg.pid = item.recipient
126
- }
127
- let data_type = ""
128
- for (const v of item.item.tags) {
129
- if (v.name === "Content-Type") data_type = v.value
130
- }
131
- block.txs.push(item.id)
132
- _txs.block = block.id
133
- await this.mem.set(_txs, "txs", item.id)
134
- }
135
- }
136
- let _tags = []
137
- for (const v of tx.tags) {
138
- _tags.push({
139
- name: base64url.decode(v.name),
140
- value: base64url.decode(v.value),
141
- })
142
- }
143
- const __tags = t(_tags)
144
- if (__tags.Type === "Module") {
145
- await this.mem.set(
146
- {
147
- data: Buffer.from(tx.data, "base64"),
148
- format: __tags["Module-Format"],
149
- },
150
- "wasms",
151
- tx.id,
152
- )
153
- }
154
- tx.tags = _tags
155
- tx.owner = await this.arweave.wallets.jwkToAddress({ n: tx.owner })
156
- let _txs = tx
157
- block.txs.push(tx.id)
158
- _txs.block = block.id
159
- await this.mem.set(_txs, "txs", tx.id)
160
- let blocks = await this.mem.get("blocks")
161
- blocks.push(block.id)
162
- await this.mem.set(blocks, "blocks")
163
- await this.mem.set(block, "blockmap", block.id)
164
-
165
- if (jwk) {
166
- const owner = await this.arweave.wallets.jwkToAddress(jwk)
167
- await this.mem.set({ address: owner, key: jwk.n }, "addrmap", owner)
168
- }
169
- res = { id: tx.id, status: 200, statusText: "200" }
170
- if (this.log) {
171
- if (msg) {
172
- console.log(
173
- `New ${msg.type}:\t${msg.id}${msg.pid ? ` > ${msg.pid}` : ""}`,
174
- )
175
- } else {
176
- console.log(`New Post:\t${tx.id}`)
177
- }
178
- }
179
- return { res, err, id: tx.id }
180
- }
181
-
182
- async tx(id) {
183
- return await this.mem.get("txs", id)
184
- }
185
-
186
- async data(id, _string, log) {
187
- let decode = true
188
- let string = _string
189
- if (is(Object, _string)) {
190
- if (!isNil(_string.decode)) decode = _string.decode
191
- if (!isNil(_string.string)) string = _string.string
192
- }
193
- let tx = await this.mem.get("txs", id)
194
- let _data = tx?.data ?? null
195
- if (tx?.format === 2 && _data) {
196
- _data = Buffer.from(_data, "base64")
197
- } else if (_data) {
198
- _data = Buffer.from(base64url.decode(_data))
199
- }
200
- let isBuf = is(Uint8Array, _data) || is(ArrayBuffer, _data)
201
- let isStr = is(String, _data)
202
- if (decode === false) {
203
- if (isStr) _data = new TextEncoder().encode(_data)
204
- return base64url.encode(_data)
205
- } else {
206
- if (isBuf && string) {
207
- return _data.toString()
208
- } else if (isStr && string !== true) {
209
- return new TextEncoder().encode(_data)
210
- }
211
- }
212
- return _data
6
+ super({ ...opt, ArMem })
213
7
  }
214
8
  }
215
9
 
package/esm/wao.js CHANGED
@@ -1,127 +1,10 @@
1
- import MAO from "./ao.js"
2
- import { createDataItemSigner } from "@permaweb/aoconnect"
3
- import { srcs, buildTags } from "./utils.js"
4
- import AR from "./war.js"
1
+ import BAO from "./bao.js"
5
2
  import { connect } from "./aoconnect-web.js"
6
- import { acc, mu } from "./web.js"
7
- import { mergeLeft, is, map } from "ramda"
8
-
9
- let log = `
10
- local json = require("json")
11
- ao = ao or {}
12
-
13
- function ao.log(...)
14
- local args = {...}
15
- local txt = json.encode(args)
16
- if type(ao.outbox.Output) == 'string' then
17
- ao.outbox.Output = {ao.outbox.Output}
18
- end
19
- table.insert(ao.outbox.Output, txt)
20
- ao.outbox.Logs = ao.outbox.Logs or {}
21
- table.insert(ao.outbox.Logs, txt)
22
- end
23
-
24
- function ao.result(result)
25
- if ao.outbox.Error or result.Error then
26
- return { Error = result.Error or ao.outbox.Error, Output = ao.outbox.Logs }
27
- end
28
- return {
29
- Output = ao.outbox.Logs or result.Output or ao.outbox.Output,
30
- Messages = ao.outbox.Messages,
31
- Spawns = ao.outbox.Spawns,
32
- Assignments = ao.outbox.Assignments,
33
- }
34
- end
35
- `
36
-
37
- const renderLogs = logs => {
38
- if (is(Array, logs) && logs.length > 0) {
39
- for (const v of logs) {
40
- let l = v
41
- try {
42
- const p = JSON.parse(v)
43
- if (JSON.stringify(p) === v) l = p
44
- console.log(...l)
45
- } catch (e) {
46
- console.log(l)
47
- }
48
- }
49
- }
50
- }
3
+ import AR from "./war.js"
51
4
 
52
- class AO extends MAO {
5
+ class AO extends BAO {
53
6
  constructor(opt = {}) {
54
- super({ ...opt, in_memory: true })
55
- this.in_memory = true
56
- const {
57
- modules,
58
- results,
59
- assign,
60
- result,
61
- message,
62
- spawn,
63
- dryrun,
64
- monitor,
65
- unmonitor,
66
- mem,
67
- } = connect(opt.mem, {
68
- extensions: opt.extensions,
69
- cache: opt.cache,
70
- reset: opt.reset,
71
- })
72
- this.assign = assign
73
- this.result = async (...opt) => {
74
- const res = await result(...opt)
75
- renderLogs(res.Output)
76
- return res
77
- }
78
-
79
- this.results = results
80
- this.message = message
81
- this.spawn = async (...opt) => {
82
- const res = await spawn(...opt)
83
- await this.load({ data: log, pid: res })
84
- return res
85
- }
86
- this.dryrun = async (...opt) => {
87
- const res = await dryrun(...opt)
88
- renderLogs(res.Output)
89
- return res
90
- }
91
- this.monitor = monitor
92
- this.unmonitor = unmonitor
93
- this.mem = mem
94
- this.ar = new AR({ mem })
95
- }
96
-
97
- async init(jwk) {
98
- if (!jwk && this.acc[0]) jwk = this.acc[0].jwk
99
- await this.ar.init(jwk)
100
- return this
101
- }
102
-
103
- async postModule({ data, tags = {}, jwk }) {
104
- let err = null
105
- ;({ jwk, err } = await this.ar.checkWallet({ jwk }))
106
- if (err) return { err }
107
- const t = mergeLeft(tags, {
108
- "Data-Protocol": "ao",
109
- Variant: "ao.TN.1",
110
- Type: "Module",
111
- "Module-Format": "wasm64-unknown-emscripten-draft_2024_02_15",
112
- "Input-Encoding": "JSON-V1",
113
- "Output-Encoding": "JSON-V1",
114
- "Memory-Limit": "1-gb",
115
- "Compute-Limit": "9000000000000",
116
- })
117
- const signer = createDataItemSigner(jwk)
118
- const { id, owner, item } = await this.ar.dataitem({
119
- tags: t,
120
- data,
121
- signer,
122
- })
123
- await this.ar.postItems(item, jwk)
124
- return { id }
7
+ super({ ...opt, connect, AR })
125
8
  }
126
9
  }
127
10