@ocap/mcrypto 1.16.15 → 1.17.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/lib/crypter/aes.d.ts +8 -0
- package/lib/crypter/aes.js +51 -28
- package/lib/encode.d.ts +2 -2
- package/lib/encode.js +22 -25
- package/lib/hasher/keccak.d.ts +13 -0
- package/lib/hasher/keccak.js +36 -78
- package/lib/hasher/sha2.d.ts +13 -0
- package/lib/hasher/sha2.js +40 -82
- package/lib/hasher/sha3.d.ts +13 -0
- package/lib/hasher/sha3.js +36 -78
- package/lib/index.d.ts +203 -3
- package/lib/index.js +171 -201
- package/lib/protocols/crypter.d.ts +2 -0
- package/lib/protocols/crypter.js +9 -3
- package/lib/protocols/hasher.d.ts +2 -0
- package/lib/protocols/hasher.js +9 -3
- package/lib/protocols/signer.d.ts +2 -0
- package/lib/protocols/signer.js +9 -3
- package/lib/signer/ed25519.d.ts +53 -0
- package/lib/signer/ed25519.js +81 -86
- package/lib/signer/ethereum.d.ts +17 -0
- package/lib/signer/ethereum.js +35 -34
- package/lib/signer/secp256k1.d.ts +39 -0
- package/lib/signer/secp256k1.js +82 -107
- package/package.json +23 -16
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
+
import Signer from '../protocols/signer';
|
|
3
|
+
/**
|
|
4
|
+
* Signer implementation for ed25519, based on `tweetnacl`
|
|
5
|
+
*
|
|
6
|
+
* @class Ed25519Signer
|
|
7
|
+
*/
|
|
8
|
+
declare class Ed25519Signer extends Signer {
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
* @typedefKeyPairType
|
|
13
|
+
* @prop {string} publicKey - publicKey in hex format
|
|
14
|
+
* @prop {string} secretKey - secretKey in hex format
|
|
15
|
+
* @memberof Ed25519Signer
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Generate random secret/public key pair
|
|
19
|
+
*
|
|
20
|
+
* @param {Buffer|Uint8Array} [userSeed=undefined]
|
|
21
|
+
* @param {string} [encoding='hex']
|
|
22
|
+
* @returns {KeyPairType}
|
|
23
|
+
* @memberof Ed25519Signer
|
|
24
|
+
*/
|
|
25
|
+
genKeyPair(encoding?: EncodingType, userSeed?: BytesType): KeyPairType;
|
|
26
|
+
/**
|
|
27
|
+
* Get publicKey from secretKey
|
|
28
|
+
*
|
|
29
|
+
* @param {hex|buffer|base58|Uint8Array} sk - can be either a hex encoded string or a buffer
|
|
30
|
+
* @returns {string} hex encoded publicKey
|
|
31
|
+
*/
|
|
32
|
+
getPublicKey(sk: BytesType, encoding?: EncodingType): BytesType;
|
|
33
|
+
/**
|
|
34
|
+
* Sign a message and get the signature hex
|
|
35
|
+
*
|
|
36
|
+
* @param {hex|base58|buffer|Uint8Array} message
|
|
37
|
+
* @param {hex|base58|buffer|Uint8Array} sk
|
|
38
|
+
* @returns {string} hex encoded signature
|
|
39
|
+
*/
|
|
40
|
+
sign(message: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
41
|
+
/**
|
|
42
|
+
* Verify if a signature is valid
|
|
43
|
+
*
|
|
44
|
+
* @param {string|buffer} message
|
|
45
|
+
* @param {string|buffer} signature
|
|
46
|
+
* @param {string|buffer} pk
|
|
47
|
+
* @returns {bool}
|
|
48
|
+
*/
|
|
49
|
+
verify(message: BytesType, signature: BytesType, pk: BytesType): boolean;
|
|
50
|
+
}
|
|
51
|
+
declare const _default: Ed25519Signer;
|
|
52
|
+
export default _default;
|
|
53
|
+
export { Ed25519Signer };
|
package/lib/signer/ed25519.js
CHANGED
|
@@ -1,93 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Ed25519Signer = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-useless-constructor */
|
|
8
|
+
const tweetnacl_1 = __importDefault(require("tweetnacl"));
|
|
9
|
+
const randombytes_1 = __importDefault(require("randombytes"));
|
|
10
|
+
const util_1 = require("@ocap/util");
|
|
11
|
+
const signer_1 = __importDefault(require("../protocols/signer"));
|
|
12
|
+
const encode_1 = __importDefault(require("../encode"));
|
|
13
|
+
const ed25519 = tweetnacl_1.default.sign;
|
|
10
14
|
/**
|
|
11
15
|
* Signer implementation for ed25519, based on `tweetnacl`
|
|
12
16
|
*
|
|
13
17
|
* @class Ed25519Signer
|
|
14
18
|
*/
|
|
15
|
-
class Ed25519Signer extends
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
19
|
+
class Ed25519Signer extends signer_1.default {
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
* @typedefKeyPairType
|
|
26
|
+
* @prop {string} publicKey - publicKey in hex format
|
|
27
|
+
* @prop {string} secretKey - secretKey in hex format
|
|
28
|
+
* @memberof Ed25519Signer
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Generate random secret/public key pair
|
|
32
|
+
*
|
|
33
|
+
* @param {Buffer|Uint8Array} [userSeed=undefined]
|
|
34
|
+
* @param {string} [encoding='hex']
|
|
35
|
+
* @returns {KeyPairType}
|
|
36
|
+
* @memberof Ed25519Signer
|
|
37
|
+
*/
|
|
38
|
+
genKeyPair(encoding = 'hex', userSeed) {
|
|
39
|
+
const seed = userSeed ? (0, util_1.toUint8Array)(userSeed) : new Uint8Array((0, randombytes_1.default)(32));
|
|
40
|
+
if (seed.byteLength !== 32) {
|
|
41
|
+
throw new Error('Invalid seed to generate key pair');
|
|
42
|
+
}
|
|
43
|
+
const keyPair = ed25519.keyPair.fromSeed(seed);
|
|
44
|
+
keyPair.publicKey = (0, encode_1.default)(keyPair.publicKey, encoding);
|
|
45
|
+
keyPair.secretKey = (0, encode_1.default)(keyPair.secretKey, encoding);
|
|
46
|
+
return keyPair;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get publicKey from secretKey
|
|
50
|
+
*
|
|
51
|
+
* @param {hex|buffer|base58|Uint8Array} sk - can be either a hex encoded string or a buffer
|
|
52
|
+
* @returns {string} hex encoded publicKey
|
|
53
|
+
*/
|
|
54
|
+
getPublicKey(sk, encoding = 'hex') {
|
|
55
|
+
const skBytes = (0, util_1.toUint8Array)(sk);
|
|
56
|
+
const pk = ed25519.keyPair.fromSecretKey(skBytes).publicKey;
|
|
57
|
+
return (0, encode_1.default)(pk, encoding);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sign a message and get the signature hex
|
|
61
|
+
*
|
|
62
|
+
* @param {hex|base58|buffer|Uint8Array} message
|
|
63
|
+
* @param {hex|base58|buffer|Uint8Array} sk
|
|
64
|
+
* @returns {string} hex encoded signature
|
|
65
|
+
*/
|
|
66
|
+
sign(message, sk, encoding = 'hex') {
|
|
67
|
+
const skBytes = (0, util_1.toUint8Array)(sk);
|
|
68
|
+
const messageBytes = (0, util_1.toUint8Array)(message);
|
|
69
|
+
const signature = ed25519.detached(messageBytes, skBytes);
|
|
70
|
+
return (0, encode_1.default)(signature, encoding);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Verify if a signature is valid
|
|
74
|
+
*
|
|
75
|
+
* @param {string|buffer} message
|
|
76
|
+
* @param {string|buffer} signature
|
|
77
|
+
* @param {string|buffer} pk
|
|
78
|
+
* @returns {bool}
|
|
79
|
+
*/
|
|
80
|
+
verify(message, signature, pk) {
|
|
81
|
+
const pkBytes = (0, util_1.toUint8Array)(pk);
|
|
82
|
+
const messageBytes = (0, util_1.toUint8Array)(message);
|
|
83
|
+
const signatureBytes = (0, util_1.toUint8Array)(signature);
|
|
84
|
+
return ed25519.detached.verify(messageBytes, signatureBytes, pkBytes);
|
|
40
85
|
}
|
|
41
|
-
|
|
42
|
-
const keyPair = ed25519.keyPair.fromSeed(seed);
|
|
43
|
-
|
|
44
|
-
keyPair.publicKey = encode(keyPair.publicKey, encoding);
|
|
45
|
-
keyPair.secretKey = encode(keyPair.secretKey, encoding);
|
|
46
|
-
|
|
47
|
-
return keyPair;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get publicKey from secretKey
|
|
52
|
-
*
|
|
53
|
-
* @param {hex|buffer|base58|Uint8Array} sk - can be either a hex encoded string or a buffer
|
|
54
|
-
* @returns {string} hex encoded publicKey
|
|
55
|
-
*/
|
|
56
|
-
getPublicKey(sk, encoding = 'hex') {
|
|
57
|
-
const skBytes = toUint8Array(sk);
|
|
58
|
-
const pk = ed25519.keyPair.fromSecretKey(skBytes).publicKey;
|
|
59
|
-
return encode(pk, encoding);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Sign a message and get the signature hex
|
|
64
|
-
*
|
|
65
|
-
* @param {hex|base58|buffer|Uint8Array} message
|
|
66
|
-
* @param {hex|base58|buffer|Uint8Array} sk
|
|
67
|
-
* @returns {string} hex encoded signature
|
|
68
|
-
*/
|
|
69
|
-
sign(message, sk, encoding = 'hex') {
|
|
70
|
-
const skBytes = toUint8Array(sk);
|
|
71
|
-
const messageBytes = toUint8Array(message);
|
|
72
|
-
const signature = ed25519.detached(messageBytes, skBytes);
|
|
73
|
-
return encode(signature, encoding);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Verify if a signature is valid
|
|
78
|
-
*
|
|
79
|
-
* @param {string|buffer} message
|
|
80
|
-
* @param {string|buffer} signature
|
|
81
|
-
* @param {string|buffer} pk
|
|
82
|
-
* @returns {bool}
|
|
83
|
-
*/
|
|
84
|
-
verify(message, signature, pk) {
|
|
85
|
-
const pkBytes = toUint8Array(pk);
|
|
86
|
-
const messageBytes = toUint8Array(message);
|
|
87
|
-
const signatureBytes = toUint8Array(signature);
|
|
88
|
-
return ed25519.detached.verify(messageBytes, signatureBytes, pkBytes);
|
|
89
|
-
}
|
|
90
86
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
module.exports.Ed25519Signer = Ed25519Signer;
|
|
87
|
+
exports.Ed25519Signer = Ed25519Signer;
|
|
88
|
+
exports.default = new Ed25519Signer();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BytesType } from '@ocap/util';
|
|
2
|
+
import { Secp256k1Signer } from './secp256k1';
|
|
3
|
+
/**
|
|
4
|
+
* Signer implementation for secp256k1, based on `elliptic`, and ethereum compatible
|
|
5
|
+
*
|
|
6
|
+
* @class EthereumSigner
|
|
7
|
+
*/
|
|
8
|
+
declare class EthereumSigner extends Secp256k1Signer {
|
|
9
|
+
pkHasFormatPrefix: boolean;
|
|
10
|
+
constructor();
|
|
11
|
+
ethHash(data: string): string;
|
|
12
|
+
ethSign(data: BytesType, privateKey: BytesType): string;
|
|
13
|
+
ethRecover(data: BytesType, signature: BytesType): string;
|
|
14
|
+
}
|
|
15
|
+
declare const _default: EthereumSigner;
|
|
16
|
+
export default _default;
|
|
17
|
+
export { EthereumSigner };
|
package/lib/signer/ethereum.js
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EthereumSigner = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const account_1 = __importDefault(require("eth-lib/lib/account"));
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const hash_1 = __importDefault(require("eth-lib/lib/hash"));
|
|
12
|
+
const util_1 = require("@ocap/util");
|
|
13
|
+
const secp256k1_1 = require("./secp256k1");
|
|
9
14
|
/**
|
|
10
15
|
* Signer implementation for secp256k1, based on `elliptic`, and ethereum compatible
|
|
11
16
|
*
|
|
12
17
|
* @class EthereumSigner
|
|
13
18
|
*/
|
|
14
|
-
class EthereumSigner extends Secp256k1Signer {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
ethRecover(data, signature) {
|
|
35
|
-
return Account.recover(data, signature);
|
|
36
|
-
}
|
|
19
|
+
class EthereumSigner extends secp256k1_1.Secp256k1Signer {
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
this.pkHasFormatPrefix = false;
|
|
23
|
+
}
|
|
24
|
+
ethHash(data) {
|
|
25
|
+
const messageHex = (0, util_1.isHexStrict)(data) ? data : (0, util_1.utf8ToHex)(data);
|
|
26
|
+
const messageBytes = (0, util_1.hexToBytes)(messageHex);
|
|
27
|
+
const messageBuffer = Buffer.from(messageBytes);
|
|
28
|
+
const preamble = `\x19Ethereum Signed Message:\n${messageBytes.length}`;
|
|
29
|
+
const preambleBuffer = Buffer.from(preamble);
|
|
30
|
+
const ethMessage = Buffer.concat([preambleBuffer, messageBuffer]);
|
|
31
|
+
return hash_1.default.keccak256s(ethMessage);
|
|
32
|
+
}
|
|
33
|
+
ethSign(data, privateKey) {
|
|
34
|
+
return account_1.default.sign(data, privateKey);
|
|
35
|
+
}
|
|
36
|
+
ethRecover(data, signature) {
|
|
37
|
+
return account_1.default.recover(data, signature);
|
|
38
|
+
}
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
module.exports.EthereumSigner = EthereumSigner;
|
|
40
|
+
exports.EthereumSigner = EthereumSigner;
|
|
41
|
+
exports.default = new EthereumSigner();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BytesType, KeyPairType, EncodingType } from '@ocap/util';
|
|
2
|
+
import Signer from '../protocols/signer';
|
|
3
|
+
/**
|
|
4
|
+
* Signer implementation for secp256k1, based on `elliptic`
|
|
5
|
+
*
|
|
6
|
+
* @class Secp256k1Signer
|
|
7
|
+
*/
|
|
8
|
+
declare class Secp256k1Signer extends Signer {
|
|
9
|
+
pkCompressed: boolean;
|
|
10
|
+
pkHasFormatPrefix: boolean;
|
|
11
|
+
constructor();
|
|
12
|
+
isValidSK(sk: BytesType): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
* @typedefKeyPairType
|
|
16
|
+
* @prop {string} publicKey - publicKey in hex format
|
|
17
|
+
* @prop {string} secretKey - secretKey in hex format
|
|
18
|
+
* @memberof Secp256k1Signer
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Generate random secret/public key pair
|
|
22
|
+
*/
|
|
23
|
+
genKeyPair(encoding?: EncodingType): KeyPairType;
|
|
24
|
+
/**
|
|
25
|
+
* Get publicKey from secretKey
|
|
26
|
+
*/
|
|
27
|
+
getPublicKey(sk: BytesType, encoding?: EncodingType): BytesType;
|
|
28
|
+
/**
|
|
29
|
+
* Sign a message and get the signature hex
|
|
30
|
+
*/
|
|
31
|
+
sign(message: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
32
|
+
/**
|
|
33
|
+
* Verify if a signature is valid
|
|
34
|
+
*/
|
|
35
|
+
verify(message: BytesType, signature: BytesType, pk: BytesType): boolean;
|
|
36
|
+
}
|
|
37
|
+
declare const _default: Secp256k1Signer;
|
|
38
|
+
export default _default;
|
|
39
|
+
export { Secp256k1Signer };
|
package/lib/signer/secp256k1.js
CHANGED
|
@@ -1,120 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Secp256k1Signer = void 0;
|
|
7
|
+
const elliptic_1 = __importDefault(require("elliptic"));
|
|
8
|
+
const util_1 = require("@ocap/util");
|
|
9
|
+
const randombytes_1 = __importDefault(require("randombytes"));
|
|
10
|
+
const signer_1 = __importDefault(require("../protocols/signer"));
|
|
11
|
+
const encode_1 = __importDefault(require("../encode"));
|
|
1
12
|
/* eslint-disable class-methods-use-this */
|
|
2
13
|
/* eslint-disable no-useless-constructor */
|
|
3
|
-
const EC =
|
|
4
|
-
const { BN } = require('@ocap/util');
|
|
5
|
-
const randomBytes = require('randombytes');
|
|
6
|
-
const { stripHexPrefix, toHex, toBuffer, toUint8Array } = require('@ocap/util');
|
|
7
|
-
const Signer = require('../protocols/signer');
|
|
8
|
-
|
|
14
|
+
const EC = elliptic_1.default.ec;
|
|
9
15
|
const secp256k1 = new EC('secp256k1');
|
|
10
|
-
const encode = require('../encode');
|
|
11
|
-
|
|
12
16
|
/**
|
|
13
17
|
* Signer implementation for secp256k1, based on `elliptic`
|
|
14
18
|
*
|
|
15
19
|
* @class Secp256k1Signer
|
|
16
20
|
*/
|
|
17
|
-
class Secp256k1Signer extends
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
isValidSK(sk) {
|
|
25
|
-
const bn = new BN(sk);
|
|
26
|
-
return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @public
|
|
31
|
-
* @typedef KeyPair
|
|
32
|
-
* @prop {string} publicKey - publicKey in hex format
|
|
33
|
-
* @prop {string} secretKey - secretKey in hex format
|
|
34
|
-
* @memberof Secp256k1Signer
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Generate random secret/public key pair
|
|
39
|
-
*
|
|
40
|
-
* @param {string} [encoding='hex']
|
|
41
|
-
* @returns {KeyPair}
|
|
42
|
-
* @memberof Secp256k1Signer
|
|
43
|
-
*/
|
|
44
|
-
genKeyPair(encoding = 'hex') {
|
|
45
|
-
let sk = null;
|
|
46
|
-
do {
|
|
47
|
-
sk = new Uint8Array(randomBytes(32));
|
|
48
|
-
} while (!this.isValidSK(sk));
|
|
49
|
-
const pk = this.getPublicKey(toHex(sk));
|
|
50
|
-
return { secretKey: encode(sk, encoding), publicKey: encode(pk, encoding) };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Get publicKey from secretKey
|
|
55
|
-
*
|
|
56
|
-
* @param {string} sk - must be a hex encoded string
|
|
57
|
-
* @param {string} [encoding='hex']
|
|
58
|
-
* @returns {string} hex encoded publicKey
|
|
59
|
-
* @memberof Secp256k1Signer
|
|
60
|
-
*/
|
|
61
|
-
getPublicKey(sk, encoding = 'hex') {
|
|
62
|
-
let pk = secp256k1.keyFromPrivate(toBuffer(sk)).getPublic(this.pkCompressed, 'hex');
|
|
63
|
-
if (this.pkHasFormatPrefix === false) {
|
|
64
|
-
pk = pk.slice(2);
|
|
21
|
+
class Secp256k1Signer extends signer_1.default {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
this.pkHasFormatPrefix = true;
|
|
25
|
+
this.pkCompressed = false;
|
|
65
26
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Sign a message and get the signature hex
|
|
72
|
-
*
|
|
73
|
-
* @param {string} message
|
|
74
|
-
* @param {string} sk
|
|
75
|
-
* @param {string} [encoding='hex']
|
|
76
|
-
* @returns {string} hex encoded signature
|
|
77
|
-
* @memberof Secp256k1Signer
|
|
78
|
-
*/
|
|
79
|
-
sign(message, sk, encoding = 'hex') {
|
|
80
|
-
let msg = message;
|
|
81
|
-
try {
|
|
82
|
-
msg = toUint8Array(message);
|
|
83
|
-
} catch (err) {
|
|
84
|
-
// Do nothing;
|
|
27
|
+
isValidSK(sk) {
|
|
28
|
+
const bn = new util_1.BN(sk);
|
|
29
|
+
return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero();
|
|
85
30
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
let msg = message;
|
|
104
|
-
try {
|
|
105
|
-
msg = toUint8Array(message);
|
|
106
|
-
} catch (err) {
|
|
107
|
-
// Do nothing;
|
|
31
|
+
/**
|
|
32
|
+
* @public
|
|
33
|
+
* @typedefKeyPairType
|
|
34
|
+
* @prop {string} publicKey - publicKey in hex format
|
|
35
|
+
* @prop {string} secretKey - secretKey in hex format
|
|
36
|
+
* @memberof Secp256k1Signer
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Generate random secret/public key pair
|
|
40
|
+
*/
|
|
41
|
+
genKeyPair(encoding = 'hex') {
|
|
42
|
+
let sk = null;
|
|
43
|
+
do {
|
|
44
|
+
sk = new Uint8Array((0, randombytes_1.default)(32));
|
|
45
|
+
} while (!this.isValidSK(sk));
|
|
46
|
+
const pk = this.getPublicKey((0, util_1.toHex)(sk));
|
|
47
|
+
return { secretKey: (0, encode_1.default)(sk, encoding), publicKey: (0, encode_1.default)(pk, encoding) };
|
|
108
48
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Get publicKey from secretKey
|
|
51
|
+
*/
|
|
52
|
+
getPublicKey(sk, encoding = 'hex') {
|
|
53
|
+
let pk = secp256k1.keyFromPrivate((0, util_1.toBuffer)(sk)).getPublic(this.pkCompressed, 'hex');
|
|
54
|
+
if (this.pkHasFormatPrefix === false) {
|
|
55
|
+
pk = pk.slice(2);
|
|
56
|
+
}
|
|
57
|
+
return (0, encode_1.default)(`0x${pk}`, encoding);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sign a message and get the signature hex
|
|
61
|
+
*/
|
|
62
|
+
sign(message, sk, encoding = 'hex') {
|
|
63
|
+
let msg = message;
|
|
64
|
+
try {
|
|
65
|
+
msg = (0, util_1.toUint8Array)(message);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
// Do nothing;
|
|
69
|
+
}
|
|
70
|
+
const signature = secp256k1
|
|
71
|
+
.keyFromPrivate((0, util_1.toBuffer)(sk))
|
|
72
|
+
.sign((0, util_1.stripHexPrefix)(msg), { canonical: true })
|
|
73
|
+
.toDER('hex');
|
|
74
|
+
return (0, encode_1.default)(`0x${signature}`, encoding);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Verify if a signature is valid
|
|
78
|
+
*/
|
|
79
|
+
verify(message, signature, pk) {
|
|
80
|
+
let msg = message;
|
|
81
|
+
try {
|
|
82
|
+
msg = (0, util_1.toUint8Array)(message);
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
// Do nothing;
|
|
86
|
+
}
|
|
87
|
+
let pkBuffer = (0, util_1.toBuffer)(pk);
|
|
88
|
+
if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 0x04 && pkBuffer.byteLength === 64) {
|
|
89
|
+
pkBuffer = Buffer.concat([Buffer.from([0x04]), pkBuffer]);
|
|
90
|
+
}
|
|
91
|
+
return secp256k1.keyFromPublic(pkBuffer).verify((0, util_1.stripHexPrefix)(msg), (0, util_1.stripHexPrefix)((0, util_1.toHex)(signature)));
|
|
113
92
|
}
|
|
114
|
-
|
|
115
|
-
return secp256k1.keyFromPublic(pkBuffer).verify(stripHexPrefix(msg), stripHexPrefix(toHex(signature)));
|
|
116
|
-
}
|
|
117
93
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
module.exports.Secp256k1Signer = Secp256k1Signer;
|
|
94
|
+
exports.Secp256k1Signer = Secp256k1Signer;
|
|
95
|
+
exports.default = new Secp256k1Signer();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -22,36 +22,43 @@
|
|
|
22
22
|
"homepage": "https://github.com/ArcBlock/asset-chain/tree/master/core/mcrypto",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
24
|
"main": "lib/index.js",
|
|
25
|
+
"typings": "lib/index.d.ts",
|
|
25
26
|
"files": [
|
|
26
27
|
"lib"
|
|
27
28
|
],
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
30
|
+
"@arcblock/eslint-config-ts": "0.2.2",
|
|
31
|
+
"@types/crypto-js": "^4.1.1",
|
|
32
|
+
"@types/elliptic": "^6.4.14",
|
|
33
|
+
"@types/node": "^17.0.38",
|
|
34
|
+
"@types/randombytes": "^2.0.0",
|
|
35
|
+
"eslint": "^8.17.0",
|
|
36
|
+
"jest": "^28.1.1",
|
|
37
|
+
"ts-jest": "^28.0.3",
|
|
38
|
+
"typescript": "^4.7.3"
|
|
31
39
|
},
|
|
32
40
|
"repository": {
|
|
33
41
|
"type": "git",
|
|
34
42
|
"url": "git+https://github.com/ArcBlock/asset-chain.git"
|
|
35
43
|
},
|
|
36
44
|
"scripts": {
|
|
37
|
-
"lint": "eslint tests
|
|
38
|
-
"lint:fix": "
|
|
39
|
-
"docs": "yarn gen-dts && yarn gen-docs && yarn cleanup-docs && yarn format-docs",
|
|
40
|
-
"cleanup-docs": "node ../../scripts/cleanup-docs.js docs/README.md $npm_package_name",
|
|
41
|
-
"gen-dts": "j2d lib/index.js",
|
|
45
|
+
"lint": "eslint src tests",
|
|
46
|
+
"lint:fix": "npm run lint -- --fix",
|
|
42
47
|
"prepush": "CI=1 yarn test",
|
|
43
48
|
"test": "jest --forceExit --detectOpenHandles",
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
49
|
+
"coverage": "yarn test -- --coverage",
|
|
50
|
+
"clean": "rm -fr lib",
|
|
51
|
+
"prebuild": "npm run clean",
|
|
52
|
+
"build": "tsc",
|
|
53
|
+
"build:watch": "npm run build -- -w"
|
|
47
54
|
},
|
|
48
55
|
"bugs": {
|
|
49
56
|
"url": "https://github.com/ArcBlock/asset-chain/issues"
|
|
50
57
|
},
|
|
51
58
|
"dependencies": {
|
|
52
|
-
"@ocap/util": "1.
|
|
53
|
-
"bn.js": "5.2.
|
|
54
|
-
"crypto-js": "^4.
|
|
59
|
+
"@ocap/util": "1.17.0",
|
|
60
|
+
"bn.js": "5.2.1",
|
|
61
|
+
"crypto-js": "^4.1.1",
|
|
55
62
|
"elliptic": "^6.5.3",
|
|
56
63
|
"eth-lib": "^0.2.8",
|
|
57
64
|
"hash.js": "^1.1.7",
|
|
@@ -61,8 +68,8 @@
|
|
|
61
68
|
"tweetnacl": "^1.0.3"
|
|
62
69
|
},
|
|
63
70
|
"resolutions": {
|
|
64
|
-
"bn.js": "5.2.
|
|
71
|
+
"bn.js": "5.2.1",
|
|
65
72
|
"elliptic": "6.5.3"
|
|
66
73
|
},
|
|
67
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "f0944b60a5fb4115e1a3727bb07f68174bc56c5c"
|
|
68
75
|
}
|