@useopentab/cli 0.0.0-stage → 0.1.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.
@@ -0,0 +1,360 @@
1
+ import {
2
+ CIS8004,
3
+ InvalidInputError,
4
+ RegistryParseError,
5
+ TESTNET_CAIP2
6
+ } from "./chunk-S5IAH7CT.js";
7
+
8
+ // packages/registry/src/base58.ts
9
+ import { sha256 } from "@noble/hashes/sha2.js";
10
+
11
+ // packages/registry/src/bytes.ts
12
+ var HEX_RE = /^(?:[0-9a-fA-F]{2})*$/;
13
+ var UTF8_STRICT = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
14
+ var UTF8_ENCODER = new TextEncoder();
15
+ var U64_MAX = (1n << 64n) - 1n;
16
+ function toHex(bytes) {
17
+ let out = "";
18
+ for (const b of bytes) out += b.toString(16).padStart(2, "0");
19
+ return out;
20
+ }
21
+ function fromHex(hex, what = "hex") {
22
+ const clean = hex.startsWith("0x") || hex.startsWith("0X") ? hex.slice(2) : hex;
23
+ if (!HEX_RE.test(clean)) throw new InvalidInputError(`${what} is not valid hex`);
24
+ const out = new Uint8Array(clean.length / 2);
25
+ for (let i = 0; i < out.length; i++) out[i] = Number.parseInt(clean.slice(2 * i, 2 * i + 2), 16);
26
+ return out;
27
+ }
28
+ function bytesEqual(a, b) {
29
+ if (a.length !== b.length) return false;
30
+ let diff = 0;
31
+ for (let i = 0; i < a.length; i++) diff |= (a[i] ?? 0) ^ (b[i] ?? 0);
32
+ return diff === 0;
33
+ }
34
+ function concatBytes(...parts) {
35
+ let len = 0;
36
+ for (const p of parts) len += p.length;
37
+ const out = new Uint8Array(len);
38
+ let off = 0;
39
+ for (const p of parts) {
40
+ out.set(p, off);
41
+ off += p.length;
42
+ }
43
+ return out;
44
+ }
45
+ function utf8(s) {
46
+ return UTF8_ENCODER.encode(s);
47
+ }
48
+ function toU64(value, what = "value") {
49
+ let v;
50
+ if (typeof value === "bigint") v = value;
51
+ else if (typeof value === "number") {
52
+ if (!Number.isSafeInteger(value)) throw new InvalidInputError(`${what} must be a safe integer`);
53
+ v = BigInt(value);
54
+ } else {
55
+ if (!/^(?:0|[1-9][0-9]*)$/.test(value)) throw new InvalidInputError(`${what} must be a decimal u64`);
56
+ v = BigInt(value);
57
+ }
58
+ if (v < 0n || v > U64_MAX) throw new InvalidInputError(`${what} is out of u64 range`);
59
+ return v;
60
+ }
61
+ var ByteWriter = class {
62
+ #parts = [];
63
+ u8(v) {
64
+ if (!Number.isInteger(v) || v < 0 || v > 255) throw new InvalidInputError("u8 out of range");
65
+ this.#parts.push(Uint8Array.of(v));
66
+ return this;
67
+ }
68
+ u32(v) {
69
+ if (!Number.isInteger(v) || v < 0 || v > 4294967295) throw new InvalidInputError("u32 out of range");
70
+ const b = new Uint8Array(4);
71
+ new DataView(b.buffer).setUint32(0, v, true);
72
+ this.#parts.push(b);
73
+ return this;
74
+ }
75
+ u64(v) {
76
+ if (v < 0n || v > U64_MAX) throw new InvalidInputError("u64 out of range");
77
+ const b = new Uint8Array(8);
78
+ new DataView(b.buffer).setBigUint64(0, v, true);
79
+ this.#parts.push(b);
80
+ return this;
81
+ }
82
+ raw(bytes) {
83
+ this.#parts.push(Uint8Array.from(bytes));
84
+ return this;
85
+ }
86
+ /** `u32` byte length, then UTF-8 bytes. */
87
+ string(s) {
88
+ const b = utf8(s);
89
+ return this.u32(b.length).raw(b);
90
+ }
91
+ /** `u32` length, then raw bytes. */
92
+ bytestring(bytes) {
93
+ return this.u32(bytes.length).raw(bytes);
94
+ }
95
+ finish() {
96
+ return concatBytes(...this.#parts);
97
+ }
98
+ };
99
+ var ByteReader = class {
100
+ #buf;
101
+ #view;
102
+ #what;
103
+ #pos = 0;
104
+ constructor(buf, what) {
105
+ this.#buf = buf;
106
+ this.#view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
107
+ this.#what = what;
108
+ }
109
+ get offset() {
110
+ return this.#pos;
111
+ }
112
+ get remaining() {
113
+ return this.#buf.length - this.#pos;
114
+ }
115
+ fail(message, at = this.#pos) {
116
+ throw new RegistryParseError(`${this.#what}: ${message}`, at);
117
+ }
118
+ #need(n, field) {
119
+ if (n > this.remaining) this.fail(`${field}: needs ${n} bytes, ${this.remaining} left`);
120
+ }
121
+ u8(field) {
122
+ this.#need(1, field);
123
+ return this.#buf[this.#pos++];
124
+ }
125
+ u16(field) {
126
+ this.#need(2, field);
127
+ const v = this.#view.getUint16(this.#pos, true);
128
+ this.#pos += 2;
129
+ return v;
130
+ }
131
+ u32(field) {
132
+ this.#need(4, field);
133
+ const v = this.#view.getUint32(this.#pos, true);
134
+ this.#pos += 4;
135
+ return v;
136
+ }
137
+ u64(field) {
138
+ this.#need(8, field);
139
+ const v = this.#view.getBigUint64(this.#pos, true);
140
+ this.#pos += 8;
141
+ return v;
142
+ }
143
+ bytes(n, field) {
144
+ this.#need(n, field);
145
+ const out = this.#buf.slice(this.#pos, this.#pos + n);
146
+ this.#pos += n;
147
+ return out;
148
+ }
149
+ /** `u32` length then that many bytes; the length is checked against what is left first. */
150
+ bytestring(field) {
151
+ const at = this.#pos;
152
+ const n = this.u32(`${field} length`);
153
+ if (n > this.remaining) this.fail(`${field}: length ${n} exceeds the ${this.remaining} bytes left`, at);
154
+ return this.bytes(n, field);
155
+ }
156
+ /** `u32` length then UTF-8; invalid UTF-8 is rejected. */
157
+ string(field) {
158
+ const at = this.#pos;
159
+ const raw = this.bytestring(field);
160
+ try {
161
+ return UTF8_STRICT.decode(raw);
162
+ } catch {
163
+ return this.fail(`${field}: invalid UTF-8`, at);
164
+ }
165
+ }
166
+ /** A 0/1 option tag followed by the value when present. Any other tag is rejected. */
167
+ option(field, read) {
168
+ const at = this.#pos;
169
+ const tag = this.u8(`${field} tag`);
170
+ if (tag === 0) return null;
171
+ if (tag === 1) return read();
172
+ return this.fail(`${field}: unknown option tag ${tag}`, at);
173
+ }
174
+ /** Reject trailing bytes. */
175
+ end() {
176
+ if (this.remaining !== 0) this.fail(`${this.remaining} trailing byte(s)`);
177
+ }
178
+ };
179
+
180
+ // packages/registry/src/base58.ts
181
+ var ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
182
+ var INDEX = new Map([...ALPHABET].map((c, i) => [c, i]));
183
+ var MAX_INPUT = 128;
184
+ function base58Encode(bytes) {
185
+ let zeros = 0;
186
+ while (zeros < bytes.length && bytes[zeros] === 0) zeros++;
187
+ const digits = [];
188
+ for (let i = zeros; i < bytes.length; i++) {
189
+ let carry = bytes[i];
190
+ for (let j = 0; j < digits.length; j++) {
191
+ carry += digits[j] << 8;
192
+ digits[j] = carry % 58;
193
+ carry = Math.floor(carry / 58);
194
+ }
195
+ while (carry > 0) {
196
+ digits.push(carry % 58);
197
+ carry = Math.floor(carry / 58);
198
+ }
199
+ }
200
+ let out = "1".repeat(zeros);
201
+ for (let i = digits.length - 1; i >= 0; i--) out += ALPHABET[digits[i]];
202
+ return out;
203
+ }
204
+ function base58Decode(s) {
205
+ if (s.length === 0 || s.length > MAX_INPUT) throw new InvalidInputError("base58: bad length");
206
+ let zeros = 0;
207
+ while (zeros < s.length && s[zeros] === "1") zeros++;
208
+ const bytes = [];
209
+ for (let i = zeros; i < s.length; i++) {
210
+ const v = INDEX.get(s[i]);
211
+ if (v === void 0) throw new InvalidInputError("base58: invalid character");
212
+ let carry = v;
213
+ for (let j = 0; j < bytes.length; j++) {
214
+ carry += bytes[j] * 58;
215
+ bytes[j] = carry & 255;
216
+ carry >>= 8;
217
+ }
218
+ while (carry > 0) {
219
+ bytes.push(carry & 255);
220
+ carry >>= 8;
221
+ }
222
+ }
223
+ const out = new Uint8Array(zeros + bytes.length);
224
+ for (let i = 0; i < bytes.length; i++) out[out.length - 1 - i] = bytes[i];
225
+ return out;
226
+ }
227
+ function checksum(payload) {
228
+ return sha256(sha256(payload)).slice(0, 4);
229
+ }
230
+ function base58CheckEncode(version, payload) {
231
+ const body = concatBytes(Uint8Array.of(version), payload);
232
+ return base58Encode(concatBytes(body, checksum(body)));
233
+ }
234
+ function base58CheckDecode(s, version) {
235
+ const raw = base58Decode(s);
236
+ if (raw.length < 5) throw new InvalidInputError("base58check: too short");
237
+ const body = raw.slice(0, raw.length - 4);
238
+ if (!bytesEqual(checksum(body), raw.slice(raw.length - 4))) {
239
+ throw new InvalidInputError("base58check: bad checksum");
240
+ }
241
+ if (body[0] !== version) throw new InvalidInputError(`base58check: version byte ${body[0]}, expected ${version}`);
242
+ return body.slice(1);
243
+ }
244
+
245
+ // packages/registry/src/address.ts
246
+ var ACCOUNT_VERSION = 1;
247
+ var TOKEN_ADDRESS_VERSION = 2;
248
+ function accountToBase58(raw) {
249
+ if (raw.length !== 32) throw new InvalidInputError("account address must be 32 bytes");
250
+ return base58CheckEncode(ACCOUNT_VERSION, raw);
251
+ }
252
+ function accountFromBase58(address) {
253
+ const raw = base58CheckDecode(address, ACCOUNT_VERSION);
254
+ if (raw.length !== 32) throw new InvalidInputError("account address must decode to 32 bytes");
255
+ return raw;
256
+ }
257
+ function isAccountAddress(s) {
258
+ try {
259
+ accountFromBase58(s);
260
+ return true;
261
+ } catch {
262
+ return false;
263
+ }
264
+ }
265
+ function isAccountAlias(a, b) {
266
+ const x = accountFromBase58(a);
267
+ const y = accountFromBase58(b);
268
+ for (let i = 0; i < 29; i++) if (x[i] !== y[i]) return false;
269
+ return true;
270
+ }
271
+ function formatContractAddress(c) {
272
+ return `<${c.index},${c.subindex}>`;
273
+ }
274
+ function contractEquals(a, b) {
275
+ return a.index === b.index && a.subindex === b.subindex;
276
+ }
277
+ function ulebEncode(value) {
278
+ if (value < 0n || value > U64_MAX) throw new InvalidInputError("uleb128 value out of u64 range");
279
+ const out = [];
280
+ let v = value;
281
+ do {
282
+ let byte = Number(v & 0x7fn);
283
+ v >>= 7n;
284
+ if (v !== 0n) byte |= 128;
285
+ out.push(byte);
286
+ } while (v !== 0n);
287
+ return Uint8Array.from(out);
288
+ }
289
+ function ulebDecode(bytes, offset) {
290
+ let value = 0n;
291
+ let shift = 0n;
292
+ for (let i = offset; i < bytes.length && i < offset + 10; i++) {
293
+ const byte = bytes[i];
294
+ value |= BigInt(byte & 127) << shift;
295
+ if ((byte & 128) === 0) {
296
+ if (byte === 0 && i !== offset) throw new InvalidInputError("uleb128: overlong encoding");
297
+ if (value > U64_MAX) throw new InvalidInputError("uleb128: exceeds u64");
298
+ return { value, next: i + 1 };
299
+ }
300
+ shift += 7n;
301
+ }
302
+ throw new InvalidInputError("uleb128: truncated or too long");
303
+ }
304
+ function tokenIdU64Bytes(tokenId) {
305
+ return new ByteWriter().u64(toU64(tokenId, "token id")).finish();
306
+ }
307
+ function encodeTokenAddress(contract, tokenIdBytes) {
308
+ if (tokenIdBytes.length > 255) throw new InvalidInputError("token id longer than 255 bytes");
309
+ return base58CheckEncode(
310
+ TOKEN_ADDRESS_VERSION,
311
+ concatBytes(ulebEncode(contract.index), ulebEncode(contract.subindex), tokenIdBytes)
312
+ );
313
+ }
314
+ function decodeTokenAddress(s) {
315
+ const payload = base58CheckDecode(s, TOKEN_ADDRESS_VERSION);
316
+ const a = ulebDecode(payload, 0);
317
+ const b = ulebDecode(payload, a.next);
318
+ const id = payload.slice(b.next);
319
+ if (id.length > 255) throw new InvalidInputError("token id longer than 255 bytes");
320
+ const tokenId = id.length === 8 ? new DataView(id.buffer, id.byteOffset, 8).getBigUint64(0, true) : null;
321
+ return { contract: { index: a.value, subindex: b.value }, tokenIdHex: toHex(id), tokenId };
322
+ }
323
+ function agentBadge(tokenId, registry = CIS8004.address) {
324
+ return encodeTokenAddress(registry, tokenIdU64Bytes(tokenId));
325
+ }
326
+ function parseAgentBadge(badge, registry = CIS8004.address) {
327
+ const t = decodeTokenAddress(badge);
328
+ if (!contractEquals(t.contract, registry)) {
329
+ throw new InvalidInputError(`badge belongs to ${formatContractAddress(t.contract)}, not ${formatContractAddress(registry)}`);
330
+ }
331
+ if (t.tokenId === null) throw new InvalidInputError("badge token id is not a u64");
332
+ return t.tokenId;
333
+ }
334
+ function accountCaip10(address, chain = TESTNET_CAIP2) {
335
+ accountFromBase58(address);
336
+ return `${chain}:${address}`;
337
+ }
338
+ function contractCaip(c, chain = TESTNET_CAIP2) {
339
+ return `${chain}:${c.index}.${c.subindex}`;
340
+ }
341
+
342
+ export {
343
+ toHex,
344
+ fromHex,
345
+ bytesEqual,
346
+ concatBytes,
347
+ toU64,
348
+ ByteWriter,
349
+ ByteReader,
350
+ accountToBase58,
351
+ accountFromBase58,
352
+ isAccountAddress,
353
+ isAccountAlias,
354
+ formatContractAddress,
355
+ decodeTokenAddress,
356
+ agentBadge,
357
+ parseAgentBadge,
358
+ accountCaip10,
359
+ contractCaip
360
+ };