@eluvio/elv-client-js 4.0.78 → 4.0.79

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.
@@ -16,7 +16,10 @@ var Ethers = require("ethers");
16
16
  var _require = require("../Validation"),
17
17
  ValidateAddress = _require.ValidateAddress,
18
18
  ValidateParameters = _require.ValidateParameters,
19
- ValidatePresence = _require.ValidatePresence;
19
+ ValidatePresence = _require.ValidatePresence,
20
+ ValidateObject = _require.ValidateObject,
21
+ ValidateVersion = _require.ValidateVersion;
22
+ var Utils = require("../Utils");
20
23
 
21
24
  /**
22
25
  * Return the name of the contract, as specified in the contracts "version" string
@@ -515,13 +518,16 @@ exports.ReplaceContractMetadata = /*#__PURE__*/function () {
515
518
  contractAddress = _ref20.contractAddress, metadataKey = _ref20.metadataKey, metadata = _ref20.metadata;
516
519
  ValidatePresence("contractAddress", contractAddress);
517
520
  ValidatePresence("metadataKey", metadataKey);
518
- _context10.next = 5;
521
+ if (_typeof(metadata) === "object") {
522
+ metadata = JSON.stringify(metadata);
523
+ }
524
+ _context10.next = 6;
519
525
  return this.CallContractMethodAndWait({
520
526
  contractAddress: contractAddress,
521
527
  methodName: "putMeta",
522
- methodArgs: [metadataKey, JSON.stringify(metadata)]
528
+ methodArgs: [metadataKey, metadata]
523
529
  });
524
- case 5:
530
+ case 6:
525
531
  case "end":
526
532
  return _context10.stop();
527
533
  }
