@gvnrdao/dh-sdk 0.0.166 → 0.0.205
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +86 -5
- package/browser/dist/397.browser.js +1 -1
- package/browser/dist/833.browser.js +1 -1
- package/browser/dist/browser.js +1 -13
- package/browser/dist/browser.js.LICENSE.txt +1 -70
- package/browser/dist/index.d.ts +3 -4
- package/browser/dist/index.d.ts.map +1 -1
- package/browser/dist/index.js +4 -3
- package/dist/constants/chunks/contract-abis.d.ts +7 -0
- package/dist/constants/chunks/deployment-addresses.d.ts +68 -0
- package/dist/constants/chunks/encrypted-provider-params.d.ts +21 -0
- package/dist/constants/chunks/environment.browser.d.ts +45 -0
- package/dist/constants/chunks/environment.d.ts +57 -0
- package/dist/constants/chunks/network-configs.d.ts +65 -0
- package/dist/constants/chunks/sdk-config.d.ts +33 -0
- package/dist/constants/chunks/sdk-limits.d.ts +66 -0
- package/dist/constants/index.d.ts +15 -0
- package/dist/graphs/client.d.ts +19 -0
- package/dist/graphs/diamond-hands.d.ts +248 -0
- package/dist/index.d.ts +47 -7391
- package/dist/index.js +4525 -16860
- package/dist/index.mjs +4489 -16801
- package/dist/interfaces/chunks/btc.i.d.ts +36 -0
- package/dist/interfaces/chunks/config.i.d.ts +250 -0
- package/dist/interfaces/chunks/contract-interactions.i.d.ts +64 -0
- package/dist/interfaces/chunks/contract-types.i.d.ts +165 -0
- package/dist/interfaces/chunks/lit-actions-results.i.d.ts +165 -0
- package/dist/interfaces/chunks/lit-actions.i.d.ts +98 -0
- package/dist/interfaces/chunks/loan-operations.i.d.ts +332 -0
- package/dist/interfaces/chunks/pkp-integration.i.d.ts +87 -0
- package/dist/interfaces/chunks/position-query.i.d.ts +76 -0
- package/dist/interfaces/chunks/requests.i.d.ts +55 -0
- package/dist/interfaces/chunks/ucd-minting.i.d.ts +34 -0
- package/dist/interfaces/chunks/utility.i.d.ts +64 -0
- package/dist/interfaces/index.d.ts +17 -0
- package/dist/modules/bitcoin/bitcoin-operations.module.d.ts +223 -0
- package/dist/modules/cache/cache-manager.module.d.ts +92 -0
- package/dist/modules/contract/contract-manager.module.d.ts +136 -0
- package/dist/modules/diamond-hands-sdk.d.ts +669 -0
- package/dist/modules/loan/loan-creator.module.d.ts +143 -0
- package/dist/modules/loan/loan-query.module.d.ts +206 -0
- package/dist/modules/mock/mock-token-manager.module.d.ts +83 -0
- package/dist/modules/pkp/pkp-manager.module.d.ts +136 -0
- package/dist/protocol/protocol-pause.d.ts +19 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.js +285 -0
- package/dist/server.mjs +242 -0
- package/dist/types/authorization-params.d.ts +160 -0
- package/dist/types/branded/domain-values.d.ts +138 -0
- package/dist/types/branded/ids.d.ts +23 -0
- package/dist/types/event-types.d.ts +235 -0
- package/dist/types/graph-dtos.d.ts +228 -0
- package/dist/types/loanStatus.d.ts +10 -0
- package/dist/types/result.d.ts +120 -0
- package/dist/utils/bitcoin-address-cache.utils.d.ts +87 -0
- package/dist/utils/bitcoin-provider.utils.d.ts +48 -0
- package/dist/utils/bitcoin-signature.d.ts +20 -0
- package/dist/utils/chunks/bitcoin-utils.d.ts +75 -0
- package/dist/utils/chunks/eip1559-broadcast.utils.d.ts +24 -0
- package/dist/utils/error-handler.d.ts +106 -0
- package/dist/utils/ethers-interop.utils.d.ts +146 -0
- package/dist/utils/extend-authorization.utils.d.ts +61 -0
- package/dist/utils/lit-signature.utils.d.ts +6 -0
- package/dist/utils/logger.utils.d.ts +142 -0
- package/dist/utils/mint-authorization.utils.d.ts +224 -0
- package/dist/utils/quantum-timing.d.ts +75 -0
- package/dist/utils/signature-tempering.utils.d.ts +31 -0
- package/dist/utils/telegram-messaging.utils.d.ts +188 -0
- package/package.json +43 -22
- package/dist/index.d.mts +0 -7392
package/dist/server.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/server.ts
|
|
31
|
+
var server_exports = {};
|
|
32
|
+
__export(server_exports, {
|
|
33
|
+
envLog: () => envLog,
|
|
34
|
+
getCurrentEnvironment: () => getCurrentEnvironment,
|
|
35
|
+
getEncryptedPriceProviders: () => getEncryptedPriceProviders,
|
|
36
|
+
loadEncryptedProviderRegistry: () => loadEncryptedProviderRegistry,
|
|
37
|
+
loadSDKConfig: () => loadSDKConfig,
|
|
38
|
+
resolveLitNetworkFromEnv: () => resolveLitNetworkFromEnv,
|
|
39
|
+
validateSDKEnvironment: () => validateSDKEnvironment
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(server_exports);
|
|
42
|
+
|
|
43
|
+
// src/constants/chunks/environment.ts
|
|
44
|
+
var import_fs = require("fs");
|
|
45
|
+
var import_dotenv = require("dotenv");
|
|
46
|
+
var path = __toESM(require("path"));
|
|
47
|
+
|
|
48
|
+
// src/constants/chunks/sdk-config.ts
|
|
49
|
+
var DEFAULT_LIT_NETWORKS = {
|
|
50
|
+
CHIPOTLE: "chipotle"
|
|
51
|
+
};
|
|
52
|
+
var DEFAULT_LIT_NETWORK = DEFAULT_LIT_NETWORKS.CHIPOTLE;
|
|
53
|
+
var VALID_LIT_NETWORKS = [
|
|
54
|
+
DEFAULT_LIT_NETWORKS.CHIPOTLE
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
// src/constants/chunks/environment.ts
|
|
58
|
+
var possibleEnvPaths = [
|
|
59
|
+
".env",
|
|
60
|
+
path.join(__dirname, "../../.env"),
|
|
61
|
+
path.join(__dirname, "../../../.env")
|
|
62
|
+
];
|
|
63
|
+
var envPathsAttemptedAbs = [];
|
|
64
|
+
var envPathsMergedAbs = [];
|
|
65
|
+
for (const envPath of possibleEnvPaths) {
|
|
66
|
+
const abs = path.resolve(envPath);
|
|
67
|
+
envPathsAttemptedAbs.push(abs);
|
|
68
|
+
if (!(0, import_fs.existsSync)(abs)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const result = (0, import_dotenv.config)({ path: abs });
|
|
73
|
+
if (!result.error) {
|
|
74
|
+
envPathsMergedAbs.push(abs);
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
var envDiscoveryLogged = false;
|
|
80
|
+
function logEnvDiscoveryOnce() {
|
|
81
|
+
if (envDiscoveryLogged) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
envDiscoveryLogged = true;
|
|
85
|
+
if (process.env.DEBUG_SDK !== "true") {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
console.log("[SDK] .env multi-path load", {
|
|
89
|
+
attempted: envPathsAttemptedAbs,
|
|
90
|
+
mergedFromExistingFiles: envPathsMergedAbs,
|
|
91
|
+
note: "later paths override earlier (dotenv)"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
logEnvDiscoveryOnce();
|
|
95
|
+
function environmentNameFromLitNetwork(_network) {
|
|
96
|
+
return "chipotle";
|
|
97
|
+
}
|
|
98
|
+
function resolveLitNetworkFromEnv() {
|
|
99
|
+
const env = process.env;
|
|
100
|
+
const raw = env["LIT_NETWORK"];
|
|
101
|
+
if (raw === void 0 || raw === "") {
|
|
102
|
+
return DEFAULT_LIT_NETWORK;
|
|
103
|
+
}
|
|
104
|
+
const network = raw;
|
|
105
|
+
if (!VALID_LIT_NETWORKS.includes(network)) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Invalid LIT_NETWORK: "${raw}". Must be one of: ${VALID_LIT_NETWORKS.join(
|
|
108
|
+
", "
|
|
109
|
+
)}. Omit or leave empty for default "${DEFAULT_LIT_NETWORK}" (production Chipotle).`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return network;
|
|
113
|
+
}
|
|
114
|
+
function getCurrentEnvironment() {
|
|
115
|
+
return environmentNameFromLitNetwork(resolveLitNetworkFromEnv());
|
|
116
|
+
}
|
|
117
|
+
function loadSDKConfig() {
|
|
118
|
+
const env = process.env;
|
|
119
|
+
const litNetwork = resolveLitNetworkFromEnv();
|
|
120
|
+
const environment = environmentNameFromLitNetwork(litNetwork);
|
|
121
|
+
const realLitTesting = env["REAL_LIT_TESTING"] === "true";
|
|
122
|
+
const debugMode = env["DEBUG_SDK"] === "true";
|
|
123
|
+
const runRealIntegrationTests = env["RUN_REAL_INTEGRATION_TESTS"] === "true";
|
|
124
|
+
const litRelayUrl = env["LIT_RELAY_URL"];
|
|
125
|
+
const ethRpcUrl = env["ETH_RPC_URL"];
|
|
126
|
+
const polygonRpcUrl = env["POLYGON_RPC_URL"];
|
|
127
|
+
if (!ethRpcUrl) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
"ETH_RPC_URL environment variable is required for SDK configuration"
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
if (!polygonRpcUrl) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
"POLYGON_RPC_URL environment variable is required for SDK configuration"
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
const pkpMintPrivateKey = env["PKP_MINT_PRIVATE_KEY"];
|
|
138
|
+
const config2 = {
|
|
139
|
+
environment,
|
|
140
|
+
litNetwork,
|
|
141
|
+
realLitTesting,
|
|
142
|
+
debugMode,
|
|
143
|
+
runRealIntegrationTests,
|
|
144
|
+
ethRpcUrl,
|
|
145
|
+
polygonRpcUrl
|
|
146
|
+
};
|
|
147
|
+
if (litRelayUrl)
|
|
148
|
+
config2.litRelayUrl = litRelayUrl;
|
|
149
|
+
if (pkpMintPrivateKey)
|
|
150
|
+
config2.pkpMintPrivateKey = pkpMintPrivateKey;
|
|
151
|
+
return config2;
|
|
152
|
+
}
|
|
153
|
+
function envLog(message, ...args) {
|
|
154
|
+
const config2 = loadSDKConfig();
|
|
155
|
+
if (config2.debugMode) {
|
|
156
|
+
console.log(
|
|
157
|
+
`[SDK:${config2.environment.toUpperCase()}] ${message}`,
|
|
158
|
+
...args
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function validateSDKEnvironment(requirements) {
|
|
163
|
+
const config2 = loadSDKConfig();
|
|
164
|
+
if (requirements.requiresRealLitTesting && !config2.realLitTesting) {
|
|
165
|
+
throw new Error("REAL_LIT_TESTING=true is required for this operation");
|
|
166
|
+
}
|
|
167
|
+
if (requirements.requiresPrivateKey && !config2.pkpMintPrivateKey) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
"PKP_MINT_PRIVATE_KEY environment variable is required for this operation"
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return config2;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/constants/chunks/encrypted-provider-params.ts
|
|
176
|
+
var fs = __toESM(require("fs"));
|
|
177
|
+
var path2 = __toESM(require("path"));
|
|
178
|
+
var DEFAULT_REGISTRY_PATH = path2.resolve(
|
|
179
|
+
__dirname,
|
|
180
|
+
"../../../config/encrypted-params.sepolia.json"
|
|
181
|
+
);
|
|
182
|
+
function assertNonEmptyString(value, fieldName, providerName) {
|
|
183
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
184
|
+
const prefix = providerName ? `provider "${providerName}"` : "registry";
|
|
185
|
+
throw new Error(
|
|
186
|
+
`Invalid encrypted params ${prefix}: "${fieldName}" must be a non-empty string`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
return value.trim();
|
|
190
|
+
}
|
|
191
|
+
function parseRegistry(raw, registryPath) {
|
|
192
|
+
let parsed;
|
|
193
|
+
try {
|
|
194
|
+
parsed = JSON.parse(raw);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
197
|
+
throw new Error(
|
|
198
|
+
`Failed to parse encrypted params registry at ${registryPath}: ${message}`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (!parsed || typeof parsed !== "object") {
|
|
202
|
+
throw new Error(
|
|
203
|
+
`Invalid encrypted params registry at ${registryPath}: expected JSON object`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
const candidate = parsed;
|
|
207
|
+
const network = assertNonEmptyString(candidate.network, "network");
|
|
208
|
+
if (!Array.isArray(candidate.providers) || candidate.providers.length === 0) {
|
|
209
|
+
throw new Error(
|
|
210
|
+
`Invalid encrypted params registry at ${registryPath}: "providers" must be a non-empty array`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const providers = candidate.providers.map((provider, index) => {
|
|
214
|
+
const p = provider;
|
|
215
|
+
const name = assertNonEmptyString(
|
|
216
|
+
p.name,
|
|
217
|
+
"name",
|
|
218
|
+
`index ${index.toString()}`
|
|
219
|
+
).toLowerCase();
|
|
220
|
+
const pkpId = assertNonEmptyString(p.pkpId, "pkpId", name);
|
|
221
|
+
const apiKeyEncrypted = assertNonEmptyString(
|
|
222
|
+
p.apiKeyEncrypted,
|
|
223
|
+
"apiKeyEncrypted",
|
|
224
|
+
name
|
|
225
|
+
);
|
|
226
|
+
if (typeof p.version !== "number" || !Number.isFinite(p.version) || p.version < 1) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
`Invalid encrypted params provider "${name}": "version" must be a positive number`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
const updatedAt = assertNonEmptyString(p.updatedAt, "updatedAt", name);
|
|
232
|
+
const normalized = {
|
|
233
|
+
name,
|
|
234
|
+
pkpId,
|
|
235
|
+
apiKeyEncrypted,
|
|
236
|
+
version: p.version,
|
|
237
|
+
updatedAt
|
|
238
|
+
};
|
|
239
|
+
if (p.apiSecretEncrypted !== void 0) {
|
|
240
|
+
normalized.apiSecretEncrypted = assertNonEmptyString(
|
|
241
|
+
p.apiSecretEncrypted,
|
|
242
|
+
"apiSecretEncrypted",
|
|
243
|
+
name
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return normalized;
|
|
247
|
+
});
|
|
248
|
+
return { network, providers };
|
|
249
|
+
}
|
|
250
|
+
function loadEncryptedProviderRegistry(registryPath = DEFAULT_REGISTRY_PATH) {
|
|
251
|
+
if (!fs.existsSync(registryPath)) {
|
|
252
|
+
throw new Error(`Encrypted params registry file not found: ${registryPath}`);
|
|
253
|
+
}
|
|
254
|
+
const raw = fs.readFileSync(registryPath, "utf8");
|
|
255
|
+
return parseRegistry(raw, registryPath);
|
|
256
|
+
}
|
|
257
|
+
function getEncryptedPriceProviders(providerNames = ["coinbase", "coinmarketcap", "binance"], registryPath = DEFAULT_REGISTRY_PATH) {
|
|
258
|
+
const registry = loadEncryptedProviderRegistry(registryPath);
|
|
259
|
+
const desiredNames = providerNames.map((name) => name.trim().toLowerCase());
|
|
260
|
+
const selected = desiredNames.map((name) => {
|
|
261
|
+
const found = registry.providers.find((provider) => provider.name === name);
|
|
262
|
+
if (!found) {
|
|
263
|
+
throw new Error(
|
|
264
|
+
`Encrypted params registry missing provider "${name}" for network "${registry.network}"`
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
name: found.name,
|
|
269
|
+
pkpId: found.pkpId,
|
|
270
|
+
apiKey: found.apiKeyEncrypted,
|
|
271
|
+
apiSecret: found.apiSecretEncrypted
|
|
272
|
+
};
|
|
273
|
+
});
|
|
274
|
+
return selected;
|
|
275
|
+
}
|
|
276
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
277
|
+
0 && (module.exports = {
|
|
278
|
+
envLog,
|
|
279
|
+
getCurrentEnvironment,
|
|
280
|
+
getEncryptedPriceProviders,
|
|
281
|
+
loadEncryptedProviderRegistry,
|
|
282
|
+
loadSDKConfig,
|
|
283
|
+
resolveLitNetworkFromEnv,
|
|
284
|
+
validateSDKEnvironment
|
|
285
|
+
});
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// src/constants/chunks/environment.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { config } from "dotenv";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
|
|
6
|
+
// src/constants/chunks/sdk-config.ts
|
|
7
|
+
var DEFAULT_LIT_NETWORKS = {
|
|
8
|
+
CHIPOTLE: "chipotle"
|
|
9
|
+
};
|
|
10
|
+
var DEFAULT_LIT_NETWORK = DEFAULT_LIT_NETWORKS.CHIPOTLE;
|
|
11
|
+
var VALID_LIT_NETWORKS = [
|
|
12
|
+
DEFAULT_LIT_NETWORKS.CHIPOTLE
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
// src/constants/chunks/environment.ts
|
|
16
|
+
var possibleEnvPaths = [
|
|
17
|
+
".env",
|
|
18
|
+
path.join(__dirname, "../../.env"),
|
|
19
|
+
path.join(__dirname, "../../../.env")
|
|
20
|
+
];
|
|
21
|
+
var envPathsAttemptedAbs = [];
|
|
22
|
+
var envPathsMergedAbs = [];
|
|
23
|
+
for (const envPath of possibleEnvPaths) {
|
|
24
|
+
const abs = path.resolve(envPath);
|
|
25
|
+
envPathsAttemptedAbs.push(abs);
|
|
26
|
+
if (!existsSync(abs)) {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const result = config({ path: abs });
|
|
31
|
+
if (!result.error) {
|
|
32
|
+
envPathsMergedAbs.push(abs);
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
var envDiscoveryLogged = false;
|
|
38
|
+
function logEnvDiscoveryOnce() {
|
|
39
|
+
if (envDiscoveryLogged) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
envDiscoveryLogged = true;
|
|
43
|
+
if (process.env.DEBUG_SDK !== "true") {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
console.log("[SDK] .env multi-path load", {
|
|
47
|
+
attempted: envPathsAttemptedAbs,
|
|
48
|
+
mergedFromExistingFiles: envPathsMergedAbs,
|
|
49
|
+
note: "later paths override earlier (dotenv)"
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
logEnvDiscoveryOnce();
|
|
53
|
+
function environmentNameFromLitNetwork(_network) {
|
|
54
|
+
return "chipotle";
|
|
55
|
+
}
|
|
56
|
+
function resolveLitNetworkFromEnv() {
|
|
57
|
+
const env = process.env;
|
|
58
|
+
const raw = env["LIT_NETWORK"];
|
|
59
|
+
if (raw === void 0 || raw === "") {
|
|
60
|
+
return DEFAULT_LIT_NETWORK;
|
|
61
|
+
}
|
|
62
|
+
const network = raw;
|
|
63
|
+
if (!VALID_LIT_NETWORKS.includes(network)) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Invalid LIT_NETWORK: "${raw}". Must be one of: ${VALID_LIT_NETWORKS.join(
|
|
66
|
+
", "
|
|
67
|
+
)}. Omit or leave empty for default "${DEFAULT_LIT_NETWORK}" (production Chipotle).`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return network;
|
|
71
|
+
}
|
|
72
|
+
function getCurrentEnvironment() {
|
|
73
|
+
return environmentNameFromLitNetwork(resolveLitNetworkFromEnv());
|
|
74
|
+
}
|
|
75
|
+
function loadSDKConfig() {
|
|
76
|
+
const env = process.env;
|
|
77
|
+
const litNetwork = resolveLitNetworkFromEnv();
|
|
78
|
+
const environment = environmentNameFromLitNetwork(litNetwork);
|
|
79
|
+
const realLitTesting = env["REAL_LIT_TESTING"] === "true";
|
|
80
|
+
const debugMode = env["DEBUG_SDK"] === "true";
|
|
81
|
+
const runRealIntegrationTests = env["RUN_REAL_INTEGRATION_TESTS"] === "true";
|
|
82
|
+
const litRelayUrl = env["LIT_RELAY_URL"];
|
|
83
|
+
const ethRpcUrl = env["ETH_RPC_URL"];
|
|
84
|
+
const polygonRpcUrl = env["POLYGON_RPC_URL"];
|
|
85
|
+
if (!ethRpcUrl) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
"ETH_RPC_URL environment variable is required for SDK configuration"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (!polygonRpcUrl) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"POLYGON_RPC_URL environment variable is required for SDK configuration"
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const pkpMintPrivateKey = env["PKP_MINT_PRIVATE_KEY"];
|
|
96
|
+
const config2 = {
|
|
97
|
+
environment,
|
|
98
|
+
litNetwork,
|
|
99
|
+
realLitTesting,
|
|
100
|
+
debugMode,
|
|
101
|
+
runRealIntegrationTests,
|
|
102
|
+
ethRpcUrl,
|
|
103
|
+
polygonRpcUrl
|
|
104
|
+
};
|
|
105
|
+
if (litRelayUrl)
|
|
106
|
+
config2.litRelayUrl = litRelayUrl;
|
|
107
|
+
if (pkpMintPrivateKey)
|
|
108
|
+
config2.pkpMintPrivateKey = pkpMintPrivateKey;
|
|
109
|
+
return config2;
|
|
110
|
+
}
|
|
111
|
+
function envLog(message, ...args) {
|
|
112
|
+
const config2 = loadSDKConfig();
|
|
113
|
+
if (config2.debugMode) {
|
|
114
|
+
console.log(
|
|
115
|
+
`[SDK:${config2.environment.toUpperCase()}] ${message}`,
|
|
116
|
+
...args
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function validateSDKEnvironment(requirements) {
|
|
121
|
+
const config2 = loadSDKConfig();
|
|
122
|
+
if (requirements.requiresRealLitTesting && !config2.realLitTesting) {
|
|
123
|
+
throw new Error("REAL_LIT_TESTING=true is required for this operation");
|
|
124
|
+
}
|
|
125
|
+
if (requirements.requiresPrivateKey && !config2.pkpMintPrivateKey) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
"PKP_MINT_PRIVATE_KEY environment variable is required for this operation"
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return config2;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/constants/chunks/encrypted-provider-params.ts
|
|
134
|
+
import * as fs from "fs";
|
|
135
|
+
import * as path2 from "path";
|
|
136
|
+
var DEFAULT_REGISTRY_PATH = path2.resolve(
|
|
137
|
+
__dirname,
|
|
138
|
+
"../../../config/encrypted-params.sepolia.json"
|
|
139
|
+
);
|
|
140
|
+
function assertNonEmptyString(value, fieldName, providerName) {
|
|
141
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
142
|
+
const prefix = providerName ? `provider "${providerName}"` : "registry";
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Invalid encrypted params ${prefix}: "${fieldName}" must be a non-empty string`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return value.trim();
|
|
148
|
+
}
|
|
149
|
+
function parseRegistry(raw, registryPath) {
|
|
150
|
+
let parsed;
|
|
151
|
+
try {
|
|
152
|
+
parsed = JSON.parse(raw);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Failed to parse encrypted params registry at ${registryPath}: ${message}`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (!parsed || typeof parsed !== "object") {
|
|
160
|
+
throw new Error(
|
|
161
|
+
`Invalid encrypted params registry at ${registryPath}: expected JSON object`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
const candidate = parsed;
|
|
165
|
+
const network = assertNonEmptyString(candidate.network, "network");
|
|
166
|
+
if (!Array.isArray(candidate.providers) || candidate.providers.length === 0) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Invalid encrypted params registry at ${registryPath}: "providers" must be a non-empty array`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const providers = candidate.providers.map((provider, index) => {
|
|
172
|
+
const p = provider;
|
|
173
|
+
const name = assertNonEmptyString(
|
|
174
|
+
p.name,
|
|
175
|
+
"name",
|
|
176
|
+
`index ${index.toString()}`
|
|
177
|
+
).toLowerCase();
|
|
178
|
+
const pkpId = assertNonEmptyString(p.pkpId, "pkpId", name);
|
|
179
|
+
const apiKeyEncrypted = assertNonEmptyString(
|
|
180
|
+
p.apiKeyEncrypted,
|
|
181
|
+
"apiKeyEncrypted",
|
|
182
|
+
name
|
|
183
|
+
);
|
|
184
|
+
if (typeof p.version !== "number" || !Number.isFinite(p.version) || p.version < 1) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`Invalid encrypted params provider "${name}": "version" must be a positive number`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
const updatedAt = assertNonEmptyString(p.updatedAt, "updatedAt", name);
|
|
190
|
+
const normalized = {
|
|
191
|
+
name,
|
|
192
|
+
pkpId,
|
|
193
|
+
apiKeyEncrypted,
|
|
194
|
+
version: p.version,
|
|
195
|
+
updatedAt
|
|
196
|
+
};
|
|
197
|
+
if (p.apiSecretEncrypted !== void 0) {
|
|
198
|
+
normalized.apiSecretEncrypted = assertNonEmptyString(
|
|
199
|
+
p.apiSecretEncrypted,
|
|
200
|
+
"apiSecretEncrypted",
|
|
201
|
+
name
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
return normalized;
|
|
205
|
+
});
|
|
206
|
+
return { network, providers };
|
|
207
|
+
}
|
|
208
|
+
function loadEncryptedProviderRegistry(registryPath = DEFAULT_REGISTRY_PATH) {
|
|
209
|
+
if (!fs.existsSync(registryPath)) {
|
|
210
|
+
throw new Error(`Encrypted params registry file not found: ${registryPath}`);
|
|
211
|
+
}
|
|
212
|
+
const raw = fs.readFileSync(registryPath, "utf8");
|
|
213
|
+
return parseRegistry(raw, registryPath);
|
|
214
|
+
}
|
|
215
|
+
function getEncryptedPriceProviders(providerNames = ["coinbase", "coinmarketcap", "binance"], registryPath = DEFAULT_REGISTRY_PATH) {
|
|
216
|
+
const registry = loadEncryptedProviderRegistry(registryPath);
|
|
217
|
+
const desiredNames = providerNames.map((name) => name.trim().toLowerCase());
|
|
218
|
+
const selected = desiredNames.map((name) => {
|
|
219
|
+
const found = registry.providers.find((provider) => provider.name === name);
|
|
220
|
+
if (!found) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Encrypted params registry missing provider "${name}" for network "${registry.network}"`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
name: found.name,
|
|
227
|
+
pkpId: found.pkpId,
|
|
228
|
+
apiKey: found.apiKeyEncrypted,
|
|
229
|
+
apiSecret: found.apiSecretEncrypted
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
return selected;
|
|
233
|
+
}
|
|
234
|
+
export {
|
|
235
|
+
envLog,
|
|
236
|
+
getCurrentEnvironment,
|
|
237
|
+
getEncryptedPriceProviders,
|
|
238
|
+
loadEncryptedProviderRegistry,
|
|
239
|
+
loadSDKConfig,
|
|
240
|
+
resolveLitNetworkFromEnv,
|
|
241
|
+
validateSDKEnvironment
|
|
242
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed Authorization Parameters using Discriminated Unions
|
|
3
|
+
*
|
|
4
|
+
* This file defines type-safe authorization parameter types that replace
|
|
5
|
+
* the generic `Record<string, any>` previously used in AuthorizationRequest.
|
|
6
|
+
*
|
|
7
|
+
* Each authorization action has its own specific parameter type, ensuring
|
|
8
|
+
* compile-time safety and better IDE support.
|
|
9
|
+
*/
|
|
10
|
+
import { UCD, Satoshis, UnixTimestamp } from "./branded/domain-values";
|
|
11
|
+
/**
|
|
12
|
+
* Base authorization parameters shared by all authorization types
|
|
13
|
+
*/
|
|
14
|
+
interface BaseAuthParams {
|
|
15
|
+
/** Unix timestamp when the authorization request was created */
|
|
16
|
+
timestamp: UnixTimestamp;
|
|
17
|
+
/** Chain ID where the operation will be executed */
|
|
18
|
+
chainId: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parameters for creating a new loan/position
|
|
22
|
+
*/
|
|
23
|
+
export interface CreateLoanAuthParams extends BaseAuthParams {
|
|
24
|
+
type: "create";
|
|
25
|
+
/** Selected loan term in months (e.g., 12, 24, 36) */
|
|
26
|
+
selectedTerm: number;
|
|
27
|
+
/** Borrower's Ethereum address */
|
|
28
|
+
borrower: string;
|
|
29
|
+
/** PKP public key for Bitcoin custody */
|
|
30
|
+
publicKey: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parameters for minting UCD against existing collateral
|
|
34
|
+
*/
|
|
35
|
+
export interface MintUCDAuthParams extends BaseAuthParams {
|
|
36
|
+
type: "mint";
|
|
37
|
+
/** Position/loan identifier */
|
|
38
|
+
positionId: string;
|
|
39
|
+
/** Amount of UCD to mint (in wei, 18 decimals) */
|
|
40
|
+
amount: UCD;
|
|
41
|
+
/** PKP public key (optional - validator PKP is passed separately to authorizeMintUCD) */
|
|
42
|
+
publicKey?: string;
|
|
43
|
+
/** Bitcoin provider URL for balance verification */
|
|
44
|
+
bitcoinProviderUrl?: string;
|
|
45
|
+
/** Selected term in months (override when contract state is zero) */
|
|
46
|
+
selectedTerm?: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Parameters for withdrawing Bitcoin from vault
|
|
50
|
+
*/
|
|
51
|
+
export interface WithdrawBTCAuthParams extends BaseAuthParams {
|
|
52
|
+
type: "withdraw";
|
|
53
|
+
/** Position/loan identifier */
|
|
54
|
+
positionId: string;
|
|
55
|
+
/** Destination Bitcoin address */
|
|
56
|
+
destinationAddress: string;
|
|
57
|
+
/** Amount to withdraw in satoshis */
|
|
58
|
+
amount: Satoshis;
|
|
59
|
+
/** PKP public key */
|
|
60
|
+
publicKey: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parameters for repaying UCD debt
|
|
64
|
+
*/
|
|
65
|
+
export interface RepayDebtAuthParams extends BaseAuthParams {
|
|
66
|
+
type: "repay";
|
|
67
|
+
/** Position/loan identifier */
|
|
68
|
+
positionId: string;
|
|
69
|
+
/** Amount of UCD to repay (in wei, 18 decimals) */
|
|
70
|
+
amount: UCD;
|
|
71
|
+
/** Whether this is a full repayment closing the position */
|
|
72
|
+
closeLoan: boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Parameters for extending loan term
|
|
76
|
+
*/
|
|
77
|
+
export interface ExtendTermAuthParams extends BaseAuthParams {
|
|
78
|
+
type: "extend";
|
|
79
|
+
/** Position/loan identifier */
|
|
80
|
+
positionId: string;
|
|
81
|
+
/** New term length in months */
|
|
82
|
+
newTerm: number;
|
|
83
|
+
/** Expected extension fee in UCD */
|
|
84
|
+
extensionFee: UCD;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parameters for closing a position
|
|
88
|
+
*/
|
|
89
|
+
export interface ClosePositionAuthParams extends BaseAuthParams {
|
|
90
|
+
type: "close";
|
|
91
|
+
/** Position/loan identifier */
|
|
92
|
+
positionId: string;
|
|
93
|
+
/** Final debt amount being repaid */
|
|
94
|
+
finalDebt: UCD;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Union type of all possible authorization parameters
|
|
98
|
+
*
|
|
99
|
+
* This is a discriminated union keyed by the 'type' field.
|
|
100
|
+
* TypeScript can narrow the type based on the 'type' value.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* function handleAuth(params: AuthorizationParams) {
|
|
105
|
+
* switch (params.type) {
|
|
106
|
+
* case 'mint':
|
|
107
|
+
* // TypeScript knows params has positionId, amount, publicKey
|
|
108
|
+
* return handleMint(params.positionId, params.amount);
|
|
109
|
+
* case 'withdraw':
|
|
110
|
+
* // TypeScript knows params has destinationAddress
|
|
111
|
+
* return handleWithdraw(params.destinationAddress, params.amount);
|
|
112
|
+
* }
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export type AuthorizationParams = CreateLoanAuthParams | MintUCDAuthParams | WithdrawBTCAuthParams | RepayDebtAuthParams | ExtendTermAuthParams | ClosePositionAuthParams;
|
|
117
|
+
/**
|
|
118
|
+
* Type guard to check if params are for loan creation
|
|
119
|
+
*/
|
|
120
|
+
export declare function isCreateLoanAuth(params: AuthorizationParams): params is CreateLoanAuthParams;
|
|
121
|
+
/**
|
|
122
|
+
* Type guard to check if params are for UCD minting
|
|
123
|
+
*/
|
|
124
|
+
export declare function isMintUCDAuth(params: AuthorizationParams): params is MintUCDAuthParams;
|
|
125
|
+
/**
|
|
126
|
+
* Type guard to check if params are for BTC withdrawal
|
|
127
|
+
*/
|
|
128
|
+
export declare function isWithdrawBTCAuth(params: AuthorizationParams): params is WithdrawBTCAuthParams;
|
|
129
|
+
/**
|
|
130
|
+
* Type guard to check if params are for debt repayment
|
|
131
|
+
*/
|
|
132
|
+
export declare function isRepayDebtAuth(params: AuthorizationParams): params is RepayDebtAuthParams;
|
|
133
|
+
/**
|
|
134
|
+
* Type guard to check if params are for term extension
|
|
135
|
+
*/
|
|
136
|
+
export declare function isExtendTermAuth(params: AuthorizationParams): params is ExtendTermAuthParams;
|
|
137
|
+
/**
|
|
138
|
+
* Type guard to check if params are for position closure
|
|
139
|
+
*/
|
|
140
|
+
export declare function isClosePositionAuth(params: AuthorizationParams): params is ClosePositionAuthParams;
|
|
141
|
+
/**
|
|
142
|
+
* Helper to extract the action type from authorization params
|
|
143
|
+
*/
|
|
144
|
+
export declare function getAuthActionType(params: AuthorizationParams): string;
|
|
145
|
+
/**
|
|
146
|
+
* Validation helpers for authorization parameters
|
|
147
|
+
*/
|
|
148
|
+
export declare namespace AuthParamsValidation {
|
|
149
|
+
function validateCreateLoan(params: CreateLoanAuthParams): string[];
|
|
150
|
+
function validateMintUCD(params: MintUCDAuthParams): string[];
|
|
151
|
+
function validateWithdrawBTC(params: WithdrawBTCAuthParams): string[];
|
|
152
|
+
function validateRepayDebt(params: RepayDebtAuthParams): string[];
|
|
153
|
+
function validateExtendTerm(params: ExtendTermAuthParams): string[];
|
|
154
|
+
function validateClosePosition(params: ClosePositionAuthParams): string[];
|
|
155
|
+
/**
|
|
156
|
+
* Validates authorization params based on their type
|
|
157
|
+
*/
|
|
158
|
+
function validate(params: AuthorizationParams): string[];
|
|
159
|
+
}
|
|
160
|
+
export {};
|