wao 0.26.2 → 0.27.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/hb.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { connect, createSigner } from "@permaweb/aoconnect"
2
2
  import { isEmpty, last, isNotNil, mergeLeft } from "ramda"
3
3
  import { toAddr, buildTags } from "./utils.js"
4
- import { send as _send, createRequest } from "./signer.js"
4
+ import { signer } from "./signer.js"
5
+ import { send as _send } from "./send.js"
5
6
  import hyper_aos from "./lua/hyper-aos.js"
6
7
  import aos_wamr from "./lua/aos_wamr.js"
7
8
 
@@ -73,7 +74,7 @@ class HB {
73
74
  _init(jwk) {
74
75
  this.signer = createSigner(jwk, this.url)
75
76
  this.addr = toAddr(jwk.n)
76
- this.sign = createRequest({ signer: this.signer, url: this.url })
77
+ this.sign = signer({ signer: this.signer, url: this.url })
77
78
 
78
79
  const { request } = connect({
79
80
  MODE: "mainnet",
@@ -87,7 +88,9 @@ class HB {
87
88
 
88
89
  async init(jwk) {
89
90
  this._init(jwk)
90
- this._info = await this.dev.meta.info({})
91
+ try {
92
+ this._info = await this.dev.meta.info({})
93
+ } catch (e) {}
91
94
  return this
92
95
  }
93
96
 
package/esm/hyperbeam.js CHANGED
@@ -7,7 +7,7 @@ export default class HyperBEAM {
7
7
  constructor({
8
8
  port = 10001,
9
9
  cu = 6363,
10
- as = ["genesis_wasm"],
10
+ as,
11
11
  bundler,
12
12
  gateway,
13
13
  wallet,
@@ -22,7 +22,9 @@ export default class HyperBEAM {
22
22
  store_prefix,
23
23
  operator,
24
24
  console = true,
25
+ shell = true,
25
26
  } = {}) {
27
+ as ??= shell ? [] : ["genesis_wasm"]
26
28
  this.console = console
27
29
  if (clearCache) {
28
30
  const dirname = resolve(process.cwd(), cwd)
@@ -53,11 +55,11 @@ export default class HyperBEAM {
53
55
  this.gateway = gateway
54
56
  this.wallet = wallet
55
57
  this.cwd = cwd
56
- this.run()
58
+ if (shell) this.shell()
57
59
  }
58
- run() {
60
+ shell() {
59
61
  const _as = this.as.length === 0 ? [] : ["as", this.as.join(", ")]
60
- this.hbeam = spawn(
62
+ this._shell = spawn(
61
63
  "rebar3",
62
64
  [
63
65
  ..._as,
@@ -71,16 +73,61 @@ export default class HyperBEAM {
71
73
  }
72
74
  )
73
75
  if (this.console) {
74
- this.hbeam.stdout.on("data", chunk => console.log(chunk.toString()))
75
- this.hbeam.stderr.on("data", err => console.error(err.toString()))
76
- this.hbeam.on("error", err =>
76
+ this._shell.stdout.on("data", chunk => console.log(chunk.toString()))
77
+ this._shell.stderr.on("data", err => console.error(err.toString()))
78
+ this._shell.on("error", err =>
77
79
  console.error(`failed to start process: ${err}`)
78
80
  )
79
- this.hbeam.on("close", code =>
81
+ this._shell.on("close", code => {
80
82
  console.log(`child process exited with code ${code}`)
81
- )
83
+ delete this._shell
84
+ })
82
85
  }
83
86
  }
87
+ eunit(module, test) {
88
+ return new Promise(res => {
89
+ let isTest = !isNil(test)
90
+ if (Array.isArray(module)) {
91
+ for (const v of module) {
92
+ if (Array.isArray(v) || /:/.test(v)) {
93
+ isTest = true
94
+ break
95
+ }
96
+ }
97
+ }
98
+ const _as = this.as.length === 0 ? [] : ["as", this.as.join(", ")]
99
+ const _test = Array.isArray(test) ? test.join("+") : test
100
+ let _module = ""
101
+
102
+ if (Array.isArray(module)) {
103
+ for (const v of module) {
104
+ _module += _module === "" ? "" : ","
105
+ if (Array.isArray(v)) _module += `${v[0]}:${v[1].join("+")}`
106
+ else _module += v
107
+ }
108
+ } else {
109
+ _module = test ? `${module}:${_test}` : module
110
+ }
111
+
112
+ const _arg = isTest ? "--test" : "--module"
113
+ let params = [..._as, "eunit", _arg, _module]
114
+ const _eunit = spawn("rebar3", params, {
115
+ env: { ...process.env, ...this.genEnv() },
116
+ cwd: resolve(process.cwd(), this.cwd),
117
+ })
118
+ if (this.console) {
119
+ _eunit.stdout.on("data", chunk => console.log(chunk.toString()))
120
+ _eunit.stderr.on("data", err => console.error(err.toString()))
121
+ _eunit.on("error", err =>
122
+ console.error(`failed to start process: ${err}`)
123
+ )
124
+ _eunit.on("close", code => {
125
+ console.log(`child process exited with code ${code}`)
126
+ res()
127
+ })
128
+ }
129
+ })
130
+ }
84
131
  async ok() {
85
132
  try {
86
133
  const address = await fetch(
@@ -111,6 +158,7 @@ export default class HyperBEAM {
111
158
  }
112
159
  genEnv() {
113
160
  let _env = {}
161
+ if (this.diagnostic) _env.DIAGNOSTIC = this.diagnostic
114
162
  if (this.c) {
115
163
  _env.CC = `gcc-${this.c}`
116
164
  _env.CXX = `g++-${this.c}`
@@ -168,6 +216,6 @@ export default class HyperBEAM {
168
216
  }
169
217
 
170
218
  kill() {
171
- this.hbeam.kill("SIGKILL")
219
+ this._shell.kill("SIGKILL")
172
220
  }
173
221
  }
package/esm/send.js ADDED
@@ -0,0 +1,126 @@
1
+ import base64url from "base64url"
2
+ import { httpbis } from "http-message-signatures"
3
+ import { parseItem, serializeList } from "structured-headers"
4
+ const {
5
+ augmentHeaders,
6
+ createSignatureBase,
7
+ createSigningParameters,
8
+ formatSignatureBase,
9
+ } = httpbis
10
+
11
+ export async function send(signedMsg, fetchImpl = fetch) {
12
+ const fetchOptions = {
13
+ method: signedMsg.method,
14
+ headers: signedMsg.headers,
15
+ redirect: "follow",
16
+ }
17
+ if (
18
+ signedMsg.body !== undefined &&
19
+ signedMsg.method !== "GET" &&
20
+ signedMsg.method !== "HEAD"
21
+ ) {
22
+ fetchOptions.body = signedMsg.body
23
+ }
24
+ const response = await fetchImpl(signedMsg.url, fetchOptions)
25
+
26
+ if (response.status >= 400) {
27
+ throw new Error(`${response.status}: ${await response.text()}`)
28
+ }
29
+
30
+ let headers = {}
31
+ if (response.headers && typeof response.headers.forEach === "function") {
32
+ response.headers.forEach((v, k) => (headers[k] = v))
33
+ } else headers = response.headers
34
+
35
+ return {
36
+ response,
37
+ headers,
38
+ body: await response.text(),
39
+ status: response.status,
40
+ }
41
+ }
42
+
43
+ const httpSigName = address => {
44
+ const decoded = base64url.toBuffer(address)
45
+ const hexString = [...decoded.subarray(1, 9)]
46
+ .map(byte => byte.toString(16).padStart(2, "0"))
47
+ .join("")
48
+ return `http-sig-${hexString}`
49
+ }
50
+
51
+ const toView = value => {
52
+ if (ArrayBuffer.isView(value)) {
53
+ return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
54
+ } else if (typeof value === "string") return base64url.toBuffer(value)
55
+
56
+ throw new Error(
57
+ "Value must be Uint8Array, ArrayBuffer, or base64url-encoded string"
58
+ )
59
+ }
60
+
61
+ export const toHttpSigner = signer => {
62
+ const params = ["alg", "keyid"].sort()
63
+ return async ({ request, fields }) => {
64
+ let signatureBase
65
+ let signatureInput
66
+ let createCalled = false
67
+
68
+ const create = injected => {
69
+ createCalled = true
70
+
71
+ const { publicKey, alg = "rsa-pss-sha512" } = injected
72
+
73
+ const publicKeyBuffer = toView(publicKey)
74
+
75
+ const signingParameters = createSigningParameters({
76
+ params,
77
+ paramValues: {
78
+ keyid: base64url.encode(publicKeyBuffer),
79
+ alg,
80
+ },
81
+ })
82
+
83
+ const signatureBaseArray = createSignatureBase({ fields }, request)
84
+ signatureInput = serializeList([
85
+ [
86
+ signatureBaseArray.map(([item]) => parseItem(item)),
87
+ signingParameters,
88
+ ],
89
+ ])
90
+
91
+ signatureBaseArray.push(['"@signature-params"', [signatureInput]])
92
+ signatureBase = formatSignatureBase(signatureBaseArray)
93
+
94
+ return new TextEncoder().encode(signatureBase)
95
+ }
96
+
97
+ const result = await signer(create, "httpsig")
98
+
99
+ if (!createCalled) {
100
+ throw new Error(
101
+ "create() must be invoked in order to construct the data to sign"
102
+ )
103
+ }
104
+
105
+ if (!result.signature || !result.address) {
106
+ throw new Error("Signer must return signature and address")
107
+ }
108
+
109
+ const signatureBuffer = toView(result.signature)
110
+ const signedHeaders = augmentHeaders(
111
+ request.headers,
112
+ signatureBuffer,
113
+ signatureInput,
114
+ httpSigName(result.address)
115
+ )
116
+
117
+ const finalHeaders = {}
118
+ for (const [key, value] of Object.entries(signedHeaders)) {
119
+ if (key === "Signature" || key === "Signature-Input") {
120
+ finalHeaders[key.toLowerCase()] = value
121
+ } else finalHeaders[key] = value
122
+ }
123
+
124
+ return { ...request, headers: finalHeaders }
125
+ }
126
+ }