@witnium/witniumchain 0.25.0 → 0.27.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.
- package/README.md +91 -8
- package/dist/index.d.mts +376 -9
- package/dist/index.d.ts +376 -9
- package/dist/index.js +142 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +138 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,6 +13,100 @@ var WitniumchainApiError = class extends Error {
|
|
|
13
13
|
this.body = args.body ?? null;
|
|
14
14
|
}
|
|
15
15
|
};
|
|
16
|
+
var WitnessProposeError = class extends WitniumchainApiError {
|
|
17
|
+
/** The scope of the failed propose (64 lowercase hex). */
|
|
18
|
+
scope;
|
|
19
|
+
constructor(cause, scope) {
|
|
20
|
+
if (cause instanceof WitniumchainApiError) {
|
|
21
|
+
super({ status: cause.status, message: cause.message, errorLabel: cause.errorLabel, body: cause.body });
|
|
22
|
+
} else {
|
|
23
|
+
super({ status: 0, message: cause instanceof Error ? cause.message : String(cause) });
|
|
24
|
+
}
|
|
25
|
+
this.name = "WitnessProposeError";
|
|
26
|
+
this.scope = scope;
|
|
27
|
+
this.cause = cause;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var WitnessScopeConflictError = class extends WitnessProposeError {
|
|
31
|
+
code;
|
|
32
|
+
/** The witness already in the scope (`scope_details_mismatch` once it is mined). */
|
|
33
|
+
witnessId;
|
|
34
|
+
constructor(cause, scope, code, witnessId) {
|
|
35
|
+
super(cause, scope);
|
|
36
|
+
this.name = "WitnessScopeConflictError";
|
|
37
|
+
this.code = code;
|
|
38
|
+
this.witnessId = witnessId;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/witness-scope.ts
|
|
43
|
+
var SCOPE_PATTERN = /^[0-9a-fA-F]{64}$/;
|
|
44
|
+
var CONFLICT_CODES = /* @__PURE__ */ new Set([
|
|
45
|
+
"scope_details_mismatch",
|
|
46
|
+
"scope_in_progress",
|
|
47
|
+
"scope_registry_mismatch"
|
|
48
|
+
]);
|
|
49
|
+
function randomWitnessScope() {
|
|
50
|
+
const bytes = new Uint8Array(32);
|
|
51
|
+
cryptoRef().getRandomValues(bytes);
|
|
52
|
+
return toHex(bytes);
|
|
53
|
+
}
|
|
54
|
+
async function witnessScopeFor(key) {
|
|
55
|
+
if (key.length === 0) {
|
|
56
|
+
throw new Error("witnessScopeFor: key must not be empty");
|
|
57
|
+
}
|
|
58
|
+
const digest = await cryptoRef().subtle.digest("SHA-256", new TextEncoder().encode(key));
|
|
59
|
+
return toHex(new Uint8Array(digest));
|
|
60
|
+
}
|
|
61
|
+
function resolveWitnessScope(scope) {
|
|
62
|
+
if (scope === void 0) return randomWitnessScope();
|
|
63
|
+
if (!SCOPE_PATTERN.test(scope)) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"proposeWitnessV5: scope must be the SHA-256 of your key, exactly 64 hex characters (see witnessScopeFor)"
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return scope.toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
function scopedProposeResult(answer, scope) {
|
|
71
|
+
const a = typeof answer === "object" && answer !== null ? answer : {};
|
|
72
|
+
const required = a.outcome === "created" ? { intentId: "string", witnessNonce: "string", payloadToSign: "string", expiresInSeconds: "number", requiredSigners: "string[]" } : a.outcome === "existing" ? { intentId: "string", witnessId: "string", blockNumber: "number", chainTimestamp: "number" } : void 0;
|
|
73
|
+
if (required === void 0) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`proposeWitnessV5: accounts answered a scoped propose without a valid outcome (got ${JSON.stringify(a.outcome)}); this accounts does not support witness scopes`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
const missing = Object.entries(required).filter(
|
|
79
|
+
([field, type]) => type === "string[]" ? !(Array.isArray(a[field]) && a[field].every((v) => typeof v === "string")) : typeof a[field] !== type
|
|
80
|
+
).map(([field]) => field);
|
|
81
|
+
if (missing.length > 0) {
|
|
82
|
+
throw new Error(`proposeWitnessV5: accounts' "${String(a.outcome)}" answer lacks ${missing.join(", ")}`);
|
|
83
|
+
}
|
|
84
|
+
return { ...a, scope };
|
|
85
|
+
}
|
|
86
|
+
function scopedProposeError(err, scope) {
|
|
87
|
+
if (err instanceof WitniumchainApiError && err.status === 409) {
|
|
88
|
+
const body = err.body;
|
|
89
|
+
if (typeof body?.code === "string" && CONFLICT_CODES.has(body.code)) {
|
|
90
|
+
const witnessId = typeof body.witnessId === "string" ? body.witnessId : void 0;
|
|
91
|
+
return new WitnessScopeConflictError(err, scope, body.code, witnessId);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return new WitnessProposeError(err, scope);
|
|
95
|
+
}
|
|
96
|
+
function toHex(bytes) {
|
|
97
|
+
let out = "";
|
|
98
|
+
for (const b of bytes) out += b.toString(16).padStart(2, "0");
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
function cryptoRef() {
|
|
102
|
+
const c = globalThis.crypto;
|
|
103
|
+
if (!c || !c.subtle || !c.getRandomValues) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
"WitniumchainClient: globalThis.crypto with subtle + getRandomValues is required for witness scopes. Modern browsers and Node 18+ provide this natively."
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return c;
|
|
109
|
+
}
|
|
16
110
|
|
|
17
111
|
// src/pkce.ts
|
|
18
112
|
var STORAGE_PREFIX = "witniumchain.pkce.";
|
|
@@ -37,17 +131,17 @@ function defaultVerifierStorage() {
|
|
|
37
131
|
}
|
|
38
132
|
function generateCodeVerifier() {
|
|
39
133
|
const bytes = new Uint8Array(64);
|
|
40
|
-
|
|
134
|
+
cryptoRef2().getRandomValues(bytes);
|
|
41
135
|
return base64UrlEncode(bytes);
|
|
42
136
|
}
|
|
43
137
|
async function deriveCodeChallenge(verifier) {
|
|
44
138
|
const data = new TextEncoder().encode(verifier);
|
|
45
|
-
const digest = await
|
|
139
|
+
const digest = await cryptoRef2().subtle.digest("SHA-256", data);
|
|
46
140
|
return base64UrlEncode(new Uint8Array(digest));
|
|
47
141
|
}
|
|
48
142
|
function generateState() {
|
|
49
143
|
const bytes = new Uint8Array(32);
|
|
50
|
-
|
|
144
|
+
cryptoRef2().getRandomValues(bytes);
|
|
51
145
|
return base64UrlEncode(bytes);
|
|
52
146
|
}
|
|
53
147
|
function base64UrlEncode(bytes) {
|
|
@@ -56,7 +150,7 @@ function base64UrlEncode(bytes) {
|
|
|
56
150
|
const b64 = typeof btoa === "function" ? btoa(binary) : Buffer.from(binary, "binary").toString("base64");
|
|
57
151
|
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
58
152
|
}
|
|
59
|
-
function
|
|
153
|
+
function cryptoRef2() {
|
|
60
154
|
const c = globalThis.crypto;
|
|
61
155
|
if (!c || !c.subtle || !c.getRandomValues) {
|
|
62
156
|
throw new Error(
|
|
@@ -135,6 +229,8 @@ var WitniumchainClient = class {
|
|
|
135
229
|
recovery;
|
|
136
230
|
/** The signed-in user's sign-in methods, behind step-up. `client.security.*`. */
|
|
137
231
|
security;
|
|
232
|
+
/** The signed-in member's org: rename it (org admins). `client.organization.*`. */
|
|
233
|
+
organization;
|
|
138
234
|
/** Session-cookie team management (org-admin only). `client.orgUsers.*`. */
|
|
139
235
|
orgUsers;
|
|
140
236
|
/** The home org's API keys (session; org admins create and revoke). `client.apiKeys.*`. */
|
|
@@ -169,6 +265,7 @@ var WitniumchainClient = class {
|
|
|
169
265
|
this.enrollment = new Enrollment(this);
|
|
170
266
|
this.recovery = new Recovery(this);
|
|
171
267
|
this.security = new AccountSecurity(this);
|
|
268
|
+
this.organization = new Organization(this);
|
|
172
269
|
this.orgUsers = new OrgMembers(this);
|
|
173
270
|
this.apiKeys = new OrgApiKeys(this);
|
|
174
271
|
this.dashboardKey = new DashboardKey(this);
|
|
@@ -251,6 +348,9 @@ var WitniumchainClient = class {
|
|
|
251
348
|
expectNoContent: true
|
|
252
349
|
});
|
|
253
350
|
}
|
|
351
|
+
renameOrg(body) {
|
|
352
|
+
return this.req("PATCH", "/v1/orgs/me", { auth: "SessionCookie", body });
|
|
353
|
+
}
|
|
254
354
|
listMembers() {
|
|
255
355
|
return this.req("GET", "/v1/orgs/me/members", { auth: "SessionCookie" });
|
|
256
356
|
}
|
|
@@ -522,29 +622,24 @@ var WitniumchainClient = class {
|
|
|
522
622
|
{ auth: "Public" }
|
|
523
623
|
);
|
|
524
624
|
}
|
|
525
|
-
// ────────────────────────────────────────────────────────────────────────
|
|
526
|
-
// v5 Witnesses (/v5/contracts/{addr}/witnesses/*)
|
|
527
|
-
//
|
|
528
|
-
// The v5 write surface is a metered proxy in accounts: every billable
|
|
529
|
-
// call (propose, revoke) reserves a credit; sign/finalize forward to
|
|
530
|
-
// chain-api with the admin token. Auth is OAuth Bearer (the URL
|
|
531
|
-
// contract must match the user's bound contract), or an org API key for
|
|
532
|
-
// propose, sign and finalize (its org's active contract).
|
|
533
|
-
//
|
|
534
|
-
// The propose/revoke methods default Idempotency-Key to a stable hash
|
|
535
|
-
// of the request body so an application-level retry with the same
|
|
536
|
-
// arguments dedupes against the original reservation instead of
|
|
537
|
-
// burning a second credit. Pass an explicit `idempotencyKey` to
|
|
538
|
-
// override (e.g. if you genuinely want two witnesses for the same
|
|
539
|
-
// body).
|
|
540
|
-
// ────────────────────────────────────────────────────────────────────────
|
|
541
625
|
async proposeWitnessV5(contractAddress, body, idempotencyKey) {
|
|
542
|
-
const
|
|
543
|
-
|
|
544
|
-
"
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
626
|
+
const path = `/v5/contracts/${encodeURIComponent(contractAddress)}/witnesses/propose`;
|
|
627
|
+
if (idempotencyKey !== void 0) {
|
|
628
|
+
if (idempotencyKey.trim() === "") {
|
|
629
|
+
throw new Error("proposeWitnessV5: idempotencyKey must not be empty (pass a `scope` in the body instead)");
|
|
630
|
+
}
|
|
631
|
+
if (body.scope !== void 0) {
|
|
632
|
+
throw new Error("proposeWitnessV5: pass either a `scope` in the body or the deprecated idempotencyKey, not both");
|
|
633
|
+
}
|
|
634
|
+
return this.req("POST", path, { auth: this.writeAuth(), body, headers: { "idempotency-key": idempotencyKey } });
|
|
635
|
+
}
|
|
636
|
+
const scope = resolveWitnessScope(body.scope);
|
|
637
|
+
try {
|
|
638
|
+
const answer = await this.req("POST", path, { auth: this.writeAuth(), body: { ...body, scope } });
|
|
639
|
+
return scopedProposeResult(answer, scope);
|
|
640
|
+
} catch (err) {
|
|
641
|
+
throw scopedProposeError(err, scope);
|
|
642
|
+
}
|
|
548
643
|
}
|
|
549
644
|
/**
|
|
550
645
|
* What happened to an intent whose propose or finalize answer was lost
|
|
@@ -1708,6 +1803,21 @@ var OrgApiKeys = class {
|
|
|
1708
1803
|
return this.client.revokeApiKey(id);
|
|
1709
1804
|
}
|
|
1710
1805
|
};
|
|
1806
|
+
var Organization = class {
|
|
1807
|
+
constructor(client) {
|
|
1808
|
+
this.client = client;
|
|
1809
|
+
}
|
|
1810
|
+
client;
|
|
1811
|
+
/**
|
|
1812
|
+
* Rename the org (org admins). This is the display name the dashboard and
|
|
1813
|
+
* invitations show; the billing name on invoices is separate and edited in
|
|
1814
|
+
* the billing portal. Surrounding spaces are dropped; the current name
|
|
1815
|
+
* again is a no-op.
|
|
1816
|
+
*/
|
|
1817
|
+
rename(name) {
|
|
1818
|
+
return this.client.renameOrg({ name });
|
|
1819
|
+
}
|
|
1820
|
+
};
|
|
1711
1821
|
var OrgMembers = class {
|
|
1712
1822
|
constructor(client) {
|
|
1713
1823
|
this.client = client;
|
|
@@ -1907,9 +2017,8 @@ var WitniumchainApiKeyClient = class {
|
|
|
1907
2017
|
sign(body, requestId) {
|
|
1908
2018
|
return this.inner.sign(body, requestId);
|
|
1909
2019
|
}
|
|
1910
|
-
/** Propose a witness on the org's contract; reserves one credit. */
|
|
1911
2020
|
proposeWitnessV5(contractAddress, body, idempotencyKey) {
|
|
1912
|
-
return this.inner.proposeWitnessV5(contractAddress, body, idempotencyKey);
|
|
2021
|
+
return idempotencyKey === void 0 ? this.inner.proposeWitnessV5(contractAddress, body) : this.inner.proposeWitnessV5(contractAddress, body, idempotencyKey);
|
|
1913
2022
|
}
|
|
1914
2023
|
/** Submit the signature for an intent this key proposed. */
|
|
1915
2024
|
signWitnessV5(contractAddress, intentId, body) {
|
|
@@ -2066,9 +2175,12 @@ exports.OauthSessions = OauthSessions;
|
|
|
2066
2175
|
exports.OrgApiKeys = OrgApiKeys;
|
|
2067
2176
|
exports.OrgContract = OrgContract;
|
|
2068
2177
|
exports.OrgMembers = OrgMembers;
|
|
2178
|
+
exports.Organization = Organization;
|
|
2069
2179
|
exports.Recovery = Recovery;
|
|
2070
2180
|
exports.SigningKeys = SigningKeys;
|
|
2071
2181
|
exports.Subscriptions = Subscriptions;
|
|
2182
|
+
exports.WitnessProposeError = WitnessProposeError;
|
|
2183
|
+
exports.WitnessScopeConflictError = WitnessScopeConflictError;
|
|
2072
2184
|
exports.WitniumchainApiError = WitniumchainApiError;
|
|
2073
2185
|
exports.WitniumchainApiKeyClient = WitniumchainApiKeyClient;
|
|
2074
2186
|
exports.WitniumchainChainAdminClient = WitniumchainChainAdminClient;
|
|
@@ -2082,7 +2194,9 @@ exports.generateOwnerKeyFile = generateOwnerKeyFile;
|
|
|
2082
2194
|
exports.ownerKeyFile = ownerKeyFile;
|
|
2083
2195
|
exports.ownerKeyFileName = ownerKeyFileName;
|
|
2084
2196
|
exports.parseOwnerKeyFile = parseOwnerKeyFile;
|
|
2197
|
+
exports.randomWitnessScope = randomWitnessScope;
|
|
2085
2198
|
exports.serializeOwnerKeyFile = serializeOwnerKeyFile;
|
|
2086
2199
|
exports.signWithOwnerKeyFile = signWithOwnerKeyFile;
|
|
2200
|
+
exports.witnessScopeFor = witnessScopeFor;
|
|
2087
2201
|
//# sourceMappingURL=index.js.map
|
|
2088
2202
|
//# sourceMappingURL=index.js.map
|