@fangorn-network/sdk 0.1.0-dev → 0.1.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/lib/cli/cli.js +328 -128
- package/lib/cli/index.d.ts +4 -1
- package/lib/cli/index.js +20 -2
- package/lib/config.d.ts +111 -48
- package/lib/config.js +39 -11
- package/lib/contracts/app-registry/abi.d.ts +1265 -0
- package/lib/contracts/app-registry/abi.js +1454 -0
- package/lib/contracts/app-registry/index.d.ts +161 -0
- package/lib/contracts/app-registry/index.js +274 -0
- package/lib/contracts/data-registry/abi.d.ts +19 -195
- package/lib/contracts/data-registry/abi.js +206 -409
- package/lib/contracts/data-registry/index.d.ts +2 -126
- package/lib/contracts/data-registry/index.js +1 -174
- package/lib/contracts/index.d.ts +7 -4
- package/lib/contracts/index.js +7 -4
- package/lib/contracts/logs.d.ts +1 -1
- package/lib/contracts/logs.js +1 -1
- package/lib/contracts/{settlement-registry → membership-registry}/abi.d.ts +636 -301
- package/lib/contracts/membership-registry/abi.js +1333 -0
- package/lib/contracts/membership-registry/index.d.ts +79 -0
- package/lib/contracts/membership-registry/index.js +104 -0
- package/lib/contracts/types.d.ts +10 -0
- package/lib/contracts/types.js +12 -0
- package/lib/crypto/aes.d.ts +2 -2
- package/lib/crypto/aes.js +4 -4
- package/lib/crypto/encryption.d.ts +109 -8
- package/lib/crypto/encryption.js +114 -16
- package/lib/engine/car.d.ts +1 -5
- package/lib/engine/car.js +2 -2
- package/lib/engine/index.d.ts +14 -33
- package/lib/engine/index.js +41 -98
- package/lib/engine/records.d.ts +8 -19
- package/lib/engine/records.js +7 -81
- package/lib/engine/test-helpers.d.ts +3 -2
- package/lib/engine/test-helpers.js +5 -7
- package/lib/erc8004.js +10 -7
- package/lib/fangorn.d.ts +86 -65
- package/lib/fangorn.js +153 -115
- package/lib/feed.d.ts +8 -7
- package/lib/feed.js +8 -7
- package/lib/harness/examples/hackmd/build-markdown-graph.d.ts +1 -0
- package/lib/harness/examples/hackmd/build-markdown-graph.js +17 -0
- package/lib/harness/index.d.ts +58 -0
- package/lib/harness/index.js +73 -0
- package/lib/index.d.ts +13 -10
- package/lib/index.js +13 -10
- package/lib/node_modules/{@jridgewell → .pnpm/@jridgewell_sourcemap-codec@1.5.5/node_modules/@jridgewell}/sourcemap-codec/dist/sourcemap-codec.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_expect@4.0.18/node_modules/@vitest}/expect/dist/index.js +8 -8
- package/lib/node_modules/{@vitest → .pnpm/@vitest_pretty-format@4.0.18/node_modules/@vitest}/pretty-format/dist/index.js +2 -2
- package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/chunk-tasks.js +2 -2
- package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/index.js +4 -4
- package/lib/node_modules/{@vitest → .pnpm/@vitest_snapshot@4.0.18/node_modules/@vitest}/snapshot/dist/index.js +4 -4
- package/lib/node_modules/{@vitest → .pnpm/@vitest_spy@4.0.18/node_modules/@vitest}/spy/dist/index.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-_commonjsHelpers.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/chunk-pathe.M-eThtNZ.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/diff.js +3 -3
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/display.js +2 -2
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/error.js +2 -2
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/helpers.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/serialize.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/source-map.js +1 -1
- package/lib/node_modules/{@vitest → .pnpm/@vitest_utils@4.0.18/node_modules/@vitest}/utils/dist/timers.js +1 -1
- package/lib/node_modules/{chai → .pnpm/chai@6.2.2/node_modules/chai}/index.js +1 -1
- package/lib/node_modules/{magic-string → .pnpm/magic-string@0.30.21/node_modules/magic-string}/dist/magic-string.es.js +2 -2
- package/lib/node_modules/{pathe → .pnpm/pathe@2.0.3/node_modules/pathe}/dist/shared/pathe.M-eThtNZ.js +1 -1
- package/lib/node_modules/{tinyrainbow → .pnpm/tinyrainbow@3.0.3/node_modules/tinyrainbow}/dist/index.js +1 -1
- package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/_commonjsHelpers.D26ty3Ew.js +1 -1
- package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/date.Bq6ZW5rf.js +1 -1
- package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/utils.DvEY5TfP.js +2 -2
- package/lib/node_modules/{vitest → .pnpm/vitest@4.0.18_@types_node@24.10.1_jiti@2.6.1_terser@5.51.2_tsx@4.21.0_yaml@2.8.2/node_modules/vitest}/dist/chunks/vi.2VT5v0um.js +11 -11
- package/lib/providers/storage/signed-url.d.ts +10 -2
- package/lib/providers/storage/signed-url.js +15 -5
- package/lib/providers/storage/types.d.ts +1 -1
- package/lib/providers/storage/utils.js +3 -5
- package/lib/registry.d.ts +118 -0
- package/lib/registry.js +184 -0
- package/lib/test/mem-registry.d.ts +19 -14
- package/lib/test/mem-registry.js +37 -44
- package/lib/test/sdk-suite.d.ts +4 -1
- package/lib/test/sdk-suite.js +89 -18
- package/lib/test/testbed.d.ts +5227 -5218
- package/lib/test/testbed.js +56 -29
- package/lib/types/index.d.ts +11 -6
- package/package.json +1 -1
- package/lib/cli/sell.d.ts +0 -28
- package/lib/cli/sell.js +0 -95
- package/lib/cli/stage.d.ts +0 -19
- package/lib/cli/stage.js +0 -94
- package/lib/contracts/settlement-registry/abi.js +0 -962
- package/lib/contracts/settlement-registry/index.d.ts +0 -143
- package/lib/contracts/settlement-registry/index.js +0 -213
- package/lib/crypto/paid.d.ts +0 -104
- package/lib/crypto/paid.js +0 -147
- package/lib/test/key-server.d.ts +0 -12
- package/lib/test/key-server.js +0 -30
- /package/lib/node_modules/{@vitest → .pnpm/@vitest_runner@4.0.18/node_modules/@vitest}/runner/dist/utils.js +0 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/membership-registry/index.d.ts
|
|
4
|
+
/** A signed ERC-3009 `receiveWithAuthorization` paying the registry, as `join` takes it. */
|
|
5
|
+
interface MembershipPayment {
|
|
6
|
+
from: Address;
|
|
7
|
+
value: bigint;
|
|
8
|
+
validAfter: bigint;
|
|
9
|
+
validBefore: bigint;
|
|
10
|
+
nonce: Hex;
|
|
11
|
+
v: number;
|
|
12
|
+
r: Hex;
|
|
13
|
+
s: Hex;
|
|
14
|
+
}
|
|
15
|
+
/** What `join` needs besides the payment: the app, the commitment, the salt in the nonce. */
|
|
16
|
+
interface JoinArgs {
|
|
17
|
+
appId: Hex;
|
|
18
|
+
commitment: bigint;
|
|
19
|
+
salt: Hex;
|
|
20
|
+
payment: MembershipPayment;
|
|
21
|
+
}
|
|
22
|
+
/** What `claim` needs: a Semaphore proof over the (app, epoch) group, message = holder. */
|
|
23
|
+
interface ClaimArgs {
|
|
24
|
+
appId: Hex;
|
|
25
|
+
epoch: bigint;
|
|
26
|
+
holder: Address;
|
|
27
|
+
merkleTreeDepth: bigint;
|
|
28
|
+
merkleTreeRoot: bigint;
|
|
29
|
+
nullifier: bigint;
|
|
30
|
+
points: readonly [bigint, bigint, bigint, bigint, bigint, bigint, bigint, bigint];
|
|
31
|
+
}
|
|
32
|
+
/** The nonce a payer signs for `join`, the same derivation as the contract's `joinNonce`. */
|
|
33
|
+
declare const joinNonce: (appId: Hex, epoch: bigint, commitment: bigint, salt: Hex) => Hex;
|
|
34
|
+
/** The Semaphore scope of (app, epoch), as the contract fixes it in `claim`. */
|
|
35
|
+
declare const membershipScope: (appId: Hex, epoch: bigint) => bigint;
|
|
36
|
+
/** A holder's membership token id in an app. */
|
|
37
|
+
declare const membershipTokenId: (appId: Hex, holder: Address) => bigint;
|
|
38
|
+
/**
|
|
39
|
+
* Client for the MembershipRegistry.
|
|
40
|
+
*
|
|
41
|
+
* The app owner's half is here (`setPlan`), with the reads every gate needs
|
|
42
|
+
* (`canRead`, `planOf`, `currentEpoch`) and the two relayed writes (`join`, `claim`),
|
|
43
|
+
* which take what a buyer signed and proved, so anyone may submit them.
|
|
44
|
+
*
|
|
45
|
+
* The buyer's half (Semaphore identity, stealth holder, the payment signature, the
|
|
46
|
+
* group from `Joined` logs, the proof) lives in `@fangorn-network/fetch`: this SDK
|
|
47
|
+
* carries no Semaphore proving dependency.
|
|
48
|
+
*/
|
|
49
|
+
declare class MembershipRegistryClient {
|
|
50
|
+
private contractAddress;
|
|
51
|
+
private publicClient;
|
|
52
|
+
private walletClient?;
|
|
53
|
+
constructor(contractAddress: Address, publicClient: PublicClient, walletClient?: WalletClient | undefined);
|
|
54
|
+
getAddress(): Address;
|
|
55
|
+
private write;
|
|
56
|
+
private read;
|
|
57
|
+
/** Price (USDC base units) and period (seconds) of a membership in `appId`. App owner only. */
|
|
58
|
+
setPlan(appId: Hex, price: bigint, period: bigint): Promise<Hash>;
|
|
59
|
+
join({
|
|
60
|
+
appId,
|
|
61
|
+
commitment,
|
|
62
|
+
salt,
|
|
63
|
+
payment
|
|
64
|
+
}: JoinArgs): Promise<Hash>;
|
|
65
|
+
claim(a: ClaimArgs): Promise<Hash>;
|
|
66
|
+
planOf(appId: Hex): Promise<{
|
|
67
|
+
price: bigint;
|
|
68
|
+
period: bigint;
|
|
69
|
+
}>;
|
|
70
|
+
currentEpoch(appId: Hex): Promise<bigint>;
|
|
71
|
+
/** What `who` may read with `tokenId`: the app, and until when (seconds; 0 = nothing). */
|
|
72
|
+
canRead(tokenId: bigint, who: Address): Promise<{
|
|
73
|
+
appId: Hex;
|
|
74
|
+
until: bigint;
|
|
75
|
+
}>;
|
|
76
|
+
expiresAt(tokenId: bigint): Promise<bigint>;
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
export { ClaimArgs, JoinArgs, MembershipPayment, MembershipRegistryClient, joinNonce, membershipScope, membershipTokenId };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { requireWallet, sendWrite } from "../write.js";
|
|
2
|
+
import { MEMBERSHIP_REGISTRY_ABI } from "./abi.js";
|
|
3
|
+
import { encodeAbiParameters, keccak256 } from "viem";
|
|
4
|
+
|
|
5
|
+
//#region src/contracts/membership-registry/index.ts
|
|
6
|
+
/** The nonce a payer signs for `join`, the same derivation as the contract's `joinNonce`. */
|
|
7
|
+
const joinNonce = (appId, epoch, commitment, salt) => keccak256(encodeAbiParameters([
|
|
8
|
+
{ type: "bytes32" },
|
|
9
|
+
{ type: "uint256" },
|
|
10
|
+
{ type: "uint256" },
|
|
11
|
+
{ type: "bytes32" }
|
|
12
|
+
], [
|
|
13
|
+
appId,
|
|
14
|
+
epoch,
|
|
15
|
+
commitment,
|
|
16
|
+
salt
|
|
17
|
+
]));
|
|
18
|
+
/** The Semaphore scope of (app, epoch), as the contract fixes it in `claim`. */
|
|
19
|
+
const membershipScope = (appId, epoch) => BigInt(keccak256(encodeAbiParameters([{ type: "bytes32" }, { type: "uint256" }], [appId, epoch])));
|
|
20
|
+
/** A holder's membership token id in an app. */
|
|
21
|
+
const membershipTokenId = (appId, holder) => BigInt(keccak256(encodeAbiParameters([{ type: "bytes32" }, { type: "address" }], [appId, holder])));
|
|
22
|
+
/**
|
|
23
|
+
* Client for the MembershipRegistry.
|
|
24
|
+
*
|
|
25
|
+
* The app owner's half is here (`setPlan`), with the reads every gate needs
|
|
26
|
+
* (`canRead`, `planOf`, `currentEpoch`) and the two relayed writes (`join`, `claim`),
|
|
27
|
+
* which take what a buyer signed and proved, so anyone may submit them.
|
|
28
|
+
*
|
|
29
|
+
* The buyer's half (Semaphore identity, stealth holder, the payment signature, the
|
|
30
|
+
* group from `Joined` logs, the proof) lives in `@fangorn-network/fetch`: this SDK
|
|
31
|
+
* carries no Semaphore proving dependency.
|
|
32
|
+
*/
|
|
33
|
+
var MembershipRegistryClient = class {
|
|
34
|
+
constructor(contractAddress, publicClient, walletClient) {
|
|
35
|
+
this.contractAddress = contractAddress;
|
|
36
|
+
this.publicClient = publicClient;
|
|
37
|
+
this.walletClient = walletClient;
|
|
38
|
+
}
|
|
39
|
+
getAddress() {
|
|
40
|
+
return this.contractAddress;
|
|
41
|
+
}
|
|
42
|
+
write(functionName, args) {
|
|
43
|
+
return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, MEMBERSHIP_REGISTRY_ABI, functionName, args);
|
|
44
|
+
}
|
|
45
|
+
read(functionName, args = []) {
|
|
46
|
+
return this.publicClient.readContract({
|
|
47
|
+
address: this.contractAddress,
|
|
48
|
+
abi: MEMBERSHIP_REGISTRY_ABI,
|
|
49
|
+
functionName,
|
|
50
|
+
args
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Price (USDC base units) and period (seconds) of a membership in `appId`. App owner only. */
|
|
54
|
+
setPlan(appId, price, period) {
|
|
55
|
+
return this.write("setPlan", [
|
|
56
|
+
appId,
|
|
57
|
+
price,
|
|
58
|
+
period
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
join({ appId, commitment, salt, payment }) {
|
|
62
|
+
return this.write("join", [
|
|
63
|
+
appId,
|
|
64
|
+
commitment,
|
|
65
|
+
salt,
|
|
66
|
+
payment
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
claim(a) {
|
|
70
|
+
return this.write("claim", [
|
|
71
|
+
a.appId,
|
|
72
|
+
a.epoch,
|
|
73
|
+
a.holder,
|
|
74
|
+
a.merkleTreeDepth,
|
|
75
|
+
a.merkleTreeRoot,
|
|
76
|
+
a.nullifier,
|
|
77
|
+
a.points
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
async planOf(appId) {
|
|
81
|
+
const [price, period] = await this.read("planOf", [appId]);
|
|
82
|
+
return {
|
|
83
|
+
price,
|
|
84
|
+
period
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
currentEpoch(appId) {
|
|
88
|
+
return this.read("currentEpoch", [appId]);
|
|
89
|
+
}
|
|
90
|
+
/** What `who` may read with `tokenId`: the app, and until when (seconds; 0 = nothing). */
|
|
91
|
+
async canRead(tokenId, who) {
|
|
92
|
+
const [appId, until] = await this.read("canRead", [tokenId, who]);
|
|
93
|
+
return {
|
|
94
|
+
appId,
|
|
95
|
+
until
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
expiresAt(tokenId) {
|
|
99
|
+
return this.read("expiresAt", [tokenId]);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
export { MembershipRegistryClient, joinNonce, membershipScope, membershipTokenId };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/contracts/types.ts
|
|
2
|
+
let PublisherStatus = /* @__PURE__ */ function(PublisherStatus$1) {
|
|
3
|
+
PublisherStatus$1[PublisherStatus$1["NONE"] = 0] = "NONE";
|
|
4
|
+
PublisherStatus$1[PublisherStatus$1["ACTIVE"] = 1] = "ACTIVE";
|
|
5
|
+
PublisherStatus$1[PublisherStatus$1["SUSPENDED"] = 2] = "SUSPENDED";
|
|
6
|
+
/** Retired: invited under the old join flow and never joined. Not a member. */
|
|
7
|
+
PublisherStatus$1[PublisherStatus$1["INVITED"] = 3] = "INVITED";
|
|
8
|
+
return PublisherStatus$1;
|
|
9
|
+
}({});
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { PublisherStatus };
|
package/lib/crypto/aes.d.ts
CHANGED
|
@@ -9,11 +9,11 @@ declare const GCM_TAG_LENGTH = 16;
|
|
|
9
9
|
* AES-256-GCM encrypt. Returns the ciphertext with the 16-byte auth tag
|
|
10
10
|
* appended (noble's convention) — i.e. `ciphertext || tag`.
|
|
11
11
|
*/
|
|
12
|
-
declare function aesGcmEncrypt(key: Uint8Array, plaintext: Uint8Array, nonce: Uint8Array
|
|
12
|
+
declare function aesGcmEncrypt(key: Uint8Array, plaintext: Uint8Array, nonce: Uint8Array): Uint8Array;
|
|
13
13
|
/**
|
|
14
14
|
* AES-256-GCM decrypt. `sealed` is `ciphertext || tag` as produced by
|
|
15
15
|
* {@link aesGcmEncrypt}. Throws if the tag does not verify.
|
|
16
16
|
*/
|
|
17
|
-
declare function aesGcmDecrypt(key: Uint8Array, sealed: Uint8Array, nonce: Uint8Array
|
|
17
|
+
declare function aesGcmDecrypt(key: Uint8Array, sealed: Uint8Array, nonce: Uint8Array): Uint8Array;
|
|
18
18
|
//#endregion
|
|
19
19
|
export { AES_KEY_LENGTH, GCM_NONCE_LENGTH, GCM_TAG_LENGTH, aesGcmDecrypt, aesGcmEncrypt };
|
package/lib/crypto/aes.js
CHANGED
|
@@ -11,15 +11,15 @@ const GCM_TAG_LENGTH = 16;
|
|
|
11
11
|
* AES-256-GCM encrypt. Returns the ciphertext with the 16-byte auth tag
|
|
12
12
|
* appended (noble's convention) — i.e. `ciphertext || tag`.
|
|
13
13
|
*/
|
|
14
|
-
function aesGcmEncrypt(key, plaintext, nonce
|
|
15
|
-
return gcm(key, nonce
|
|
14
|
+
function aesGcmEncrypt(key, plaintext, nonce) {
|
|
15
|
+
return gcm(key, nonce).encrypt(plaintext);
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* AES-256-GCM decrypt. `sealed` is `ciphertext || tag` as produced by
|
|
19
19
|
* {@link aesGcmEncrypt}. Throws if the tag does not verify.
|
|
20
20
|
*/
|
|
21
|
-
function aesGcmDecrypt(key, sealed, nonce
|
|
22
|
-
return gcm(key, nonce
|
|
21
|
+
function aesGcmDecrypt(key, sealed, nonce) {
|
|
22
|
+
return gcm(key, nonce).decrypt(sealed);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
//#endregion
|
|
@@ -1,23 +1,57 @@
|
|
|
1
1
|
import { Hex } from "viem";
|
|
2
2
|
|
|
3
3
|
//#region src/crypto/encryption.d.ts
|
|
4
|
-
/** A field only its owner can open: sealSelf under the owner's own secret. */
|
|
5
4
|
declare const GADGET_SELF_HKDF_V1 = "self-hkdf-v1";
|
|
5
|
+
declare const GADGET_WORKER_USDC_V1 = "worker-usdc-v1";
|
|
6
|
+
type Gadget = typeof GADGET_SELF_HKDF_V1 | typeof GADGET_WORKER_USDC_V1;
|
|
7
|
+
/**
|
|
8
|
+
* A sealed-field handle: where the ciphertext lives and how to verify/open it.
|
|
9
|
+
* This is what gets embedded in a vertex `payload[field]` in place of the
|
|
10
|
+
* plaintext value — the engine treats it as an opaque object.
|
|
11
|
+
*/
|
|
12
|
+
interface HandleFieldInput {
|
|
13
|
+
"@type": "handle";
|
|
14
|
+
/** R2 object key. Signed over in the /access request; resolve via {@link decryptHandle}. */
|
|
15
|
+
objectKey: string;
|
|
16
|
+
/**
|
|
17
|
+
* Access-worker endpoint. NOTE: this is the *access* worker, distinct from
|
|
18
|
+
* the Pinata-presign upload worker in the CLI config — do not conflate them.
|
|
19
|
+
*/
|
|
20
|
+
workerUrl: string;
|
|
21
|
+
encryption: {
|
|
22
|
+
gadget: Gadget;
|
|
23
|
+
/**
|
|
24
|
+
* 32-byte resource id the ciphertext is bound to. Doubles as the HKDF
|
|
25
|
+
* binding and the Settlement Registry key (`isSettled`/`getPrice`).
|
|
26
|
+
*/
|
|
27
|
+
resourceId: Hex;
|
|
28
|
+
ciphertextHash: Hex;
|
|
29
|
+
/** Present only for worker-usdc-v1: the worker's static X25519 pubkey. */
|
|
30
|
+
workerPubkey?: Hex;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
6
33
|
/** Length of an X25519 public key, in bytes. */
|
|
7
34
|
declare const X25519_PUBKEY_LENGTH = 32;
|
|
35
|
+
declare const sha256Hex: (bytes: Uint8Array) => Hex;
|
|
8
36
|
/**
|
|
9
|
-
*
|
|
37
|
+
* worker-usdc-v1 seal: ephemeral-static ECDH to the worker's static key, keyed
|
|
38
|
+
* to the resourceId.
|
|
10
39
|
*
|
|
11
40
|
* ciphertext = ephemeralPub(32) || nonce(12) || aes-256-gcm-ct
|
|
12
41
|
*
|
|
13
|
-
* Only the holder of the secret matching `
|
|
42
|
+
* Only the holder of the secret matching `workerPubkey` can derive the same AES
|
|
14
43
|
* key, and only with the matching `resourceId`.
|
|
15
44
|
*/
|
|
16
|
-
declare function seal(plaintext: Uint8Array,
|
|
17
|
-
/**
|
|
18
|
-
|
|
45
|
+
declare function seal(plaintext: Uint8Array, workerPubkey: Uint8Array, resourceId: Hex): Uint8Array;
|
|
46
|
+
/**
|
|
47
|
+
* Inverse of {@link seal}. Recovers the plaintext given the worker's static
|
|
48
|
+
* secret and the same resourceId. This is the operation the access worker
|
|
49
|
+
* performs after the settlement gate passes; exposed here for parity testing
|
|
50
|
+
* and local tooling.
|
|
51
|
+
*/
|
|
52
|
+
declare function unseal(ciphertext: Uint8Array, workerSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
19
53
|
/**
|
|
20
|
-
*
|
|
54
|
+
* self-hkdf-v1 seal: derive the AES key straight from the owner's own 32-byte
|
|
21
55
|
* secret, bound to the resourceId. No ECDH, no recipient — only a party holding
|
|
22
56
|
* `ownSecret` can re-derive the key.
|
|
23
57
|
*
|
|
@@ -26,6 +60,34 @@ declare function unseal(ciphertext: Uint8Array, recipientSecret: Uint8Array, res
|
|
|
26
60
|
declare function sealSelf(plaintext: Uint8Array, ownSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
27
61
|
/** Inverse of {@link sealSelf}. */
|
|
28
62
|
declare function unsealSelf(ciphertext: Uint8Array, ownSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
63
|
+
interface StorageTarget {
|
|
64
|
+
/** Access-worker upload endpoint (NOT the Pinata-presign worker). */
|
|
65
|
+
workerUrl: string;
|
|
66
|
+
/** Bearer token the worker requires for uploads. */
|
|
67
|
+
authToken: string;
|
|
68
|
+
contentType: string;
|
|
69
|
+
}
|
|
70
|
+
type EncryptAndUploadParams = {
|
|
71
|
+
gadget: typeof GADGET_SELF_HKDF_V1;
|
|
72
|
+
plaintext: Uint8Array;
|
|
73
|
+
resourceId: Hex;
|
|
74
|
+
storage: StorageTarget;
|
|
75
|
+
/** The owner's own 32-byte secret. */
|
|
76
|
+
ownSecret: Uint8Array;
|
|
77
|
+
} | {
|
|
78
|
+
gadget: typeof GADGET_WORKER_USDC_V1;
|
|
79
|
+
plaintext: Uint8Array;
|
|
80
|
+
resourceId: Hex;
|
|
81
|
+
storage: StorageTarget;
|
|
82
|
+
/** The access worker's static X25519 public key (32 bytes). */
|
|
83
|
+
workerPubkey: Uint8Array;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Seal `plaintext` under the chosen gadget, upload the opaque ciphertext to the
|
|
87
|
+
* access worker (which writes it to R2), and return the handle describing where
|
|
88
|
+
* to fetch it and how to open it.
|
|
89
|
+
*/
|
|
90
|
+
declare function encryptAndUpload(params: EncryptAndUploadParams): Promise<HandleFieldInput>;
|
|
29
91
|
/** Anything that can personal_sign a raw 32-byte hash (viem LocalAccount / WalletClient). */
|
|
30
92
|
interface AccessSigner {
|
|
31
93
|
signMessage(args: {
|
|
@@ -34,5 +96,44 @@ interface AccessSigner {
|
|
|
34
96
|
};
|
|
35
97
|
}): Promise<Hex>;
|
|
36
98
|
}
|
|
99
|
+
interface AccessRequest {
|
|
100
|
+
nullifier: Hex;
|
|
101
|
+
resourceId: Hex;
|
|
102
|
+
objectKey: string;
|
|
103
|
+
timestamp: number;
|
|
104
|
+
signature: Hex;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The message the worker recovers the stealth address from — must match
|
|
108
|
+
* `buildMessageHash` in the worker byte-for-byte:
|
|
109
|
+
* keccak256(abi.encodePacked(uint256 nullifier, bytes32 resourceId, string objectKey, uint64 timestamp))
|
|
110
|
+
*/
|
|
111
|
+
declare function accessMessageHash(nullifier: Hex, resourceId: Hex, objectKey: string, timestamp: number): Hex;
|
|
112
|
+
/** Sign the packed message so the worker can recover the settling stealth address. */
|
|
113
|
+
declare function buildAccessRequest(params: {
|
|
114
|
+
signer: AccessSigner;
|
|
115
|
+
nullifier: Hex;
|
|
116
|
+
resourceId: Hex;
|
|
117
|
+
objectKey: string;
|
|
118
|
+
/** Unix seconds; defaults to now. Must be inside the worker's TIMESTAMP_WINDOW. */
|
|
119
|
+
timestamp?: number;
|
|
120
|
+
}): Promise<AccessRequest>;
|
|
121
|
+
interface DecryptHandleParams {
|
|
122
|
+
handle: HandleFieldInput;
|
|
123
|
+
/** Signs the /access request; its address is what the worker checks settlement for. */
|
|
124
|
+
signer: AccessSigner;
|
|
125
|
+
/** Per-read nullifier (replay/anonymity); passed through to the worker. */
|
|
126
|
+
nullifier: Hex;
|
|
127
|
+
/** self-hkdf-v1 only: the same 32-byte secret used to seal. */
|
|
128
|
+
ownSecret?: Uint8Array;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Resolve a {@link HandleFieldInput} back to plaintext via the worker's
|
|
132
|
+
* settlement-gated `POST /access`.
|
|
133
|
+
*
|
|
134
|
+
* self-hkdf-v1 bytes come back sealed → verify hash → unseal with ownSecret.
|
|
135
|
+
* worker-usdc-v1 the worker already unsealed → bytes are plaintext.
|
|
136
|
+
*/
|
|
137
|
+
declare function decryptHandle(params: DecryptHandleParams): Promise<Uint8Array>;
|
|
37
138
|
//#endregion
|
|
38
|
-
export { AccessSigner, GADGET_SELF_HKDF_V1, X25519_PUBKEY_LENGTH, seal, sealSelf, unseal, unsealSelf };
|
|
139
|
+
export { AccessRequest, AccessSigner, DecryptHandleParams, EncryptAndUploadParams, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf };
|
package/lib/crypto/encryption.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { GCM_NONCE_LENGTH, aesGcmDecrypt, aesGcmEncrypt } from "./aes.js";
|
|
2
2
|
import { getRandomValues } from "./rand.js";
|
|
3
|
-
import { hexToBytes } from "viem";
|
|
4
|
-
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
+
import { bytesToHex, encodePacked, hexToBytes, keccak256 } from "viem";
|
|
5
4
|
import { x25519 } from "@noble/curves/ed25519";
|
|
5
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
6
6
|
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
7
7
|
|
|
8
8
|
//#region src/crypto/encryption.ts
|
|
9
|
-
/** A field only its owner can open: sealSelf under the owner's own secret. */
|
|
10
9
|
const GADGET_SELF_HKDF_V1 = "self-hkdf-v1";
|
|
10
|
+
const GADGET_WORKER_USDC_V1 = "worker-usdc-v1";
|
|
11
11
|
/** Length of an X25519 public key, in bytes. */
|
|
12
12
|
const X25519_PUBKEY_LENGTH = 32;
|
|
13
13
|
const utf8 = (s) => new TextEncoder().encode(s);
|
|
14
|
-
function concat
|
|
14
|
+
function concat(...parts) {
|
|
15
15
|
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
16
16
|
const out = new Uint8Array(total);
|
|
17
17
|
let off = 0;
|
|
@@ -22,31 +22,38 @@ function concat$1(...parts) {
|
|
|
22
22
|
return out;
|
|
23
23
|
}
|
|
24
24
|
const hkdfSha256 = (ikm, salt, info, length) => hkdf(sha256, ikm, salt, info, length);
|
|
25
|
-
const
|
|
25
|
+
const sha256Hex = (bytes) => bytesToHex(sha256(bytes));
|
|
26
|
+
const sealInfo = (resourceId) => concat(hexToBytes(resourceId), utf8(":sealed"));
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* worker-usdc-v1 seal: ephemeral-static ECDH to the worker's static key, keyed
|
|
29
|
+
* to the resourceId.
|
|
28
30
|
*
|
|
29
31
|
* ciphertext = ephemeralPub(32) || nonce(12) || aes-256-gcm-ct
|
|
30
32
|
*
|
|
31
|
-
* Only the holder of the secret matching `
|
|
33
|
+
* Only the holder of the secret matching `workerPubkey` can derive the same AES
|
|
32
34
|
* key, and only with the matching `resourceId`.
|
|
33
35
|
*/
|
|
34
|
-
function seal(plaintext,
|
|
36
|
+
function seal(plaintext, workerPubkey, resourceId) {
|
|
35
37
|
const ephSec = x25519.utils.randomSecretKey();
|
|
36
38
|
const ephPub = x25519.getPublicKey(ephSec);
|
|
37
|
-
const aesKey = hkdfSha256(x25519.getSharedSecret(ephSec,
|
|
39
|
+
const aesKey = hkdfSha256(x25519.getSharedSecret(ephSec, workerPubkey), void 0, sealInfo(resourceId), 32);
|
|
38
40
|
const nonce = getRandomValues(new Uint8Array(GCM_NONCE_LENGTH));
|
|
39
|
-
return concat
|
|
41
|
+
return concat(ephPub, nonce, aesGcmEncrypt(aesKey, plaintext, nonce));
|
|
40
42
|
}
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Inverse of {@link seal}. Recovers the plaintext given the worker's static
|
|
45
|
+
* secret and the same resourceId. This is the operation the access worker
|
|
46
|
+
* performs after the settlement gate passes; exposed here for parity testing
|
|
47
|
+
* and local tooling.
|
|
48
|
+
*/
|
|
49
|
+
function unseal(ciphertext, workerSecret, resourceId) {
|
|
43
50
|
const ephPub = ciphertext.slice(0, X25519_PUBKEY_LENGTH);
|
|
44
51
|
const nonce = ciphertext.slice(X25519_PUBKEY_LENGTH, X25519_PUBKEY_LENGTH + GCM_NONCE_LENGTH);
|
|
45
52
|
const aesCt = ciphertext.slice(X25519_PUBKEY_LENGTH + GCM_NONCE_LENGTH);
|
|
46
|
-
return aesGcmDecrypt(hkdfSha256(x25519.getSharedSecret(
|
|
53
|
+
return aesGcmDecrypt(hkdfSha256(x25519.getSharedSecret(workerSecret, ephPub), void 0, sealInfo(resourceId), 32), aesCt, nonce);
|
|
47
54
|
}
|
|
48
55
|
/**
|
|
49
|
-
*
|
|
56
|
+
* self-hkdf-v1 seal: derive the AES key straight from the owner's own 32-byte
|
|
50
57
|
* secret, bound to the resourceId. No ECDH, no recipient — only a party holding
|
|
51
58
|
* `ownSecret` can re-derive the key.
|
|
52
59
|
*
|
|
@@ -55,7 +62,7 @@ function unseal(ciphertext, recipientSecret, resourceId) {
|
|
|
55
62
|
function sealSelf(plaintext, ownSecret, resourceId) {
|
|
56
63
|
const aesKey = hkdfSha256(ownSecret, void 0, sealInfo(resourceId), 32);
|
|
57
64
|
const nonce = getRandomValues(new Uint8Array(GCM_NONCE_LENGTH));
|
|
58
|
-
return concat
|
|
65
|
+
return concat(nonce, aesGcmEncrypt(aesKey, plaintext, nonce));
|
|
59
66
|
}
|
|
60
67
|
/** Inverse of {@link sealSelf}. */
|
|
61
68
|
function unsealSelf(ciphertext, ownSecret, resourceId) {
|
|
@@ -63,6 +70,97 @@ function unsealSelf(ciphertext, ownSecret, resourceId) {
|
|
|
63
70
|
const aesCt = ciphertext.slice(GCM_NONCE_LENGTH);
|
|
64
71
|
return aesGcmDecrypt(hkdfSha256(ownSecret, void 0, sealInfo(resourceId), 32), aesCt, nonce);
|
|
65
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Seal `plaintext` under the chosen gadget, upload the opaque ciphertext to the
|
|
75
|
+
* access worker (which writes it to R2), and return the handle describing where
|
|
76
|
+
* to fetch it and how to open it.
|
|
77
|
+
*/
|
|
78
|
+
async function encryptAndUpload(params) {
|
|
79
|
+
const { plaintext, resourceId, storage } = params;
|
|
80
|
+
const ciphertext = params.gadget === GADGET_SELF_HKDF_V1 ? sealSelf(plaintext, params.ownSecret, resourceId) : seal(plaintext, params.workerPubkey, resourceId);
|
|
81
|
+
const uploadRes = await fetch(`${storage.workerUrl}/upload`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
Authorization: `Bearer ${storage.authToken}`,
|
|
85
|
+
"Content-Type": storage.contentType
|
|
86
|
+
},
|
|
87
|
+
body: ciphertext
|
|
88
|
+
});
|
|
89
|
+
if (!uploadRes.ok) throw new Error(`upload failed: ${uploadRes.status.toString()}`);
|
|
90
|
+
const { objectKey } = await uploadRes.json();
|
|
91
|
+
return {
|
|
92
|
+
"@type": "handle",
|
|
93
|
+
objectKey,
|
|
94
|
+
workerUrl: storage.workerUrl,
|
|
95
|
+
encryption: {
|
|
96
|
+
gadget: params.gadget,
|
|
97
|
+
resourceId,
|
|
98
|
+
ciphertextHash: sha256Hex(ciphertext),
|
|
99
|
+
...params.gadget === GADGET_WORKER_USDC_V1 ? { workerPubkey: bytesToHex(params.workerPubkey) } : {}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The message the worker recovers the stealth address from — must match
|
|
105
|
+
* `buildMessageHash` in the worker byte-for-byte:
|
|
106
|
+
* keccak256(abi.encodePacked(uint256 nullifier, bytes32 resourceId, string objectKey, uint64 timestamp))
|
|
107
|
+
*/
|
|
108
|
+
function accessMessageHash(nullifier, resourceId, objectKey, timestamp) {
|
|
109
|
+
return keccak256(encodePacked([
|
|
110
|
+
"uint256",
|
|
111
|
+
"bytes32",
|
|
112
|
+
"string",
|
|
113
|
+
"uint64"
|
|
114
|
+
], [
|
|
115
|
+
BigInt(nullifier),
|
|
116
|
+
resourceId,
|
|
117
|
+
objectKey,
|
|
118
|
+
BigInt(timestamp)
|
|
119
|
+
]));
|
|
120
|
+
}
|
|
121
|
+
/** Sign the packed message so the worker can recover the settling stealth address. */
|
|
122
|
+
async function buildAccessRequest(params) {
|
|
123
|
+
const timestamp = params.timestamp ?? Math.floor(Date.now() / 1e3);
|
|
124
|
+
const hash = accessMessageHash(params.nullifier, params.resourceId, params.objectKey, timestamp);
|
|
125
|
+
const signature = await params.signer.signMessage({ message: { raw: hash } });
|
|
126
|
+
return {
|
|
127
|
+
nullifier: params.nullifier,
|
|
128
|
+
resourceId: params.resourceId,
|
|
129
|
+
objectKey: params.objectKey,
|
|
130
|
+
timestamp,
|
|
131
|
+
signature
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Resolve a {@link HandleFieldInput} back to plaintext via the worker's
|
|
136
|
+
* settlement-gated `POST /access`.
|
|
137
|
+
*
|
|
138
|
+
* self-hkdf-v1 bytes come back sealed → verify hash → unseal with ownSecret.
|
|
139
|
+
* worker-usdc-v1 the worker already unsealed → bytes are plaintext.
|
|
140
|
+
*/
|
|
141
|
+
async function decryptHandle(params) {
|
|
142
|
+
const { handle, signer, nullifier } = params;
|
|
143
|
+
const { gadget, resourceId, ciphertextHash } = handle.encryption;
|
|
144
|
+
const access = await buildAccessRequest({
|
|
145
|
+
signer,
|
|
146
|
+
nullifier,
|
|
147
|
+
resourceId,
|
|
148
|
+
objectKey: handle.objectKey
|
|
149
|
+
});
|
|
150
|
+
const res = await fetch(`${handle.workerUrl}/access`, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
headers: { "Content-Type": "application/json" },
|
|
153
|
+
body: JSON.stringify(access)
|
|
154
|
+
});
|
|
155
|
+
if (!res.ok) throw new Error(`access failed: ${res.status.toString()}`);
|
|
156
|
+
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
157
|
+
if (gadget === GADGET_SELF_HKDF_V1) {
|
|
158
|
+
if (!params.ownSecret) throw new Error(`${gadget} requires ownSecret to decrypt`);
|
|
159
|
+
if (sha256Hex(bytes) !== ciphertextHash) throw new Error("ciphertext hash mismatch");
|
|
160
|
+
return unsealSelf(bytes, params.ownSecret, resourceId);
|
|
161
|
+
}
|
|
162
|
+
return bytes;
|
|
163
|
+
}
|
|
66
164
|
|
|
67
165
|
//#endregion
|
|
68
|
-
export { GADGET_SELF_HKDF_V1, X25519_PUBKEY_LENGTH, seal, sealSelf, unseal, unsealSelf };
|
|
166
|
+
export { GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf };
|
package/lib/engine/car.d.ts
CHANGED
|
@@ -5,10 +5,6 @@ import { CID } from "multiformats/cid";
|
|
|
5
5
|
/** Serialize blocks into CAR v1 bytes under a single root. */
|
|
6
6
|
declare function packCar(root: CID, blocks: Iterable<Block>): Promise<Uint8Array>;
|
|
7
7
|
/** Decode CAR bytes back into blocks, each checked against its CID (the host is untrusted). */
|
|
8
|
-
declare function readCar(bytes: Uint8Array
|
|
9
|
-
verify
|
|
10
|
-
}?: {
|
|
11
|
-
verify?: boolean | undefined;
|
|
12
|
-
}): Promise<Block[]>;
|
|
8
|
+
declare function readCar(bytes: Uint8Array): Promise<Block[]>;
|
|
13
9
|
//#endregion
|
|
14
10
|
export { packCar, readCar };
|
package/lib/engine/car.js
CHANGED
|
@@ -24,12 +24,12 @@ async function packCar(root, blocks) {
|
|
|
24
24
|
return buf;
|
|
25
25
|
}
|
|
26
26
|
/** Decode CAR bytes back into blocks, each checked against its CID (the host is untrusted). */
|
|
27
|
-
async function readCar(bytes
|
|
27
|
+
async function readCar(bytes) {
|
|
28
28
|
const reader = await CarReader.fromBytes(bytes);
|
|
29
29
|
const blocks = [];
|
|
30
30
|
for await (const { cid, bytes: blockBytes } of reader.blocks()) {
|
|
31
31
|
const ours = CID.parse(cid.toString());
|
|
32
|
-
|
|
32
|
+
assertBlockMatches(ours, blockBytes);
|
|
33
33
|
blocks.push({
|
|
34
34
|
cid: ours,
|
|
35
35
|
bytes: blockBytes
|