@peridotvault/peridotbase-sdk 0.0.0-stage → 0.2.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/dist/auth.d.ts +33 -0
- package/dist/auth.js +72 -0
- package/dist/client.d.ts +1149 -0
- package/dist/client.js +6030 -0
- package/dist/ddl.d.ts +31 -0
- package/dist/ddl.js +28 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.js +187 -0
- package/dist/expr.d.ts +127 -0
- package/dist/expr.js +1177 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +28 -0
- package/dist/org-multisig.d.ts +29 -0
- package/dist/org-multisig.js +90 -0
- package/dist/pda.d.ts +95 -0
- package/dist/pda.js +327 -0
- package/dist/sql.d.ts +244 -0
- package/dist/sql.js +1523 -0
- package/dist/tooling.d.ts +68 -0
- package/dist/tooling.js +432 -0
- package/dist/v3.d.ts +357 -0
- package/dist/v3.js +1649 -0
- package/dist/values.d.ts +229 -0
- package/dist/values.js +1362 -0
- package/dist/verify.d.ts +37 -0
- package/dist/verify.js +210 -0
- package/idl/peridotbase_core.json +17404 -0
- package/package.json +43 -4
- package/README.md +0 -3
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT verification helper for application auth adapters.
|
|
3
|
+
*
|
|
4
|
+
* Peridotbase never stores JWT secrets on-chain. An application verifies a JWT
|
|
5
|
+
* off-chain and maps its subject to a wallet; the program still authorizes that
|
|
6
|
+
* wallet as usual. This helper checks RS256 signatures and standard claims.
|
|
7
|
+
*/
|
|
8
|
+
export interface JwtClaims {
|
|
9
|
+
sub?: string;
|
|
10
|
+
iss?: string;
|
|
11
|
+
aud?: string | string[];
|
|
12
|
+
exp?: number;
|
|
13
|
+
nbf?: number;
|
|
14
|
+
[claim: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface JwtVerifyOptions {
|
|
17
|
+
/** JWKS document with RSA public keys. */
|
|
18
|
+
jwks?: {
|
|
19
|
+
keys: JsonWebKey[];
|
|
20
|
+
};
|
|
21
|
+
/** Single RSA public key alternative to `jwks`. */
|
|
22
|
+
publicKey?: JsonWebKey;
|
|
23
|
+
issuer?: string;
|
|
24
|
+
audience?: string;
|
|
25
|
+
/** Clock skew in seconds; defaults to 30. */
|
|
26
|
+
clockSkewSeconds?: number;
|
|
27
|
+
/** Injectable clock for tests; defaults to `Date.now()`. */
|
|
28
|
+
now?: () => number;
|
|
29
|
+
}
|
|
30
|
+
/** Verifies an RS256 JWT and returns its claims. Throws on any failure. */
|
|
31
|
+
export declare function verifyJwt(token: string, options?: JwtVerifyOptions): Promise<JwtClaims>;
|
|
32
|
+
/** Exposed for adapters that only need to read claims after verifying elsewhere. */
|
|
33
|
+
export declare function decodeJwtClaims(token: string): JwtClaims;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* JWT verification helper for application auth adapters.
|
|
4
|
+
*
|
|
5
|
+
* Peridotbase never stores JWT secrets on-chain. An application verifies a JWT
|
|
6
|
+
* off-chain and maps its subject to a wallet; the program still authorizes that
|
|
7
|
+
* wallet as usual. This helper checks RS256 signatures and standard claims.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.verifyJwt = verifyJwt;
|
|
11
|
+
exports.decodeJwtClaims = decodeJwtClaims;
|
|
12
|
+
function base64UrlDecode(input) {
|
|
13
|
+
const padded = input.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((input.length + 3) % 4);
|
|
14
|
+
return Uint8Array.from(Buffer.from(padded, "base64"));
|
|
15
|
+
}
|
|
16
|
+
function decodeJson(part) {
|
|
17
|
+
try {
|
|
18
|
+
return JSON.parse(Buffer.from(base64UrlDecode(part)).toString("utf8"));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new Error("invalid JWT encoding");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Verifies an RS256 JWT and returns its claims. Throws on any failure. */
|
|
25
|
+
async function verifyJwt(token, options = {}) {
|
|
26
|
+
const parts = token.split(".");
|
|
27
|
+
if (parts.length !== 3)
|
|
28
|
+
throw new Error("invalid JWT: expected three segments");
|
|
29
|
+
const [headerPart, payloadPart, signaturePart] = parts;
|
|
30
|
+
const header = decodeJson(headerPart);
|
|
31
|
+
const claims = decodeJson(payloadPart);
|
|
32
|
+
if (header.alg !== "RS256")
|
|
33
|
+
throw new Error(`unsupported JWT alg: ${String(header.alg)}`);
|
|
34
|
+
let jwk = options.publicKey;
|
|
35
|
+
if (!jwk && options.jwks) {
|
|
36
|
+
jwk = options.jwks.keys.find((candidate) => !header.kid || candidate.kid === header.kid);
|
|
37
|
+
}
|
|
38
|
+
if (!jwk)
|
|
39
|
+
throw new Error("no matching JWT verification key");
|
|
40
|
+
const subtle = globalThis.crypto?.subtle;
|
|
41
|
+
if (!subtle)
|
|
42
|
+
throw new Error("SHA-256 verification requires WebCrypto");
|
|
43
|
+
const key = await subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["verify"]);
|
|
44
|
+
const signed = Buffer.from(`${headerPart}.${payloadPart}`, "utf8");
|
|
45
|
+
const valid = await subtle.verify("RSASSA-PKCS1-v1_5", key, base64UrlDecode(signaturePart), signed);
|
|
46
|
+
if (!valid)
|
|
47
|
+
throw new Error("invalid JWT signature");
|
|
48
|
+
const now = Math.floor((options.now?.() ?? Date.now()) / 1000);
|
|
49
|
+
const skew = options.clockSkewSeconds ?? 30;
|
|
50
|
+
if (typeof claims.exp === "number" && now > claims.exp + skew) {
|
|
51
|
+
throw new Error("JWT expired");
|
|
52
|
+
}
|
|
53
|
+
if (typeof claims.nbf === "number" && now + skew < claims.nbf) {
|
|
54
|
+
throw new Error("JWT not yet valid");
|
|
55
|
+
}
|
|
56
|
+
if (options.issuer && claims.iss !== options.issuer) {
|
|
57
|
+
throw new Error("JWT issuer mismatch");
|
|
58
|
+
}
|
|
59
|
+
if (options.audience) {
|
|
60
|
+
const audiences = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
61
|
+
if (!audiences.includes(options.audience))
|
|
62
|
+
throw new Error("JWT audience mismatch");
|
|
63
|
+
}
|
|
64
|
+
return claims;
|
|
65
|
+
}
|
|
66
|
+
/** Exposed for adapters that only need to read claims after verifying elsewhere. */
|
|
67
|
+
function decodeJwtClaims(token) {
|
|
68
|
+
const parts = token.split(".");
|
|
69
|
+
if (parts.length < 2)
|
|
70
|
+
throw new Error("invalid JWT");
|
|
71
|
+
return decodeJson(parts[1]);
|
|
72
|
+
}
|