@trustvc/trustvc 2.9.0 → 2.10.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.
@@ -21,18 +21,12 @@ const getRoleString = /* @__PURE__ */ __name(async (documentStoreAddress, role,
21
21
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
22
22
  provider
23
23
  );
24
- switch (role) {
25
- case "admin":
26
- return await documentStore.DEFAULT_ADMIN_ROLE();
27
- case "issuer":
28
- return await documentStore.ISSUER_ROLE();
29
- case "revoker":
30
- return await documentStore.REVOKER_ROLE();
31
- default:
32
- throw new Error("Invalid role");
24
+ if (typeof documentStore[role] !== "function") {
25
+ throw new Error(`Invalid role: ${role}`);
33
26
  }
27
+ return await documentStore[role]();
34
28
  }, "getRoleString");
35
- const rolesList = ["admin", "issuer", "revoker"];
29
+ const rolesList = ["DEFAULT_ADMIN_ROLE", "ISSUER_ROLE", "REVOKER_ROLE"];
36
30
 
37
31
  exports.getRoleString = getRoleString;
38
32
  exports.rolesList = rolesList;
@@ -6,6 +6,7 @@ var revokeRole = require('./revoke-role');
6
6
  var grantRole = require('./grant-role');
7
7
  var transferOwnership = require('./transferOwnership');
8
8
  var documentStore$1 = require('../deploy/document-store');
9
+ var documentStoreRoles = require('./document-store-roles');
9
10
  var supportInterfaceIds = require('./supportInterfaceIds');
10
11
  var documentStore = require('@trustvc/document-store');
11
12
 
@@ -35,6 +36,10 @@ Object.defineProperty(exports, "deployDocumentStore", {
35
36
  enumerable: true,
36
37
  get: function () { return documentStore$1.deployDocumentStore; }
37
38
  });
