@coordination-network/network-member-governance 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +250 -0
- package/dist/index.cjs +583 -0
- package/dist/index.js +555 -0
- package/package.json +54 -0
- package/src/constants.ts +19 -0
- package/src/governance-rule.ts +20 -0
- package/src/index.ts +43 -0
- package/src/network-member-calls.ts +321 -0
- package/src/safe-transaction.ts +361 -0
- package/src/types.ts +37 -0
- package/types/index.d.ts +472 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
import { getAddress, encodeFunctionData, recoverAddress, isAddressEqual, decodeEventLog } from 'viem';
|
|
2
|
+
import { encodeMultiSendData, buildSignatureBytes, calculateSafeTransactionHash, generatePreValidatedSignature, generateTypedData, EthSafeSignature, DEFAULT_SAFE_VERSION } from '@safe-global/protocol-kit';
|
|
3
|
+
import { getMultiSendCallOnlyDeployment, getSafeSingletonDeployment } from '@safe-global/safe-deployments';
|
|
4
|
+
import { OperationType } from '@safe-global/types-kit';
|
|
5
|
+
export { OperationType } from '@safe-global/types-kit';
|
|
6
|
+
|
|
7
|
+
/** The zero address, the Safe's "unset" value for `gasToken` and `refundReceiver`. */
|
|
8
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
9
|
+
/**
|
|
10
|
+
* The gas and refund fields of every transaction this package builds, all zeroed.
|
|
11
|
+
*
|
|
12
|
+
* A Safe transaction can reimburse whoever submitted it out of the Safe's own
|
|
13
|
+
* balance. This flow does not: the network member that sends the transaction
|
|
14
|
+
* pays its own gas, and the Safe never holds or moves funds. Zeroing these
|
|
15
|
+
* fields is what turns that off, so every transaction this package builds
|
|
16
|
+
* refunds nobody.
|
|
17
|
+
*/
|
|
18
|
+
const NO_REFUND = {
|
|
19
|
+
safeTxGas: "0",
|
|
20
|
+
baseGas: "0",
|
|
21
|
+
gasPrice: "0",
|
|
22
|
+
gasToken: ZERO_ADDRESS,
|
|
23
|
+
refundReceiver: ZERO_ADDRESS
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The signatures a Safe of `memberCount` network members must require: a majority, one more than
|
|
28
|
+
* half. Five members need three, eight need five.
|
|
29
|
+
*
|
|
30
|
+
* The authoritative copy of this rule is on chain, in `NetworkMemberGovernance.governanceThreshold`,
|
|
31
|
+
* which every admission and removal sets the Safe's threshold from. This is the same expression for
|
|
32
|
+
* off-chain use: checking that a Safe is configured correctly, and reporting what a pending operation
|
|
33
|
+
* will change the threshold to. No call carries a threshold, so nothing depends on this value being
|
|
34
|
+
* right.
|
|
35
|
+
*
|
|
36
|
+
* @param memberCount How many members the Safe has owners for, after the change.
|
|
37
|
+
* @returns The required number of approvals.
|
|
38
|
+
*/
|
|
39
|
+
function governanceThreshold(memberCount) {
|
|
40
|
+
if (!Number.isInteger(memberCount) || memberCount < 1) {
|
|
41
|
+
throw new Error(`A member count must be a positive integer, got ${memberCount}`);
|
|
42
|
+
}
|
|
43
|
+
return Math.floor(memberCount / 2) + 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function safeVersionOf(context) {
|
|
47
|
+
return context.safeVersion ?? DEFAULT_SAFE_VERSION;
|
|
48
|
+
}
|
|
49
|
+
function chainIdOf(context) {
|
|
50
|
+
return BigInt(context.chainId);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The MultiSendCallOnly ABI for this context's Safe version.
|
|
54
|
+
*
|
|
55
|
+
* Deliberately not chain-scoped. Only the address varies by chain, so resolving the ABI separately is
|
|
56
|
+
* what lets {SafeContext.multiSend} work on a chain with no canonical deployment, which is the only
|
|
57
|
+
* situation that override exists for.
|
|
58
|
+
*
|
|
59
|
+
* @param context The Safe version the batch is for.
|
|
60
|
+
* @returns The MultiSendCallOnly ABI.
|
|
61
|
+
*/
|
|
62
|
+
function multiSendAbi(context) {
|
|
63
|
+
const deployment = getMultiSendCallOnlyDeployment({ version: safeVersionOf(context) });
|
|
64
|
+
if (!deployment)
|
|
65
|
+
throw new Error(`No MultiSendCallOnly deployment for Safe ${safeVersionOf(context)}`);
|
|
66
|
+
return deployment.abi;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The canonical MultiSendCallOnly address for this context's chain.
|
|
70
|
+
*
|
|
71
|
+
* @param context The Safe and chain the batch is for.
|
|
72
|
+
* @returns The address, or throws when the chain has no canonical deployment.
|
|
73
|
+
*/
|
|
74
|
+
function multiSendAddress(context) {
|
|
75
|
+
const chainId = chainIdOf(context).toString();
|
|
76
|
+
const deployment = getMultiSendCallOnlyDeployment({ version: safeVersionOf(context), network: chainId });
|
|
77
|
+
if (!deployment?.networkAddresses[chainId]) {
|
|
78
|
+
throw new Error(`No canonical MultiSendCallOnly deployment for chain ${chainId} at Safe ${safeVersionOf(context)}. Pass \`multiSend\` explicitly.`);
|
|
79
|
+
}
|
|
80
|
+
return getAddress(deployment.networkAddresses[chainId]);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The MultiSendCallOnly the Safe delegate-calls to batch several calls.
|
|
84
|
+
*
|
|
85
|
+
* Resolved from `@safe-global/safe-deployments` for this chain and Safe version,
|
|
86
|
+
* which is what keeps a wrong address out of a delegate call. Override it in the
|
|
87
|
+
* context only for a chain where the canonical deployment is missing.
|
|
88
|
+
*
|
|
89
|
+
* @param context The Safe and chain the batch is for.
|
|
90
|
+
* @returns The MultiSendCallOnly address.
|
|
91
|
+
*/
|
|
92
|
+
function resolveMultiSend(context) {
|
|
93
|
+
if (context.multiSend)
|
|
94
|
+
return getAddress(context.multiSend);
|
|
95
|
+
return multiSendAddress(context);
|
|
96
|
+
}
|
|
97
|
+
/** The Safe singleton ABI for this context's version, from the Safe deployments. */
|
|
98
|
+
function safeAbi(context) {
|
|
99
|
+
const deployment = getSafeSingletonDeployment({ version: safeVersionOf(context) });
|
|
100
|
+
if (!deployment)
|
|
101
|
+
throw new Error(`No Safe singleton deployment for version ${safeVersionOf(context)}`);
|
|
102
|
+
return deployment.abi;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Builds the Safe transaction data for one or more calls, with the gas and refund
|
|
106
|
+
* fields zeroed so whoever submits it pays.
|
|
107
|
+
*
|
|
108
|
+
* A single call is executed directly. Several calls are batched through
|
|
109
|
+
* MultiSendCallOnly as one delegate call, so the Safe applies them together or
|
|
110
|
+
* not at all.
|
|
111
|
+
*
|
|
112
|
+
* @param transactions The calls, in the order the Safe should make them.
|
|
113
|
+
* @param context The Safe and chain the transaction is for.
|
|
114
|
+
* @param nonce The Safe's current `nonce()`. Read it from the chain: a
|
|
115
|
+
* transaction built against a stale nonce is rejected, and executing any Safe
|
|
116
|
+
* transaction invalidates every approval recorded against that nonce.
|
|
117
|
+
* @returns The transaction data to hash and sign.
|
|
118
|
+
*/
|
|
119
|
+
function buildSafeTransactionData(transactions, context, nonce) {
|
|
120
|
+
if (transactions.length === 0)
|
|
121
|
+
throw new Error("A Safe transaction needs at least one call");
|
|
122
|
+
if (transactions.length === 1) {
|
|
123
|
+
const [transaction] = transactions;
|
|
124
|
+
return {
|
|
125
|
+
to: getAddress(transaction.to),
|
|
126
|
+
value: transaction.value,
|
|
127
|
+
data: transaction.data,
|
|
128
|
+
operation: transaction.operation ?? OperationType.Call,
|
|
129
|
+
...NO_REFUND,
|
|
130
|
+
nonce
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// `encodeMultiSendData` packs the calls; MultiSendCallOnly still has to be
|
|
134
|
+
// called through its own `multiSend` entry point.
|
|
135
|
+
const packedCalls = encodeMultiSendData(transactions.map((transaction) => ({
|
|
136
|
+
...transaction,
|
|
137
|
+
operation: transaction.operation ?? OperationType.Call
|
|
138
|
+
})));
|
|
139
|
+
return {
|
|
140
|
+
to: resolveMultiSend(context),
|
|
141
|
+
value: "0",
|
|
142
|
+
data: encodeFunctionData({
|
|
143
|
+
abi: multiSendAbi(context),
|
|
144
|
+
functionName: "multiSend",
|
|
145
|
+
args: [packedCalls]
|
|
146
|
+
}),
|
|
147
|
+
operation: OperationType.DelegateCall,
|
|
148
|
+
...NO_REFUND,
|
|
149
|
+
nonce
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The digest the Safe's owners sign, as the Safe's own `getTransactionHash`
|
|
154
|
+
* computes it. Bound to the Safe and the chain by the EIP-712 domain, so a
|
|
155
|
+
* signature cannot be replayed against another Safe or another network.
|
|
156
|
+
*
|
|
157
|
+
* @param context The Safe and chain the transaction is for.
|
|
158
|
+
* @param transaction The transaction data to hash.
|
|
159
|
+
* @returns The EIP-712 digest.
|
|
160
|
+
*/
|
|
161
|
+
function hashSafeTransaction(context, transaction) {
|
|
162
|
+
return calculateSafeTransactionHash(getAddress(context.safeAddress), transaction, safeVersionOf(context), chainIdOf(context));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Signs a Safe transaction as one approver.
|
|
166
|
+
*
|
|
167
|
+
* Governance approves on chain with {encodeApproveHash} and never circulates a
|
|
168
|
+
* signature, so nothing in this package's flow calls this. It stays as a Safe
|
|
169
|
+
* primitive, and the tests use it to check the packing in
|
|
170
|
+
* {encodeSafeSignatures}.
|
|
171
|
+
*
|
|
172
|
+
* The signer must be an owner of the Safe. A viem account works directly, and so
|
|
173
|
+
* does any wrapper over a KMS or HSM that signs EIP-712 and knows its address.
|
|
174
|
+
*
|
|
175
|
+
* @param signer The approver's signing key.
|
|
176
|
+
* @param context The Safe and chain the transaction is for.
|
|
177
|
+
* @param transaction The transaction to approve.
|
|
178
|
+
* @returns The approver's signature.
|
|
179
|
+
*/
|
|
180
|
+
async function signSafeTransaction(signer, context, transaction) {
|
|
181
|
+
const typedData = generateTypedData({
|
|
182
|
+
safeAddress: getAddress(context.safeAddress),
|
|
183
|
+
safeVersion: safeVersionOf(context),
|
|
184
|
+
chainId: chainIdOf(context),
|
|
185
|
+
data: transaction
|
|
186
|
+
});
|
|
187
|
+
const signature = await signer.signTypedData(typedData);
|
|
188
|
+
return new EthSafeSignature(getAddress(signer.address), signature);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Recovers the owner behind a signature over a Safe transaction, trusting the
|
|
192
|
+
* signature rather than a claimed sender. Like {signSafeTransaction}, it serves
|
|
193
|
+
* off-chain signatures, which this package's flow does not produce.
|
|
194
|
+
*
|
|
195
|
+
* @param context The Safe and chain the transaction is for.
|
|
196
|
+
* @param transaction The signed transaction.
|
|
197
|
+
* @param signature The 65-byte signature.
|
|
198
|
+
* @returns The recovered signer address.
|
|
199
|
+
*/
|
|
200
|
+
async function recoverSafeSigner(context, transaction, signature) {
|
|
201
|
+
return recoverAddress({ hash: hashSafeTransaction(context, transaction), signature });
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Calldata for `approveHash`, which records an approval in the Safe's own storage instead of handing a
|
|
205
|
+
* signature to somebody.
|
|
206
|
+
*
|
|
207
|
+
* This is the fully on-chain approval path, and its appeal is that nothing is ever circulated: each
|
|
208
|
+
* approver derives the transaction from the chain, approves its hash from its own key, and the member
|
|
209
|
+
* that completes the quorum reads the approvals and executes. There is no channel to secure, and no
|
|
210
|
+
* approver ever acts on a payload another party assembled, which is what makes blind signing
|
|
211
|
+
* impossible rather than merely detectable.
|
|
212
|
+
*
|
|
213
|
+
* Only an owner may call it, and the hash must be the one from {hashSafeTransaction}: an approval is
|
|
214
|
+
* bound to one exact transaction, so approving one candidate's admission says nothing about another's.
|
|
215
|
+
*
|
|
216
|
+
* @param context The Safe and chain the transaction is for.
|
|
217
|
+
* @param safeTxHash The digest from {hashSafeTransaction}.
|
|
218
|
+
* @returns Calldata to send to the Safe address, from the approver's own key.
|
|
219
|
+
*/
|
|
220
|
+
function encodeApproveHash(context, safeTxHash) {
|
|
221
|
+
return encodeFunctionData({
|
|
222
|
+
abi: safeAbi(context),
|
|
223
|
+
functionName: "approveHash",
|
|
224
|
+
args: [safeTxHash]
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* The signature entry for an owner that approved on chain with {encodeApproveHash}.
|
|
229
|
+
*
|
|
230
|
+
* It carries no signature material: the Safe reads `v == 1` and looks the approval up in its own
|
|
231
|
+
* storage, so the entry only names the approver. Feed these to {encodeSafeSignatures} or
|
|
232
|
+
* {encodeExecTransaction} exactly like signatures made off chain, and the two kinds may be mixed in one
|
|
233
|
+
* transaction.
|
|
234
|
+
*
|
|
235
|
+
* The member that submits gets its own entry for free: the Safe treats the executor as having approved,
|
|
236
|
+
* so it can include itself without a prior `approveHash`, turning a 3-of-5 into two approval
|
|
237
|
+
* transactions plus the execution.
|
|
238
|
+
*
|
|
239
|
+
* @param owner The approver, which must still be an owner when the transaction executes.
|
|
240
|
+
* @returns A signature entry naming that approver.
|
|
241
|
+
*/
|
|
242
|
+
function onChainApproval(owner) {
|
|
243
|
+
return generatePreValidatedSignature(getAddress(owner));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Packs signatures into the `signatures` argument of `execTransaction`.
|
|
247
|
+
*
|
|
248
|
+
* The Safe reads the packed signatures at fixed 65-byte offsets and requires each recovered signer to be
|
|
249
|
+
* strictly greater than the previous one, so the SDK sorts them by signer here.
|
|
250
|
+
* A set that counts the same owner twice would sort fine and then be rejected on
|
|
251
|
+
* chain, so it is refused up front.
|
|
252
|
+
*
|
|
253
|
+
* @param signatures The approvals, in any order. {onChainApproval} produces them for the on-chain
|
|
254
|
+
* flow.
|
|
255
|
+
* @returns The entries concatenated into the single `bytes` value `execTransaction` takes, 65 bytes
|
|
256
|
+
* per owner, ordered by signer.
|
|
257
|
+
*/
|
|
258
|
+
function encodeSafeSignatures(signatures) {
|
|
259
|
+
if (signatures.length === 0)
|
|
260
|
+
throw new Error("A Safe transaction needs at least one signature");
|
|
261
|
+
const seen = new Set();
|
|
262
|
+
signatures.forEach(({ signer }) => {
|
|
263
|
+
const owner = getAddress(signer);
|
|
264
|
+
if (seen.has(owner))
|
|
265
|
+
throw new Error(`Duplicate signature from ${owner}: the Safe counts each owner once`);
|
|
266
|
+
seen.add(owner);
|
|
267
|
+
});
|
|
268
|
+
return buildSignatureBytes([...signatures]);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Checks a set of approvers against the Safe's owners and threshold, so a member
|
|
272
|
+
* knows whether it can submit yet and, if not, how many approvals are missing.
|
|
273
|
+
* Addresses that are not current owners are ignored rather than counted.
|
|
274
|
+
*
|
|
275
|
+
* @param owners The Safe's current owners, from `getOwners()`.
|
|
276
|
+
* @param threshold The Safe's current `getThreshold()`.
|
|
277
|
+
* @param signers The addresses that have approved, read from the chain.
|
|
278
|
+
* @returns Whether the set is enough, and what is still missing.
|
|
279
|
+
*/
|
|
280
|
+
function checkQuorum(owners, threshold, signers) {
|
|
281
|
+
const isOwner = (candidate) => owners.some((owner) => isAddressEqual(owner, candidate));
|
|
282
|
+
const distinct = [...new Set(signers.filter(isOwner).map((signer) => getAddress(signer)))].sort((a, b) => BigInt(a) < BigInt(b) ? -1 : 1);
|
|
283
|
+
return {
|
|
284
|
+
hasQuorum: distinct.length >= threshold,
|
|
285
|
+
signers: distinct,
|
|
286
|
+
missing: Math.max(threshold - distinct.length, 0)
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Encodes the `execTransaction` call that lands the transaction on chain, against
|
|
291
|
+
* the Safe ABI for this context's version. The result is plain calldata to the
|
|
292
|
+
* Safe, so anyone can submit it: the approvals carry the authority, not the
|
|
293
|
+
* sender.
|
|
294
|
+
*
|
|
295
|
+
* @param context The Safe and chain the transaction is for.
|
|
296
|
+
* @param transaction The approved transaction.
|
|
297
|
+
* @param signatures The approvals, in any order.
|
|
298
|
+
* @returns Calldata to send to the Safe address.
|
|
299
|
+
*/
|
|
300
|
+
function encodeExecTransaction(context, transaction, signatures) {
|
|
301
|
+
return encodeFunctionData({
|
|
302
|
+
abi: safeAbi(context),
|
|
303
|
+
functionName: "execTransaction",
|
|
304
|
+
args: [
|
|
305
|
+
transaction.to,
|
|
306
|
+
BigInt(transaction.value),
|
|
307
|
+
transaction.data,
|
|
308
|
+
transaction.operation,
|
|
309
|
+
BigInt(transaction.safeTxGas),
|
|
310
|
+
BigInt(transaction.baseGas),
|
|
311
|
+
BigInt(transaction.gasPrice),
|
|
312
|
+
transaction.gasToken,
|
|
313
|
+
transaction.refundReceiver,
|
|
314
|
+
encodeSafeSignatures(signatures)
|
|
315
|
+
]
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* The `NetworkMemberGovernance` surface: the governance operations, each one call.
|
|
321
|
+
*
|
|
322
|
+
* There is no threshold argument anywhere here. The contract computes it from the Safe's live owner
|
|
323
|
+
* count, and removal reads the member's commitment and signer from the registry, so no caller can
|
|
324
|
+
* carry a wrong threshold, point at another member's leaf, or take arbitrary calldata.
|
|
325
|
+
*
|
|
326
|
+
* `expectedMemberCount` is not a policy input either. It pins the member set a transaction was built
|
|
327
|
+
* against, so a transaction goes stale the moment that set changes. It has to exist because a
|
|
328
|
+
* voluntary exit changes the owner count and the threshold without advancing the Safe's nonce, which is
|
|
329
|
+
* what would otherwise invalidate transactions in flight.
|
|
330
|
+
*/
|
|
331
|
+
const NETWORK_MEMBER_GOVERNANCE_ABI = [
|
|
332
|
+
{
|
|
333
|
+
type: "function",
|
|
334
|
+
name: "admitMember",
|
|
335
|
+
stateMutability: "nonpayable",
|
|
336
|
+
inputs: [
|
|
337
|
+
{ name: "memberNetworkId", type: "bytes32" },
|
|
338
|
+
{ name: "semaphoreCommitment", type: "uint256" },
|
|
339
|
+
{ name: "governanceSigner", type: "address" },
|
|
340
|
+
{ name: "expectedMemberCount", type: "uint256" }
|
|
341
|
+
],
|
|
342
|
+
outputs: []
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
type: "function",
|
|
346
|
+
name: "removeMember",
|
|
347
|
+
stateMutability: "nonpayable",
|
|
348
|
+
inputs: [
|
|
349
|
+
{ name: "memberNetworkId", type: "bytes32" },
|
|
350
|
+
{ name: "merkleProofSiblings", type: "uint256[]" },
|
|
351
|
+
{ name: "expectedMemberCount", type: "uint256" }
|
|
352
|
+
],
|
|
353
|
+
outputs: []
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
type: "function",
|
|
357
|
+
name: "exitNetwork",
|
|
358
|
+
stateMutability: "nonpayable",
|
|
359
|
+
inputs: [
|
|
360
|
+
{ name: "memberNetworkId", type: "bytes32" },
|
|
361
|
+
{ name: "merkleProofSiblings", type: "uint256[]" }
|
|
362
|
+
],
|
|
363
|
+
outputs: []
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
type: "function",
|
|
367
|
+
name: "governanceThreshold",
|
|
368
|
+
stateMutability: "pure",
|
|
369
|
+
inputs: [{ name: "memberCount", type: "uint256" }],
|
|
370
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
type: "function",
|
|
374
|
+
name: "thresholdAfterAdmission",
|
|
375
|
+
stateMutability: "view",
|
|
376
|
+
inputs: [],
|
|
377
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
378
|
+
}
|
|
379
|
+
];
|
|
380
|
+
/** The `NetworkMemberRegistry` surface a member server needs directly. */
|
|
381
|
+
const NETWORK_MEMBER_REGISTRY_ABI = [
|
|
382
|
+
{
|
|
383
|
+
type: "function",
|
|
384
|
+
name: "requestToJoin",
|
|
385
|
+
stateMutability: "nonpayable",
|
|
386
|
+
inputs: [
|
|
387
|
+
{ name: "memberNetworkId", type: "bytes32" },
|
|
388
|
+
{ name: "semaphoreCommitment", type: "uint256" },
|
|
389
|
+
{ name: "governanceSigner", type: "address" },
|
|
390
|
+
{ name: "metadataURI", type: "string" }
|
|
391
|
+
],
|
|
392
|
+
outputs: []
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
type: "event",
|
|
396
|
+
name: "NetworkMemberJoinRequested",
|
|
397
|
+
inputs: [
|
|
398
|
+
{ name: "memberNetworkId", type: "bytes32", indexed: true },
|
|
399
|
+
{ name: "semaphoreCommitment", type: "uint256", indexed: false },
|
|
400
|
+
{ name: "governanceSigner", type: "address", indexed: false },
|
|
401
|
+
{ name: "metadataURI", type: "string", indexed: false }
|
|
402
|
+
]
|
|
403
|
+
}
|
|
404
|
+
];
|
|
405
|
+
/**
|
|
406
|
+
* Turns a `NetworkMemberJoinRequested` log into the request it announced.
|
|
407
|
+
*
|
|
408
|
+
* This is the only honest source of a {NetworkMemberJoinRequest}: an approver reads the candidate's own
|
|
409
|
+
* event from a node it trusts and decodes it here, so the transaction it approves is a function of what
|
|
410
|
+
* the chain reported. Building the object by hand from something another party sent gives up that
|
|
411
|
+
* property, and with it the meaning of the approval.
|
|
412
|
+
*
|
|
413
|
+
* It stays free of any RPC dependency: fetching the log is the caller's job, with `getLogs` against its
|
|
414
|
+
* own node. What this removes is the field mapping, and the chance of a silent mismatch in it.
|
|
415
|
+
*
|
|
416
|
+
* @param log The log, as viem returns it from `getLogs`.
|
|
417
|
+
* @param registry The registry the log must have come from. Checked when the log carries an address.
|
|
418
|
+
* @returns The request the candidate published.
|
|
419
|
+
*/
|
|
420
|
+
function decodeJoinRequestLog(log, registry) {
|
|
421
|
+
if (log.address && registry && !isAddressEqual(log.address, getAddress(registry))) {
|
|
422
|
+
throw new Error(`Log came from ${log.address}, not from the registry at ${getAddress(registry)}`);
|
|
423
|
+
}
|
|
424
|
+
const { args } = decodeEventLog({
|
|
425
|
+
abi: NETWORK_MEMBER_REGISTRY_ABI,
|
|
426
|
+
eventName: "NetworkMemberJoinRequested",
|
|
427
|
+
data: log.data,
|
|
428
|
+
topics: log.topics
|
|
429
|
+
});
|
|
430
|
+
return {
|
|
431
|
+
memberNetworkId: args.memberNetworkId,
|
|
432
|
+
semaphoreCommitment: args.semaphoreCommitment,
|
|
433
|
+
governanceSigner: getAddress(args.governanceSigner),
|
|
434
|
+
metadataURI: args.metadataURI
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* The single call that admits a network member: its record, its group leaf, and its seat at the Safe.
|
|
439
|
+
*
|
|
440
|
+
* @param admission The vetted join request being approved.
|
|
441
|
+
* @param memberCount The Safe's live owner count, from `getOwners().length`. Pinned into the call so the
|
|
442
|
+
* transaction goes stale if the member set changes before it executes.
|
|
443
|
+
* @returns Calldata for the orchestrator.
|
|
444
|
+
*/
|
|
445
|
+
function encodeAdmitMember(admission, memberCount) {
|
|
446
|
+
return encodeFunctionData({
|
|
447
|
+
abi: NETWORK_MEMBER_GOVERNANCE_ABI,
|
|
448
|
+
functionName: "admitMember",
|
|
449
|
+
args: [
|
|
450
|
+
admission.memberNetworkId,
|
|
451
|
+
admission.semaphoreCommitment,
|
|
452
|
+
getAddress(admission.governanceSigner),
|
|
453
|
+
BigInt(memberCount)
|
|
454
|
+
]
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* The single call that removes a network member from all three places.
|
|
459
|
+
*
|
|
460
|
+
* @param removal The member to remove, and its membership proof.
|
|
461
|
+
* @param memberCount The Safe's live owner count, pinned for the reason given on {encodeAdmitMember}.
|
|
462
|
+
* @returns Calldata for the orchestrator.
|
|
463
|
+
*/
|
|
464
|
+
function encodeRemoveMember(removal, memberCount) {
|
|
465
|
+
return encodeFunctionData({
|
|
466
|
+
abi: NETWORK_MEMBER_GOVERNANCE_ABI,
|
|
467
|
+
functionName: "removeMember",
|
|
468
|
+
args: [removal.memberNetworkId, removal.merkleProofSiblings, BigInt(memberCount)]
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
/** The one call an admission is, as a Safe meta-transaction. */
|
|
472
|
+
function admissionCall(contracts, admission, memberCount) {
|
|
473
|
+
return { to: getAddress(contracts.governance), value: "0", data: encodeAdmitMember(admission, memberCount) };
|
|
474
|
+
}
|
|
475
|
+
/** The one call a removal is, as a Safe meta-transaction. */
|
|
476
|
+
function removalCall(contracts, removal, memberCount) {
|
|
477
|
+
return { to: getAddress(contracts.governance), value: "0", data: encodeRemoveMember(removal, memberCount) };
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Builds the Safe transaction that admits a network member.
|
|
481
|
+
*
|
|
482
|
+
* One plain call to the orchestrator, so there is no MultiSend batch to decode and no threshold in the
|
|
483
|
+
* calldata to check. The result is a pure function of the on-chain request and the nonce, which is what
|
|
484
|
+
* makes building safe to do in parallel: two approvers who build from the same chain state produce
|
|
485
|
+
* byte-identical transactions and therefore the same `safeTxHash`, so their approvals accumulate
|
|
486
|
+
* against one hash instead of competing.
|
|
487
|
+
*
|
|
488
|
+
* @param contracts The governance contracts.
|
|
489
|
+
* @param admission The vetted join request being approved.
|
|
490
|
+
* @param context The Safe and chain the transaction is for.
|
|
491
|
+
* @param nonce The Safe's current `nonce()`.
|
|
492
|
+
* @param memberCount The Safe's live owner count, from `getOwners().length`.
|
|
493
|
+
* @returns The transaction data to sign.
|
|
494
|
+
*/
|
|
495
|
+
function buildAdmissionTransaction(contracts, admission, context, nonce, memberCount) {
|
|
496
|
+
return buildSafeTransactionData([admissionCall(contracts, admission, memberCount)], context, nonce);
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Builds the Safe transaction that removes a network member.
|
|
500
|
+
*
|
|
501
|
+
* @param contracts The governance contracts.
|
|
502
|
+
* @param removal The member to remove, and its membership proof.
|
|
503
|
+
* @param context The Safe and chain the transaction is for.
|
|
504
|
+
* @param nonce The Safe's current `nonce()`.
|
|
505
|
+
* @param memberCount The Safe's live owner count, from `getOwners().length`.
|
|
506
|
+
* @returns The transaction data to sign.
|
|
507
|
+
*/
|
|
508
|
+
function buildRemovalTransaction(contracts, removal, context, nonce, memberCount) {
|
|
509
|
+
return buildSafeTransactionData([removalCall(contracts, removal, memberCount)], context, nonce);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Calldata for a member leaving the network of its own accord: its record, its group leaf and its seat
|
|
513
|
+
* at the Safe, in one call.
|
|
514
|
+
*
|
|
515
|
+
* Not a Safe transaction. The member's own governance signer sends this directly to the orchestrator,
|
|
516
|
+
* because departure does not need the consent of the members being left, so there is no transaction
|
|
517
|
+
* for anyone else to approve. It is also how a member that is winding down avoids leaving a seat behind
|
|
518
|
+
* that nobody can sign for, which would still count toward everyone else's threshold.
|
|
519
|
+
*
|
|
520
|
+
* Rejoining means a new request and a new place in the group, and every credential the member issued
|
|
521
|
+
* stops verifying once its record is gone.
|
|
522
|
+
*
|
|
523
|
+
* @param memberNetworkId The member leaving. The caller must be its governance signer.
|
|
524
|
+
* @param merkleProofSiblings The membership proof for its group leaf, computed off chain against the
|
|
525
|
+
* group state this transaction will execute in.
|
|
526
|
+
* @returns Calldata to send to the orchestrator.
|
|
527
|
+
*/
|
|
528
|
+
function encodeExitNetwork(memberNetworkId, merkleProofSiblings) {
|
|
529
|
+
return encodeFunctionData({
|
|
530
|
+
abi: NETWORK_MEMBER_GOVERNANCE_ABI,
|
|
531
|
+
functionName: "exitNetwork",
|
|
532
|
+
args: [memberNetworkId, merkleProofSiblings]
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Calldata for a candidate's own join request, the one permissionless call in this flow. It must be
|
|
537
|
+
* sent from the auth address the networkId encodes, and `governanceSigner` must be a different key.
|
|
538
|
+
*
|
|
539
|
+
* @param request The request to announce.
|
|
540
|
+
* @returns Calldata for the registry.
|
|
541
|
+
*/
|
|
542
|
+
function encodeRequestToJoin(request) {
|
|
543
|
+
return encodeFunctionData({
|
|
544
|
+
abi: NETWORK_MEMBER_REGISTRY_ABI,
|
|
545
|
+
functionName: "requestToJoin",
|
|
546
|
+
args: [
|
|
547
|
+
request.memberNetworkId,
|
|
548
|
+
request.semaphoreCommitment,
|
|
549
|
+
getAddress(request.governanceSigner),
|
|
550
|
+
request.metadataURI ?? ""
|
|
551
|
+
]
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export { NETWORK_MEMBER_GOVERNANCE_ABI, NETWORK_MEMBER_REGISTRY_ABI, NO_REFUND, ZERO_ADDRESS, admissionCall, buildAdmissionTransaction, buildRemovalTransaction, buildSafeTransactionData, checkQuorum, decodeJoinRequestLog, encodeAdmitMember, encodeApproveHash, encodeExecTransaction, encodeExitNetwork, encodeRemoveMember, encodeRequestToJoin, encodeSafeSignatures, governanceThreshold, hashSafeTransaction, onChainApproval, recoverSafeSigner, removalCall, resolveMultiSend, signSafeTransaction };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coordination-network/network-member-governance",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Governance Safe transactions for the network member registry: build the admission or removal transaction, sign it as an approved network member, and aggregate the signatures the Safe accepts on chain.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/zk-kit/coordination-network.git",
|
|
9
|
+
"directory": "packages/network-member-governance"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/zk-kit/coordination-network/tree/main/packages/network-member-governance",
|
|
12
|
+
"bugs": "https://github.com/zk-kit/coordination-network/issues",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "dist/index.cjs",
|
|
16
|
+
"module": "dist/index.js",
|
|
17
|
+
"types": "types/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./types/index.d.ts",
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/",
|
|
27
|
+
"types/",
|
|
28
|
+
"src/",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "rimraf dist types && rollup -c rollup.config.mjs"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@safe-global/protocol-kit": "^8.0.6",
|
|
40
|
+
"@safe-global/safe-deployments": "^1.37.62",
|
|
41
|
+
"@safe-global/types-kit": "^4.0.1",
|
|
42
|
+
"viem": "^2.40.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
46
|
+
"@types/jest": "^30.0.0",
|
|
47
|
+
"rimraf": "^6.1.3",
|
|
48
|
+
"rollup": "^4.62.2",
|
|
49
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
50
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
51
|
+
"tslib": "^2.8.0",
|
|
52
|
+
"typescript": "^6.0.3"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The zero address, the Safe's "unset" value for `gasToken` and `refundReceiver`. */
|
|
2
|
+
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" as const
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The gas and refund fields of every transaction this package builds, all zeroed.
|
|
6
|
+
*
|
|
7
|
+
* A Safe transaction can reimburse whoever submitted it out of the Safe's own
|
|
8
|
+
* balance. This flow does not: the network member that sends the transaction
|
|
9
|
+
* pays its own gas, and the Safe never holds or moves funds. Zeroing these
|
|
10
|
+
* fields is what turns that off, so every transaction this package builds
|
|
11
|
+
* refunds nobody.
|
|
12
|
+
*/
|
|
13
|
+
export const NO_REFUND = {
|
|
14
|
+
safeTxGas: "0",
|
|
15
|
+
baseGas: "0",
|
|
16
|
+
gasPrice: "0",
|
|
17
|
+
gasToken: ZERO_ADDRESS,
|
|
18
|
+
refundReceiver: ZERO_ADDRESS
|
|
19
|
+
} as const
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The signatures a Safe of `memberCount` network members must require: a majority, one more than
|
|
3
|
+
* half. Five members need three, eight need five.
|
|
4
|
+
*
|
|
5
|
+
* The authoritative copy of this rule is on chain, in `NetworkMemberGovernance.governanceThreshold`,
|
|
6
|
+
* which every admission and removal sets the Safe's threshold from. This is the same expression for
|
|
7
|
+
* off-chain use: checking that a Safe is configured correctly, and reporting what a pending operation
|
|
8
|
+
* will change the threshold to. No call carries a threshold, so nothing depends on this value being
|
|
9
|
+
* right.
|
|
10
|
+
*
|
|
11
|
+
* @param memberCount How many members the Safe has owners for, after the change.
|
|
12
|
+
* @returns The required number of approvals.
|
|
13
|
+
*/
|
|
14
|
+
export function governanceThreshold(memberCount: number): number {
|
|
15
|
+
if (!Number.isInteger(memberCount) || memberCount < 1) {
|
|
16
|
+
throw new Error(`A member count must be a positive integer, got ${memberCount}`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return Math.floor(memberCount / 2) + 1
|
|
20
|
+
}
|