@requestnetwork/utils 0.28.1-next.1351 → 0.28.1-next.709
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 +2 -20
- package/dist/amount.d.ts +5 -9
- package/dist/amount.d.ts.map +1 -1
- package/dist/amount.js +19 -23
- package/dist/amount.js.map +1 -1
- package/dist/bignumber.d.ts +7 -0
- package/dist/bignumber.d.ts.map +1 -0
- package/dist/bignumber.js +11 -0
- package/dist/bignumber.js.map +1 -0
- package/dist/cached-throttle.d.ts +2 -2
- package/dist/cached-throttle.d.ts.map +1 -1
- package/dist/cached-throttle.js +5 -3
- package/dist/cached-throttle.js.map +1 -1
- package/dist/crypto/crypto-wrapper.d.ts +1 -8
- package/dist/crypto/crypto-wrapper.d.ts.map +1 -1
- package/dist/crypto/crypto-wrapper.js +19 -31
- package/dist/crypto/crypto-wrapper.js.map +1 -1
- package/dist/crypto/ec-utils.d.ts +6 -14
- package/dist/crypto/ec-utils.d.ts.map +1 -1
- package/dist/crypto/ec-utils.js +77 -41
- package/dist/crypto/ec-utils.js.map +1 -1
- package/dist/crypto.d.ts +14 -26
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +40 -30
- package/dist/crypto.js.map +1 -1
- package/dist/encryption.d.ts +1 -6
- package/dist/encryption.d.ts.map +1 -1
- package/dist/encryption.js +15 -27
- package/dist/encryption.js.map +1 -1
- package/dist/identity.d.ts +4 -9
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +11 -12
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +12 -69
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +66 -28
- package/dist/index.js.map +1 -1
- package/dist/normalize-gas-fees.d.ts +19 -0
- package/dist/normalize-gas-fees.d.ts.map +1 -0
- package/dist/normalize-gas-fees.js +44 -0
- package/dist/normalize-gas-fees.js.map +1 -0
- package/dist/providers.d.ts +40 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +149 -0
- package/dist/providers.js.map +1 -0
- package/dist/retry.d.ts +10 -6
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +15 -16
- package/dist/retry.js.map +1 -1
- package/dist/signature.d.ts +3 -8
- package/dist/signature.d.ts.map +1 -1
- package/dist/signature.js +13 -19
- package/dist/signature.js.map +1 -1
- package/dist/simple-logger.d.ts +1 -1
- package/dist/simple-logger.d.ts.map +1 -1
- package/dist/simple-logger.js +4 -3
- package/dist/simple-logger.js.map +1 -1
- package/dist/utils.d.ts +11 -20
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +20 -21
- package/dist/utils.js.map +1 -1
- package/package.json +18 -26
- package/CHANGELOG.md +0 -1047
package/dist/crypto/ec-utils.js
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
decrypt,
|
|
18
|
-
encrypt,
|
|
19
|
-
getAddressFromPrivateKey,
|
|
20
|
-
getAddressFromPublicKey,
|
|
21
|
-
recover,
|
|
22
|
-
sign,
|
|
23
|
-
};
|
|
3
|
+
exports.ecSign = exports.ecRecover = exports.getAddressFromPublicKey = exports.getAddressFromPrivateKey = exports.ecEncrypt = exports.ecDecrypt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const secp256k1_1 = require("secp256k1");
|
|
6
|
+
const ethers_1 = require("ethers");
|
|
7
|
+
const eccrypto_1 = require("@toruslabs/eccrypto");
|
|
24
8
|
/**
|
|
25
9
|
* Function to derive the address from an EC private key
|
|
26
10
|
*
|
|
@@ -30,17 +14,21 @@ exports.default = {
|
|
|
30
14
|
*/
|
|
31
15
|
function getAddressFromPrivateKey(privateKey) {
|
|
32
16
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
if (!privateKey.match(/^0x/)) {
|
|
18
|
+
privateKey = `0x` + privateKey;
|
|
19
|
+
}
|
|
20
|
+
return ethers_1.ethers.utils.computeAddress(ethers_1.ethers.utils.hexlify(privateKey));
|
|
35
21
|
}
|
|
36
22
|
catch (e) {
|
|
37
23
|
if (e.message === 'private key length is invalid' ||
|
|
38
|
-
e.message === 'Expected private key to be an Uint8Array with length 32'
|
|
24
|
+
e.message === 'Expected private key to be an Uint8Array with length 32' ||
|
|
25
|
+
e.code === 'INVALID_ARGUMENT') {
|
|
39
26
|
throw new Error('The private key must be a string representing 32 bytes');
|
|
40
27
|
}
|
|
41
28
|
throw e;
|
|
42
29
|
}
|
|
43
30
|
}
|
|
31
|
+
exports.getAddressFromPrivateKey = getAddressFromPrivateKey;
|
|
44
32
|
/**
|
|
45
33
|
* Function to derive the address from an EC public key
|
|
46
34
|
*
|
|
@@ -50,35 +38,40 @@ function getAddressFromPrivateKey(privateKey) {
|
|
|
50
38
|
*/
|
|
51
39
|
function getAddressFromPublicKey(publicKey) {
|
|
52
40
|
try {
|
|
53
|
-
return
|
|
41
|
+
return ethers_1.ethers.utils.computeAddress(compressPublicKey(publicKey));
|
|
54
42
|
}
|
|
55
43
|
catch (e) {
|
|
56
44
|
if (e.message === 'public key length is invalid' ||
|
|
57
|
-
e.message === 'Expected public key to be an Uint8Array with length [33, 65]'
|
|
45
|
+
e.message === 'Expected public key to be an Uint8Array with length [33, 65]' ||
|
|
46
|
+
e.code === 'INVALID_ARGUMENT') {
|
|
58
47
|
throw new Error('The public key must be a string representing 64 bytes');
|
|
59
48
|
}
|
|
60
49
|
throw e;
|
|
61
50
|
}
|
|
62
51
|
}
|
|
52
|
+
exports.getAddressFromPublicKey = getAddressFromPublicKey;
|
|
63
53
|
/**
|
|
64
|
-
* Function
|
|
54
|
+
* Function ecSigndata with ECDSA
|
|
65
55
|
*
|
|
66
56
|
* @param data the data to sign
|
|
67
57
|
*
|
|
68
58
|
* @returns the signature
|
|
69
59
|
*/
|
|
70
|
-
function
|
|
60
|
+
function ecSign(privateKey, data) {
|
|
71
61
|
try {
|
|
72
|
-
|
|
62
|
+
const signingKey = new ethers_1.ethers.utils.SigningKey(privateKey);
|
|
63
|
+
return ethers_1.ethers.utils.joinSignature(signingKey.signDigest(data));
|
|
73
64
|
}
|
|
74
65
|
catch (e) {
|
|
75
66
|
if (e.message === 'private key length is invalid' ||
|
|
76
|
-
e.message === 'Expected private key to be an Uint8Array with length 32'
|
|
67
|
+
e.message === 'Expected private key to be an Uint8Array with length 32' ||
|
|
68
|
+
e.code === 'INVALID_ARGUMENT') {
|
|
77
69
|
throw new Error('The private key must be a string representing 32 bytes');
|
|
78
70
|
}
|
|
79
71
|
throw e;
|
|
80
72
|
}
|
|
81
73
|
}
|
|
74
|
+
exports.ecSign = ecSign;
|
|
82
75
|
/**
|
|
83
76
|
* Function to recover address from a signature
|
|
84
77
|
*
|
|
@@ -87,18 +80,26 @@ function sign(privateKey, data) {
|
|
|
87
80
|
*
|
|
88
81
|
* @returns the address
|
|
89
82
|
*/
|
|
90
|
-
function
|
|
83
|
+
function ecRecover(signature, data) {
|
|
91
84
|
try {
|
|
92
|
-
|
|
85
|
+
signature = signature.replace(/^0x/, '');
|
|
86
|
+
data = data.replace(/^0x/, '');
|
|
87
|
+
// split into v-value and sig
|
|
88
|
+
const sigOnly = signature.substring(0, signature.length - 2); // all but last 2 chars
|
|
89
|
+
const vValue = signature.slice(-2); // last 2 chars
|
|
90
|
+
const recoveryNumber = vValue === '1c' ? 1 : 0;
|
|
91
|
+
return ethers_1.ethers.utils.computeAddress(Buffer.from((0, secp256k1_1.ecdsaRecover)(new Uint8Array(Buffer.from(sigOnly, 'hex')), recoveryNumber, new Uint8Array(Buffer.from(data, 'hex')), false)));
|
|
93
92
|
}
|
|
94
93
|
catch (e) {
|
|
95
94
|
if (e.message === 'signature length is invalid' ||
|
|
96
|
-
e.message === 'Expected signature to be an Uint8Array with length 64'
|
|
95
|
+
e.message === 'Expected signature to be an Uint8Array with length 64' ||
|
|
96
|
+
e.code === 'INVALID_ARGUMENT') {
|
|
97
97
|
throw new Error('The signature must be a string representing 66 bytes');
|
|
98
98
|
}
|
|
99
99
|
throw e;
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
+
exports.ecRecover = ecRecover;
|
|
102
103
|
/**
|
|
103
104
|
* Function to encrypt data with a public key
|
|
104
105
|
*
|
|
@@ -107,13 +108,19 @@ function recover(signature, data) {
|
|
|
107
108
|
*
|
|
108
109
|
* @returns the encrypted data
|
|
109
110
|
*/
|
|
110
|
-
function
|
|
111
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
function ecEncrypt(publicKey, data) {
|
|
112
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
112
113
|
try {
|
|
113
|
-
//
|
|
114
|
-
const
|
|
114
|
+
// encrypts the data with the publicKey, returns the encrypted data with encryption parameters (such as IV..)
|
|
115
|
+
const compressed = compressPublicKey(publicKey);
|
|
116
|
+
const encrypted = yield (0, eccrypto_1.encrypt)(Buffer.from(compressed), Buffer.from(data));
|
|
115
117
|
// Transforms the object with the encrypted data into a smaller string-representation.
|
|
116
|
-
return
|
|
118
|
+
return Buffer.concat([
|
|
119
|
+
encrypted.iv,
|
|
120
|
+
(0, secp256k1_1.publicKeyConvert)(encrypted.ephemPublicKey),
|
|
121
|
+
encrypted.mac,
|
|
122
|
+
encrypted.ciphertext,
|
|
123
|
+
]).toString('hex');
|
|
117
124
|
}
|
|
118
125
|
catch (e) {
|
|
119
126
|
if (e.message === 'public key length is invalid' ||
|
|
@@ -124,6 +131,7 @@ function encrypt(publicKey, data) {
|
|
|
124
131
|
}
|
|
125
132
|
});
|
|
126
133
|
}
|
|
134
|
+
exports.ecEncrypt = ecEncrypt;
|
|
127
135
|
/**
|
|
128
136
|
* Function to decrypt data with a public key
|
|
129
137
|
*
|
|
@@ -132,10 +140,11 @@ function encrypt(publicKey, data) {
|
|
|
132
140
|
*
|
|
133
141
|
* @returns the decrypted data
|
|
134
142
|
*/
|
|
135
|
-
function
|
|
136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
function ecDecrypt(privateKey, data) {
|
|
144
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
137
145
|
try {
|
|
138
|
-
|
|
146
|
+
const buf = yield (0, eccrypto_1.decrypt)(Buffer.from(privateKey.replace(/^0x/, ''), 'hex'), eciesSplit(data));
|
|
147
|
+
return buf.toString();
|
|
139
148
|
}
|
|
140
149
|
catch (e) {
|
|
141
150
|
if (e.message === 'Bad private key' ||
|
|
@@ -145,6 +154,7 @@ function decrypt(privateKey, encrypted) {
|
|
|
145
154
|
if (e.message === 'public key length is invalid' ||
|
|
146
155
|
e.message === 'Expected public key to be an Uint8Array with length [33, 65]' ||
|
|
147
156
|
e.message === 'Bad MAC' ||
|
|
157
|
+
e.message === 'bad MAC after trying padded' ||
|
|
148
158
|
e.message === 'the public key could not be parsed or is invalid' ||
|
|
149
159
|
e.message === 'Public Key could not be parsed') {
|
|
150
160
|
throw new Error('The encrypted data is not well formatted');
|
|
@@ -153,4 +163,30 @@ function decrypt(privateKey, encrypted) {
|
|
|
153
163
|
}
|
|
154
164
|
});
|
|
155
165
|
}
|
|
166
|
+
exports.ecDecrypt = ecDecrypt;
|
|
167
|
+
/**
|
|
168
|
+
* Converts a public key to its compressed form.
|
|
169
|
+
*/
|
|
170
|
+
function compressPublicKey(publicKey) {
|
|
171
|
+
publicKey = publicKey.replace(/^0x/, '');
|
|
172
|
+
// if there are more bytes than the key itself, it means there is already a prefix
|
|
173
|
+
if (publicKey.length % 32 === 0) {
|
|
174
|
+
publicKey = `04${publicKey}`;
|
|
175
|
+
}
|
|
176
|
+
return (0, secp256k1_1.publicKeyConvert)(Buffer.from(publicKey, 'hex'));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Split an encrypted string to ECIES params
|
|
180
|
+
* inspired from https://github.com/pubkey/eth-crypto/blob/master/src/ecDecrypt-with-private-key.js
|
|
181
|
+
*/
|
|
182
|
+
const eciesSplit = (str) => {
|
|
183
|
+
const buf = Buffer.from(str, 'hex');
|
|
184
|
+
const ephemPublicKeyStr = buf.toString('hex', 16, 49);
|
|
185
|
+
return {
|
|
186
|
+
iv: Buffer.from(buf.toString('hex', 0, 16), 'hex'),
|
|
187
|
+
mac: Buffer.from(buf.toString('hex', 49, 81), 'hex'),
|
|
188
|
+
ciphertext: Buffer.from(buf.toString('hex', 81, buf.length), 'hex'),
|
|
189
|
+
ephemPublicKey: Buffer.from((0, secp256k1_1.publicKeyConvert)(new Uint8Array(Buffer.from(ephemPublicKeyStr, 'hex')), false)),
|
|
190
|
+
};
|
|
191
|
+
};
|
|
156
192
|
//# sourceMappingURL=ec-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ec-utils.js","sourceRoot":"","sources":["../../src/crypto/ec-utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ec-utils.js","sourceRoot":"","sources":["../../src/crypto/ec-utils.ts"],"names":[],"mappings":";;;;AAAA,yCAA2D;AAC3D,mCAAgC;AAChC,kDAA8D;AAc9D;;;;;;GAMG;AACH,SAAS,wBAAwB,CAAC,UAAkB;IAClD,IAAI;QACF,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAC5B,UAAU,GAAG,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,KAAK,EAAE,EAAE,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC;QACnE,cAAc,EAAE,MAAM,CAAC,IAAI,CACzB,IAAA,4BAAgB,EAAC,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAC/E;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/crypto.d.ts
CHANGED
|
@@ -1,31 +1,10 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { MultiFormatTypes } from '@requestnetwork/types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
decryptWithAes256cbc: (encryptedAndIv: Buffer, key: Buffer) => Promise<Buffer>;
|
|
6
|
-
decryptWithAes256gcm: (encryptedAndIv: Buffer, key: Buffer) => Promise<Buffer>;
|
|
7
|
-
encryptWithAes256cbc: (data: Buffer, key: Buffer) => Promise<Buffer>;
|
|
8
|
-
encryptWithAes256gcm: (data: Buffer, key: Buffer) => Promise<Buffer>;
|
|
9
|
-
random32Bytes: () => Promise<Buffer>;
|
|
10
|
-
};
|
|
11
|
-
EcUtils: {
|
|
12
|
-
decrypt: (privateKey: string, encrypted: string) => Promise<string>;
|
|
13
|
-
encrypt: (publicKey: string, data: string) => Promise<string>;
|
|
14
|
-
getAddressFromPrivateKey: (privateKey: string) => string;
|
|
15
|
-
getAddressFromPublicKey: (publicKey: string) => string;
|
|
16
|
-
recover: (signature: string, data: string) => string;
|
|
17
|
-
sign: (privateKey: string, data: string) => string;
|
|
18
|
-
};
|
|
19
|
-
generate32BufferKey: typeof generate32BufferKey;
|
|
20
|
-
generate8randomBytes: typeof generate8randomBytes;
|
|
21
|
-
keccak256Hash: typeof keccak256Hash;
|
|
22
|
-
normalize: typeof normalize;
|
|
23
|
-
normalizeKeccak256Hash: typeof normalizeKeccak256Hash;
|
|
24
|
-
};
|
|
2
|
+
import { decryptWithAes256cbc, decryptWithAes256gcm, encryptWithAes256cbc, encryptWithAes256gcm, random32Bytes } from './crypto/crypto-wrapper';
|
|
3
|
+
import { ecDecrypt, ecEncrypt, getAddressFromPrivateKey, getAddressFromPublicKey, ecRecover, ecSign } from './crypto/ec-utils';
|
|
25
4
|
/**
|
|
26
5
|
* manages crypto functions
|
|
27
6
|
*/
|
|
28
|
-
export
|
|
7
|
+
export { decryptWithAes256cbc, decryptWithAes256gcm, encryptWithAes256cbc, encryptWithAes256gcm, random32Bytes, ecDecrypt, ecEncrypt, getAddressFromPrivateKey, getAddressFromPublicKey, ecRecover, ecSign, generate32BufferKey, generate8randomBytes, keccak256Hash, last20bytesOfNormalizedKeccak256Hash, normalize, normalizeKeccak256Hash, };
|
|
29
8
|
/**
|
|
30
9
|
* Hashes with the keccak256 algorithm with a normalization before and formats it
|
|
31
10
|
*
|
|
@@ -34,14 +13,14 @@ export default _default;
|
|
|
34
13
|
* @param data The data to hash
|
|
35
14
|
* @returns The hashed data multi-formatted
|
|
36
15
|
*/
|
|
37
|
-
declare function normalizeKeccak256Hash(data:
|
|
16
|
+
declare function normalizeKeccak256Hash(data: unknown): MultiFormatTypes.HashTypes.IHash;
|
|
38
17
|
/**
|
|
39
18
|
* Normalizes data: sorts the object by keys and convert it in string
|
|
40
19
|
*
|
|
41
20
|
* @param data The data to normalize
|
|
42
21
|
* @returns The normalized data
|
|
43
22
|
*/
|
|
44
|
-
declare function normalize(data:
|
|
23
|
+
declare function normalize(data: unknown): string;
|
|
45
24
|
/**
|
|
46
25
|
* Hashes with the keccak256 algorithm
|
|
47
26
|
*
|
|
@@ -49,6 +28,15 @@ declare function normalize(data: any): string;
|
|
|
49
28
|
* @returns The hashed data multi-formatted
|
|
50
29
|
*/
|
|
51
30
|
declare function keccak256Hash(data: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Hashes with the keccak256 algorithm with a normalization before and formats it
|
|
33
|
+
*
|
|
34
|
+
* @notice It will sort the object by keys before hashing
|
|
35
|
+
*
|
|
36
|
+
* @param data The data to hash
|
|
37
|
+
* @returns The hashed data multi-formatted
|
|
38
|
+
*/
|
|
39
|
+
declare function last20bytesOfNormalizedKeccak256Hash(data: unknown): string;
|
|
52
40
|
/**
|
|
53
41
|
* Generates a 32 bytes key in a base64 string
|
|
54
42
|
*
|
package/dist/crypto.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,SAAS,EACT,SAAS,EACT,wBAAwB,EACxB,uBAAuB,EACvB,SAAS,EACT,MAAM,EACP,MAAM,mBAAmB,CAAC;AAG3B;;GAEG;AACH,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,SAAS,EACT,SAAS,EACT,wBAAwB,EACxB,uBAAuB,EACvB,SAAS,EACT,MAAM,EACN,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,oCAAoC,EACpC,SAAS,EACT,sBAAsB,GACvB,CAAC;AAEF;;;;;;;GAOG;AACH,iBAAS,sBAAsB,CAAC,IAAI,EAAE,OAAO,GAAG,gBAAgB,CAAC,SAAS,CAAC,KAAK,CAK/E;AAED;;;;;GAKG;AACH,iBAAS,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAUxC;AAED;;;;;GAKG;AACH,iBAAS,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE3C;AAED;;;;;;;GAOG;AACH,iBAAS,oCAAoC,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAInE;AAED;;;;GAIG;AACH,iBAAe,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAEpD;AAED;;;;;;GAMG;AACH,iBAAe,oBAAoB,IAAI,OAAO,CAAC,MAAM,CAAC,CAGrD"}
|
package/dist/crypto.js
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeKeccak256Hash = exports.normalize = exports.last20bytesOfNormalizedKeccak256Hash = exports.keccak256Hash = exports.generate8randomBytes = exports.generate32BufferKey = exports.ecSign = exports.ecRecover = exports.getAddressFromPublicKey = exports.getAddressFromPrivateKey = exports.ecEncrypt = exports.ecDecrypt = exports.random32Bytes = exports.encryptWithAes256gcm = exports.encryptWithAes256cbc = exports.decryptWithAes256gcm = exports.decryptWithAes256cbc = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
12
5
|
const types_1 = require("@requestnetwork/types");
|
|
13
|
-
const
|
|
6
|
+
const ethers_1 = require("ethers");
|
|
14
7
|
const crypto_wrapper_1 = require("./crypto/crypto-wrapper");
|
|
8
|
+
Object.defineProperty(exports, "decryptWithAes256cbc", { enumerable: true, get: function () { return crypto_wrapper_1.decryptWithAes256cbc; } });
|
|
9
|
+
Object.defineProperty(exports, "decryptWithAes256gcm", { enumerable: true, get: function () { return crypto_wrapper_1.decryptWithAes256gcm; } });
|
|
10
|
+
Object.defineProperty(exports, "encryptWithAes256cbc", { enumerable: true, get: function () { return crypto_wrapper_1.encryptWithAes256cbc; } });
|
|
11
|
+
Object.defineProperty(exports, "encryptWithAes256gcm", { enumerable: true, get: function () { return crypto_wrapper_1.encryptWithAes256gcm; } });
|
|
12
|
+
Object.defineProperty(exports, "random32Bytes", { enumerable: true, get: function () { return crypto_wrapper_1.random32Bytes; } });
|
|
15
13
|
const ec_utils_1 = require("./crypto/ec-utils");
|
|
14
|
+
Object.defineProperty(exports, "ecDecrypt", { enumerable: true, get: function () { return ec_utils_1.ecDecrypt; } });
|
|
15
|
+
Object.defineProperty(exports, "ecEncrypt", { enumerable: true, get: function () { return ec_utils_1.ecEncrypt; } });
|
|
16
|
+
Object.defineProperty(exports, "getAddressFromPrivateKey", { enumerable: true, get: function () { return ec_utils_1.getAddressFromPrivateKey; } });
|
|
17
|
+
Object.defineProperty(exports, "getAddressFromPublicKey", { enumerable: true, get: function () { return ec_utils_1.getAddressFromPublicKey; } });
|
|
18
|
+
Object.defineProperty(exports, "ecRecover", { enumerable: true, get: function () { return ec_utils_1.ecRecover; } });
|
|
19
|
+
Object.defineProperty(exports, "ecSign", { enumerable: true, get: function () { return ec_utils_1.ecSign; } });
|
|
16
20
|
const utils_1 = require("./utils");
|
|
17
|
-
/**
|
|
18
|
-
* manages crypto functions
|
|
19
|
-
*/
|
|
20
|
-
exports.default = {
|
|
21
|
-
CryptoWrapper: crypto_wrapper_1.default,
|
|
22
|
-
EcUtils: ec_utils_1.default,
|
|
23
|
-
generate32BufferKey,
|
|
24
|
-
generate8randomBytes,
|
|
25
|
-
keccak256Hash,
|
|
26
|
-
normalize,
|
|
27
|
-
normalizeKeccak256Hash,
|
|
28
|
-
};
|
|
29
21
|
/**
|
|
30
22
|
* Hashes with the keccak256 algorithm with a normalization before and formats it
|
|
31
23
|
*
|
|
@@ -40,6 +32,7 @@ function normalizeKeccak256Hash(data) {
|
|
|
40
32
|
value: keccak256Hash(normalize(data)),
|
|
41
33
|
};
|
|
42
34
|
}
|
|
35
|
+
exports.normalizeKeccak256Hash = normalizeKeccak256Hash;
|
|
43
36
|
/**
|
|
44
37
|
* Normalizes data: sorts the object by keys and convert it in string
|
|
45
38
|
*
|
|
@@ -51,10 +44,11 @@ function normalize(data) {
|
|
|
51
44
|
return 'undefined';
|
|
52
45
|
}
|
|
53
46
|
// deeply sort data keys
|
|
54
|
-
const sortedData = utils_1.
|
|
47
|
+
const sortedData = (0, utils_1.deepSort)(data);
|
|
55
48
|
// convert to string and lowerCase it, to be case insensitive (e.g: avoid ethereum address casing checksum)
|
|
56
49
|
return JSON.stringify(sortedData).toLowerCase();
|
|
57
50
|
}
|
|
51
|
+
exports.normalize = normalize;
|
|
58
52
|
/**
|
|
59
53
|
* Hashes with the keccak256 algorithm
|
|
60
54
|
*
|
|
@@ -62,19 +56,34 @@ function normalize(data) {
|
|
|
62
56
|
* @returns The hashed data multi-formatted
|
|
63
57
|
*/
|
|
64
58
|
function keccak256Hash(data) {
|
|
65
|
-
return
|
|
59
|
+
return ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.toUtf8Bytes(data));
|
|
60
|
+
}
|
|
61
|
+
exports.keccak256Hash = keccak256Hash;
|
|
62
|
+
/**
|
|
63
|
+
* Hashes with the keccak256 algorithm with a normalization before and formats it
|
|
64
|
+
*
|
|
65
|
+
* @notice It will sort the object by keys before hashing
|
|
66
|
+
*
|
|
67
|
+
* @param data The data to hash
|
|
68
|
+
* @returns The hashed data multi-formatted
|
|
69
|
+
*/
|
|
70
|
+
function last20bytesOfNormalizedKeccak256Hash(data) {
|
|
71
|
+
const hash = keccak256Hash(normalize(data));
|
|
72
|
+
// eslint-disable-next-line no-magic-numbers
|
|
73
|
+
return `0x${hash.slice(-40)}`;
|
|
66
74
|
}
|
|
75
|
+
exports.last20bytesOfNormalizedKeccak256Hash = last20bytesOfNormalizedKeccak256Hash;
|
|
67
76
|
/**
|
|
68
77
|
* Generates a 32 bytes key in a base64 string
|
|
69
78
|
*
|
|
70
79
|
* @returns a random buffer of 32 bytes in a base64 string
|
|
71
80
|
*/
|
|
72
81
|
function generate32BufferKey() {
|
|
73
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
return (yield crypto_wrapper_1.
|
|
82
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
return (yield (0, crypto_wrapper_1.random32Bytes)()).toString('base64');
|
|
75
84
|
});
|
|
76
85
|
}
|
|
77
|
-
|
|
86
|
+
exports.generate32BufferKey = generate32BufferKey;
|
|
78
87
|
/**
|
|
79
88
|
* Generate 8 random bytes and return as a hexadecimal string.
|
|
80
89
|
* Used for salt in ETH input data.
|
|
@@ -83,9 +92,10 @@ function generate32BufferKey() {
|
|
|
83
92
|
* @returns a string of 8 random bytes
|
|
84
93
|
*/
|
|
85
94
|
function generate8randomBytes() {
|
|
86
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
const
|
|
88
|
-
return
|
|
95
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const random32BytesHex = yield (0, crypto_wrapper_1.random32Bytes)();
|
|
97
|
+
return random32BytesHex.slice(0, 8).toString('hex');
|
|
89
98
|
});
|
|
90
99
|
}
|
|
100
|
+
exports.generate8randomBytes = generate8randomBytes;
|
|
91
101
|
//# sourceMappingURL=crypto.js.map
|
package/dist/crypto.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":";;;;AAAA,iDAAyD;AACzD,mCAAgC;AAChC,4DAMiC;AAe/B,qGApBA,qCAAoB,OAoBA;AACpB,qGApBA,qCAAoB,OAoBA;AACpB,qGApBA,qC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A8DtC;;;;GAIG;AACH,SAAe,mBAAmB;;QAChC,OAAO,CAAC,MAAM,IAAA,8BAAa,GAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;CAAA;AAxEC,kDAAmB;AA0ErB;;;;;;GAMG;AACH,SAAe,oBAAoB;;QACjC,MAAM,gBAAgB,GAAG,MAAM,IAAA,8BAAa,GAAE,CAAC;QAC/C,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACtD,CAAC;CAAA;AAnFC,oDAAoB"}
|
package/dist/encryption.d.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
import { EncryptionTypes, IdentityTypes } from '@requestnetwork/types';
|
|
2
|
-
declare const _default: {
|
|
3
|
-
decrypt: typeof decrypt;
|
|
4
|
-
encrypt: typeof encrypt;
|
|
5
|
-
getIdentityFromEncryptionParams: typeof getIdentityFromEncryptionParams;
|
|
6
|
-
};
|
|
7
2
|
/**
|
|
8
3
|
* Functions to manage encryption
|
|
9
4
|
*/
|
|
10
|
-
export
|
|
5
|
+
export { decrypt, encrypt, getIdentityFromEncryptionParams };
|
|
11
6
|
/**
|
|
12
7
|
* Function to get the identity from the encryption parameters
|
|
13
8
|
*
|
package/dist/encryption.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC
|
|
1
|
+
{"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAWvE;;GAEG;AACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,+BAA+B,EAAE,CAAC;AAE7D;;;;;;GAMG;AACH,iBAAS,+BAA+B,CACtC,gBAAgB,EAAE,eAAe,CAAC,qBAAqB,GACtD,aAAa,CAAC,SAAS,CASzB;AAED;;;;;;GAMG;AACH,iBAAe,OAAO,CACpB,IAAI,EAAE,MAAM,EACZ,gBAAgB,EAAE,eAAe,CAAC,qBAAqB,GACtD,OAAO,CAAC,eAAe,CAAC,cAAc,CAAC,CAgCzC;AAED;;;;;;;;GAQG;AACH,iBAAe,OAAO,CACpB,aAAa,EAAE,eAAe,CAAC,cAAc,EAC7C,gBAAgB,EAAE,eAAe,CAAC,qBAAqB,GACtD,OAAO,CAAC,MAAM,CAAC,CAkCjB"}
|
package/dist/encryption.js
CHANGED
|
@@ -1,24 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getIdentityFromEncryptionParams = exports.encrypt = exports.decrypt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
12
5
|
const types_1 = require("@requestnetwork/types");
|
|
13
|
-
const
|
|
14
|
-
/**
|
|
15
|
-
* Functions to manage encryption
|
|
16
|
-
*/
|
|
17
|
-
exports.default = {
|
|
18
|
-
decrypt,
|
|
19
|
-
encrypt,
|
|
20
|
-
getIdentityFromEncryptionParams,
|
|
21
|
-
};
|
|
6
|
+
const index_1 = require("./index");
|
|
22
7
|
/**
|
|
23
8
|
* Function to get the identity from the encryption parameters
|
|
24
9
|
*
|
|
@@ -30,11 +15,12 @@ function getIdentityFromEncryptionParams(encryptionParams) {
|
|
|
30
15
|
if (encryptionParams.method === types_1.EncryptionTypes.METHOD.ECIES) {
|
|
31
16
|
return {
|
|
32
17
|
type: types_1.IdentityTypes.TYPE.ETHEREUM_ADDRESS,
|
|
33
|
-
value:
|
|
18
|
+
value: (0, index_1.getAddressFromPublicKey)(encryptionParams.key),
|
|
34
19
|
};
|
|
35
20
|
}
|
|
36
21
|
throw new Error('encryptionParams.method not supported');
|
|
37
22
|
}
|
|
23
|
+
exports.getIdentityFromEncryptionParams = getIdentityFromEncryptionParams;
|
|
38
24
|
/**
|
|
39
25
|
* Encrypts data using encryption parameters
|
|
40
26
|
*
|
|
@@ -43,23 +29,23 @@ function getIdentityFromEncryptionParams(encryptionParams) {
|
|
|
43
29
|
* @returns the encrypted data
|
|
44
30
|
*/
|
|
45
31
|
function encrypt(data, encryptionParams) {
|
|
46
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
47
33
|
if (encryptionParams.method === types_1.EncryptionTypes.METHOD.ECIES) {
|
|
48
|
-
const encryptedData = yield
|
|
34
|
+
const encryptedData = yield (0, index_1.ecEncrypt)(encryptionParams.key, data);
|
|
49
35
|
return {
|
|
50
36
|
type: types_1.EncryptionTypes.METHOD.ECIES,
|
|
51
37
|
value: encryptedData,
|
|
52
38
|
};
|
|
53
39
|
}
|
|
54
40
|
if (encryptionParams.method === types_1.EncryptionTypes.METHOD.AES256_CBC) {
|
|
55
|
-
const encryptedDataBuffer = yield
|
|
41
|
+
const encryptedDataBuffer = yield (0, index_1.encryptWithAes256cbc)(Buffer.from(data, 'utf-8'), Buffer.from(encryptionParams.key, 'base64'));
|
|
56
42
|
return {
|
|
57
43
|
type: types_1.EncryptionTypes.METHOD.AES256_CBC,
|
|
58
44
|
value: encryptedDataBuffer.toString('base64'),
|
|
59
45
|
};
|
|
60
46
|
}
|
|
61
47
|
if (encryptionParams.method === types_1.EncryptionTypes.METHOD.AES256_GCM) {
|
|
62
|
-
const encryptedDataBuffer = yield
|
|
48
|
+
const encryptedDataBuffer = yield (0, index_1.encryptWithAes256gcm)(Buffer.from(data, 'utf-8'), Buffer.from(encryptionParams.key, 'base64'));
|
|
63
49
|
return {
|
|
64
50
|
type: types_1.EncryptionTypes.METHOD.AES256_GCM,
|
|
65
51
|
value: encryptedDataBuffer.toString('base64'),
|
|
@@ -68,6 +54,7 @@ function encrypt(data, encryptionParams) {
|
|
|
68
54
|
throw new Error('encryptionParams.method not supported');
|
|
69
55
|
});
|
|
70
56
|
}
|
|
57
|
+
exports.encrypt = encrypt;
|
|
71
58
|
/**
|
|
72
59
|
* Decrypt data using decryption parameters
|
|
73
60
|
*
|
|
@@ -78,18 +65,18 @@ function encrypt(data, encryptionParams) {
|
|
|
78
65
|
* @returns the decrypted data
|
|
79
66
|
*/
|
|
80
67
|
function decrypt(encryptedData, decryptionParams) {
|
|
81
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
82
69
|
if (encryptedData.type === types_1.EncryptionTypes.METHOD.ECIES) {
|
|
83
70
|
if (decryptionParams.method !== types_1.EncryptionTypes.METHOD.ECIES) {
|
|
84
71
|
throw new Error(`decryptionParams.method should be ${types_1.EncryptionTypes.METHOD.ECIES}`);
|
|
85
72
|
}
|
|
86
|
-
return
|
|
73
|
+
return (0, index_1.ecDecrypt)(decryptionParams.key, encryptedData.value);
|
|
87
74
|
}
|
|
88
75
|
if (encryptedData.type === types_1.EncryptionTypes.METHOD.AES256_CBC) {
|
|
89
76
|
if (decryptionParams.method !== types_1.EncryptionTypes.METHOD.AES256_CBC) {
|
|
90
77
|
throw new Error(`decryptionParams.method should be ${types_1.EncryptionTypes.METHOD.AES256_CBC}`);
|
|
91
78
|
}
|
|
92
|
-
const dataBuffer = yield
|
|
79
|
+
const dataBuffer = yield (0, index_1.decryptWithAes256cbc)(
|
|
93
80
|
// remove the multi-format padding and decode from the base64 to a buffer
|
|
94
81
|
Buffer.from(encryptedData.value, 'base64'), Buffer.from(decryptionParams.key, 'base64'));
|
|
95
82
|
return dataBuffer.toString();
|
|
@@ -98,7 +85,7 @@ function decrypt(encryptedData, decryptionParams) {
|
|
|
98
85
|
if (decryptionParams.method !== types_1.EncryptionTypes.METHOD.AES256_GCM) {
|
|
99
86
|
throw new Error(`decryptionParams.method should be ${types_1.EncryptionTypes.METHOD.AES256_GCM}`);
|
|
100
87
|
}
|
|
101
|
-
const dataBuffer = yield
|
|
88
|
+
const dataBuffer = yield (0, index_1.decryptWithAes256gcm)(
|
|
102
89
|
// remove the multi-format padding and decode from the base64 to a buffer
|
|
103
90
|
Buffer.from(encryptedData.value, 'base64'), Buffer.from(decryptionParams.key, 'base64'));
|
|
104
91
|
return dataBuffer.toString();
|
|
@@ -106,4 +93,5 @@ function decrypt(encryptedData, decryptionParams) {
|
|
|
106
93
|
throw new Error('encryptedData method not supported');
|
|
107
94
|
});
|
|
108
95
|
}
|
|
96
|
+
exports.decrypt = decrypt;
|
|
109
97
|
//# sourceMappingURL=encryption.js.map
|
package/dist/encryption.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":";;;;AAAA,iDAAuE;AACvE,mCAQiB;AAOjB;;;;;;GAMG;AACH,SAAS,+BAA+B,CACtC,gBAAuD;IAEvD,IAAI,gBAAgB,CAAC,MAAM,KAAK,uBAAe,CAAC,MAAM,CAAC,KAAK,EAAE;QAC5D,OAAO;YACL,IAAI,EAAE,qBAAa,CAAC,IAAI,CAAC,gBAAgB;YACzC,KAAK,EAA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AAqC,uBAAe,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;aAC3F;YAED,MAAM,UAAU,GAAG,MAAM,IAAA,4BAAoB;YAC3C,yEAAyE;YACzE,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,QAAQ,CAAC,EAC1C,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAC5C,CAAC;YACF,OAAO,UAAU,CAAC,QAAQ,EAAE,CAAC;SAC9B;QAED,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,CAAC;CAAA;AAhHQ,0BAAO"}
|
package/dist/identity.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { IdentityTypes } from '@requestnetwork/types';
|
|
2
|
-
declare const
|
|
3
|
-
areEqual: typeof areEqual;
|
|
4
|
-
hasError: typeof hasError;
|
|
5
|
-
normalizeIdentityValue: typeof normalizeIdentityValue;
|
|
6
|
-
supportedIdentities: IdentityTypes.TYPE[];
|
|
7
|
-
};
|
|
2
|
+
declare const supportedIdentities: IdentityTypes.TYPE[];
|
|
8
3
|
/**
|
|
9
4
|
* Module to manage Request Logic Identity
|
|
10
5
|
*/
|
|
11
|
-
export
|
|
6
|
+
export { areEqualIdentities, identityHasError, normalizeIdentityValue, supportedIdentities };
|
|
12
7
|
/**
|
|
13
8
|
* Checks if two identities are equals
|
|
14
9
|
*
|
|
@@ -16,7 +11,7 @@ export default _default;
|
|
|
16
11
|
* @param IIdentity id2 second identity
|
|
17
12
|
* @returns boolean
|
|
18
13
|
*/
|
|
19
|
-
declare function
|
|
14
|
+
declare function areEqualIdentities(id1?: IdentityTypes.IIdentity, id2?: IdentityTypes.IIdentity): boolean;
|
|
20
15
|
/**
|
|
21
16
|
* Normalizes identity values
|
|
22
17
|
*
|
|
@@ -30,5 +25,5 @@ declare function normalizeIdentityValue(value: string): string;
|
|
|
30
25
|
* @param id identity to check
|
|
31
26
|
* @returns the error or null if valid
|
|
32
27
|
*/
|
|
33
|
-
declare function
|
|
28
|
+
declare function identityHasError(id: IdentityTypes.IIdentity): string | null;
|
|
34
29
|
//# sourceMappingURL=identity.d.ts.map
|
package/dist/identity.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,QAAA,MAAM,mBAAmB,EAAE,aAAa,CAAC,IAAI,EAG5C,CAAC;AAEF;;GAEG;AACH,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,CAAC;AAE7F;;;;;;GAMG;AACH,iBAAS,kBAAkB,CAAC,GAAG,CAAC,EAAE,aAAa,CAAC,SAAS,EAAE,GAAG,CAAC,EAAE,aAAa,CAAC,SAAS,GAAG,OAAO,CAOjG;AAED;;;;;GAKG;AACH,iBAAS,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;GAKG;AACH,iBAAS,gBAAgB,CAAC,EAAE,EAAE,aAAa,CAAC,SAAS,GAAG,MAAM,GAAG,IAAI,CAQpE"}
|