corvuszk 1.1.0 → 1.1.1

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 CHANGED
@@ -35,7 +35,7 @@
35
35
  |---|---|
36
36
  | [`contracts/`](contracts/) | The four Compact contracts: `eligibility`, `condition`, `age_over`, `balance_above` |
37
37
  | [`midnight/witnesses.ts`](midnight/witnesses.ts) | Typed witness implementations (private inputs) for all four contracts, checked against the compiler's generated types |
38
- | [`midnight/managed/`](midnight/managed/) | Compiler output: TypeScript contract API, ZKIR circuits, proving and verifying keys |
38
+ | [`midnight/managed/`](midnight/managed/) | Compiler output: TypeScript contract API, ZKIR circuits, proving and verifying keys (the large proving keys are in the repo only, not the npm package) |
39
39
  | [`midnight/eligibility.mjs`](midnight/eligibility.mjs) | Mock issuer, Schnorr credential, XRPL preimage binding, `fulfillmentFromRevealedLedger` |
40
40
  | [`midnight/sdk-bridge.mjs`](midnight/sdk-bridge.mjs), [`providers.mjs`](midnight/providers.mjs), [`wallet9.mjs`](midnight/wallet9.mjs) | Midnight.js provider stack: wallet, proof server, indexer, deploy, `callTx`, ledger reads |
41
41
  | [`midnight/live-eligibility.mjs`](midnight/live-eligibility.mjs) | The end-to-end script behind the 12 Nov run (`npm run live:eligibility`) |
@@ -127,7 +127,7 @@ Short version. The full write-up, with tables for each check and replay case, is
127
127
 
128
128
  ## Who it is for
129
129
 
130
- TypeScript builders shipping small XRPL apps that need a private yes/no before releasing escrowed XRP: age-gated goods, proof-of-funds style checks, "condition met" attestations, without becoming a custodial wallet. Why Midnight: XRPL has native escrow but no private programmable checks; Midnight's Compact contracts add those without moving the money off XRPL. Comparisons with XRPL Credentials, Xahau Hooks, Boundless Smart Escrows and other Midnight projects, plus fee notes, are in [docs/alternatives-and-costs.md](docs/alternatives-and-costs.md).
130
+ TypeScript builders shipping small XRPL apps that need a private yes/no before releasing escrowed XRP: age-gated goods, proof-of-funds style checks, "condition met" attestations, without becoming a custodial wallet. Why Midnight: XRPL escrows have no private checks today. XRPL's own Confidential Transfers are a separate feature that does not work with escrows and was not enabled on mainnet as of 27 Nov 2026 ([xrpl.org](https://xrpl.org/), re-check before relying on it). Midnight's Compact contracts add private checks without moving the money off XRPL. To run the Midnight half, an XRPL builder must hold some NIGHT so a wallet can generate DUST, which pays Midnight fees. Per IO, Midnight mainnet is in a federated phase as of 23 Nov 2026 (re-check). Comparisons with XRPL Credentials, Xahau Hooks, Boundless Smart Escrows and other Midnight projects, plus fee notes, are in [docs/alternatives-and-costs.md](docs/alternatives-and-costs.md).
131
131
 
