burnledger 0.2.2 → 0.3.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/cjs/index.browser.d.ts +2 -1
- package/dist/cjs/index.browser.d.ts.map +1 -1
- package/dist/cjs/index.browser.js +7 -1
- package/dist/cjs/index.browser.js.map +1 -1
- package/dist/cjs/index.d.ts +9 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/models.d.ts +28 -1
- package/dist/cjs/models.d.ts.map +1 -1
- package/dist/cjs/models.js +8 -0
- package/dist/cjs/models.js.map +1 -1
- package/dist/cjs/verify.d.ts +55 -1
- package/dist/cjs/verify.d.ts.map +1 -1
- package/dist/cjs/verify.js +414 -104
- package/dist/cjs/verify.js.map +1 -1
- package/dist/cjs/web-verifier.d.ts +29 -0
- package/dist/cjs/web-verifier.d.ts.map +1 -0
- package/dist/cjs/web-verifier.js +70 -0
- package/dist/cjs/web-verifier.js.map +1 -0
- package/dist/esm/cli.d.ts.map +1 -1
- package/dist/esm/cli.js +12 -5
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/index.browser.d.ts +2 -1
- package/dist/esm/index.browser.d.ts.map +1 -1
- package/dist/esm/index.browser.js +3 -0
- package/dist/esm/index.browser.js.map +1 -1
- package/dist/esm/index.d.ts +9 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +13 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/models.d.ts +28 -1
- package/dist/esm/models.d.ts.map +1 -1
- package/dist/esm/models.js +8 -0
- package/dist/esm/models.js.map +1 -1
- package/dist/esm/verify.d.ts +55 -1
- package/dist/esm/verify.d.ts.map +1 -1
- package/dist/esm/verify.js +411 -105
- package/dist/esm/verify.js.map +1 -1
- package/dist/esm/web-verifier.d.ts +29 -0
- package/dist/esm/web-verifier.d.ts.map +1 -0
- package/dist/esm/web-verifier.js +63 -0
- package/dist/esm/web-verifier.js.map +1 -0
- package/package.json +12 -11
- package/src/cli.ts +207 -0
- package/src/client.ts +555 -0
- package/src/crypto-browser.ts +49 -0
- package/src/crypto-node.ts +40 -0
- package/src/crypto.ts +10 -0
- package/src/errors.ts +154 -0
- package/src/http.ts +209 -0
- package/src/index.browser.ts +110 -0
- package/src/index.ts +134 -0
- package/src/keys.ts +18 -0
- package/src/models.ts +558 -0
- package/src/pagination.ts +64 -0
- package/src/verify.ts +956 -0
- package/src/web-verifier.ts +89 -0
- package/src/webhooks.ts +76 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entry point for the public browser verifier at `website/verify/`.
|
|
3
|
+
*
|
|
4
|
+
* Imports the leaf modules directly rather than re-exporting from
|
|
5
|
+
* `index.browser.ts`: that entry pulls in the HTTP client, which imports
|
|
6
|
+
* `node:fs/promises` and cannot be bundled for a browser at all. The page needs
|
|
7
|
+
* four functions, so this exposes four functions — the surface a relying party's
|
|
8
|
+
* browser is handed is then something you can read in one screen instead of
|
|
9
|
+
* inferring from a minified artifact.
|
|
10
|
+
*
|
|
11
|
+
* Built by `scripts/build-web-verifier.mjs`. The bundle this replaces was built
|
|
12
|
+
* by hand once and never regenerated, and had drifted into disagreeing with the
|
|
13
|
+
* Go reference on 24 of 29 fixture cases — accepting relabelled timestamps and
|
|
14
|
+
* an unsigned tree_size, and rejecting genuine certificates with a non-UTC
|
|
15
|
+
* offset (#457). It also contained `ed25519Supported`, which existed in no
|
|
16
|
+
* source file anywhere in the repository.
|
|
17
|
+
*/
|
|
18
|
+
import { browserCrypto } from "./crypto-browser.js";
|
|
19
|
+
import {
|
|
20
|
+
verifyCertificate as _verifyCertificate,
|
|
21
|
+
verifyCertificateWithStatus as _verifyCertificateWithStatus,
|
|
22
|
+
verifyTransparency as _verifyTransparency,
|
|
23
|
+
publicKeyFromHex as _publicKeyFromHex,
|
|
24
|
+
} from "./verify.js";
|
|
25
|
+
import type { PublicKeyInfo } from "./verify.js";
|
|
26
|
+
import type { VerificationResult, TransparencyResult } from "./models.js";
|
|
27
|
+
|
|
28
|
+
type Cert = Record<string, unknown>;
|
|
29
|
+
|
|
30
|
+
/** Verify all signatures on a deletion certificate offline. */
|
|
31
|
+
export function verifyCertificate(
|
|
32
|
+
certificate: Cert,
|
|
33
|
+
publicKeys: Map<string, PublicKeyInfo>,
|
|
34
|
+
): Promise<VerificationResult> {
|
|
35
|
+
return _verifyCertificate(browserCrypto, certificate, publicKeys);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Verify a certificate and, separately, what a signed statement says about its
|
|
40
|
+
* revocation. Returns "VALID_REVOCATION_UNKNOWN" when no fresh statement is
|
|
41
|
+
* supplied — never "VALID", because "not revoked" is not something an offline
|
|
42
|
+
* check can establish on its own.
|
|
43
|
+
*/
|
|
44
|
+
export function verifyCertificateWithStatus(
|
|
45
|
+
certificate: Cert,
|
|
46
|
+
publicKeys: Map<string, PublicKeyInfo>,
|
|
47
|
+
status?: Record<string, unknown> | null,
|
|
48
|
+
now?: Date,
|
|
49
|
+
): Promise<string> {
|
|
50
|
+
return _verifyCertificateWithStatus(browserCrypto, certificate, publicKeys, status, now);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Verify the transparency proof embedded in a certificate. */
|
|
54
|
+
export function verifyTransparency(
|
|
55
|
+
certificate: Cert,
|
|
56
|
+
publicKeys: Map<string, PublicKeyInfo>,
|
|
57
|
+
): Promise<TransparencyResult> {
|
|
58
|
+
return _verifyTransparency(browserCrypto, certificate, publicKeys);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Construct a PublicKeyInfo from a hex-encoded Ed25519 public key. */
|
|
62
|
+
export function publicKeyFromHex(
|
|
63
|
+
hexKey: string,
|
|
64
|
+
opts?: { revoked?: boolean },
|
|
65
|
+
): Promise<PublicKeyInfo> {
|
|
66
|
+
return _publicKeyFromHex(browserCrypto, hexKey, opts);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Report whether this browser's WebCrypto can verify Ed25519, so the page can
|
|
71
|
+
* say "your browser cannot check this" rather than failing silently.
|
|
72
|
+
*
|
|
73
|
+
* Feature-detected by importing a well-formed SPKI key rather than sniffing the
|
|
74
|
+
* user agent: support arrived at different versions across engines, and a
|
|
75
|
+
* capability question deserves a capability answer.
|
|
76
|
+
*/
|
|
77
|
+
export async function ed25519Supported(): Promise<boolean> {
|
|
78
|
+
try {
|
|
79
|
+
const spki = new Uint8Array([
|
|
80
|
+
0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
|
|
81
|
+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
|
82
|
+
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
|
|
83
|
+
]);
|
|
84
|
+
await crypto.subtle.importKey("spki", spki.buffer as ArrayBuffer, { name: "Ed25519" }, false, ["verify"]);
|
|
85
|
+
return true;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/webhooks.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** Webhook signature verification for incoming BurnLedger events.
|
|
2
|
+
*
|
|
3
|
+
* The server signs webhook payloads with HMAC-SHA256 using the webhook secret.
|
|
4
|
+
* The signature is sent in the X-BurnLedger-Signature header as a hex string,
|
|
5
|
+
* and the timestamp in the X-BurnLedger-Timestamp header as an RFC3339 string.
|
|
6
|
+
*
|
|
7
|
+
* CR-M01: the signed preimage is `timestamp + "." + body`, where `timestamp` is
|
|
8
|
+
* the exact RFC3339 string from the X-BurnLedger-Timestamp header. Binding the
|
|
9
|
+
* timestamp into the MAC prevents a captured delivery from being replayed under
|
|
10
|
+
* a different timestamp. Receivers MUST also verify the timestamp is fresh (see
|
|
11
|
+
* isTimestampFresh) — the signature proves authenticity, freshness prevents
|
|
12
|
+
* replay of an old-but-validly-signed delivery.
|
|
13
|
+
*
|
|
14
|
+
* Node-only — webhooks are received server-side.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Verify that a webhook payload was signed by the expected secret.
|
|
21
|
+
*
|
|
22
|
+
* @param secret The webhook secret (raw UTF-8, as returned by the API)
|
|
23
|
+
* @param timestamp The RFC3339 timestamp from the X-BurnLedger-Timestamp header
|
|
24
|
+
* @param body The raw request body bytes
|
|
25
|
+
* @param signature The hex-encoded HMAC-SHA256 signature from the X-BurnLedger-Signature header
|
|
26
|
+
* @returns true if the signature is valid
|
|
27
|
+
*/
|
|
28
|
+
export function verifyWebhookSignature(
|
|
29
|
+
secret: string,
|
|
30
|
+
timestamp: string,
|
|
31
|
+
body: Uint8Array | string,
|
|
32
|
+
signature: string,
|
|
33
|
+
): boolean {
|
|
34
|
+
if (secret.length === 0) return false;
|
|
35
|
+
if (timestamp.length === 0) return false;
|
|
36
|
+
if (signature.length === 0) return false;
|
|
37
|
+
|
|
38
|
+
// Preimage: timestamp + "." + body. An RFC3339 timestamp never contains a
|
|
39
|
+
// ".", so the field boundary is unambiguous.
|
|
40
|
+
const bodyBytes =
|
|
41
|
+
typeof body === "string" ? Buffer.from(body, "utf-8") : Buffer.from(body);
|
|
42
|
+
const expected = createHmac("sha256", secret)
|
|
43
|
+
.update(Buffer.from(`${timestamp}.`, "utf-8"))
|
|
44
|
+
.update(bodyBytes)
|
|
45
|
+
.digest();
|
|
46
|
+
|
|
47
|
+
let received: Buffer;
|
|
48
|
+
try {
|
|
49
|
+
received = Buffer.from(signature, "hex");
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (received.length !== expected.length) return false;
|
|
55
|
+
|
|
56
|
+
return timingSafeEqual(expected, received);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Verify that the webhook timestamp is within an acceptable freshness window.
|
|
61
|
+
* Receivers MUST call this (in addition to verifyWebhookSignature) to prevent
|
|
62
|
+
* replay of an old-but-validly-signed delivery.
|
|
63
|
+
*
|
|
64
|
+
* @param timestamp The RFC3339 timestamp from the X-BurnLedger-Timestamp header
|
|
65
|
+
* @param toleranceSeconds Maximum accepted age/skew in seconds (default 300)
|
|
66
|
+
* @returns true if the timestamp parses and is within +/- tolerance of now
|
|
67
|
+
*/
|
|
68
|
+
export function isTimestampFresh(
|
|
69
|
+
timestamp: string,
|
|
70
|
+
toleranceSeconds: number = 300,
|
|
71
|
+
): boolean {
|
|
72
|
+
const parsed = Date.parse(timestamp);
|
|
73
|
+
if (Number.isNaN(parsed)) return false;
|
|
74
|
+
const ageSeconds = Math.abs(Date.now() - parsed) / 1000;
|
|
75
|
+
return ageSeconds <= toleranceSeconds;
|
|
76
|
+
}
|