@invisible-labs/sdk 0.6.0-devnet.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/README.md +210 -0
- package/dist/chunk-7DSVN3MV.js +1145 -0
- package/dist/chunk-7DSVN3MV.js.map +1 -0
- package/dist/chunk-JNNVPZEU.js +599 -0
- package/dist/chunk-JNNVPZEU.js.map +1 -0
- package/dist/chunk-JRMKCE3S.js +50 -0
- package/dist/chunk-JRMKCE3S.js.map +1 -0
- package/dist/chunk-LBC7ALYO.js +3783 -0
- package/dist/chunk-LBC7ALYO.js.map +1 -0
- package/dist/chunk-NCY4FCYU.js +1911 -0
- package/dist/chunk-NCY4FCYU.js.map +1 -0
- package/dist/chunk-OHIM2YWU.js +126 -0
- package/dist/chunk-OHIM2YWU.js.map +1 -0
- package/dist/chunk-SZAYO2L5.js +123 -0
- package/dist/chunk-SZAYO2L5.js.map +1 -0
- package/dist/chunk-TQNNTV5F.js +17 -0
- package/dist/chunk-TQNNTV5F.js.map +1 -0
- package/dist/chunk-VR6T6OJS.js +24 -0
- package/dist/chunk-VR6T6OJS.js.map +1 -0
- package/dist/chunk-XUWBGET5.js +221 -0
- package/dist/chunk-XUWBGET5.js.map +1 -0
- package/dist/coordinator-7Y45MCCZ.js +4 -0
- package/dist/coordinator-7Y45MCCZ.js.map +1 -0
- package/dist/createSession-D3ym-Ira.d.ts +177 -0
- package/dist/dkgWorker.d.ts +2 -0
- package/dist/dkgWorker.js +61 -0
- package/dist/dkgWorker.js.map +1 -0
- package/dist/events.d.ts +54 -0
- package/dist/events.js +143 -0
- package/dist/events.js.map +1 -0
- package/dist/frostRuntime-JESDHN6O.js +4 -0
- package/dist/frostRuntime-JESDHN6O.js.map +1 -0
- package/dist/index.d.ts +499 -0
- package/dist/index.js +132 -0
- package/dist/index.js.map +1 -0
- package/dist/lp.d.ts +388 -0
- package/dist/lp.js +1711 -0
- package/dist/lp.js.map +1 -0
- package/dist/presets.d.ts +43 -0
- package/dist/presets.js +70 -0
- package/dist/presets.js.map +1 -0
- package/dist/session-DFuy54C-.d.ts +31 -0
- package/dist/stats.d.ts +47 -0
- package/dist/stats.js +21 -0
- package/dist/stats.js.map +1 -0
- package/dist/storage.d.ts +82 -0
- package/dist/storage.js +246 -0
- package/dist/storage.js.map +1 -0
- package/dist/types-ZhV7TIQY.d.ts +76 -0
- package/dist/types.generated-CHGSbmLp.d.ts +67 -0
- package/dist/user.d.ts +377 -0
- package/dist/user.js +1225 -0
- package/dist/user.js.map +1 -0
- package/package.json +82 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { PolicyValidationError, AttestationError } from './chunk-OHIM2YWU.js';
|
|
2
|
+
|
|
3
|
+
// src/core/solanaAddress.ts
|
|
4
|
+
var SOLANA_PUBLIC_KEY_BYTE_LENGTH = 32;
|
|
5
|
+
var SOLANA_ADDRESS_MIN_LENGTH = 32;
|
|
6
|
+
var SOLANA_ADDRESS_MAX_LENGTH = 44;
|
|
7
|
+
var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
8
|
+
var BASE58_RADIX = BigInt(BASE58_ALPHABET.length);
|
|
9
|
+
var BASE58_LEADING_ZERO_CHAR = "1";
|
|
10
|
+
var BYTE_MASK = 0xffn;
|
|
11
|
+
var BYTE_RADIX_BITS = 8n;
|
|
12
|
+
var MAX_SOLANA_PUBLIC_KEY_VALUE = (1n << BigInt(SOLANA_PUBLIC_KEY_BYTE_LENGTH) * 8n) - 1n;
|
|
13
|
+
var BASE58_DIGITS = new Map(
|
|
14
|
+
Array.from(BASE58_ALPHABET, (character, index) => [character, index])
|
|
15
|
+
);
|
|
16
|
+
function assertCanonicalSolanaAddress(value) {
|
|
17
|
+
if (typeof value !== "string") {
|
|
18
|
+
throw new PolicyValidationError(
|
|
19
|
+
"INVALID_DESTINATION_ADDRESS",
|
|
20
|
+
`destination address must be a string, received ${typeof value}`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
if (value.trim() !== value || value.length < SOLANA_ADDRESS_MIN_LENGTH || value.length > SOLANA_ADDRESS_MAX_LENGTH) {
|
|
24
|
+
throwInvalidDestinationAddress(value);
|
|
25
|
+
}
|
|
26
|
+
const decoded = decodeBase58SolanaAddress(value);
|
|
27
|
+
if (encodeBase58(decoded) !== value) {
|
|
28
|
+
throwInvalidDestinationAddress(value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function solanaAddressFromPublicKeyBytes(bytes) {
|
|
32
|
+
if (bytes.byteLength !== SOLANA_PUBLIC_KEY_BYTE_LENGTH) {
|
|
33
|
+
throw new PolicyValidationError(
|
|
34
|
+
"INVALID_DESTINATION_ADDRESS",
|
|
35
|
+
`Solana public key must be ${SOLANA_PUBLIC_KEY_BYTE_LENGTH} bytes, received ${bytes.byteLength}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return encodeBase58(bytes);
|
|
39
|
+
}
|
|
40
|
+
function decodeBase58SolanaAddress(value) {
|
|
41
|
+
let decoded = 0n;
|
|
42
|
+
for (const character of value) {
|
|
43
|
+
const digit = BASE58_DIGITS.get(character);
|
|
44
|
+
if (digit === void 0) {
|
|
45
|
+
throwInvalidDestinationAddress(value);
|
|
46
|
+
}
|
|
47
|
+
decoded = decoded * BASE58_RADIX + BigInt(digit);
|
|
48
|
+
if (decoded > MAX_SOLANA_PUBLIC_KEY_VALUE) {
|
|
49
|
+
throwInvalidDestinationAddress(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const bytes = new Uint8Array(SOLANA_PUBLIC_KEY_BYTE_LENGTH);
|
|
53
|
+
for (let index = SOLANA_PUBLIC_KEY_BYTE_LENGTH - 1; index >= 0; index -= 1) {
|
|
54
|
+
bytes[index] = Number(decoded & BYTE_MASK);
|
|
55
|
+
decoded >>= BYTE_RADIX_BITS;
|
|
56
|
+
}
|
|
57
|
+
return bytes;
|
|
58
|
+
}
|
|
59
|
+
function encodeBase58(bytes) {
|
|
60
|
+
let leadingZeros = 0;
|
|
61
|
+
for (const byte of bytes) {
|
|
62
|
+
if (byte !== 0) break;
|
|
63
|
+
leadingZeros += 1;
|
|
64
|
+
}
|
|
65
|
+
let value = 0n;
|
|
66
|
+
for (const byte of bytes) {
|
|
67
|
+
value = (value << BYTE_RADIX_BITS) + BigInt(byte);
|
|
68
|
+
}
|
|
69
|
+
let encoded = "";
|
|
70
|
+
while (value > 0n) {
|
|
71
|
+
const digit = Number(value % BASE58_RADIX);
|
|
72
|
+
encoded = BASE58_ALPHABET[digit] + encoded;
|
|
73
|
+
value /= BASE58_RADIX;
|
|
74
|
+
}
|
|
75
|
+
return BASE58_LEADING_ZERO_CHAR.repeat(leadingZeros) + encoded;
|
|
76
|
+
}
|
|
77
|
+
function throwInvalidDestinationAddress(value) {
|
|
78
|
+
throw new PolicyValidationError(
|
|
79
|
+
"INVALID_DESTINATION_ADDRESS",
|
|
80
|
+
`destination address must be a canonical 32-byte Solana address: ${value}`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/core/brands.ts
|
|
85
|
+
function lamports(value) {
|
|
86
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
87
|
+
throw new PolicyValidationError(
|
|
88
|
+
"INVALID_AMOUNT",
|
|
89
|
+
`lamports must be a non-negative safe integer, received ${String(value)}`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
function publicKey(value) {
|
|
95
|
+
assertCanonicalSolanaAddress(value);
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
function requestId(value) {
|
|
99
|
+
if (value.length === 0) {
|
|
100
|
+
throw new Error("requestId must be a non-empty string");
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
function hex(value) {
|
|
105
|
+
if (!/^[0-9a-f]*$/.test(value)) {
|
|
106
|
+
throw new Error(`not lowercase hex: ${value}`);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/session/guards.ts
|
|
112
|
+
function assertAttested(session) {
|
|
113
|
+
if (!session.attested) {
|
|
114
|
+
throw new AttestationError(
|
|
115
|
+
"NOT_ATTESTED",
|
|
116
|
+
"session is not attested; sensitive commands are blocked until attestation completes"
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { assertAttested, hex, lamports, publicKey, requestId, solanaAddressFromPublicKeyBytes };
|
|
122
|
+
//# sourceMappingURL=chunk-SZAYO2L5.js.map
|
|
123
|
+
//# sourceMappingURL=chunk-SZAYO2L5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/solanaAddress.ts","../src/core/brands.ts","../src/session/guards.ts"],"names":[],"mappings":";;;AAEA,IAAM,6BAAA,GAAgC,EAAA;AACtC,IAAM,yBAAA,GAA4B,EAAA;AAClC,IAAM,yBAAA,GAA4B,EAAA;AAClC,IAAM,eAAA,GAAkB,4DAAA;AACxB,IAAM,YAAA,GAAe,MAAA,CAAO,eAAA,CAAgB,MAAM,CAAA;AAClD,IAAM,wBAAA,GAA2B,GAAA;AACjC,IAAM,SAAA,GAAY,KAAA;AAClB,IAAM,eAAA,GAAkB,EAAA;AACxB,IAAM,2BAAA,GAAA,CAA+B,EAAA,IAAO,MAAA,CAAO,6BAA6B,IAAI,EAAA,IAAO,EAAA;AAE3F,IAAM,gBAAgB,IAAI,GAAA;AAAA,EACxB,KAAA,CAAM,KAAK,eAAA,EAAiB,CAAC,WAAW,KAAA,KAAU,CAAC,SAAA,EAAW,KAAK,CAAC;AACtE,CAAA;AAEO,SAAS,6BAA6B,KAAA,EAAyC;AACpF,EAAA,IAAI,OAAO,UAAU,QAAA,EAAU;AAC7B,IAAA,MAAM,IAAI,qBAAA;AAAA,MACR,6BAAA;AAAA,MACA,CAAA,+CAAA,EAAkD,OA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E;AAAA,EACF;AACA,EAAA,OAAO,KAAA;AACT;AASO,SAAS,UAAU,KAAA,EAA0B;AAClD,EAAA,4BAAA,CAA6B,KAAK,CAAA;AAClC,EAAA,OAAO,KAAA;AACT;AAQO,SAAS,UAAU,KAAA,EAA0B;AAClD,EAAA,IAAI,KAAA,CAAM,WAAW,CAAA,EAAG;AACtB,IAAA,MAAM,IAAI,MAAM,sCAAsC,CAAA;AAAA,EACxD;AACA,EAAA,OAAO,KAAA;AACT;AAOO,SAAS,IAAI,KAAA,EAAoB;AACtC,EAAA,IAAI,CAAC,aAAA,CAAc,IAAA,CAAK,KAAK,CAAA,EAAG;AAC9B,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,mBAAA,EAAsB,KAAK,CAAA,CAAE,CAAA;AAAA,EAC/C;AACA,EAAA,OAAO,KAAA;AACT;;;ACpEO,SAAS,eAAe,OAAA,EAAwB;AACrD,EAAA,IAAI,CAAC,QAAQ,QAAA,EAAU;AACrB,IAAA,MAAM,IAAI,gBAAA;AAAA,MACR,cAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AACF","file":"chunk-SZAYO2L5.js","sourcesContent":["import { PolicyValidationError } from \"./errors.js\";\n\nconst SOLANA_PUBLIC_KEY_BYTE_LENGTH = 32;\nconst SOLANA_ADDRESS_MIN_LENGTH = 32;\nconst SOLANA_ADDRESS_MAX_LENGTH = 44;\nconst BASE58_ALPHABET = \"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz\";\nconst BASE58_RADIX = BigInt(BASE58_ALPHABET.length);\nconst BASE58_LEADING_ZERO_CHAR = \"1\";\nconst BYTE_MASK = 0xffn;\nconst BYTE_RADIX_BITS = 8n;\nconst MAX_SOLANA_PUBLIC_KEY_VALUE = (1n << (BigInt(SOLANA_PUBLIC_KEY_BYTE_LENGTH) * 8n)) - 1n;\n\nconst BASE58_DIGITS = new Map<string, number>(\n Array.from(BASE58_ALPHABET, (character, index) => [character, index]),\n);\n\nexport function assertCanonicalSolanaAddress(value: unknown): asserts value is string {\n if (typeof value !== \"string\") {\n throw new PolicyValidationError(\n \"INVALID_DESTINATION_ADDRESS\",\n `destination address must be a string, received ${typeof value}`,\n );\n }\n if (\n value.trim() !== value ||\n value.length < SOLANA_ADDRESS_MIN_LENGTH ||\n value.length > SOLANA_ADDRESS_MAX_LENGTH\n ) {\n throwInvalidDestinationAddress(value);\n }\n\n const decoded = decodeBase58SolanaAddress(value);\n if (encodeBase58(decoded) !== value) {\n throwInvalidDestinationAddress(value);\n }\n}\n\n/** Encode a 32-byte Ed25519 public key as its canonical Solana address. */\nexport function solanaAddressFromPublicKeyBytes(bytes: Uint8Array): string {\n if (bytes.byteLength !== SOLANA_PUBLIC_KEY_BYTE_LENGTH) {\n throw new PolicyValidationError(\n \"INVALID_DESTINATION_ADDRESS\",\n `Solana public key must be ${SOLANA_PUBLIC_KEY_BYTE_LENGTH} bytes, received ${bytes.byteLength}`,\n );\n }\n return encodeBase58(bytes);\n}\n\nfunction decodeBase58SolanaAddress(value: string): Uint8Array {\n let decoded = 0n;\n for (const character of value) {\n const digit = BASE58_DIGITS.get(character);\n if (digit === undefined) {\n throwInvalidDestinationAddress(value);\n }\n decoded = decoded * BASE58_RADIX + BigInt(digit);\n if (decoded > MAX_SOLANA_PUBLIC_KEY_VALUE) {\n throwInvalidDestinationAddress(value);\n }\n }\n\n const bytes = new Uint8Array(SOLANA_PUBLIC_KEY_BYTE_LENGTH);\n for (let index = SOLANA_PUBLIC_KEY_BYTE_LENGTH - 1; index >= 0; index -= 1) {\n bytes[index] = Number(decoded & BYTE_MASK);\n decoded >>= BYTE_RADIX_BITS;\n }\n return bytes;\n}\n\nfunction encodeBase58(bytes: Uint8Array): string {\n let leadingZeros = 0;\n for (const byte of bytes) {\n if (byte !== 0) break;\n leadingZeros += 1;\n }\n\n let value = 0n;\n for (const byte of bytes) {\n value = (value << BYTE_RADIX_BITS) + BigInt(byte);\n }\n\n let encoded = \"\";\n while (value > 0n) {\n const digit = Number(value % BASE58_RADIX);\n encoded = BASE58_ALPHABET[digit]! + encoded;\n value /= BASE58_RADIX;\n }\n\n return BASE58_LEADING_ZERO_CHAR.repeat(leadingZeros) + encoded;\n}\n\nfunction throwInvalidDestinationAddress(value: string): never {\n throw new PolicyValidationError(\n \"INVALID_DESTINATION_ADDRESS\",\n `destination address must be a canonical 32-byte Solana address: ${value}`,\n );\n}\n","/**\n * Branded scalar types.\n *\n * Branding makes structurally-identical primitives (a lamport amount, a base58\n * address, a correlation id) non-interchangeable at the type level, so the\n * compiler catches \"passed a request id where an address was expected\" before\n * it reaches the wire. The brand exists only at compile time; at runtime these\n * are plain `number` / `string`.\n *\n * Construct branded values through the smart constructors here. These\n * constructors enforce scalar-level invariants; payout-policy semantics stay in\n * the validation module.\n */\n\nimport { PolicyValidationError } from \"./errors.js\";\nimport { assertCanonicalSolanaAddress } from \"./solanaAddress.js\";\n\n/** Attach a compile-time-only brand `B` to base type `T`. */\nexport type Brand<T, B extends string> = T & { readonly __brand: B };\n\n/** A non-negative integer amount of lamports (1 SOL = 1_000_000_000 lamports). */\nexport type Lamports = Brand<number, \"Lamports\">;\n\n/** A Solana account address as base58 text. */\nexport type PublicKey = Brand<string, \"PublicKey\">;\n\n/** A client-generated correlation id for one logical command. */\nexport type RequestId = Brand<string, \"RequestId\">;\n\n/** Lowercase hexadecimal text (no `0x` prefix). */\nexport type Hex = Brand<string, \"Hex\">;\n\n/**\n * Brand a number as {@link Lamports}.\n *\n * @throws PolicyValidationError `INVALID_AMOUNT` if not a non-negative safe integer.\n */\nexport function lamports(value: number): Lamports {\n if (!Number.isSafeInteger(value) || value < 0) {\n throw new PolicyValidationError(\n \"INVALID_AMOUNT\",\n `lamports must be a non-negative safe integer, received ${String(value)}`,\n );\n }\n return value as Lamports;\n}\n\n/**\n * Brand a string as a {@link PublicKey}.\n *\n * Enforces canonical base58 encoding of a 32-byte Solana public key.\n *\n * @throws PolicyValidationError `INVALID_DESTINATION_ADDRESS` if obviously malformed.\n */\nexport function publicKey(value: string): PublicKey {\n assertCanonicalSolanaAddress(value);\n return value as PublicKey;\n}\n\n/**\n * Brand a non-empty string as a {@link RequestId}.\n *\n * @throws PolicyValidationError `INVALID_AMOUNT` is not appropriate here; an empty\n * id is a programming error, so this throws a plain `Error`.\n */\nexport function requestId(value: string): RequestId {\n if (value.length === 0) {\n throw new Error(\"requestId must be a non-empty string\");\n }\n return value as RequestId;\n}\n\n/**\n * Brand a string as lowercase {@link Hex}.\n *\n * @throws Error if the string contains non-hex characters.\n */\nexport function hex(value: string): Hex {\n if (!/^[0-9a-f]*$/.test(value)) {\n throw new Error(`not lowercase hex: ${value}`);\n }\n return value as Hex;\n}\n","/**\n * Session guards used by mutating commands.\n */\n\nimport { AttestationError } from \"../core/errors.js\";\nimport type { Session } from \"../core/session.js\";\n\n/**\n * Assert the session has passed attestation. Every mutating actor command calls\n * this before touching the wire, so an unattested session can never sign or\n * send sensitive material.\n *\n * @throws AttestationError `NOT_ATTESTED` when the session is not attested.\n */\nexport function assertAttested(session: Session): void {\n if (!session.attested) {\n throw new AttestationError(\n \"NOT_ATTESTED\",\n \"session is not attested; sensitive commands are blocked until attestation completes\",\n );\n }\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// ../shared/src/index.ts
|
|
2
|
+
var LP_POSITION_ID_MIN_LENGTH = 3;
|
|
3
|
+
var LP_POSITION_ID_MAX_LENGTH = 96;
|
|
4
|
+
var LP_POSITION_ID_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
5
|
+
var LP_DEFAULT_TARGET_SHARDS = 200;
|
|
6
|
+
var LP_MAX_TARGET_SHARDS = 300;
|
|
7
|
+
var LP_DEFAULT_REFILL_BATCH_SIZE = 50;
|
|
8
|
+
var LP_MAX_REFILL_BATCH_SIZE = 50;
|
|
9
|
+
var PRODUCT_MIN_ENTRY_LAMPORTS = 4e8;
|
|
10
|
+
var PRODUCT_MAX_ENTRY_LAMPORTS = 1e11;
|
|
11
|
+
function isValidLpPositionId(lpPositionId) {
|
|
12
|
+
return lpPositionId.length >= LP_POSITION_ID_MIN_LENGTH && lpPositionId.length <= LP_POSITION_ID_MAX_LENGTH && LP_POSITION_ID_PATTERN.test(lpPositionId);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { LP_DEFAULT_REFILL_BATCH_SIZE, LP_DEFAULT_TARGET_SHARDS, LP_MAX_REFILL_BATCH_SIZE, LP_MAX_TARGET_SHARDS, PRODUCT_MAX_ENTRY_LAMPORTS, PRODUCT_MIN_ENTRY_LAMPORTS, isValidLpPositionId };
|
|
16
|
+
//# sourceMappingURL=chunk-TQNNTV5F.js.map
|
|
17
|
+
//# sourceMappingURL=chunk-TQNNTV5F.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../shared/src/index.ts"],"names":[],"mappings":";AAgBO,IAAM,yBAAA,GAA4B,CAAA;AAClC,IAAM,yBAAA,GAA4B,EAAA;AAClC,IAAM,sBAAA,GAAyB,kBAAA;AAC/B,IAAM,wBAAA,GAA2B;AACjC,IAAM,oBAAA,GAAuB;AAC7B,IAAM,4BAAA,GAA+B;AACrC,IAAM,wBAAA,GAA2B;AAKjC,IAAM,0BAAA,GAA6B;AACnC,IAAM,0BAAA,GAA6B;AAGnC,SAAS,oBAAoB,YAAA,EAA+B;AACjE,EAAA,OACE,YAAA,CAAa,UAAU,yBAAA,IACvB,YAAA,CAAa,UAAU,yBAAA,IACvB,sBAAA,CAAuB,KAAK,YAAY,CAAA;AAE5C","file":"chunk-TQNNTV5F.js","sourcesContent":["/**\n * Shared workspace entrypoint.\n *\n * Keep exports here pure and reusable across workspaces. Do not put React,\n * DOM-only, libp2p runtime state, or feature-specific glue in this package.\n */\nexport function sleep(ms: number): Promise<void> {\n if (ms <= 0) {\n return Promise.resolve();\n }\n\n return new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n\nexport const LP_POSITION_ID_MIN_LENGTH = 3;\nexport const LP_POSITION_ID_MAX_LENGTH = 96;\nexport const LP_POSITION_ID_PATTERN = /^[A-Za-z0-9_-]+$/;\nexport const LP_DEFAULT_TARGET_SHARDS = 200;\nexport const LP_MAX_TARGET_SHARDS = 300;\nexport const LP_DEFAULT_REFILL_BATCH_SIZE = 50;\nexport const LP_MAX_REFILL_BATCH_SIZE = 50;\n\n// Temporary TypeScript mirrors of the Rust TEE entry limits, plus the distinct\n// mainnet UI default. Keep them guarded by `npm run product-limits:check` until\n// cross-language generated product constants exist.\nexport const PRODUCT_MIN_ENTRY_LAMPORTS = 400_000_000;\nexport const PRODUCT_MAX_ENTRY_LAMPORTS = 100_000_000_000;\nexport const PRODUCT_DEFAULT_MAINNET_ENTRY_LAMPORTS = 3_000_000_000;\n\nexport function isValidLpPositionId(lpPositionId: string): boolean {\n return (\n lpPositionId.length >= LP_POSITION_ID_MIN_LENGTH &&\n lpPositionId.length <= LP_POSITION_ID_MAX_LENGTH &&\n LP_POSITION_ID_PATTERN.test(lpPositionId)\n );\n}\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
var __toBinary = Uint8Array.fromBase64 || /* @__PURE__ */ (() => {
|
|
7
|
+
var table = new Uint8Array(128);
|
|
8
|
+
for (var i = 0; i < 64; i++) table[i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i * 4 - 205] = i;
|
|
9
|
+
return (base64) => {
|
|
10
|
+
var n = base64.length, bytes = new Uint8Array((n - (base64[n - 1] == "=") - (base64[n - 2] == "=")) * 3 / 4 | 0);
|
|
11
|
+
for (var i2 = 0, j = 0; i2 < n; ) {
|
|
12
|
+
var c0 = table[base64.charCodeAt(i2++)], c1 = table[base64.charCodeAt(i2++)];
|
|
13
|
+
var c2 = table[base64.charCodeAt(i2++)], c3 = table[base64.charCodeAt(i2++)];
|
|
14
|
+
bytes[j++] = c0 << 2 | c1 >> 4;
|
|
15
|
+
bytes[j++] = c1 << 4 | c2 >> 2;
|
|
16
|
+
bytes[j++] = c2 << 6 | c3;
|
|
17
|
+
}
|
|
18
|
+
return bytes;
|
|
19
|
+
};
|
|
20
|
+
})();
|
|
21
|
+
|
|
22
|
+
export { __export, __toBinary };
|
|
23
|
+
//# sourceMappingURL=chunk-VR6T6OJS.js.map
|
|
24
|
+
//# sourceMappingURL=chunk-VR6T6OJS.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"chunk-VR6T6OJS.js"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { getSessionState } from './chunk-JRMKCE3S.js';
|
|
2
|
+
import { TransportError, CommandError } from './chunk-OHIM2YWU.js';
|
|
3
|
+
|
|
4
|
+
// src/coordinator/protocolClient.ts
|
|
5
|
+
var EnvelopeKind = {
|
|
6
|
+
REQUEST: "request",
|
|
7
|
+
RESPONSE: "response",
|
|
8
|
+
PUSH: "push",
|
|
9
|
+
ERROR: "error"
|
|
10
|
+
};
|
|
11
|
+
var CoordinatorMessageType = {
|
|
12
|
+
ContractRequest: "ContractRequest",
|
|
13
|
+
DkgInit: "DkgInit",
|
|
14
|
+
DkgRound1: "DkgRound1",
|
|
15
|
+
DkgRound2: "DkgRound2",
|
|
16
|
+
DkgComplete: "DkgComplete",
|
|
17
|
+
DelegateShare: "DelegateShare",
|
|
18
|
+
DelegateAck: "DelegateAck",
|
|
19
|
+
DepositConfirmed: "DepositConfirmed",
|
|
20
|
+
Sync: "Sync",
|
|
21
|
+
SyncRequired: "SyncRequired",
|
|
22
|
+
Refill: "Refill",
|
|
23
|
+
PayoutExecuted: "PayoutExecuted",
|
|
24
|
+
WithdrawRequest: "WithdrawRequest",
|
|
25
|
+
WithdrawAccepted: "WithdrawAccepted",
|
|
26
|
+
WithdrawExecuted: "WithdrawExecuted",
|
|
27
|
+
LpCommand: "LpCommand",
|
|
28
|
+
Error: "Error"
|
|
29
|
+
};
|
|
30
|
+
var protocolPromise = null;
|
|
31
|
+
function loadProtocol() {
|
|
32
|
+
protocolPromise ??= import('./coordinator-7Y45MCCZ.js');
|
|
33
|
+
return protocolPromise;
|
|
34
|
+
}
|
|
35
|
+
var runtimes = /* @__PURE__ */ new WeakMap();
|
|
36
|
+
async function requestEnvelope(session, type, payload, options = {}) {
|
|
37
|
+
const protocol = await loadProtocol();
|
|
38
|
+
const state = getSessionState(session);
|
|
39
|
+
const runtime = ensureProtocolRuntime(state);
|
|
40
|
+
const channel = state.channel;
|
|
41
|
+
if (!state.attested || channel === null) {
|
|
42
|
+
throw new TransportError("CONNECTION_LOST", "coordinator request requires an attested channel");
|
|
43
|
+
}
|
|
44
|
+
const reqId = ++runtime.reqCounter;
|
|
45
|
+
const envelope = {
|
|
46
|
+
version: protocol.ENVELOPE_VERSION,
|
|
47
|
+
kind: protocol.EnvelopeKind.REQUEST,
|
|
48
|
+
type,
|
|
49
|
+
swap_id: options.swapId ?? null,
|
|
50
|
+
req_id: reqId,
|
|
51
|
+
timestamp_ms: Date.now(),
|
|
52
|
+
payload
|
|
53
|
+
};
|
|
54
|
+
const frame = protocol.encodeValidated(envelope);
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const timer = options.timeoutMs === void 0 ? null : setTimeout(() => {
|
|
57
|
+
runtime.pending.delete(reqId);
|
|
58
|
+
reject(
|
|
59
|
+
new CommandError(
|
|
60
|
+
"REJECTED_STALE_JOB",
|
|
61
|
+
`coordinator request timed out for ${type} after ${options.timeoutMs}ms`
|
|
62
|
+
)
|
|
63
|
+
);
|
|
64
|
+
}, options.timeoutMs);
|
|
65
|
+
runtime.pending.set(reqId, { resolve, reject, timer });
|
|
66
|
+
options.onLog?.(`-> ${type} req_id=${reqId}`, "out");
|
|
67
|
+
try {
|
|
68
|
+
channel.send(frame);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
runtime.pending.delete(reqId);
|
|
71
|
+
if (timer) clearTimeout(timer);
|
|
72
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function onEnvelope(session, handler) {
|
|
77
|
+
const runtime = ensureProtocolRuntime(getSessionState(session));
|
|
78
|
+
runtime.envelopeHandlers.add(handler);
|
|
79
|
+
return () => runtime.envelopeHandlers.delete(handler);
|
|
80
|
+
}
|
|
81
|
+
function closeProtocolRuntime(session) {
|
|
82
|
+
const state = getSessionState(session);
|
|
83
|
+
const runtime = runtimes.get(state);
|
|
84
|
+
if (!runtime) return;
|
|
85
|
+
const error = new TransportError("CONNECTION_LOST", "coordinator protocol runtime closed");
|
|
86
|
+
rejectAll(runtime, error);
|
|
87
|
+
runtime.unsubscribeChannel?.();
|
|
88
|
+
runtime.unsubscribeTransport?.();
|
|
89
|
+
runtimes.delete(state);
|
|
90
|
+
}
|
|
91
|
+
function ensureProtocolRuntime(state) {
|
|
92
|
+
const existing = runtimes.get(state);
|
|
93
|
+
if (existing) return existing;
|
|
94
|
+
const runtime = {
|
|
95
|
+
reqCounter: 0,
|
|
96
|
+
pending: /* @__PURE__ */ new Map(),
|
|
97
|
+
pushWaiters: /* @__PURE__ */ new Map(),
|
|
98
|
+
envelopeHandlers: /* @__PURE__ */ new Set(),
|
|
99
|
+
unsubscribeChannel: null,
|
|
100
|
+
unsubscribeTransport: null
|
|
101
|
+
};
|
|
102
|
+
if (state.channel === null) {
|
|
103
|
+
throw new TransportError("CONNECTION_LOST", "coordinator protocol requires a live channel");
|
|
104
|
+
}
|
|
105
|
+
runtime.unsubscribeChannel = state.channel.onMessage((frame) => handleFrame(runtime, frame));
|
|
106
|
+
runtime.unsubscribeTransport = state.transport.onStateChange((transportState) => {
|
|
107
|
+
if (transportState === "closing" || transportState === "closed") {
|
|
108
|
+
rejectAll(runtime, new TransportError("CONNECTION_LOST", "coordinator transport closed"));
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
runtimes.set(state, runtime);
|
|
112
|
+
return runtime;
|
|
113
|
+
}
|
|
114
|
+
function handleFrame(runtime, frame) {
|
|
115
|
+
void handleFrameAsync(runtime, frame);
|
|
116
|
+
}
|
|
117
|
+
async function handleFrameAsync(runtime, frame) {
|
|
118
|
+
const protocol = await loadProtocol();
|
|
119
|
+
const decoded = protocol.decode(frame);
|
|
120
|
+
if (!decoded.ok) {
|
|
121
|
+
rejectAll(runtime, decoded.error);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const envelope = decoded.value;
|
|
125
|
+
for (const handler of runtime.envelopeHandlers) handler(envelope);
|
|
126
|
+
if (envelope.kind === protocol.EnvelopeKind.ERROR) {
|
|
127
|
+
const payload = protocol.asErrorPayload(envelope);
|
|
128
|
+
const error = commandErrorFromCoordinatorError(payload);
|
|
129
|
+
if (payload.failed_req_id === void 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
rejectPending(runtime, payload.failed_req_id, error);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (envelope.kind === protocol.EnvelopeKind.PUSH) {
|
|
136
|
+
resolvePushWaiter(runtime, envelope);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (envelope.kind !== protocol.EnvelopeKind.RESPONSE) return;
|
|
140
|
+
const pending = runtime.pending.get(envelope.req_id);
|
|
141
|
+
if (!pending) return;
|
|
142
|
+
runtime.pending.delete(envelope.req_id);
|
|
143
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
144
|
+
pending.resolve(envelope);
|
|
145
|
+
}
|
|
146
|
+
function resolvePushWaiter(runtime, envelope) {
|
|
147
|
+
const waiters = runtime.pushWaiters.get(envelope.type);
|
|
148
|
+
const waiter = waiters?.shift();
|
|
149
|
+
if (!waiter || !waiters) return;
|
|
150
|
+
if (waiters.length === 0) {
|
|
151
|
+
runtime.pushWaiters.delete(envelope.type);
|
|
152
|
+
}
|
|
153
|
+
if (waiter.timer) clearTimeout(waiter.timer);
|
|
154
|
+
waiter.resolve(envelope);
|
|
155
|
+
}
|
|
156
|
+
function rejectPending(runtime, reqId, error) {
|
|
157
|
+
const pending = runtime.pending.get(reqId);
|
|
158
|
+
if (!pending) return;
|
|
159
|
+
runtime.pending.delete(reqId);
|
|
160
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
161
|
+
pending.reject(error);
|
|
162
|
+
}
|
|
163
|
+
function commandErrorFromCoordinatorError(payload) {
|
|
164
|
+
const coordinatorCode = toCoordinatorWireErrorCode(payload.code);
|
|
165
|
+
return new CommandError(commandCodeFromCoordinatorCode(coordinatorCode), payload.message, {
|
|
166
|
+
coordinatorCode,
|
|
167
|
+
...payload.failed_req_id !== void 0 ? { failedReqId: payload.failed_req_id } : {}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function commandCodeFromCoordinatorCode(code) {
|
|
171
|
+
switch (code) {
|
|
172
|
+
case "ERR_UNEXPECTED_TYPE":
|
|
173
|
+
case "ERR_UNKNOWN_SWAP":
|
|
174
|
+
case "ERR_SWAP_NOT_REFUNDABLE":
|
|
175
|
+
return "REJECTED_STATE";
|
|
176
|
+
case "ERR_INTERNAL":
|
|
177
|
+
case "ERR_SETTLEMENT_FAILED":
|
|
178
|
+
return "REJECTED_STALE_JOB";
|
|
179
|
+
case "ERR_INVALID_PAYLOAD":
|
|
180
|
+
case "ERR_POLICY_VIOLATION":
|
|
181
|
+
case "ERR_INVALID_RECOVERY_CODE":
|
|
182
|
+
case "ERR_DKG_FAILURE":
|
|
183
|
+
case "ERR_INVALID_DEPOSIT":
|
|
184
|
+
return "REJECTED_GUARD";
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function toCoordinatorWireErrorCode(code) {
|
|
188
|
+
switch (code) {
|
|
189
|
+
case "ERR_INVALID_PAYLOAD":
|
|
190
|
+
case "ERR_UNEXPECTED_TYPE":
|
|
191
|
+
case "ERR_UNKNOWN_SWAP":
|
|
192
|
+
case "ERR_POLICY_VIOLATION":
|
|
193
|
+
case "ERR_INVALID_RECOVERY_CODE":
|
|
194
|
+
case "ERR_SWAP_NOT_REFUNDABLE":
|
|
195
|
+
case "ERR_DKG_FAILURE":
|
|
196
|
+
case "ERR_INTERNAL":
|
|
197
|
+
case "ERR_INVALID_DEPOSIT":
|
|
198
|
+
case "ERR_SETTLEMENT_FAILED":
|
|
199
|
+
return code;
|
|
200
|
+
default:
|
|
201
|
+
return "ERR_INTERNAL";
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function rejectAll(runtime, error) {
|
|
205
|
+
for (const [reqId, pending] of runtime.pending.entries()) {
|
|
206
|
+
runtime.pending.delete(reqId);
|
|
207
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
208
|
+
pending.reject(error);
|
|
209
|
+
}
|
|
210
|
+
for (const [type, waiters] of runtime.pushWaiters.entries()) {
|
|
211
|
+
runtime.pushWaiters.delete(type);
|
|
212
|
+
for (const waiter of waiters) {
|
|
213
|
+
if (waiter.timer) clearTimeout(waiter.timer);
|
|
214
|
+
waiter.reject(error);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export { CoordinatorMessageType, EnvelopeKind, closeProtocolRuntime, onEnvelope, requestEnvelope };
|
|
220
|
+
//# sourceMappingURL=chunk-XUWBGET5.js.map
|
|
221
|
+
//# sourceMappingURL=chunk-XUWBGET5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/coordinator/protocolClient.ts"],"names":[],"mappings":";;;;AAYO,IAAM,YAAA,GAAe;AAAA,EAC1B,OAAA,EAAS,SAAA;AAAA,EACT,QAAA,EAAU,UAAA;AAAA,EACV,IAAA,EAAM,MAAA;AAAA,EACN,KAAA,EAAO;AACT;AAIO,IAAM,sBAAA,GAAyB;AAAA,EACpC,eAAA,EAAiB,iBAAA;AAAA,EACjB,OAAA,EAAS,SA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,IACL,KAAK,cAAA;AAAA,IACL,KAAK,qBAAA;AAAA,IACL,KAAK,uBAAA;AACH,MAAA,OAAO,IAAA;AAAA,IACT;AACE,MAAA,OAAO,cAAA;AAAA;AAEb;AAcA,SAAS,SAAA,CAAU,SAA0B,KAAA,EAAoB;AAC/D,EAAA,KAAA,MAAW,CAAC,KAAA,EAAO,OAAO,KAAK,OAAA,CAAQ,OAAA,CAAQ,SAAQ,EAAG;AACxD,IAAA,OAAA,CAAQ,OAAA,CAAQ,OAAO,KAAK,CAAA;AAC5B,IAAA,IAAI,OAAA,CAAQ,KAAA,EAAO,YAAA,CAAa,OAAA,CAAQ,KAAK,CAAA;AAC7C,IAAA,OAAA,CAAQ,OAAO,KAAK,CAAA;AAAA,EACtB;AACA,EAAA,KAAA,MAAW,CAAC,IAAA,EAAM,OAAO,KAAK,OAAA,CAAQ,WAAA,CAAY,SAAQ,EAAG;AAC3D,IAAA,OAAA,CAAQ,WAAA,CAAY,OAAO,IAAI,CAAA;AAC/B,IAAA,KAAA,MAAW,UAAU,OAAA,EAAS;AAC5B,MAAA,IAAI,MAAA,CAAO,KAAA,EAAO,YAAA,CAAa,MAAA,CAAO,KAAK,CAAA;AAC3C,MAAA,MAAA,CAAO,OAAO,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"chunk-XUWBGET5.js","sourcesContent":["import {\n CommandError,\n TransportError,\n type CommandErrorCode,\n type CoordinatorWireErrorCode,\n} from \"../core/errors.js\";\nimport type { Session } from \"../core/session.js\";\nimport { getSessionState, type SessionState } from \"../session/state.js\";\nimport type { Unsubscribe } from \"../transport/types.js\";\n\nconst COORDINATOR_ENVELOPE_VERSION = 2;\n\nexport const EnvelopeKind = {\n REQUEST: \"request\",\n RESPONSE: \"response\",\n PUSH: \"push\",\n ERROR: \"error\",\n} as const;\n\nexport type EnvelopeKind = (typeof EnvelopeKind)[keyof typeof EnvelopeKind];\n\nexport const CoordinatorMessageType = {\n ContractRequest: \"ContractRequest\",\n DkgInit: \"DkgInit\",\n DkgRound1: \"DkgRound1\",\n DkgRound2: \"DkgRound2\",\n DkgComplete: \"DkgComplete\",\n DelegateShare: \"DelegateShare\",\n DelegateAck: \"DelegateAck\",\n DepositConfirmed: \"DepositConfirmed\",\n Sync: \"Sync\",\n SyncRequired: \"SyncRequired\",\n Refill: \"Refill\",\n PayoutExecuted: \"PayoutExecuted\",\n WithdrawRequest: \"WithdrawRequest\",\n WithdrawAccepted: \"WithdrawAccepted\",\n WithdrawExecuted: \"WithdrawExecuted\",\n LpCommand: \"LpCommand\",\n Error: \"Error\",\n} as const;\n\nexport type CoordinatorMessageType =\n (typeof CoordinatorMessageType)[keyof typeof CoordinatorMessageType];\n\nexport type Envelope = {\n readonly version: typeof COORDINATOR_ENVELOPE_VERSION;\n readonly kind: EnvelopeKind;\n readonly type: CoordinatorMessageType;\n readonly swap_id: string | null;\n readonly req_id: number;\n readonly timestamp_ms: number;\n readonly payload: unknown;\n};\n\ntype ProtocolModule = {\n readonly ENVELOPE_VERSION: typeof COORDINATOR_ENVELOPE_VERSION;\n readonly EnvelopeKind: typeof EnvelopeKind;\n readonly CoordinatorMessageType: typeof CoordinatorMessageType;\n readonly encodeValidated: (envelope: Envelope) => Uint8Array;\n readonly decode: (raw: Uint8Array) => { ok: true; value: Envelope } | { ok: false; error: Error };\n readonly asErrorPayload: (envelope: Envelope) => {\n readonly code: string;\n readonly failed_req_id?: number;\n readonly message: string;\n };\n};\n\nlet protocolPromise: Promise<ProtocolModule> | null = null;\n\nfunction loadProtocol(): Promise<ProtocolModule> {\n protocolPromise ??= import(\"protocol/coordinator\") as Promise<ProtocolModule>;\n return protocolPromise;\n}\n\nexport type ProtocolLogDirection = \"in\" | \"out\" | \"info\";\nexport type ProtocolLogHandler = (message: string, direction: ProtocolLogDirection) => void;\nexport type ProtocolEnvelopeHandler = (envelope: Envelope) => void;\n\nexport interface RequestEnvelopeOptions {\n readonly swapId?: string | null;\n readonly timeoutMs?: number;\n readonly onLog?: ProtocolLogHandler;\n}\n\nexport interface AwaitPushOptions {\n readonly timeoutMs?: number | null;\n}\n\ntype PendingRequest = {\n readonly resolve: (envelope: Envelope) => void;\n readonly reject: (error: Error) => void;\n readonly timer: ReturnType<typeof setTimeout> | null;\n};\n\ntype PushWaiter = {\n readonly resolve: (envelope: Envelope) => void;\n readonly reject: (error: Error) => void;\n readonly timer: ReturnType<typeof setTimeout> | null;\n};\n\ntype ProtocolRuntime = {\n reqCounter: number;\n readonly pending: Map<number, PendingRequest>;\n readonly pushWaiters: Map<CoordinatorMessageType, PushWaiter[]>;\n readonly envelopeHandlers: Set<ProtocolEnvelopeHandler>;\n unsubscribeChannel: Unsubscribe | null;\n unsubscribeTransport: Unsubscribe | null;\n};\n\nconst runtimes = new WeakMap<SessionState, ProtocolRuntime>();\n\nexport async function requestEnvelope(\n session: Session,\n type: CoordinatorMessageType,\n payload: unknown,\n options: RequestEnvelopeOptions = {},\n): Promise<Envelope> {\n const protocol = await loadProtocol();\n const state = getSessionState(session);\n const runtime = ensureProtocolRuntime(state);\n const channel = state.channel;\n if (!state.attested || channel === null) {\n throw new TransportError(\"CONNECTION_LOST\", \"coordinator request requires an attested channel\");\n }\n\n const reqId = ++runtime.reqCounter;\n const envelope: Envelope = {\n version: protocol.ENVELOPE_VERSION,\n kind: protocol.EnvelopeKind.REQUEST,\n type,\n swap_id: options.swapId ?? null,\n req_id: reqId,\n timestamp_ms: Date.now(),\n payload,\n };\n const frame = protocol.encodeValidated(envelope);\n\n return new Promise((resolve, reject) => {\n const timer =\n options.timeoutMs === undefined\n ? null\n : setTimeout(() => {\n runtime.pending.delete(reqId);\n reject(\n new CommandError(\n \"REJECTED_STALE_JOB\",\n `coordinator request timed out for ${type} after ${options.timeoutMs}ms`,\n ),\n );\n }, options.timeoutMs);\n\n runtime.pending.set(reqId, { resolve, reject, timer });\n options.onLog?.(`-> ${type} req_id=${reqId}`, \"out\");\n try {\n channel.send(frame);\n } catch (error) {\n runtime.pending.delete(reqId);\n if (timer) clearTimeout(timer);\n reject(error instanceof Error ? error : new Error(String(error)));\n }\n });\n}\n\nexport function awaitPush(\n session: Session,\n type: CoordinatorMessageType,\n options: AwaitPushOptions = {},\n): Promise<Envelope> {\n const state = getSessionState(session);\n const runtime = ensureProtocolRuntime(state);\n const timeoutMs = options.timeoutMs === undefined ? 30_000 : options.timeoutMs;\n\n return new Promise((resolve, reject) => {\n const timer =\n timeoutMs === null\n ? null\n : setTimeout(() => {\n removePushWaiter(runtime, type, waiter);\n reject(new CommandError(\"REJECTED_STALE_JOB\", `timed out waiting for ${type} push`));\n }, timeoutMs);\n const waiter: PushWaiter = { resolve, reject, timer };\n const waiters = runtime.pushWaiters.get(type) ?? [];\n waiters.push(waiter);\n runtime.pushWaiters.set(type, waiters);\n });\n}\n\nexport function onEnvelope(session: Session, handler: ProtocolEnvelopeHandler): Unsubscribe {\n const runtime = ensureProtocolRuntime(getSessionState(session));\n runtime.envelopeHandlers.add(handler);\n return () => runtime.envelopeHandlers.delete(handler);\n}\n\nexport function closeProtocolRuntime(session: Session): void {\n const state = getSessionState(session);\n const runtime = runtimes.get(state);\n if (!runtime) return;\n const error = new TransportError(\"CONNECTION_LOST\", \"coordinator protocol runtime closed\");\n rejectAll(runtime, error);\n runtime.unsubscribeChannel?.();\n runtime.unsubscribeTransport?.();\n runtimes.delete(state);\n}\n\nfunction ensureProtocolRuntime(state: SessionState): ProtocolRuntime {\n const existing = runtimes.get(state);\n if (existing) return existing;\n\n const runtime: ProtocolRuntime = {\n reqCounter: 0,\n pending: new Map(),\n pushWaiters: new Map(),\n envelopeHandlers: new Set(),\n unsubscribeChannel: null,\n unsubscribeTransport: null,\n };\n\n if (state.channel === null) {\n throw new TransportError(\"CONNECTION_LOST\", \"coordinator protocol requires a live channel\");\n }\n runtime.unsubscribeChannel = state.channel.onMessage((frame) => handleFrame(runtime, frame));\n runtime.unsubscribeTransport = state.transport.onStateChange((transportState) => {\n if (transportState === \"closing\" || transportState === \"closed\") {\n rejectAll(runtime, new TransportError(\"CONNECTION_LOST\", \"coordinator transport closed\"));\n }\n });\n\n runtimes.set(state, runtime);\n return runtime;\n}\n\nfunction handleFrame(runtime: ProtocolRuntime, frame: Uint8Array): void {\n void handleFrameAsync(runtime, frame);\n}\n\nasync function handleFrameAsync(runtime: ProtocolRuntime, frame: Uint8Array): Promise<void> {\n const protocol = await loadProtocol();\n const decoded = protocol.decode(frame);\n if (!decoded.ok) {\n rejectAll(runtime, decoded.error);\n return;\n }\n\n const envelope = decoded.value;\n for (const handler of runtime.envelopeHandlers) handler(envelope);\n\n if (envelope.kind === protocol.EnvelopeKind.ERROR) {\n const payload = protocol.asErrorPayload(envelope);\n const error = commandErrorFromCoordinatorError(payload);\n if (payload.failed_req_id === undefined) {\n return;\n }\n rejectPending(runtime, payload.failed_req_id, error);\n return;\n }\n\n if (envelope.kind === protocol.EnvelopeKind.PUSH) {\n resolvePushWaiter(runtime, envelope);\n return;\n }\n\n if (envelope.kind !== protocol.EnvelopeKind.RESPONSE) return;\n const pending = runtime.pending.get(envelope.req_id);\n if (!pending) return;\n runtime.pending.delete(envelope.req_id);\n if (pending.timer) clearTimeout(pending.timer);\n pending.resolve(envelope);\n}\n\nfunction resolvePushWaiter(runtime: ProtocolRuntime, envelope: Envelope): void {\n const waiters = runtime.pushWaiters.get(envelope.type);\n const waiter = waiters?.shift();\n if (!waiter || !waiters) return;\n if (waiters.length === 0) {\n runtime.pushWaiters.delete(envelope.type);\n }\n if (waiter.timer) clearTimeout(waiter.timer);\n waiter.resolve(envelope);\n}\n\nfunction rejectPending(runtime: ProtocolRuntime, reqId: number, error: Error): void {\n const pending = runtime.pending.get(reqId);\n if (!pending) return;\n runtime.pending.delete(reqId);\n if (pending.timer) clearTimeout(pending.timer);\n pending.reject(error);\n}\n\nfunction commandErrorFromCoordinatorError(payload: {\n readonly code: string;\n readonly failed_req_id?: number;\n readonly message: string;\n}): CommandError {\n const coordinatorCode = toCoordinatorWireErrorCode(payload.code);\n return new CommandError(commandCodeFromCoordinatorCode(coordinatorCode), payload.message, {\n coordinatorCode,\n ...(payload.failed_req_id !== undefined ? { failedReqId: payload.failed_req_id } : {}),\n });\n}\n\nfunction commandCodeFromCoordinatorCode(code: CoordinatorWireErrorCode): CommandErrorCode {\n switch (code) {\n case \"ERR_UNEXPECTED_TYPE\":\n case \"ERR_UNKNOWN_SWAP\":\n case \"ERR_SWAP_NOT_REFUNDABLE\":\n return \"REJECTED_STATE\";\n case \"ERR_INTERNAL\":\n case \"ERR_SETTLEMENT_FAILED\":\n return \"REJECTED_STALE_JOB\";\n case \"ERR_INVALID_PAYLOAD\":\n case \"ERR_POLICY_VIOLATION\":\n case \"ERR_INVALID_RECOVERY_CODE\":\n case \"ERR_DKG_FAILURE\":\n case \"ERR_INVALID_DEPOSIT\":\n return \"REJECTED_GUARD\";\n }\n}\n\nfunction toCoordinatorWireErrorCode(code: string): CoordinatorWireErrorCode {\n switch (code) {\n case \"ERR_INVALID_PAYLOAD\":\n case \"ERR_UNEXPECTED_TYPE\":\n case \"ERR_UNKNOWN_SWAP\":\n case \"ERR_POLICY_VIOLATION\":\n case \"ERR_INVALID_RECOVERY_CODE\":\n case \"ERR_SWAP_NOT_REFUNDABLE\":\n case \"ERR_DKG_FAILURE\":\n case \"ERR_INTERNAL\":\n case \"ERR_INVALID_DEPOSIT\":\n case \"ERR_SETTLEMENT_FAILED\":\n return code;\n default:\n return \"ERR_INTERNAL\";\n }\n}\n\nfunction removePushWaiter(\n runtime: ProtocolRuntime,\n type: CoordinatorMessageType,\n waiter: PushWaiter,\n): void {\n const waiters = runtime.pushWaiters.get(type);\n if (!waiters) return;\n const index = waiters.indexOf(waiter);\n if (index >= 0) waiters.splice(index, 1);\n if (waiters.length === 0) runtime.pushWaiters.delete(type);\n}\n\nfunction rejectAll(runtime: ProtocolRuntime, error: Error): void {\n for (const [reqId, pending] of runtime.pending.entries()) {\n runtime.pending.delete(reqId);\n if (pending.timer) clearTimeout(pending.timer);\n pending.reject(error);\n }\n for (const [type, waiters] of runtime.pushWaiters.entries()) {\n runtime.pushWaiters.delete(type);\n for (const waiter of waiters) {\n if (waiter.timer) clearTimeout(waiter.timer);\n waiter.reject(error);\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { COORDINATOR_BINARY_FIELD_NAMES, COORDINATOR_ENVELOPE_SCHEMA, COORDINATOR_SCHEMA_PAYLOAD_CATEGORIES, CoordinatorError, CoordinatorMessageType, DEFAULT_PAYOUT_WINDOW_ID, DIRECTION_BY_TYPE, ENVELOPE_VERSION, EnvelopeKind, ErrorCode, INITIAL_STATE, MAX_FRAME_SIZE, MessageDirection, PAYOUT_WINDOW_OPTIONS, TERMINAL_STATES, TRANSITIONS, VALID_SWAP_STATES, asDelegateAckPayload, asDelegateSharePayload, asDelegateShareResponsePayload, asDepositConfirmedPayload, asDkgCompletePayload, asDkgInitPayload, asDkgInitResponsePayload, asDkgRound1Payload, asDkgRound1ResponsePayload, asDkgRound2Payload, asDkgRound2ResponsePayload, asErrorPayload, asLpCommandResponsePayload, asPayoutExecutedPayload, asSwapRequestPayload, asSwapRequestResponsePayload, asSyncRequiredPayload, asSyncUserRequestPayload, asSyncUserResponsePayload, asWithdrawAcceptedPayload, asWithdrawExecutedPayload, asWithdrawRequestPayload, decode, encode, encodeLpCommandForSigning, encodeValidated, isAllowedInState, isAllowedKindForType, isPayoutWindowId, isValidEnvelopeKind, isValidErrorCode, isValidMessageType, isValidSwapState, lpSignedCommandEnvelope, nextState, payloadCategory, payoutWindowLabelForId, payoutWindowOptionById, safeEncode, scheduledPayoutWindowOptions, scheduledPayoutWindowSeconds } from './chunk-LBC7ALYO.js';
|
|
2
|
+
import './chunk-VR6T6OJS.js';
|
|
3
|
+
//# sourceMappingURL=coordinator-7Y45MCCZ.js.map
|
|
4
|
+
//# sourceMappingURL=coordinator-7Y45MCCZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"coordinator-7Y45MCCZ.js"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { S as Session } from './session-DFuy54C-.js';
|
|
2
|
+
import { T as Transport } from './types-ZhV7TIQY.js';
|
|
3
|
+
import { a as CoordinatorPoolConfig } from './types.generated-CHGSbmLp.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The closed error taxonomy from docs/sdk-specification.md section 8.3.
|
|
7
|
+
*
|
|
8
|
+
* Every error the SDK throws extends {@link InvisibleError} and carries a
|
|
9
|
+
* `code` drawn from a closed, per-class string-literal union. Consumers can
|
|
10
|
+
* branch on `error instanceof CommandError` and then exhaustively `switch` on
|
|
11
|
+
* `error.code` with compile-time completeness.
|
|
12
|
+
*
|
|
13
|
+
* Error classes are the one deliberate exception to the SDK's "no classes"
|
|
14
|
+
* rule: JavaScript errors must extend `Error` to carry a stack and support
|
|
15
|
+
* `instanceof`. This mirrors @solana/kit, whose only class is `SolanaError`.
|
|
16
|
+
* The conformance lint allowlists this file for that reason.
|
|
17
|
+
*/
|
|
18
|
+
/** Attestation verification failures. Stop; do not retry without a new pin. */
|
|
19
|
+
type AttestationErrorCode = "CERT_CHAIN_INVALID" | "TCB_REJECTED" | "FRESHNESS_NONCE_MISMATCH" | "NOISE_BINDING_MISMATCH" | "DEBUG_BIT_SET" | "MRTD_MISMATCH" | "AZURE_MAA_ISSUER_MISMATCH" | "AZURE_MAA_JWKS_INVALID" | "AZURE_MAA_POLICY_HASH_MISMATCH" | "AZURE_MAA_NOT_COMPLIANT" | "AZURE_MAA_DCAP_MISMATCH" | "AZURE_HCL_KEYS_MISMATCH" | "AZURE_AK_PUB_INVALID" | "AZURE_TPM_AK_QUOTE_INVALID" | "AZURE_TPM_QUALIFYING_DATA_MISMATCH" | "NOT_ATTESTED";
|
|
20
|
+
/** Transport-level failures. Reconnect through the pool and re-attest. */
|
|
21
|
+
type TransportErrorCode = "WS_HANDSHAKE_FAILED" | "NOISE_HANDSHAKE_FAILED" | "NOISE_FRAME_TOO_LARGE" | "CONNECTION_LOST";
|
|
22
|
+
/** Leader/role routing failures. Failover and re-attest. */
|
|
23
|
+
type RoutingErrorCode = "WRONG_ROLE" | "STALE_LEADER_EPOCH" | "POOL_DEGRADED" | "VERSION_NOT_SUPPORTED";
|
|
24
|
+
/** Coordinator-side command rejections. Product decides whether to retry. */
|
|
25
|
+
type CommandErrorCode = "REJECTED_GUARD" | "REJECTED_STATE" | "REJECTED_EXPIRED" | "REJECTED_STALE_JOB" | "REJECTED_RESOURCE_CAP";
|
|
26
|
+
/** Coordinator wire error code preserved on command rejections. */
|
|
27
|
+
type CoordinatorWireErrorCode = "ERR_INVALID_PAYLOAD" | "ERR_UNEXPECTED_TYPE" | "ERR_UNKNOWN_SWAP" | "ERR_POLICY_VIOLATION" | "ERR_INVALID_RECOVERY_CODE" | "ERR_SWAP_NOT_REFUNDABLE" | "ERR_DKG_FAILURE" | "ERR_INTERNAL" | "ERR_INVALID_DEPOSIT" | "ERR_SETTLEMENT_FAILED";
|
|
28
|
+
/** Local input-validation failures. Never sent on the wire; fix the input. */
|
|
29
|
+
type PolicyValidationErrorCode = "INVALID_PAYOUT_PLAN" | "INVALID_WITHDRAWAL_PLAN" | "INVALID_POSITION_AUTH" | "INVALID_REDEMPTION_PLAN" | "INVALID_DESTINATION_ADDRESS" | "INVALID_AMOUNT";
|
|
30
|
+
/** Storage-adapter failures. */
|
|
31
|
+
type StorageErrorCode = "STORAGE_NOT_AVAILABLE" | "STORAGE_KEY_MISSING" | "STORAGE_DECRYPT_FAILED";
|
|
32
|
+
/** Wallet-adapter failures. Bubble to the integrator UX. */
|
|
33
|
+
type WalletErrorCode = "WALLET_NOT_CONNECTED" | "WALLET_USER_REJECTED" | "WALLET_INVALID_SIGNATURE";
|
|
34
|
+
/** LP lifecycle local state that callers can branch on. */
|
|
35
|
+
type LpLifecycleErrorCode = "LP_POSITION_AUTH_MISSING" | "LP_REDEMPTION_RECONCILING";
|
|
36
|
+
/** Skeleton-only: a surface that exists but has no implementation yet. */
|
|
37
|
+
type NotImplementedErrorCode = "NOT_IMPLEMENTED";
|
|
38
|
+
/** Every code the SDK can surface, across all classes. */
|
|
39
|
+
type InvisibleErrorCode = AttestationErrorCode | TransportErrorCode | RoutingErrorCode | CommandErrorCode | PolicyValidationErrorCode | StorageErrorCode | WalletErrorCode | LpLifecycleErrorCode | NotImplementedErrorCode;
|
|
40
|
+
/** Options accepted by every {@link InvisibleError}. */
|
|
41
|
+
interface InvisibleErrorOptions {
|
|
42
|
+
/** The underlying error, preserved for debugging. */
|
|
43
|
+
readonly cause?: unknown;
|
|
44
|
+
}
|
|
45
|
+
/** Options accepted by {@link CommandError}. */
|
|
46
|
+
interface CommandErrorOptions extends InvisibleErrorOptions {
|
|
47
|
+
/** Exact coordinator `Error` envelope code, when the rejection came from the wire. */
|
|
48
|
+
readonly coordinatorCode?: CoordinatorWireErrorCode;
|
|
49
|
+
/** Request id rejected by the coordinator, when present in the `Error` envelope. */
|
|
50
|
+
readonly failedReqId?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Base class for every error the SDK throws. Carries a closed `code` and sets
|
|
54
|
+
* `name` to the concrete subclass name so logs and `instanceof` agree.
|
|
55
|
+
*/
|
|
56
|
+
declare abstract class InvisibleError extends Error {
|
|
57
|
+
/** The closed error code for this error's class. */
|
|
58
|
+
abstract readonly code: InvisibleErrorCode;
|
|
59
|
+
constructor(message: string, options?: InvisibleErrorOptions);
|
|
60
|
+
}
|
|
61
|
+
/** Attestation verification failure. */
|
|
62
|
+
declare class AttestationError extends InvisibleError {
|
|
63
|
+
readonly code: AttestationErrorCode;
|
|
64
|
+
constructor(code: AttestationErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
65
|
+
}
|
|
66
|
+
/** Transport-level failure. */
|
|
67
|
+
declare class TransportError extends InvisibleError {
|
|
68
|
+
readonly code: TransportErrorCode;
|
|
69
|
+
constructor(code: TransportErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
70
|
+
}
|
|
71
|
+
/** Leader/role routing failure. */
|
|
72
|
+
declare class RoutingError extends InvisibleError {
|
|
73
|
+
readonly code: RoutingErrorCode;
|
|
74
|
+
constructor(code: RoutingErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
75
|
+
}
|
|
76
|
+
/** Coordinator-side command rejection. */
|
|
77
|
+
declare class CommandError extends InvisibleError {
|
|
78
|
+
readonly code: CommandErrorCode;
|
|
79
|
+
readonly coordinatorCode?: CoordinatorWireErrorCode;
|
|
80
|
+
readonly failedReqId?: number;
|
|
81
|
+
constructor(code: CommandErrorCode, message: string, options?: CommandErrorOptions);
|
|
82
|
+
}
|
|
83
|
+
/** User deposit did not match the coordinator policy amount. */
|
|
84
|
+
declare class InvalidDepositAmountError extends CommandError {
|
|
85
|
+
readonly receivedLamports: bigint;
|
|
86
|
+
readonly expectedLamports: bigint;
|
|
87
|
+
constructor(receivedLamports: bigint, expectedLamports: bigint);
|
|
88
|
+
}
|
|
89
|
+
/** Local input-validation failure; never reaches the wire. */
|
|
90
|
+
declare class PolicyValidationError extends InvisibleError {
|
|
91
|
+
readonly code: PolicyValidationErrorCode;
|
|
92
|
+
constructor(code: PolicyValidationErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
93
|
+
}
|
|
94
|
+
/** Storage-adapter failure. */
|
|
95
|
+
declare class StorageError extends InvisibleError {
|
|
96
|
+
readonly code: StorageErrorCode;
|
|
97
|
+
constructor(code: StorageErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
98
|
+
}
|
|
99
|
+
/** Wallet-adapter failure. */
|
|
100
|
+
declare class WalletError extends InvisibleError {
|
|
101
|
+
readonly code: WalletErrorCode;
|
|
102
|
+
constructor(code: WalletErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
103
|
+
}
|
|
104
|
+
/** LP lifecycle state that is local to the SDK client. */
|
|
105
|
+
declare class LpLifecycleError extends InvisibleError {
|
|
106
|
+
readonly code: LpLifecycleErrorCode;
|
|
107
|
+
constructor(code: LpLifecycleErrorCode, message: string, options?: InvisibleErrorOptions);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A surface that exists in the typed contract but has no runtime behavior yet.
|
|
111
|
+
* The skeleton throws this from every command that is not the connection layer.
|
|
112
|
+
* The message carries the fully-qualified command name (e.g. `user.contractRequest`).
|
|
113
|
+
*/
|
|
114
|
+
declare class NotImplementedError extends InvisibleError {
|
|
115
|
+
readonly code: "NOT_IMPLEMENTED";
|
|
116
|
+
/** The command that is not yet implemented, e.g. `lp.redeem`. */
|
|
117
|
+
readonly command: string;
|
|
118
|
+
constructor(command: string, options?: InvisibleErrorOptions);
|
|
119
|
+
}
|
|
120
|
+
/** Type guard: is `value` any SDK error. */
|
|
121
|
+
declare function isInvisibleError(value: unknown): value is InvisibleError;
|
|
122
|
+
/**
|
|
123
|
+
* Reduce any thrown value to a readable, single-line message. Guarantees the
|
|
124
|
+
* SDK never surfaces `[object Object]` / `[object ErrorEvent]` (spec section
|
|
125
|
+
* 8.3). For an {@link InvisibleError} the code is prefixed as `[CODE] message`.
|
|
126
|
+
*/
|
|
127
|
+
declare function normalizeError(error: unknown, fallback?: string): string;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* `createSession` - the single entrypoint that opens an attested session.
|
|
131
|
+
*
|
|
132
|
+
* It validates the endpoint pool, opens the transport, runs the Noise XX
|
|
133
|
+
* handshake + TDX attestation, and returns an opaque {@link Session} that has
|
|
134
|
+
* already passed attestation (`attested === true`). It resolves only once
|
|
135
|
+
* attestation succeeds; any transport or attestation failure rejects with a
|
|
136
|
+
* typed {@link TransportError} / {@link AttestationError}.
|
|
137
|
+
*
|
|
138
|
+
* A re-attestation timer re-runs verification over the live channel every
|
|
139
|
+
* {@link REATTEST_EVERY_MS}; on failure it marks the session unattested and
|
|
140
|
+
* notifies any registered policy-violation handlers. The transport is
|
|
141
|
+
* injectable for tests.
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
/** Options for {@link createSession}. */
|
|
145
|
+
interface CreateSessionOptions {
|
|
146
|
+
/**
|
|
147
|
+
* The coordinator endpoint pool. Must contain at least one endpoint. No
|
|
148
|
+
* hostname is provided by default; supply your own or use `@invisible-labs/sdk/presets`.
|
|
149
|
+
*/
|
|
150
|
+
readonly coordinator: CoordinatorPoolConfig;
|
|
151
|
+
/**
|
|
152
|
+
* Storage adapter for recoverable secrets. Typed by `@invisible-labs/sdk/storage`;
|
|
153
|
+
* accepted here so the session can be threaded into storage-backed flows.
|
|
154
|
+
*/
|
|
155
|
+
readonly storage?: unknown;
|
|
156
|
+
/** Wallet adapter used for storage key derivation and signing. */
|
|
157
|
+
readonly wallet?: unknown;
|
|
158
|
+
/**
|
|
159
|
+
* Inject a transport. Defaults to a Noise WebSocket transport over the first
|
|
160
|
+
* endpoint. Tests pass an in-memory transport (paired with a loopback
|
|
161
|
+
* coordinator that can complete the handshake + attestation).
|
|
162
|
+
*/
|
|
163
|
+
readonly transport?: Transport;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Open an attested session against the configured coordinator pool.
|
|
167
|
+
*
|
|
168
|
+
* @throws Error if the pool config is structurally invalid (including an empty
|
|
169
|
+
* `endpoints` array).
|
|
170
|
+
* @throws TransportError if the connection or Noise handshake fails.
|
|
171
|
+
* @throws AttestationError if attestation verification fails.
|
|
172
|
+
*/
|
|
173
|
+
declare function createSession(options: CreateSessionOptions): Promise<Session>;
|
|
174
|
+
/** Close a session created by {@link createSession}. */
|
|
175
|
+
declare function closeSession(session: Session): void;
|
|
176
|
+
|
|
177
|
+
export { AttestationError as A, type CreateSessionOptions as C, InvalidDepositAmountError as I, LpLifecycleError as L, NotImplementedError as N, PolicyValidationError as P, RoutingError as R, StorageError as S, TransportError as T, WalletError as W, type AttestationErrorCode as a, CommandError as b, type CommandErrorCode as c, type CommandErrorOptions as d, type CoordinatorWireErrorCode as e, InvisibleError as f, type InvisibleErrorCode as g, type InvisibleErrorOptions as h, type LpLifecycleErrorCode as i, type NotImplementedErrorCode as j, type PolicyValidationErrorCode as k, type RoutingErrorCode as l, type StorageErrorCode as m, type TransportErrorCode as n, type WalletErrorCode as o, closeSession as p, createSession as q, isInvisibleError as r, normalizeError as s };
|