@ocap/mcrypto 1.17.2 → 1.17.3
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/lib/encode.d.ts +8 -1
- package/lib/encode.js +2 -1
- package/lib/hasher/keccak.js +2 -2
- package/lib/hasher/sha2.js +2 -5
- package/lib/hasher/sha3.js +2 -2
- package/lib/index.d.ts +45 -20
- package/lib/index.js +4 -8
- package/lib/signer/ed25519.d.ts +2 -2
- package/lib/signer/ed25519.js +5 -5
- package/lib/signer/ethereum.d.ts +2 -3
- package/lib/signer/secp256k1.js +4 -4
- package/package.json +5 -4
package/lib/encode.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { BytesType, EncodingType } from '@ocap/util';
|
|
2
|
-
export
|
|
3
|
+
export declare function encode(data: BytesType, encoding?: 'hex'): string;
|
|
4
|
+
export declare function encode(data: BytesType, encoding?: 'base16'): string;
|
|
5
|
+
export declare function encode(data: BytesType, encoding?: 'base58'): string;
|
|
6
|
+
export declare function encode(data: BytesType, encoding?: 'base64'): string;
|
|
7
|
+
export declare function encode(data: BytesType, encoding?: 'buffer'): Buffer;
|
|
8
|
+
export declare function encode(data: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
9
|
+
export declare function encode(data: BytesType, encoding?: EncodingType): BytesType;
|
package/lib/encode.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encode = void 0;
|
|
3
4
|
const util_1 = require("@ocap/util");
|
|
4
5
|
function encode(data, encoding = 'hex') {
|
|
5
6
|
if (['hex', 'base16'].includes(encoding)) {
|
|
@@ -19,4 +20,4 @@ function encode(data, encoding = 'hex') {
|
|
|
19
20
|
}
|
|
20
21
|
return data;
|
|
21
22
|
}
|
|
22
|
-
exports.
|
|
23
|
+
exports.encode = encode;
|
package/lib/hasher/keccak.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.KeccakHasher = void 0;
|
|
|
7
7
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
8
|
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
9
9
|
const util_1 = require("@ocap/util");
|
|
10
|
-
const encode_1 =
|
|
10
|
+
const encode_1 = require("../encode");
|
|
11
11
|
/**
|
|
12
12
|
* Keccak support with different hash length
|
|
13
13
|
*
|
|
@@ -34,7 +34,7 @@ class KeccakHasher {
|
|
|
34
34
|
// Do nothing
|
|
35
35
|
}
|
|
36
36
|
const res = hashFn(input, round);
|
|
37
|
-
return (0, encode_1.
|
|
37
|
+
return (0, encode_1.encode)(`0x${res}`, encoding);
|
|
38
38
|
};
|
|
39
39
|
});
|
|
40
40
|
}
|
package/lib/hasher/sha2.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Sha2Hasher = void 0;
|
|
7
4
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
5
|
const util_1 = require("@ocap/util");
|
|
9
6
|
const hash_js_1 = require("hash.js");
|
|
10
|
-
const encode_1 =
|
|
7
|
+
const encode_1 = require("../encode");
|
|
11
8
|
const hashFns = {
|
|
12
9
|
sha224: hash_js_1.sha224,
|
|
13
10
|
sha256: hash_js_1.sha256,
|
|
@@ -40,7 +37,7 @@ class Sha2Hasher {
|
|
|
40
37
|
};
|
|
41
38
|
this[name] = (data, round = 2, encoding = 'hex') => {
|
|
42
39
|
const res = hashFn(data, round);
|
|
43
|
-
return (0, encode_1.
|
|
40
|
+
return (0, encode_1.encode)(res, encoding);
|
|
44
41
|
};
|
|
45
42
|
});
|
|
46
43
|
}
|
package/lib/hasher/sha3.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Sha3Hasher = void 0;
|
|
7
7
|
const js_sha3_1 = __importDefault(require("js-sha3"));
|
|
8
8
|
const util_1 = require("@ocap/util");
|
|
9
|
-
const encode_1 =
|
|
9
|
+
const encode_1 = require("../encode");
|
|
10
10
|
/**
|
|
11
11
|
* Sha3 support with different hash length
|
|
12
12
|
*
|
|
@@ -34,7 +34,7 @@ class Sha3Hasher {
|
|
|
34
34
|
// Do nothing
|
|
35
35
|
}
|
|
36
36
|
const res = hashFn(input, round);
|
|
37
|
-
return (0, encode_1.
|
|
37
|
+
return (0, encode_1.encode)(`0x${res}`, encoding);
|
|
38
38
|
};
|
|
39
39
|
});
|
|
40
40
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,19 +1,41 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
-
|
|
3
|
-
export declare type
|
|
4
|
-
export declare type
|
|
5
|
-
export declare type
|
|
6
|
-
export declare type
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
import { LiteralUnion } from 'type-fest';
|
|
4
|
+
export declare type KeyType = LiteralUnion<'ED25519' | 'SECP256K1' | 'ETHEREUM', string>;
|
|
5
|
+
export declare type HashType = LiteralUnion<'KECCAK' | 'SHA3' | 'KECCAK_384' | 'SHA3_384' | 'KECCAK_512' | 'SHA3_512' | 'SHA2', string>;
|
|
6
|
+
export declare type RoleType = LiteralUnion<'ROLE_ACCOUNT' | 'ROLE_NODE' | 'ROLE_DEVICE' | 'ROLE_APPLICATION' | 'ROLE_SMART_CONTRACT' | 'ROLE_BOT' | 'ROLE_ASSET' | 'ROLE_STAKE' | 'ROLE_VALIDATOR' | 'ROLE_GROUP' | 'ROLE_GROUP' | 'ROLE_TX' | 'ROLE_TETHER' | 'ROLE_SWAP' | 'ROLE_DELEGATION' | 'ROLE_VC' | 'ROLE_BLOCKLET' | 'ROLE_REGISTRY' | 'ROLE_TOKEN' | 'ROLE_FACTORY' | 'ROLE_ROLLUP' | 'ROLE_ANY', string>;
|
|
7
|
+
export declare type AddressType = LiteralUnion<'BASE16' | 'BASE58', string>;
|
|
8
|
+
export interface HashFnType {
|
|
9
|
+
(data: BytesType, round: number, encoding?: 'hex'): string;
|
|
10
|
+
(data: BytesType, round: number, encoding?: 'base16'): string;
|
|
11
|
+
(data: BytesType, round: number, encoding?: 'base58'): string;
|
|
12
|
+
(data: BytesType, round: number, encoding?: 'base64'): string;
|
|
13
|
+
(data: BytesType, round: number, encoding?: 'buffer'): Buffer;
|
|
14
|
+
(data: BytesType, round: number, encoding?: 'Uint8Array'): Uint8Array;
|
|
15
|
+
(data: BytesType, round: number, encoding?: EncodingType): BytesType;
|
|
16
|
+
}
|
|
17
|
+
export interface SignerType {
|
|
18
|
+
genKeyPair(encoding?: EncodingType, seed?: BytesType): KeyPairType;
|
|
19
|
+
getPublicKey(sk: BytesType, encoding?: 'hex'): string;
|
|
20
|
+
getPublicKey(sk: BytesType, encoding?: 'base16'): string;
|
|
21
|
+
getPublicKey(sk: BytesType, encoding?: 'base58'): string;
|
|
22
|
+
getPublicKey(sk: BytesType, encoding?: 'base64'): string;
|
|
23
|
+
getPublicKey(sk: BytesType, encoding?: 'buffer'): Buffer;
|
|
24
|
+
getPublicKey(sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
25
|
+
getPublicKey(sk: BytesType, encoding?: EncodingType): BytesType;
|
|
26
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'hex'): string;
|
|
27
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base16'): string;
|
|
28
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base58'): string;
|
|
29
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'base64'): string;
|
|
30
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'buffer'): Buffer;
|
|
31
|
+
sign(data: BytesType, sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
32
|
+
sign(data: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
33
|
+
verify(data: BytesType, pk: BytesType, signature: BytesType): boolean;
|
|
34
|
+
ethHash?(data: string): string;
|
|
35
|
+
ethSign?(data: string, sk: string): string;
|
|
36
|
+
ethVerify?(data: string, pk: string, signature: BytesType): boolean;
|
|
37
|
+
ethRecover?(hash: string, signature: string): string;
|
|
38
|
+
}
|
|
17
39
|
/**
|
|
18
40
|
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
19
41
|
*
|
|
@@ -187,14 +209,17 @@ export declare function getSigner(type: number): SignerType;
|
|
|
187
209
|
* const hashFn = getHasher(types.HashType.SHA3);
|
|
188
210
|
* const hash2 = hashFn(message);
|
|
189
211
|
*/
|
|
190
|
-
export declare function getHasher(type: number):
|
|
212
|
+
export declare function getHasher(type: number): HashFnType;
|
|
191
213
|
/**
|
|
192
214
|
* Get random bytes in specified encoding
|
|
193
|
-
*
|
|
194
|
-
* @param {number} [length=32]
|
|
195
|
-
* @returns {buffer|hex|uint8|base64}
|
|
196
215
|
*/
|
|
197
|
-
export declare function getRandomBytes(length
|
|
216
|
+
export declare function getRandomBytes(length: number, encoding?: 'hex'): string;
|
|
217
|
+
export declare function getRandomBytes(length: number, encoding?: 'base16'): string;
|
|
218
|
+
export declare function getRandomBytes(length: number, encoding?: 'base58'): string;
|
|
219
|
+
export declare function getRandomBytes(length: number, encoding?: 'base64'): string;
|
|
220
|
+
export declare function getRandomBytes(length: number, encoding?: 'buffer'): Buffer;
|
|
221
|
+
export declare function getRandomBytes(length: number, encoding?: 'Uint8Array'): Uint8Array;
|
|
222
|
+
export declare function getRandomBytes(length: number, encoding?: EncodingType): BytesType;
|
|
198
223
|
export declare const Signers: Readonly<{
|
|
199
224
|
[x: number]: import("./signer/ed25519").Ed25519Signer | import("./signer/secp256k1").Secp256k1Signer;
|
|
200
225
|
}>;
|
package/lib/index.js
CHANGED
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Hashers = exports.Signers = exports.getRandomBytes = exports.getHasher = exports.getSigner = exports.types = exports.Hasher = exports.Signer = void 0;
|
|
7
7
|
const randombytes_1 = __importDefault(require("randombytes"));
|
|
8
|
-
const encode_1 =
|
|
8
|
+
const encode_1 = require("./encode");
|
|
9
9
|
const keccak_1 = __importDefault(require("./hasher/keccak"));
|
|
10
10
|
const sha2_1 = __importDefault(require("./hasher/sha2"));
|
|
11
11
|
const sha3_1 = __importDefault(require("./hasher/sha3"));
|
|
@@ -170,6 +170,7 @@ function getSigner(type) {
|
|
|
170
170
|
if (typeof exports.Signers[type] === 'undefined') {
|
|
171
171
|
throw new Error(`Unsupported signer type: ${type}`);
|
|
172
172
|
}
|
|
173
|
+
// @ts-ignore
|
|
173
174
|
return exports.Signers[type];
|
|
174
175
|
}
|
|
175
176
|
exports.getSigner = getSigner;
|
|
@@ -195,17 +196,12 @@ function getHasher(type) {
|
|
|
195
196
|
if (typeof exports.Hashers[type] === 'undefined') {
|
|
196
197
|
throw new Error(`Unsupported hash type: ${type}`);
|
|
197
198
|
}
|
|
199
|
+
// @ts-ignore
|
|
198
200
|
return exports.Hashers[type];
|
|
199
201
|
}
|
|
200
202
|
exports.getHasher = getHasher;
|
|
201
|
-
/**
|
|
202
|
-
* Get random bytes in specified encoding
|
|
203
|
-
*
|
|
204
|
-
* @param {number} [length=32]
|
|
205
|
-
* @returns {buffer|hex|uint8|base64}
|
|
206
|
-
*/
|
|
207
203
|
function getRandomBytes(length = 32, encoding = 'hex') {
|
|
208
|
-
return (0, encode_1.
|
|
204
|
+
return (0, encode_1.encode)((0, randombytes_1.default)(length), encoding);
|
|
209
205
|
}
|
|
210
206
|
exports.getRandomBytes = getRandomBytes;
|
|
211
207
|
exports.Signers = Object.freeze({
|
package/lib/signer/ed25519.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
-
import
|
|
2
|
+
import BaseSigner from '../protocols/signer';
|
|
3
3
|
/**
|
|
4
4
|
* Signer implementation for ed25519, based on `tweetnacl`
|
|
5
5
|
*
|
|
6
6
|
* @class Ed25519Signer
|
|
7
7
|
*/
|
|
8
|
-
declare class Ed25519Signer extends
|
|
8
|
+
declare class Ed25519Signer extends BaseSigner {
|
|
9
9
|
constructor();
|
|
10
10
|
/**
|
|
11
11
|
* @public
|
package/lib/signer/ed25519.js
CHANGED
|
@@ -9,7 +9,7 @@ const tweetnacl_1 = __importDefault(require("tweetnacl"));
|
|
|
9
9
|
const randombytes_1 = __importDefault(require("randombytes"));
|
|
10
10
|
const util_1 = require("@ocap/util");
|
|
11
11
|
const signer_1 = __importDefault(require("../protocols/signer"));
|
|
12
|
-
const encode_1 =
|
|
12
|
+
const encode_1 = require("../encode");
|
|
13
13
|
const ed25519 = tweetnacl_1.default.sign;
|
|
14
14
|
/**
|
|
15
15
|
* Signer implementation for ed25519, based on `tweetnacl`
|
|
@@ -41,8 +41,8 @@ class Ed25519Signer extends signer_1.default {
|
|
|
41
41
|
throw new Error('Invalid seed to generate key pair');
|
|
42
42
|
}
|
|
43
43
|
const keyPair = ed25519.keyPair.fromSeed(seed);
|
|
44
|
-
keyPair.publicKey = (0, encode_1.
|
|
45
|
-
keyPair.secretKey = (0, encode_1.
|
|
44
|
+
keyPair.publicKey = (0, encode_1.encode)(keyPair.publicKey, encoding);
|
|
45
|
+
keyPair.secretKey = (0, encode_1.encode)(keyPair.secretKey, encoding);
|
|
46
46
|
return keyPair;
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
@@ -54,7 +54,7 @@ class Ed25519Signer extends signer_1.default {
|
|
|
54
54
|
getPublicKey(sk, encoding = 'hex') {
|
|
55
55
|
const skBytes = (0, util_1.toUint8Array)(sk);
|
|
56
56
|
const pk = ed25519.keyPair.fromSecretKey(skBytes).publicKey;
|
|
57
|
-
return (0, encode_1.
|
|
57
|
+
return (0, encode_1.encode)(pk, encoding);
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Sign a message and get the signature hex
|
|
@@ -67,7 +67,7 @@ class Ed25519Signer extends signer_1.default {
|
|
|
67
67
|
const skBytes = (0, util_1.toUint8Array)(sk);
|
|
68
68
|
const messageBytes = (0, util_1.toUint8Array)(message);
|
|
69
69
|
const signature = ed25519.detached(messageBytes, skBytes);
|
|
70
|
-
return (0, encode_1.
|
|
70
|
+
return (0, encode_1.encode)(signature, encoding);
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Verify if a signature is valid
|
package/lib/signer/ethereum.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { BytesType } from '@ocap/util';
|
|
2
1
|
import { Secp256k1Signer } from './secp256k1';
|
|
3
2
|
/**
|
|
4
3
|
* Signer implementation for secp256k1, based on `elliptic`, and ethereum compatible
|
|
@@ -9,8 +8,8 @@ declare class EthereumSigner extends Secp256k1Signer {
|
|
|
9
8
|
pkHasFormatPrefix: boolean;
|
|
10
9
|
constructor();
|
|
11
10
|
ethHash(data: string): string;
|
|
12
|
-
ethSign(data:
|
|
13
|
-
ethRecover(data:
|
|
11
|
+
ethSign(data: string, privateKey: string): string;
|
|
12
|
+
ethRecover(data: string, signature: string): string;
|
|
14
13
|
}
|
|
15
14
|
declare const _default: EthereumSigner;
|
|
16
15
|
export default _default;
|
package/lib/signer/secp256k1.js
CHANGED
|
@@ -8,7 +8,7 @@ const elliptic_1 = __importDefault(require("elliptic"));
|
|
|
8
8
|
const util_1 = require("@ocap/util");
|
|
9
9
|
const randombytes_1 = __importDefault(require("randombytes"));
|
|
10
10
|
const signer_1 = __importDefault(require("../protocols/signer"));
|
|
11
|
-
const encode_1 =
|
|
11
|
+
const encode_1 = require("../encode");
|
|
12
12
|
/* eslint-disable class-methods-use-this */
|
|
13
13
|
/* eslint-disable no-useless-constructor */
|
|
14
14
|
const EC = elliptic_1.default.ec;
|
|
@@ -44,7 +44,7 @@ class Secp256k1Signer extends signer_1.default {
|
|
|
44
44
|
sk = new Uint8Array((0, randombytes_1.default)(32));
|
|
45
45
|
} while (!this.isValidSK(sk));
|
|
46
46
|
const pk = this.getPublicKey((0, util_1.toHex)(sk));
|
|
47
|
-
return { secretKey: (0, encode_1.
|
|
47
|
+
return { secretKey: (0, encode_1.encode)(sk, encoding), publicKey: (0, encode_1.encode)(pk, encoding) };
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
50
|
* Get publicKey from secretKey
|
|
@@ -54,7 +54,7 @@ class Secp256k1Signer extends signer_1.default {
|
|
|
54
54
|
if (this.pkHasFormatPrefix === false) {
|
|
55
55
|
pk = pk.slice(2);
|
|
56
56
|
}
|
|
57
|
-
return (0, encode_1.
|
|
57
|
+
return (0, encode_1.encode)(`0x${pk}`, encoding);
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Sign a message and get the signature hex
|
|
@@ -71,7 +71,7 @@ class Secp256k1Signer extends signer_1.default {
|
|
|
71
71
|
.keyFromPrivate((0, util_1.toBuffer)(sk))
|
|
72
72
|
.sign((0, util_1.stripHexPrefix)(msg), { canonical: true })
|
|
73
73
|
.toDER('hex');
|
|
74
|
-
return (0, encode_1.
|
|
74
|
+
return (0, encode_1.encode)(`0x${signature}`, encoding);
|
|
75
75
|
}
|
|
76
76
|
/**
|
|
77
77
|
* Verify if a signature is valid
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.17.
|
|
3
|
+
"version": "1.17.3",
|
|
4
4
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"url": "https://github.com/ArcBlock/asset-chain/issues"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@ocap/util": "1.17.
|
|
59
|
+
"@ocap/util": "1.17.3",
|
|
60
60
|
"bn.js": "5.2.1",
|
|
61
61
|
"crypto-js": "^4.1.1",
|
|
62
62
|
"elliptic": "^6.5.3",
|
|
@@ -65,11 +65,12 @@
|
|
|
65
65
|
"interface": "^1.2.1",
|
|
66
66
|
"js-sha3": "^0.8.0",
|
|
67
67
|
"randombytes": "^2.1.0",
|
|
68
|
-
"tweetnacl": "^1.0.3"
|
|
68
|
+
"tweetnacl": "^1.0.3",
|
|
69
|
+
"type-fest": "^2.13.1"
|
|
69
70
|
},
|
|
70
71
|
"resolutions": {
|
|
71
72
|
"bn.js": "5.2.1",
|
|
72
73
|
"elliptic": "6.5.3"
|
|
73
74
|
},
|
|
74
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "7a74e4e2b362a6e6ea8d14617f0480966c3363d5"
|
|
75
76
|
}
|