@seed-hypermedia/client 0.0.68 → 0.0.70
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/LICENSE +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +266 -0
- package/dist/auto-link.d.ts +3 -2
- package/dist/auto-link.mjs +21 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +170 -0
- package/dist/blobs.mjs +51 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.d.ts +7 -2
- package/dist/capability.mjs +14 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.d.ts +8 -7
- package/dist/change.mjs +24 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-37WRGUK4.mjs +125 -0
- package/dist/chunk-3KESF5XJ.mjs +297 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-4Q3SAL5R.mjs +75 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/chunk-BUPMCRR4.mjs +48 -0
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
- package/dist/chunk-DR3HJ6BV.mjs +25 -0
- package/dist/chunk-EU36RFR3.mjs +159 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-IT5LLTMP.mjs +209 -0
- package/dist/chunk-JT57MV7Y.mjs +136 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LVAHHQIK.mjs +41 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NYWCMQTH.mjs +412 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PJ2RFT5A.mjs +113 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-SHOAYNJY.mjs +289 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.mjs +15 -0
- package/dist/comment.d.ts +5 -4
- package/dist/comment.mjs +19 -0
- package/dist/contact.d.ts +5 -4
- package/dist/contact.mjs +15 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +53 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +132 -5453
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.d.ts +5 -4
- package/dist/ref.mjs +13 -0
- package/dist/resource-read.mjs +15 -0
- package/dist/signer.d.ts +42 -0
- package/dist/signer.mjs +9 -0
- package/dist/signing.d.ts +10 -2
- package/dist/signing.mjs +16 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +10 -6
- package/dist/vault.d.ts +14 -4
- package/dist/vault.mjs +6 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
unpackHmId
|
|
3
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/document-state.ts
|
|
6
|
+
async function resolveDocumentState(client, targetId) {
|
|
7
|
+
const unpacked = unpackHmId(targetId);
|
|
8
|
+
if (!unpacked) throw new Error(`Invalid ID: ${targetId}`);
|
|
9
|
+
const changesResp = await client.request("ListChanges", { targetId: unpacked });
|
|
10
|
+
if (!changesResp.changes || changesResp.changes.length === 0) {
|
|
11
|
+
throw new Error(`No changes found for ${targetId}. Document may not exist.`);
|
|
12
|
+
}
|
|
13
|
+
const depthMap = computeDepths(changesResp);
|
|
14
|
+
let genesis = "";
|
|
15
|
+
for (const change of changesResp.changes) {
|
|
16
|
+
if (!change.deps || change.deps.length === 0) {
|
|
17
|
+
genesis = change.id;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!genesis) {
|
|
22
|
+
throw new Error("Could not find genesis change (change with no deps).");
|
|
23
|
+
}
|
|
24
|
+
const allDeps = /* @__PURE__ */ new Set();
|
|
25
|
+
for (const change of changesResp.changes) {
|
|
26
|
+
if (change.deps) {
|
|
27
|
+
for (const dep of change.deps) {
|
|
28
|
+
allDeps.add(dep);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const heads = [];
|
|
33
|
+
for (const change of changesResp.changes) {
|
|
34
|
+
if (change.id && !allDeps.has(change.id)) {
|
|
35
|
+
heads.push(change.id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (heads.length === 0) {
|
|
39
|
+
throw new Error("Could not determine document heads.");
|
|
40
|
+
}
|
|
41
|
+
let maxHeadDepth = 0;
|
|
42
|
+
for (const head of heads) {
|
|
43
|
+
const d = depthMap.get(head) ?? 0;
|
|
44
|
+
if (d > maxHeadDepth) maxHeadDepth = d;
|
|
45
|
+
}
|
|
46
|
+
const version = heads.join(".");
|
|
47
|
+
return {
|
|
48
|
+
genesis,
|
|
49
|
+
heads,
|
|
50
|
+
headDepth: maxHeadDepth,
|
|
51
|
+
version
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function computeDepths(changesResp) {
|
|
55
|
+
const depthMap = /* @__PURE__ */ new Map();
|
|
56
|
+
const depsMap = /* @__PURE__ */ new Map();
|
|
57
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
58
|
+
for (const change of changesResp.changes) {
|
|
59
|
+
const id = change.id;
|
|
60
|
+
const deps = change.deps ?? [];
|
|
61
|
+
depsMap.set(id, deps);
|
|
62
|
+
for (const dep of deps) {
|
|
63
|
+
const existing = dependents.get(dep) ?? [];
|
|
64
|
+
existing.push(id);
|
|
65
|
+
dependents.set(dep, existing);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const queue = [];
|
|
69
|
+
for (const change of changesResp.changes) {
|
|
70
|
+
const deps = change.deps ?? [];
|
|
71
|
+
if (deps.length === 0) {
|
|
72
|
+
depthMap.set(change.id, 0);
|
|
73
|
+
queue.push(change.id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
while (queue.length > 0) {
|
|
77
|
+
const current = queue.shift();
|
|
78
|
+
const children = dependents.get(current) ?? [];
|
|
79
|
+
for (const child of children) {
|
|
80
|
+
const childDeps = depsMap.get(child) ?? [];
|
|
81
|
+
let allResolved = true;
|
|
82
|
+
let maxDepDepth = 0;
|
|
83
|
+
for (const dep of childDeps) {
|
|
84
|
+
const d = depthMap.get(dep);
|
|
85
|
+
if (d === void 0) {
|
|
86
|
+
allResolved = false;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
if (d > maxDepDepth) maxDepDepth = d;
|
|
90
|
+
}
|
|
91
|
+
if (allResolved && !depthMap.has(child)) {
|
|
92
|
+
depthMap.set(child, maxDepDepth + 1);
|
|
93
|
+
queue.push(child);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return depthMap;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export {
|
|
101
|
+
resolveDocumentState
|
|
102
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
toPrincipalSigner
|
|
3
|
+
} from "./chunk-DR3HJ6BV.mjs";
|
|
4
|
+
import {
|
|
5
|
+
code,
|
|
6
|
+
decode,
|
|
7
|
+
encode
|
|
8
|
+
} from "./chunk-3TPCJCVO.mjs";
|
|
9
|
+
|
|
10
|
+
// src/blobs.ts
|
|
11
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
12
|
+
import { sha256 as sha256hash } from "@noble/hashes/sha2.js";
|
|
13
|
+
import { base58btc } from "multiformats/bases/base58";
|
|
14
|
+
import { CID } from "multiformats/cid";
|
|
15
|
+
import * as Digest from "multiformats/hashes/digest";
|
|
16
|
+
import { sha256 as sha256hasher } from "multiformats/hashes/sha2";
|
|
17
|
+
var ED25519_VARINT_PREFIX = new Uint8Array([237, 1]);
|
|
18
|
+
var ED25519_SIGNATURE_SIZE = 64;
|
|
19
|
+
var ED25519_PUBLIC_KEY_SIZE = 32;
|
|
20
|
+
var ED25519_PRINCIPAL_SIZE = ED25519_VARINT_PREFIX.length + ED25519_PUBLIC_KEY_SIZE;
|
|
21
|
+
var NobleKeyPair = class {
|
|
22
|
+
principal;
|
|
23
|
+
publicKey;
|
|
24
|
+
/** Raw 32-byte Ed25519 private key seed. Needed for vault storage. */
|
|
25
|
+
seed;
|
|
26
|
+
constructor(seed) {
|
|
27
|
+
this.seed = new Uint8Array(seed);
|
|
28
|
+
this.publicKey = new Uint8Array(ed25519.getPublicKey(this.seed));
|
|
29
|
+
this.principal = principalFromEd25519(this.publicKey);
|
|
30
|
+
}
|
|
31
|
+
async sign(data) {
|
|
32
|
+
return new Uint8Array(ed25519.sign(data, this.seed));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function generateNobleKeyPair() {
|
|
36
|
+
return new NobleKeyPair(crypto.getRandomValues(new Uint8Array(32)));
|
|
37
|
+
}
|
|
38
|
+
function nobleKeyPairFromSeed(seed) {
|
|
39
|
+
return new NobleKeyPair(seed);
|
|
40
|
+
}
|
|
41
|
+
var WebCryptoKeyPair = class {
|
|
42
|
+
principal;
|
|
43
|
+
publicKey;
|
|
44
|
+
/** The Web Crypto key pair. Exposed for LocalWebIdentity compatibility. */
|
|
45
|
+
keyPair;
|
|
46
|
+
constructor(keyPair, publicKeyRaw) {
|
|
47
|
+
if (keyPair.privateKey.algorithm.name !== "Ed25519" || keyPair.publicKey.algorithm.name !== "Ed25519") {
|
|
48
|
+
throw new Error("WebCryptoKeyPair requires Ed25519 keys");
|
|
49
|
+
}
|
|
50
|
+
this.keyPair = keyPair;
|
|
51
|
+
this.publicKey = new Uint8Array(publicKeyRaw);
|
|
52
|
+
this.principal = principalFromEd25519(this.publicKey);
|
|
53
|
+
}
|
|
54
|
+
async sign(data) {
|
|
55
|
+
const sig = await crypto.subtle.sign("Ed25519", this.keyPair.privateKey, data);
|
|
56
|
+
return new Uint8Array(sig);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
async function generateWebCryptoKeyPair() {
|
|
60
|
+
const keyPair = await crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"]);
|
|
61
|
+
const publicKeyRaw = new Uint8Array(await crypto.subtle.exportKey("raw", keyPair.publicKey));
|
|
62
|
+
return new WebCryptoKeyPair(keyPair, publicKeyRaw);
|
|
63
|
+
}
|
|
64
|
+
function verify(blob) {
|
|
65
|
+
if (blob.signer[0] !== ED25519_VARINT_PREFIX[0] || blob.signer[1] !== ED25519_VARINT_PREFIX[1]) return false;
|
|
66
|
+
const rawPubKey = blob.signer.slice(ED25519_VARINT_PREFIX.length);
|
|
67
|
+
if (rawPubKey.length !== ED25519_PUBLIC_KEY_SIZE) return false;
|
|
68
|
+
const sigCopy = new Uint8Array(blob.sig);
|
|
69
|
+
const unsigned = { ...blob, sig: new Uint8Array(ED25519_SIGNATURE_SIZE) };
|
|
70
|
+
const data = new Uint8Array(encode(unsigned));
|
|
71
|
+
try {
|
|
72
|
+
return ed25519.verify(sigCopy, data, rawPubKey);
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function principalFromEd25519(rawPublicKey) {
|
|
78
|
+
const out = new Uint8Array(ED25519_VARINT_PREFIX.length + rawPublicKey.length);
|
|
79
|
+
out.set(ED25519_VARINT_PREFIX);
|
|
80
|
+
out.set(rawPublicKey, ED25519_VARINT_PREFIX.length);
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
function principalToString(p) {
|
|
84
|
+
return base58btc.encode(p);
|
|
85
|
+
}
|
|
86
|
+
function principalFromString(s) {
|
|
87
|
+
let decoded;
|
|
88
|
+
try {
|
|
89
|
+
decoded = base58btc.decode(s);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
92
|
+
throw new Error(`Invalid principal encoding: ${message}`);
|
|
93
|
+
}
|
|
94
|
+
if (decoded.length !== ED25519_PRINCIPAL_SIZE) {
|
|
95
|
+
throw new Error(`Invalid principal length: expected ${ED25519_PRINCIPAL_SIZE} bytes, got ${decoded.length}`);
|
|
96
|
+
}
|
|
97
|
+
if (decoded[0] !== ED25519_VARINT_PREFIX[0] || decoded[1] !== ED25519_VARINT_PREFIX[1]) {
|
|
98
|
+
throw new Error("Invalid principal multicodec: expected Ed25519 (0xed01)");
|
|
99
|
+
}
|
|
100
|
+
return decoded;
|
|
101
|
+
}
|
|
102
|
+
function principalEqual(a, b) {
|
|
103
|
+
if (a.length !== b.length) return false;
|
|
104
|
+
for (let i = 0; i < a.length; i++) {
|
|
105
|
+
if (a[i] !== b[i]) return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
async function createProfile(anySigner, opts, ts) {
|
|
110
|
+
const signer = await toPrincipalSigner(anySigner);
|
|
111
|
+
opts = { ...opts };
|
|
112
|
+
if (opts.account && principalEqual(opts.account, signer.principal)) {
|
|
113
|
+
delete opts.account;
|
|
114
|
+
}
|
|
115
|
+
if (opts.avatar === void 0) {
|
|
116
|
+
delete opts.avatar;
|
|
117
|
+
}
|
|
118
|
+
if (opts.description === void 0) {
|
|
119
|
+
delete opts.description;
|
|
120
|
+
}
|
|
121
|
+
const blob = {
|
|
122
|
+
type: "Profile",
|
|
123
|
+
signer: signer.principal,
|
|
124
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
125
|
+
ts,
|
|
126
|
+
...opts
|
|
127
|
+
};
|
|
128
|
+
return encode2(await sign(signer, blob));
|
|
129
|
+
}
|
|
130
|
+
async function createProfileAlias(anySigner, alias, ts) {
|
|
131
|
+
const signer = await toPrincipalSigner(anySigner);
|
|
132
|
+
const blob = {
|
|
133
|
+
type: "Profile",
|
|
134
|
+
signer: signer.principal,
|
|
135
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
136
|
+
ts,
|
|
137
|
+
alias
|
|
138
|
+
};
|
|
139
|
+
return encode2(await sign(signer, blob));
|
|
140
|
+
}
|
|
141
|
+
async function createCapability(anyIssuer, delegate, role, ts, opts = {}) {
|
|
142
|
+
const issuer = await toPrincipalSigner(anyIssuer);
|
|
143
|
+
const present = {};
|
|
144
|
+
if (opts.path !== void 0) present.path = opts.path;
|
|
145
|
+
if (opts.label !== void 0) present.label = opts.label;
|
|
146
|
+
if (opts.audience !== void 0) present.audience = opts.audience;
|
|
147
|
+
const blob = {
|
|
148
|
+
type: "Capability",
|
|
149
|
+
signer: issuer.principal,
|
|
150
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
151
|
+
ts,
|
|
152
|
+
delegate,
|
|
153
|
+
role,
|
|
154
|
+
...present
|
|
155
|
+
};
|
|
156
|
+
return encode2(await sign(issuer, blob));
|
|
157
|
+
}
|
|
158
|
+
async function sign(signer, blob) {
|
|
159
|
+
const unsigned = { ...blob, sig: new Uint8Array(ED25519_SIGNATURE_SIZE) };
|
|
160
|
+
const data = new Uint8Array(encode(unsigned));
|
|
161
|
+
const sig = await (await toPrincipalSigner(signer)).sign(data);
|
|
162
|
+
return { ...unsigned, sig };
|
|
163
|
+
}
|
|
164
|
+
function encode2(blob) {
|
|
165
|
+
const data = new Uint8Array(encode(blob));
|
|
166
|
+
const hash = sha256hash(data);
|
|
167
|
+
const digest = Digest.create(sha256hasher.code, hash);
|
|
168
|
+
const cid = CID.createV1(code, digest);
|
|
169
|
+
return { cid, data, decoded: blob };
|
|
170
|
+
}
|
|
171
|
+
function decodeBlob(data, expectedCid) {
|
|
172
|
+
const decoded = decode(data);
|
|
173
|
+
const reencoded = encode2(decoded);
|
|
174
|
+
if (reencoded.cid.toString() !== expectedCid.toString()) {
|
|
175
|
+
throw new Error(`CID mismatch: expected ${expectedCid}, got ${reencoded.cid}`);
|
|
176
|
+
}
|
|
177
|
+
return { cid: expectedCid, data, decoded };
|
|
178
|
+
}
|
|
179
|
+
async function generateKeyPair() {
|
|
180
|
+
return generateNobleKeyPair();
|
|
181
|
+
}
|
|
182
|
+
async function keyPairFromPrivateKey(rawPrivateKey) {
|
|
183
|
+
return nobleKeyPairFromSeed(rawPrivateKey);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export {
|
|
187
|
+
ED25519_VARINT_PREFIX,
|
|
188
|
+
ED25519_SIGNATURE_SIZE,
|
|
189
|
+
ED25519_PUBLIC_KEY_SIZE,
|
|
190
|
+
ED25519_PRINCIPAL_SIZE,
|
|
191
|
+
NobleKeyPair,
|
|
192
|
+
generateNobleKeyPair,
|
|
193
|
+
nobleKeyPairFromSeed,
|
|
194
|
+
WebCryptoKeyPair,
|
|
195
|
+
generateWebCryptoKeyPair,
|
|
196
|
+
verify,
|
|
197
|
+
principalFromEd25519,
|
|
198
|
+
principalToString,
|
|
199
|
+
principalFromString,
|
|
200
|
+
principalEqual,
|
|
201
|
+
createProfile,
|
|
202
|
+
createProfileAlias,
|
|
203
|
+
createCapability,
|
|
204
|
+
sign,
|
|
205
|
+
encode2 as encode,
|
|
206
|
+
decodeBlob,
|
|
207
|
+
generateKeyPair,
|
|
208
|
+
keyPairFromPrivateKey
|
|
209
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createVersionRef
|
|
3
|
+
} from "./chunk-PJ2RFT5A.mjs";
|
|
4
|
+
import {
|
|
5
|
+
cborCodec,
|
|
6
|
+
normalizeBytes,
|
|
7
|
+
signerPublicKey
|
|
8
|
+
} from "./chunk-LVAHHQIK.mjs";
|
|
9
|
+
|
|
10
|
+
// src/change.ts
|
|
11
|
+
import { decode as cborDecode, encode as cborEncode } from "@ipld/dag-cbor";
|
|
12
|
+
import { CID } from "multiformats";
|
|
13
|
+
import * as Block from "multiformats/block";
|
|
14
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
15
|
+
function createChangeOps(input) {
|
|
16
|
+
const ts = input.ts ?? BigInt(Date.now());
|
|
17
|
+
const unsigned = {
|
|
18
|
+
type: "Change",
|
|
19
|
+
body: {
|
|
20
|
+
ops: input.ops,
|
|
21
|
+
opCount: input.ops.length
|
|
22
|
+
},
|
|
23
|
+
signer: null,
|
|
24
|
+
ts,
|
|
25
|
+
sig: null
|
|
26
|
+
};
|
|
27
|
+
if (input.genesisCid) {
|
|
28
|
+
unsigned.genesis = input.genesisCid;
|
|
29
|
+
unsigned.deps = input.deps ?? [];
|
|
30
|
+
unsigned.depth = input.depth ?? 1;
|
|
31
|
+
}
|
|
32
|
+
return { unsignedBytes: cborEncode(unsigned), ts };
|
|
33
|
+
}
|
|
34
|
+
async function createChange(unsignedBytes, signer) {
|
|
35
|
+
const change = cborDecode(unsignedBytes);
|
|
36
|
+
const genesis = change.genesis instanceof CID ? change.genesis : null;
|
|
37
|
+
const ts = timestampToNumber(change.ts);
|
|
38
|
+
change.signer = new Uint8Array(await signerPublicKey(signer));
|
|
39
|
+
change.sig = new Uint8Array(64);
|
|
40
|
+
change.sig = await signer.sign(cborEncode(change));
|
|
41
|
+
const block = await Block.encode({ value: change, codec: cborCodec, hasher: sha256 });
|
|
42
|
+
return { bytes: block.bytes, cid: block.cid, genesis, ts };
|
|
43
|
+
}
|
|
44
|
+
var signPreparedChange = async (unsignedBytes, signer) => {
|
|
45
|
+
const result = await createChange(unsignedBytes, signer);
|
|
46
|
+
return { signedBytes: result.bytes, cid: result.cid, genesis: result.genesis };
|
|
47
|
+
};
|
|
48
|
+
async function createGenesisChange(signer) {
|
|
49
|
+
const pubKey = await signerPublicKey(signer);
|
|
50
|
+
const unsigned = {
|
|
51
|
+
type: "Change",
|
|
52
|
+
signer: new Uint8Array(pubKey),
|
|
53
|
+
sig: new Uint8Array(64),
|
|
54
|
+
ts: BigInt(0)
|
|
55
|
+
};
|
|
56
|
+
unsigned.sig = await signer.sign(cborEncode(unsigned));
|
|
57
|
+
const block = await Block.encode({ value: unsigned, codec: cborCodec, hasher: sha256 });
|
|
58
|
+
return { bytes: block.bytes, cid: block.cid };
|
|
59
|
+
}
|
|
60
|
+
async function createDocumentChangeFromOps(input, signer) {
|
|
61
|
+
const { unsignedBytes, ts } = createChangeOps(input);
|
|
62
|
+
const { bytes, cid } = await createChange(unsignedBytes, signer);
|
|
63
|
+
return { bytes, cid, ts };
|
|
64
|
+
}
|
|
65
|
+
async function createDocumentChange(input, signer) {
|
|
66
|
+
const ops = protoChangesToOps(input.changes);
|
|
67
|
+
const { unsignedBytes } = createChangeOps({ ops, genesisCid: input.genesisCid, deps: input.deps, depth: input.depth });
|
|
68
|
+
const { bytes, cid } = await createChange(unsignedBytes, signer);
|
|
69
|
+
return { bytes, cid };
|
|
70
|
+
}
|
|
71
|
+
function protoChangesToOps(changes) {
|
|
72
|
+
return changes.map((change) => {
|
|
73
|
+
const op = change.op;
|
|
74
|
+
if (!op || !op.case) throw new Error("Invalid change: missing op");
|
|
75
|
+
switch (op.case) {
|
|
76
|
+
case "setMetadata":
|
|
77
|
+
return {
|
|
78
|
+
type: "SetAttributes",
|
|
79
|
+
attrs: [{ key: [op.value.key], value: op.value.value }]
|
|
80
|
+
};
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Op "${op.case}" is not supported for client-side change creation. Use PrepareDocumentChange + createChange instead.`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
var RESOURCE_VISIBILITY_PRIVATE = 2;
|
|
89
|
+
function visibilityToCbor(protoVisibility) {
|
|
90
|
+
return protoVisibility === RESOURCE_VISIBILITY_PRIVATE ? "Private" : void 0;
|
|
91
|
+
}
|
|
92
|
+
function timestampToNumber(ts) {
|
|
93
|
+
if (typeof ts === "number") return ts;
|
|
94
|
+
if (typeof ts === "bigint") return Number(ts);
|
|
95
|
+
return void 0;
|
|
96
|
+
}
|
|
97
|
+
async function signDocumentChange(input, signer) {
|
|
98
|
+
const {
|
|
99
|
+
bytes: signedBytes,
|
|
100
|
+
cid: changeCid,
|
|
101
|
+
genesis: changeGenesis,
|
|
102
|
+
ts: changeTs
|
|
103
|
+
} = await createChange(input.unsignedChange, signer);
|
|
104
|
+
const effectiveGenesis = input.genesis || (changeGenesis ? changeGenesis.toString() : changeCid.toString());
|
|
105
|
+
const effectiveGeneration = input.generation != null ? Number(input.generation) : changeTs ?? Date.now();
|
|
106
|
+
const refBlobs = await createVersionRef(
|
|
107
|
+
{
|
|
108
|
+
space: input.account,
|
|
109
|
+
path: input.path ?? "",
|
|
110
|
+
genesis: effectiveGenesis,
|
|
111
|
+
version: changeCid.toString(),
|
|
112
|
+
generation: effectiveGeneration,
|
|
113
|
+
capability: input.capability,
|
|
114
|
+
visibility: visibilityToCbor(input.visibility),
|
|
115
|
+
ts: changeTs
|
|
116
|
+
},
|
|
117
|
+
signer
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
changeCid,
|
|
121
|
+
publishInput: {
|
|
122
|
+
blobs: [{ data: normalizeBytes(signedBytes), cid: changeCid.toString() }, ...refBlobs.blobs]
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export {
|
|
128
|
+
createChangeOps,
|
|
129
|
+
createChange,
|
|
130
|
+
signPreparedChange,
|
|
131
|
+
createGenesisChange,
|
|
132
|
+
createDocumentChangeFromOps,
|
|
133
|
+
createDocumentChange,
|
|
134
|
+
visibilityToCbor,
|
|
135
|
+
signDocumentChange
|
|
136
|
+
};
|