@glowlabs-org/utils 0.2.50 → 0.2.52
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/dist/cjs/browser.js +69 -7
- package/dist/cjs/browser.js.map +1 -1
- package/dist/cjs/utils/stake-control.d.ts +42 -9
- package/dist/esm/browser.js +66 -8
- package/dist/esm/browser.js.map +1 -1
- package/dist/esm/utils/stake-control.d.ts +42 -9
- package/package.json +1 -1
- package/src/utils/stake-control.ts +94 -7
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import z from "zod";
|
|
2
|
-
export declare const stakeControlEIP712Domain: {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export declare const stakeControlEIP712Domain: (chainId: number) => {
|
|
3
|
+
name: string;
|
|
4
|
+
version: string;
|
|
5
|
+
chainId: number;
|
|
6
|
+
verifyingContract: "0x0000000000000000000000000000000000000000";
|
|
7
7
|
};
|
|
8
8
|
export declare const stakeEIP712Types: {
|
|
9
9
|
readonly Stake: readonly [{
|
|
@@ -71,6 +71,21 @@ export declare const restakeEIP712Types: {
|
|
|
71
71
|
readonly type: "uint256";
|
|
72
72
|
}];
|
|
73
73
|
};
|
|
74
|
+
export declare const commitKickstarterEIP712Types: {
|
|
75
|
+
readonly CommitKickstarter: readonly [{
|
|
76
|
+
readonly name: "nonce";
|
|
77
|
+
readonly type: "uint256";
|
|
78
|
+
}, {
|
|
79
|
+
readonly name: "amount";
|
|
80
|
+
readonly type: "uint256";
|
|
81
|
+
}, {
|
|
82
|
+
readonly name: "kickstarterId";
|
|
83
|
+
readonly type: "string";
|
|
84
|
+
}, {
|
|
85
|
+
readonly name: "deadline";
|
|
86
|
+
readonly type: "uint256";
|
|
87
|
+
}];
|
|
88
|
+
};
|
|
74
89
|
export declare const stakeSignatureRequestSchema: z.ZodObject<{
|
|
75
90
|
wallet: z.ZodString;
|
|
76
91
|
signature: z.ZodString;
|
|
@@ -105,10 +120,19 @@ export declare const restakeSignatureRequestSchema: z.ZodObject<{
|
|
|
105
120
|
toZoneId: z.ZodString;
|
|
106
121
|
deadline: z.ZodString;
|
|
107
122
|
}, z.core.$strip>;
|
|
123
|
+
export declare const commitKickstarterSignatureRequestSchema: z.ZodObject<{
|
|
124
|
+
wallet: z.ZodString;
|
|
125
|
+
signature: z.ZodString;
|
|
126
|
+
nonce: z.ZodString;
|
|
127
|
+
amount: z.ZodString;
|
|
128
|
+
kickstarterId: z.ZodString;
|
|
129
|
+
deadline: z.ZodString;
|
|
130
|
+
}, z.core.$strip>;
|
|
108
131
|
export type StakeSignatureRequest = z.infer<typeof stakeSignatureRequestSchema>;
|
|
109
132
|
export type UnstakeUnlockSignatureRequest = z.infer<typeof unstakeUnlockSignatureRequestSchema>;
|
|
110
133
|
export type UnstakeMoveSignatureRequest = z.infer<typeof unstakeMoveSignatureRequestSchema>;
|
|
111
134
|
export type RestakeSignatureRequest = z.infer<typeof restakeSignatureRequestSchema>;
|
|
135
|
+
export type CommitKickstarterSignatureRequest = z.infer<typeof commitKickstarterSignatureRequestSchema>;
|
|
112
136
|
export type StakeMessage = {
|
|
113
137
|
nonce: bigint;
|
|
114
138
|
amount: bigint;
|
|
@@ -135,6 +159,12 @@ export type RestakeMessage = {
|
|
|
135
159
|
toZoneId: bigint;
|
|
136
160
|
deadline: bigint;
|
|
137
161
|
};
|
|
162
|
+
export type CommitKickstarterMessage = {
|
|
163
|
+
nonce: bigint;
|
|
164
|
+
amount: bigint;
|
|
165
|
+
kickstarterId: string;
|
|
166
|
+
deadline: bigint;
|
|
167
|
+
};
|
|
138
168
|
export type SignatureValidationReason = "deadline_expired" | "signature_failed" | "signer_mismatch" | null;
|
|
139
169
|
export type SignatureValidationResult = {
|
|
140
170
|
valid: boolean;
|
|
@@ -145,12 +175,15 @@ type StakeMessageInput = Pick<StakeSignatureRequest, "nonce" | "amount" | "toZon
|
|
|
145
175
|
type UnstakeUnlockMessageInput = Pick<UnstakeUnlockSignatureRequest, "nonce" | "amount" | "fromZoneId" | "deadline">;
|
|
146
176
|
type UnstakeMoveMessageInput = Pick<UnstakeMoveSignatureRequest, "nonce" | "amount" | "fromZoneId" | "toZoneId" | "deadline">;
|
|
147
177
|
type RestakeMessageInput = Pick<RestakeSignatureRequest, "nonce" | "amount" | "fromZoneId" | "toZoneId" | "deadline">;
|
|
178
|
+
type CommitKickstarterMessageInput = Pick<CommitKickstarterSignatureRequest, "nonce" | "amount" | "kickstarterId" | "deadline">;
|
|
148
179
|
export declare function buildStakeMessage(req: StakeMessageInput): StakeMessage;
|
|
149
180
|
export declare function buildUnstakeUnlockMessage(req: UnstakeUnlockMessageInput): UnstakeUnlockMessage;
|
|
150
181
|
export declare function buildUnstakeMoveMessage(req: UnstakeMoveMessageInput): UnstakeMoveMessage;
|
|
151
182
|
export declare function buildRestakeMessage(req: RestakeMessageInput): RestakeMessage;
|
|
152
|
-
export declare function
|
|
153
|
-
export declare function
|
|
154
|
-
export declare function
|
|
155
|
-
export declare function
|
|
183
|
+
export declare function buildCommitKickstarterMessage(req: CommitKickstarterMessageInput): CommitKickstarterMessage;
|
|
184
|
+
export declare function validateStakeSignature(input: StakeSignatureRequest, domain?: ReturnType<typeof stakeControlEIP712Domain>): Promise<SignatureValidationResult>;
|
|
185
|
+
export declare function validateUnstakeUnlockSignature(input: UnstakeUnlockSignatureRequest, domain?: ReturnType<typeof stakeControlEIP712Domain>): Promise<SignatureValidationResult>;
|
|
186
|
+
export declare function validateUnstakeMoveSignature(input: UnstakeMoveSignatureRequest, domain?: ReturnType<typeof stakeControlEIP712Domain>): Promise<SignatureValidationResult>;
|
|
187
|
+
export declare function validateRestakeSignature(input: RestakeSignatureRequest, domain?: ReturnType<typeof stakeControlEIP712Domain>): Promise<SignatureValidationResult>;
|
|
188
|
+
export declare function validateCommitKickstarterSignature(input: CommitKickstarterSignatureRequest, domain?: ReturnType<typeof stakeControlEIP712Domain>): Promise<SignatureValidationResult>;
|
|
156
189
|
export {};
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { verifyTypedData, type Hex } from "viem";
|
|
2
2
|
import z from "zod";
|
|
3
3
|
|
|
4
|
-
export const stakeControlEIP712Domain = {
|
|
4
|
+
export const stakeControlEIP712Domain = (chainId: number) => ({
|
|
5
5
|
name: "ControlManager",
|
|
6
6
|
version: "1",
|
|
7
|
-
chainId
|
|
7
|
+
chainId,
|
|
8
8
|
verifyingContract: "0x0000000000000000000000000000000000000000" as const,
|
|
9
|
-
}
|
|
9
|
+
});
|
|
10
10
|
|
|
11
11
|
// Distinct EIP-712 type sets for stake vs unstake
|
|
12
12
|
export const stakeEIP712Types = {
|
|
@@ -48,6 +48,15 @@ export const restakeEIP712Types = {
|
|
|
48
48
|
],
|
|
49
49
|
} as const;
|
|
50
50
|
|
|
51
|
+
export const commitKickstarterEIP712Types = {
|
|
52
|
+
CommitKickstarter: [
|
|
53
|
+
{ name: "nonce", type: "uint256" },
|
|
54
|
+
{ name: "amount", type: "uint256" },
|
|
55
|
+
{ name: "kickstarterId", type: "string" },
|
|
56
|
+
{ name: "deadline", type: "uint256" },
|
|
57
|
+
],
|
|
58
|
+
} as const;
|
|
59
|
+
|
|
51
60
|
// Separate request schemas for clarity between stake and unstake
|
|
52
61
|
export const stakeSignatureRequestSchema = z.object({
|
|
53
62
|
wallet: z.string().regex(/^0x[a-fA-F0-9]{40}$/),
|
|
@@ -87,6 +96,15 @@ export const restakeSignatureRequestSchema = z.object({
|
|
|
87
96
|
deadline: z.string(),
|
|
88
97
|
});
|
|
89
98
|
|
|
99
|
+
export const commitKickstarterSignatureRequestSchema = z.object({
|
|
100
|
+
wallet: z.string().regex(/^0x[a-fA-F0-9]{40}$/),
|
|
101
|
+
signature: z.string().regex(/^0x[a-fA-F0-9]{130}$/),
|
|
102
|
+
nonce: z.string(),
|
|
103
|
+
amount: z.string(),
|
|
104
|
+
kickstarterId: z.string(),
|
|
105
|
+
deadline: z.string(),
|
|
106
|
+
});
|
|
107
|
+
|
|
90
108
|
export type StakeSignatureRequest = z.infer<typeof stakeSignatureRequestSchema>;
|
|
91
109
|
export type UnstakeUnlockSignatureRequest = z.infer<
|
|
92
110
|
typeof unstakeUnlockSignatureRequestSchema
|
|
@@ -97,6 +115,9 @@ export type UnstakeMoveSignatureRequest = z.infer<
|
|
|
97
115
|
export type RestakeSignatureRequest = z.infer<
|
|
98
116
|
typeof restakeSignatureRequestSchema
|
|
99
117
|
>;
|
|
118
|
+
export type CommitKickstarterSignatureRequest = z.infer<
|
|
119
|
+
typeof commitKickstarterSignatureRequestSchema
|
|
120
|
+
>;
|
|
100
121
|
|
|
101
122
|
export type StakeMessage = {
|
|
102
123
|
nonce: bigint;
|
|
@@ -128,6 +149,13 @@ export type RestakeMessage = {
|
|
|
128
149
|
deadline: bigint;
|
|
129
150
|
};
|
|
130
151
|
|
|
152
|
+
export type CommitKickstarterMessage = {
|
|
153
|
+
nonce: bigint;
|
|
154
|
+
amount: bigint;
|
|
155
|
+
kickstarterId: string;
|
|
156
|
+
deadline: bigint;
|
|
157
|
+
};
|
|
158
|
+
|
|
131
159
|
export type SignatureValidationReason =
|
|
132
160
|
| "deadline_expired"
|
|
133
161
|
| "signature_failed"
|
|
@@ -163,6 +191,11 @@ type RestakeMessageInput = Pick<
|
|
|
163
191
|
"nonce" | "amount" | "fromZoneId" | "toZoneId" | "deadline"
|
|
164
192
|
>;
|
|
165
193
|
|
|
194
|
+
type CommitKickstarterMessageInput = Pick<
|
|
195
|
+
CommitKickstarterSignatureRequest,
|
|
196
|
+
"nonce" | "amount" | "kickstarterId" | "deadline"
|
|
197
|
+
>;
|
|
198
|
+
|
|
166
199
|
export function buildStakeMessage(req: StakeMessageInput): StakeMessage {
|
|
167
200
|
const nonce = BigInt(req.nonce);
|
|
168
201
|
const amount = BigInt(req.amount);
|
|
@@ -219,10 +252,26 @@ export function buildRestakeMessage(req: RestakeMessageInput): RestakeMessage {
|
|
|
219
252
|
return { nonce, amount, fromZoneId, toZoneId, deadline };
|
|
220
253
|
}
|
|
221
254
|
|
|
255
|
+
export function buildCommitKickstarterMessage(
|
|
256
|
+
req: CommitKickstarterMessageInput
|
|
257
|
+
): CommitKickstarterMessage {
|
|
258
|
+
const nonce = BigInt(req.nonce);
|
|
259
|
+
const amount = BigInt(req.amount);
|
|
260
|
+
const deadline = BigInt(req.deadline);
|
|
261
|
+
const kickstarterId = req.kickstarterId;
|
|
262
|
+
if (nonce < 0n) throw new Error("Nonce must be non-negative");
|
|
263
|
+
if (amount < 0n) throw new Error("Amount must be non-negative");
|
|
264
|
+
if (deadline < 0n) throw new Error("Deadline must be non-negative");
|
|
265
|
+
if (!kickstarterId) throw new Error("kickstarterId must be non-empty");
|
|
266
|
+
return { nonce, amount, kickstarterId, deadline };
|
|
267
|
+
}
|
|
268
|
+
|
|
222
269
|
// Helper to validate the signature using viem
|
|
223
270
|
export async function validateStakeSignature(
|
|
224
271
|
input: StakeSignatureRequest,
|
|
225
|
-
domain:
|
|
272
|
+
domain: ReturnType<
|
|
273
|
+
typeof stakeControlEIP712Domain
|
|
274
|
+
> = stakeControlEIP712Domain(1)
|
|
226
275
|
): Promise<SignatureValidationResult> {
|
|
227
276
|
const message = buildStakeMessage({
|
|
228
277
|
nonce: input.nonce,
|
|
@@ -252,7 +301,9 @@ export async function validateStakeSignature(
|
|
|
252
301
|
|
|
253
302
|
export async function validateUnstakeUnlockSignature(
|
|
254
303
|
input: UnstakeUnlockSignatureRequest,
|
|
255
|
-
domain:
|
|
304
|
+
domain: ReturnType<
|
|
305
|
+
typeof stakeControlEIP712Domain
|
|
306
|
+
> = stakeControlEIP712Domain(1)
|
|
256
307
|
): Promise<SignatureValidationResult> {
|
|
257
308
|
const message = buildUnstakeUnlockMessage({
|
|
258
309
|
nonce: input.nonce,
|
|
@@ -282,7 +333,9 @@ export async function validateUnstakeUnlockSignature(
|
|
|
282
333
|
|
|
283
334
|
export async function validateUnstakeMoveSignature(
|
|
284
335
|
input: UnstakeMoveSignatureRequest,
|
|
285
|
-
domain:
|
|
336
|
+
domain: ReturnType<
|
|
337
|
+
typeof stakeControlEIP712Domain
|
|
338
|
+
> = stakeControlEIP712Domain(1)
|
|
286
339
|
): Promise<SignatureValidationResult> {
|
|
287
340
|
const message = buildUnstakeMoveMessage({
|
|
288
341
|
nonce: input.nonce,
|
|
@@ -313,7 +366,9 @@ export async function validateUnstakeMoveSignature(
|
|
|
313
366
|
|
|
314
367
|
export async function validateRestakeSignature(
|
|
315
368
|
input: RestakeSignatureRequest,
|
|
316
|
-
domain:
|
|
369
|
+
domain: ReturnType<
|
|
370
|
+
typeof stakeControlEIP712Domain
|
|
371
|
+
> = stakeControlEIP712Domain(1)
|
|
317
372
|
): Promise<SignatureValidationResult> {
|
|
318
373
|
const message = buildRestakeMessage({
|
|
319
374
|
nonce: input.nonce,
|
|
@@ -341,3 +396,35 @@ export async function validateRestakeSignature(
|
|
|
341
396
|
return { valid: false, recovered: null, reason: "signature_failed" };
|
|
342
397
|
}
|
|
343
398
|
}
|
|
399
|
+
|
|
400
|
+
export async function validateCommitKickstarterSignature(
|
|
401
|
+
input: CommitKickstarterSignatureRequest,
|
|
402
|
+
domain: ReturnType<
|
|
403
|
+
typeof stakeControlEIP712Domain
|
|
404
|
+
> = stakeControlEIP712Domain(1)
|
|
405
|
+
): Promise<SignatureValidationResult> {
|
|
406
|
+
const message = buildCommitKickstarterMessage({
|
|
407
|
+
nonce: input.nonce,
|
|
408
|
+
amount: input.amount,
|
|
409
|
+
kickstarterId: input.kickstarterId,
|
|
410
|
+
deadline: input.deadline,
|
|
411
|
+
});
|
|
412
|
+
if (isDeadlineExpired(message.deadline)) {
|
|
413
|
+
return { valid: false, recovered: null, reason: "deadline_expired" };
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const verified = await verifyTypedData({
|
|
417
|
+
address: input.wallet as Hex,
|
|
418
|
+
domain,
|
|
419
|
+
types: commitKickstarterEIP712Types,
|
|
420
|
+
primaryType: "CommitKickstarter",
|
|
421
|
+
message,
|
|
422
|
+
signature: input.signature as Hex,
|
|
423
|
+
});
|
|
424
|
+
return verified
|
|
425
|
+
? { valid: true, recovered: input.wallet, reason: null }
|
|
426
|
+
: { valid: false, recovered: null, reason: "signer_mismatch" };
|
|
427
|
+
} catch (_) {
|
|
428
|
+
return { valid: false, recovered: null, reason: "signature_failed" };
|
|
429
|
+
}
|
|
430
|
+
}
|