blockintel-gate-sdk 0.4.5 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1217,6 +1217,12 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1217
1217
  if (options.mode === "dry-run") {
1218
1218
  return await originalClient.send(new clientKms.SignCommand(command));
1219
1219
  }
1220
+ const GATEWAY_STAGES = ["HARD_KMS_GATEWAY", "HARD_GCP_GATEWAY", "HARD_HSM_GATEWAY"];
1221
+ const currentStage = gateClient.heartbeatManager?.getAdoptionStage?.();
1222
+ if (currentStage && GATEWAY_STAGES.includes(currentStage)) {
1223
+ emitMetric(options.metricsSink, "sign_success_total", labels);
1224
+ return await signViaProxy(gateClient, decision, command, signerId);
1225
+ }
1220
1226
  return await originalClient.send(new clientKms.SignCommand(command));
1221
1227
  } catch (error) {
1222
1228
  if (error instanceof BlockIntelBlockedError) {
@@ -1230,6 +1236,72 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1230
1236
  throw error;
1231
1237
  }
1232
1238
  }
1239
+ async function signViaProxy(gateClient, decision, command, signerId) {
1240
+ const config = gateClient.config;
1241
+ const baseUrl = config?.baseUrl || config?.controlPlaneUrl;
1242
+ const tenantId = config?.tenantId;
1243
+ if (!baseUrl || !tenantId) {
1244
+ throw new Error("[Gate SDK] Cannot use signing proxy: baseUrl or tenantId not configured on GateClient");
1245
+ }
1246
+ const message = command.input?.Message ?? command.Message;
1247
+ if (!message) {
1248
+ throw new Error("[Gate SDK] SignCommand missing Message for proxy signing");
1249
+ }
1250
+ const messageBuffer = message instanceof Buffer ? message : Buffer.from(message);
1251
+ const messageBase64 = messageBuffer.toString("base64");
1252
+ const keyId = command.input?.KeyId ?? command.KeyId;
1253
+ if (!keyId) {
1254
+ throw new Error("[Gate SDK] SignCommand missing KeyId for proxy signing");
1255
+ }
1256
+ const signingAlgorithm = command.input?.SigningAlgorithm ?? command.SigningAlgorithm ?? "ECDSA_SHA_256";
1257
+ const messageType = command.input?.MessageType ?? command.MessageType ?? "RAW";
1258
+ const proxyUrl = `${baseUrl.replace("/defense", "")}/tenants/${tenantId}/defense/sign`;
1259
+ const headers = {
1260
+ "Content-Type": "application/json"
1261
+ };
1262
+ const authHeaders = gateClient.getAuthHeaders?.();
1263
+ if (authHeaders) {
1264
+ Object.assign(headers, authHeaders);
1265
+ } else {
1266
+ const auth = config?.auth;
1267
+ if (auth?.mode === "api_key" && auth?.apiKey) {
1268
+ headers["x-api-key"] = auth.apiKey;
1269
+ }
1270
+ }
1271
+ const jwt = gateClient.jwt || gateClient.config?.jwt;
1272
+ if (jwt) {
1273
+ headers["Authorization"] = `Bearer ${jwt}`;
1274
+ }
1275
+ const response = await fetch(proxyUrl, {
1276
+ method: "POST",
1277
+ headers,
1278
+ body: JSON.stringify({
1279
+ requestId: decision.decisionId || decision.requestId,
1280
+ decisionToken: decision.decisionToken,
1281
+ keyId,
1282
+ message: messageBase64,
1283
+ signingAlgorithm,
1284
+ messageType
1285
+ })
1286
+ });
1287
+ if (!response.ok) {
1288
+ const errorBody = await response.json().catch(() => ({}));
1289
+ const code = errorBody?.error?.code || "SIGN_PROXY_FAILED";
1290
+ const msg = errorBody?.error?.message || `Signing proxy returned ${response.status}`;
1291
+ throw new Error(`[Gate SDK] ${code}: ${msg}`);
1292
+ }
1293
+ const result = await response.json();
1294
+ const data = result?.data;
1295
+ if (!data?.signature) {
1296
+ throw new Error("[Gate SDK] Signing proxy returned no signature");
1297
+ }
1298
+ return {
1299
+ Signature: Buffer.from(data.signature, "base64"),
1300
+ KeyId: data.keyId || keyId,
1301
+ SigningAlgorithm: data.signingAlgorithm || signingAlgorithm,
1302
+ $metadata: { httpStatusCode: 200 }
1303
+ };
1304
+ }
1233
1305
 
1234
1306
  // src/provenance/ProvenanceProvider.ts
1235
1307
  var ProvenanceProvider = class {