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.js CHANGED
@@ -1212,6 +1212,12 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1212
1212
  if (options.mode === "dry-run") {
1213
1213
  return await originalClient.send(new SignCommand(command));
1214
1214
  }
1215
+ const GATEWAY_STAGES = ["HARD_KMS_GATEWAY", "HARD_GCP_GATEWAY", "HARD_HSM_GATEWAY"];
1216
+ const currentStage = gateClient.heartbeatManager?.getAdoptionStage?.();
1217
+ if (currentStage && GATEWAY_STAGES.includes(currentStage)) {
1218
+ emitMetric(options.metricsSink, "sign_success_total", labels);
1219
+ return await signViaProxy(gateClient, decision, command, signerId);
1220
+ }
1215
1221
  return await originalClient.send(new SignCommand(command));
1216
1222
  } catch (error) {
1217
1223
  if (error instanceof BlockIntelBlockedError) {
@@ -1225,6 +1231,72 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1225
1231
  throw error;
1226
1232
  }
1227
1233
  }
1234
+ async function signViaProxy(gateClient, decision, command, signerId) {
1235
+ const config = gateClient.config;
1236
+ const baseUrl = config?.baseUrl || config?.controlPlaneUrl;
1237
+ const tenantId = config?.tenantId;
1238
+ if (!baseUrl || !tenantId) {
1239
+ throw new Error("[Gate SDK] Cannot use signing proxy: baseUrl or tenantId not configured on GateClient");
1240
+ }
1241
+ const message = command.input?.Message ?? command.Message;
1242
+ if (!message) {
1243
+ throw new Error("[Gate SDK] SignCommand missing Message for proxy signing");
1244
+ }
1245
+ const messageBuffer = message instanceof Buffer ? message : Buffer.from(message);
1246
+ const messageBase64 = messageBuffer.toString("base64");
1247
+ const keyId = command.input?.KeyId ?? command.KeyId;
1248
+ if (!keyId) {
1249
+ throw new Error("[Gate SDK] SignCommand missing KeyId for proxy signing");
1250
+ }
1251
+ const signingAlgorithm = command.input?.SigningAlgorithm ?? command.SigningAlgorithm ?? "ECDSA_SHA_256";
1252
+ const messageType = command.input?.MessageType ?? command.MessageType ?? "RAW";
1253
+ const proxyUrl = `${baseUrl.replace("/defense", "")}/tenants/${tenantId}/defense/sign`;
1254
+ const headers = {
1255
+ "Content-Type": "application/json"
1256
+ };
1257
+ const authHeaders = gateClient.getAuthHeaders?.();
1258
+ if (authHeaders) {
1259
+ Object.assign(headers, authHeaders);
1260
+ } else {
1261
+ const auth = config?.auth;
1262
+ if (auth?.mode === "api_key" && auth?.apiKey) {
1263
+ headers["x-api-key"] = auth.apiKey;
1264
+ }
1265
+ }
1266
+ const jwt = gateClient.jwt || gateClient.config?.jwt;
1267
+ if (jwt) {
1268
+ headers["Authorization"] = `Bearer ${jwt}`;
1269
+ }
1270
+ const response = await fetch(proxyUrl, {
1271
+ method: "POST",
1272
+ headers,
1273
+ body: JSON.stringify({
1274
+ requestId: decision.decisionId || decision.requestId,
1275
+ decisionToken: decision.decisionToken,
1276
+ keyId,
1277
+ message: messageBase64,
1278
+ signingAlgorithm,
1279
+ messageType
1280
+ })
1281
+ });
1282
+ if (!response.ok) {
1283
+ const errorBody = await response.json().catch(() => ({}));
1284
+ const code = errorBody?.error?.code || "SIGN_PROXY_FAILED";
1285
+ const msg = errorBody?.error?.message || `Signing proxy returned ${response.status}`;
1286
+ throw new Error(`[Gate SDK] ${code}: ${msg}`);
1287
+ }
1288
+ const result = await response.json();
1289
+ const data = result?.data;
1290
+ if (!data?.signature) {
1291
+ throw new Error("[Gate SDK] Signing proxy returned no signature");
1292
+ }
1293
+ return {
1294
+ Signature: Buffer.from(data.signature, "base64"),
1295
+ KeyId: data.keyId || keyId,
1296
+ SigningAlgorithm: data.signingAlgorithm || signingAlgorithm,
1297
+ $metadata: { httpStatusCode: 200 }
1298
+ };
1299
+ }
1228
1300
 
1229
1301
  // src/provenance/ProvenanceProvider.ts
1230
1302
  var ProvenanceProvider = class {