@ocap/mcrypto 1.27.16 → 1.28.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/esm/crypter/aes-legacy.d.ts +8 -6
- package/esm/crypter/aes-legacy.js +27 -24
- package/esm/crypter/aes.d.ts +9 -7
- package/esm/crypter/aes.js +27 -25
- package/esm/crypter/rsa-browserify.d.ts +7 -6
- package/esm/crypter/rsa-browserify.js +34 -31
- package/esm/crypter/rsa.d.ts +11 -9
- package/esm/crypter/rsa.js +31 -28
- package/esm/encode.d.ts +12 -8
- package/esm/encode.js +13 -18
- package/esm/hasher/keccak.d.ts +8 -5
- package/esm/hasher/keccak.js +37 -36
- package/esm/hasher/sha2.d.ts +8 -5
- package/esm/hasher/sha2.js +42 -40
- package/esm/hasher/sha3.d.ts +8 -5
- package/esm/hasher/sha3.js +37 -36
- package/esm/index.d.ts +148 -143
- package/esm/index.js +172 -209
- package/esm/protocols/crypter.d.ts +3 -1
- package/esm/protocols/crypter.js +7 -4
- package/esm/protocols/hasher.d.ts +3 -1
- package/esm/protocols/hasher.js +7 -4
- package/esm/protocols/signer.d.ts +3 -1
- package/esm/protocols/signer.js +7 -4
- package/esm/signer/ed25519.d.ts +49 -46
- package/esm/signer/ed25519.js +80 -80
- package/esm/signer/ethereum.d.ts +11 -8
- package/esm/signer/ethereum.js +34 -34
- package/esm/signer/passkey.d.ts +21 -18
- package/esm/signer/passkey.js +49 -58
- package/esm/signer/secp256k1.d.ts +35 -32
- package/esm/signer/secp256k1.js +80 -93
- package/lib/_virtual/rolldown_runtime.js +29 -0
- package/lib/crypter/aes-legacy.d.ts +8 -6
- package/lib/crypter/aes-legacy.js +35 -52
- package/lib/crypter/aes.d.ts +9 -7
- package/lib/crypter/aes.js +29 -29
- package/lib/crypter/rsa-browserify.d.ts +7 -6
- package/lib/crypter/rsa-browserify.js +36 -33
- package/lib/crypter/rsa.d.ts +11 -9
- package/lib/crypter/rsa.js +33 -32
- package/lib/encode.d.ts +12 -8
- package/lib/encode.js +14 -21
- package/lib/hasher/keccak.d.ts +8 -5
- package/lib/hasher/keccak.js +40 -41
- package/lib/hasher/sha2.d.ts +8 -5
- package/lib/hasher/sha2.js +43 -42
- package/lib/hasher/sha3.d.ts +8 -5
- package/lib/hasher/sha3.js +40 -41
- package/lib/index.d.ts +148 -143
- package/lib/index.js +177 -216
- package/lib/protocols/crypter.d.ts +3 -1
- package/lib/protocols/crypter.js +10 -9
- package/lib/protocols/hasher.d.ts +3 -1
- package/lib/protocols/hasher.js +10 -9
- package/lib/protocols/signer.d.ts +3 -1
- package/lib/protocols/signer.js +10 -9
- package/lib/signer/ed25519.d.ts +49 -46
- package/lib/signer/ed25519.js +85 -86
- package/lib/signer/ethereum.d.ts +11 -8
- package/lib/signer/ethereum.js +38 -39
- package/lib/signer/passkey.d.ts +21 -18
- package/lib/signer/passkey.js +51 -63
- package/lib/signer/secp256k1.d.ts +35 -32
- package/lib/signer/secp256k1.js +85 -99
- package/package.json +25 -10
package/lib/signer/secp256k1.js
CHANGED
|
@@ -1,101 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const EC = elliptic_1.default.ec;
|
|
15
|
-
const secp256k1 = new EC('secp256k1');
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
|
|
3
|
+
const require_encode = require('../encode.js');
|
|
4
|
+
const require_protocols_signer = require('../protocols/signer.js');
|
|
5
|
+
let _ocap_util = require("@ocap/util");
|
|
6
|
+
let randombytes = require("randombytes");
|
|
7
|
+
randombytes = require_rolldown_runtime.__toESM(randombytes);
|
|
8
|
+
let elliptic = require("elliptic");
|
|
9
|
+
elliptic = require_rolldown_runtime.__toESM(elliptic);
|
|
10
|
+
|
|
11
|
+
//#region src/signer/secp256k1.ts
|
|
12
|
+
const EC = elliptic.default.ec;
|
|
13
|
+
const secp256k1 = new EC("secp256k1");
|
|
16
14
|
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
msg = (0, util_1.toUint8Array)(message);
|
|
89
|
-
}
|
|
90
|
-
catch (err) {
|
|
91
|
-
// Do nothing;
|
|
92
|
-
}
|
|
93
|
-
let pkBuffer = (0, util_1.toBuffer)(pk);
|
|
94
|
-
if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 0x04 && pkBuffer.byteLength === 64) {
|
|
95
|
-
pkBuffer = Buffer.concat([Buffer.from([0x04]), pkBuffer]);
|
|
96
|
-
}
|
|
97
|
-
return secp256k1.keyFromPublic(pkBuffer).verify((0, util_1.stripHexPrefix)(msg), (0, util_1.stripHexPrefix)((0, util_1.toHex)(signature)));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
15
|
+
* Signer implementation for secp256k1, based on `elliptic`
|
|
16
|
+
*
|
|
17
|
+
* @class Secp256k1Signer
|
|
18
|
+
*/
|
|
19
|
+
var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
this.pkHasFormatPrefix = true;
|
|
23
|
+
this.pkCompressed = false;
|
|
24
|
+
}
|
|
25
|
+
isValidSK(sk) {
|
|
26
|
+
if (sk.byteLength !== 32) return false;
|
|
27
|
+
const bn = new _ocap_util.BN(sk);
|
|
28
|
+
return bn.cmp(secp256k1.curve.n) < 0 && !bn.isZero();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @public
|
|
32
|
+
* @typedefKeyPairType
|
|
33
|
+
* @prop {string} publicKey - publicKey in hex format
|
|
34
|
+
* @prop {string} secretKey - secretKey in hex format
|
|
35
|
+
* @memberof Secp256k1Signer
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* Generate random secret/public key pair
|
|
39
|
+
*/
|
|
40
|
+
genKeyPair(encoding = "hex") {
|
|
41
|
+
let sk = null;
|
|
42
|
+
do
|
|
43
|
+
sk = new Uint8Array((0, randombytes.default)(32));
|
|
44
|
+
while (!this.isValidSK(sk));
|
|
45
|
+
const pk = this.getPublicKey((0, _ocap_util.toHex)(sk));
|
|
46
|
+
return {
|
|
47
|
+
secretKey: require_encode.encode(sk, encoding),
|
|
48
|
+
publicKey: require_encode.encode(pk, encoding)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get publicKey from secretKey
|
|
53
|
+
*/
|
|
54
|
+
getPublicKey(sk, encoding = "hex") {
|
|
55
|
+
if (!this.isValidSK((0, _ocap_util.toUint8Array)(sk))) throw new Error("Invalid secret key");
|
|
56
|
+
let pk = secp256k1.keyFromPrivate((0, _ocap_util.toBuffer)(sk)).getPublic(this.pkCompressed, "hex");
|
|
57
|
+
if (this.pkHasFormatPrefix === false) pk = pk.slice(2);
|
|
58
|
+
return require_encode.encode(`0x${pk}`, encoding);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sign a message and get the signature hex
|
|
62
|
+
*/
|
|
63
|
+
sign(message, sk, encoding = "hex") {
|
|
64
|
+
let msg = message;
|
|
65
|
+
try {
|
|
66
|
+
msg = (0, _ocap_util.toUint8Array)(message);
|
|
67
|
+
} catch (err) {}
|
|
68
|
+
return require_encode.encode(`0x${secp256k1.keyFromPrivate((0, _ocap_util.toBuffer)(sk)).sign((0, _ocap_util.stripHexPrefix)(msg), { canonical: true }).toDER("hex")}`, encoding);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Verify if a signature is valid
|
|
72
|
+
*/
|
|
73
|
+
verify(message, signature, pk) {
|
|
74
|
+
let msg = message;
|
|
75
|
+
try {
|
|
76
|
+
msg = (0, _ocap_util.toUint8Array)(message);
|
|
77
|
+
} catch (err) {}
|
|
78
|
+
let pkBuffer = (0, _ocap_util.toBuffer)(pk);
|
|
79
|
+
if (this.pkHasFormatPrefix === false && pkBuffer[0] !== 4 && pkBuffer.byteLength === 64) pkBuffer = Buffer.concat([Buffer.from([4]), pkBuffer]);
|
|
80
|
+
return secp256k1.keyFromPublic(pkBuffer).verify((0, _ocap_util.stripHexPrefix)(msg), (0, _ocap_util.stripHexPrefix)((0, _ocap_util.toHex)(signature)));
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var secp256k1_default = new Secp256k1Signer();
|
|
84
|
+
|
|
85
|
+
//#endregion
|
|
100
86
|
exports.Secp256k1Signer = Secp256k1Signer;
|
|
101
|
-
exports.default =
|
|
87
|
+
exports.default = secp256k1_default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.28.0",
|
|
4
4
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -21,8 +21,26 @@
|
|
|
21
21
|
],
|
|
22
22
|
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/mcrypto",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
|
+
"sideEffects": false,
|
|
24
25
|
"main": "./lib/index.js",
|
|
25
|
-
"
|
|
26
|
+
"module": "./esm/index.js",
|
|
27
|
+
"types": "./esm/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./esm/index.d.ts",
|
|
32
|
+
"default": "./esm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./lib/index.d.ts",
|
|
36
|
+
"default": "./lib/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./lib/*": {
|
|
40
|
+
"types": "./lib/*.d.ts",
|
|
41
|
+
"require": "./lib/*.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
26
44
|
"files": [
|
|
27
45
|
"lib",
|
|
28
46
|
"esm"
|
|
@@ -35,10 +53,11 @@
|
|
|
35
53
|
"@types/randombytes": "^2.0.3",
|
|
36
54
|
"eslint": "^8.57.0",
|
|
37
55
|
"jest": "^29.7.0",
|
|
56
|
+
"tsdown": "^0.18.4",
|
|
38
57
|
"ts-jest": "^29.2.5",
|
|
39
58
|
"type-fest": "^3.1.0",
|
|
40
59
|
"typescript": "^5.6.2",
|
|
41
|
-
"@ocap/e2e-test": "1.
|
|
60
|
+
"@ocap/e2e-test": "1.28.0"
|
|
42
61
|
},
|
|
43
62
|
"repository": {
|
|
44
63
|
"type": "git",
|
|
@@ -58,7 +77,7 @@
|
|
|
58
77
|
"js-sha3": "^0.8.0",
|
|
59
78
|
"randombytes": "^2.1.0",
|
|
60
79
|
"tweetnacl": "^1.0.3",
|
|
61
|
-
"@ocap/util": "1.
|
|
80
|
+
"@ocap/util": "1.28.0"
|
|
62
81
|
},
|
|
63
82
|
"resolutions": {
|
|
64
83
|
"bn.js": "5.2.2",
|
|
@@ -70,11 +89,7 @@
|
|
|
70
89
|
"prepush": "CI=1 npm run test",
|
|
71
90
|
"test": "jest --forceExit --detectOpenHandles",
|
|
72
91
|
"coverage": "npm run test -- --coverage",
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
76
|
-
"build:esm": "tsc -p tsconfig.esm.json",
|
|
77
|
-
"build": "pnpm run build:cjs && pnpm run build:esm",
|
|
78
|
-
"build:watch": "pnpm run build -- -w"
|
|
92
|
+
"build": "tsdown",
|
|
93
|
+
"build:watch": "tsdown --watch"
|
|
79
94
|
}
|
|
80
95
|
}
|