eth-crypto-ts 0.0.1 → 0.0.2
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/dist/constants/index.d.ts +2 -0
- package/dist/index.cjs.js +212 -0
- package/dist/index.cjs.js.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +2 -2
- package/dist/index.esm.js +195 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +195 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/cipher/index.d.ts +11 -0
- package/dist/lib/compress-public-key.d.ts +1 -0
- package/dist/lib/create-identity.d.ts +16 -0
- package/dist/lib/decompress-public-key.d.ts +1 -0
- package/dist/lib/decrypt-with-private-key.d.ts +2 -0
- package/dist/lib/encrypt-with-private-key.d.ts +2 -0
- package/dist/lib/hash/index.d.ts +4 -0
- package/dist/lib/public-key-by-private-key.d.ts +8 -0
- package/dist/lib/recover-public-key.d.ts +7 -0
- package/dist/lib/sign.d.ts +8 -0
- package/dist/lib/utils.d.ts +4 -0
- package/dist/types/index.d.ts +10 -0
- package/package.json +16 -2
- package/.eslintrc.js +0 -21
- package/.github/workflows/cd.yml +0 -43
- package/.github/workflows/ci.yml +0 -37
- package/.nvmrc +0 -1
- package/.prettierrc +0 -7
- package/.releaserc.json +0 -22
- package/audit-ci.jsonc +0 -7
- package/rollup.config.js +0 -76
- package/src/constants/index.ts +0 -3
- package/src/lib/cipher/index.ts +0 -46
- package/src/lib/compress-public-key.ts +0 -10
- package/src/lib/create-identity.ts +0 -41
- package/src/lib/decompress-public-key.ts +0 -14
- package/src/lib/decrypt-with-private-key.ts +0 -23
- package/src/lib/encrypt-with-private-key.ts +0 -23
- package/src/lib/hash/index.ts +0 -20
- package/src/lib/public-key-by-private-key.ts +0 -18
- package/src/lib/recover-public-key.ts +0 -30
- package/src/lib/sign.ts +0 -24
- package/src/lib/utils.ts +0 -19
- package/src/types/index.ts +0 -11
- package/tsconfig.json +0 -25
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var secp256k1 = require('secp256k1');
|
|
6
|
+
var ethers = require('ethers');
|
|
7
|
+
var eccrypto = require('eccrypto');
|
|
8
|
+
var ethereumjsUtil = require('ethereumjs-util');
|
|
9
|
+
|
|
10
|
+
function removeLeading0x(str) {
|
|
11
|
+
if (str.startsWith('0x'))
|
|
12
|
+
return str.substring(2);
|
|
13
|
+
else
|
|
14
|
+
return str;
|
|
15
|
+
}
|
|
16
|
+
function addLeading0x(str) {
|
|
17
|
+
if (!str.startsWith('0x'))
|
|
18
|
+
return '0x' + str;
|
|
19
|
+
else
|
|
20
|
+
return str;
|
|
21
|
+
}
|
|
22
|
+
function uint8ArrayToHex(arr) {
|
|
23
|
+
return Buffer.from(arr).toString('hex');
|
|
24
|
+
}
|
|
25
|
+
function hexToUnit8Array(str) {
|
|
26
|
+
return new Uint8Array(Buffer.from(str, 'hex'));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function compress(startsWith04) {
|
|
30
|
+
// add trailing 04 if not done before
|
|
31
|
+
const testBuffer = Buffer.from(startsWith04, 'hex');
|
|
32
|
+
if (testBuffer.length === 64)
|
|
33
|
+
startsWith04 = '04' + startsWith04;
|
|
34
|
+
return uint8ArrayToHex(secp256k1.publicKeyConvert(hexToUnit8Array(startsWith04), true));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function decompress(startsWith02Or03) {
|
|
38
|
+
// if already decompressed an not has trailing 04
|
|
39
|
+
const testBuffer = Buffer.from(startsWith02Or03, 'hex');
|
|
40
|
+
if (testBuffer.length === 64)
|
|
41
|
+
startsWith02Or03 = '04' + startsWith02Or03;
|
|
42
|
+
let decompressed = uint8ArrayToHex(secp256k1.publicKeyConvert(hexToUnit8Array(startsWith02Or03), false));
|
|
43
|
+
// remove trailing 04
|
|
44
|
+
decompressed = decompressed.substring(2);
|
|
45
|
+
return decompressed;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class Cipher {
|
|
49
|
+
constructor() { }
|
|
50
|
+
stringify(encrypted) {
|
|
51
|
+
if (typeof encrypted === 'string')
|
|
52
|
+
return encrypted;
|
|
53
|
+
// use compressed key because it's smaller
|
|
54
|
+
const compressedKey = compress(encrypted.ephemPublicKey);
|
|
55
|
+
const ret = Buffer.concat([
|
|
56
|
+
Buffer.from(encrypted.iv, 'hex'), // 16bit
|
|
57
|
+
Buffer.from(compressedKey, 'hex'), // 33bit
|
|
58
|
+
Buffer.from(encrypted.mac, 'hex'), // 32bit
|
|
59
|
+
Buffer.from(encrypted.ciphertext, 'hex'), // var bit
|
|
60
|
+
]);
|
|
61
|
+
return ret.toString('hex');
|
|
62
|
+
}
|
|
63
|
+
parse(str) {
|
|
64
|
+
if (typeof str !== 'string')
|
|
65
|
+
return str;
|
|
66
|
+
const buf = Buffer.from(str, 'hex');
|
|
67
|
+
const ret = {
|
|
68
|
+
iv: buf.toString('hex', 0, 16),
|
|
69
|
+
ephemPublicKey: buf.toString('hex', 16, 49),
|
|
70
|
+
mac: buf.toString('hex', 49, 81),
|
|
71
|
+
ciphertext: buf.toString('hex', 81, buf.length),
|
|
72
|
+
};
|
|
73
|
+
// decompress publicKey
|
|
74
|
+
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey);
|
|
75
|
+
return ret;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class Hash {
|
|
80
|
+
constructor() { }
|
|
81
|
+
keccak256(params) {
|
|
82
|
+
const types = [];
|
|
83
|
+
const values = [];
|
|
84
|
+
if (!Array.isArray(params)) {
|
|
85
|
+
types.push('string');
|
|
86
|
+
values.push(params);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
params.forEach((p) => {
|
|
90
|
+
types.push(p.type);
|
|
91
|
+
values.push(p.value);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return ethers.solidityPackedKeccak256(types, values);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* signs the given message
|
|
100
|
+
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
|
|
101
|
+
* @param {string} privateKey
|
|
102
|
+
* @param {string} hash
|
|
103
|
+
* @return {string} hexString
|
|
104
|
+
*/
|
|
105
|
+
function sign(privateKey, hash) {
|
|
106
|
+
hash = addLeading0x(hash);
|
|
107
|
+
if (hash.length !== 66)
|
|
108
|
+
throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);
|
|
109
|
+
const sigObj = secp256k1.ecdsaSign(new Uint8Array(Buffer.from(removeLeading0x(hash), 'hex')), new Uint8Array(Buffer.from(removeLeading0x(privateKey), 'hex')));
|
|
110
|
+
const recoveryId = sigObj.recid === 1 ? '1c' : '1b';
|
|
111
|
+
const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId;
|
|
112
|
+
return newSignature;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function encryptWithPublicKey(publicKey, message, options) {
|
|
116
|
+
// ensure its an uncompressed publicKey
|
|
117
|
+
publicKey = decompress(publicKey);
|
|
118
|
+
// re-add the compression-flag
|
|
119
|
+
const pubString = '04' + publicKey;
|
|
120
|
+
return eccrypto.encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), options ? options : {}).then((encryptedBuffers) => {
|
|
121
|
+
const encrypted = {
|
|
122
|
+
iv: encryptedBuffers.iv.toString('hex'),
|
|
123
|
+
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'),
|
|
124
|
+
ciphertext: encryptedBuffers.ciphertext.toString('hex'),
|
|
125
|
+
mac: encryptedBuffers.mac.toString('hex'),
|
|
126
|
+
};
|
|
127
|
+
return encrypted;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function decryptWithPrivateKey(privateKey, encrypted) {
|
|
132
|
+
const cipher = new Cipher();
|
|
133
|
+
// ensuring the encrypted data is in the correct format.
|
|
134
|
+
encrypted = cipher.parse(encrypted);
|
|
135
|
+
// remove trailing '0x' from privateKey
|
|
136
|
+
const twoStripped = removeLeading0x(privateKey);
|
|
137
|
+
const encryptedBuffer = {
|
|
138
|
+
iv: Buffer.from(encrypted.iv, 'hex'),
|
|
139
|
+
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'),
|
|
140
|
+
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'),
|
|
141
|
+
mac: Buffer.from(encrypted.mac, 'hex'),
|
|
142
|
+
};
|
|
143
|
+
return eccrypto.decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then((decryptedBuffer) => decryptedBuffer.toString());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Generate publicKey from the privateKey.
|
|
148
|
+
* This creates the uncompressed publicKey,
|
|
149
|
+
* where 04 has stripped from left
|
|
150
|
+
* @param {string} privateKey
|
|
151
|
+
* @returns {string}
|
|
152
|
+
*/
|
|
153
|
+
function publicKeyByPrivateKey(privateKey) {
|
|
154
|
+
privateKey = addLeading0x(privateKey);
|
|
155
|
+
const publicKeyBuffer = ethereumjsUtil.privateToPublic(ethereumjsUtil.toBuffer(privateKey));
|
|
156
|
+
return publicKeyBuffer.toString('hex');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const MIN_ENTROPY_SIZE = 128;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* create a privateKey from the given entropy or a new one
|
|
163
|
+
* @param {Buffer} entropy
|
|
164
|
+
* @return {string}
|
|
165
|
+
*/
|
|
166
|
+
function createPrivateKey(entropy) {
|
|
167
|
+
if (entropy) {
|
|
168
|
+
if (!Buffer.isBuffer(entropy))
|
|
169
|
+
throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer');
|
|
170
|
+
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE)
|
|
171
|
+
throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE);
|
|
172
|
+
const outerHex = ethers.keccak256(entropy);
|
|
173
|
+
return outerHex;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
const innerHex = ethers.keccak256(ethers.concat([ethers.randomBytes(32), ethers.randomBytes(32)]));
|
|
177
|
+
const middleHex = ethers.concat([ethers.concat([ethers.randomBytes(32), innerHex]), ethers.randomBytes(32)]);
|
|
178
|
+
const outerHex = ethers.keccak256(middleHex);
|
|
179
|
+
return outerHex;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* creates a new object with
|
|
184
|
+
* private-, public-Key and address
|
|
185
|
+
* @param {Buffer?} entropy if provided, will use that as single random-source
|
|
186
|
+
*/
|
|
187
|
+
function createIdentity(entropy) {
|
|
188
|
+
const privateKey = createPrivateKey(entropy);
|
|
189
|
+
const wallet = new ethers.Wallet(privateKey);
|
|
190
|
+
const identity = {
|
|
191
|
+
privateKey: privateKey,
|
|
192
|
+
publicKey: publicKeyByPrivateKey(privateKey),
|
|
193
|
+
address: wallet.address,
|
|
194
|
+
};
|
|
195
|
+
return identity;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
exports.Cipher = Cipher;
|
|
199
|
+
exports.Hash = Hash;
|
|
200
|
+
exports.addLeading0x = addLeading0x;
|
|
201
|
+
exports.compress = compress;
|
|
202
|
+
exports.createIdentity = createIdentity;
|
|
203
|
+
exports.createPrivateKey = createPrivateKey;
|
|
204
|
+
exports.decompress = decompress;
|
|
205
|
+
exports.decryptWithPrivateKey = decryptWithPrivateKey;
|
|
206
|
+
exports.encryptWithPublicKey = encryptWithPublicKey;
|
|
207
|
+
exports.hexToUnit8Array = hexToUnit8Array;
|
|
208
|
+
exports.publicKeyByPrivateKey = publicKeyByPrivateKey;
|
|
209
|
+
exports.removeLeading0x = removeLeading0x;
|
|
210
|
+
exports.sign = sign;
|
|
211
|
+
exports.uint8ArrayToHex = uint8ArrayToHex;
|
|
212
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/lib/utils.ts","../src/lib/compress-public-key.ts","../src/lib/decompress-public-key.ts","../src/lib/cipher/index.ts","../src/lib/hash/index.ts","../src/lib/sign.ts","../src/lib/encrypt-with-private-key.ts","../src/lib/decrypt-with-private-key.ts","../src/lib/public-key-by-private-key.ts","../src/constants/index.ts","../src/lib/create-identity.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null],"names":["publicKeyConvert","solidityPackedKeccak256","secp256k1_sign","encrypt","decrypt","privateToPublic","toBuffer","keccak256","concat","randomBytes","Wallet"],"mappings":";;;;;;;;;AAAM,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;AACpB,QAAA,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;;AACvB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,YAAY,CAAC,GAAW,EAAA;AACtC,IAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;QACrB,OAAO,IAAI,GAAG,GAAG,CAAC;;AACjB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,eAAe,CAAC,GAAuB,EAAA;IACrD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC1C,CAAC;AAEK,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AACjD;;ACfM,SAAU,QAAQ,CAAC,YAAiB,EAAA;;IAExC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC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,CAAC;AAC7C,IAAA,MAAM,MAAM,GAAG,IAAIG,aAAM,CAAC,UAAU,CAAC,CAAC;AACtC,IAAA,MAAM,QAAQ,GAAG;AACf,QAAA,UAAU,EAAE,UAAU;AACtB,QAAA,SAAS,EAAE,qBAAqB,CAAC,UAAU,CAAC;QAC5C,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;AAEF,IAAA,OAAO,QAAQ,CAAC;AAClB;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export * from './lib/cipher';
|
|
2
2
|
export * from './lib/hash';
|
|
3
3
|
export * from './lib/sign';
|
|
4
|
-
export * from './lib/utils'
|
|
4
|
+
export * from './lib/utils';
|
|
5
5
|
export * from './lib/encrypt-with-private-key';
|
|
6
6
|
export * from './lib/decrypt-with-private-key';
|
|
7
7
|
export * from './lib/compress-public-key';
|
|
8
8
|
export * from './lib/decompress-public-key';
|
|
9
9
|
export * from './lib/create-identity';
|
|
10
|
-
export * from './lib/public-key-by-private-key';
|
|
10
|
+
export * from './lib/public-key-by-private-key';
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { publicKeyConvert, ecdsaSign } from 'secp256k1';
|
|
2
|
+
import { solidityPackedKeccak256, keccak256, concat, randomBytes, Wallet } from 'ethers';
|
|
3
|
+
import { encrypt, decrypt } from 'eccrypto';
|
|
4
|
+
import { privateToPublic, toBuffer } from 'ethereumjs-util';
|
|
5
|
+
|
|
6
|
+
function removeLeading0x(str) {
|
|
7
|
+
if (str.startsWith('0x'))
|
|
8
|
+
return str.substring(2);
|
|
9
|
+
else
|
|
10
|
+
return str;
|
|
11
|
+
}
|
|
12
|
+
function addLeading0x(str) {
|
|
13
|
+
if (!str.startsWith('0x'))
|
|
14
|
+
return '0x' + str;
|
|
15
|
+
else
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
function uint8ArrayToHex(arr) {
|
|
19
|
+
return Buffer.from(arr).toString('hex');
|
|
20
|
+
}
|
|
21
|
+
function hexToUnit8Array(str) {
|
|
22
|
+
return new Uint8Array(Buffer.from(str, 'hex'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function compress(startsWith04) {
|
|
26
|
+
// add trailing 04 if not done before
|
|
27
|
+
const testBuffer = Buffer.from(startsWith04, 'hex');
|
|
28
|
+
if (testBuffer.length === 64)
|
|
29
|
+
startsWith04 = '04' + startsWith04;
|
|
30
|
+
return uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith04), true));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function decompress(startsWith02Or03) {
|
|
34
|
+
// if already decompressed an not has trailing 04
|
|
35
|
+
const testBuffer = Buffer.from(startsWith02Or03, 'hex');
|
|
36
|
+
if (testBuffer.length === 64)
|
|
37
|
+
startsWith02Or03 = '04' + startsWith02Or03;
|
|
38
|
+
let decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false));
|
|
39
|
+
// remove trailing 04
|
|
40
|
+
decompressed = decompressed.substring(2);
|
|
41
|
+
return decompressed;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class Cipher {
|
|
45
|
+
constructor() { }
|
|
46
|
+
stringify(encrypted) {
|
|
47
|
+
if (typeof encrypted === 'string')
|
|
48
|
+
return encrypted;
|
|
49
|
+
// use compressed key because it's smaller
|
|
50
|
+
const compressedKey = compress(encrypted.ephemPublicKey);
|
|
51
|
+
const ret = Buffer.concat([
|
|
52
|
+
Buffer.from(encrypted.iv, 'hex'), // 16bit
|
|
53
|
+
Buffer.from(compressedKey, 'hex'), // 33bit
|
|
54
|
+
Buffer.from(encrypted.mac, 'hex'), // 32bit
|
|
55
|
+
Buffer.from(encrypted.ciphertext, 'hex'), // var bit
|
|
56
|
+
]);
|
|
57
|
+
return ret.toString('hex');
|
|
58
|
+
}
|
|
59
|
+
parse(str) {
|
|
60
|
+
if (typeof str !== 'string')
|
|
61
|
+
return str;
|
|
62
|
+
const buf = Buffer.from(str, 'hex');
|
|
63
|
+
const ret = {
|
|
64
|
+
iv: buf.toString('hex', 0, 16),
|
|
65
|
+
ephemPublicKey: buf.toString('hex', 16, 49),
|
|
66
|
+
mac: buf.toString('hex', 49, 81),
|
|
67
|
+
ciphertext: buf.toString('hex', 81, buf.length),
|
|
68
|
+
};
|
|
69
|
+
// decompress publicKey
|
|
70
|
+
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey);
|
|
71
|
+
return ret;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class Hash {
|
|
76
|
+
constructor() { }
|
|
77
|
+
keccak256(params) {
|
|
78
|
+
const types = [];
|
|
79
|
+
const values = [];
|
|
80
|
+
if (!Array.isArray(params)) {
|
|
81
|
+
types.push('string');
|
|
82
|
+
values.push(params);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
params.forEach((p) => {
|
|
86
|
+
types.push(p.type);
|
|
87
|
+
values.push(p.value);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return solidityPackedKeccak256(types, values);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* signs the given message
|
|
96
|
+
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
|
|
97
|
+
* @param {string} privateKey
|
|
98
|
+
* @param {string} hash
|
|
99
|
+
* @return {string} hexString
|
|
100
|
+
*/
|
|
101
|
+
function sign(privateKey, hash) {
|
|
102
|
+
hash = addLeading0x(hash);
|
|
103
|
+
if (hash.length !== 66)
|
|
104
|
+
throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);
|
|
105
|
+
const sigObj = ecdsaSign(new Uint8Array(Buffer.from(removeLeading0x(hash), 'hex')), new Uint8Array(Buffer.from(removeLeading0x(privateKey), 'hex')));
|
|
106
|
+
const recoveryId = sigObj.recid === 1 ? '1c' : '1b';
|
|
107
|
+
const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId;
|
|
108
|
+
return newSignature;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function encryptWithPublicKey(publicKey, message, options) {
|
|
112
|
+
// ensure its an uncompressed publicKey
|
|
113
|
+
publicKey = decompress(publicKey);
|
|
114
|
+
// re-add the compression-flag
|
|
115
|
+
const pubString = '04' + publicKey;
|
|
116
|
+
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), options ? options : {}).then((encryptedBuffers) => {
|
|
117
|
+
const encrypted = {
|
|
118
|
+
iv: encryptedBuffers.iv.toString('hex'),
|
|
119
|
+
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'),
|
|
120
|
+
ciphertext: encryptedBuffers.ciphertext.toString('hex'),
|
|
121
|
+
mac: encryptedBuffers.mac.toString('hex'),
|
|
122
|
+
};
|
|
123
|
+
return encrypted;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function decryptWithPrivateKey(privateKey, encrypted) {
|
|
128
|
+
const cipher = new Cipher();
|
|
129
|
+
// ensuring the encrypted data is in the correct format.
|
|
130
|
+
encrypted = cipher.parse(encrypted);
|
|
131
|
+
// remove trailing '0x' from privateKey
|
|
132
|
+
const twoStripped = removeLeading0x(privateKey);
|
|
133
|
+
const encryptedBuffer = {
|
|
134
|
+
iv: Buffer.from(encrypted.iv, 'hex'),
|
|
135
|
+
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'),
|
|
136
|
+
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'),
|
|
137
|
+
mac: Buffer.from(encrypted.mac, 'hex'),
|
|
138
|
+
};
|
|
139
|
+
return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then((decryptedBuffer) => decryptedBuffer.toString());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Generate publicKey from the privateKey.
|
|
144
|
+
* This creates the uncompressed publicKey,
|
|
145
|
+
* where 04 has stripped from left
|
|
146
|
+
* @param {string} privateKey
|
|
147
|
+
* @returns {string}
|
|
148
|
+
*/
|
|
149
|
+
function publicKeyByPrivateKey(privateKey) {
|
|
150
|
+
privateKey = addLeading0x(privateKey);
|
|
151
|
+
const publicKeyBuffer = privateToPublic(toBuffer(privateKey));
|
|
152
|
+
return publicKeyBuffer.toString('hex');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const MIN_ENTROPY_SIZE = 128;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* create a privateKey from the given entropy or a new one
|
|
159
|
+
* @param {Buffer} entropy
|
|
160
|
+
* @return {string}
|
|
161
|
+
*/
|
|
162
|
+
function createPrivateKey(entropy) {
|
|
163
|
+
if (entropy) {
|
|
164
|
+
if (!Buffer.isBuffer(entropy))
|
|
165
|
+
throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer');
|
|
166
|
+
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE)
|
|
167
|
+
throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE);
|
|
168
|
+
const outerHex = keccak256(entropy);
|
|
169
|
+
return outerHex;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const innerHex = keccak256(concat([randomBytes(32), randomBytes(32)]));
|
|
173
|
+
const middleHex = concat([concat([randomBytes(32), innerHex]), randomBytes(32)]);
|
|
174
|
+
const outerHex = keccak256(middleHex);
|
|
175
|
+
return outerHex;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* creates a new object with
|
|
180
|
+
* private-, public-Key and address
|
|
181
|
+
* @param {Buffer?} entropy if provided, will use that as single random-source
|
|
182
|
+
*/
|
|
183
|
+
function createIdentity(entropy) {
|
|
184
|
+
const privateKey = createPrivateKey(entropy);
|
|
185
|
+
const wallet = new Wallet(privateKey);
|
|
186
|
+
const identity = {
|
|
187
|
+
privateKey: privateKey,
|
|
188
|
+
publicKey: publicKeyByPrivateKey(privateKey),
|
|
189
|
+
address: wallet.address,
|
|
190
|
+
};
|
|
191
|
+
return identity;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { Cipher, Hash, addLeading0x, compress, createIdentity, createPrivateKey, decompress, decryptWithPrivateKey, encryptWithPublicKey, hexToUnit8Array, publicKeyByPrivateKey, removeLeading0x, sign, uint8ArrayToHex };
|
|
195
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/lib/utils.ts","../src/lib/compress-public-key.ts","../src/lib/decompress-public-key.ts","../src/lib/cipher/index.ts","../src/lib/hash/index.ts","../src/lib/sign.ts","../src/lib/encrypt-with-private-key.ts","../src/lib/decrypt-with-private-key.ts","../src/lib/public-key-by-private-key.ts","../src/constants/index.ts","../src/lib/create-identity.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null],"names":["secp256k1_sign"],"mappings":";;;;;AAAM,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;AACpB,QAAA,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;;AACvB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,YAAY,CAAC,GAAW,EAAA;AACtC,IAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;QACrB,OAAO,IAAI,GAAG,GAAG,CAAC;;AACjB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,eAAe,CAAC,GAAuB,EAAA;IACrD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC1C,CAAC;AAEK,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC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AAIG;AACG,SAAU,cAAc,CAAC,OAAgB,EAAA;AAC7C,IAAA,MAAM,UAAU,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AAC7C,IAAA,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;AACtC,IAAA,MAAM,QAAQ,GAAG;AACf,QAAA,UAAU,EAAE,UAAU;AACtB,QAAA,SAAS,EAAE,qBAAqB,CAAC,UAAU,CAAC;QAC5C,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;AAEF,IAAA,OAAO,QAAQ,CAAC;AAClB;;;;"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { publicKeyConvert, ecdsaSign } from 'secp256k1';
|
|
2
|
+
import { solidityPackedKeccak256, keccak256, concat, randomBytes, Wallet } from 'ethers';
|
|
3
|
+
import { encrypt, decrypt } from 'eccrypto';
|
|
4
|
+
import { privateToPublic, toBuffer } from 'ethereumjs-util';
|
|
5
|
+
|
|
6
|
+
function removeLeading0x(str) {
|
|
7
|
+
if (str.startsWith('0x'))
|
|
8
|
+
return str.substring(2);
|
|
9
|
+
else
|
|
10
|
+
return str;
|
|
11
|
+
}
|
|
12
|
+
function addLeading0x(str) {
|
|
13
|
+
if (!str.startsWith('0x'))
|
|
14
|
+
return '0x' + str;
|
|
15
|
+
else
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
function uint8ArrayToHex(arr) {
|
|
19
|
+
return Buffer.from(arr).toString('hex');
|
|
20
|
+
}
|
|
21
|
+
function hexToUnit8Array(str) {
|
|
22
|
+
return new Uint8Array(Buffer.from(str, 'hex'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function compress(startsWith04) {
|
|
26
|
+
// add trailing 04 if not done before
|
|
27
|
+
const testBuffer = Buffer.from(startsWith04, 'hex');
|
|
28
|
+
if (testBuffer.length === 64)
|
|
29
|
+
startsWith04 = '04' + startsWith04;
|
|
30
|
+
return uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith04), true));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function decompress(startsWith02Or03) {
|
|
34
|
+
// if already decompressed an not has trailing 04
|
|
35
|
+
const testBuffer = Buffer.from(startsWith02Or03, 'hex');
|
|
36
|
+
if (testBuffer.length === 64)
|
|
37
|
+
startsWith02Or03 = '04' + startsWith02Or03;
|
|
38
|
+
let decompressed = uint8ArrayToHex(publicKeyConvert(hexToUnit8Array(startsWith02Or03), false));
|
|
39
|
+
// remove trailing 04
|
|
40
|
+
decompressed = decompressed.substring(2);
|
|
41
|
+
return decompressed;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class Cipher {
|
|
45
|
+
constructor() { }
|
|
46
|
+
stringify(encrypted) {
|
|
47
|
+
if (typeof encrypted === 'string')
|
|
48
|
+
return encrypted;
|
|
49
|
+
// use compressed key because it's smaller
|
|
50
|
+
const compressedKey = compress(encrypted.ephemPublicKey);
|
|
51
|
+
const ret = Buffer.concat([
|
|
52
|
+
Buffer.from(encrypted.iv, 'hex'), // 16bit
|
|
53
|
+
Buffer.from(compressedKey, 'hex'), // 33bit
|
|
54
|
+
Buffer.from(encrypted.mac, 'hex'), // 32bit
|
|
55
|
+
Buffer.from(encrypted.ciphertext, 'hex'), // var bit
|
|
56
|
+
]);
|
|
57
|
+
return ret.toString('hex');
|
|
58
|
+
}
|
|
59
|
+
parse(str) {
|
|
60
|
+
if (typeof str !== 'string')
|
|
61
|
+
return str;
|
|
62
|
+
const buf = Buffer.from(str, 'hex');
|
|
63
|
+
const ret = {
|
|
64
|
+
iv: buf.toString('hex', 0, 16),
|
|
65
|
+
ephemPublicKey: buf.toString('hex', 16, 49),
|
|
66
|
+
mac: buf.toString('hex', 49, 81),
|
|
67
|
+
ciphertext: buf.toString('hex', 81, buf.length),
|
|
68
|
+
};
|
|
69
|
+
// decompress publicKey
|
|
70
|
+
ret.ephemPublicKey = '04' + decompress(ret.ephemPublicKey);
|
|
71
|
+
return ret;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class Hash {
|
|
76
|
+
constructor() { }
|
|
77
|
+
keccak256(params) {
|
|
78
|
+
const types = [];
|
|
79
|
+
const values = [];
|
|
80
|
+
if (!Array.isArray(params)) {
|
|
81
|
+
types.push('string');
|
|
82
|
+
values.push(params);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
params.forEach((p) => {
|
|
86
|
+
types.push(p.type);
|
|
87
|
+
values.push(p.value);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return solidityPackedKeccak256(types, values);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* signs the given message
|
|
96
|
+
* we do not use sign from eth-lib because the pure secp256k1-version is 90% faster
|
|
97
|
+
* @param {string} privateKey
|
|
98
|
+
* @param {string} hash
|
|
99
|
+
* @return {string} hexString
|
|
100
|
+
*/
|
|
101
|
+
function sign(privateKey, hash) {
|
|
102
|
+
hash = addLeading0x(hash);
|
|
103
|
+
if (hash.length !== 66)
|
|
104
|
+
throw new Error('EthCrypto.sign(): Can only sign hashes, given: ' + hash);
|
|
105
|
+
const sigObj = ecdsaSign(new Uint8Array(Buffer.from(removeLeading0x(hash), 'hex')), new Uint8Array(Buffer.from(removeLeading0x(privateKey), 'hex')));
|
|
106
|
+
const recoveryId = sigObj.recid === 1 ? '1c' : '1b';
|
|
107
|
+
const newSignature = '0x' + Buffer.from(sigObj.signature).toString('hex') + recoveryId;
|
|
108
|
+
return newSignature;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function encryptWithPublicKey(publicKey, message, options) {
|
|
112
|
+
// ensure its an uncompressed publicKey
|
|
113
|
+
publicKey = decompress(publicKey);
|
|
114
|
+
// re-add the compression-flag
|
|
115
|
+
const pubString = '04' + publicKey;
|
|
116
|
+
return encrypt(Buffer.from(pubString, 'hex'), Buffer.from(message), options ? options : {}).then((encryptedBuffers) => {
|
|
117
|
+
const encrypted = {
|
|
118
|
+
iv: encryptedBuffers.iv.toString('hex'),
|
|
119
|
+
ephemPublicKey: encryptedBuffers.ephemPublicKey.toString('hex'),
|
|
120
|
+
ciphertext: encryptedBuffers.ciphertext.toString('hex'),
|
|
121
|
+
mac: encryptedBuffers.mac.toString('hex'),
|
|
122
|
+
};
|
|
123
|
+
return encrypted;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function decryptWithPrivateKey(privateKey, encrypted) {
|
|
128
|
+
const cipher = new Cipher();
|
|
129
|
+
// ensuring the encrypted data is in the correct format.
|
|
130
|
+
encrypted = cipher.parse(encrypted);
|
|
131
|
+
// remove trailing '0x' from privateKey
|
|
132
|
+
const twoStripped = removeLeading0x(privateKey);
|
|
133
|
+
const encryptedBuffer = {
|
|
134
|
+
iv: Buffer.from(encrypted.iv, 'hex'),
|
|
135
|
+
ephemPublicKey: Buffer.from(encrypted.ephemPublicKey, 'hex'),
|
|
136
|
+
ciphertext: Buffer.from(encrypted.ciphertext, 'hex'),
|
|
137
|
+
mac: Buffer.from(encrypted.mac, 'hex'),
|
|
138
|
+
};
|
|
139
|
+
return decrypt(Buffer.from(twoStripped, 'hex'), encryptedBuffer).then((decryptedBuffer) => decryptedBuffer.toString());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Generate publicKey from the privateKey.
|
|
144
|
+
* This creates the uncompressed publicKey,
|
|
145
|
+
* where 04 has stripped from left
|
|
146
|
+
* @param {string} privateKey
|
|
147
|
+
* @returns {string}
|
|
148
|
+
*/
|
|
149
|
+
function publicKeyByPrivateKey(privateKey) {
|
|
150
|
+
privateKey = addLeading0x(privateKey);
|
|
151
|
+
const publicKeyBuffer = privateToPublic(toBuffer(privateKey));
|
|
152
|
+
return publicKeyBuffer.toString('hex');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const MIN_ENTROPY_SIZE = 128;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* create a privateKey from the given entropy or a new one
|
|
159
|
+
* @param {Buffer} entropy
|
|
160
|
+
* @return {string}
|
|
161
|
+
*/
|
|
162
|
+
function createPrivateKey(entropy) {
|
|
163
|
+
if (entropy) {
|
|
164
|
+
if (!Buffer.isBuffer(entropy))
|
|
165
|
+
throw new Error('EthCrypto.createPrivateKey(): given entropy is no Buffer');
|
|
166
|
+
if (Buffer.byteLength(entropy, 'utf8') < MIN_ENTROPY_SIZE)
|
|
167
|
+
throw new Error('EthCrypto.createPrivateKey(): Entropy-size must be at least ' + MIN_ENTROPY_SIZE);
|
|
168
|
+
const outerHex = keccak256(entropy);
|
|
169
|
+
return outerHex;
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const innerHex = keccak256(concat([randomBytes(32), randomBytes(32)]));
|
|
173
|
+
const middleHex = concat([concat([randomBytes(32), innerHex]), randomBytes(32)]);
|
|
174
|
+
const outerHex = keccak256(middleHex);
|
|
175
|
+
return outerHex;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* creates a new object with
|
|
180
|
+
* private-, public-Key and address
|
|
181
|
+
* @param {Buffer?} entropy if provided, will use that as single random-source
|
|
182
|
+
*/
|
|
183
|
+
function createIdentity(entropy) {
|
|
184
|
+
const privateKey = createPrivateKey(entropy);
|
|
185
|
+
const wallet = new Wallet(privateKey);
|
|
186
|
+
const identity = {
|
|
187
|
+
privateKey: privateKey,
|
|
188
|
+
publicKey: publicKeyByPrivateKey(privateKey),
|
|
189
|
+
address: wallet.address,
|
|
190
|
+
};
|
|
191
|
+
return identity;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { Cipher, Hash, addLeading0x, compress, createIdentity, createPrivateKey, decompress, decryptWithPrivateKey, encryptWithPublicKey, hexToUnit8Array, publicKeyByPrivateKey, removeLeading0x, sign, uint8ArrayToHex };
|
|
195
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/lib/utils.ts","../src/lib/compress-public-key.ts","../src/lib/decompress-public-key.ts","../src/lib/cipher/index.ts","../src/lib/hash/index.ts","../src/lib/sign.ts","../src/lib/encrypt-with-private-key.ts","../src/lib/decrypt-with-private-key.ts","../src/lib/public-key-by-private-key.ts","../src/constants/index.ts","../src/lib/create-identity.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,null],"names":["secp256k1_sign"],"mappings":";;;;;AAAM,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,IAAI,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;AACpB,QAAA,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;;AACvB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,YAAY,CAAC,GAAW,EAAA;AACtC,IAAA,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;QACrB,OAAO,IAAI,GAAG,GAAG,CAAC;;AACjB,QAAA,OAAO,GAAG,CAAC;AAClB,CAAC;AAEK,SAAU,eAAe,CAAC,GAAuB,EAAA;IACrD,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC1C,CAAC;AAEK,SAAU,eAAe,CAAC,GAAW,EAAA;AACzC,IAAA,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AACjD;;ACfM,SAAU,QAAQ,CAAC,YAAiB,EAAA;;IAExC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;AACpD,IAAA,IAAI,UAAU,CAAC,MAAM,KAAK,EAAE;AAAE,QAAA,YAAY,GAAG,IAAI,GAAG,YAAY,CAAC;AAEjE,IAAA,OAAO,eAAe,CAAC,gBAAgB,CAAC,eAAe,CAAC,YA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C,OAAO,EAAE,MAAM,CAAC,OAAO;KACxB,CAAC;AAEF,IAAA,OAAO,QAAQ,CAAC;AAClB;;;;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Encrypted } from '../../types';
|
|
2
|
+
interface CipherInterface {
|
|
3
|
+
stringify(encrypted: Encrypted): string;
|
|
4
|
+
parse(string: string): Encrypted;
|
|
5
|
+
}
|
|
6
|
+
export declare class Cipher implements CipherInterface {
|
|
7
|
+
constructor();
|
|
8
|
+
stringify(encrypted: Encrypted): string;
|
|
9
|
+
parse(str: string): Encrypted;
|
|
10
|
+
}
|
|
11
|
+
export {};
|