blockintel-gate-sdk 0.4.5 → 0.4.7

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.
@@ -1184,6 +1184,12 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1184
1184
  if (options.mode === "dry-run") {
1185
1185
  return await originalClient.send(new SignCommand(command));
1186
1186
  }
1187
+ const GATEWAY_STAGES = ["HARD_KMS_GATEWAY", "HARD_GCP_GATEWAY"];
1188
+ const currentStage = gateClient.heartbeatManager?.getAdoptionStage?.();
1189
+ if (currentStage && GATEWAY_STAGES.includes(currentStage)) {
1190
+ emitMetric(options.metricsSink, "sign_success_total", labels);
1191
+ return await signViaProxy(gateClient, decision, command, signerId);
1192
+ }
1187
1193
  return await originalClient.send(new SignCommand(command));
1188
1194
  } catch (error) {
1189
1195
  if (error instanceof BlockIntelBlockedError) {
@@ -1197,6 +1203,72 @@ async function handleSignCommand(command, originalClient, gateClient, options) {
1197
1203
  throw error;
1198
1204
  }
1199
1205
  }
1206
+ async function signViaProxy(gateClient, decision, command, signerId) {
1207
+ const config = gateClient.config;
1208
+ const baseUrl = config?.baseUrl || config?.controlPlaneUrl;
1209
+ const tenantId = config?.tenantId;
1210
+ if (!baseUrl || !tenantId) {
1211
+ throw new Error("[Gate SDK] Cannot use signing proxy: baseUrl or tenantId not configured on GateClient");
1212
+ }
1213
+ const message = command.input?.Message ?? command.Message;
1214
+ if (!message) {
1215
+ throw new Error("[Gate SDK] SignCommand missing Message for proxy signing");
1216
+ }
1217
+ const messageBuffer = message instanceof Buffer ? message : Buffer.from(message);
1218
+ const messageBase64 = messageBuffer.toString("base64");
1219
+ const keyId = command.input?.KeyId ?? command.KeyId;
1220
+ if (!keyId) {
1221
+ throw new Error("[Gate SDK] SignCommand missing KeyId for proxy signing");
1222
+ }
1223
+ const signingAlgorithm = command.input?.SigningAlgorithm ?? command.SigningAlgorithm ?? "ECDSA_SHA_256";
1224
+ const messageType = command.input?.MessageType ?? command.MessageType ?? "RAW";
1225
+ const proxyUrl = `${baseUrl.replace("/defense", "")}/tenants/${tenantId}/defense/sign`;
1226
+ const headers = {
1227
+ "Content-Type": "application/json"
1228
+ };
1229
+ const authHeaders = gateClient.getAuthHeaders?.();
1230
+ if (authHeaders) {
1231
+ Object.assign(headers, authHeaders);
1232
+ } else {
1233
+ const auth = config?.auth;
1234
+ if (auth?.mode === "api_key" && auth?.apiKey) {
1235
+ headers["x-api-key"] = auth.apiKey;
1236
+ }
1237
+ }
1238
+ const jwt = gateClient.jwt || gateClient.config?.jwt;
1239
+ if (jwt) {
1240
+ headers["Authorization"] = `Bearer ${jwt}`;
1241
+ }
1242
+ const response = await fetch(proxyUrl, {
1243
+ method: "POST",
1244
+ headers,
1245
+ body: JSON.stringify({
1246
+ requestId: decision.decisionId || decision.requestId,
1247
+ decisionToken: decision.decisionToken,
1248
+ keyId,
1249
+ message: messageBase64,
1250
+ signingAlgorithm,
1251
+ messageType
1252
+ })
1253
+ });
1254
+ if (!response.ok) {
1255
+ const errorBody = await response.json().catch(() => ({}));
1256
+ const code = errorBody?.error?.code || "SIGN_PROXY_FAILED";
1257
+ const msg = errorBody?.error?.message || `Signing proxy returned ${response.status}`;
1258
+ throw new Error(`[Gate SDK] ${code}: ${msg}`);
1259
+ }
1260
+ const result = await response.json();
1261
+ const data = result?.data;
1262
+ if (!data?.signature) {
1263
+ throw new Error("[Gate SDK] Signing proxy returned no signature");
1264
+ }
1265
+ return {
1266
+ Signature: Buffer.from(data.signature, "base64"),
1267
+ KeyId: data.keyId || keyId,
1268
+ SigningAlgorithm: data.signingAlgorithm || signingAlgorithm,
1269
+ $metadata: { httpStatusCode: 200 }
1270
+ };
1271
+ }
1200
1272
 
1201
1273
  // src/provenance/ProvenanceProvider.ts
1202
1274
  var ProvenanceProvider = class {