132
132
  Docs: [security notice](SECURITY-NOTICE.md) · [getting started](docs/getting-started.md) · [what's included](WHAT-YOU-GET.md) · [the proof templates](docs/templates.md) · [XRPL escrow flow](docs/xrpl-escrow-flow.md) · [live eligibility run how-to](docs/live-eligibility.md) · [on-chain submit](docs/on-chain-submit.md) · [proof server](docs/proof-server.md) · [SECURITY.md](SECURITY.md) · [CHANGELOG](CHANGELOG.md)
133
133
 
@@ -200,7 +200,7 @@ Docs for chain submit: [on-chain submit](docs/on-chain-submit.md) · [proof serv
200
200
  4. For **on-chain submit** against contracts already in `midnight/deployments.preprod.json`: start the proof server (see [docs/proof-server.md](docs/proof-server.md)), ensure the deployer wallet has DUST, then `backend.submitOnChain(...)` or `cd midnight && node submit-onchain.mjs --template age_over`. For a **fresh deploy**, use `midnight/deploy.mjs` / `ensure-pipeline.sh` (longer; registers DUST if needed). Faucet: https://midnight-tmnight-preprod.nethermind.dev/
201
201
 
202
202
  ## Licence
203
- Apache-2.0. Dependencies: xrpl.js (ISC), five-bells-condition (Apache-2.0), Compact/Midnight.js (Apache-2.0, optional under `midnight/`). Not affiliated with Ripple, the XRPL Foundation or the Midnight Foundation.
203
+ Apache-2.0. Dependencies: xrpl.js (ISC; its only runtime dependency tree), Compact/Midnight.js (Apache-2.0, optional under `midnight/`). Not affiliated with Ripple, the XRPL Foundation or the Midnight Foundation.
204
204
 
205
205
  ## Buildathon Wave 3 materials
206
206
  Pitch deck, demo script, and what was built during Wave 3: [docs/buildathon/](docs/buildathon/).
package/WHAT-YOU-GET.md CHANGED
@@ -9,7 +9,7 @@ Honest inventory of this toolkit as of **13 Nov 2026** (four Compact contracts l
9
9
  | TypeScript SDK (`src/`, builds to `dist/`) | Non-custodial. Never holds funds, keys, or fulfillments. |
10
10
  | Four Compact contracts | `condition`, `balance_above`, `age_over`, and `eligibility` under `contracts/` |
11
11
  | Eligibility template | `contracts/eligibility.compact`: mock Schnorr issuer, one-time nullifier bound to XRPL owner, offer sequence, destination, and preimage commitment. Run live on Preprod ledger 9 with an XRPL testnet escrow release on 12 Nov 2026 ([receipts/ledger9-preprod-2026-11-12.md](receipts/ledger9-preprod-2026-11-12.md)); first live run on ledger 8, 6 Nov 2026 ([docs/live-run.md](docs/live-run.md)). |
12
- | Compiled circuit artifacts | `midnight/managed/` (needed for `npm run simulate` and `MidnightBackend`) |
12
+ | Compiled circuit artifacts | `midnight/managed/` contract code, ZKIR, compiler info and the small verifier keys are in the npm package. The proving keys (about 200 MB) and `midnight/package.json` are **not**; get them from the GitHub repository (clone it, then `npm ci && (cd midnight && npm ci)`) for `npm run simulate` and real proving. |
13
13
  | Live Midnight Preprod contracts (ledger 9, deployed 11–12 Nov 2026) | Addresses and tx hashes in `midnight/deployments.preprod.json` and the links below; ledger 8 history (3 Nov) under `ledger8History` |
14
14
  | Live Midnight **mainnet** contracts (13 Nov 2026) | All four contracts. Addresses and proof transactions in `midnight/deployments.mainnet.json` and the README deployed-contracts table. Unaudited. Not for real value. The mainnet `eligibility` instance is a demonstration only (mock issuer, no real XRPL escrow, no `reveal`). |
15
15
  | **On-chain submit from the SDK** | `MidnightBackend.submitOnChain` → `midnight/sdk-bridge.mjs` `submitCallTx` (bounded wallet sync; no redeploy) |
@@ -0,0 +1,13 @@
1
+ /** Largest preimage accepted. XRPL limits a fulfillment to 256 bytes; this is a generous parser bound. */
2
+ export declare const MAX_PREIMAGE_BYTES = 65535;
3
+ /** Condition + fulfillment (uppercase hex, the form XRPL transactions use) for a preimage. */
4
+ export declare function preimageSha256(preimage: Uint8Array): {
5
+ condition: string;
6
+ fulfillment: string;
7
+ };
8
+ /** Extract the preimage from a PREIMAGE-SHA-256 fulfillment. Throws on anything else. */
9
+ export declare function preimageFromFulfillment(fulfillment: Uint8Array | string): Buffer;
10
+ /** Condition (uppercase hex) that a fulfillment satisfies. */
11
+ export declare function conditionFromFulfillment(fulfillment: Uint8Array | string): string;
12
+ /** True only if the fulfillment is a well-formed PREIMAGE-SHA-256 fulfillment for exactly this condition. */
13
+ export declare function validateFulfillment(fulfillment: Uint8Array | string, condition: Uint8Array | string): boolean;
@@ -0,0 +1,105 @@
1
+ /**
2
+ * PREIMAGE-SHA-256 crypto-conditions (draft-thomas-crypto-conditions, as used by XRPL escrows).
3
+ * Replaces the five-bells-condition dependency: CorvusZK only needs this one condition type,
4
+ * and node:crypto plus a minimal DER encoder covers it with no transitive packages.
5
+ *
6
+ * fulfillment = A0 len( 80 len(preimage) preimage )
7
+ * condition = A0 len( 80 20 sha256(preimage) 81 len(cost) cost ) cost = preimage length in bytes
8
+ *
9
+ * Parsing is strict: only definite, minimal DER lengths; no trailing bytes; no other types.
10
+ */
11
+ import crypto from 'node:crypto';
12
+ /** Largest preimage accepted. XRPL limits a fulfillment to 256 bytes; this is a generous parser bound. */
13
+ export const MAX_PREIMAGE_BYTES = 65_535;
14
+ function derLength(n) {
15
+ if (n < 0x80)
16
+ return Buffer.from([n]);
17
+ if (n <= 0xff)
18
+ return Buffer.from([0x81, n]);
19
+ if (n <= 0xffff)
20
+ return Buffer.from([0x82, n >> 8, n & 0xff]);
21
+ throw new RangeError('crypto-condition: length too large');
22
+ }
23
+ function tlv(tag, body) {
24
+ return Buffer.concat([Buffer.from([tag]), derLength(body.length), body]);
25
+ }
26
+ /** DER INTEGER content for a non-negative number: minimal big-endian, with a 0x00 sign byte when the top bit is set. */
27
+ function uintBytes(n) {
28
+ if (n === 0)
29
+ return Buffer.from([0]);
30
+ const out = [];
31
+ while (n > 0) {
32
+ out.unshift(n & 0xff);
33
+ n = Math.floor(n / 256);
34
+ }
35
+ if (out[0] & 0x80)
36
+ out.unshift(0);
37
+ return Buffer.from(out);
38
+ }
39
+ function readTlv(buf, at) {
40
+ if (at + 2 > buf.length)
41
+ throw new Error('crypto-condition: truncated');
42
+ const tag = buf[at];
43
+ let len = buf[at + 1];
44
+ let p = at + 2;
45
+ if (len & 0x80) {
46
+ const nb = len & 0x7f;
47
+ if (nb === 0 || nb > 2 || p + nb > buf.length)
48
+ throw new Error('crypto-condition: bad length');
49
+ len = 0;
50
+ for (let i = 0; i < nb; i++)
51
+ len = len * 256 + buf[p + i];
52
+ if ((nb === 1 && len < 0x80) || (nb === 2 && len <= 0xff))
53
+ throw new Error('crypto-condition: non-minimal length');
54
+ p += nb;
55
+ }
56
+ if (p + len > buf.length)
57
+ throw new Error('crypto-condition: truncated');
58
+ return { tag, body: buf.subarray(p, p + len), end: p + len };
59
+ }
60
+ function toBuf(x, what) {
61
+ if (typeof x === 'string') {
62
+ if (!/^([0-9a-fA-F]{2})*$/.test(x))
63
+ throw new Error(`crypto-condition: ${what} must be hex`);
64
+ return Buffer.from(x, 'hex');
65
+ }
66
+ return Buffer.from(x);
67
+ }
68
+ /** Condition + fulfillment (uppercase hex, the form XRPL transactions use) for a preimage. */
69
+ export function preimageSha256(preimage) {
70
+ const p = Buffer.from(preimage);
71
+ if (p.length > MAX_PREIMAGE_BYTES)
72
+ throw new RangeError('crypto-condition: preimage too long');
73
+ const fulfillment = tlv(0xa0, tlv(0x80, p));
74
+ const hash = crypto.createHash('sha256').update(p).digest();
75
+ const condition = tlv(0xa0, Buffer.concat([tlv(0x80, hash), tlv(0x81, uintBytes(p.length))]));
76
+ return { condition: condition.toString('hex').toUpperCase(), fulfillment: fulfillment.toString('hex').toUpperCase() };
77
+ }
78
+ /** Extract the preimage from a PREIMAGE-SHA-256 fulfillment. Throws on anything else. */
79
+ export function preimageFromFulfillment(fulfillment) {
80
+ const buf = toBuf(fulfillment, 'fulfillment');
81
+ const outer = readTlv(buf, 0);
82
+ if (outer.tag !== 0xa0 || outer.end !== buf.length)
83
+ throw new Error('crypto-condition: not a PREIMAGE-SHA-256 fulfillment');
84
+ const inner = readTlv(outer.body, 0);
85
+ if (inner.tag !== 0x80 || inner.end !== outer.body.length)
86
+ throw new Error('crypto-condition: malformed fulfillment');
87
+ if (inner.body.length > MAX_PREIMAGE_BYTES)
88
+ throw new Error('crypto-condition: preimage too long');
89
+ return Buffer.from(inner.body);
90
+ }
91
+ /** Condition (uppercase hex) that a fulfillment satisfies. */
92
+ export function conditionFromFulfillment(fulfillment) {
93
+ return preimageSha256(preimageFromFulfillment(fulfillment)).condition;
94
+ }
95
+ /** True only if the fulfillment is a well-formed PREIMAGE-SHA-256 fulfillment for exactly this condition. */
96
+ export function validateFulfillment(fulfillment, condition) {
97
+ try {
98
+ const want = toBuf(condition, 'condition');
99
+ const got = Buffer.from(conditionFromFulfillment(fulfillment), 'hex');
100
+ return got.length === want.length && crypto.timingSafeEqual(got, want);
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
package/dist/index.d.ts CHANGED
@@ -313,8 +313,8 @@ export declare function feesFromMidnightReceipt(pub: {
313
313
  * The bound path is midnight/eligibility.mjs (see README "Security model").
314
314
  */
315
315
  export declare function createCryptoCondition(): {
316
- condition: any;
317
- fulfillment: any;
316
+ condition: string;
317
+ fulfillment: string;
318
318
  };
319
319
  export declare function createEscrow(client: xrpl.Client, payer: xrpl.Wallet, o: {
320
320
  destination: string;
@@ -361,3 +361,4 @@ export * from './midnight-config.js';
361
361
  export * from './intent.js';
362
362
  export * from './registry.js';
363
363
  export * from './strict-release.js';
364
+ export * from './crypto-condition.js';
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@ import crypto from 'node:crypto';
15
15
  import path from 'node:path';
16
16
  import { fileURLToPath, pathToFileURL } from 'node:url';
17
17
  import * as xrpl from 'xrpl';
18
- import cc from 'five-bells-condition';
18
+ import { preimageSha256 } from './crypto-condition.js';
19
19
  import { preprodConfig } from './midnight-config.js';
20
20
  export const EVIDENCE_KINDS = [
21
21
  'simulation-attestation',
@@ -605,13 +605,7 @@ export function feesFromMidnightReceipt(pub) {
605
605
  * The bound path is midnight/eligibility.mjs (see README "Security model").
606
606
  */
607
607
  export function createCryptoCondition() {
608
- const preimage = crypto.randomBytes(32);
609
- const f = new cc.PreimageSha256();
610
- f.setPreimage(preimage);
611
- return {
612
- condition: f.getConditionBinary().toString('hex').toUpperCase(),
613
- fulfillment: f.serializeBinary().toString('hex').toUpperCase(),
614
- };
608
+ return preimageSha256(crypto.randomBytes(32));
615
609
  }
616
610
  export async function createEscrow(client, payer, o) {
617
611
  const now = xrpl.isoTimeToRippleTime(new Date().toISOString());
@@ -662,3 +656,4 @@ export * from './midnight-config.js';
662
656
  export * from './intent.js';
663
657
  export * from './registry.js';
664
658
  export * from './strict-release.js';
659
+ export * from './crypto-condition.js';
@@ -13,7 +13,7 @@
13
13
  * Retries are idempotent: a settled intent is never submitted again. XRPL consumes an escrow
14
14
  * once, so a duplicate EscrowFinish cannot pay twice; an unknown outcome returns `reconcile`.
15
15
  */
16
- import cc from 'five-bells-condition';
16
+ import { preimageSha256 } from './crypto-condition.js';
17
17
  import * as xrpl from 'xrpl';
18
18
  import { normalizeIntentV2, intentHashV2 } from './intent.js';
19
19
  import { RegistryConflict } from './registry.js';
@@ -96,11 +96,10 @@ export async function releaseStrict(o) {
96
96
  const pre = String(ledger.revealedPreimage ?? '');
97
97
  if (!/^[0-9a-f]{64}$/.test(pre))
98
98
  return res('refused', 'no-revealed-preimage');
99
- const f = new cc.PreimageSha256();
100
- f.setPreimage(Buffer.from(pre, 'hex'));
101
- if (f.getConditionBinary().toString('hex').toUpperCase() !== intent.condition)
99
+ const opened = preimageSha256(Buffer.from(pre, 'hex'));
100
+ if (opened.condition !== intent.condition)
102
101
  return res('refused', 'revealed-preimage-does-not-open-condition');
103
- const fulfillment = f.serializeBinary().toString('hex').toUpperCase();
102
+ const fulfillment = opened.fulfillment;
104
103
  // One credential, one intent (CZ-008).
105
104
  const credNull = String(ledger.credentialNullifier ?? '');
106
105
  if (!/^[0-9a-f]{64}$/.test(credNull) || /^0+$/.test(credNull))
@@ -10,6 +10,12 @@ Funds never sit with CorvusZK. Secrets and fulfillments stay with the caller.
10
10
 
11
11
  All four Compact contracts are live on Midnight mainnet (13 Nov 2026). Unaudited. Not for real value. The eligibility contract already deployed on Midnight mainnet uses the insecure demo issuer and must not be used with real value ([SECURITY-NOTICE.md](../SECURITY-NOTICE.md)). The full eligibility prove → reveal → XRPL EscrowFinish path has been proven on Midnight Preprod + XRPL testnet only. Addresses and explorer links are in the [README deployed-contracts table](../README.md#deployed-contracts).
12
12
 
13
+ ## Before you use Midnight
14
+
15
+ - **You need NIGHT.** An XRPL builder who runs the Midnight half must hold some NIGHT so a wallet can generate DUST, which pays Midnight fees. There is no pay-with-XRP option. The offline backend needs none of this.
16
+ - **Network stage.** Per IO, Midnight mainnet is in a federated phase (checked 27 Nov 2026; re-check). Do not read it as a statement about how decentralized the network is.
17
+ - **XRPL privacy today.** XRPL Confidential Transfers exist but do not work with escrows and were not enabled on mainnet as of 27 Nov 2026.
18
+
13
19
  ## Pick a backend
14
20
 
15
21
  | Backend | ZK? | Needs | Use for |
@@ -37,11 +43,16 @@ One test creates and finishes a real XRPL **testnet** escrow after a simulation
37
43
 
38
44
  ## Step 3 — real Compact circuits locally
39
45
 
46
+ This step needs the full repository, not the npm package. The npm package is lean (about 1.7 MB): it leaves out the Midnight proving keys (`*.prover`, about 200 MB) and `midnight/package.json`. The small verifier keys are included. To run real circuits or prove on Midnight:
47
+
40
48
  ```bash
41
- cd midnight && npm ci && cd ..
49
+ git clone https://github.com/CorvusZK-1/CorvusZK && cd CorvusZK
50
+ npm ci && (cd midnight && npm ci)
42
51
  npm run simulate
43
52
  ```
44
53
 
54
+ The proving keys are in `midnight/managed/*/keys/` in the repository. You can also regenerate them with `npm run compile:contracts` (needs the Compact toolchain).
55
+
45
56
  Then in code:
46
57
 
47
58
  ```ts
@@ -4,7 +4,7 @@ This section answers four questions as the code behaves today. There are two rel
4
4
 
5
5
  ## 1. How are Midnight proofs connected to XRPL escrow conditions?
6
6
 
7
- **Bound path.** `samplePreimage(role)` in `midnight/eligibility.mjs` samples 32 random bytes. The XRPL condition is the PREIMAGE-SHA-256 crypto-condition of those bytes (`five-bells-condition`). The Compact constructor stores `persistentHash("corvuszk:preimage:v1" || preimage)`, the escrow owner, the offer sequence, and the destination. It does not store the preimage. Owner and destination are the 20-byte XRPL account id in the first 20 bytes of a 32-byte value, with the rest zero. Sequence is a `Uint<32>`.
7
+ **Bound path.** `samplePreimage(role)` in `midnight/eligibility.mjs` samples 32 random bytes. The XRPL condition is the PREIMAGE-SHA-256 crypto-condition of those bytes (`src/crypto-condition.ts`, node:crypto only). The Compact constructor stores `persistentHash("corvuszk:preimage:v1" || preimage)`, the escrow owner, the offer sequence, and the destination. It does not store the preimage. Owner and destination are the 20-byte XRPL account id in the first 20 bytes of a 32-byte value, with the rest zero. Sequence is a `Uint<32>`.
8
8
 
9
9
  Who samples the preimage is a mode:
10
10
 
@@ -12,7 +12,7 @@ Releaser reads `released` from the Midnight indexer ──EscrowFinish(Fulfillme
12
12
 
13
13
  1. **Crypto-condition.** `createCryptoCondition()` makes a PREIMAGE-SHA-256 pair: the public `condition` and the secret `fulfillment`. Whoever holds the fulfillment can finish the escrow, so keep it with the releaser.
14
14
  2. **Lock funds.** `createEscrow(client, buyer, { destination, amountXrp, condition, cancelAfterSec })`. XRPL itself enforces that the money can only go to `destination`, or back to the buyer after `CancelAfter`.
15
- 3. **Prove.** The attester calls `attest()` on the Midnight `condition` contract. Midnight only accepts the transaction if the zero-knowledge proof verifies, so `released` can only become `true` honestly.
15
+ 3. **Prove.** The attester calls `attest()` on the Midnight `condition` contract. Midnight only accepts the transaction if the zero-knowledge proof verifies, so `released` can only become `true` when the attester key holder says so. The `deliveryOk()` input is a witness the caller supplies, so the proof shows that the key holder attested delivery, not that delivery objectively happened. See the trust model below.
16
16
  4. **Check and release.** The releaser reads the contract's public state (`queryContractState`) and submits `EscrowFinish` with the fulfillment only if `released === true` (see `midnight/e2e-escrow.mjs`). With the simulation backend, `verifyAndReleaseEscrow()` does the same check against a signed attestation.
17
17
  5. **Fallback.** If nothing is proven before `CancelAfter`, the buyer cancels and gets the XRP back.
18
18
 
@@ -19,7 +19,7 @@ import crypto from 'node:crypto';
19
19
  import path from 'node:path';
20
20
  import { pathToFileURL } from 'node:url';
21
21
  import * as rt from '@midnight-ntwrk/compact-runtime';
22
- import cc from 'five-bells-condition';
22
+ import { preimageSha256 } from '../dist/crypto-condition.js';
23
23
  import { decodeAccountID } from 'xrpl';
24
24
 
25
25
  export const MOCK_ISSUER_LABEL =
@@ -255,13 +255,7 @@ export function issueMockCredential(issuer, o) {
255
255
  }
256
256
 
257
257
  export function xrplConditionFromPreimage(preimage) {
258
- const raw = bytes32(preimage);
259
- const f = new cc.PreimageSha256();
260
- f.setPreimage(Buffer.from(raw));
261
- return {
262
- condition: f.getConditionBinary().toString('hex').toUpperCase(),
263
- fulfillment: f.serializeBinary().toString('hex').toUpperCase(),
264
- };
258
+ return preimageSha256(bytes32(preimage));
265
259
  }
266
260
 
267
261
  const TRUST = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corvuszk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Non-custodial TypeScript SDK + Compact templates for proof-gated XRPL escrows on Midnight",
5
5
  "main": "dist/index.js",
6
6
  "directories": {
@@ -16,7 +16,7 @@
16
16
  "example:offline": "node examples/escrow-offline.mjs",
17
17
  "example:eligibility": "node examples/eligibility-release.mjs",
18
18
  "live:eligibility": "npm run build && node midnight/live-eligibility.mjs",
19
- "test:offline": "node --test test/unit.test.mjs test/eligibility.test.mjs test/templates.test.mjs test/adversarial/*.test.mjs test/lifecycle/*.test.mjs",
19
+ "test:offline": "node --test test/docs-wording.test.mjs test/unit.test.mjs test/crypto-condition.test.mjs test/eligibility.test.mjs test/templates.test.mjs test/adversarial/*.test.mjs test/lifecycle/*.test.mjs",
20
20
  "test:lifecycle": "node --test test/lifecycle/*.test.mjs",
21
21
  "test:xrpl": "node --test test/xrpl.integration.test.mjs",
22
22
  "typecheck:witnesses": "tsc -p midnight/tsconfig.json",
@@ -40,7 +40,6 @@
40
40
  "license": "Apache-2.0",
41
41
  "type": "module",
42
42
  "dependencies": {
43
- "five-bells-condition": "^5.0.1",
44
43
  "xrpl": "^5.3.0"
45
44
  },
46
45
  "devDependencies": {
@@ -68,9 +67,11 @@
68
67
  "demo/",
69
68
  "midnight/*.mjs",
70
69
  "midnight/*.sh",
71
- "midnight/package.json",
72
70
  "midnight/deployments.preprod.json",
73
71
  "midnight/managed/",
72
+ "!midnight/managed/**/keys/*.prover",
73
+ "!midnight/package.json",
74
+ "!midnight/package-lock.json",
74
75
  "midnight/witnesses.ts",
75
76
  "midnight/tsconfig.json",
76
77
  "scripts/",
@@ -0,0 +1,96 @@
1
+ /**
2
+ * PREIMAGE-SHA-256 crypto-conditions (draft-thomas-crypto-conditions, as used by XRPL escrows).
3
+ * Replaces the five-bells-condition dependency: CorvusZK only needs this one condition type,
4
+ * and node:crypto plus a minimal DER encoder covers it with no transitive packages.
5
+ *
6
+ * fulfillment = A0 len( 80 len(preimage) preimage )
7
+ * condition = A0 len( 80 20 sha256(preimage) 81 len(cost) cost ) cost = preimage length in bytes
8
+ *
9
+ * Parsing is strict: only definite, minimal DER lengths; no trailing bytes; no other types.
10
+ */
11
+ import crypto from 'node:crypto';
12
+
13
+ /** Largest preimage accepted. XRPL limits a fulfillment to 256 bytes; this is a generous parser bound. */
14
+ export const MAX_PREIMAGE_BYTES = 65_535;
15
+
16
+ function derLength(n: number): Buffer {
17
+ if (n < 0x80) return Buffer.from([n]);
18
+ if (n <= 0xff) return Buffer.from([0x81, n]);
19
+ if (n <= 0xffff) return Buffer.from([0x82, n >> 8, n & 0xff]);
20
+ throw new RangeError('crypto-condition: length too large');
21
+ }
22
+ function tlv(tag: number, body: Buffer): Buffer {
23
+ return Buffer.concat([Buffer.from([tag]), derLength(body.length), body]);
24
+ }
25
+ /** DER INTEGER content for a non-negative number: minimal big-endian, with a 0x00 sign byte when the top bit is set. */
26
+ function uintBytes(n: number): Buffer {
27
+ if (n === 0) return Buffer.from([0]);
28
+ const out: number[] = [];
29
+ while (n > 0) {
30
+ out.unshift(n & 0xff);
31
+ n = Math.floor(n / 256);
32
+ }
33
+ if (out[0] & 0x80) out.unshift(0);
34
+ return Buffer.from(out);
35
+ }
36
+ function readTlv(buf: Buffer, at: number): { tag: number; body: Buffer; end: number } {
37
+ if (at + 2 > buf.length) throw new Error('crypto-condition: truncated');
38
+ const tag = buf[at];
39
+ let len = buf[at + 1];
40
+ let p = at + 2;
41
+ if (len & 0x80) {
42
+ const nb = len & 0x7f;
43
+ if (nb === 0 || nb > 2 || p + nb > buf.length) throw new Error('crypto-condition: bad length');
44
+ len = 0;
45
+ for (let i = 0; i < nb; i++) len = len * 256 + buf[p + i];
46
+ if ((nb === 1 && len < 0x80) || (nb === 2 && len <= 0xff)) throw new Error('crypto-condition: non-minimal length');
47
+ p += nb;
48
+ }
49
+ if (p + len > buf.length) throw new Error('crypto-condition: truncated');
50
+ return { tag, body: buf.subarray(p, p + len), end: p + len };
51
+ }
52
+
53
+ function toBuf(x: Uint8Array | string, what: string): Buffer {
54
+ if (typeof x === 'string') {
55
+ if (!/^([0-9a-fA-F]{2})*$/.test(x)) throw new Error(`crypto-condition: ${what} must be hex`);
56
+ return Buffer.from(x, 'hex');
57
+ }
58
+ return Buffer.from(x);
59
+ }
60
+
61
+ /** Condition + fulfillment (uppercase hex, the form XRPL transactions use) for a preimage. */
62
+ export function preimageSha256(preimage: Uint8Array): { condition: string; fulfillment: string } {
63
+ const p = Buffer.from(preimage);
64
+ if (p.length > MAX_PREIMAGE_BYTES) throw new RangeError('crypto-condition: preimage too long');
65
+ const fulfillment = tlv(0xa0, tlv(0x80, p));
66
+ const hash = crypto.createHash('sha256').update(p).digest();
67
+ const condition = tlv(0xa0, Buffer.concat([tlv(0x80, hash), tlv(0x81, uintBytes(p.length))]));
68
+ return { condition: condition.toString('hex').toUpperCase(), fulfillment: fulfillment.toString('hex').toUpperCase() };
69
+ }
70
+
71
+ /** Extract the preimage from a PREIMAGE-SHA-256 fulfillment. Throws on anything else. */
72
+ export function preimageFromFulfillment(fulfillment: Uint8Array | string): Buffer {
73
+ const buf = toBuf(fulfillment, 'fulfillment');
74
+ const outer = readTlv(buf, 0);
75
+ if (outer.tag !== 0xa0 || outer.end !== buf.length) throw new Error('crypto-condition: not a PREIMAGE-SHA-256 fulfillment');
76
+ const inner = readTlv(outer.body, 0);
77
+ if (inner.tag !== 0x80 || inner.end !== outer.body.length) throw new Error('crypto-condition: malformed fulfillment');
78
+ if (inner.body.length > MAX_PREIMAGE_BYTES) throw new Error('crypto-condition: preimage too long');
79
+ return Buffer.from(inner.body);
80
+ }
81
+
82
+ /** Condition (uppercase hex) that a fulfillment satisfies. */
83
+ export function conditionFromFulfillment(fulfillment: Uint8Array | string): string {
84
+ return preimageSha256(preimageFromFulfillment(fulfillment)).condition;
85
+ }
86
+
87
+ /** True only if the fulfillment is a well-formed PREIMAGE-SHA-256 fulfillment for exactly this condition. */
88
+ export function validateFulfillment(fulfillment: Uint8Array | string, condition: Uint8Array | string): boolean {
89
+ try {
90
+ const want = toBuf(condition, 'condition');
91
+ const got = Buffer.from(conditionFromFulfillment(fulfillment), 'hex');
92
+ return got.length === want.length && crypto.timingSafeEqual(got, want);
93
+ } catch {
94
+ return false;
95
+ }
96
+ }
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@ import crypto from 'node:crypto';
15
15
  import path from 'node:path';
16
16
  import { fileURLToPath, pathToFileURL } from 'node:url';
17
17
  import * as xrpl from 'xrpl';
18
- import cc from 'five-bells-condition';
18
+ import { preimageSha256 } from './crypto-condition.js';
19
19
  import { preprodConfig } from './midnight-config.js';
20
20
 
21
21
  export type Claim =
@@ -856,13 +856,7 @@ export function feesFromMidnightReceipt(pub: { fees?: { paidFees?: unknown; esti
856
856
  * The bound path is midnight/eligibility.mjs (see README "Security model").
857
857
  */
858
858
  export function createCryptoCondition() {
859
- const preimage = crypto.randomBytes(32);
860
- const f = new cc.PreimageSha256();
861
- f.setPreimage(preimage);
862
- return {
863
- condition: f.getConditionBinary().toString('hex').toUpperCase(),
864
- fulfillment: f.serializeBinary().toString('hex').toUpperCase(),
865
- };
859
+ return preimageSha256(crypto.randomBytes(32));
866
860
  }
867
861
 
868
862
  export async function createEscrow(
@@ -937,3 +931,4 @@ export * from './midnight-config.js';
937
931
  export * from './intent.js';
938
932
  export * from './registry.js';
939
933
  export * from './strict-release.js';
934
+ export * from './crypto-condition.js';
@@ -13,7 +13,7 @@
13
13
  * Retries are idempotent: a settled intent is never submitted again. XRPL consumes an escrow
14
14
  * once, so a duplicate EscrowFinish cannot pay twice; an unknown outcome returns `reconcile`.
15
15
  */
16
- import cc from 'five-bells-condition';
16
+ import { preimageSha256 } from './crypto-condition.js';
17
17
  import * as xrpl from 'xrpl';
18
18
  import { normalizeIntentV2, intentHashV2, type IntentV2 } from './intent.js';
19
19
  import { RegistryConflict, type MemoryPolicyRegistry } from './registry.js';
@@ -116,10 +116,9 @@ export async function releaseStrict(o: {
116
116
  // The revealed preimage must open exactly this intent's condition.
117
117
  const pre = String(ledger.revealedPreimage ?? '');
118
118
  if (!/^[0-9a-f]{64}$/.test(pre)) return res('refused', 'no-revealed-preimage');
119
- const f = new cc.PreimageSha256();
120
- f.setPreimage(Buffer.from(pre, 'hex'));
121
- if (f.getConditionBinary().toString('hex').toUpperCase() !== intent.condition) return res('refused', 'revealed-preimage-does-not-open-condition');
122
- const fulfillment = f.serializeBinary().toString('hex').toUpperCase();
119
+ const opened = preimageSha256(Buffer.from(pre, 'hex'));
120
+ if (opened.condition !== intent.condition) return res('refused', 'revealed-preimage-does-not-open-condition');
121
+ const fulfillment = opened.fulfillment;
123
122
 
124
123
  // One credential, one intent (CZ-008).
125
124
  const credNull = String(ledger.credentialNullifier ?? '');
@@ -1,4 +0,0 @@
1
- [diffend] Oversized file quarantined before diffing.
2
- name: package/midnight/managed/eligibility/keys/prove.prover
3
- size: 49292273 bytes
4
- sha256: 226c52e64a0f45a021ee815f89f3dd96b470034da54d09d66a37f78464d789ac
@@ -1,4 +0,0 @@
1
- [diffend] Oversized file quarantined before diffing.
2
- name: package/midnight/managed/eligibility_v2/keys/prove.prover
3
- size: 98576436 bytes
4
- sha256: 2647e937c6142ea60ffc936496dbf6b39b3f2592fe7482c9d152601028af41a0
@@ -1,27 +0,0 @@
1
- {
2
- "name": "midnight",
3
- "version": "2.0.0",
4
- "private": true,
5
- "type": "module",
6
- "description": "Midnight ledger 9 dependencies (Preprod after the 9 Nov 2026 upgrade)",
7
- "engines": {
8
- "node": ">=22.12"
9
- },
10
- "dependencies": {
11
- "@midnight-ntwrk/compact-js": "3.0.0",
12
- "@midnight-ntwrk/compact-runtime": "0.21.0",
13
- "@midnight-ntwrk/midnight-js": "5.0.0",
14
- "@midnight-ntwrk/midnight-js-http-client-proof-provider": "5.0.0",
15
- "@midnight-ntwrk/midnight-js-indexer-public-data-provider": "5.0.0",
16
- "@midnight-ntwrk/midnight-js-level-private-state-provider": "5.0.0",
17
- "@midnight-ntwrk/midnight-js-node-zk-config-provider": "5.0.0",
18
- "@midnight-ntwrk/testkit-js": "5.0.0",
19
- "@midnightntwrk/ledger-v9": "1.0.1",
20
- "@midnightntwrk/wallet-sdk": "2.0.0",
21
- "rxjs": "^7.8.2",
22
- "ws": "^8.22.1"
23
- },
24
- "overrides": {
25
- "@midnightntwrk/ledger-v9": "1.0.1"
26
- }
27
- }
@@ -1 +0,0 @@
1
- declare module 'five-bells-condition';