@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
|
@@ -1,143 +0,0 @@
|
|
|
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
|
-
/** Every commitment in the subject's group, in the order it was added: what a Semaphore proof is built from. */
|
|
113
|
-
membersOf(subject: Hex): Promise<readonly bigint[]>;
|
|
114
|
-
isRegistered(subject: Hex, commitment: bigint): Promise<boolean>;
|
|
115
|
-
isSettled(reader: Address, subject: Hex): Promise<boolean>;
|
|
116
|
-
/** Settled for the record or the whole repo, and neither is disabled. */
|
|
117
|
-
canRead(reader: Address, publisher: Address, repo: Hex, key: Hex): Promise<boolean>;
|
|
118
|
-
}
|
|
119
|
-
/** Signed terms as a record carries them under `"@terms"`: bigints as decimal strings. */
|
|
120
|
-
interface TermsJson {
|
|
121
|
-
terms: Omit<Terms, "price" | "generation" | "deadline"> & {
|
|
122
|
-
price: string;
|
|
123
|
-
generation: string;
|
|
124
|
-
deadline: string;
|
|
125
|
-
};
|
|
126
|
-
signature: Hex;
|
|
127
|
-
}
|
|
128
|
-
declare const termsToJson: ({
|
|
129
|
-
terms,
|
|
130
|
-
signature
|
|
131
|
-
}: {
|
|
132
|
-
terms: Terms;
|
|
133
|
-
signature: Hex;
|
|
134
|
-
}) => TermsJson;
|
|
135
|
-
declare const termsFromJson: ({
|
|
136
|
-
terms,
|
|
137
|
-
signature
|
|
138
|
-
}: TermsJson) => {
|
|
139
|
-
terms: Terms;
|
|
140
|
-
signature: Hex;
|
|
141
|
-
};
|
|
142
|
-
//#endregion
|
|
143
|
-
export { RegisterArgs, SettleArgs, SettlementPayment, SettlementRegistryClient, TERMS_TYPES, Terms, TermsJson, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson };
|
|
@@ -1,213 +0,0 @@
|
|
|
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
|
-
/** Every commitment in the subject's group, in the order it was added: what a Semaphore proof is built from. */
|
|
174
|
-
membersOf(subject) {
|
|
175
|
-
return this.read("membersOf", [subject]);
|
|
176
|
-
}
|
|
177
|
-
isRegistered(subject, commitment) {
|
|
178
|
-
return this.read("isRegistered", [subject, commitment]);
|
|
179
|
-
}
|
|
180
|
-
isSettled(reader, subject) {
|
|
181
|
-
return this.read("isSettled", [reader, subject]);
|
|
182
|
-
}
|
|
183
|
-
/** Settled for the record or the whole repo, and neither is disabled. */
|
|
184
|
-
canRead(reader, publisher, repo, key) {
|
|
185
|
-
return this.read("canRead", [
|
|
186
|
-
reader,
|
|
187
|
-
publisher,
|
|
188
|
-
repo,
|
|
189
|
-
key
|
|
190
|
-
]);
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
const termsToJson = ({ terms, signature }) => ({
|
|
194
|
-
terms: {
|
|
195
|
-
...terms,
|
|
196
|
-
price: terms.price.toString(),
|
|
197
|
-
generation: terms.generation.toString(),
|
|
198
|
-
deadline: terms.deadline.toString()
|
|
199
|
-
},
|
|
200
|
-
signature
|
|
201
|
-
});
|
|
202
|
-
const termsFromJson = ({ terms, signature }) => ({
|
|
203
|
-
terms: {
|
|
204
|
-
...terms,
|
|
205
|
-
price: BigInt(terms.price),
|
|
206
|
-
generation: BigInt(terms.generation),
|
|
207
|
-
deadline: BigInt(terms.deadline)
|
|
208
|
-
},
|
|
209
|
-
signature
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
//#endregion
|
|
213
|
-
export { SettlementRegistryClient, TERMS_TYPES, WHOLE_REPO, recordKey, registerNonce, settlementDomain, subjectOf, termsFromJson, termsToJson };
|
package/lib/crypto/paid.d.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { MetadataStorage } from "../providers/storage/types.js";
|
|
2
|
-
import { AccessSigner } from "./encryption.js";
|
|
3
|
-
import { Address, Hex } from "viem";
|
|
4
|
-
|
|
5
|
-
//#region src/crypto/paid.d.ts
|
|
6
|
-
declare const GADGET_FIELD_V1 = "field-v1";
|
|
7
|
-
/** What a repo key is sealed under. `repo` is `repoId(name)`. */
|
|
8
|
-
interface PaidScope {
|
|
9
|
-
chainId: number;
|
|
10
|
-
registry: Address;
|
|
11
|
-
publisher: Address;
|
|
12
|
-
repo: Hex;
|
|
13
|
-
generation: bigint;
|
|
14
|
-
}
|
|
15
|
-
/** The repo key, sealed to a key server. Public: it goes in the repo as a record. */
|
|
16
|
-
interface Deposit {
|
|
17
|
-
"@type": "deposit";
|
|
18
|
-
gadget: typeof GADGET_FIELD_V1;
|
|
19
|
-
chainId: number;
|
|
20
|
-
registry: Address;
|
|
21
|
-
publisher: Address;
|
|
22
|
-
repo: Hex;
|
|
23
|
-
generation: string;
|
|
24
|
-
keyServer: string;
|
|
25
|
-
sealed: Hex;
|
|
26
|
-
}
|
|
27
|
-
/** A paid field's value in a record payload. */
|
|
28
|
-
interface FieldHandle {
|
|
29
|
-
"@type": "handle";
|
|
30
|
-
gadget: typeof GADGET_FIELD_V1;
|
|
31
|
-
generation: string;
|
|
32
|
-
cid: string;
|
|
33
|
-
}
|
|
34
|
-
/** What a buyer posts to a key server's `/access`. */
|
|
35
|
-
interface AccessRequest {
|
|
36
|
-
publisher: Address;
|
|
37
|
-
repo: Hex;
|
|
38
|
-
generation: string;
|
|
39
|
-
/** `recordKey(id)`, or `WHOLE_REPO` for the repo key */
|
|
40
|
-
key: Hex;
|
|
41
|
-
/** A throwaway X25519 public key; the reply is sealed to it. */
|
|
42
|
-
readerKey: Hex;
|
|
43
|
-
/** The deposit's `sealed`. */
|
|
44
|
-
sealed: Hex;
|
|
45
|
-
timestamp: number;
|
|
46
|
-
signature: Hex;
|
|
47
|
-
}
|
|
48
|
-
declare const statement: (s: PaidScope) => Hex;
|
|
49
|
-
/**
|
|
50
|
-
* A random repo key. The publisher doesn't keep it: to seal more records later,
|
|
51
|
-
* it asks the key server for it (`requestKey` with `WHOLE_REPO`), like a buyer
|
|
52
|
-
* of the whole repo.
|
|
53
|
-
*/
|
|
54
|
-
declare const newRepoKey: () => Uint8Array;
|
|
55
|
-
declare const deriveRecordKey: (kRepo: Uint8Array, stmt: Hex, key: Hex) => Uint8Array;
|
|
56
|
-
/** `nonce(12) || AES-256-GCM(kRecord, plaintext, aad = stmt || key || field)` */
|
|
57
|
-
declare function encryptField(kRecord: Uint8Array, plaintext: Uint8Array, stmt: Hex, key: Hex, field: string): Uint8Array;
|
|
58
|
-
declare function decryptField(kRecord: Uint8Array, ciphertext: Uint8Array, stmt: Hex, key: Hex, field: string): Uint8Array;
|
|
59
|
-
declare function makeDeposit(p: {
|
|
60
|
-
kRepo: Uint8Array;
|
|
61
|
-
keyServerPubkey: Uint8Array;
|
|
62
|
-
keyServer: string;
|
|
63
|
-
scope: PaidScope;
|
|
64
|
-
}): Deposit;
|
|
65
|
-
/** Encrypt one field, upload the ciphertext, and return the handle that replaces the value. */
|
|
66
|
-
declare function sealField(p: {
|
|
67
|
-
plaintext: Uint8Array;
|
|
68
|
-
kRepo: Uint8Array;
|
|
69
|
-
scope: PaidScope;
|
|
70
|
-
/** `recordKey(id)` */
|
|
71
|
-
key: Hex;
|
|
72
|
-
field: string;
|
|
73
|
-
storage: Pick<MetadataStorage, "putFile">;
|
|
74
|
-
}): Promise<FieldHandle>;
|
|
75
|
-
/** What the reader signs (EIP-191, raw): the statement, the key asked for, the reply key and the time. */
|
|
76
|
-
declare const accessMessage: (stmt: Hex, key: Hex, readerKey: Hex, timestamp: number) => Hex;
|
|
77
|
-
declare function buildAccessRequest(p: {
|
|
78
|
-
signer: AccessSigner;
|
|
79
|
-
deposit: Deposit;
|
|
80
|
-
key: Hex;
|
|
81
|
-
readerKey: Hex;
|
|
82
|
-
/** unix seconds; defaults to now */
|
|
83
|
-
timestamp?: number;
|
|
84
|
-
}): Promise<AccessRequest>;
|
|
85
|
-
/** Ask the deposit's key server for `key`'s key: the record key, or the repo key for `WHOLE_REPO`. */
|
|
86
|
-
declare function requestKey(p: {
|
|
87
|
-
deposit: Deposit;
|
|
88
|
-
key: Hex;
|
|
89
|
-
signer: AccessSigner;
|
|
90
|
-
fetch?: typeof globalThis.fetch;
|
|
91
|
-
}): Promise<Uint8Array>;
|
|
92
|
-
/** Get the record's key, fetch the field's ciphertext and decrypt it. */
|
|
93
|
-
declare function openField(p: {
|
|
94
|
-
handle: FieldHandle;
|
|
95
|
-
deposit: Deposit;
|
|
96
|
-
/** `recordKey(id)` */
|
|
97
|
-
key: Hex;
|
|
98
|
-
field: string;
|
|
99
|
-
signer: AccessSigner;
|
|
100
|
-
storage: Pick<MetadataStorage, "getFile">;
|
|
101
|
-
fetch?: typeof globalThis.fetch;
|
|
102
|
-
}): Promise<Uint8Array>;
|
|
103
|
-
//#endregion
|
|
104
|
-
export { AccessRequest, Deposit, FieldHandle, GADGET_FIELD_V1, PaidScope, accessMessage, buildAccessRequest, decryptField, deriveRecordKey, encryptField, makeDeposit, newRepoKey, openField, requestKey, sealField, statement };
|
package/lib/crypto/paid.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { GCM_NONCE_LENGTH, aesGcmDecrypt, aesGcmEncrypt } from "./aes.js";
|
|
2
|
-
import { getRandomValues } from "./rand.js";
|
|
3
|
-
import { seal, unseal } from "./encryption.js";
|
|
4
|
-
import { bytesToHex, concat, encodeAbiParameters, hexToBytes, keccak256, toHex } from "viem";
|
|
5
|
-
import { sha256 } from "@noble/hashes/sha2.js";
|
|
6
|
-
import { x25519 } from "@noble/curves/ed25519";
|
|
7
|
-
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
8
|
-
|
|
9
|
-
//#region src/crypto/paid.ts
|
|
10
|
-
const GADGET_FIELD_V1 = "field-v1";
|
|
11
|
-
const hkdf32 = (ikm, info) => hkdf(sha256, ikm, void 0, typeof info === "string" ? hexToBytes(info) : info, 32);
|
|
12
|
-
const statement = (s) => keccak256(encodeAbiParameters([
|
|
13
|
-
{ type: "string" },
|
|
14
|
-
{ type: "uint256" },
|
|
15
|
-
{ type: "address" },
|
|
16
|
-
{ type: "address" },
|
|
17
|
-
{ type: "bytes32" },
|
|
18
|
-
{ type: "uint64" }
|
|
19
|
-
], [
|
|
20
|
-
"fangorn:pay:v1",
|
|
21
|
-
BigInt(s.chainId),
|
|
22
|
-
s.registry,
|
|
23
|
-
s.publisher,
|
|
24
|
-
s.repo,
|
|
25
|
-
s.generation
|
|
26
|
-
]));
|
|
27
|
-
const scopeOf = (d) => ({
|
|
28
|
-
chainId: d.chainId,
|
|
29
|
-
registry: d.registry,
|
|
30
|
-
publisher: d.publisher,
|
|
31
|
-
repo: d.repo,
|
|
32
|
-
generation: BigInt(d.generation)
|
|
33
|
-
});
|
|
34
|
-
/**
|
|
35
|
-
* A random repo key. The publisher doesn't keep it: to seal more records later,
|
|
36
|
-
* it asks the key server for it (`requestKey` with `WHOLE_REPO`), like a buyer
|
|
37
|
-
* of the whole repo.
|
|
38
|
-
*/
|
|
39
|
-
const newRepoKey = () => getRandomValues(new Uint8Array(32));
|
|
40
|
-
const deriveRecordKey = (kRepo, stmt, key) => hkdf32(kRepo, concat([
|
|
41
|
-
stmt,
|
|
42
|
-
key,
|
|
43
|
-
toHex("fangorn:record-key")
|
|
44
|
-
]));
|
|
45
|
-
const fieldAad = (stmt, key, field) => hexToBytes(concat([
|
|
46
|
-
stmt,
|
|
47
|
-
key,
|
|
48
|
-
toHex(field)
|
|
49
|
-
]));
|
|
50
|
-
/** `nonce(12) || AES-256-GCM(kRecord, plaintext, aad = stmt || key || field)` */
|
|
51
|
-
function encryptField(kRecord, plaintext, stmt, key, field) {
|
|
52
|
-
const nonce = getRandomValues(new Uint8Array(GCM_NONCE_LENGTH));
|
|
53
|
-
const ct = aesGcmEncrypt(kRecord, plaintext, nonce, fieldAad(stmt, key, field));
|
|
54
|
-
const out = new Uint8Array(nonce.length + ct.length);
|
|
55
|
-
out.set(nonce);
|
|
56
|
-
out.set(ct, nonce.length);
|
|
57
|
-
return out;
|
|
58
|
-
}
|
|
59
|
-
function decryptField(kRecord, ciphertext, stmt, key, field) {
|
|
60
|
-
return aesGcmDecrypt(kRecord, ciphertext.slice(GCM_NONCE_LENGTH), ciphertext.slice(0, GCM_NONCE_LENGTH), fieldAad(stmt, key, field));
|
|
61
|
-
}
|
|
62
|
-
function makeDeposit(p) {
|
|
63
|
-
const { scope } = p;
|
|
64
|
-
return {
|
|
65
|
-
"@type": "deposit",
|
|
66
|
-
gadget: GADGET_FIELD_V1,
|
|
67
|
-
chainId: scope.chainId,
|
|
68
|
-
registry: scope.registry,
|
|
69
|
-
publisher: scope.publisher,
|
|
70
|
-
repo: scope.repo,
|
|
71
|
-
generation: scope.generation.toString(),
|
|
72
|
-
keyServer: p.keyServer,
|
|
73
|
-
sealed: bytesToHex(seal(p.kRepo, p.keyServerPubkey, statement(scope)))
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
/** Encrypt one field, upload the ciphertext, and return the handle that replaces the value. */
|
|
77
|
-
async function sealField(p) {
|
|
78
|
-
const stmt = statement(p.scope);
|
|
79
|
-
const ct = encryptField(deriveRecordKey(p.kRepo, stmt, p.key), p.plaintext, stmt, p.key, p.field);
|
|
80
|
-
return {
|
|
81
|
-
"@type": "handle",
|
|
82
|
-
gadget: GADGET_FIELD_V1,
|
|
83
|
-
generation: p.scope.generation.toString(),
|
|
84
|
-
cid: await p.storage.putFile(ct, `${p.key}.${p.field}`)
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
/** What the reader signs (EIP-191, raw): the statement, the key asked for, the reply key and the time. */
|
|
88
|
-
const accessMessage = (stmt, key, readerKey, timestamp) => keccak256(encodeAbiParameters([
|
|
89
|
-
{ type: "string" },
|
|
90
|
-
{ type: "bytes32" },
|
|
91
|
-
{ type: "bytes32" },
|
|
92
|
-
{ type: "bytes32" },
|
|
93
|
-
{ type: "uint64" }
|
|
94
|
-
], [
|
|
95
|
-
"fangorn:access:v1",
|
|
96
|
-
stmt,
|
|
97
|
-
key,
|
|
98
|
-
readerKey,
|
|
99
|
-
BigInt(timestamp)
|
|
100
|
-
]));
|
|
101
|
-
async function buildAccessRequest(p) {
|
|
102
|
-
const { deposit, key, readerKey } = p;
|
|
103
|
-
const timestamp = p.timestamp ?? Math.floor(Date.now() / 1e3);
|
|
104
|
-
const raw = accessMessage(statement(scopeOf(deposit)), key, readerKey, timestamp);
|
|
105
|
-
return {
|
|
106
|
-
publisher: deposit.publisher,
|
|
107
|
-
repo: deposit.repo,
|
|
108
|
-
generation: deposit.generation,
|
|
109
|
-
key,
|
|
110
|
-
readerKey,
|
|
111
|
-
sealed: deposit.sealed,
|
|
112
|
-
timestamp,
|
|
113
|
-
signature: await p.signer.signMessage({ message: { raw } })
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
/** Ask the deposit's key server for `key`'s key: the record key, or the repo key for `WHOLE_REPO`. */
|
|
117
|
-
async function requestKey(p) {
|
|
118
|
-
const secret = x25519.utils.randomSecretKey();
|
|
119
|
-
const req = await buildAccessRequest({
|
|
120
|
-
signer: p.signer,
|
|
121
|
-
deposit: p.deposit,
|
|
122
|
-
key: p.key,
|
|
123
|
-
readerKey: bytesToHex(x25519.getPublicKey(secret))
|
|
124
|
-
});
|
|
125
|
-
const res = await (p.fetch ?? fetch)(`${p.deposit.keyServer}/access`, {
|
|
126
|
-
method: "POST",
|
|
127
|
-
headers: { "Content-Type": "application/json" },
|
|
128
|
-
body: JSON.stringify(req)
|
|
129
|
-
});
|
|
130
|
-
if (!res.ok) throw new Error(`key server refused: ${res.status.toString()} ${await res.text()}`);
|
|
131
|
-
const { sealed } = await res.json();
|
|
132
|
-
return unseal(hexToBytes(sealed), secret, statement(scopeOf(p.deposit)));
|
|
133
|
-
}
|
|
134
|
-
/** Get the record's key, fetch the field's ciphertext and decrypt it. */
|
|
135
|
-
async function openField(p) {
|
|
136
|
-
if (p.handle.generation !== p.deposit.generation) throw new Error("the handle and the deposit are for different generations");
|
|
137
|
-
const [kRecord, ct] = await Promise.all([requestKey({
|
|
138
|
-
deposit: p.deposit,
|
|
139
|
-
key: p.key,
|
|
140
|
-
signer: p.signer,
|
|
141
|
-
fetch: p.fetch
|
|
142
|
-
}), p.storage.getFile(p.handle.cid)]);
|
|
143
|
-
return decryptField(kRecord, ct, statement(scopeOf(p.deposit)), p.key, p.field);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
//#endregion
|
|
147
|
-
export { GADGET_FIELD_V1, accessMessage, buildAccessRequest, decryptField, deriveRecordKey, encryptField, makeDeposit, newRepoKey, openField, requestKey, sealField, statement };
|
package/lib/test/key-server.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AccessRequest } from "../crypto/paid.js";
|
|
2
|
-
import { Address, Hex } from "viem";
|
|
3
|
-
|
|
4
|
-
//#region src/test/key-server.d.ts
|
|
5
|
-
declare function testKeyServer(body: AccessRequest, ks: {
|
|
6
|
-
secret: Uint8Array;
|
|
7
|
-
chainId: number;
|
|
8
|
-
registry: Address;
|
|
9
|
-
canRead(reader: Address, publisher: Address, repo: Hex, key: Hex): Promise<boolean>;
|
|
10
|
-
}): Promise<Response>;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { testKeyServer };
|
package/lib/test/key-server.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { WHOLE_REPO } from "../contracts/settlement-registry/index.js";
|
|
2
|
-
import { seal, unseal } from "../crypto/encryption.js";
|
|
3
|
-
import { accessMessage, deriveRecordKey, statement } from "../crypto/paid.js";
|
|
4
|
-
import { bytesToHex, hexToBytes, recoverMessageAddress } from "viem";
|
|
5
|
-
|
|
6
|
-
//#region src/test/key-server.ts
|
|
7
|
-
async function testKeyServer(body, ks) {
|
|
8
|
-
const stmt = statement({
|
|
9
|
-
...ks,
|
|
10
|
-
publisher: body.publisher,
|
|
11
|
-
repo: body.repo,
|
|
12
|
-
generation: BigInt(body.generation)
|
|
13
|
-
});
|
|
14
|
-
const reader = await recoverMessageAddress({
|
|
15
|
-
message: { raw: accessMessage(stmt, body.key, body.readerKey, body.timestamp) },
|
|
16
|
-
signature: body.signature
|
|
17
|
-
});
|
|
18
|
-
if (!(reader.toLowerCase() === body.publisher.toLowerCase()) && !await ks.canRead(reader, body.publisher, body.repo, body.key)) return new Response("not settled", { status: 403 });
|
|
19
|
-
let kRepo;
|
|
20
|
-
try {
|
|
21
|
-
kRepo = unseal(hexToBytes(body.sealed), ks.secret, stmt);
|
|
22
|
-
} catch {
|
|
23
|
-
return new Response("deposit does not match", { status: 400 });
|
|
24
|
-
}
|
|
25
|
-
const out = body.key === WHOLE_REPO ? kRepo : deriveRecordKey(kRepo, stmt, body.key);
|
|
26
|
-
return Response.json({ sealed: bytesToHex(seal(out, hexToBytes(body.readerKey), stmt)) });
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
|
-
export { testKeyServer };
|
|
File without changes
|