@fangorn-network/sdk 0.1.0-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/LICENSE.md +20 -0
- package/README.md +80 -0
- package/lib/cli/cli.d.ts +2 -0
- package/lib/cli/cli.js +571 -0
- package/lib/cli/index.d.ts +10 -0
- package/lib/cli/index.js +18 -0
- package/lib/cli/sell.d.ts +28 -0
- package/lib/cli/sell.js +95 -0
- package/lib/cli/stage.d.ts +19 -0
- package/lib/cli/stage.js +94 -0
- package/lib/config.d.ts +697 -0
- package/lib/config.js +45 -0
- package/lib/contracts/data-registry/abi.d.ts +442 -0
- package/lib/contracts/data-registry/abi.js +508 -0
- package/lib/contracts/data-registry/index.d.ts +131 -0
- package/lib/contracts/data-registry/index.js +183 -0
- package/lib/contracts/index.d.ts +6 -0
- package/lib/contracts/index.js +6 -0
- package/lib/contracts/logs.d.ts +17 -0
- package/lib/contracts/logs.js +28 -0
- package/lib/contracts/settlement-registry/abi.d.ts +810 -0
- package/lib/contracts/settlement-registry/abi.js +962 -0
- package/lib/contracts/settlement-registry/index.d.ts +143 -0
- package/lib/contracts/settlement-registry/index.js +213 -0
- package/lib/contracts/write.d.ts +59 -0
- package/lib/contracts/write.js +102 -0
- package/lib/crypto/aes.d.ts +19 -0
- package/lib/crypto/aes.js +26 -0
- package/lib/crypto/encryption.d.ts +38 -0
- package/lib/crypto/encryption.js +68 -0
- package/lib/crypto/paid.d.ts +104 -0
- package/lib/crypto/paid.js +147 -0
- package/lib/crypto/rand.d.ts +5 -0
- package/lib/crypto/rand.js +16 -0
- package/lib/crypto/types.d.ts +23 -0
- package/lib/crypto/types.js +1 -0
- package/lib/engine/car.d.ts +14 -0
- package/lib/engine/car.js +42 -0
- package/lib/engine/index.d.ts +206 -0
- package/lib/engine/index.js +377 -0
- package/lib/engine/records.d.ts +121 -0
- package/lib/engine/records.js +239 -0
- package/lib/engine/test-helpers.d.ts +38 -0
- package/lib/engine/test-helpers.js +63 -0
- package/lib/erc8004.d.ts +56 -0
- package/lib/erc8004.js +140 -0
- package/lib/fangorn.d.ts +342 -0
- package/lib/fangorn.js +642 -0
- package/lib/feed.d.ts +34 -0
- package/lib/feed.js +67 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +16 -0
- package/lib/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +77 -0
- package/lib/node_modules/@vitest/expect/dist/index.js +1414 -0
- package/lib/node_modules/@vitest/pretty-format/dist/index.js +858 -0
- package/lib/node_modules/@vitest/runner/dist/chunk-tasks.js +51 -0
- package/lib/node_modules/@vitest/runner/dist/index.js +1199 -0
- package/lib/node_modules/@vitest/runner/dist/utils.js +3 -0
- package/lib/node_modules/@vitest/snapshot/dist/index.js +1075 -0
- package/lib/node_modules/@vitest/spy/dist/index.js +353 -0
- package/lib/node_modules/@vitest/utils/dist/chunk-_commonjsHelpers.js +7 -0
- package/lib/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +81 -0
- package/lib/node_modules/@vitest/utils/dist/diff.js +1345 -0
- package/lib/node_modules/@vitest/utils/dist/display.js +534 -0
- package/lib/node_modules/@vitest/utils/dist/error.js +30 -0
- package/lib/node_modules/@vitest/utils/dist/helpers.js +118 -0
- package/lib/node_modules/@vitest/utils/dist/serialize.js +76 -0
- package/lib/node_modules/@vitest/utils/dist/source-map.js +93 -0
- package/lib/node_modules/@vitest/utils/dist/timers.js +36 -0
- package/lib/node_modules/chai/index.js +2874 -0
- package/lib/node_modules/magic-string/dist/magic-string.es.js +939 -0
- package/lib/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +81 -0
- package/lib/node_modules/tinyrainbow/dist/index.js +85 -0
- package/lib/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +5 -0
- package/lib/node_modules/vitest/dist/chunks/date.Bq6ZW5rf.js +50 -0
- package/lib/node_modules/vitest/dist/chunks/utils.DvEY5TfP.js +44 -0
- package/lib/node_modules/vitest/dist/chunks/vi.2VT5v0um.js +2578 -0
- package/lib/providers/storage/car.d.ts +36 -0
- package/lib/providers/storage/car.js +81 -0
- package/lib/providers/storage/index.d.ts +5 -0
- package/lib/providers/storage/index.js +5 -0
- package/lib/providers/storage/pinata.d.ts +53 -0
- package/lib/providers/storage/pinata.js +178 -0
- package/lib/providers/storage/signed-url.d.ts +54 -0
- package/lib/providers/storage/signed-url.js +122 -0
- package/lib/providers/storage/types.d.ts +62 -0
- package/lib/providers/storage/types.js +1 -0
- package/lib/providers/storage/utils.d.ts +30 -0
- package/lib/providers/storage/utils.js +107 -0
- package/lib/repo/index.d.ts +56 -0
- package/lib/repo/index.js +73 -0
- package/lib/test/key-server.d.ts +12 -0
- package/lib/test/key-server.js +30 -0
- package/lib/test/mem-registry.d.ts +46 -0
- package/lib/test/mem-registry.js +114 -0
- package/lib/test/sdk-suite.d.ts +10 -0
- package/lib/test/sdk-suite.js +155 -0
- package/lib/test/testbed.d.ts +16786 -0
- package/lib/test/testbed.js +175 -0
- package/lib/types/index.d.ts +37 -0
- package/lib/types/index.js +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,143 @@
|
|
|
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 };
|
|
@@ -0,0 +1,213 @@
|
|
|
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 };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Abi, Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/write.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An unsigned transaction, hex-encoded and ready to hand to a browser wallet's
|
|
7
|
+
* `eth_sendTransaction` — no `from`, since the signing wallet supplies it.
|
|
8
|
+
*/
|
|
9
|
+
interface PreparedTx {
|
|
10
|
+
to: Address;
|
|
11
|
+
data: Hex;
|
|
12
|
+
chainId: number | undefined;
|
|
13
|
+
gas: Hex;
|
|
14
|
+
maxFeePerGas: Hex;
|
|
15
|
+
maxPriorityFeePerGas: Hex;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The one write path every contract client shares.
|
|
19
|
+
*
|
|
20
|
+
* Each client used to carry its own copy of this, which meant four places to fix
|
|
21
|
+
* whenever the gas or fee policy moved. The type-safe per-function argument
|
|
22
|
+
* checking still happens at the call site, in each client's thin `executeWrite`
|
|
23
|
+
* wrapper — only the body lives here.
|
|
24
|
+
*
|
|
25
|
+
* Two deliberate choices, both about transactions that would otherwise silently
|
|
26
|
+
* fail on a live L2:
|
|
27
|
+
*
|
|
28
|
+
* - **30% gas headroom.** The estimate is taken against the current state; by the
|
|
29
|
+
* time the transaction lands the state has moved. Unused gas is refunded, so
|
|
30
|
+
* the headroom is free and running out of it is not.
|
|
31
|
+
* - **3x maxFeePerGas.** This is a ceiling, not a payment — you pay
|
|
32
|
+
* base + priority. Quoting it tightly is how a transaction gets stuck when the
|
|
33
|
+
* base fee ticks up between estimate and inclusion.
|
|
34
|
+
*/
|
|
35
|
+
declare function sendWrite(publicClient: PublicClient, walletClient: WalletClient, address: Address, abi: Abi, functionName: string, args: readonly unknown[], value?: bigint): Promise<Hash>;
|
|
36
|
+
/** Guard for clients that accept a read-only construction. */
|
|
37
|
+
declare function requireWallet(walletClient: WalletClient | undefined, what: string): WalletClient;
|
|
38
|
+
/**
|
|
39
|
+
* Build a call as an UNSIGNED transaction for `from` to sign in their own wallet,
|
|
40
|
+
* instead of sending it from a client's wallet.
|
|
41
|
+
*
|
|
42
|
+
* Fees and gas are quoted here rather than left to the wallet. Wallet estimation
|
|
43
|
+
* runs too tight against a live L2 base fee, and an eth_estimateGas from the
|
|
44
|
+
* wallet against a public RPC is what surfaces as "Network fee Unavailable" in
|
|
45
|
+
* MetaMask. maxFeePerGas is only a ceiling — you pay base+priority — so headroom
|
|
46
|
+
* costs nothing.
|
|
47
|
+
*
|
|
48
|
+
* Simulating also turns a would-be revert into a clear error now, naming the
|
|
49
|
+
* contract's reason, instead of a cryptic wallet message after the user has
|
|
50
|
+
* already clicked sign. `simulate: false` skips it for a call that can only
|
|
51
|
+
* succeed after an earlier one in the same batch lands (a join after its
|
|
52
|
+
* approval); `fallbackGas` is then the gas, as it is when the RPC fails.
|
|
53
|
+
*/
|
|
54
|
+
declare function prepareWrite(publicClient: PublicClient, from: Address, address: Address, abi: Abi, functionName: string, args: readonly unknown[], opts: {
|
|
55
|
+
fallbackGas: bigint;
|
|
56
|
+
simulate?: boolean;
|
|
57
|
+
}): Promise<PreparedTx>;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { PreparedTx, prepareWrite, requireWallet, sendWrite };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { encodeFunctionData, toHex } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/contracts/write.ts
|
|
4
|
+
/**
|
|
5
|
+
* The one write path every contract client shares.
|
|
6
|
+
*
|
|
7
|
+
* Each client used to carry its own copy of this, which meant four places to fix
|
|
8
|
+
* whenever the gas or fee policy moved. The type-safe per-function argument
|
|
9
|
+
* checking still happens at the call site, in each client's thin `executeWrite`
|
|
10
|
+
* wrapper — only the body lives here.
|
|
11
|
+
*
|
|
12
|
+
* Two deliberate choices, both about transactions that would otherwise silently
|
|
13
|
+
* fail on a live L2:
|
|
14
|
+
*
|
|
15
|
+
* - **30% gas headroom.** The estimate is taken against the current state; by the
|
|
16
|
+
* time the transaction lands the state has moved. Unused gas is refunded, so
|
|
17
|
+
* the headroom is free and running out of it is not.
|
|
18
|
+
* - **3x maxFeePerGas.** This is a ceiling, not a payment — you pay
|
|
19
|
+
* base + priority. Quoting it tightly is how a transaction gets stuck when the
|
|
20
|
+
* base fee ticks up between estimate and inclusion.
|
|
21
|
+
*/
|
|
22
|
+
async function sendWrite(publicClient, walletClient, address, abi, functionName, args, value) {
|
|
23
|
+
if (!walletClient.chain) throw new Error("Chain required");
|
|
24
|
+
if (!walletClient.account) throw new Error("Account required");
|
|
25
|
+
const { chain, account } = walletClient;
|
|
26
|
+
const fees = await publicClient.estimateFeesPerGas();
|
|
27
|
+
const gas = await publicClient.estimateContractGas({
|
|
28
|
+
address,
|
|
29
|
+
abi,
|
|
30
|
+
functionName,
|
|
31
|
+
args,
|
|
32
|
+
account,
|
|
33
|
+
value
|
|
34
|
+
});
|
|
35
|
+
const hash = await walletClient.writeContract({
|
|
36
|
+
address,
|
|
37
|
+
abi,
|
|
38
|
+
functionName,
|
|
39
|
+
args,
|
|
40
|
+
chain,
|
|
41
|
+
account,
|
|
42
|
+
gas: gas * 130n / 100n,
|
|
43
|
+
maxFeePerGas: fees.maxFeePerGas * 3n,
|
|
44
|
+
maxPriorityFeePerGas: fees.maxPriorityFeePerGas,
|
|
45
|
+
value
|
|
46
|
+
});
|
|
47
|
+
await publicClient.waitForTransactionReceipt({ hash });
|
|
48
|
+
return hash;
|
|
49
|
+
}
|
|
50
|
+
/** Guard for clients that accept a read-only construction. */
|
|
51
|
+
function requireWallet(walletClient, what) {
|
|
52
|
+
if (!walletClient) throw new Error(`${what} requires a wallet client; this client is read-only`);
|
|
53
|
+
return walletClient;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a call as an UNSIGNED transaction for `from` to sign in their own wallet,
|
|
57
|
+
* instead of sending it from a client's wallet.
|
|
58
|
+
*
|
|
59
|
+
* Fees and gas are quoted here rather than left to the wallet. Wallet estimation
|
|
60
|
+
* runs too tight against a live L2 base fee, and an eth_estimateGas from the
|
|
61
|
+
* wallet against a public RPC is what surfaces as "Network fee Unavailable" in
|
|
62
|
+
* MetaMask. maxFeePerGas is only a ceiling — you pay base+priority — so headroom
|
|
63
|
+
* costs nothing.
|
|
64
|
+
*
|
|
65
|
+
* Simulating also turns a would-be revert into a clear error now, naming the
|
|
66
|
+
* contract's reason, instead of a cryptic wallet message after the user has
|
|
67
|
+
* already clicked sign. `simulate: false` skips it for a call that can only
|
|
68
|
+
* succeed after an earlier one in the same batch lands (a join after its
|
|
69
|
+
* approval); `fallbackGas` is then the gas, as it is when the RPC fails.
|
|
70
|
+
*/
|
|
71
|
+
async function prepareWrite(publicClient, from, address, abi, functionName, args, opts) {
|
|
72
|
+
const data = encodeFunctionData({
|
|
73
|
+
abi,
|
|
74
|
+
functionName,
|
|
75
|
+
args
|
|
76
|
+
});
|
|
77
|
+
const fees = await publicClient.estimateFeesPerGas();
|
|
78
|
+
let gas = opts.fallbackGas;
|
|
79
|
+
if (opts.simulate ?? true) try {
|
|
80
|
+
gas = await publicClient.estimateContractGas({
|
|
81
|
+
address,
|
|
82
|
+
abi,
|
|
83
|
+
functionName,
|
|
84
|
+
args,
|
|
85
|
+
account: from
|
|
86
|
+
}) * 3n / 2n;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
89
|
+
if (/revert/i.test(message)) throw new Error(`${functionName} would revert: ${message}`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
to: address,
|
|
93
|
+
data,
|
|
94
|
+
chainId: publicClient.chain?.id,
|
|
95
|
+
gas: toHex(gas),
|
|
96
|
+
maxFeePerGas: toHex(fees.maxFeePerGas * 2n),
|
|
97
|
+
maxPriorityFeePerGas: toHex(fees.maxPriorityFeePerGas)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
export { prepareWrite, requireWallet, sendWrite };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/crypto/aes.d.ts
|
|
2
|
+
/** AES-256 key length, in bytes. */
|
|
3
|
+
declare const AES_KEY_LENGTH = 32;
|
|
4
|
+
/** GCM nonce (IV) length, in bytes. The standard 96-bit nonce. */
|
|
5
|
+
declare const GCM_NONCE_LENGTH = 12;
|
|
6
|
+
/** GCM authentication tag length, in bytes. */
|
|
7
|
+
declare const GCM_TAG_LENGTH = 16;
|
|
8
|
+
/**
|
|
9
|
+
* AES-256-GCM encrypt. Returns the ciphertext with the 16-byte auth tag
|
|
10
|
+
* appended (noble's convention) — i.e. `ciphertext || tag`.
|
|
11
|
+
*/
|
|
12
|
+
declare function aesGcmEncrypt(key: Uint8Array, plaintext: Uint8Array, nonce: Uint8Array, aad?: Uint8Array): Uint8Array;
|
|
13
|
+
/**
|
|
14
|
+
* AES-256-GCM decrypt. `sealed` is `ciphertext || tag` as produced by
|
|
15
|
+
* {@link aesGcmEncrypt}. Throws if the tag does not verify.
|
|
16
|
+
*/
|
|
17
|
+
declare function aesGcmDecrypt(key: Uint8Array, sealed: Uint8Array, nonce: Uint8Array, aad?: Uint8Array): Uint8Array;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { AES_KEY_LENGTH, GCM_NONCE_LENGTH, GCM_TAG_LENGTH, aesGcmDecrypt, aesGcmEncrypt };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { gcm } from "@noble/ciphers/aes.js";
|
|
2
|
+
|
|
3
|
+
//#region src/crypto/aes.ts
|
|
4
|
+
/** AES-256 key length, in bytes. */
|
|
5
|
+
const AES_KEY_LENGTH = 32;
|
|
6
|
+
/** GCM nonce (IV) length, in bytes. The standard 96-bit nonce. */
|
|
7
|
+
const GCM_NONCE_LENGTH = 12;
|
|
8
|
+
/** GCM authentication tag length, in bytes. */
|
|
9
|
+
const GCM_TAG_LENGTH = 16;
|
|
10
|
+
/**
|
|
11
|
+
* AES-256-GCM encrypt. Returns the ciphertext with the 16-byte auth tag
|
|
12
|
+
* appended (noble's convention) — i.e. `ciphertext || tag`.
|
|
13
|
+
*/
|
|
14
|
+
function aesGcmEncrypt(key, plaintext, nonce, aad) {
|
|
15
|
+
return gcm(key, nonce, aad).encrypt(plaintext);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* AES-256-GCM decrypt. `sealed` is `ciphertext || tag` as produced by
|
|
19
|
+
* {@link aesGcmEncrypt}. Throws if the tag does not verify.
|
|
20
|
+
*/
|
|
21
|
+
function aesGcmDecrypt(key, sealed, nonce, aad) {
|
|
22
|
+
return gcm(key, nonce, aad).decrypt(sealed);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { AES_KEY_LENGTH, GCM_NONCE_LENGTH, GCM_TAG_LENGTH, aesGcmDecrypt, aesGcmEncrypt };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Hex } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/crypto/encryption.d.ts
|
|
4
|
+
/** A field only its owner can open: sealSelf under the owner's own secret. */
|
|
5
|
+
declare const GADGET_SELF_HKDF_V1 = "self-hkdf-v1";
|
|
6
|
+
/** Length of an X25519 public key, in bytes. */
|
|
7
|
+
declare const X25519_PUBKEY_LENGTH = 32;
|
|
8
|
+
/**
|
|
9
|
+
* Ephemeral-static ECDH to the recipient's static key, keyed to the resourceId.
|
|
10
|
+
*
|
|
11
|
+
* ciphertext = ephemeralPub(32) || nonce(12) || aes-256-gcm-ct
|
|
12
|
+
*
|
|
13
|
+
* Only the holder of the secret matching `recipientPubkey` can derive the same AES
|
|
14
|
+
* key, and only with the matching `resourceId`.
|
|
15
|
+
*/
|
|
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;
|
|
19
|
+
/**
|
|
20
|
+
* Derive the AES key straight from the owner's own 32-byte
|
|
21
|
+
* secret, bound to the resourceId. No ECDH, no recipient — only a party holding
|
|
22
|
+
* `ownSecret` can re-derive the key.
|
|
23
|
+
*
|
|
24
|
+
* ciphertext = nonce(12) || aes-256-gcm-ct
|
|
25
|
+
*/
|
|
26
|
+
declare function sealSelf(plaintext: Uint8Array, ownSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
27
|
+
/** Inverse of {@link sealSelf}. */
|
|
28
|
+
declare function unsealSelf(ciphertext: Uint8Array, ownSecret: Uint8Array, resourceId: Hex): Uint8Array;
|
|
29
|
+
/** Anything that can personal_sign a raw 32-byte hash (viem LocalAccount / WalletClient). */
|
|
30
|
+
interface AccessSigner {
|
|
31
|
+
signMessage(args: {
|
|
32
|
+
message: {
|
|
33
|
+
raw: Hex;
|
|
34
|
+
};
|
|
35
|
+
}): Promise<Hex>;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { AccessSigner, GADGET_SELF_HKDF_V1, X25519_PUBKEY_LENGTH, seal, sealSelf, unseal, unsealSelf };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GCM_NONCE_LENGTH, aesGcmDecrypt, aesGcmEncrypt } from "./aes.js";
|
|
2
|
+
import { getRandomValues } from "./rand.js";
|
|
3
|
+
import { hexToBytes } from "viem";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
5
|
+
import { x25519 } from "@noble/curves/ed25519";
|
|
6
|
+
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
7
|
+
|
|
8
|
+
//#region src/crypto/encryption.ts
|
|
9
|
+
/** A field only its owner can open: sealSelf under the owner's own secret. */
|
|
10
|
+
const GADGET_SELF_HKDF_V1 = "self-hkdf-v1";
|
|
11
|
+
/** Length of an X25519 public key, in bytes. */
|
|
12
|
+
const X25519_PUBKEY_LENGTH = 32;
|
|
13
|
+
const utf8 = (s) => new TextEncoder().encode(s);
|
|
14
|
+
function concat$1(...parts) {
|
|
15
|
+
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
16
|
+
const out = new Uint8Array(total);
|
|
17
|
+
let off = 0;
|
|
18
|
+
for (const p of parts) {
|
|
19
|
+
out.set(p, off);
|
|
20
|
+
off += p.length;
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
const hkdfSha256 = (ikm, salt, info, length) => hkdf(sha256, ikm, salt, info, length);
|
|
25
|
+
const sealInfo = (resourceId) => concat$1(hexToBytes(resourceId), utf8(":sealed"));
|
|
26
|
+
/**
|
|
27
|
+
* Ephemeral-static ECDH to the recipient's static key, keyed to the resourceId.
|
|
28
|
+
*
|
|
29
|
+
* ciphertext = ephemeralPub(32) || nonce(12) || aes-256-gcm-ct
|
|
30
|
+
*
|
|
31
|
+
* Only the holder of the secret matching `recipientPubkey` can derive the same AES
|
|
32
|
+
* key, and only with the matching `resourceId`.
|
|
33
|
+
*/
|
|
34
|
+
function seal(plaintext, recipientPubkey, resourceId) {
|
|
35
|
+
const ephSec = x25519.utils.randomSecretKey();
|
|
36
|
+
const ephPub = x25519.getPublicKey(ephSec);
|
|
37
|
+
const aesKey = hkdfSha256(x25519.getSharedSecret(ephSec, recipientPubkey), void 0, sealInfo(resourceId), 32);
|
|
38
|
+
const nonce = getRandomValues(new Uint8Array(GCM_NONCE_LENGTH));
|
|
39
|
+
return concat$1(ephPub, nonce, aesGcmEncrypt(aesKey, plaintext, nonce));
|
|
40
|
+
}
|
|
41
|
+
/** Inverse of {@link seal}, with the recipient's secret and the same resourceId. */
|
|
42
|
+
function unseal(ciphertext, recipientSecret, resourceId) {
|
|
43
|
+
const ephPub = ciphertext.slice(0, X25519_PUBKEY_LENGTH);
|
|
44
|
+
const nonce = ciphertext.slice(X25519_PUBKEY_LENGTH, X25519_PUBKEY_LENGTH + GCM_NONCE_LENGTH);
|
|
45
|
+
const aesCt = ciphertext.slice(X25519_PUBKEY_LENGTH + GCM_NONCE_LENGTH);
|
|
46
|
+
return aesGcmDecrypt(hkdfSha256(x25519.getSharedSecret(recipientSecret, ephPub), void 0, sealInfo(resourceId), 32), aesCt, nonce);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Derive the AES key straight from the owner's own 32-byte
|
|
50
|
+
* secret, bound to the resourceId. No ECDH, no recipient — only a party holding
|
|
51
|
+
* `ownSecret` can re-derive the key.
|
|
52
|
+
*
|
|
53
|
+
* ciphertext = nonce(12) || aes-256-gcm-ct
|
|
54
|
+
*/
|
|
55
|
+
function sealSelf(plaintext, ownSecret, resourceId) {
|
|
56
|
+
const aesKey = hkdfSha256(ownSecret, void 0, sealInfo(resourceId), 32);
|
|
57
|
+
const nonce = getRandomValues(new Uint8Array(GCM_NONCE_LENGTH));
|
|
58
|
+
return concat$1(nonce, aesGcmEncrypt(aesKey, plaintext, nonce));
|
|
59
|
+
}
|
|
60
|
+
/** Inverse of {@link sealSelf}. */
|
|
61
|
+
function unsealSelf(ciphertext, ownSecret, resourceId) {
|
|
62
|
+
const nonce = ciphertext.slice(0, GCM_NONCE_LENGTH);
|
|
63
|
+
const aesCt = ciphertext.slice(GCM_NONCE_LENGTH);
|
|
64
|
+
return aesGcmDecrypt(hkdfSha256(ownSecret, void 0, sealInfo(resourceId), 32), aesCt, nonce);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
export { GADGET_SELF_HKDF_V1, X25519_PUBKEY_LENGTH, seal, sealSelf, unseal, unsealSelf };
|