@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 +422 -356
- package/package.json +2 -2
- package/protobuf/tdx/volt_api/data/v1/sqlite.proto +13 -13
- package/protobuf/tdx/volt_api/volt/v1/volt.proto +3 -0
- package/protobuf/tdx/volt_api/volt/v1/wire_api.proto +1 -1
- package/src/grpc-call.js +269 -246
- package/src/volt-client-internal.js +93 -51
- package/src/volt-client.js +65 -64
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(
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
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
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
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
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
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.
|
|
1285
|
-
|
|
1307
|
+
typeof this._voltConfig?.relay === "string" ||
|
|
1308
|
+
this._voltConfig?.relay?.http_address
|
|
1286
1309
|
) {
|
|
1287
|
-
const relayURL =
|
|
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
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
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.
|
|
1296
|
-
this.
|
|
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?.
|
|
1459
|
-
this._config?.relay?.
|
|
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
|
-
|
|
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
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
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(
|
|
1475
|
-
|
|
1476
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
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
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
1685
|
+
this._config = { ...this._config, volt: didConfig };
|
|
1621
1686
|
log("resolved config from DID: %s", JSON.stringify(didConfig, null, 2));
|
|
1622
|
-
} else if (
|
|
1623
|
-
const discoConfig = await fetchVoltConfig.call(
|
|
1624
|
-
|
|
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.
|
|
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.
|
|
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
|
|
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
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
2047
|
+
return new Promise((resolve, reject) => {
|
|
2048
|
+
const grpcClient = this.getVoltAPIClient();
|
|
1982
2049
|
|
|
1983
|
-
|
|
2050
|
+
const blocks = [];
|
|
1984
2051
|
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
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
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2007
|
-
log("unrecognised response payload %s", response.payload);
|
|
2008
|
-
break;
|
|
2009
|
-
}
|
|
2010
|
-
});
|
|
2074
|
+
});
|
|
2011
2075
|
|
|
2012
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
2097
|
+
return subscribeCall.start(grpcClient, request);
|
|
2032
2098
|
}
|
|
2033
2099
|
|
|
2034
2100
|
/**
|
|
2035
2101
|
* WireAPI
|
|
2036
2102
|
*/
|
|
2037
|
-
PublishWire(request
|
|
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
|
|
2107
|
+
return publishCall.start(grpcClient, request);
|
|
2042
2108
|
}
|
|
2043
2109
|
|
|
2044
|
-
SubscribeWire(request
|
|
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
|
|
2118
|
+
return subscribeCall.start(grpcClient, request);
|
|
2053
2119
|
}
|
|
2054
2120
|
}
|
|
2055
2121
|
|