@trustvc/trustvc 2.9.1 → 2.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/README.md +54 -0
- package/dist/cjs/__tests__/fixtures/sample-oa-document.json +61 -0
- package/dist/cjs/index.js +11 -0
- package/dist/cjs/open-attestation/decrypt.js +34 -0
- package/dist/cjs/open-attestation/encrypt.js +47 -0
- package/dist/cjs/open-attestation/index.js +30 -20
- package/dist/cjs/open-attestation/utils.js +43 -0
- package/dist/cjs/transaction/cancel.js +57 -0
- package/dist/cjs/transaction/index.js +10 -0
- package/dist/esm/__tests__/fixtures/sample-oa-document.json +61 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/open-attestation/decrypt.js +28 -0
- package/dist/esm/open-attestation/encrypt.js +41 -0
- package/dist/esm/open-attestation/index.js +2 -0
- package/dist/esm/open-attestation/utils.js +36 -1
- package/dist/esm/transaction/cancel.js +55 -0
- package/dist/esm/transaction/index.js +1 -0
- package/dist/types/index.d.ts +5 -2
- package/dist/types/open-attestation/decrypt.d.ts +12 -0
- package/dist/types/open-attestation/encrypt.d.ts +13 -0
- package/dist/types/open-attestation/index.d.ts +4 -2
- package/dist/types/open-attestation/types.d.ts +8 -1
- package/dist/types/open-attestation/utils.d.ts +32 -1
- package/dist/types/transaction/cancel.d.ts +37 -0
- package/dist/types/transaction/index.d.ts +1 -0
- package/package.json +3 -1
package/dist/cjs/index.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var tokenRegistryV4 = require('./token-registry-v4');
|
|
4
4
|
var tokenRegistryV5 = require('./token-registry-v5');
|
|
5
5
|
var documentStore = require('./document-store');
|
|
6
|
+
var transaction = require('./transaction');
|
|
6
7
|
var tokenRegistryFunctions = require('./token-registry-functions');
|
|
7
8
|
var core = require('./core');
|
|
8
9
|
var openAttestation = require('./open-attestation');
|
|
@@ -118,6 +119,16 @@ Object.defineProperty(exports, "getRoleString", {
|
|
|
118
119
|
enumerable: true,
|
|
119
120
|
get: function () { return documentStore.getRoleString; }
|
|
120
121
|
});
|
|
122
|
+
Object.defineProperty(exports, "cancelTransaction", {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
get: function () { return transaction.cancelTransaction; }
|
|
125
|
+
});
|
|
126
|
+
Object.keys(transaction).forEach(function (k) {
|
|
127
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
get: function () { return transaction[k]; }
|
|
130
|
+
});
|
|
131
|
+
});
|
|
121
132
|
Object.keys(tokenRegistryFunctions).forEach(function (k) {
|
|
122
133
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
123
134
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ethers = require('ethers');
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
const GAS_PRICE_SCALE_WHEN_FROM_HASH = 2;
|
|
8
|
+
const cancelTransaction = /* @__PURE__ */ __name(async (signer, options) => {
|
|
9
|
+
if (!signer.provider) {
|
|
10
|
+
throw new Error("Provider is required on signer");
|
|
11
|
+
}
|
|
12
|
+
const { nonce, gasPrice, transactionHash } = options;
|
|
13
|
+
let transactionNonce = nonce;
|
|
14
|
+
let transactionGasPrice = gasPrice;
|
|
15
|
+
if (transactionHash) {
|
|
16
|
+
if (typeof signer.provider.getTransaction !== "function") {
|
|
17
|
+
throw new Error("Provider does not support getTransaction");
|
|
18
|
+
}
|
|
19
|
+
const tx = await signer.provider.getTransaction(transactionHash);
|
|
20
|
+
if (!tx) {
|
|
21
|
+
throw new Error(`Transaction not found: ${transactionHash}`);
|
|
22
|
+
}
|
|
23
|
+
const txNonce = tx.nonce;
|
|
24
|
+
const txGasPrice = tx.gasPrice;
|
|
25
|
+
if (txGasPrice == null) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Transaction uses EIP-1559 (no gasPrice). Cancel by providing --nonce and --gas-price explicitly."
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
transactionNonce = String(txNonce);
|
|
31
|
+
const scaled = typeof txGasPrice === "bigint" ? txGasPrice * BigInt(GAS_PRICE_SCALE_WHEN_FROM_HASH) : ethers.BigNumber.from(txGasPrice).mul(GAS_PRICE_SCALE_WHEN_FROM_HASH);
|
|
32
|
+
transactionGasPrice = String(scaled);
|
|
33
|
+
}
|
|
34
|
+
if (!transactionNonce || !transactionGasPrice) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
"Provide either (--nonce and --gas-price) or --transaction-hash to cancel the pending transaction"
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const address = await signer.getAddress();
|
|
40
|
+
if (!/^\d+$/.test(transactionNonce)) {
|
|
41
|
+
throw new Error("Invalid nonce: expected a non-negative integer");
|
|
42
|
+
}
|
|
43
|
+
if (!/^\d+$/.test(transactionGasPrice) || transactionGasPrice === "0") {
|
|
44
|
+
throw new Error("Invalid gasPrice: expected a positive integer string in wei");
|
|
45
|
+
}
|
|
46
|
+
const nonceNum = Number(transactionNonce);
|
|
47
|
+
const txResponse = await signer.sendTransaction({
|
|
48
|
+
to: address,
|
|
49
|
+
value: 0,
|
|
50
|
+
nonce: nonceNum,
|
|
51
|
+
gasPrice: transactionGasPrice,
|
|
52
|
+
gasLimit: 21e3
|
|
53
|
+
});
|
|
54
|
+
return txResponse.hash;
|
|
55
|
+
}, "cancelTransaction");
|
|
56
|
+
|
|
57
|
+
exports.cancelTransaction = cancelTransaction;
|