@mysten/sui 2.33.2 → 2.34.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.
@@ -0,0 +1,172 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { toHex } from '@mysten/bcs';
5
+ import { argon2dAsync } from '@noble/hashes/argon2.js';
6
+ import type { InferOutput } from 'valibot';
7
+ import {
8
+ check,
9
+ integer,
10
+ literal,
11
+ maxLength,
12
+ minValue,
13
+ number,
14
+ object,
15
+ pipe,
16
+ regex,
17
+ string,
18
+ } from 'valibot';
19
+
20
+ import { isValidTransactionDigest } from '../utils/sui-types.js';
21
+
22
+ const U64 = 1n << 64n;
23
+
24
+ export const FaucetU64 = pipe(
25
+ string(),
26
+ regex(/^(0|[1-9][0-9]{0,19})$/),
27
+ check((value) => BigInt(value) < U64),
28
+ );
29
+ export const FaucetAmount = pipe(
30
+ FaucetU64,
31
+ check((value) => BigInt(value) > 0n),
32
+ );
33
+ export const FaucetAddress = pipe(string(), regex(/^0x[0-9a-f]{64}$/));
34
+ export const FaucetDigest = pipe(
35
+ string(),
36
+ check((value) => value.length <= 44 && isValidTransactionDigest(value)),
37
+ );
38
+ export const FaucetDifficulty = pipe(
39
+ FaucetU64,
40
+ check((value) => BigInt(value) >= 2n && BigInt(value) <= 1n << 48n),
41
+ );
42
+
43
+ // PoW version 1 fixes these costs; never let a server choose the client's memory allocation.
44
+ export const FaucetChallenge = pipe(
45
+ object({
46
+ version: literal(1),
47
+ domain: literal('sui-faucet-pow/1'),
48
+ algorithm: literal('argon2d'),
49
+ argon2Version: literal(19),
50
+ salt: literal('sui-faucet-pow-1'),
51
+ memorySize: literal(8192),
52
+ iterations: literal(1),
53
+ parallelism: literal(1),
54
+ hashLength: literal(32),
55
+ chainId: FaucetDigest,
56
+ checkpointSeq: FaucetU64,
57
+ checkpointDigest: FaucetDigest,
58
+ randomBytes: pipe(
59
+ string(),
60
+ maxLength(1024),
61
+ regex(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/),
62
+ ),
63
+ faucetAddress: FaucetAddress,
64
+ recipient: FaucetAddress,
65
+ difficulty: FaucetDifficulty,
66
+ threshold: FaucetU64,
67
+ windowSeconds: pipe(number(), integer(), minValue(1)),
68
+ amountMist: FaucetAmount,
69
+ }),
70
+ check((value) => BigInt(value.threshold) === U64 / BigInt(value.difficulty)),
71
+ );
72
+
73
+ type Challenge = InferOutput<typeof FaucetChallenge>;
74
+
75
+ type NativeArgon2 = (
76
+ algorithm: 'argon2d',
77
+ parameters: {
78
+ message: string;
79
+ nonce: string;
80
+ parallelism: number;
81
+ tagLength: number;
82
+ memory: number;
83
+ passes: number;
84
+ },
85
+ callback: (error: Error | null, hash: Uint8Array) => void,
86
+ ) => void;
87
+
88
+ type NodeRuntime = {
89
+ process?: {
90
+ getBuiltinModule?: (id: 'node:crypto') => { argon2?: NativeArgon2 } | undefined;
91
+ };
92
+ };
93
+
94
+ export function hashFaucetProof(challenge: Challenge, nonce: bigint): Promise<Uint8Array> {
95
+ const preimage = [
96
+ challenge.domain,
97
+ challenge.chainId,
98
+ challenge.checkpointSeq,
99
+ challenge.checkpointDigest,
100
+ challenge.randomBytes,
101
+ challenge.faucetAddress,
102
+ challenge.recipient,
103
+ nonce.toString(),
104
+ ].join('\n');
105
+
106
+ // Avoid Node imports so browser bundles can use the Noble fallback without polyfills.
107
+ const nativeArgon2 = (globalThis as NodeRuntime).process?.getBuiltinModule?.(
108
+ 'node:crypto',
109
+ )?.argon2;
110
+ if (nativeArgon2) {
111
+ return new Promise((resolve, reject) => {
112
+ nativeArgon2(
113
+ 'argon2d',
114
+ {
115
+ message: preimage,
116
+ nonce: challenge.salt,
117
+ parallelism: 1,
118
+ tagLength: 32,
119
+ memory: 8192,
120
+ passes: 1,
121
+ },
122
+ (error, hash) => {
123
+ if (error) reject(error);
124
+ else resolve(hash);
125
+ },
126
+ );
127
+ });
128
+ }
129
+
130
+ return argon2dAsync(preimage, challenge.salt, {
131
+ version: 19,
132
+ m: 8192,
133
+ t: 1,
134
+ p: 1,
135
+ dkLen: 32,
136
+ });
137
+ }
138
+
139
+ export async function solveFaucetChallenge(
140
+ challenge: Challenge,
141
+ deadline: number,
142
+ signal: AbortSignal,
143
+ ) {
144
+ let nonce = globalThis.crypto.getRandomValues(new BigUint64Array(1))[0];
145
+ const threshold = BigInt(challenge.threshold);
146
+ while (Date.now() < deadline) {
147
+ signal.throwIfAborted();
148
+ let onAbort!: () => void;
149
+ let hash: Uint8Array;
150
+ try {
151
+ // Stop waiting even if native hashing is still queued in the worker pool.
152
+ hash = await Promise.race([
153
+ new Promise<never>((_, reject) => {
154
+ onAbort = () => reject(signal.reason);
155
+ signal.addEventListener('abort', onAbort, { once: true });
156
+ }),
157
+ hashFaucetProof(challenge, nonce),
158
+ ]);
159
+ } finally {
160
+ signal.removeEventListener('abort', onAbort);
161
+ }
162
+ // Yield to timers and cancellation, even when the hash implementation only yields microtasks.
163
+ await new Promise((resolve) => setTimeout(resolve, 0));
164
+ signal.throwIfAborted();
165
+ if (Date.now() >= deadline) break;
166
+ if (new DataView(hash.buffer, hash.byteOffset, hash.byteLength).getBigUint64(0) < threshold) {
167
+ return { nonce: nonce.toString(), hashHex: toHex(hash) };
168
+ }
169
+ nonce = (nonce + 1n) % U64;
170
+ }
171
+ return null;
172
+ }
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
 
4
4
  // This file is generated by genversion.mjs. Do not edit it directly.
5
5
 
6
- export const PACKAGE_VERSION = '2.33.2';
6
+ export const PACKAGE_VERSION = '2.34.0';