wao 0.3.1 → 0.4.1

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