@seed-hypermedia/client 0.0.76 → 0.0.78
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/dist/{chunk-DBHL3BYB.mjs → chunk-EZ5UFUMQ.mjs} +78 -1
- package/dist/editor-types.d.ts +1 -0
- package/dist/vault-local.mjs +1 -1
- package/dist/vault-merge.d.ts +67 -0
- package/dist/vault-merge.mjs +332 -0
- package/dist/vault.d.ts +23 -0
- package/dist/vault.mjs +9 -1
- package/package.json +1 -1
|
@@ -45,6 +45,76 @@ async function deserialize(compressed) {
|
|
|
45
45
|
}
|
|
46
46
|
return decodeState(decoded);
|
|
47
47
|
}
|
|
48
|
+
async function serializeState(data) {
|
|
49
|
+
const encodedCb = cborg.encode(encodeStateGoParity(data), cborEncodeOpts);
|
|
50
|
+
return compress(new Uint8Array(encodedCb));
|
|
51
|
+
}
|
|
52
|
+
async function deserializeState(compressed) {
|
|
53
|
+
const decodedCb = await decompress(compressed);
|
|
54
|
+
const decoded = dagCBOR.decode(decodedCb);
|
|
55
|
+
if (decoded.version !== VAULT_VERSION) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Vault schema version mismatch: stored version is ${decoded.version}, but this client expects version ${VAULT_VERSION}. The vault data is incompatible with this version of the application.`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return decodeStateGoParity(decoded);
|
|
61
|
+
}
|
|
62
|
+
function decodeStateGoParity(decoded) {
|
|
63
|
+
const accounts = Array.isArray(decoded.accounts) ? decoded.accounts.map((account) => fillEmptyAccountName(decodeAccountRaw(account))) : [];
|
|
64
|
+
const restored = {
|
|
65
|
+
version: VAULT_VERSION,
|
|
66
|
+
accounts
|
|
67
|
+
};
|
|
68
|
+
if (typeof decoded.notificationServerUrl === "string") {
|
|
69
|
+
restored.notificationServerUrl = decoded.notificationServerUrl;
|
|
70
|
+
}
|
|
71
|
+
if (isDeletedAccounts(decoded.deletedAccounts)) {
|
|
72
|
+
restored.deletedAccounts = { ...decoded.deletedAccounts };
|
|
73
|
+
}
|
|
74
|
+
restored[stateExtraKey] = omitKnown(decoded, ["version", "notificationServerUrl", "deletedAccounts", "accounts"]);
|
|
75
|
+
return restored;
|
|
76
|
+
}
|
|
77
|
+
function encodeStateGoParity(data) {
|
|
78
|
+
const encoded = {
|
|
79
|
+
...data[stateExtraKey],
|
|
80
|
+
version: VAULT_VERSION,
|
|
81
|
+
accounts: data.accounts.map((account) => encodeAccountGoParity(account))
|
|
82
|
+
};
|
|
83
|
+
if (data.notificationServerUrl) {
|
|
84
|
+
encoded.notificationServerUrl = data.notificationServerUrl;
|
|
85
|
+
}
|
|
86
|
+
if (data.deletedAccounts && Object.keys(data.deletedAccounts).length > 0) {
|
|
87
|
+
encoded.deletedAccounts = data.deletedAccounts;
|
|
88
|
+
}
|
|
89
|
+
return encoded;
|
|
90
|
+
}
|
|
91
|
+
function encodeAccountGoParity(account) {
|
|
92
|
+
const filled = fillEmptyAccountName(account);
|
|
93
|
+
return {
|
|
94
|
+
...filled[accountExtraKey],
|
|
95
|
+
seed: filled.seed,
|
|
96
|
+
createTime: filled.createTime,
|
|
97
|
+
delegations: (filled.delegations ?? []).map((delegation) => encodeDelegation(delegation)),
|
|
98
|
+
name: filled.name
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function fillEmptyAccountName(account) {
|
|
102
|
+
if (typeof account.name === "string" && account.name.trim() !== "") return account;
|
|
103
|
+
return { ...account, name: accountIdFromSeed(account.seed) };
|
|
104
|
+
}
|
|
105
|
+
function getUnknownFields(target) {
|
|
106
|
+
return target[unknownFieldsKeyFor(target)] ?? {};
|
|
107
|
+
}
|
|
108
|
+
function setUnknownFields(target, extras) {
|
|
109
|
+
;
|
|
110
|
+
target[unknownFieldsKeyFor(target)] = { ...extras };
|
|
111
|
+
}
|
|
112
|
+
function unknownFieldsKeyFor(target) {
|
|
113
|
+
if ("accounts" in target) return stateExtraKey;
|
|
114
|
+
if ("seed" in target) return accountExtraKey;
|
|
115
|
+
if ("capability" in target) return delegationExtraKey;
|
|
116
|
+
return capabilityExtraKey;
|
|
117
|
+
}
|
|
48
118
|
function getAccountName(account) {
|
|
49
119
|
const normalized = typeof account.name === "string" ? account.name.trim() : "";
|
|
50
120
|
if (normalized && accountNamePattern.test(normalized)) {
|
|
@@ -99,6 +169,9 @@ function decodeState(decoded) {
|
|
|
99
169
|
return restored;
|
|
100
170
|
}
|
|
101
171
|
function decodeAccount(decoded) {
|
|
172
|
+
return normalizeAccount(decodeAccountRaw(decoded));
|
|
173
|
+
}
|
|
174
|
+
function decodeAccountRaw(decoded) {
|
|
102
175
|
const account = {
|
|
103
176
|
name: typeof decoded.name === "string" ? decoded.name : void 0,
|
|
104
177
|
seed: new Uint8Array(decoded.seed),
|
|
@@ -106,7 +179,7 @@ function decodeAccount(decoded) {
|
|
|
106
179
|
delegations: Array.isArray(decoded.delegations) ? decoded.delegations.map((delegation) => decodeDelegation(delegation)) : []
|
|
107
180
|
};
|
|
108
181
|
account[accountExtraKey] = omitKnown(decoded, ["name", "seed", "createTime", "delegations"]);
|
|
109
|
-
return
|
|
182
|
+
return account;
|
|
110
183
|
}
|
|
111
184
|
function decodeDelegation(decoded) {
|
|
112
185
|
const delegation = {
|
|
@@ -314,6 +387,10 @@ export {
|
|
|
314
387
|
createEmpty,
|
|
315
388
|
serialize,
|
|
316
389
|
deserialize,
|
|
390
|
+
serializeState,
|
|
391
|
+
deserializeState,
|
|
392
|
+
getUnknownFields,
|
|
393
|
+
setUnknownFields,
|
|
317
394
|
getAccountName,
|
|
318
395
|
deriveHKDFKey,
|
|
319
396
|
deriveEncryptionKey,
|
package/dist/editor-types.d.ts
CHANGED
package/dist/vault-local.mjs
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vault state merge — a line-for-line port of the Go implementation in
|
|
3
|
+
* backend/storage/vault/vault.go (normalizeState/mergeNormalizedStates/
|
|
4
|
+
* statesEqual and helpers, vault.go:2387-2790).
|
|
5
|
+
*
|
|
6
|
+
* Byte-compatibility with Go is the prime directive here: the merge is what
|
|
7
|
+
* keeps every device's vault convergent, and any divergence makes devices
|
|
8
|
+
* oscillate or silently lose identities. Notable Go semantics preserved:
|
|
9
|
+
*
|
|
10
|
+
* - Accounts are keyed by PRINCIPAL (base58 account ID derived from the seed),
|
|
11
|
+
* never by display name. Two accounts may share a display name.
|
|
12
|
+
* - ALL string comparisons are byte-wise (Go's `<`/`>` on strings), NEVER
|
|
13
|
+
* locale-aware: base58 principals are mixed-case and ICU collation orders
|
|
14
|
+
* them differently than Go does.
|
|
15
|
+
* - Tombstones kill an account only when `tombstone >= latestLiveCreateTime`
|
|
16
|
+
* (and > 0); otherwise the tombstone is deleted (resurrection).
|
|
17
|
+
* - Unknown CBOR fields (Go's `Extra`) merge as remote's overlaid by local's,
|
|
18
|
+
* and participate in equality via dag-cbor byte comparison.
|
|
19
|
+
*
|
|
20
|
+
* Merge decisions are logged through the `log` callback (default: console.log
|
|
21
|
+
* with the same 🔑 VAULT-MERGE marker the Go daemon uses) — this path has
|
|
22
|
+
* burned the team before, keep it loud and greppable.
|
|
23
|
+
*/
|
|
24
|
+
import './base64';
|
|
25
|
+
import { type Account, type State } from './vault';
|
|
26
|
+
/** Loud, greppable marker matching Go's vaultMergeLogPrefix. */
|
|
27
|
+
export declare const VAULT_MERGE_LOG_PREFIX = "\uD83D\uDD11 VAULT-MERGE";
|
|
28
|
+
export type VaultMergeLogger = (message: string, fields?: Record<string, unknown>) => void;
|
|
29
|
+
export type NormalizedAccount = {
|
|
30
|
+
principal: string;
|
|
31
|
+
account: Account;
|
|
32
|
+
};
|
|
33
|
+
export type NormalizedState = {
|
|
34
|
+
notificationServerUrl: string;
|
|
35
|
+
/** Accounts keyed by principal (base58 account ID). */
|
|
36
|
+
accounts: Map<string, NormalizedAccount>;
|
|
37
|
+
/** Tombstones keyed by principal. */
|
|
38
|
+
deletedAccounts: Record<string, number>;
|
|
39
|
+
/** Unknown top-level CBOR fields (Go State.Extra), preserved through the merge. */
|
|
40
|
+
extra: Record<string, unknown>;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Normalize a state for merging: key accounts by principal, resolve duplicate
|
|
44
|
+
* principals, normalize display names (merge-time only) and dedupe delegations
|
|
45
|
+
* by capability CID. Throws on invalid (non-32-byte) seeds, like Go errors.
|
|
46
|
+
* `source` labels error messages ('local' or 'remote').
|
|
47
|
+
*/
|
|
48
|
+
export declare function normalizeState(state: State, source: string): NormalizedState;
|
|
49
|
+
/** Semantic state equality, matching Go's statesEqual (never compares serialized bytes). */
|
|
50
|
+
export declare function statesEqual(left: State, right: State): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Merge two states, matching Go's mergeStates convenience path:
|
|
53
|
+
* normalize both sides, then mergeNormalizedStates.
|
|
54
|
+
*/
|
|
55
|
+
export declare function mergeStates(local: State, remote: State, localVersion: number, remoteVersion: number, log?: VaultMergeLogger): {
|
|
56
|
+
merged: State;
|
|
57
|
+
changedFromLocal: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The merge algorithm itself (Go mergeNormalizedStates). `changedFromLocal`
|
|
61
|
+
* reports whether the merged result differs semantically from `local` — the
|
|
62
|
+
* caller uses it to decide whether the local vault needs rewriting.
|
|
63
|
+
*/
|
|
64
|
+
export declare function mergeNormalizedStates(local: NormalizedState, remote: NormalizedState, localVersion: number, remoteVersion: number, log?: VaultMergeLogger): {
|
|
65
|
+
merged: State;
|
|
66
|
+
changedFromLocal: boolean;
|
|
67
|
+
};
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VAULT_VERSION,
|
|
3
|
+
accountIdFromSeed,
|
|
4
|
+
getUnknownFields,
|
|
5
|
+
setUnknownFields
|
|
6
|
+
} from "./chunk-EZ5UFUMQ.mjs";
|
|
7
|
+
import "./chunk-UOL3F6XQ.mjs";
|
|
8
|
+
import "./chunk-2MD3FXKB.mjs";
|
|
9
|
+
import "./chunk-IT5LLTMP.mjs";
|
|
10
|
+
import "./chunk-DR3HJ6BV.mjs";
|
|
11
|
+
import "./chunk-3TPCJCVO.mjs";
|
|
12
|
+
import "./chunk-7P6ASYW6.mjs";
|
|
13
|
+
|
|
14
|
+
// src/vault-merge.ts
|
|
15
|
+
import * as dagCBOR from "@ipld/dag-cbor";
|
|
16
|
+
import * as cborg from "cborg";
|
|
17
|
+
var cborEncodeOpts = {
|
|
18
|
+
...dagCBOR.encodeOptions,
|
|
19
|
+
typeEncoders: {
|
|
20
|
+
...dagCBOR.encodeOptions.typeEncoders,
|
|
21
|
+
undefined: () => null
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var localKeyNameFormat = /^[a-zA-Z0-9_-]+$/;
|
|
25
|
+
var ED25519_SEED_SIZE = 32;
|
|
26
|
+
var VAULT_MERGE_LOG_PREFIX = "\u{1F511} VAULT-MERGE";
|
|
27
|
+
var defaultLog = (message, fields) => {
|
|
28
|
+
if (fields === void 0) {
|
|
29
|
+
console.log(`${VAULT_MERGE_LOG_PREFIX} ${message}`);
|
|
30
|
+
} else {
|
|
31
|
+
console.log(`${VAULT_MERGE_LOG_PREFIX} ${message}`, fields);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function normalizeState(state, source) {
|
|
35
|
+
const normalized = {
|
|
36
|
+
notificationServerUrl: state.notificationServerUrl ?? "",
|
|
37
|
+
accounts: /* @__PURE__ */ new Map(),
|
|
38
|
+
deletedAccounts: { ...state.deletedAccounts ?? {} },
|
|
39
|
+
extra: { ...getUnknownFields(state) }
|
|
40
|
+
};
|
|
41
|
+
state.accounts.forEach((account, idx) => {
|
|
42
|
+
if (account.seed.length !== ED25519_SEED_SIZE) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`${source} account ${idx} has invalid seed length: got ${account.seed.length}, expected ${ED25519_SEED_SIZE}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
const principal = accountIdFromSeed(account.seed);
|
|
48
|
+
let normalizedDelegations;
|
|
49
|
+
try {
|
|
50
|
+
normalizedDelegations = normalizeDelegations(account.delegations);
|
|
51
|
+
} catch (err) {
|
|
52
|
+
throw new Error(`${source} account ${idx} has invalid delegations: ${err.message}`);
|
|
53
|
+
}
|
|
54
|
+
const nextAccount = {
|
|
55
|
+
name: normalizeKeyName(account.name, principal),
|
|
56
|
+
seed: new Uint8Array(account.seed),
|
|
57
|
+
createTime: account.createTime,
|
|
58
|
+
delegations: normalizedDelegations
|
|
59
|
+
};
|
|
60
|
+
setUnknownFields(nextAccount, getUnknownFields(account));
|
|
61
|
+
const next = { principal, account: nextAccount };
|
|
62
|
+
const existing = normalized.accounts.get(principal);
|
|
63
|
+
if (!existing) {
|
|
64
|
+
normalized.accounts.set(principal, next);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
normalized.accounts.set(principal, mergeDuplicateAccount(existing, next));
|
|
68
|
+
});
|
|
69
|
+
return normalized;
|
|
70
|
+
}
|
|
71
|
+
function statesEqual(left, right) {
|
|
72
|
+
return normalizedStatesEqual(normalizeState(left, "local"), normalizeState(right, "remote"));
|
|
73
|
+
}
|
|
74
|
+
function mergeStates(local, remote, localVersion, remoteVersion, log = defaultLog) {
|
|
75
|
+
return mergeNormalizedStates(
|
|
76
|
+
normalizeState(local, "local"),
|
|
77
|
+
normalizeState(remote, "remote"),
|
|
78
|
+
localVersion,
|
|
79
|
+
remoteVersion,
|
|
80
|
+
log
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
function mergeNormalizedStates(local, remote, localVersion, remoteVersion, log = defaultLog) {
|
|
84
|
+
const mergedDeleted = { ...local.deletedAccounts };
|
|
85
|
+
for (const [accountID, deleteTime] of Object.entries(remote.deletedAccounts)) {
|
|
86
|
+
if ((mergedDeleted[accountID] ?? 0) < deleteTime) {
|
|
87
|
+
mergedDeleted[accountID] = deleteTime;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const merged = {
|
|
91
|
+
version: VAULT_VERSION,
|
|
92
|
+
accounts: []
|
|
93
|
+
};
|
|
94
|
+
const notificationServerUrl = chooseMergedNotificationServerURL(
|
|
95
|
+
local.notificationServerUrl,
|
|
96
|
+
remote.notificationServerUrl,
|
|
97
|
+
localVersion,
|
|
98
|
+
remoteVersion
|
|
99
|
+
);
|
|
100
|
+
if (notificationServerUrl) {
|
|
101
|
+
merged.notificationServerUrl = notificationServerUrl;
|
|
102
|
+
}
|
|
103
|
+
setUnknownFields(merged, { ...remote.extra, ...local.extra });
|
|
104
|
+
const accountIDs = /* @__PURE__ */ new Set([
|
|
105
|
+
...local.accounts.keys(),
|
|
106
|
+
...remote.accounts.keys(),
|
|
107
|
+
...Object.keys(mergedDeleted)
|
|
108
|
+
]);
|
|
109
|
+
const accountIDsList = [...accountIDs].sort(compareStringsBytewise);
|
|
110
|
+
for (const accountID of accountIDsList) {
|
|
111
|
+
const localAccount = local.accounts.get(accountID);
|
|
112
|
+
const remoteAccount = remote.accounts.get(accountID);
|
|
113
|
+
const localTombstone = local.deletedAccounts[accountID] ?? 0;
|
|
114
|
+
const remoteTombstone = remote.deletedAccounts[accountID] ?? 0;
|
|
115
|
+
const winningTombstone = Math.max(localTombstone, remoteTombstone);
|
|
116
|
+
let latestLiveCreateTime = 0;
|
|
117
|
+
if (localAccount && localAccount.account.createTime > latestLiveCreateTime) {
|
|
118
|
+
latestLiveCreateTime = localAccount.account.createTime;
|
|
119
|
+
}
|
|
120
|
+
if (remoteAccount && remoteAccount.account.createTime > latestLiveCreateTime) {
|
|
121
|
+
latestLiveCreateTime = remoteAccount.account.createTime;
|
|
122
|
+
}
|
|
123
|
+
if (winningTombstone >= latestLiveCreateTime && winningTombstone > 0) {
|
|
124
|
+
mergedDeleted[accountID] = winningTombstone;
|
|
125
|
+
log("account deleted by tombstone", { accountID, winningTombstone, latestLiveCreateTime });
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
delete mergedDeleted[accountID];
|
|
129
|
+
if (!localAccount && !remoteAccount) continue;
|
|
130
|
+
let next;
|
|
131
|
+
if (localAccount && remoteAccount) {
|
|
132
|
+
if (bytesEqual(localAccount.account.seed, remoteAccount.account.seed)) {
|
|
133
|
+
next = mergeMatchingAccounts(localAccount.account, remoteAccount.account);
|
|
134
|
+
} else if (accountWinsByTiebreak(localAccount, remoteAccount)) {
|
|
135
|
+
log("divergent key material for principal: REMOTE account won the tiebreak", { accountID });
|
|
136
|
+
next = remoteAccount.account;
|
|
137
|
+
} else {
|
|
138
|
+
log("divergent key material for principal: LOCAL account won the tiebreak", { accountID });
|
|
139
|
+
next = localAccount.account;
|
|
140
|
+
}
|
|
141
|
+
} else if (localAccount) {
|
|
142
|
+
next = localAccount.account;
|
|
143
|
+
} else {
|
|
144
|
+
next = remoteAccount.account;
|
|
145
|
+
}
|
|
146
|
+
merged.accounts.push(next);
|
|
147
|
+
}
|
|
148
|
+
merged.accounts.sort((a, b) => {
|
|
149
|
+
const nameDiff = compareStringsBytewise(a.name ?? "", b.name ?? "");
|
|
150
|
+
if (nameDiff !== 0) return nameDiff;
|
|
151
|
+
return compareStringsBytewise(accountIdFromSeed(a.seed), accountIdFromSeed(b.seed));
|
|
152
|
+
});
|
|
153
|
+
if (Object.keys(mergedDeleted).length > 0) {
|
|
154
|
+
merged.deletedAccounts = mergedDeleted;
|
|
155
|
+
}
|
|
156
|
+
const mergedNormalized = normalizeState(merged, "local");
|
|
157
|
+
const changedFromLocal = !normalizedStatesEqual(local, mergedNormalized);
|
|
158
|
+
log("mergeNormalizedStates result (local \u2295 remote \u2192 merged)", {
|
|
159
|
+
localVersion,
|
|
160
|
+
remoteVersion,
|
|
161
|
+
changedLocalVault: changedFromLocal,
|
|
162
|
+
localIdentityCount: local.accounts.size,
|
|
163
|
+
remoteIdentityCount: remote.accounts.size,
|
|
164
|
+
mergedIdentityCount: merged.accounts.length,
|
|
165
|
+
mergedIdentities: merged.accounts.map((account) => `${account.name ?? ""} (${accountIdFromSeed(account.seed)})`),
|
|
166
|
+
mergedTombstones: Object.keys(mergedDeleted).sort(compareStringsBytewise)
|
|
167
|
+
});
|
|
168
|
+
return { merged, changedFromLocal };
|
|
169
|
+
}
|
|
170
|
+
function normalizedStatesEqual(left, right) {
|
|
171
|
+
if (left.notificationServerUrl !== right.notificationServerUrl) return false;
|
|
172
|
+
if (!equalDeletedAccounts(left.deletedAccounts, right.deletedAccounts)) return false;
|
|
173
|
+
if (!cborValuesEqual(left.extra, right.extra)) return false;
|
|
174
|
+
if (left.accounts.size !== right.accounts.size) return false;
|
|
175
|
+
for (const [principal, leftAccount] of left.accounts) {
|
|
176
|
+
const rightAccount = right.accounts.get(principal);
|
|
177
|
+
if (!rightAccount) return false;
|
|
178
|
+
if (!bytesEqual(leftAccount.account.seed, rightAccount.account.seed)) return false;
|
|
179
|
+
if (!cborValuesEqual(accountToEncodable(leftAccount.account), accountToEncodable(rightAccount.account))) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
function accountWinsByTiebreak(current, candidate) {
|
|
186
|
+
if (candidate.account.createTime > current.account.createTime) return true;
|
|
187
|
+
if (candidate.account.createTime < current.account.createTime) return false;
|
|
188
|
+
const principalDiff = compareStringsBytewise(candidate.principal, current.principal);
|
|
189
|
+
if (principalDiff > 0) return true;
|
|
190
|
+
if (principalDiff < 0) return false;
|
|
191
|
+
return compareStringsBytewise(candidate.account.name ?? "", current.account.name ?? "") > 0;
|
|
192
|
+
}
|
|
193
|
+
function mergeDuplicateAccount(existing, candidate) {
|
|
194
|
+
if (bytesEqual(existing.account.seed, candidate.account.seed)) {
|
|
195
|
+
const mergedDelegations = normalizeDelegations([...existing.account.delegations, ...candidate.account.delegations]);
|
|
196
|
+
if (accountWinsByTiebreak(existing, candidate)) {
|
|
197
|
+
candidate.account.delegations = mergedDelegations;
|
|
198
|
+
return candidate;
|
|
199
|
+
}
|
|
200
|
+
existing.account.delegations = mergedDelegations;
|
|
201
|
+
return existing;
|
|
202
|
+
}
|
|
203
|
+
if (accountWinsByTiebreak(existing, candidate)) return candidate;
|
|
204
|
+
return existing;
|
|
205
|
+
}
|
|
206
|
+
function mergeMatchingAccounts(local, remote) {
|
|
207
|
+
const mergedDelegations = normalizeDelegations([...local.delegations, ...remote.delegations]);
|
|
208
|
+
const merged = {
|
|
209
|
+
name: local.name || remote.name,
|
|
210
|
+
seed: local.seed.length > 0 ? new Uint8Array(local.seed) : remote.seed,
|
|
211
|
+
createTime: Math.max(local.createTime, remote.createTime),
|
|
212
|
+
delegations: mergedDelegations
|
|
213
|
+
};
|
|
214
|
+
setUnknownFields(merged, { ...getUnknownFields(remote), ...getUnknownFields(local) });
|
|
215
|
+
return merged;
|
|
216
|
+
}
|
|
217
|
+
function normalizeDelegations(delegations) {
|
|
218
|
+
if (!delegations || delegations.length === 0) return [];
|
|
219
|
+
const byCID = /* @__PURE__ */ new Map();
|
|
220
|
+
delegations.forEach((delegation, idx) => {
|
|
221
|
+
let cidKey;
|
|
222
|
+
try {
|
|
223
|
+
cidKey = delegationCapabilityCIDKey(delegation);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
throw new Error(`delegation ${idx}: ${err.message}`);
|
|
226
|
+
}
|
|
227
|
+
const existing = byCID.get(cidKey);
|
|
228
|
+
if (!existing || delegationWinsByTiebreak(existing, delegation)) {
|
|
229
|
+
byCID.set(cidKey, delegation);
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
const keys = [...byCID.keys()].sort(compareStringsBytewise);
|
|
233
|
+
return keys.map((key) => byCID.get(key));
|
|
234
|
+
}
|
|
235
|
+
function delegationWinsByTiebreak(current, candidate) {
|
|
236
|
+
if (candidate.createTime > current.createTime) return true;
|
|
237
|
+
if (candidate.createTime < current.createTime) return false;
|
|
238
|
+
const clientDiff = compareStringsBytewise(candidate.clientId, current.clientId);
|
|
239
|
+
if (clientDiff > 0) return true;
|
|
240
|
+
if (clientDiff < 0) return false;
|
|
241
|
+
return compareStringsBytewise(candidate.deviceType ?? "", current.deviceType ?? "") > 0;
|
|
242
|
+
}
|
|
243
|
+
function delegationCapabilityCIDKey(delegation) {
|
|
244
|
+
var _a;
|
|
245
|
+
const cid = (_a = delegation.capability) == null ? void 0 : _a.cid;
|
|
246
|
+
if (!cid) {
|
|
247
|
+
throw new Error("delegation capability CID is missing");
|
|
248
|
+
}
|
|
249
|
+
return dagCBOR.encode(cid).toBase64({ alphabet: "base64url", omitPadding: true });
|
|
250
|
+
}
|
|
251
|
+
function chooseMergedNotificationServerURL(local, remote, localVersion, remoteVersion) {
|
|
252
|
+
if (remoteVersion > localVersion) return remote;
|
|
253
|
+
if (localVersion > remoteVersion) return local;
|
|
254
|
+
if (local !== "" || remote === "") return local;
|
|
255
|
+
return remote;
|
|
256
|
+
}
|
|
257
|
+
function normalizeKeyName(name, principal) {
|
|
258
|
+
const normalized = (name ?? "").trim();
|
|
259
|
+
if (normalized === "" || !localKeyNameFormat.test(normalized)) return principal;
|
|
260
|
+
return normalized;
|
|
261
|
+
}
|
|
262
|
+
function equalDeletedAccounts(left, right) {
|
|
263
|
+
const leftEntries = Object.entries(left);
|
|
264
|
+
if (leftEntries.length !== Object.keys(right).length) return false;
|
|
265
|
+
for (const [name, leftValue] of leftEntries) {
|
|
266
|
+
if (right[name] !== leftValue) return false;
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
function cborValuesEqual(left, right) {
|
|
271
|
+
let leftEncoded;
|
|
272
|
+
let rightEncoded;
|
|
273
|
+
try {
|
|
274
|
+
leftEncoded = cborg.encode(left, cborEncodeOpts);
|
|
275
|
+
rightEncoded = cborg.encode(right, cborEncodeOpts);
|
|
276
|
+
} catch {
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
return bytesEqual(leftEncoded, rightEncoded);
|
|
280
|
+
}
|
|
281
|
+
function accountToEncodable(account) {
|
|
282
|
+
const out = {
|
|
283
|
+
...getUnknownFields(account),
|
|
284
|
+
seed: account.seed,
|
|
285
|
+
createTime: account.createTime,
|
|
286
|
+
delegations: account.delegations.map((delegation) => delegationToEncodable(delegation))
|
|
287
|
+
};
|
|
288
|
+
if (account.name) out.name = account.name;
|
|
289
|
+
return out;
|
|
290
|
+
}
|
|
291
|
+
function delegationToEncodable(delegation) {
|
|
292
|
+
const out = {
|
|
293
|
+
...getUnknownFields(delegation),
|
|
294
|
+
clientId: delegation.clientId,
|
|
295
|
+
capability: capabilityToEncodable(delegation.capability),
|
|
296
|
+
createTime: delegation.createTime
|
|
297
|
+
};
|
|
298
|
+
if (delegation.deviceType) out.deviceType = delegation.deviceType;
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
function capabilityToEncodable(capability) {
|
|
302
|
+
return {
|
|
303
|
+
...getUnknownFields(capability),
|
|
304
|
+
cid: capability.cid,
|
|
305
|
+
delegate: capability.delegate
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
var utf8Encoder = new TextEncoder();
|
|
309
|
+
function compareStringsBytewise(a, b) {
|
|
310
|
+
const aBytes = utf8Encoder.encode(a);
|
|
311
|
+
const bBytes = utf8Encoder.encode(b);
|
|
312
|
+
const len = Math.min(aBytes.length, bBytes.length);
|
|
313
|
+
for (let i = 0; i < len; i++) {
|
|
314
|
+
const diff = aBytes[i] - bBytes[i];
|
|
315
|
+
if (diff !== 0) return diff;
|
|
316
|
+
}
|
|
317
|
+
return aBytes.length - bBytes.length;
|
|
318
|
+
}
|
|
319
|
+
function bytesEqual(a, b) {
|
|
320
|
+
if (a.length !== b.length) return false;
|
|
321
|
+
for (let i = 0; i < a.length; i++) {
|
|
322
|
+
if (a[i] !== b[i]) return false;
|
|
323
|
+
}
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
export {
|
|
327
|
+
VAULT_MERGE_LOG_PREFIX,
|
|
328
|
+
mergeNormalizedStates,
|
|
329
|
+
mergeStates,
|
|
330
|
+
normalizeState,
|
|
331
|
+
statesEqual
|
|
332
|
+
};
|
package/dist/vault.d.ts
CHANGED
|
@@ -78,12 +78,34 @@ export interface State {
|
|
|
78
78
|
/** List of Hypermedia accounts. */
|
|
79
79
|
accounts: Account[];
|
|
80
80
|
}
|
|
81
|
+
type UnknownFields = Record<string, unknown>;
|
|
81
82
|
/** Create an empty vault. */
|
|
82
83
|
export declare function createEmpty(): State;
|
|
83
84
|
/** Serialize vault data: CBOR encode → gzip compress. Returns compressed bytes. */
|
|
84
85
|
export declare function serialize(data: State): Promise<Uint8Array>;
|
|
85
86
|
/** Deserialize vault data: gzip decompress → CBOR decode. Throws if version mismatches. */
|
|
86
87
|
export declare function deserialize(compressed: Uint8Array): Promise<State>;
|
|
88
|
+
/**
|
|
89
|
+
* Go-parity serialize: CBOR encode → gzip, matching backend/storage/vault
|
|
90
|
+
* state.go `encodeState`. Never dedupes or renames accounts; only fills
|
|
91
|
+
* whitespace-empty names with the account principal.
|
|
92
|
+
*/
|
|
93
|
+
export declare function serializeState(data: State): Promise<Uint8Array>;
|
|
94
|
+
/**
|
|
95
|
+
* Go-parity deserialize: gzip decompress → CBOR decode, matching
|
|
96
|
+
* backend/storage/vault state.go `decodeState`. Never dedupes or renames
|
|
97
|
+
* accounts; only fills whitespace-empty names with the account principal.
|
|
98
|
+
* Throws if the schema version mismatches.
|
|
99
|
+
*/
|
|
100
|
+
export declare function deserializeState(compressed: Uint8Array): Promise<State>;
|
|
101
|
+
/**
|
|
102
|
+
* Read the unknown CBOR fields preserved on a decoded vault object (Go's
|
|
103
|
+
* `Extra` / mapstructure ",remain" fields). Returns the live extras object
|
|
104
|
+
* (empty when none) — treat it as read-only.
|
|
105
|
+
*/
|
|
106
|
+
export declare function getUnknownFields(target: State | Account | DelegatedSession | CapabilityMeta): UnknownFields;
|
|
107
|
+
/** Replace the unknown CBOR fields carried by a vault object (stores a shallow copy). */
|
|
108
|
+
export declare function setUnknownFields(target: State | Account | DelegatedSession | CapabilityMeta, extras: UnknownFields): void;
|
|
87
109
|
/** Ensure account name compatibility for older vault records that have no label. */
|
|
88
110
|
export declare function getAccountName(account: Pick<Account, 'name' | 'seed'>): string;
|
|
89
111
|
/**
|
|
@@ -139,3 +161,4 @@ export declare function unwrapDEK(envelope: VaultEnvelope, wrappingKey: Uint8Arr
|
|
|
139
161
|
* Decrypt and decode the vault state from an envelope using the unwrapped DEK.
|
|
140
162
|
*/
|
|
141
163
|
export declare function decryptVaultState(envelope: VaultEnvelope, dek: Uint8Array): Promise<State>;
|
|
164
|
+
export {};
|
package/dist/vault.mjs
CHANGED
|
@@ -8,13 +8,17 @@ import {
|
|
|
8
8
|
deriveHKDFKey,
|
|
9
9
|
deriveSecretCredentialAuthKey,
|
|
10
10
|
deserialize,
|
|
11
|
+
deserializeState,
|
|
11
12
|
getAccountName,
|
|
13
|
+
getUnknownFields,
|
|
12
14
|
parseVaultEnvelope,
|
|
13
15
|
principalFromPublicKey,
|
|
14
16
|
principalToString,
|
|
15
17
|
serialize,
|
|
18
|
+
serializeState,
|
|
19
|
+
setUnknownFields,
|
|
16
20
|
unwrapDEK
|
|
17
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-EZ5UFUMQ.mjs";
|
|
18
22
|
import "./chunk-UOL3F6XQ.mjs";
|
|
19
23
|
import "./chunk-2MD3FXKB.mjs";
|
|
20
24
|
import "./chunk-IT5LLTMP.mjs";
|
|
@@ -31,10 +35,14 @@ export {
|
|
|
31
35
|
deriveHKDFKey,
|
|
32
36
|
deriveSecretCredentialAuthKey,
|
|
33
37
|
deserialize,
|
|
38
|
+
deserializeState,
|
|
34
39
|
getAccountName,
|
|
40
|
+
getUnknownFields,
|
|
35
41
|
parseVaultEnvelope,
|
|
36
42
|
principalFromPublicKey,
|
|
37
43
|
principalToString,
|
|
38
44
|
serialize,
|
|
45
|
+
serializeState,
|
|
46
|
+
setUnknownFields,
|
|
39
47
|
unwrapDEK
|
|
40
48
|
};
|