39
+ Object.defineProperty(exports, "getRoleString", {
40
+ enumerable: true,
41
+ get: function () { return documentStoreRoles.getRoleString; }
42
+ });
38
43
  Object.defineProperty(exports, "supportInterfaceIds", {
39
44
  enumerable: true,
40
45
  get: function () { return supportInterfaceIds.supportInterfaceIds; }
@@ -3,6 +3,11 @@
3
3
  var revokeRole = require('./revoke-role');
4
4
  var grantRole = require('./grant-role');
5
5
  var documentStoreRoles = require('./document-store-roles');
6
+ var core = require('../core');
7
+ var supportInterfaceIds = require('./supportInterfaceIds');
8
+ var ttDocumentStoreAbi = require('./tt-document-store-abi');
9
+ var ethers = require('../utils/ethers');
10
+ var documentStore = require('@trustvc/document-store');
6
11
 
7
12
  var __defProp = Object.defineProperty;
8
13
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
@@ -11,29 +16,72 @@ const documentStoreTransferOwnership = /* @__PURE__ */ __name(async (documentSto
11
16
  if (!signer.provider) throw new Error("Provider is required");
12
17
  if (!account) throw new Error("Account is required");
13
18
  const ownerAddress = await signer.getAddress();
14
- const roleString = await documentStoreRoles.getRoleString(documentStoreAddress, "admin", {
19
+ const roleString = await documentStoreRoles.getRoleString(documentStoreAddress, "DEFAULT_ADMIN_ROLE", {
15
20
  provider: signer.provider
16
21
  });
17
- const grantTransaction = grantRole.documentStoreGrantRole(
22
+ const Contract = ethers.getEthersContractFromProvider(signer.provider);
23
+ const isDocumentStore = await core.checkSupportsInterface(
24
+ documentStoreAddress,
25
+ supportInterfaceIds.supportInterfaceIds.IDocumentStore,
26
+ signer.provider
27
+ );
28
+ const isTransferableDocumentStore = await core.checkSupportsInterface(
29
+ documentStoreAddress,
30
+ supportInterfaceIds.supportInterfaceIds.ITransferableDocumentStore,
31
+ signer.provider
32
+ );
33
+ let documentStoreAbi;
34
+ if (isDocumentStore || isTransferableDocumentStore) {
35
+ const DocumentStoreFactory = isTransferableDocumentStore ? documentStore.TransferableDocumentStore__factory : documentStore.DocumentStore__factory;
36
+ documentStoreAbi = DocumentStoreFactory.abi;
37
+ } else {
38
+ documentStoreAbi = ttDocumentStoreAbi.TT_DOCUMENT_STORE_ABI;
39
+ }
40
+ const documentStoreContract = new Contract(
41
+ documentStoreAddress,
42
+ documentStoreAbi,
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ signer
45
+ );
46
+ const isV6 = ethers.isV6EthersProvider(signer.provider);
47
+ try {
48
+ if (isV6) {
49
+ await documentStoreContract.grantRole.staticCall(roleString, account);
50
+ } else {
51
+ await documentStoreContract.callStatic.grantRole(roleString, account);
52
+ }
53
+ } catch (e) {
54
+ console.error("callStatic failed:", e);
55
+ throw new Error("Pre-check (callStatic) for grant-role failed");
56
+ }
57
+ try {
58
+ if (isV6) {
59
+ await documentStoreContract.revokeRole.staticCall(roleString, ownerAddress);
60
+ } else {
61
+ await documentStoreContract.callStatic.revokeRole(roleString, ownerAddress);
62
+ }
63
+ } catch (e) {
64
+ console.error("callStatic failed:", e);
65
+ throw new Error("Pre-check (callStatic) for revoke-role failed");
66
+ }
67
+ const grantTransaction = await grantRole.documentStoreGrantRole(
18
68
  documentStoreAddress,
19
69
  roleString,
20
70
  account,
21
71
  signer,
22
72
  options
23
73
  );
24
- const grantTransactionResult = await grantTransaction;
25
- if (!grantTransactionResult) {
74
+ if (!grantTransaction) {
26
75
  throw new Error("Grant transaction failed, not proceeding with revoke transaction");
27
76
  }
28
- const revokeTransaction = revokeRole.documentStoreRevokeRole(
77
+ const revokeTransaction = await revokeRole.documentStoreRevokeRole(
29
78
  documentStoreAddress,
30
79
  roleString,
31
80
  ownerAddress,
32
81
  signer,
33
82
  options
34
83
  );
35
- const revokeTransactionResult = await revokeTransaction;
36
- if (!revokeTransactionResult) {
84
+ if (!revokeTransaction) {
37
85
  throw new Error("Revoke transaction failed");
38
86
  }
39
87
  return { grantTransaction, revokeTransaction };
package/dist/cjs/index.js CHANGED
@@ -110,6 +110,14 @@ Object.defineProperty(exports, "documentStoreRevokeRole", {
110
110
  enumerable: true,
111
111
  get: function () { return documentStore.documentStoreRevokeRole; }
112
112
  });
113
+ Object.defineProperty(exports, "documentStoreTransferOwnership", {
114
+ enumerable: true,
115
+ get: function () { return documentStore.documentStoreTransferOwnership; }
116
+ });
117
+ Object.defineProperty(exports, "getRoleString", {
118
+ enumerable: true,
119
+ get: function () { return documentStore.getRoleString; }
120
+ });
113
121
  Object.keys(tokenRegistryFunctions).forEach(function (k) {
114
122
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
115
123
  enumerable: true,
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var forge = require('node-forge');
4
+ var utils = require('./utils');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var forge__default = /*#__PURE__*/_interopDefault(forge);
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
+ const decryptString = /* @__PURE__ */ __name(({ cipherText, tag, iv, key, type }) => {
13
+ if (type !== utils.ENCRYPTION_PARAMETERS.version) {
14
+ throw new Error(`Expecting version ${utils.ENCRYPTION_PARAMETERS.version} but got ${type}`);
15
+ }
16
+ const keyBytestring = forge__default.default.util.hexToBytes(key);
17
+ const cipherTextBytestring = forge__default.default.util.decode64(cipherText);
18
+ const ivBytestring = forge__default.default.util.decode64(iv);
19
+ const tagBytestring = forge__default.default.util.decode64(tag);
20
+ const decipher = forge__default.default.cipher.createDecipher("AES-GCM", keyBytestring);
21
+ decipher.start({
22
+ iv: ivBytestring,
23
+ tagLength: utils.ENCRYPTION_PARAMETERS.tagLength,
24
+ tag: forge__default.default.util.createBuffer(tagBytestring, "raw")
25
+ });
26
+ decipher.update(forge__default.default.util.createBuffer(cipherTextBytestring, "raw"));
27
+ const success = decipher.finish();
28
+ if (!success) {
29
+ throw new Error("Error decrypting message");
30
+ }
31
+ return utils.decodeDocument(decipher.output.data);
32
+ }, "decryptString");
33
+
34
+ exports.decryptString = decryptString;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ var forge = require('node-forge');
4
+ var utils = require('./utils');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var forge__default = /*#__PURE__*/_interopDefault(forge);
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
+ const generateIv = /* @__PURE__ */ __name((ivLengthInBits = utils.ENCRYPTION_PARAMETERS.ivLength) => {
13
+ const iv = forge__default.default.random.getBytesSync(ivLengthInBits / 8);
14
+ return forge__default.default.util.encode64(iv);
15
+ }, "generateIv");
16
+ const makeCipher = /* @__PURE__ */ __name((encryptionKey = utils.generateEncryptionKey()) => {
17
+ const iv = generateIv();
18
+ const cipher = forge__default.default.cipher.createCipher(
19
+ utils.ENCRYPTION_PARAMETERS.algorithm,
20
+ forge__default.default.util.hexToBytes(encryptionKey)
21
+ );
22
+ cipher.start({
23
+ iv: forge__default.default.util.decode64(iv),
24
+ tagLength: utils.ENCRYPTION_PARAMETERS.tagLength
25
+ });
26
+ return { cipher, encryptionKey, iv };
27
+ }, "makeCipher");
28
+ const encryptString = /* @__PURE__ */ __name((document, key) => {
29
+ if (typeof document !== "string") {
30
+ throw new Error("encryptString only accepts strings");
31
+ }
32
+ const { cipher, encryptionKey, iv } = makeCipher(key);
33
+ const buffer = forge__default.default.util.createBuffer(utils.encodeDocument(document));
34
+ cipher.update(buffer);
35
+ cipher.finish();
36
+ const encryptedMessage = forge__default.default.util.encode64(cipher.output.data);
37
+ const tag = forge__default.default.util.encode64(cipher.mode.tag.data);
38
+ return {
39
+ cipherText: encryptedMessage,
40
+ iv,
41
+ tag,
42
+ key: encryptionKey,
43
+ type: utils.ENCRYPTION_PARAMETERS.version
44
+ };
45
+ }, "encryptString");
46
+
47
+ exports.encryptString = encryptString;
@@ -5,36 +5,46 @@ var types = require('./types');
5
5
  var utils = require('./utils');
6
6
  var verify = require('./verify');
7
7
  var wrap = require('./wrap');
8
+ var encrypt = require('./encrypt');
9
+ var decrypt = require('./decrypt');
8
10
 
9
11
 
10
12
 
13
+ Object.defineProperty(exports, "encryptString", {
14
+ enumerable: true,
15
+ get: function () { return encrypt.encryptString; }
16
+ });
17
+ Object.defineProperty(exports, "decryptString", {
18
+ enumerable: true,
19
+ get: function () { return decrypt.decryptString; }
20
+ });
11
21
  Object.keys(sign).forEach(function (k) {
12
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
13
- enumerable: true,
14
- get: function () { return sign[k]; }
15
- });
22
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
23
+ enumerable: true,
24
+ get: function () { return sign[k]; }
25
+ });
16
26
  });
17
27
  Object.keys(types).forEach(function (k) {
18
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
19
- enumerable: true,
20
- get: function () { return types[k]; }
21
- });
28
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
29
+ enumerable: true,
30
+ get: function () { return types[k]; }
31
+ });
22
32
  });
23
33
  Object.keys(utils).forEach(function (k) {
24
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
25
- enumerable: true,
26
- get: function () { return utils[k]; }
27
- });
34
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
35
+ enumerable: true,
36
+ get: function () { return utils[k]; }
37
+ });
28
38
  });
