@fangorn-network/sdk 2026.10.28 → 2026.11.3-dev
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 +36 -658
- package/lib/cli/cli.js +30 -356
- package/lib/cli/index.d.ts +1 -4
- package/lib/cli/index.js +2 -20
- package/lib/config.d.ts +163 -226
- package/lib/config.js +8 -35
- package/lib/contracts/data-registry/abi.d.ts +206 -6
- package/lib/contracts/data-registry/abi.js +236 -7
- package/lib/contracts/data-registry/index.d.ts +126 -2
- package/lib/contracts/data-registry/index.js +174 -1
- package/lib/contracts/index.d.ts +5 -8
- package/lib/contracts/index.js +4 -7
- package/lib/contracts/logs.d.ts +1 -1
- package/lib/contracts/logs.js +1 -1
- package/lib/contracts/{membership-registry → settlement-registry}/abi.d.ts +288 -637
- package/lib/contracts/{membership-registry → settlement-registry}/abi.js +326 -712
- package/lib/contracts/settlement-registry/index.d.ts +141 -0
- package/lib/contracts/settlement-registry/index.js +209 -0
- package/lib/crypto/aes.d.ts +2 -2
- package/lib/crypto/aes.js +4 -4
- package/lib/crypto/encryption.d.ts +8 -109
- package/lib/crypto/encryption.js +16 -114
- package/lib/crypto/paid.d.ts +104 -0
- package/lib/crypto/paid.js +147 -0
- package/lib/engine/index.d.ts +23 -12
- package/lib/engine/index.js +68 -29
- package/lib/engine/records.d.ts +4 -7
- package/lib/engine/records.js +6 -6
- package/lib/engine/test-helpers.d.ts +2 -3
- package/lib/engine/test-helpers.js +7 -5
- package/lib/erc8004.js +1 -4
- package/lib/fangorn.d.ts +64 -95
- package/lib/fangorn.js +106 -164
- package/lib/feed.d.ts +7 -8
- package/lib/feed.js +7 -8
- package/lib/index.d.ts +10 -14
- package/lib/index.js +9 -13
- package/lib/providers/storage/signed-url.d.ts +2 -10
- package/lib/providers/storage/signed-url.js +4 -14
- package/lib/providers/storage/types.d.ts +1 -1
- package/lib/providers/storage/utils.js +5 -3
- package/lib/test/key-server.d.ts +12 -0
- package/lib/test/key-server.js +30 -0
- package/lib/test/mem-registry.d.ts +14 -19
- package/lib/test/mem-registry.js +44 -37
- package/lib/test/sdk-suite.d.ts +1 -4
- package/lib/test/sdk-suite.js +16 -87
- package/lib/test/testbed.d.ts +5204 -5214
- package/lib/test/testbed.js +29 -56
- package/lib/types/index.d.ts +6 -11
- package/package.json +1 -4
- package/lib/contracts/app-registry/abi.d.ts +0 -1306
- package/lib/contracts/app-registry/abi.js +0 -1493
- package/lib/contracts/app-registry/index.d.ts +0 -200
- package/lib/contracts/app-registry/index.js +0 -325
- package/lib/contracts/membership-registry/index.d.ts +0 -79
- package/lib/contracts/membership-registry/index.js +0 -104
- package/lib/contracts/types.d.ts +0 -8
- package/lib/contracts/types.js +0 -10
- package/lib/harness/examples/hackmd/build-markdown-graph.d.ts +0 -1
- package/lib/harness/examples/hackmd/build-markdown-graph.js +0 -17
- package/lib/harness/index.d.ts +0 -58
- package/lib/harness/index.js +0 -73
- package/lib/registry.d.ts +0 -118
- package/lib/registry.js +0 -184
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Address, Hash, Hex, PublicClient, TypedDataDomain, WalletClient } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/settlement-registry/index.d.ts
|
|
4
|
+
/** The key of a whole-repo subject. */
|
|
5
|
+
declare const WHOLE_REPO: Hex;
|
|
6
|
+
/** What a publisher signs: the price of one subject, and the referrer's share of it. */
|
|
7
|
+
interface Terms {
|
|
8
|
+
publisher: Address;
|
|
9
|
+
/** `repoId(name)` */
|
|
10
|
+
repo: Hex;
|
|
11
|
+
/** `recordKey(id)`, or `WHOLE_REPO` */
|
|
12
|
+
key: Hex;
|
|
13
|
+
/** USDC base units */
|
|
14
|
+
price: bigint;
|
|
15
|
+
/** 0..10000 */
|
|
16
|
+
shareBps: number;
|
|
17
|
+
generation: bigint;
|
|
18
|
+
/** unix seconds */
|
|
19
|
+
deadline: bigint;
|
|
20
|
+
}
|
|
21
|
+
/** A signed ERC-3009 `receiveWithAuthorization` paying the registry. */
|
|
22
|
+
interface SettlementPayment {
|
|
23
|
+
from: Address;
|
|
24
|
+
value: bigint;
|
|
25
|
+
validAfter: bigint;
|
|
26
|
+
validBefore: bigint;
|
|
27
|
+
nonce: Hex;
|
|
28
|
+
v: number;
|
|
29
|
+
r: Hex;
|
|
30
|
+
s: Hex;
|
|
31
|
+
}
|
|
32
|
+
interface RegisterArgs {
|
|
33
|
+
terms: Terms;
|
|
34
|
+
signature: Hex;
|
|
35
|
+
commitment: bigint;
|
|
36
|
+
/** zero address for none */
|
|
37
|
+
referrer: Address;
|
|
38
|
+
salt: Hex;
|
|
39
|
+
/** ignored by the contract when the price is zero */
|
|
40
|
+
payment: SettlementPayment;
|
|
41
|
+
}
|
|
42
|
+
/** A Semaphore proof over the subject's group, with `reader` as its message. */
|
|
43
|
+
interface SettleArgs {
|
|
44
|
+
subject: Hex;
|
|
45
|
+
reader: Address;
|
|
46
|
+
merkleTreeDepth: bigint;
|
|
47
|
+
merkleTreeRoot: bigint;
|
|
48
|
+
nullifier: bigint;
|
|
49
|
+
points: readonly [bigint, bigint, bigint, bigint, bigint, bigint, bigint, bigint];
|
|
50
|
+
}
|
|
51
|
+
declare const TERMS_TYPES: {
|
|
52
|
+
readonly Terms: readonly [{
|
|
53
|
+
readonly name: "publisher";
|
|
54
|
+
readonly type: "address";
|
|
55
|
+
}, {
|
|
56
|
+
readonly name: "repo";
|
|
57
|
+
readonly type: "bytes32";
|
|
58
|
+
}, {
|
|
59
|
+
readonly name: "key";
|
|
60
|
+
readonly type: "bytes32";
|
|
61
|
+
}, {
|
|
62
|
+
readonly name: "price";
|
|
63
|
+
readonly type: "uint256";
|
|
64
|
+
}, {
|
|
65
|
+
readonly name: "shareBps";
|
|
66
|
+
readonly type: "uint16";
|
|
67
|
+
}, {
|
|
68
|
+
readonly name: "generation";
|
|
69
|
+
readonly type: "uint64";
|
|
70
|
+
}, {
|
|
71
|
+
readonly name: "deadline";
|
|
72
|
+
readonly type: "uint64";
|
|
73
|
+
}];
|
|
74
|
+
};
|
|
75
|
+
declare const settlementDomain: (chainId: number, registry: Address) => TypedDataDomain;
|
|
76
|
+
/** A record's key within its repo. */
|
|
77
|
+
declare const recordKey: (id: string) => Hex;
|
|
78
|
+
declare const subjectOf: (publisher: Address, repo: Hex, key?: Hex) => Hex;
|
|
79
|
+
/** The nonce a payer signs for `register`. */
|
|
80
|
+
declare const registerNonce: (subject: Hex, commitment: bigint, referrer: Address, salt: Hex) => Hex;
|
|
81
|
+
/**
|
|
82
|
+
* Client for the SettlementRegistry.
|
|
83
|
+
*
|
|
84
|
+
* Publishers sign terms (no transaction) and manage generations, denials and takedowns.
|
|
85
|
+
* `register` and `settle` take what a buyer signed and proved, so anyone may submit them.
|
|
86
|
+
* Semaphore identities and proofs live with the buyer, not here.
|
|
87
|
+
*/
|
|
88
|
+
declare class SettlementRegistryClient {
|
|
89
|
+
private contractAddress;
|
|
90
|
+
private publicClient;
|
|
91
|
+
private walletClient?;
|
|
92
|
+
constructor(contractAddress: Address, publicClient: PublicClient, walletClient?: WalletClient | undefined);
|
|
93
|
+
getAddress(): Address;
|
|
94
|
+
private write;
|
|
95
|
+
private read;
|
|
96
|
+
/** Sign terms as this client's wallet, at the repo's current generation unless given. */
|
|
97
|
+
signTerms(t: Omit<Terms, "publisher" | "generation"> & {
|
|
98
|
+
generation?: bigint;
|
|
99
|
+
}): Promise<{
|
|
100
|
+
terms: Terms;
|
|
101
|
+
signature: Hex;
|
|
102
|
+
}>;
|
|
103
|
+
raiseGeneration(repo: Hex): Promise<Hash>;
|
|
104
|
+
setDenied(referrer: Address, denied: boolean): Promise<Hash>;
|
|
105
|
+
/** Takes down (or restores) one of this wallet's own subjects. */
|
|
106
|
+
setDisabled(repo: Hex, key: Hex, disabled: boolean): Promise<Hash>;
|
|
107
|
+
register(a: RegisterArgs): Promise<Hash>;
|
|
108
|
+
settle(a: SettleArgs): Promise<Hash>;
|
|
109
|
+
generationOf(publisher: Address, repo: Hex): Promise<bigint>;
|
|
110
|
+
isDenied(publisher: Address, referrer: Address): Promise<boolean>;
|
|
111
|
+
isDisabled(subject: Hex): Promise<boolean>;
|
|
112
|
+
isRegistered(subject: Hex, commitment: bigint): Promise<boolean>;
|
|
113
|
+
isSettled(reader: Address, subject: Hex): Promise<boolean>;
|
|
114
|
+
/** Settled for the record or the whole repo, and neither is disabled. */
|
|
115
|
+
canRead(reader: Address, publisher: Address, repo: Hex, key: Hex): Promise<boolean>;
|
|
116
|
+
}
|
|
117
|
+
/** Signed terms as a record carries them under `"@terms"`: bigints as decimal strings. */
|
|
118
|
+
interface TermsJson {
|
|
119
|
+
terms: Omit<Terms, "price" | "generation" | "deadline"> & {
|
|
120
|
+
price: string;
|
|
121
|
+
generation: string;
|
|
122
|
+
deadline: string;
|
|
123
|
+
};
|
|
124
|
+
signature: Hex;
|
|
125
|
+
}
|
|
126
|
+
declare const termsToJson: ({
|
|
127
|
+
terms,
|
|
128
|
+
signature
|
|
129
|
+
}: {
|
|
130
|
+
terms: Terms;
|
|
131
|
+
signature: Hex;
|
|
132
|
+
}) => TermsJson;
|
|
133
|
+
declare const termsFromJson: ({
|
|
134
|
+
terms,
|
|
135
|
+
signature
|
|
136
|
+
}: TermsJson) => {
|
|
137
|
+
terms: Terms;
|
|
138
|
+
signature: Hex;
|
|
139
|
+
};
|
|
140
|
+
//#endregion
|
|
141
|
+
export { RegisterArgs, SettleArgs, SettlementPayment, SettlementRegistryClient, TERMS_TYPES, Terms, TermsJson, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson };
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { requireWallet, sendWrite } from "../write.js";
|
|
2
|
+
import { SETTLEMENT_REGISTRY_ABI } from "./abi.js";
|
|
3
|
+
import { encodeAbiParameters, keccak256, toHex, zeroHash } from "viem";
|
|
4
|
+
|
|
5
|
+
//#region src/contracts/settlement-registry/index.ts
|
|
6
|
+
/** The key of a whole-repo subject. */
|
|
7
|
+
const WHOLE_REPO = zeroHash;
|
|
8
|
+
const ZERO_PAYMENT = {
|
|
9
|
+
from: "0x0000000000000000000000000000000000000000",
|
|
10
|
+
value: 0n,
|
|
11
|
+
validAfter: 0n,
|
|
12
|
+
validBefore: 0n,
|
|
13
|
+
nonce: zeroHash,
|
|
14
|
+
v: 0,
|
|
15
|
+
r: zeroHash,
|
|
16
|
+
s: zeroHash
|
|
17
|
+
};
|
|
18
|
+
const TERMS_TYPES = { Terms: [
|
|
19
|
+
{
|
|
20
|
+
name: "publisher",
|
|
21
|
+
type: "address"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "repo",
|
|
25
|
+
type: "bytes32"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: "key",
|
|
29
|
+
type: "bytes32"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "price",
|
|
33
|
+
type: "uint256"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "shareBps",
|
|
37
|
+
type: "uint16"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: "generation",
|
|
41
|
+
type: "uint64"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: "deadline",
|
|
45
|
+
type: "uint64"
|
|
46
|
+
}
|
|
47
|
+
] };
|
|
48
|
+
const settlementDomain = (chainId, registry) => ({
|
|
49
|
+
name: "Fangorn Settlement",
|
|
50
|
+
version: "1",
|
|
51
|
+
chainId,
|
|
52
|
+
verifyingContract: registry
|
|
53
|
+
});
|
|
54
|
+
/** A record's key within its repo. */
|
|
55
|
+
const recordKey = (id) => keccak256(toHex(id));
|
|
56
|
+
const subjectOf = (publisher, repo, key = WHOLE_REPO) => keccak256(encodeAbiParameters([
|
|
57
|
+
{ type: "address" },
|
|
58
|
+
{ type: "bytes32" },
|
|
59
|
+
{ type: "bytes32" }
|
|
60
|
+
], [
|
|
61
|
+
publisher,
|
|
62
|
+
repo,
|
|
63
|
+
key
|
|
64
|
+
]));
|
|
65
|
+
/** The nonce a payer signs for `register`. */
|
|
66
|
+
const registerNonce = (subject, commitment, referrer, salt) => keccak256(encodeAbiParameters([
|
|
67
|
+
{ type: "bytes32" },
|
|
68
|
+
{ type: "uint256" },
|
|
69
|
+
{ type: "address" },
|
|
70
|
+
{ type: "bytes32" }
|
|
71
|
+
], [
|
|
72
|
+
subject,
|
|
73
|
+
commitment,
|
|
74
|
+
referrer,
|
|
75
|
+
salt
|
|
76
|
+
]));
|
|
77
|
+
/**
|
|
78
|
+
* Client for the SettlementRegistry.
|
|
79
|
+
*
|
|
80
|
+
* Publishers sign terms (no transaction) and manage generations, denials and takedowns.
|
|
81
|
+
* `register` and `settle` take what a buyer signed and proved, so anyone may submit them.
|
|
82
|
+
* Semaphore identities and proofs live with the buyer, not here.
|
|
83
|
+
*/
|
|
84
|
+
var SettlementRegistryClient = class {
|
|
85
|
+
constructor(contractAddress, publicClient, walletClient) {
|
|
86
|
+
this.contractAddress = contractAddress;
|
|
87
|
+
this.publicClient = publicClient;
|
|
88
|
+
this.walletClient = walletClient;
|
|
89
|
+
}
|
|
90
|
+
getAddress() {
|
|
91
|
+
return this.contractAddress;
|
|
92
|
+
}
|
|
93
|
+
write(functionName, args) {
|
|
94
|
+
return sendWrite(this.publicClient, requireWallet(this.walletClient, functionName), this.contractAddress, SETTLEMENT_REGISTRY_ABI, functionName, args);
|
|
95
|
+
}
|
|
96
|
+
read(functionName, args = []) {
|
|
97
|
+
return this.publicClient.readContract({
|
|
98
|
+
address: this.contractAddress,
|
|
99
|
+
abi: SETTLEMENT_REGISTRY_ABI,
|
|
100
|
+
functionName,
|
|
101
|
+
args
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/** Sign terms as this client's wallet, at the repo's current generation unless given. */
|
|
105
|
+
async signTerms(t) {
|
|
106
|
+
const wallet = requireWallet(this.walletClient, "signTerms");
|
|
107
|
+
const account = wallet.account;
|
|
108
|
+
if (!account) throw new Error("signTerms: the wallet client has no account");
|
|
109
|
+
if (!Number.isInteger(t.shareBps) || t.shareBps < 0 || t.shareBps > 1e4) throw new Error(`signTerms: shareBps must be an integer in 0..10000, got ${String(t.shareBps)}`);
|
|
110
|
+
const terms = {
|
|
111
|
+
...t,
|
|
112
|
+
publisher: account.address,
|
|
113
|
+
generation: t.generation ?? await this.generationOf(account.address, t.repo)
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
terms,
|
|
117
|
+
signature: await wallet.signTypedData({
|
|
118
|
+
account,
|
|
119
|
+
domain: settlementDomain(await this.publicClient.getChainId(), this.contractAddress),
|
|
120
|
+
types: TERMS_TYPES,
|
|
121
|
+
primaryType: "Terms",
|
|
122
|
+
message: terms
|
|
123
|
+
})
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
raiseGeneration(repo) {
|
|
127
|
+
return this.write("raiseGeneration", [repo]);
|
|
128
|
+
}
|
|
129
|
+
setDenied(referrer, denied) {
|
|
130
|
+
return this.write("setDenied", [referrer, denied]);
|
|
131
|
+
}
|
|
132
|
+
/** Takes down (or restores) one of this wallet's own subjects. */
|
|
133
|
+
setDisabled(repo, key, disabled) {
|
|
134
|
+
const account = requireWallet(this.walletClient, "setDisabled").account;
|
|
135
|
+
if (!account) throw new Error("setDisabled: the wallet client has no account");
|
|
136
|
+
return this.write("setDisabled", [
|
|
137
|
+
account.address,
|
|
138
|
+
repo,
|
|
139
|
+
key,
|
|
140
|
+
disabled
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
register(a) {
|
|
144
|
+
const payment = a.terms.price === 0n ? ZERO_PAYMENT : a.payment;
|
|
145
|
+
return this.write("register", [
|
|
146
|
+
a.terms,
|
|
147
|
+
a.signature,
|
|
148
|
+
a.commitment,
|
|
149
|
+
a.referrer,
|
|
150
|
+
a.salt,
|
|
151
|
+
payment
|
|
152
|
+
]);
|
|
153
|
+
}
|
|
154
|
+
settle(a) {
|
|
155
|
+
return this.write("settle", [
|
|
156
|
+
a.subject,
|
|
157
|
+
a.reader,
|
|
158
|
+
a.merkleTreeDepth,
|
|
159
|
+
a.merkleTreeRoot,
|
|
160
|
+
a.nullifier,
|
|
161
|
+
a.points
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
generationOf(publisher, repo) {
|
|
165
|
+
return this.read("generationOf", [publisher, repo]);
|
|
166
|
+
}
|
|
167
|
+
isDenied(publisher, referrer) {
|
|
168
|
+
return this.read("isDenied", [publisher, referrer]);
|
|
169
|
+
}
|
|
170
|
+
isDisabled(subject) {
|
|
171
|
+
return this.read("isDisabled", [subject]);
|
|
172
|
+
}
|
|
173
|
+
isRegistered(subject, commitment) {
|
|
174
|
+
return this.read("isRegistered", [subject, commitment]);
|
|
175
|
+
}
|
|
176
|
+
isSettled(reader, subject) {
|
|
177
|
+
return this.read("isSettled", [reader, subject]);
|
|
178
|
+
}
|
|
179
|
+
/** Settled for the record or the whole repo, and neither is disabled. */
|
|
180
|
+
canRead(reader, publisher, repo, key) {
|
|
181
|
+
return this.read("canRead", [
|
|
182
|
+
reader,
|
|
183
|
+
publisher,
|
|
184
|
+
repo,
|
|
185
|
+
key
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const termsToJson = ({ terms, signature }) => ({
|
|
190
|
+
terms: {
|
|
191
|
+
...terms,
|
|
192
|
+
price: terms.price.toString(),
|
|
193
|
+
generation: terms.generation.toString(),
|
|
194
|
+
deadline: terms.deadline.toString()
|
|
195
|
+
},
|
|
196
|
+
signature
|
|
197
|
+
});
|
|
198
|
+
const termsFromJson = ({ terms, signature }) => ({
|
|
199
|
+
terms: {
|
|
200
|
+
...terms,
|
|
201
|
+
price: BigInt(terms.price),
|
|
202
|
+
generation: BigInt(terms.generation),
|
|
203
|
+
deadline: BigInt(terms.deadline)
|
|
204
|
+
},
|
|
205
|
+
signature
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
export { SettlementRegistryClient, TERMS_TYPES, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson };
|
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): Uint8Array;
|
|
12
|
+
declare function aesGcmEncrypt(key: Uint8Array, plaintext: Uint8Array, nonce: Uint8Array, aad?: 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): Uint8Array;
|
|
17
|
+
declare function aesGcmDecrypt(key: Uint8Array, sealed: Uint8Array, nonce: Uint8Array, aad?: 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).encrypt(plaintext);
|
|
14
|
+
function aesGcmEncrypt(key, plaintext, nonce, aad) {
|
|
15
|
+
return gcm(key, nonce, aad).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).decrypt(sealed);
|
|
21
|
+
function aesGcmDecrypt(key, sealed, nonce, aad) {
|
|
22
|
+
return gcm(key, nonce, aad).decrypt(sealed);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
//#endregion
|
|
@@ -1,57 +1,23 @@
|
|
|
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. */
|
|
4
5
|
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
|
-
}
|
|
33
6
|
/** Length of an X25519 public key, in bytes. */
|
|
34
7
|
declare const X25519_PUBKEY_LENGTH = 32;
|
|
35
|
-
declare const sha256Hex: (bytes: Uint8Array) => Hex;
|
|
36
8
|
/**
|
|
37
|
-
*
|
|
38
|
-
* to the resourceId.
|
|
9
|
+
* Ephemeral-static ECDH to the recipient's static key, keyed to the resourceId.
|
|
39
10
|
*
|
|
40
11
|
* ciphertext = ephemeralPub(32) || nonce(12) || aes-256-gcm-ct
|
|
41
12
|
*
|
|
42
|
-
* Only the holder of the secret matching `
|
|
13
|
+
* Only the holder of the secret matching `recipientPubkey` can derive the same AES
|
|
43
14
|
* key, and only with the matching `resourceId`.
|
|
44
15
|
*/
|
|
45
|
-
declare function seal(plaintext: Uint8Array,
|
|
46
|
-
/**
|
|
47
|
-
|
|
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;
|
|
16
|
+
declare function seal(plaintext: Uint8Array, recipientPubkey: Uint8Array, resourceId: Hex): Uint8Array;
|
|
17
|
+
/** Inverse of {@link seal}, with the recipient's secret and the same resourceId. */
|
|
18
|
+
declare function unseal(ciphertext: Uint8Array, recipientSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
53
19
|
/**
|
|
54
|
-
*
|
|
20
|
+
* Derive the AES key straight from the owner's own 32-byte
|
|
55
21
|
* secret, bound to the resourceId. No ECDH, no recipient — only a party holding
|
|
56
22
|
* `ownSecret` can re-derive the key.
|
|
57
23
|
*
|
|
@@ -60,34 +26,6 @@ declare function unseal(ciphertext: Uint8Array, workerSecret: Uint8Array, resour
|
|
|
60
26
|
declare function sealSelf(plaintext: Uint8Array, ownSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
61
27
|
/** Inverse of {@link sealSelf}. */
|
|
62
28
|
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>;
|
|
91
29
|
/** Anything that can personal_sign a raw 32-byte hash (viem LocalAccount / WalletClient). */
|
|
92
30
|
interface AccessSigner {
|
|
93
31
|
signMessage(args: {
|
|
@@ -96,44 +34,5 @@ interface AccessSigner {
|
|
|
96
34
|
};
|
|
97
35
|
}): Promise<Hex>;
|
|
98
36
|
}
|
|
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>;
|
|
138
37
|
//#endregion
|
|
139
|
-
export {
|
|
38
|
+
export { AccessSigner, GADGET_SELF_HKDF_V1, X25519_PUBKEY_LENGTH, seal, sealSelf, unseal, unsealSelf };
|