@tdxvolt/volt-client-grpc 0.1.2 → 0.11.0

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/lib/index.cjs CHANGED
@@ -789,260 +789,283 @@ var protoUtils = /*#__PURE__*/Object.freeze({
789
789
 
790
790
  const log$3 = debug__default["default"]("volt-client-grpc:grpc-call");
791
791
 
792
- function _prepareInvokeRequest(request, method, methodType = "METHOD_TYPE_UNARY") {
793
- const meta = this._voltClient.credential.getIdentityMetadata(
794
- this._voltClient.grpc,
795
- this._voltClient.voltConfig.id,
796
- this._voltClient.isRemote
797
- );
798
- let invokeRequest;
799
- let callMethod = method;
800
-
801
- if (this._voltClient.isVoltRelay) {
802
- let requestPayload;
803
- if (request) {
804
- requestPayload = this._grpcClient[method].requestSerialize(request);
805
- } else if (request !== null) {
806
- log$3("****************LOOKOUT****************** - empty request");
807
- } else {
808
- log$3("explicit empty request payload");
809
- }
810
-
811
- const tunnelMethod = this._grpcClient.Tunnel;
812
-
813
- const tunnelResp = {
814
- method_invoke: {
815
- method_name: this._grpcClient[method].path,
816
- method_type: methodType,
817
- token: meta.token,
818
- request: requestPayload,
819
- },
820
- };
821
-
822
- const invokePayload = VoltCredential.aesEncrypt(
823
- tunnelMethod.responseSerialize(tunnelResp),
824
- meta.sharedKey.key,
825
- meta.sharedKey.iv
826
- );
827
-
828
- log$3("target volt is %s", this._voltClient.credential.voltFingerprint);
829
-
830
- invokeRequest = {
831
- identity_fingerprint: this._voltClient.credential.voltFingerprint,
832
- payload: invokePayload,
833
- };
834
-
835
- callMethod = "Invoke";
836
- } else if (this._voltClient.isRemote && meta.sharedKey.key) {
837
- const requestPayload = this._grpcClient[method].requestSerializeOriginal(request);
838
- invokeRequest = VoltCredential.aesEncrypt(requestPayload, meta.sharedKey.key, meta.sharedKey.iv);
839
- } else {
840
- invokeRequest = request;
841
- }
842
-
843
- return {
844
- meta,
845
- method: callMethod,
846
- methodName: method,
847
- request: invokeRequest,
848
- };
792
+ function _prepareInvokeRequest(
793
+ request,
794
+ method,
795
+ methodType = "METHOD_TYPE_UNARY",
796
+ ) {
797
+ const meta = this._voltClient.credential.getIdentityMetadata(
798
+ this._voltClient.grpc,
799
+ this._voltClient.voltConfig.id,
800
+ this._voltClient.isRemote,
801
+ );
802
+ let invokeRequest;
803
+ let callMethod = method;
804
+
805
+ if (this._voltClient.isVoltRelay) {
806
+ let requestPayload;
807
+ if (request) {
808
+ requestPayload = this._grpcClient[method].requestSerialize(request);
809
+ } else if (request !== null) {
810
+ log$3("****************LOOKOUT****************** - empty request");
811
+ } else {
812
+ log$3("explicit empty request payload");
813
+ }
814
+
815
+ const tunnelMethod = this._grpcClient.Tunnel;
816
+
817
+ const tunnelResp = {
818
+ method_invoke: {
819
+ method_name: this._grpcClient[method].path,
820
+ method_type: methodType,
821
+ token: meta.token,
822
+ request: requestPayload,
823
+ },
824
+ };
825
+
826
+ const invokePayload = VoltCredential.aesEncrypt(
827
+ tunnelMethod.responseSerialize(tunnelResp),
828
+ meta.sharedKey.key,
829
+ meta.sharedKey.iv,
830
+ );
831
+
832
+ log$3("target volt is %s", this._voltClient.credential.voltFingerprint);
833
+
834
+ invokeRequest = {
835
+ identity_fingerprint: this._voltClient.credential.voltFingerprint,
836
+ payload: invokePayload,
837
+ };
838
+
839
+ callMethod = "Invoke";
840
+ } else if (this._voltClient.isRemote && meta.sharedKey.key) {
841
+ const requestPayload =
842
+ this._grpcClient[method].requestSerializeOriginal(request);
843
+ invokeRequest = VoltCredential.aesEncrypt(
844
+ requestPayload,
845
+ meta.sharedKey.key,
846
+ meta.sharedKey.iv,
847
+ );
848
+ } else {
849
+ invokeRequest = request;
850
+ }
851
+
852
+ return {
853
+ meta,
854
+ method: callMethod,
855
+ methodName: method,
856
+ request: invokeRequest,
857
+ };
849
858
  }
850
859
 
851
860
  function _preparePayloadRequest(request, invokeInfo) {
852
- let payloadRequest;
853
-
854
- if (this._voltClient.isVoltRelay) {
855
- const requestPayload = this._grpcClient[invokeInfo.methodName].requestSerialize(request);
856
- const tunnelMethod = this._grpcClient.Tunnel;
857
-
858
- const tunnelResp = {
859
- method_payload: {
860
- payload: requestPayload,
861
- },
862
- };
863
-
864
- const invokePayload = VoltCredential.aesEncrypt(
865
- tunnelMethod.responseSerialize(tunnelResp),
866
- invokeInfo.meta.sharedKey.key,
867
- invokeInfo.meta.sharedKey.iv
868
- );
869
-
870
- payloadRequest = {
871
- payload: invokePayload,
872
- };
873
- } else if (this._voltClient.isRemote && invokeInfo.meta.sharedKey.key) {
874
- const requestPayload = this._grpcClient[invokeInfo.methodName].requestSerializeOriginal(request);
875
- payloadRequest = VoltCredential.aesEncrypt(
876
- requestPayload,
877
- invokeInfo.meta.sharedKey.key,
878
- invokeInfo.meta.sharedKey.iv
879
- );
880
- } else {
881
- payloadRequest = request;
882
- }
883
-
884
- return payloadRequest;
861
+ let payloadRequest;
862
+
863
+ if (this._voltClient.isVoltRelay) {
864
+ const requestPayload =
865
+ this._grpcClient[invokeInfo.methodName].requestSerialize(request);
866
+ const tunnelMethod = this._grpcClient.Tunnel;
867
+
868
+ const tunnelResp = {
869
+ method_payload: {
870
+ payload: requestPayload,
871
+ },
872
+ };
873
+
874
+ const invokePayload = VoltCredential.aesEncrypt(
875
+ tunnelMethod.responseSerialize(tunnelResp),
876
+ invokeInfo.meta.sharedKey.key,
877
+ invokeInfo.meta.sharedKey.iv,
878
+ );
879
+
880
+ payloadRequest = {
881
+ payload: invokePayload,
882
+ };
883
+ } else if (this._voltClient.isRemote && invokeInfo.meta.sharedKey.key) {
884
+ const requestPayload =
885
+ this._grpcClient[invokeInfo.methodName].requestSerializeOriginal(request);
886
+ payloadRequest = VoltCredential.aesEncrypt(
887
+ requestPayload,
888
+ invokeInfo.meta.sharedKey.key,
889
+ invokeInfo.meta.sharedKey.iv,
890
+ );
891
+ } else {
892
+ payloadRequest = request;
893
+ }
894
+
895
+ return payloadRequest;
885
896
  }
886
897
 
887
898
  function _parseResponse(method, meta, response) {
888
- let invokeResponse = {};
889
-
890
- if (this._voltClient.isVoltRelay) {
891
- if (response.payload) {
892
- const decryptedPayload = VoltCredential.aesDecrypt(response.payload, meta.sharedKey.key, meta.sharedKey.iv);
893
- const tunnelMethod = this._grpcClient.Tunnel;
894
- const responsePayload = tunnelMethod.requestDeserialize(decryptedPayload);
895
- if (responsePayload.payload === "method_payload") {
896
- invokeResponse.payload = this._grpcClient[method].responseDeserialize(responsePayload.method_payload.payload);
897
- } else if (responsePayload.payload === "method_end") {
898
- invokeResponse = responsePayload.method_end;
899
- } else {
900
- log$3("unexpected tunnel payload type: %s", responsePayload.payload);
901
- }
902
- } else if (response.status?.message) {
903
- // There's been an error in the tunnel.
904
- log$3(
905
- "Tunnel status received: %s, code %d, description: %s",
906
- response.status.message,
907
- response.status.code,
908
- response.status.description || "n/a"
909
- );
910
- invokeResponse = {ended: true, error: `Error in tunnel: ${response.status.message}`};
911
- } else {
912
- invokeResponse.methodId = response.invoke_id;
913
- log$3("started method %d for %s", invokeResponse.methodId, method);
914
- }
915
- } else if (this._voltClient.isRemote && meta.sharedKey.key) {
916
- const decryptedPayload = VoltCredential.aesDecrypt(response, meta.sharedKey.key, meta.sharedKey.iv);
917
- invokeResponse = {payload: this._grpcClient[method].responseDeserializeOriginal(decryptedPayload)};
918
- } else {
919
- invokeResponse = {payload: response};
920
- }
921
-
922
- return invokeResponse;
899
+ let invokeResponse = {};
900
+
901
+ if (this._voltClient.isVoltRelay) {
902
+ if (response.payload) {
903
+ const decryptedPayload = VoltCredential.aesDecrypt(
904
+ response.payload,
905
+ meta.sharedKey.key,
906
+ meta.sharedKey.iv,
907
+ );
908
+ const tunnelMethod = this._grpcClient.Tunnel;
909
+ const responsePayload = tunnelMethod.requestDeserialize(decryptedPayload);
910
+ if (responsePayload.payload === "method_payload") {
911
+ invokeResponse.payload = this._grpcClient[method].responseDeserialize(
912
+ responsePayload.method_payload.payload,
913
+ );
914
+ } else if (responsePayload.payload === "method_end") {
915
+ invokeResponse = responsePayload.method_end;
916
+ } else {
917
+ log$3("unexpected tunnel payload type: %s", responsePayload.payload);
918
+ }
919
+ } else if (response.status?.message) {
920
+ // There's been an error in the tunnel.
921
+ log$3(
922
+ "Tunnel status received: %s, code %d, description: %s",
923
+ response.status.message,
924
+ response.status.code,
925
+ response.status.description || "n/a",
926
+ );
927
+ invokeResponse = {
928
+ ended: true,
929
+ error: `Error in tunnel: ${response.status.message}`,
930
+ };
931
+ } else {
932
+ invokeResponse.methodId = response.invoke_id;
933
+ log$3("started method %d for %s", invokeResponse.methodId, method);
934
+ }
935
+ } else if (this._voltClient.isRemote && meta.sharedKey.key) {
936
+ const decryptedPayload = VoltCredential.aesDecrypt(
937
+ response,
938
+ meta.sharedKey.key,
939
+ meta.sharedKey.iv,
940
+ );
941
+ invokeResponse = {
942
+ payload:
943
+ this._grpcClient[method].responseDeserializeOriginal(decryptedPayload),
944
+ };
945
+ } else {
946
+ invokeResponse = { payload: response };
947
+ }
948
+
949
+ return invokeResponse;
923
950
  }
924
951
 
925
952
  class GRPCCall extends EventEmitter__default["default"] {
926
- constructor(voltClient, methodName, methodType) {
927
- super();
928
- this._methodName = methodName;
929
- this._methodType = methodType;
930
- this._call = null;
931
- this._voltClient = voltClient;
932
- this._lastResponse = null;
933
- this._promise = null;
934
- this._reject = null;
935
- }
936
-
937
- reject(err) {
938
- if (this._reject) {
939
- this._reject(err);
940
- }
941
- }
942
-
943
- wait() {
944
- return this._promise;
945
- }
946
-
947
- start(grpcClient, request, responseCB) {
948
- this._promise = new Promise((resolve, reject) => {
949
- this._grpcClient = grpcClient;
950
- this._reject = reject;
951
-
952
- const callbackIntercept = (err, response) => {
953
- log$3("received callback for method %s", this._methodName);
954
- if (err) {
955
- reject(err);
956
- } else if (response) {
957
- this._lastResponse = response;
958
- if (responseCB) {
959
- responseCB(err, response);
960
- }
961
- } else {
962
- this.end();
963
- resolve(this._lastResponse);
964
- }
965
- };
966
-
967
- this._initialRequest = _prepareInvokeRequest.call(this, request, this._methodName, this._methodType);
968
-
969
- if (!this._grpcClient[this._initialRequest.method]) {
970
- return reject(new Error(`method not found: '${this._initialRequest.method}'`));
971
- }
972
-
973
- this._call = this._grpcClient[this._initialRequest.method](this._initialRequest.meta.metadata, callbackIntercept);
974
-
975
- this._call.on("data", (streamResponse) => {
976
- try {
977
- const parsedResponse = _parseResponse.call(this, this._methodName, this._initialRequest.meta, streamResponse);
978
- if (parsedResponse.payload) {
979
- callbackIntercept(null, parsedResponse.payload);
980
- } else if (parsedResponse.ended) {
981
- if (parsedResponse.error) {
982
- callbackIntercept(new Error(parsedResponse.error), null);
983
- } else {
984
- callbackIntercept(null, null);
985
- }
986
- } else {
987
- log$3("method id is %s", parsedResponse.methodId);
988
- }
989
- } catch (err) {
990
- log$3("failure processing connect response: %s", err.message);
991
- this.emit("error", err);
992
- }
993
- });
994
-
995
- this._call.on("error", (err) => {
996
- log$3("ERROR - intercepted stream error %s", err.message);
997
- callbackIntercept(err, null);
998
- });
999
-
1000
- this._call.on("finish", () => {
1001
- // The read side has ended (i.e. we called end()).
1002
- log$3("finished call %s", this._methodName);
1003
- this.emit("finish");
1004
- });
1005
-
1006
- this._call.on("end", () => {
1007
- // The remote peer ended the stream.
1008
- log$3("ended call %s, method id: %s", this._methodName, this._initialRequest.methodId || "n/a - not tunnelling");
1009
- this.emit("end");
1010
- callbackIntercept(null, null);
1011
- });
1012
-
1013
- if (this._initialRequest.request) {
1014
- this._call.write(this._initialRequest.request);
1015
- }
1016
- });
1017
-
1018
- return this;
1019
- }
1020
-
1021
- end() {
1022
- if (this._call) {
1023
- return this._call.end();
1024
- } else {
1025
- throw new Error("call not initialised");
1026
- }
1027
- }
1028
-
1029
- get writable() {
1030
- return this._call?.writable;
1031
- }
1032
-
1033
- write(request) {
1034
- if (!this._call) {
1035
- log$3("ERROR - call not initialised");
1036
- throw new Error("call not initialised");
1037
- } else if (this._initialRequest) {
1038
- const payloadRequest = _preparePayloadRequest.call(this, request, this._initialRequest);
1039
- return this._call.write(payloadRequest);
1040
- } else {
1041
- this._initialRequest = _prepareInvokeRequest.call(this, request, this._methodName, this._methodType);
1042
-
1043
- return this._call.write(this._initialRequest.request);
1044
- }
1045
- }
953
+ constructor(voltClient, methodName, methodType) {
954
+ super();
955
+ this._methodName = methodName;
956
+ this._methodType = methodType;
957
+ this._call = null;
958
+ this._voltClient = voltClient;
959
+ }
960
+
961
+ start(grpcClient, request) {
962
+ this._grpcClient = grpcClient;
963
+
964
+ this._initialRequest = _prepareInvokeRequest.call(
965
+ this,
966
+ request,
967
+ this._methodName,
968
+ this._methodType,
969
+ );
970
+
971
+ if (!this._grpcClient[this._initialRequest.method]) {
972
+ return reject(
973
+ new Error(`method not found: '${this._initialRequest.method}'`),
974
+ );
975
+ }
976
+
977
+ this._call = this._grpcClient[this._initialRequest.method](
978
+ this._initialRequest.meta.metadata,
979
+ );
980
+
981
+ this._call.on("data", (streamResponse) => {
982
+ try {
983
+ const parsedResponse = _parseResponse.call(
984
+ this,
985
+ this._methodName,
986
+ this._initialRequest.meta,
987
+ streamResponse,
988
+ );
989
+ if (parsedResponse.payload) {
990
+ this.emit("data", parsedResponse.payload);
991
+ } else if (parsedResponse.ended) {
992
+ if (parsedResponse.error) {
993
+ this.emit("error", new Error(parsedResponse.error));
994
+ } else {
995
+ // Not sure this is necessary - we should receive "end" from grpc soon...
996
+ this.emit("end");
997
+ }
998
+ } else {
999
+ log$3("method id is %s", parsedResponse.methodId);
1000
+ }
1001
+ } catch (err) {
1002
+ log$3("failure processing connect response: %s", err.message);
1003
+ this.emit("error", err);
1004
+ }
1005
+ });
1006
+
1007
+ this._call.on("error", (err) => {
1008
+ log$3("ERROR - intercepted stream error %s", err.message);
1009
+ this.emit("error", err);
1010
+ });
1011
+
1012
+ this._call.on("finish", () => {
1013
+ // The read side has ended (i.e. we called end()).
1014
+ log$3("finished call %s", this._methodName);
1015
+ this.emit("finish");
1016
+ });
1017
+
1018
+ this._call.on("end", () => {
1019
+ // The remote peer ended the stream.
1020
+ log$3(
1021
+ "ended call %s, method id: %s",
1022
+ this._methodName,
1023
+ this._initialRequest.methodId || "n/a - not tunnelling",
1024
+ );
1025
+ this.emit("end");
1026
+ });
1027
+
1028
+ if (this._initialRequest.request) {
1029
+ this._call.write(this._initialRequest.request);
1030
+ }
1031
+
1032
+ return this;
1033
+ }
1034
+
1035
+ end() {
1036
+ if (this._call) {
1037
+ return this._call.end();
1038
+ } else {
1039
+ throw new Error("call not initialised");
1040
+ }
1041
+ }
1042
+
1043
+ get writable() {
1044
+ return this._call?.writable;
1045
+ }
1046
+
1047
+ write(request) {
1048
+ if (!this._call) {
1049
+ log$3("ERROR - call not initialised");
1050
+ throw new Error("call not initialised");
1051
+ } else if (this._initialRequest) {
1052
+ const payloadRequest = _preparePayloadRequest.call(
1053
+ this,
1054
+ request,
1055
+ this._initialRequest,
1056
+ );
1057
+ return this._call.write(payloadRequest);
1058
+ } else {
1059
+ this._initialRequest = _prepareInvokeRequest.call(
1060
+ this,
1061
+ request,
1062
+ this._methodName,
1063
+ this._methodType,
1064
+ );
1065
+
1066
+ return this._call.write(this._initialRequest.request);
1067
+ }
1068
+ }
1046
1069
  }
1047
1070
 
1048
1071
  /* eslint-disable no-use-before-define */
@@ -1281,19 +1304,21 @@ async function bindInternal() {
1281
1304
  try {
1282
1305
  // Check if we need to resolve a Relay volt.
1283
1306
  if (
1284
- this._config?.relay?.discovery_url ||
1285
- typeof this._config?.relay === "string"
1307
+ typeof this._voltConfig?.relay === "string" ||
1308
+ this._voltConfig?.relay?.http_address
1286
1309
  ) {
1287
- const relayURL = this._config.relay?.discovery_url || this._config.relay;
1310
+ const relayURL =
1311
+ this._voltConfig.relay?.http_address || this._voltConfig.relay;
1288
1312
  log$1("attempting to retrieve Relay information from %s", relayURL);
1289
- const voltConfig = await fetchVoltConfig.call(this, relayURL);
1290
- this._config.relay = voltConfig.volt;
1291
- this._config.relay.discovery_url = relayURL;
1292
- if (this._config.relay.ca_pem) {
1313
+ this._voltConfig.relay = await fetchVoltConfig.call(
1314
+ this,
1315
+ `${relayURL}/discovery`,
1316
+ );
1317
+ if (this._voltConfig.relay.ca_pem) {
1293
1318
  log$1(
1294
1319
  "Auto-fetched Relay CA %s, remote address %s",
1295
- this._config.relay.ca_pem,
1296
- this._config.relay.address,
1320
+ this._voltConfig.relay.ca_pem,
1321
+ this._voltConfig.relay.address,
1297
1322
  );
1298
1323
  } else {
1299
1324
  throw new Error("Unable to fetch Relay CA - cannot securely connect.");
@@ -1455,8 +1480,8 @@ function getVoltAPIClientInternal() {
1455
1480
  serviceAddress,
1456
1481
  this._credential,
1457
1482
  this.isRemote && !this.isVoltRelay,
1458
- this._config?.relay?.cloud_relay ? this._voltConfig.id : "",
1459
- this._config?.relay?.cloud_relay ? this._voltConfig.id : "",
1483
+ this._config?.relay?.cloud ? this._voltConfig.id : "",
1484
+ this._config?.relay?.cloud ? this._voltConfig.id : "",
1460
1485
  );
1461
1486
  }
1462
1487
 
@@ -1464,55 +1489,95 @@ function getVoltAPIClientInternal() {
1464
1489
  }
1465
1490
 
1466
1491
  function unaryCall(method, request) {
1467
- const grpcClient = this.getVoltAPIClient();
1492
+ return new Promise((resolve, reject) => {
1493
+ const grpcClient = this.getVoltAPIClient();
1494
+
1495
+ const call = new GRPCCall(this, method, "METHOD_TYPE_UNARY");
1496
+
1497
+ let response = null;
1498
+
1499
+ call.on("data", (payload) => {
1500
+ // Cache the response and wait for `end`.
1501
+ response = payload;
1502
+ });
1468
1503
 
1469
- const call = new GRPCCall(this, method, "METHOD_TYPE_UNARY");
1470
- call.start(grpcClient, request);
1471
- return call.wait();
1504
+ call.on("error", reject);
1505
+
1506
+ call.on("end", () => {
1507
+ if (!response) {
1508
+ return reject(new Error("no response received"));
1509
+ }
1510
+
1511
+ // Convert non-OK status to a JS error.
1512
+ if (response.status?.message) {
1513
+ reject(new Error(response.status.message));
1514
+ } else {
1515
+ resolve(response);
1516
+ }
1517
+ });
1518
+
1519
+ call.start(grpcClient, request);
1520
+ });
1472
1521
  }
1473
1522
 
1474
- async function fetchVoltConfig(disovery_url) {
1475
- const getJSON = bent__default["default"]("json");
1476
- const resolvedConfig = await getJSON(disovery_url);
1477
-
1478
- const voltConfig = pick(
1479
- resolvedConfig,
1480
- "id",
1481
- "display_name",
1482
- "public_key",
1483
- "fingerprint",
1484
- "address",
1485
- "ca_pem",
1486
- "challenge_code",
1487
- "cloud_relay",
1488
- );
1523
+ async function fetchVoltConfig(discovery_url) {
1524
+ try {
1525
+ const getJSON = bent__default["default"]("json");
1526
+ const fullVoltConfig = await getJSON(discovery_url);
1489
1527
 
1490
- const relayConfig = pick(resolvedConfig, "relay");
1528
+ if (!fullVoltConfig || typeof fullVoltConfig !== "object") {
1529
+ throw new Error(`invalid config resolved from: ${discovery_url}`);
1530
+ }
1531
+
1532
+ const voltConfig = pick(
1533
+ fullVoltConfig,
1534
+ "id",
1535
+ "display_name",
1536
+ "public_key",
1537
+ "fingerprint",
1538
+ "address",
1539
+ "ca_pem",
1540
+ "challenge_code",
1541
+ "cloud",
1542
+ "relay",
1543
+ );
1491
1544
 
1492
- return { volt: voltConfig, ...relayConfig };
1545
+ return voltConfig;
1546
+ } catch (err) {
1547
+ throw new Error(
1548
+ `Failure loading config from ${discovery_url}: ${err.message}`,
1549
+ );
1550
+ }
1493
1551
  }
1494
1552
 
1495
1553
  async function fetchVoltConfigFromDID(volt_did) {
1496
- const didResolution = getDIDResolutionURL(volt_did);
1497
- if (!didResolution) {
1498
- throw new Error(`Invalid Volt DID: ${volt_did}`);
1499
- }
1554
+ try {
1555
+ const didResolution = getDIDResolutionURL(volt_did);
1556
+ if (!didResolution) {
1557
+ throw new Error(`Invalid Volt DID: ${volt_did}`);
1558
+ }
1500
1559
 
1501
- const getJSON = bent__default["default"]("json");
1502
- const didDocument = await getJSON(didResolution);
1503
- const voltConfigServices = findDIDDocumentService(
1504
- didDocument,
1505
- constants.didServiceType.voltConfig,
1506
- );
1507
- if (voltConfigServices.length !== 1) {
1508
- log$1(
1509
- "Unexpected number of Volt configuration endpoints found in DID document for %s, services found: %d",
1510
- volt_did,
1511
- voltConfigServices.length,
1560
+ const getJSON = bent__default["default"]("json");
1561
+ const didDocument = await getJSON(didResolution);
1562
+ const voltConfigServices = findDIDDocumentService(
1563
+ didDocument,
1564
+ constants.didServiceType.voltConfig,
1565
+ );
1566
+ if (voltConfigServices.length !== 1) {
1567
+ log$1(
1568
+ "Unexpected number of Volt configuration endpoints found in DID document for %s, services found: %d",
1569
+ volt_did,
1570
+ voltConfigServices.length,
1571
+ );
1572
+ }
1573
+
1574
+ const serviceEndpoint = voltConfigServices[0].serviceEndpoint;
1575
+ return await fetchVoltConfig.call(this, serviceEndpoint);
1576
+ } catch (err) {
1577
+ throw new Error(
1578
+ `Failure fetching Volt config from DID document ${volt_did}: ${err.message}`,
1512
1579
  );
1513
1580
  }
1514
- const serviceEndpoint = voltConfigServices[0].serviceEndpoint;
1515
- return await fetchVoltConfig.call(this, serviceEndpoint);
1516
1581
  }
1517
1582
 
1518
1583
  const { readFile } = fs__default["default"].promises;
@@ -1601,32 +1666,38 @@ class VoltClient extends EventEmitter__default["default"] {
1601
1666
  this._config = { ...configJSON, ...extras };
1602
1667
 
1603
1668
  let voltDID = "";
1604
- let voltDiscovery = "";
1669
+ let voltHttpAddress = "";
1605
1670
  if (typeof this._config.volt === "string") {
1606
1671
  if (this._config.volt.indexOf("did:") === 0) {
1607
1672
  voltDID = this._config.volt;
1608
1673
  } else {
1609
- voltDiscovery = this._config.volt;
1674
+ voltHttpAddress = this._config.volt;
1610
1675
  }
1611
1676
  } else if (typeof this._config.volt !== "object") {
1612
1677
  throw new Error("configuration is missing the 'volt' object");
1613
1678
  } else {
1614
1679
  voltDID = this._config.volt.did;
1615
- voltDiscovery = this._config.volt.discovery_url;
1680
+ voltHttpAddress = this._config.volt.http_address;
1616
1681
  }
1617
1682
 
1618
1683
  if (voltDID) {
1619
1684
  const didConfig = await fetchVoltConfigFromDID.call(this, voltDID);
1620
- this._config = { ...this._config, ...didConfig };
1685
+ this._config = { ...this._config, volt: didConfig };
1621
1686
  log("resolved config from DID: %s", JSON.stringify(didConfig, null, 2));
1622
- } else if (voltDiscovery) {
1623
- const discoConfig = await fetchVoltConfig.call(this, voltDiscovery);
1624
- this._config = { ...this._config, ...discoConfig };
1687
+ } else if (voltHttpAddress) {
1688
+ const discoConfig = await fetchVoltConfig.call(
1689
+ this,
1690
+ `${voltHttpAddress}/discovery`,
1691
+ );
1692
+ this._config = {
1693
+ ...this._config,
1694
+ volt: { ...this._config.volt, ...discoConfig },
1695
+ };
1625
1696
  }
1626
1697
 
1627
1698
  this._voltConfig = this._config.volt;
1628
1699
  this._voltConfig.did = voltDID;
1629
- this._voltConfig.discovery_url = voltDiscovery;
1700
+ this._voltConfig.http_address = voltHttpAddress;
1630
1701
 
1631
1702
  if (!this._voltConfig.id || typeof this._voltConfig.id !== "string") {
1632
1703
  throw new Error("'id' property is missing in Volt configuration");
@@ -1756,7 +1827,7 @@ class VoltClient extends EventEmitter__default["default"] {
1756
1827
  }
1757
1828
 
1758
1829
  get isVoltRelay() {
1759
- return this.isRemote && !this._config?.relay.cloud_relay;
1830
+ return this.isRemote && !this._config?.relay.cloud;
1760
1831
  }
1761
1832
 
1762
1833
  connect() {
@@ -1777,7 +1848,7 @@ class VoltClient extends EventEmitter__default["default"] {
1777
1848
  /**
1778
1849
  * Request resource access.
1779
1850
  * @param {*} targetResourceId
1780
- * @param {*} accessType - see vault.proto for enum values.
1851
+ * @param {*} accessType - see volt.proto for values.
1781
1852
  */
1782
1853
  async RequestAccessBlocking(
1783
1854
  targetResourceId,
@@ -1952,24 +2023,19 @@ class VoltClient extends EventEmitter__default["default"] {
1952
2023
  DownloadFile(request) {
1953
2024
  const grpcClient = this.getVoltAPIClient();
1954
2025
 
1955
- return new Promise((resolve, reject) => {
1956
- const meta = this._credential.getIdentityMetadata(
1957
- this._grpc,
1958
- this._voltConfig.id,
1959
- this.isRemote,
1960
- );
1961
- resolve(grpcClient.DownloadFile(request, meta.metadata));
1962
- }).catch((err) => {
1963
- log("error during %s [%s]", method, err.message);
1964
- return Promise.reject(err);
1965
- });
2026
+ const downloadCall = new GRPCCall(
2027
+ this,
2028
+ "DownloadFile",
2029
+ "METHOD_TYPE_SERVER_STREAM",
2030
+ );
2031
+ return downloadCall.start(grpcClient, request);
1966
2032
  }
1967
2033
 
1968
- UploadFile(request, callback) {
2034
+ UploadFile(request) {
1969
2035
  const grpcClient = this.getVoltAPIClient();
1970
2036
 
1971
2037
  const uploadCall = new GRPCCall(this, "UploadFile", "METHOD_TYPE_BIDI");
1972
- return uploadCall.start(grpcClient, request, callback);
2038
+ return uploadCall.start(grpcClient, request);
1973
2039
  }
1974
2040
 
1975
2041
  /**
@@ -1978,70 +2044,70 @@ class VoltClient extends EventEmitter__default["default"] {
1978
2044
  * @returns file buffer, base64 encoded
1979
2045
  */
1980
2046
  DownloadFileSync(request) {
1981
- const grpcClient = this.getVoltAPIClient();
2047
+ return new Promise((resolve, reject) => {
2048
+ const grpcClient = this.getVoltAPIClient();
1982
2049
 
1983
- const blocks = [];
2050
+ const blocks = [];
1984
2051
 
1985
- const call = new GRPCCall(
1986
- this,
1987
- "DownloadFile",
1988
- "METHOD_TYPE_SERVER_STREAM",
1989
- );
1990
- call.start(grpcClient, request, (err, response) => {
1991
- if (err) {
1992
- return call.reject(err);
1993
- }
2052
+ const call = new GRPCCall(
2053
+ this,
2054
+ "DownloadFile",
2055
+ "METHOD_TYPE_SERVER_STREAM",
2056
+ );
1994
2057
 
1995
- switch (response.payload) {
1996
- case "block": {
1997
- blocks.push(response.block);
1998
- break;
1999
- }
2000
- case "status": {
2001
- if (response.status.message) {
2002
- return call.reject(new Error(response.status.message));
2058
+ call.on("data", (response) => {
2059
+ switch (response.payload) {
2060
+ case "block": {
2061
+ blocks.push(response.block);
2062
+ break;
2003
2063
  }
2004
- break;
2064
+ case "status": {
2065
+ if (response.status.message) {
2066
+ return call.reject(new Error(response.status.message));
2067
+ }
2068
+ break;
2069
+ }
2070
+ default:
2071
+ log("unrecognised response payload %s", response.payload);
2072
+ break;
2005
2073
  }
2006
- default:
2007
- log("unrecognised response payload %s", response.payload);
2008
- break;
2009
- }
2010
- });
2074
+ });
2011
2075
 
2012
- return call
2013
- .wait()
2014
- .then(() => {
2015
- log("Download finished");
2016
- return { buffer: Buffer.concat(blocks).toString("base64") };
2017
- })
2018
- .catch((err) => {
2076
+ call.on("error", (err) => {
2019
2077
  log("error during DownloadFile [%s]", err.message);
2020
- return Promise.reject(err);
2078
+ reject(err);
2021
2079
  });
2080
+
2081
+ call.on("end", () => {
2082
+ log("Download finished");
2083
+ resolve({ buffer: Buffer.concat(blocks).toString("base64") });
2084
+ });
2085
+
2086
+ call.start(grpcClient, request);
2087
+ });
2022
2088
  }
2023
2089
 
2024
2090
  /**
2025
2091
  * SqliteDatabaseAPI
2026
2092
  */
2027
- SqlExecute(request, callback) {
2093
+ SqlExecute(request) {
2028
2094
  const grpcClient = this.getVoltAPIClient();
2029
2095
 
2030
2096
  const subscribeCall = new GRPCCall(this, "Execute", "METHOD_TYPE_BIDI");
2031
- return subscribeCall.start(grpcClient, request, callback);
2097
+ return subscribeCall.start(grpcClient, request);
2032
2098
  }
2033
2099
 
2034
2100
  /**
2035
2101
  * WireAPI
2036
2102
  */
2037
- PublishWire(request, callback) {
2103
+ PublishWire(request) {
2038
2104
  const grpcClient = this.getVoltAPIClient();
2039
2105
 
2040
2106
  const publishCall = new GRPCCall(this, "PublishWire", "METHOD_TYPE_BIDI");
2041
- return publishCall.start(grpcClient, request, callback);
2107
+ return publishCall.start(grpcClient, request);
2042
2108
  }
2043
2109
 
2044
- SubscribeWire(request, callback) {
2110
+ SubscribeWire(request) {
2045
2111
  const grpcClient = this.getVoltAPIClient();
2046
2112
 
2047
2113
  const subscribeCall = new GRPCCall(
@@ -2049,7 +2115,7 @@ class VoltClient extends EventEmitter__default["default"] {
2049
2115
  "SubscribeWire",
2050
2116
  "METHOD_TYPE_BIDI",
2051
2117
  );
2052
- return subscribeCall.start(grpcClient, request, callback);
2118
+ return subscribeCall.start(grpcClient, request);
2053
2119
  }
2054
2120
  }
2055
2121