29
39
  Object.keys(verify).forEach(function (k) {
30
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
31
- enumerable: true,
32
- get: function () { return verify[k]; }
33
- });
40
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
41
+ enumerable: true,
42
+ get: function () { return verify[k]; }
43
+ });
34
44
  });
35
45
  Object.keys(wrap).forEach(function (k) {
36
- if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
37
- enumerable: true,
38
- get: function () { return wrap[k]; }
39
- });
46
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
47
+ enumerable: true,
48
+ get: function () { return wrap[k]; }
49
+ });
40
50
  });
@@ -1,7 +1,46 @@
1
1
  'use strict';
2
2
 
3
+ var forge = require('node-forge');
3
4
  var tradetrust = require('@tradetrust-tt/tradetrust');
4
5
 
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var forge__default = /*#__PURE__*/_interopDefault(forge);
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
+ const ENCRYPTION_PARAMETERS = Object.freeze({
13
+ algorithm: "AES-GCM",
14
+ keyLength: 256,
15
+ // Key length in bits
16
+ ivLength: 96,
17
+ // IV length in bits: NIST suggests 12 bytes
18
+ tagLength: 128,
19
+ // GCM authentication tag length in bits, see link above for explanation
20
+ version: "OPEN-ATTESTATION-TYPE-1"
21
+ // Type 1 using the above params without compression
22
+ });
23
+ const generateEncryptionKey = /* @__PURE__ */ __name((keyLengthInBits = ENCRYPTION_PARAMETERS.keyLength) => {
24
+ if (!Number.isInteger(keyLengthInBits) || ![128, 192, 256].includes(keyLengthInBits)) {
25
+ throw new Error("keyLengthInBits must be one of 128, 192, or 256");
26
+ }
27
+ const encryptionKey = forge__default.default.random.getBytesSync(keyLengthInBits / 8);
28
+ return forge__default.default.util.bytesToHex(encryptionKey);
29
+ }, "generateEncryptionKey");
30
+ const encodeDocument = /* @__PURE__ */ __name((document) => {
31
+ const bytes = forge__default.default.util.encodeUtf8(document);
32
+ const standard = forge__default.default.util.encode64(bytes);
33
+ const s = standard.replace(/\+/g, "-").replace(/\//g, "_");
34
+ const trim = s.endsWith("==") ? 2 : s.endsWith("=") ? 1 : 0;
35
+ return trim ? s.slice(0, -trim) : s;
36
+ }, "encodeDocument");
37
+ const decodeDocument = /* @__PURE__ */ __name((encoded) => {
38
+ let normalized = encoded.replace(/-/g, "+").replace(/_/g, "/");
39
+ const pad = normalized.length % 4;
40
+ if (pad) normalized += "=".repeat(4 - pad);
41
+ const decoded = forge__default.default.util.decode64(normalized);
42
+ return forge__default.default.util.decodeUtf8(decoded);
43
+ }, "decodeDocument");
5
44
  const {
6
45
  isTransferableAsset,
7
46
  isDocumentRevokable,
@@ -42,7 +81,11 @@ Object.defineProperty(exports, "validateSchema", {
42
81
  enumerable: true,
43
82
  get: function () { return tradetrust.validateSchema; }
44
83
  });
84
+ exports.ENCRYPTION_PARAMETERS = ENCRYPTION_PARAMETERS;
85
+ exports.decodeDocument = decodeDocument;
45
86
  exports.diagnose = diagnose;
87
+ exports.encodeDocument = encodeDocument;
88
+ exports.generateEncryptionKey = generateEncryptionKey;
46
89
  exports.getAssetId = getAssetId;
47
90
  exports.getDocumentData = getDocumentData;
48
91
  exports.getIssuerAddress = getIssuerAddress;