@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.
- package/CHANGELOG.md +16 -0
- package/README.md +8 -4
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/index.d.mts +36 -36
- package/dist/cryptography/signature.d.mts +6 -6
- package/dist/faucet/faucet-v3.d.mts +34 -0
- package/dist/faucet/faucet-v3.d.mts.map +1 -0
- package/dist/faucet/faucet-v3.mjs +77 -0
- package/dist/faucet/faucet-v3.mjs.map +1 -0
- package/dist/faucet/index.d.mts +2 -1
- package/dist/faucet/index.mjs +2 -1
- package/dist/faucet/pow.mjs +96 -0
- package/dist/faucet/pow.mjs.map +1 -0
- package/dist/grpc/proto/sui/forking/v1alpha/forking_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
- package/dist/transactions/Transaction.d.mts +9 -9
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/docs/index.md +47 -7
- package/package.json +4 -4
- package/src/faucet/faucet-v3.ts +133 -0
- package/src/faucet/index.ts +2 -0
- package/src/faucet/pow.ts +172 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { isValidTransactionDigest } from "../utils/sui-types.mjs";
|
|
2
|
+
import { toHex } from "@mysten/bcs";
|
|
3
|
+
import { check, integer, literal, maxLength, minValue, number, object, pipe, regex, string } from "valibot";
|
|
4
|
+
import { argon2dAsync } from "@noble/hashes/argon2.js";
|
|
5
|
+
|
|
6
|
+
//#region src/faucet/pow.ts
|
|
7
|
+
const U64 = 1n << 64n;
|
|
8
|
+
const FaucetU64 = pipe(string(), regex(/^(0|[1-9][0-9]{0,19})$/), check((value) => BigInt(value) < U64));
|
|
9
|
+
const FaucetAmount = pipe(FaucetU64, check((value) => BigInt(value) > 0n));
|
|
10
|
+
const FaucetAddress = pipe(string(), regex(/^0x[0-9a-f]{64}$/));
|
|
11
|
+
const FaucetDigest = pipe(string(), check((value) => value.length <= 44 && isValidTransactionDigest(value)));
|
|
12
|
+
const FaucetDifficulty = pipe(FaucetU64, check((value) => BigInt(value) >= 2n && BigInt(value) <= 1n << 48n));
|
|
13
|
+
const FaucetChallenge = pipe(object({
|
|
14
|
+
version: literal(1),
|
|
15
|
+
domain: literal("sui-faucet-pow/1"),
|
|
16
|
+
algorithm: literal("argon2d"),
|
|
17
|
+
argon2Version: literal(19),
|
|
18
|
+
salt: literal("sui-faucet-pow-1"),
|
|
19
|
+
memorySize: literal(8192),
|
|
20
|
+
iterations: literal(1),
|
|
21
|
+
parallelism: literal(1),
|
|
22
|
+
hashLength: literal(32),
|
|
23
|
+
chainId: FaucetDigest,
|
|
24
|
+
checkpointSeq: FaucetU64,
|
|
25
|
+
checkpointDigest: FaucetDigest,
|
|
26
|
+
randomBytes: pipe(string(), maxLength(1024), regex(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/)),
|
|
27
|
+
faucetAddress: FaucetAddress,
|
|
28
|
+
recipient: FaucetAddress,
|
|
29
|
+
difficulty: FaucetDifficulty,
|
|
30
|
+
threshold: FaucetU64,
|
|
31
|
+
windowSeconds: pipe(number(), integer(), minValue(1)),
|
|
32
|
+
amountMist: FaucetAmount
|
|
33
|
+
}), check((value) => BigInt(value.threshold) === U64 / BigInt(value.difficulty)));
|
|
34
|
+
function hashFaucetProof(challenge, nonce) {
|
|
35
|
+
const preimage = [
|
|
36
|
+
challenge.domain,
|
|
37
|
+
challenge.chainId,
|
|
38
|
+
challenge.checkpointSeq,
|
|
39
|
+
challenge.checkpointDigest,
|
|
40
|
+
challenge.randomBytes,
|
|
41
|
+
challenge.faucetAddress,
|
|
42
|
+
challenge.recipient,
|
|
43
|
+
nonce.toString()
|
|
44
|
+
].join("\n");
|
|
45
|
+
const nativeArgon2 = globalThis.process?.getBuiltinModule?.("node:crypto")?.argon2;
|
|
46
|
+
if (nativeArgon2) return new Promise((resolve, reject) => {
|
|
47
|
+
nativeArgon2("argon2d", {
|
|
48
|
+
message: preimage,
|
|
49
|
+
nonce: challenge.salt,
|
|
50
|
+
parallelism: 1,
|
|
51
|
+
tagLength: 32,
|
|
52
|
+
memory: 8192,
|
|
53
|
+
passes: 1
|
|
54
|
+
}, (error, hash) => {
|
|
55
|
+
if (error) reject(error);
|
|
56
|
+
else resolve(hash);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return argon2dAsync(preimage, challenge.salt, {
|
|
60
|
+
version: 19,
|
|
61
|
+
m: 8192,
|
|
62
|
+
t: 1,
|
|
63
|
+
p: 1,
|
|
64
|
+
dkLen: 32
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async function solveFaucetChallenge(challenge, deadline, signal) {
|
|
68
|
+
let nonce = globalThis.crypto.getRandomValues(new BigUint64Array(1))[0];
|
|
69
|
+
const threshold = BigInt(challenge.threshold);
|
|
70
|
+
while (Date.now() < deadline) {
|
|
71
|
+
signal.throwIfAborted();
|
|
72
|
+
let onAbort;
|
|
73
|
+
let hash;
|
|
74
|
+
try {
|
|
75
|
+
hash = await Promise.race([new Promise((_, reject) => {
|
|
76
|
+
onAbort = () => reject(signal.reason);
|
|
77
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
78
|
+
}), hashFaucetProof(challenge, nonce)]);
|
|
79
|
+
} finally {
|
|
80
|
+
signal.removeEventListener("abort", onAbort);
|
|
81
|
+
}
|
|
82
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
83
|
+
signal.throwIfAborted();
|
|
84
|
+
if (Date.now() >= deadline) break;
|
|
85
|
+
if (new DataView(hash.buffer, hash.byteOffset, hash.byteLength).getBigUint64(0) < threshold) return {
|
|
86
|
+
nonce: nonce.toString(),
|
|
87
|
+
hashHex: toHex(hash)
|
|
88
|
+
};
|
|
89
|
+
nonce = (nonce + 1n) % U64;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { FaucetAddress, FaucetAmount, FaucetChallenge, FaucetDifficulty, FaucetDigest, solveFaucetChallenge };
|
|
96
|
+
//# sourceMappingURL=pow.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pow.mjs","names":[],"sources":["../../src/faucet/pow.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { toHex } from '@mysten/bcs';\nimport { argon2dAsync } from '@noble/hashes/argon2.js';\nimport type { InferOutput } from 'valibot';\nimport {\n\tcheck,\n\tinteger,\n\tliteral,\n\tmaxLength,\n\tminValue,\n\tnumber,\n\tobject,\n\tpipe,\n\tregex,\n\tstring,\n} from 'valibot';\n\nimport { isValidTransactionDigest } from '../utils/sui-types.js';\n\nconst U64 = 1n << 64n;\n\nexport const FaucetU64 = pipe(\n\tstring(),\n\tregex(/^(0|[1-9][0-9]{0,19})$/),\n\tcheck((value) => BigInt(value) < U64),\n);\nexport const FaucetAmount = pipe(\n\tFaucetU64,\n\tcheck((value) => BigInt(value) > 0n),\n);\nexport const FaucetAddress = pipe(string(), regex(/^0x[0-9a-f]{64}$/));\nexport const FaucetDigest = pipe(\n\tstring(),\n\tcheck((value) => value.length <= 44 && isValidTransactionDigest(value)),\n);\nexport const FaucetDifficulty = pipe(\n\tFaucetU64,\n\tcheck((value) => BigInt(value) >= 2n && BigInt(value) <= 1n << 48n),\n);\n\n// PoW version 1 fixes these costs; never let a server choose the client's memory allocation.\nexport const FaucetChallenge = pipe(\n\tobject({\n\t\tversion: literal(1),\n\t\tdomain: literal('sui-faucet-pow/1'),\n\t\talgorithm: literal('argon2d'),\n\t\targon2Version: literal(19),\n\t\tsalt: literal('sui-faucet-pow-1'),\n\t\tmemorySize: literal(8192),\n\t\titerations: literal(1),\n\t\tparallelism: literal(1),\n\t\thashLength: literal(32),\n\t\tchainId: FaucetDigest,\n\t\tcheckpointSeq: FaucetU64,\n\t\tcheckpointDigest: FaucetDigest,\n\t\trandomBytes: pipe(\n\t\t\tstring(),\n\t\t\tmaxLength(1024),\n\t\t\tregex(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/),\n\t\t),\n\t\tfaucetAddress: FaucetAddress,\n\t\trecipient: FaucetAddress,\n\t\tdifficulty: FaucetDifficulty,\n\t\tthreshold: FaucetU64,\n\t\twindowSeconds: pipe(number(), integer(), minValue(1)),\n\t\tamountMist: FaucetAmount,\n\t}),\n\tcheck((value) => BigInt(value.threshold) === U64 / BigInt(value.difficulty)),\n);\n\ntype Challenge = InferOutput<typeof FaucetChallenge>;\n\ntype NativeArgon2 = (\n\talgorithm: 'argon2d',\n\tparameters: {\n\t\tmessage: string;\n\t\tnonce: string;\n\t\tparallelism: number;\n\t\ttagLength: number;\n\t\tmemory: number;\n\t\tpasses: number;\n\t},\n\tcallback: (error: Error | null, hash: Uint8Array) => void,\n) => void;\n\ntype NodeRuntime = {\n\tprocess?: {\n\t\tgetBuiltinModule?: (id: 'node:crypto') => { argon2?: NativeArgon2 } | undefined;\n\t};\n};\n\nexport function hashFaucetProof(challenge: Challenge, nonce: bigint): Promise<Uint8Array> {\n\tconst preimage = [\n\t\tchallenge.domain,\n\t\tchallenge.chainId,\n\t\tchallenge.checkpointSeq,\n\t\tchallenge.checkpointDigest,\n\t\tchallenge.randomBytes,\n\t\tchallenge.faucetAddress,\n\t\tchallenge.recipient,\n\t\tnonce.toString(),\n\t].join('\\n');\n\n\t// Avoid Node imports so browser bundles can use the Noble fallback without polyfills.\n\tconst nativeArgon2 = (globalThis as NodeRuntime).process?.getBuiltinModule?.(\n\t\t'node:crypto',\n\t)?.argon2;\n\tif (nativeArgon2) {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tnativeArgon2(\n\t\t\t\t'argon2d',\n\t\t\t\t{\n\t\t\t\t\tmessage: preimage,\n\t\t\t\t\tnonce: challenge.salt,\n\t\t\t\t\tparallelism: 1,\n\t\t\t\t\ttagLength: 32,\n\t\t\t\t\tmemory: 8192,\n\t\t\t\t\tpasses: 1,\n\t\t\t\t},\n\t\t\t\t(error, hash) => {\n\t\t\t\t\tif (error) reject(error);\n\t\t\t\t\telse resolve(hash);\n\t\t\t\t},\n\t\t\t);\n\t\t});\n\t}\n\n\treturn argon2dAsync(preimage, challenge.salt, {\n\t\tversion: 19,\n\t\tm: 8192,\n\t\tt: 1,\n\t\tp: 1,\n\t\tdkLen: 32,\n\t});\n}\n\nexport async function solveFaucetChallenge(\n\tchallenge: Challenge,\n\tdeadline: number,\n\tsignal: AbortSignal,\n) {\n\tlet nonce = globalThis.crypto.getRandomValues(new BigUint64Array(1))[0];\n\tconst threshold = BigInt(challenge.threshold);\n\twhile (Date.now() < deadline) {\n\t\tsignal.throwIfAborted();\n\t\tlet onAbort!: () => void;\n\t\tlet hash: Uint8Array;\n\t\ttry {\n\t\t\t// Stop waiting even if native hashing is still queued in the worker pool.\n\t\t\thash = await Promise.race([\n\t\t\t\tnew Promise<never>((_, reject) => {\n\t\t\t\t\tonAbort = () => reject(signal.reason);\n\t\t\t\t\tsignal.addEventListener('abort', onAbort, { once: true });\n\t\t\t\t}),\n\t\t\t\thashFaucetProof(challenge, nonce),\n\t\t\t]);\n\t\t} finally {\n\t\t\tsignal.removeEventListener('abort', onAbort);\n\t\t}\n\t\t// Yield to timers and cancellation, even when the hash implementation only yields microtasks.\n\t\tawait new Promise((resolve) => setTimeout(resolve, 0));\n\t\tsignal.throwIfAborted();\n\t\tif (Date.now() >= deadline) break;\n\t\tif (new DataView(hash.buffer, hash.byteOffset, hash.byteLength).getBigUint64(0) < threshold) {\n\t\t\treturn { nonce: nonce.toString(), hashHex: toHex(hash) };\n\t\t}\n\t\tnonce = (nonce + 1n) % U64;\n\t}\n\treturn null;\n}\n"],"mappings":"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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AdvanceCheckpointRequest, AdvanceCheckpointResponse, AdvanceClockRequest, AdvanceClockResponse, GetStatusRequest, GetStatusResponse } from "./forking_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime0 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc0 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/forking/v1alpha/forking_service.client.d.ts
|
|
@@ -37,9 +37,9 @@ interface IForkingServiceClient {
|
|
|
37
37
|
declare class ForkingServiceClient implements IForkingServiceClient, ServiceInfo {
|
|
38
38
|
private readonly _transport;
|
|
39
39
|
typeName: string;
|
|
40
|
-
methods:
|
|
40
|
+
methods: _protobuf_ts_runtime_rpc0.MethodInfo<any, any>[];
|
|
41
41
|
options: {
|
|
42
|
-
[extensionName: string]:
|
|
42
|
+
[extensionName: string]: _protobuf_ts_runtime0.JsonValue;
|
|
43
43
|
};
|
|
44
44
|
constructor(_transport: RpcTransport);
|
|
45
45
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BatchGetObjectsRequest, BatchGetObjectsResponse, BatchGetTransactionsRequest, BatchGetTransactionsResponse, GetCheckpointRequest, GetCheckpointResponse, GetEpochRequest, GetEpochResponse, GetObjectRequest, GetObjectResponse, GetServiceInfoRequest, GetServiceInfoResponse, GetTransactionRequest, GetTransactionResponse, ListCheckpointsRequest, ListCheckpointsResponse, ListEventsRequest, ListEventsResponse, ListTransactionsRequest, ListTransactionsResponse } from "./ledger_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime1 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc1 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServerStreamingCall, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/ledger_service.client.d.ts
|
|
@@ -73,9 +73,9 @@ interface ILedgerServiceClient {
|
|
|
73
73
|
declare class LedgerServiceClient implements ILedgerServiceClient, ServiceInfo {
|
|
74
74
|
private readonly _transport;
|
|
75
75
|
typeName: string;
|
|
76
|
-
methods:
|
|
76
|
+
methods: _protobuf_ts_runtime_rpc1.MethodInfo<any, any>[];
|
|
77
77
|
options: {
|
|
78
|
-
[extensionName: string]:
|
|
78
|
+
[extensionName: string]: _protobuf_ts_runtime1.JsonValue;
|
|
79
79
|
};
|
|
80
80
|
constructor(_transport: RpcTransport);
|
|
81
81
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { GetDatatypeRequest, GetDatatypeResponse, GetFunctionRequest, GetFunctionResponse, GetPackageRequest, GetPackageResponse, ListPackageVersionsRequest, ListPackageVersionsResponse } from "./move_package_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime2 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc2 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/move_package_service.client.d.ts
|
|
@@ -31,9 +31,9 @@ interface IMovePackageServiceClient {
|
|
|
31
31
|
declare class MovePackageServiceClient implements IMovePackageServiceClient, ServiceInfo {
|
|
32
32
|
private readonly _transport;
|
|
33
33
|
typeName: string;
|
|
34
|
-
methods:
|
|
34
|
+
methods: _protobuf_ts_runtime_rpc2.MethodInfo<any, any>[];
|
|
35
35
|
options: {
|
|
36
|
-
[extensionName: string]:
|
|
36
|
+
[extensionName: string]: _protobuf_ts_runtime2.JsonValue;
|
|
37
37
|
};
|
|
38
38
|
constructor(_transport: RpcTransport);
|
|
39
39
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LookupNameRequest, LookupNameResponse, ReverseLookupNameRequest, ReverseLookupNameResponse } from "./name_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime4 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc4 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/name_service.client.d.ts
|
|
@@ -23,9 +23,9 @@ interface INameServiceClient {
|
|
|
23
23
|
declare class NameServiceClient implements INameServiceClient, ServiceInfo {
|
|
24
24
|
private readonly _transport;
|
|
25
25
|
typeName: string;
|
|
26
|
-
methods:
|
|
26
|
+
methods: _protobuf_ts_runtime_rpc4.MethodInfo<any, any>[];
|
|
27
27
|
options: {
|
|
28
|
-
[extensionName: string]:
|
|
28
|
+
[extensionName: string]: _protobuf_ts_runtime4.JsonValue;
|
|
29
29
|
};
|
|
30
30
|
constructor(_transport: RpcTransport);
|
|
31
31
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { VerifySignatureRequest, VerifySignatureResponse } from "./signature_verification_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime6 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc6 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/signature_verification_service.client.d.ts
|
|
@@ -21,9 +21,9 @@ interface ISignatureVerificationServiceClient {
|
|
|
21
21
|
declare class SignatureVerificationServiceClient implements ISignatureVerificationServiceClient, ServiceInfo {
|
|
22
22
|
private readonly _transport;
|
|
23
23
|
typeName: string;
|
|
24
|
-
methods:
|
|
24
|
+
methods: _protobuf_ts_runtime_rpc6.MethodInfo<any, any>[];
|
|
25
25
|
options: {
|
|
26
|
-
[extensionName: string]:
|
|
26
|
+
[extensionName: string]: _protobuf_ts_runtime6.JsonValue;
|
|
27
27
|
};
|
|
28
28
|
constructor(_transport: RpcTransport);
|
|
29
29
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { GetBalanceRequest, GetBalanceResponse, GetCoinInfoRequest, GetCoinInfoResponse, ListBalancesRequest, ListBalancesResponse, ListDynamicFieldsRequest, ListDynamicFieldsResponse, ListOwnedObjectsRequest, ListOwnedObjectsResponse } from "./state_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime5 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc5 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServiceInfo, UnaryCall } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/state_service.client.d.ts
|
|
@@ -35,9 +35,9 @@ interface IStateServiceClient {
|
|
|
35
35
|
declare class StateServiceClient implements IStateServiceClient, ServiceInfo {
|
|
36
36
|
private readonly _transport;
|
|
37
37
|
typeName: string;
|
|
38
|
-
methods:
|
|
38
|
+
methods: _protobuf_ts_runtime_rpc5.MethodInfo<any, any>[];
|
|
39
39
|
options: {
|
|
40
|
-
[extensionName: string]:
|
|
40
|
+
[extensionName: string]: _protobuf_ts_runtime5.JsonValue;
|
|
41
41
|
};
|
|
42
42
|
constructor(_transport: RpcTransport);
|
|
43
43
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SubscribeCheckpointsRequest, SubscribeCheckpointsResponse, SubscribeEventsRequest, SubscribeEventsResponse, SubscribeTransactionsRequest, SubscribeTransactionsResponse } from "./subscription_service.mjs";
|
|
2
|
-
import * as
|
|
3
|
-
import * as
|
|
2
|
+
import * as _protobuf_ts_runtime3 from "@protobuf-ts/runtime";
|
|
3
|
+
import * as _protobuf_ts_runtime_rpc3 from "@protobuf-ts/runtime-rpc";
|
|
4
4
|
import { RpcOptions, RpcTransport, ServerStreamingCall, ServiceInfo } from "@protobuf-ts/runtime-rpc";
|
|
5
5
|
|
|
6
6
|
//#region src/grpc/proto/sui/rpc/v2/subscription_service.client.d.ts
|
|
@@ -100,9 +100,9 @@ interface ISubscriptionServiceClient {
|
|
|
100
100
|
declare class SubscriptionServiceClient implements ISubscriptionServiceClient, ServiceInfo {
|
|
101
101
|
private readonly _transport;
|
|
102
102
|
typeName: string;
|
|
103
|
-
methods:
|
|
103
|
+
methods: _protobuf_ts_runtime_rpc3.MethodInfo<any, any>[];
|
|
104
104
|
options: {
|
|
105
|
-
[extensionName: string]:
|
|
105
|
+
[extensionName: string]: _protobuf_ts_runtime3.JsonValue;
|
|
106
106
|
};
|
|
107
107
|
constructor(_transport: RpcTransport);
|
|
108
108
|
/**
|
|
@@ -8,7 +8,7 @@ import { createPure } from "./pure.mjs";
|
|
|
8
8
|
import { BalanceOptions } from "./intents/BalanceOptions.mjs";
|
|
9
9
|
import { SignatureWithBytes, Signer } from "../cryptography/keypair.mjs";
|
|
10
10
|
import "../cryptography/index.mjs";
|
|
11
|
-
import * as
|
|
11
|
+
import * as _mysten_bcs1254 from "@mysten/bcs";
|
|
12
12
|
import { SerializedBcs } from "@mysten/bcs";
|
|
13
13
|
import { InferInput } from "valibot";
|
|
14
14
|
|
|
@@ -110,7 +110,7 @@ declare class Transaction {
|
|
|
110
110
|
getData(): {
|
|
111
111
|
version: 2;
|
|
112
112
|
sender?: string | null | undefined;
|
|
113
|
-
expiration?:
|
|
113
|
+
expiration?: _mysten_bcs1254.EnumOutputShapeWithKeys<{
|
|
114
114
|
None: true;
|
|
115
115
|
Epoch: string | number;
|
|
116
116
|
ValidDuring: {
|
|
@@ -133,7 +133,7 @@ declare class Transaction {
|
|
|
133
133
|
chain: string;
|
|
134
134
|
nonce: number;
|
|
135
135
|
};
|
|
136
|
-
}, "
|
|
136
|
+
}, "Epoch" | "None" | "ValidDuring" | "Validity"> | null | undefined;
|
|
137
137
|
gasData: {
|
|
138
138
|
budget: string | number | null;
|
|
139
139
|
price: string | number | null;
|
|
@@ -144,8 +144,8 @@ declare class Transaction {
|
|
|
144
144
|
digest: string;
|
|
145
145
|
}[] | null;
|
|
146
146
|
};
|
|
147
|
-
inputs:
|
|
148
|
-
Object:
|
|
147
|
+
inputs: _mysten_bcs1254.EnumOutputShapeWithKeys<{
|
|
148
|
+
Object: _mysten_bcs1254.EnumOutputShapeWithKeys<{
|
|
149
149
|
ImmOrOwnedObject: {
|
|
150
150
|
objectId: string;
|
|
151
151
|
version: string | number;
|
|
@@ -161,7 +161,7 @@ declare class Transaction {
|
|
|
161
161
|
version: string | number;
|
|
162
162
|
digest: string;
|
|
163
163
|
};
|
|
164
|
-
}, "
|
|
164
|
+
}, "Receiving" | "ImmOrOwnedObject" | "SharedObject">;
|
|
165
165
|
Pure: {
|
|
166
166
|
bytes: string;
|
|
167
167
|
};
|
|
@@ -184,7 +184,7 @@ declare class Transaction {
|
|
|
184
184
|
Balance: string;
|
|
185
185
|
$kind: "Balance";
|
|
186
186
|
};
|
|
187
|
-
withdrawFrom:
|
|
187
|
+
withdrawFrom: _mysten_bcs1254.EnumOutputShapeWithKeys<{
|
|
188
188
|
Sender: true;
|
|
189
189
|
Sponsor: true;
|
|
190
190
|
SenderAllowance: {
|
|
@@ -193,8 +193,8 @@ declare class Transaction {
|
|
|
193
193
|
};
|
|
194
194
|
}, "Sender" | "Sponsor" | "SenderAllowance">;
|
|
195
195
|
};
|
|
196
|
-
}, "
|
|
197
|
-
commands:
|
|
196
|
+
}, "Object" | "Pure" | "FundsWithdrawal" | "UnresolvedPure" | "UnresolvedObject">[];
|
|
197
|
+
commands: _mysten_bcs1254.EnumOutputShapeWithKeys<{
|
|
198
198
|
MoveCall: {
|
|
199
199
|
package: string;
|
|
200
200
|
module: string;
|
package/dist/version.mjs
CHANGED
package/dist/version.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.
|
|
1
|
+
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.34.0';\n"],"mappings":";AAKA,MAAa,kBAAkB"}
|
package/dist/zklogin/bcs.d.mts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _mysten_bcs1241 from "@mysten/bcs";
|
|
2
2
|
import { InferBcsInput } from "@mysten/bcs";
|
|
3
3
|
|
|
4
4
|
//#region src/zklogin/bcs.d.ts
|
|
5
|
-
declare const zkLoginSignature:
|
|
6
|
-
inputs:
|
|
7
|
-
proofPoints:
|
|
8
|
-
a:
|
|
5
|
+
declare const zkLoginSignature: _mysten_bcs1241.BcsStruct<{
|
|
6
|
+
inputs: _mysten_bcs1241.BcsStruct<{
|
|
7
|
+
proofPoints: _mysten_bcs1241.BcsStruct<{
|
|
8
|
+
a: _mysten_bcs1241.BcsType<string[], Iterable<string> & {
|
|
9
9
|
length: number;
|
|
10
10
|
}, string>;
|
|
11
|
-
b:
|
|
11
|
+
b: _mysten_bcs1241.BcsType<string[][], Iterable<Iterable<string> & {
|
|
12
12
|
length: number;
|
|
13
13
|
}> & {
|
|
14
14
|
length: number;
|
|
15
15
|
}, string>;
|
|
16
|
-
c:
|
|
16
|
+
c: _mysten_bcs1241.BcsType<string[], Iterable<string> & {
|
|
17
17
|
length: number;
|
|
18
18
|
}, string>;
|
|
19
19
|
}, string>;
|
|
20
|
-
issBase64Details:
|
|
21
|
-
value:
|
|
22
|
-
indexMod4:
|
|
20
|
+
issBase64Details: _mysten_bcs1241.BcsStruct<{
|
|
21
|
+
value: _mysten_bcs1241.BcsType<string, string, "string">;
|
|
22
|
+
indexMod4: _mysten_bcs1241.BcsType<number, number, "u8">;
|
|
23
23
|
}, string>;
|
|
24
|
-
headerBase64:
|
|
25
|
-
addressSeed:
|
|
24
|
+
headerBase64: _mysten_bcs1241.BcsType<string, string, "string">;
|
|
25
|
+
addressSeed: _mysten_bcs1241.BcsType<string, string, "string">;
|
|
26
26
|
}, string>;
|
|
27
|
-
maxEpoch:
|
|
28
|
-
userSignature:
|
|
27
|
+
maxEpoch: _mysten_bcs1241.BcsType<string, string | number | bigint, "u64">;
|
|
28
|
+
userSignature: _mysten_bcs1241.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
|
|
29
29
|
}, string>;
|
|
30
30
|
type ZkLoginSignature = InferBcsInput<typeof zkLoginSignature>;
|
|
31
31
|
type ZkLoginSignatureInputs = ZkLoginSignature['inputs'];
|
package/docs/index.md
CHANGED
|
@@ -50,8 +50,8 @@ The following table lists the locations for Sui networks.
|
|
|
50
50
|
| Network | Full node | faucet |
|
|
51
51
|
| ------- | ------------------------------------- | ---------------------------------------- |
|
|
52
52
|
| local | `http://127.0.0.1:9000` (default) | `http://127.0.0.1:9123/v2/gas` (default) |
|
|
53
|
-
| Devnet | `https://fullnode.devnet.sui.io:443` | `https://faucet.devnet.sui.io/
|
|
54
|
-
| Testnet | `https://fullnode.testnet.sui.io:443` | `https://faucet.testnet.sui.io/
|
|
53
|
+
| Devnet | `https://fullnode.devnet.sui.io:443` | `https://faucet.devnet.sui.io/v3/gas` |
|
|
54
|
+
| Testnet | `https://fullnode.testnet.sui.io:443` | `https://faucet.testnet.sui.io/v3/gas` |
|
|
55
55
|
| Mainnet | `https://fullnode.mainnet.sui.io:443` | `null` |
|
|
56
56
|
|
|
57
57
|
> **Warning:** Use dedicated nodes/shared services rather than public endpoints for production apps. The public
|
|
@@ -92,7 +92,7 @@ console.log('Address:', keypair.toSuiAddress());
|
|
|
92
92
|
console.log('Secret key:', keypair.getSecretKey());
|
|
93
93
|
|
|
94
94
|
// Request SUI from the devnet faucet
|
|
95
|
-
await
|
|
95
|
+
await requestSuiFromFaucetV3({
|
|
96
96
|
host: getFaucetHost('devnet'),
|
|
97
97
|
recipient: keypair.toSuiAddress(),
|
|
98
98
|
});
|
|
@@ -183,17 +183,57 @@ node transfer.ts
|
|
|
183
183
|
|
|
184
184
|
### Faucet
|
|
185
185
|
|
|
186
|
-
|
|
187
|
-
|
|
186
|
+
Use `requestSuiFromFaucetV3` on Devnet and Testnet. It fetches a challenge from `/v3/challenge`,
|
|
187
|
+
computes Argon2d proof-of-work, and submits the proof to `/v3/gas`:
|
|
188
188
|
|
|
189
189
|
```typescript
|
|
190
190
|
|
|
191
|
-
await
|
|
191
|
+
const result = await requestSuiFromFaucetV3({
|
|
192
192
|
host: getFaucetHost('testnet'),
|
|
193
|
-
recipient: '0xYourAddress',
|
|
193
|
+
recipient: '0xYourAddress', // Replace with your Sui address.
|
|
194
|
+
timeout: 180_000,
|
|
194
195
|
});
|
|
196
|
+
|
|
197
|
+
console.log(result.digest, result.amountMist);
|
|
195
198
|
```
|
|
196
199
|
|
|
200
|
+
V3 credits the recipient's address balance, not a coin object. Its response contains `digest`,
|
|
201
|
+
`recipient`, `amountMist`, and `difficulty`; it does not contain V2's `coins_sent`. Amounts are
|
|
202
|
+
returned as decimal strings to preserve precision. The faucet sets the payout amount.
|
|
203
|
+
|
|
204
|
+
Set `host` to the faucet's origin (scheme, hostname, and optional port), such as
|
|
205
|
+
`https://faucet.testnet.sui.io`. The API must serve `/v3/challenge` and `/v3/gas` at the origin
|
|
206
|
+
root. Path prefixes such as `/faucet` are not supported; any path in `host` is discarded.
|
|
207
|
+
|
|
208
|
+
Optional `headers` apply to both requests. The `timeout` covers fetching, solving, and submission,
|
|
209
|
+
and defaults to 180,000 milliseconds. Pass an `AbortSignal` as `signal` to cancel earlier.
|
|
210
|
+
|
|
211
|
+
The helper uses Node's asynchronous native Argon2d when available (Node 24.7 or newer), with Noble
|
|
212
|
+
as the fallback for Node 22 and browsers. Native hashing does not require an addon. Browser apps can
|
|
213
|
+
run the helper in a Web Worker to keep proof-of-work off the UI thread.
|
|
214
|
+
|
|
215
|
+
For a local **no-PoW faucet**, keep using V2:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
|
|
219
|
+
await requestSuiFromFaucetV2({
|
|
220
|
+
host: getFaucetHost('localnet'),
|
|
221
|
+
recipient: '0xYourAddress', // Replace with your Sui address.
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
For a local **PoW faucet**, use `requestSuiFromFaucetV3` and set `host` to its origin, with
|
|
226
|
+
`/v3/challenge` and `/v3/gas` served at the origin root. If it listens on the default local faucet
|
|
227
|
+
port, use `getFaucetHost('localnet')`. The helper does not infer the protocol from the host or fall
|
|
228
|
+
back to V2.
|
|
229
|
+
|
|
230
|
+
V3 refreshes expired challenges before submission but never automatically retries a payout. HTTP 429
|
|
231
|
+
throws `FaucetRateLimitError`. Other HTTP errors throw `FaucetError`, which exposes `status` and,
|
|
232
|
+
when provided by the server, `code` and a transaction `digest`. Non-JSON error responses use a
|
|
233
|
+
fallback message and preserve the parse error as `cause`. If a digest is present, resolve that
|
|
234
|
+
transaction before requesting another payout. A timeout or connection loss during submission does
|
|
235
|
+
not prove the payout failed; do not blindly retry it.
|
|
236
|
+
|
|
197
237
|
> **Note:** Faucets on Devnet and Testnet are rate limited. If you hit the limit, wait before trying again.
|
|
198
238
|
> For Testnet, you can also get SUI through the web UI at `faucet.sui.io` or through the [Sui
|
|
199
239
|
> Discord](https://discord.gg/sui) faucet channels.
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
4
4
|
"description": "Sui TypeScript API",
|
|
5
5
|
"homepage": "https://sdk.mystenlabs.com",
|
|
6
|
-
"version": "2.
|
|
6
|
+
"version": "2.34.0",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"files": [
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"@graphql-codegen/typescript": "6.1.0",
|
|
139
139
|
"@graphql-codegen/typescript-document-nodes": "6.1.0",
|
|
140
140
|
"@graphql-codegen/typescript-operations": "^6.1.6",
|
|
141
|
-
"@grpc/grpc-js": ">=1.14.
|
|
141
|
+
"@grpc/grpc-js": ">=1.14.5",
|
|
142
142
|
"@parcel/watcher": "^2.6.0",
|
|
143
143
|
"@protobuf-ts/grpc-transport": "^2.11.1",
|
|
144
144
|
"@types/node": "^26.2.0",
|
|
@@ -168,8 +168,8 @@
|
|
|
168
168
|
"graphql": "^17.0.2",
|
|
169
169
|
"poseidon-lite": "0.3.0",
|
|
170
170
|
"valibot": "^1.4.2",
|
|
171
|
-
"@mysten/
|
|
172
|
-
"@mysten/
|
|
171
|
+
"@mysten/utils": "^0.4.1",
|
|
172
|
+
"@mysten/bcs": "^2.1.2"
|
|
173
173
|
},
|
|
174
174
|
"scripts": {
|
|
175
175
|
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { literal, object, parse } from 'valibot';
|
|
5
|
+
|
|
6
|
+
import { isValidSuiAddress, normalizeSuiAddress } from '../utils/sui-types.js';
|
|
7
|
+
import { FaucetRateLimitError } from './faucet.js';
|
|
8
|
+
import {
|
|
9
|
+
FaucetAddress,
|
|
10
|
+
FaucetAmount,
|
|
11
|
+
FaucetChallenge,
|
|
12
|
+
FaucetDifficulty,
|
|
13
|
+
FaucetDigest,
|
|
14
|
+
solveFaucetChallenge,
|
|
15
|
+
} from './pow.js';
|
|
16
|
+
|
|
17
|
+
export interface FaucetResponseV3 {
|
|
18
|
+
status: 'success';
|
|
19
|
+
digest: string;
|
|
20
|
+
recipient: string;
|
|
21
|
+
amountMist: string;
|
|
22
|
+
difficulty: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A faucet HTTP error. Resolve any transaction digest before requesting another payout. */
|
|
26
|
+
export class FaucetError extends Error {
|
|
27
|
+
readonly status: number;
|
|
28
|
+
readonly code?: string;
|
|
29
|
+
readonly digest?: string;
|
|
30
|
+
|
|
31
|
+
constructor(
|
|
32
|
+
message: string,
|
|
33
|
+
options: { status: number; code?: string; digest?: string; cause?: unknown },
|
|
34
|
+
) {
|
|
35
|
+
super(message, { cause: options.cause });
|
|
36
|
+
this.name = 'FaucetError';
|
|
37
|
+
this.status = options.status;
|
|
38
|
+
this.code = options.code;
|
|
39
|
+
this.digest = options.digest;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const FaucetResponse = object({
|
|
44
|
+
status: literal('success'),
|
|
45
|
+
digest: FaucetDigest,
|
|
46
|
+
recipient: FaucetAddress,
|
|
47
|
+
amountMist: FaucetAmount,
|
|
48
|
+
difficulty: FaucetDifficulty,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Request SUI from a PoW faucet on devnet, testnet, or localnet.
|
|
53
|
+
* Fetches and solves a challenge, then submits one payout request without retrying it.
|
|
54
|
+
*/
|
|
55
|
+
export async function requestSuiFromFaucetV3(input: {
|
|
56
|
+
host: string;
|
|
57
|
+
recipient: string;
|
|
58
|
+
headers?: HeadersInit;
|
|
59
|
+
signal?: AbortSignal;
|
|
60
|
+
/** Maximum time for fetching, solving, and submitting, in milliseconds. Defaults to 180000. */
|
|
61
|
+
timeout?: number;
|
|
62
|
+
}): Promise<FaucetResponseV3> {
|
|
63
|
+
if (!input.recipient.replace(/^0x/i, '')) throw new Error('Invalid faucet recipient');
|
|
64
|
+
const recipient = normalizeSuiAddress(input.recipient);
|
|
65
|
+
if (!isValidSuiAddress(recipient)) throw new Error('Invalid faucet recipient');
|
|
66
|
+
const timeout = AbortSignal.timeout(input.timeout ?? 180_000);
|
|
67
|
+
const signal = input.signal ? AbortSignal.any([input.signal, timeout]) : timeout;
|
|
68
|
+
const headers = new Headers(input.headers);
|
|
69
|
+
headers.set('Content-Type', 'application/json');
|
|
70
|
+
|
|
71
|
+
async function request(path: string, body?: object): Promise<unknown> {
|
|
72
|
+
signal.throwIfAborted();
|
|
73
|
+
const response = await fetch(new URL(path, input.host).toString(), {
|
|
74
|
+
method: body ? 'POST' : 'GET',
|
|
75
|
+
headers,
|
|
76
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
77
|
+
signal,
|
|
78
|
+
});
|
|
79
|
+
if (response.status === 429) {
|
|
80
|
+
throw new FaucetRateLimitError('Too many requests to the faucet. Please retry later.');
|
|
81
|
+
}
|
|
82
|
+
let parseError: unknown;
|
|
83
|
+
const result = await response.json().catch((cause: unknown) => {
|
|
84
|
+
signal.throwIfAborted();
|
|
85
|
+
if (response.ok) {
|
|
86
|
+
throw new Error(`Invalid faucet response (HTTP ${response.status})`, { cause });
|
|
87
|
+
}
|
|
88
|
+
parseError = cause;
|
|
89
|
+
});
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
throw new FaucetError(
|
|
92
|
+
typeof result?.error === 'string'
|
|
93
|
+
? result.error
|
|
94
|
+
: `Faucet request failed (HTTP ${response.status})`,
|
|
95
|
+
{
|
|
96
|
+
status: response.status,
|
|
97
|
+
cause: parseError,
|
|
98
|
+
code: typeof result?.code === 'string' ? result.code : undefined,
|
|
99
|
+
digest: typeof result?.digest === 'string' ? result.digest : undefined,
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
while (true) {
|
|
107
|
+
const startedAt = Date.now();
|
|
108
|
+
const challenge = parse(
|
|
109
|
+
FaucetChallenge,
|
|
110
|
+
await request(`/v3/challenge?${new URLSearchParams({ recipient })}`),
|
|
111
|
+
{ abortPipeEarly: true },
|
|
112
|
+
);
|
|
113
|
+
if (challenge.recipient !== recipient) throw new Error('Faucet challenge recipient mismatch');
|
|
114
|
+
// Reserve part of the window for submission; refresh only before a payout has been attempted.
|
|
115
|
+
const proof = await solveFaucetChallenge(
|
|
116
|
+
challenge,
|
|
117
|
+
startedAt + challenge.windowSeconds * 800,
|
|
118
|
+
signal,
|
|
119
|
+
);
|
|
120
|
+
if (!proof) continue;
|
|
121
|
+
const result = parse(
|
|
122
|
+
FaucetResponse,
|
|
123
|
+
await request('/v3/gas', {
|
|
124
|
+
recipient,
|
|
125
|
+
checkpointSeq: challenge.checkpointSeq,
|
|
126
|
+
...proof,
|
|
127
|
+
}),
|
|
128
|
+
{ abortPipeEarly: true },
|
|
129
|
+
);
|
|
130
|
+
if (result.recipient !== recipient) throw new Error('Faucet response recipient mismatch');
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
}
|
package/src/faucet/index.ts
CHANGED