@@ -928,4 +934,262 @@ exports.SendFunds = /*#__PURE__*/function () {
928
934
  return function (_x15) {
929
935
  return _ref36.apply(this, arguments);
930
936
  };
937
+ }();
938
+
939
+ /**
940
+ * Retrieve the ID of the tenant contract for the specified object
941
+ *
942
+ * @methodGroup Tenant
943
+ * @namedParams
944
+ * @param {string=} contractAddress - The address of the object
945
+ * @param {string=} objectId - The ID of the object
946
+ * @param {string=} versionHash - A version hash of the object
947
+ *
948
+ * @returns {Promise<string|undefined>}
949
+ */
950
+ exports.TenantContractId = /*#__PURE__*/function () {
951
+ var _ref38 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee18(_ref37) {
952
+ var contractAddress, objectId, versionHash, hasGetMetaMethod, tenantContractId, libraryId;
953
+ return _regeneratorRuntime.wrap(function _callee18$(_context18) {
954
+ while (1) switch (_context18.prev = _context18.next) {
955
+ case 0:
956
+ contractAddress = _ref37.contractAddress, objectId = _ref37.objectId, versionHash = _ref37.versionHash;
957
+ if (!contractAddress) {
958
+ _context18.next = 6;
959
+ break;
960
+ }
961
+ ValidateAddress(contractAddress);
962
+ objectId = Utils.AddressToObjectId(contractAddress);
963
+ _context18.next = 18;
964
+ break;
965
+ case 6:
966
+ if (!versionHash) {
967
+ _context18.next = 12;
968
+ break;
969
+ }
970
+ ValidateVersion(versionHash);
971
+ objectId = this.utils.DecodeVersionHash(versionHash).objectId;
972
+ contractAddress = Utils.HashToAddress(objectId);
973
+ _context18.next = 18;
974
+ break;
975
+ case 12:
976
+ if (!objectId) {
977
+ _context18.next = 17;
978
+ break;
979
+ }
980
+ ValidateObject(objectId);
981
+ contractAddress = Utils.HashToAddress(objectId);
982
+ _context18.next = 18;
983
+ break;
984
+ case 17:
985
+ throw Error("contractAddress or objectId or versionHash not specified");
986
+ case 18:
987
+ _context18.next = 20;
988
+ return this.authClient.ContractHasMethod({
989
+ contractAddress: contractAddress,
990
+ methodName: "getMeta"
991
+ });
992
+ case 20:
993
+ hasGetMetaMethod = _context18.sent;
994
+ if (!hasGetMetaMethod) {
995
+ _context18.next = 27;
996
+ break;
997
+ }
998
+ _context18.next = 24;
999
+ return this.ContractMetadata({
1000
+ contractAddress: contractAddress,
1001
+ metadataKey: "_ELV_TENANT_ID"
1002
+ });
1003
+ case 24:
1004
+ tenantContractId = _context18.sent;
1005
+ if (!(tenantContractId !== "")) {
1006
+ _context18.next = 27;
1007
+ break;
1008
+ }
1009
+ return _context18.abrupt("return", tenantContractId);
1010
+ case 27:
1011
+ _context18.next = 29;
1012
+ return this.ContentObjectLibraryId({
1013
+ objectId: objectId
1014
+ });
1015
+ case 29:
1016
+ libraryId = _context18.sent;
1017
+ _context18.next = 32;
1018
+ return this.ContentObjectMetadata({
1019
+ libraryId: libraryId,
1020
+ objectId: objectId,
1021
+ metadataSubtree: "tenantContractId"
1022
+ });
1023
+ case 32:
1024
+ return _context18.abrupt("return", _context18.sent);
1025
+ case 33:
1026
+ case "end":
1027
+ return _context18.stop();
1028
+ }
1029
+ }, _callee18, this);
1030
+ }));
1031
+ return function (_x16) {
1032
+ return _ref38.apply(this, arguments);
1033
+ };
1034
+ }();
1035
+
1036
+ /**
1037
+ * Set the tenant contract ID for the specified object
1038
+ *
1039
+ * @methodGroup Tenant
1040
+ * @namedParams
1041
+ * @param {string=} contractAddress - The address of the object
1042
+ * @param {string=} objectId - The ID of the object
1043
+ * @param {string=} versionHash - A version hash of the object
1044
+ * @param {string} tenantContractId - The tenant contract ID to set
1045
+ *
1046
+ * @returns {Promise<{tenantId: (undefined|string), tenantContractId}>}
1047
+ */
1048
+ exports.SetTenantContractId = /*#__PURE__*/function () {
1049
+ var _ref40 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee19(_ref39) {
1050
+ var contractAddress, objectId, versionHash, tenantContractId, tenantAddress, version, tenantAdminGroupAddress, hasPutMetaMethod, libraryId, editRequest;
1051
+ return _regeneratorRuntime.wrap(function _callee19$(_context19) {
1052
+ while (1) switch (_context19.prev = _context19.next) {
1053
+ case 0:
1054
+ contractAddress = _ref39.contractAddress, objectId = _ref39.objectId, versionHash = _ref39.versionHash, tenantContractId = _ref39.tenantContractId;
1055
+ if (!contractAddress) {
1056
+ _context19.next = 6;
1057
+ break;
1058
+ }
1059
+ ValidateAddress(contractAddress);
1060
+ objectId = Utils.AddressToObjectId(contractAddress);
1061
+ _context19.next = 18;
1062
+ break;
1063
+ case 6:
1064
+ if (!versionHash) {
1065
+ _context19.next = 12;
1066
+ break;
1067
+ }
1068
+ ValidateVersion(versionHash);
1069
+ objectId = this.utils.DecodeVersionHash(versionHash).objectId;
1070
+ contractAddress = Utils.HashToAddress(objectId);
1071
+ _context19.next = 18;
1072
+ break;
1073
+ case 12:
1074
+ if (!objectId) {
1075
+ _context19.next = 17;
1076
+ break;
1077
+ }
1078
+ ValidateObject(objectId);
1079
+ contractAddress = Utils.HashToAddress(objectId);
1080
+ _context19.next = 18;
1081
+ break;
1082
+ case 17:
1083
+ throw Error("contractAddress or objectId or versionHash not specified");
1084
+ case 18:
1085
+ ValidateObject(tenantContractId);
1086
+ if (!(tenantContractId && (!tenantContractId.startsWith("iten") || !Utils.ValidHash(tenantContractId)))) {
1087
+ _context19.next = 21;
1088
+ break;
1089
+ }
1090
+ throw Error("Invalid tenant ID: ".concat(tenantContractId));
1091
+ case 21:
1092
+ tenantAddress = Utils.HashToAddress(tenantContractId);
1093
+ _context19.next = 24;
1094
+ return this.authClient.AccessType(tenantContractId);
1095
+ case 24:
1096
+ version = _context19.sent;
1097
+ if (!(version !== this.authClient.ACCESS_TYPES.TENANT)) {
1098
+ _context19.next = 27;
1099
+ break;
1100
+ }
1101
+ throw Error("Invalid tenant ID: " + tenantContractId);
1102
+ case 27:
1103
+ _context19.next = 29;
1104
+ return this.CallContractMethod({
1105
+ contractAddress: tenantAddress,
1106
+ methodName: "groupsMapping",
1107
+ methodArgs: ["tenant_admin", 0],
1108
+ formatArguments: true
1109
+ });
1110
+ case 29:
1111
+ tenantAdminGroupAddress = _context19.sent;
1112
+ _context19.next = 32;
1113
+ return this.authClient.ContractHasMethod({
1114
+ contractAddress: contractAddress,
1115
+ methodName: "putMeta"
1116
+ });
1117
+ case 32:
1118
+ hasPutMetaMethod = _context19.sent;
1119
+ if (!hasPutMetaMethod) {
1120
+ _context19.next = 44;
1121
+ break;
1122
+ }
1123
+ _context19.next = 36;
1124
+ return this.ReplaceContractMetadata({
1125
+ contractAddress: contractAddress,
1126
+ metadataKey: "_ELV_TENANT_ID",
1127
+ metadata: tenantContractId
1128
+ });
1129
+ case 36:
1130
+ if (!tenantAdminGroupAddress) {
1131
+ _context19.next = 41;
1132
+ break;
1133
+ }
1134
+ _context19.next = 39;
1135
+ return this.ReplaceContractMetadata({
1136
+ contractAddress: contractAddress,
1137
+ metadataKey: "_tenantId",
1138
+ metadata: "iten".concat(Utils.AddressToHash(tenantAdminGroupAddress))
1139
+ });
1140
+ case 39:
1141
+ _context19.next = 42;
1142
+ break;
1143
+ case 41:
1144
+ // eslint-disable-next-line no-console
1145
+ console.warn("No tenant ID associated with current tenant.");
1146
+ case 42:
1147
+ _context19.next = 54;
1148
+ break;
1149
+ case 44:
1150
+ _context19.next = 46;
1151
+ return this.ContentObjectLibraryId({
1152
+ objectId: objectId
1153
+ });
1154
+ case 46:
1155
+ libraryId = _context19.sent;
1156
+ _context19.next = 49;
1157
+ return this.EditContentObject({
1158
+ libraryId: libraryId,
1159
+ objectId: objectId
1160
+ });
1161
+ case 49:
1162
+ editRequest = _context19.sent;
1163
+ _context19.next = 52;
1164
+ return this.MergeMetadata({
1165
+ libraryId: libraryId,
1166
+ objectId: objectId,
1167
+ writeToken: editRequest.write_token,
1168
+ metadata: {
1169
+ tenantContractId: tenantContractId,
1170
+ tenantId: !tenantAdminGroupAddress ? undefined : "iten".concat(Utils.AddressToHash(tenantAdminGroupAddress))
1171
+ }
1172
+ });
1173
+ case 52:
1174
+ _context19.next = 54;
1175
+ return this.FinalizeContentObject({
1176
+ libraryId: libraryId,
1177
+ objectId: objectId,
1178
+ writeToken: editRequest.write_token,
1179
+ commitMessage: "set tenant_contract_id"
1180
+ });
1181
+ case 54:
1182
+ return _context19.abrupt("return", {
1183
+ tenantContractId: tenantContractId,
1184
+ tenantId: !tenantAdminGroupAddress ? undefined : "iten".concat(Utils.AddressToHash(tenantAdminGroupAddress))
1185
+ });
1186
+ case 55:
1187
+ case "end":
1188
+ return _context19.stop();
1189
+ }
1190
+ }, _callee19, this);
1191
+ }));
1192
+ return function (_x17) {
1193
+ return _ref40.apply(this, arguments);
1194
+ };
931
1195
  }();