wao 0.3.1 → 0.4.0

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/ar.js CHANGED
@@ -13,9 +13,9 @@ class AR {
13
13
  _arweave.protocol = isLocalhost(_arweave.host) ? "http" : "https"
14
14
  if (!_arweave.port) _arweave.port = isLocalhost(_arweave.host) ? 1984 : 443
15
15
  this.port = _arweave.port
16
- this.arweave = Arweave.init(_arweave)
17
16
  this.host = _arweave.host
18
17
  this.protocol = _arweave.protocol
18
+ this.arweave = Arweave.init(_arweave)
19
19
  }
20
20
 
21
21
  isArConnect(jwk) {
package/esm/armem.js ADDED
@@ -0,0 +1,38 @@
1
+ import Arweave from "arweave"
2
+ import { last } from "ramda"
3
+
4
+ export default class ArMem {
5
+ constructor() {
6
+ this.__type__ = "mem"
7
+ this.arweave = Arweave.init()
8
+ this.arweave.transactions.getTransactionAnchor = () => {
9
+ return this.blocks.length === 0 ? "" : last(this.blocks)
10
+ }
11
+ this.arweave.transactions.getPrice = () => 0
12
+ this.txs = {}
13
+ this.height = 0
14
+ this.blocks = []
15
+ this.env = {}
16
+ this.modules = {
17
+ aos2_0_1: "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM",
18
+ aos1: "cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo",
19
+ sqlite: "ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw",
20
+ }
21
+ this.modmap = {}
22
+ this.msgs = {}
23
+ this.wasms = {
24
+ "Do_Uc2Sju_ffp6Ev0AnLVdPtot15rvMjP-a9VVaA5fM": {
25
+ file: "aos2_0_1",
26
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
27
+ },
28
+ cNlipBptaF9JeFAf4wUmpi43EojNanIBos3EfNrEOWo: {
29
+ file: "aos_1",
30
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
31
+ },
32
+ ghSkge2sIUD_F00ym5sEimC63BDBuBrq4b5OcwxOjiw: {
33
+ file: "sqlite",
34
+ format: "wasm64-unknown-emscripten-draft_2024_02_15",
35
+ },
36
+ }
37
+ }
38
+ }
package/esm/gql.js ADDED
@@ -0,0 +1,218 @@
1
+ import { includes, map, is, isNil, last, clone } from "ramda"
2
+ const full = `id anchor signature recipient owner { address key } fee { winston ar } quantity { winston ar } data { size type } tags { name value } block { id timestamp height previous } parent { id }`
3
+ const full_blocks = `id timestamp height previous`
4
+
5
+ const subs = {
6
+ owner: ["address", "key"],
7
+ fee: ["winston", "ar"],
8
+ quantity: ["winston", "ar"],
9
+ data: ["size", "type"],
10
+ tags: ["name", "value"],
11
+ block: ["id", "timestamp", "height", "previous"],
12
+ parent: ["id"],
13
+ }
14
+ const field = (key, val = true) => {
15
+ if (includes(key, ["id", "anchor", "signature", "recipient"])) {
16
+ return key
17
+ } else if (subs[key]) {
18
+ let _subs = []
19
+ if (val === true) val = subs[key]
20
+ else if (is(Object, val) && !is(Array, val)) {
21
+ let _val = []
22
+ let isTrue = false
23
+ let isFalse = false
24
+ for (const k in val)
25
+ if (val[k] === true) isTrue = true
26
+ else if (val[k] === false) isFalse = true
27
+ if (!isTrue && isFalse) {
28
+ for (const k of subs[key]) if (val[k] !== false) _val.push(k)
29
+ } else {
30
+ for (const k in val) if (val[k] === true) _val.push(k)
31
+ }
32
+ val = _val
33
+ } else if (!is(Array, val)) val = subs[key]
34
+ for (const v2 of val) {
35
+ if (is(String, v2) && includes(v2, subs[key])) _subs.push(v2)
36
+ }
37
+ if (_subs.length === 0) return null
38
+ return `${key} { ${_subs.join(" ")} }`
39
+ }
40
+ return null
41
+ }
42
+
43
+ const field_blocks = (key, val = true) => {
44
+ if (includes(key, ["id", "timestamp", "height", "previous"])) {
45
+ return key
46
+ }
47
+ return null
48
+ }
49
+
50
+ const query = (opt = {}) => {
51
+ let cond = []
52
+ let tags = []
53
+ for (const k in opt.tags ?? {}) {
54
+ if (is(String, opt.tags[k])) {
55
+ tags.push(`{ name: "${k}", values: ["${opt.tags[k]}"] }`)
56
+ } else if (is(Array, opt.tags[k])) {
57
+ tags.push(
58
+ `{ name: "${k}", values: [${map(v => `"${v}"`, opt.tags[k]).join(", ")}] }`,
59
+ )
60
+ }
61
+ }
62
+ if (tags.length > 0) cond.push(`tags: [${tags.join(", ")}]`)
63
+ let recipients = null
64
+ if (is(Array, opt.recipients)) recipients = opt.recipients
65
+ else if (is(String, opt.recipient)) recipients = [opt.recipient]
66
+ if (!isNil(recipients) && recipients.length > 0) {
67
+ cond.push(`recipients: [${map(v => `"${v}"`, recipients).join(", ")}]`)
68
+ }
69
+
70
+ let owners = null
71
+ if (is(Array, opt.owners)) owners = opt.owners
72
+ else if (is(String, opt.owner)) owners = [opt.owner]
73
+ if (!isNil(owners) && owners.length > 0) {
74
+ cond.push(`owners: [${map(v => `"${v}"`, owners).join(", ")}]`)
75
+ }
76
+
77
+ let ids = null
78
+ if (is(Array, opt.ids)) ids = opt.ids
79
+ else if (is(String, opt.id)) ids = [opt.id]
80
+ if (!isNil(ids) && ids.length > 0) {
81
+ cond.push(`ids: [${map(v => `"${v}"`, ids).join(", ")}]`)
82
+ }
83
+
84
+ let _block = opt.block
85
+ if (is(Number, opt.block)) {
86
+ _block = { min: opt.block, max: opt.block }
87
+ } else if (is(Array, opt.block) && opt.block.length > 0) {
88
+ _block = {}
89
+ if (!isNil(opt.block[0])) _block.min = opt.block[0]
90
+ if (!isNil(opt.block[1])) _block.max = opt.block[1]
91
+ }
92
+ if (!isNil(_block?.max) || !isNil(_block?.min)) {
93
+ let block = []
94
+ if (!isNil(_block.min)) block.push(`min: ${_block.min}`)
95
+ if (!isNil(_block.max)) block.push(`max: ${_block.max}`)
96
+ if (block.length > 0) cond.push(`block : {${block.join(", ")}}`)
97
+ }
98
+ if (opt.first) cond.push(`first: ${opt.first}`)
99
+ if (opt.after) cond.push(`after: "${opt.after}"`)
100
+ if (opt.asc) cond.push(`sort: HEIGHT_ASC`)
101
+
102
+ let _cond = ""
103
+ if (cond.length > 0) _cond = `(${cond.join(", ")})`
104
+ let fields = []
105
+ if (is(Array, opt.fields) && opt.fields.length > 0) {
106
+ for (const v of opt.fields) {
107
+ if (is(String, v)) {
108
+ const fld = field(v)
109
+ if (fld) fields.push(fld)
110
+ } else if (is(Object, v) && !is(Array, v)) {
111
+ for (const k in v) {
112
+ const fld = field(k, v[k])
113
+ if (fld) fields.push(fld)
114
+ }
115
+ }
116
+ }
117
+ } else if (is(Object, opt.fields) && !is(Array, opt.fields)) {
118
+ for (const k in opt.fields) {
119
+ const fld = field(k, opt.fields[k])
120
+ if (fld) fields.push(fld)
121
+ }
122
+ }
123
+ let _fields = fields.length > 0 ? fields.join(" ") : full
124
+ return `query {
125
+ transactions ${_cond}{
126
+ edges { cursor node { ${_fields} } }
127
+ }
128
+ }`
129
+ }
130
+
131
+ const query_blocks = (opt = {}) => {
132
+ let cond = []
133
+
134
+ let _height = opt.height
135
+ if (is(Number, opt.height)) {
136
+ _height = { min: opt.height, max: opt.height }
137
+ } else if (is(Array, opt.height) && opt.height.length > 0) {
138
+ _height = {}
139
+ if (!isNil(opt.height[0])) _height.min = opt.height[0]
140
+ if (!isNil(opt.height[1])) _height.max = opt.height[1]
141
+ }
142
+ if (!isNil(_height?.max) || !isNil(_height?.min)) {
143
+ let height = []
144
+ if (!isNil(_height.min)) height.push(`min: ${_height.min}`)
145
+ if (!isNil(_height.max)) height.push(`max: ${_height.max}`)
146
+ if (height.length > 0) cond.push(`height : {${height.join(", ")}}`)
147
+ }
148
+
149
+ if (opt.first) cond.push(`first: ${opt.first}`)
150
+ if (opt.after) cond.push(`after: "${opt.after}"`)
151
+ if (opt.asc) cond.push(`sort: HEIGHT_ASC`)
152
+
153
+ let _cond = ""
154
+ if (cond.length > 0) _cond = `(${cond.join(", ")})`
155
+ let fields = []
156
+ if (is(Array, opt.fields) && opt.fields.length > 0) {
157
+ let fields = []
158
+ for (const v of opt.fields) {
159
+ if (is(String, v)) {
160
+ const fld = field_blocks(v)
161
+ if (fld) fields.push(fld)
162
+ } else if (is(Object, v) && !is(Array, v)) {
163
+ for (const k in v) {
164
+ const fld = field_blocks(k, v[k])
165
+ if (fld) fields.push(fld)
166
+ }
167
+ }
168
+ }
169
+ } else if (is(Object, opt.fields) && !is(Array, opt.fields)) {
170
+ for (const k in opt.fields) {
171
+ const fld = field_blocks(k, opt.fields[k])
172
+ if (fld) fields.push(fld)
173
+ }
174
+ }
175
+ let _fields = fields.length > 0 ? fields.join(" ") : full_blocks
176
+ return `query {
177
+ blocks ${_cond}{
178
+ edges { cursor node { ${_fields} } }
179
+ }
180
+ }`
181
+ }
182
+
183
+ export default class GQL {
184
+ constructor({ url = "https://arweave.net/graphql" }) {
185
+ this.url = url
186
+ }
187
+ async _fetch(query, tar) {
188
+ const json = await fetch(this.url, {
189
+ method: "POST",
190
+ headers: { "Content-Type": "application/json" },
191
+ body: JSON.stringify({ query }),
192
+ }).then(r => r.json())
193
+ return map(v => ({ cursor: v.cursor, ...v.node }))(json.data[tar].edges)
194
+ }
195
+ async fetch(opt = {}, query, tar = "transactions") {
196
+ const data = await this._fetch(query, tar)
197
+ if (opt.next === true) {
198
+ let cursor = null
199
+ if (data.length > 0) cursor = last(data).cursor
200
+ const next = !cursor
201
+ ? null
202
+ : async () => {
203
+ let _opt = clone(opt)
204
+ _opt.after = cursor
205
+ return await this.txs(_opt)
206
+ }
207
+ return { data, next }
208
+ } else {
209
+ return data
210
+ }
211
+ }
212
+ async txs(opt = {}) {
213
+ return await this.fetch(opt, query(opt))
214
+ }
215
+ async blocks(opt = {}) {
216
+ return await this.fetch(opt, query_blocks(opt), "blocks")
217
+ }
218
+ }
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import AR from "./ar.js"
2
2
  import AO from "./ao.js"
3
+ import GQL from "./gql.js"
3
4
 
4
- export { AO, AR }
5
+ export { AO, AR, GQL }
package/esm/tao.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import MAO from "./ao.js"
2
2
  import { srcs } from "./utils.js"
3
- import AR from "./ar.js"
3
+ import AR from "./tar.js"
4
4
  import { connect } from "./aoconnect.js"
5
-
5
+ import { acc } from "./test.js"
6
6
  class AO extends MAO {
7
7
  constructor(opt = {}) {
8
8
  super({ ...opt, in_memory: true })
9
9
  this.in_memory = true
10
10
  const {
11
- accounts,
12
11
  modules,
13
12
  results,
14
13
  assign,
@@ -18,23 +17,22 @@ class AO extends MAO {
18
17
  dryrun,
19
18
  monitor,
20
19
  unmonitor,
21
- txs,
22
- } = connect()
23
- this.module = modules.aos2_0_1
20
+ mem,
21
+ } = connect(opt.mem)
22
+ this.module = mem.modules.aos2_0_1
24
23
  this.assign = assign
25
24
  this.result = result
26
25
  this.results = results
27
26
  this.message = message
28
27
  this.spawn = spawn
29
28
  this.dryrun = dryrun
30
- this.ar.txs = txs
31
29
  this.monitor = monitor
32
30
  this.unmonitor = unmonitor
33
- this.accounts = accounts
31
+ this.ar = new AR({ mem })
34
32
  }
35
33
 
36
34
  async init(jwk) {
37
- if (!jwk && this.accounts?.[0]) jwk = this.accounts[0].jwk
35
+ if (!jwk && this.acc[0]) jwk = this.acc[0].jwk
38
36
  await this.ar.init(jwk)
39
37
  return this
40
38
  }
package/esm/tar.js ADDED
@@ -0,0 +1,98 @@
1
+ import MAR from "./ar.js"
2
+ import { buildTags } from "./utils.js"
3
+ import { DataItem } from "warp-arbundles"
4
+ import base64url from "base64url"
5
+ import ArMem from "./armem.js"
6
+
7
+ class AR extends MAR {
8
+ constructor(opt = {}) {
9
+ super({ ...opt, in_memory: true })
10
+ this.in_memory = true
11
+ this.mem = opt.mem ?? new ArMem()
12
+ }
13
+
14
+ async txs(pid) {
15
+ let _txs = []
16
+ for (let v of this.mem.env[pid].txs) {
17
+ _txs.push({ tags: v.tags, id: v.id })
18
+ }
19
+ return _txs
20
+ }
21
+
22
+ async dataitem({ data = "1984", tags = {}, signer }) {
23
+ const _tags = buildTags(tags)
24
+ const item = await signer({ data, tags: _tags })
25
+ const di = new DataItem(item.raw)
26
+ const raw_owner = di.rawOwner
27
+ const hashBuffer = Buffer.from(
28
+ await crypto.subtle.digest("SHA-256", raw_owner),
29
+ )
30
+ const owner = base64url.encode(hashBuffer)
31
+ return { id: item.id, owner, item }
32
+ }
33
+
34
+ async post({ data = "1984", tags = {}, jwk }) {
35
+ let tx = await this.arweave.createTransaction({ data: data })
36
+ let _tags = buildTags(null, tags)
37
+ for (const v of _tags) tx.addTag(v.name, v.value)
38
+ this.mem.height += 1
39
+ return await this.postTx(tx, jwk, {
40
+ height: this.mem.height,
41
+ tags: _tags,
42
+ data,
43
+ })
44
+ }
45
+
46
+ async postItem(item, jwk) {
47
+ const tx = await this.mem.arweave.createTransaction({
48
+ data: item.raw,
49
+ })
50
+ tx.addTag("Bundle-Format", "binary")
51
+ tx.addTag("Bundle-Version", "2.0.0")
52
+ const di = new DataItem(item.raw)
53
+ const rowner = di.rawOwner
54
+ const hashBuffer = Buffer.from(
55
+ await crypto.subtle.digest("SHA-256", rowner),
56
+ )
57
+ const owner = base64url.encode(hashBuffer)
58
+ this.mem.height += 1
59
+ let data = di.data
60
+ try {
61
+ data = base64url.decode(di.data)
62
+ } catch (e) {}
63
+ this.mem.txs[item.id] = {
64
+ id: item.id,
65
+ item: di,
66
+ owner,
67
+ height: this.mem.height,
68
+ tags: di.tags,
69
+ data,
70
+ }
71
+ this.mem.blocks.push(item.id)
72
+ return await this.postTx(tx, jwk)
73
+ }
74
+
75
+ async postTx(tx, jwk, item) {
76
+ let [res, err] = [null, null]
77
+ await this.mem.arweave.transactions.sign(tx, jwk)
78
+ if (item) {
79
+ if (!item.id) item.id = tx.id
80
+ this.mem.txs[item.id] = item
81
+ this.mem.blocks.push(item.id)
82
+ }
83
+ res = { id: tx.id, status: 200, statusText: "200" }
84
+ //res = await this.arweave.transactions.post(tx)
85
+ //if (res.status !== 200) err = res.statusText
86
+ return { res, err, id: tx.id }
87
+ }
88
+
89
+ tx(id) {
90
+ return this.mem.txs[id]
91
+ }
92
+
93
+ data(id) {
94
+ return this.mem.txs[id].data
95
+ }
96
+ }
97
+
98
+ export default AR
package/esm/test.js CHANGED
@@ -1,3 +1,14 @@
1
- import { connect, acc, mu } from "./aoconnect.js"
1
+ import { readFileSync } from "fs"
2
+ import { resolve } from "path"
3
+ import { acc, mu, su, cu } from "./accounts.js"
4
+ import { connect } from "./aoconnect.js"
2
5
  import AO from "./tao.js"
3
- export { AO, connect, acc, mu }
6
+ import AR from "./tar.js"
7
+ import { dirname } from "./utils.js"
8
+
9
+ const blueprint = async pkg => {
10
+ return readFileSync(resolve(await dirname(), `lua/${pkg}.lua`), "utf8")
11
+ }
12
+ const scheduler = "GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA"
13
+
14
+ export { AO, AR, connect, acc, mu, su, cu, blueprint, scheduler }
package/esm/utils.js CHANGED
@@ -211,6 +211,10 @@ const srcs = {
211
211
 
212
212
  const buildTags = (act, tags) => {
213
213
  let _tags = []
214
+ if (isNil(tags) && typeof act !== "string") {
215
+ tags = act
216
+ act = null
217
+ }
214
218
  if (act) _tags.push(action(act))
215
219
  for (const k in tags) {
216
220
  if (is(Array)(tags[k])) for (const v of tags[k]) _tags.push(tag(k, v))
@@ -271,6 +275,11 @@ const isCheckComplete = (checks, check) => {
271
275
  return true
272
276
  }
273
277
 
278
+ const dirname = async () =>
279
+ typeof __dirname != "undefined"
280
+ ? __dirname
281
+ : (await import("./dirname.js")).default
282
+
274
283
  function isJSON(obj) {
275
284
  if (obj === null || obj === undefined) return false
276
285
  if (
@@ -321,4 +330,5 @@ export {
321
330
  isLocalhost,
322
331
  udl,
323
332
  isJSON,
333
+ dirname,
324
334
  }