@ocap/mcrypto 1.18.166 → 1.19.1
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 +7 -7
- package/esm/crypter/aes-legacy.d.ts +8 -0
- package/esm/crypter/aes-legacy.js +26 -0
- package/esm/crypter/aes.d.ts +9 -0
- package/esm/crypter/aes.js +25 -0
- package/esm/crypter/rsa-browserify.d.ts +9 -0
- package/esm/crypter/rsa-browserify.js +33 -0
- package/esm/crypter/rsa.d.ts +11 -0
- package/esm/crypter/rsa.js +28 -0
- package/esm/encode.d.ts +8 -0
- package/esm/encode.js +19 -0
- package/esm/hasher/keccak.d.ts +13 -0
- package/esm/hasher/keccak.js +37 -0
- package/esm/hasher/sha2.d.ts +13 -0
- package/esm/hasher/sha2.js +43 -0
- package/esm/hasher/sha3.d.ts +13 -0
- package/esm/hasher/sha3.js +37 -0
- package/esm/index.d.ts +233 -0
- package/esm/index.js +219 -0
- package/esm/protocols/crypter.d.ts +2 -0
- package/esm/protocols/crypter.js +4 -0
- package/esm/protocols/hasher.d.ts +2 -0
- package/esm/protocols/hasher.js +4 -0
- package/esm/protocols/signer.d.ts +2 -0
- package/esm/protocols/signer.js +4 -0
- package/esm/signer/ed25519.d.ts +53 -0
- package/esm/signer/ed25519.js +82 -0
- package/esm/signer/ethereum.d.ts +16 -0
- package/esm/signer/ethereum.js +35 -0
- package/esm/signer/passkey.d.ts +27 -0
- package/esm/signer/passkey.js +59 -0
- package/esm/signer/secp256k1.d.ts +39 -0
- package/esm/signer/secp256k1.js +95 -0
- package/lib/crypter/rsa-browserify.js +1 -1
- package/lib/crypter/rsa.d.ts +0 -1
- package/lib/encode.d.ts +0 -1
- package/lib/encode.js +1 -2
- package/lib/index.d.ts +9 -7
- package/lib/index.js +9 -4
- package/lib/signer/passkey.d.ts +27 -0
- package/lib/signer/passkey.js +65 -0
- package/package.json +29 -14
package/lib/encode.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.encode =
|
|
3
|
+
exports.encode = encode;
|
|
4
4
|
const util_1 = require("@ocap/util");
|
|
5
5
|
function encode(data, encoding = 'hex') {
|
|
6
6
|
if (['hex', 'base16'].includes(encoding)) {
|
|
@@ -20,4 +20,3 @@ function encode(data, encoding = 'hex') {
|
|
|
20
20
|
}
|
|
21
21
|
return data;
|
|
22
22
|
}
|
|
23
|
-
exports.encode = encode;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import type { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
3
2
|
import type { LiteralUnion } from 'type-fest';
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
3
|
+
export type KeyType = LiteralUnion<'ED25519' | 'SECP256K1' | 'ETHEREUM', string>;
|
|
4
|
+
export type HashType = LiteralUnion<'KECCAK' | 'SHA3' | 'KECCAK_384' | 'SHA3_384' | 'KECCAK_512' | 'SHA3_512' | 'SHA2', string>;
|
|
5
|
+
export 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_STORE' | 'ROLE_TOKEN' | 'ROLE_FACTORY' | 'ROLE_ROLLUP' | 'ROLE_STORAGE' | 'ROLE_PROFILE' | 'ROLE_PASSKEY' | 'ROLE_ANY', string>;
|
|
6
|
+
export type AddressType = LiteralUnion<'BASE16' | 'BASE58', string>;
|
|
8
7
|
export interface HashFnType {
|
|
9
8
|
(data: BytesType, round: number, encoding?: 'hex'): string;
|
|
10
9
|
(data: BytesType, round: number, encoding?: 'base16'): string;
|
|
@@ -30,7 +29,7 @@ export interface SignerType {
|
|
|
30
29
|
sign(data: BytesType, sk: BytesType, encoding?: 'buffer'): Buffer;
|
|
31
30
|
sign(data: BytesType, sk: BytesType, encoding?: 'Uint8Array'): Uint8Array;
|
|
32
31
|
sign(data: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
33
|
-
verify(data: BytesType, pk: BytesType, signature: BytesType): boolean;
|
|
32
|
+
verify(data: BytesType, pk: BytesType, signature: BytesType, extra?: any): boolean;
|
|
34
33
|
ethHash?(data: string): string;
|
|
35
34
|
ethSign?(data: string, sk: string): string;
|
|
36
35
|
ethVerify?(data: string, pk: string, signature: BytesType): boolean;
|
|
@@ -57,6 +56,7 @@ export declare const Signer: {
|
|
|
57
56
|
Ed25519: import("./signer/ed25519").Ed25519Signer;
|
|
58
57
|
Secp256k1: import("./signer/secp256k1").Secp256k1Signer;
|
|
59
58
|
Ethereum: import("./signer/ethereum").EthereumSigner;
|
|
59
|
+
Passkey: import("./signer/passkey").PasskeySigner;
|
|
60
60
|
};
|
|
61
61
|
/**
|
|
62
62
|
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
@@ -106,6 +106,7 @@ export declare const types: {
|
|
|
106
106
|
ED25519: number;
|
|
107
107
|
SECP256K1: number;
|
|
108
108
|
ETHEREUM: number;
|
|
109
|
+
PASSKEY: number;
|
|
109
110
|
};
|
|
110
111
|
/**
|
|
111
112
|
* Hashing algorithms
|
|
@@ -157,6 +158,7 @@ export declare const types: {
|
|
|
157
158
|
ROLE_ROLLUP: number;
|
|
158
159
|
ROLE_STORAGE: number;
|
|
159
160
|
ROLE_PROFILE: number;
|
|
161
|
+
ROLE_PASSKEY: number;
|
|
160
162
|
ROLE_ANY: number;
|
|
161
163
|
};
|
|
162
164
|
/**
|
|
@@ -223,7 +225,7 @@ export declare function getRandomBytes(length: number, encoding?: 'buffer'): Buf
|
|
|
223
225
|
export declare function getRandomBytes(length: number, encoding?: 'Uint8Array'): Uint8Array;
|
|
224
226
|
export declare function getRandomBytes(length: number, encoding?: EncodingType): BytesType;
|
|
225
227
|
export declare const Signers: Readonly<{
|
|
226
|
-
[x: number]: import("./signer/ed25519").Ed25519Signer | import("./signer/secp256k1").Secp256k1Signer;
|
|
228
|
+
[x: number]: import("./signer/ed25519").Ed25519Signer | import("./signer/secp256k1").Secp256k1Signer | import("./signer/passkey").PasskeySigner;
|
|
227
229
|
}>;
|
|
228
230
|
export declare const Hashers: Readonly<{
|
|
229
231
|
[x: number]: (data: BytesType, round?: number, encoding?: EncodingType) => BytesType;
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.toTxHash = exports.Hashers = exports.Signers = exports.
|
|
6
|
+
exports.toTxHash = exports.Hashers = exports.Signers = exports.types = exports.Hasher = exports.Signer = void 0;
|
|
7
|
+
exports.getSigner = getSigner;
|
|
8
|
+
exports.getHasher = getHasher;
|
|
9
|
+
exports.getRandomBytes = getRandomBytes;
|
|
7
10
|
const randombytes_1 = __importDefault(require("randombytes"));
|
|
8
11
|
const encode_1 = require("./encode");
|
|
9
12
|
const keccak_1 = __importDefault(require("./hasher/keccak"));
|
|
@@ -12,6 +15,7 @@ const sha3_1 = __importDefault(require("./hasher/sha3"));
|
|
|
12
15
|
const ed25519_1 = __importDefault(require("./signer/ed25519"));
|
|
13
16
|
const ethereum_1 = __importDefault(require("./signer/ethereum"));
|
|
14
17
|
const secp256k1_1 = __importDefault(require("./signer/secp256k1"));
|
|
18
|
+
const passkey_1 = __importDefault(require("./signer/passkey"));
|
|
15
19
|
/**
|
|
16
20
|
* Contains all supported signers, eg: `Ed25519` and `Secp256k1`
|
|
17
21
|
*
|
|
@@ -33,6 +37,7 @@ exports.Signer = {
|
|
|
33
37
|
Ed25519: ed25519_1.default,
|
|
34
38
|
Secp256k1: secp256k1_1.default,
|
|
35
39
|
Ethereum: ethereum_1.default,
|
|
40
|
+
Passkey: passkey_1.default,
|
|
36
41
|
};
|
|
37
42
|
/**
|
|
38
43
|
* Contains all supported hasher, eg: `SHA2`,`SHA3` and `Keccak`, each of them supports `hash224`, `hash256`, `hash384`, `hash512`
|
|
@@ -82,6 +87,7 @@ exports.types = {
|
|
|
82
87
|
ED25519: 0,
|
|
83
88
|
SECP256K1: 1,
|
|
84
89
|
ETHEREUM: 2,
|
|
90
|
+
PASSKEY: 3,
|
|
85
91
|
},
|
|
86
92
|
/**
|
|
87
93
|
* Hashing algorithms
|
|
@@ -133,6 +139,7 @@ exports.types = {
|
|
|
133
139
|
ROLE_ROLLUP: 19,
|
|
134
140
|
ROLE_STORAGE: 20,
|
|
135
141
|
ROLE_PROFILE: 21,
|
|
142
|
+
ROLE_PASSKEY: 22,
|
|
136
143
|
ROLE_ANY: 63,
|
|
137
144
|
},
|
|
138
145
|
/**
|
|
@@ -175,7 +182,6 @@ function getSigner(type) {
|
|
|
175
182
|
// @ts-ignore
|
|
176
183
|
return exports.Signers[type];
|
|
177
184
|
}
|
|
178
|
-
exports.getSigner = getSigner;
|
|
179
185
|
/**
|
|
180
186
|
* Get hasher instance
|
|
181
187
|
*
|
|
@@ -201,15 +207,14 @@ function getHasher(type) {
|
|
|
201
207
|
// @ts-ignore
|
|
202
208
|
return exports.Hashers[type];
|
|
203
209
|
}
|
|
204
|
-
exports.getHasher = getHasher;
|
|
205
210
|
function getRandomBytes(length = 32, encoding = 'hex') {
|
|
206
211
|
return (0, encode_1.encode)((0, randombytes_1.default)(length), encoding);
|
|
207
212
|
}
|
|
208
|
-
exports.getRandomBytes = getRandomBytes;
|
|
209
213
|
exports.Signers = Object.freeze({
|
|
210
214
|
[exports.types.KeyType.ED25519]: exports.Signer.Ed25519,
|
|
211
215
|
[exports.types.KeyType.SECP256K1]: exports.Signer.Secp256k1,
|
|
212
216
|
[exports.types.KeyType.ETHEREUM]: exports.Signer.Ethereum,
|
|
217
|
+
[exports.types.KeyType.PASSKEY]: exports.Signer.Passkey,
|
|
213
218
|
});
|
|
214
219
|
exports.Hashers = Object.freeze({
|
|
215
220
|
[exports.types.HashType.KECCAK]: exports.Hasher.Keccak.hash256,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BytesType, EncodingType, KeyPairType } from '@ocap/util';
|
|
2
|
+
import BaseSigner from '../protocols/signer';
|
|
3
|
+
/**
|
|
4
|
+
* Signer implementation for passkey, based on `@simplewebauthn/server`
|
|
5
|
+
* Since passkey supports only verification, we do not need to implement the sign method
|
|
6
|
+
* And passkeys can used multiple algorithms, we do not need to implement the algorithm selection
|
|
7
|
+
*
|
|
8
|
+
* @class PasskeySigner
|
|
9
|
+
*/
|
|
10
|
+
declare class PasskeySigner extends BaseSigner {
|
|
11
|
+
constructor();
|
|
12
|
+
genKeyPair(encoding?: EncodingType, userSeed?: BytesType): KeyPairType;
|
|
13
|
+
getPublicKey(sk: BytesType, encoding?: EncodingType): BytesType;
|
|
14
|
+
sign(message: BytesType, sk: BytesType, encoding?: EncodingType): BytesType;
|
|
15
|
+
/**
|
|
16
|
+
* Verify if a signature is valid
|
|
17
|
+
*
|
|
18
|
+
* @param {string|buffer} challenge - the challenge sent to passkey, should be txHash when signing a transaction
|
|
19
|
+
* @param {string|buffer} signature - signature from passkey
|
|
20
|
+
* @param {string|buffer} pk - credentialPublicKey from passkey, must be parsed with `parseAuthenticatorData`
|
|
21
|
+
* @returns {bool}
|
|
22
|
+
*/
|
|
23
|
+
verify(challenge: BytesType, signature: BytesType, pk: BytesType, extra: string): Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
declare const _default: PasskeySigner;
|
|
26
|
+
export default _default;
|
|
27
|
+
export { PasskeySigner };
|
|
@@ -0,0 +1,65 @@
|
|
|
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.PasskeySigner = void 0;
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-useless-constructor */
|
|
8
|
+
const util_1 = require("@ocap/util");
|
|
9
|
+
const helpers_1 = require("@simplewebauthn/server/helpers");
|
|
10
|
+
const signer_1 = __importDefault(require("../protocols/signer"));
|
|
11
|
+
/**
|
|
12
|
+
* Signer implementation for passkey, based on `@simplewebauthn/server`
|
|
13
|
+
* Since passkey supports only verification, we do not need to implement the sign method
|
|
14
|
+
* And passkeys can used multiple algorithms, we do not need to implement the algorithm selection
|
|
15
|
+
*
|
|
16
|
+
* @class PasskeySigner
|
|
17
|
+
*/
|
|
18
|
+
class PasskeySigner extends signer_1.default {
|
|
19
|
+
constructor() {
|
|
20
|
+
super();
|
|
21
|
+
}
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
genKeyPair(encoding = 'hex', userSeed) {
|
|
24
|
+
throw new Error('Not supported');
|
|
25
|
+
}
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
27
|
+
getPublicKey(sk, encoding = 'hex') {
|
|
28
|
+
throw new Error('Not supported');
|
|
29
|
+
}
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
31
|
+
sign(message, sk, encoding = 'hex') {
|
|
32
|
+
throw new Error('Not supported');
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Verify if a signature is valid
|
|
36
|
+
*
|
|
37
|
+
* @param {string|buffer} challenge - the challenge sent to passkey, should be txHash when signing a transaction
|
|
38
|
+
* @param {string|buffer} signature - signature from passkey
|
|
39
|
+
* @param {string|buffer} pk - credentialPublicKey from passkey, must be parsed with `parseAuthenticatorData`
|
|
40
|
+
* @returns {bool}
|
|
41
|
+
*/
|
|
42
|
+
async verify(challenge, signature, pk, extra) {
|
|
43
|
+
const parsed = JSON.parse(extra);
|
|
44
|
+
if (!parsed.authenticatorData || !parsed.clientDataJSON) {
|
|
45
|
+
throw new Error('extra.authenticatorData or extra.clientDataJSON is required for passkey signature verification');
|
|
46
|
+
}
|
|
47
|
+
const authDataBuffer = (0, util_1.toBuffer)((0, util_1.fromBase64)(parsed.authenticatorData));
|
|
48
|
+
const clientDataHash = await (0, helpers_1.toHash)(helpers_1.isoBase64URL.toBuffer(parsed.clientDataJSON));
|
|
49
|
+
const clientData = (0, helpers_1.decodeClientDataJSON)(parsed.clientDataJSON);
|
|
50
|
+
if (clientData.challenge !== (0, util_1.toBase64)(challenge)) {
|
|
51
|
+
throw new Error('challenge mismatch for passkey signature');
|
|
52
|
+
}
|
|
53
|
+
// FIXME: @wangshijun add more check here
|
|
54
|
+
// if (clientData.type !== 'ocap.tx.sign') {
|
|
55
|
+
// throw new Error('Invalid client data type');
|
|
56
|
+
// }
|
|
57
|
+
return (0, helpers_1.verifySignature)({
|
|
58
|
+
signature: helpers_1.isoBase64URL.toBuffer(typeof signature === 'string' ? signature : (0, util_1.toBase64)(signature)),
|
|
59
|
+
data: helpers_1.isoUint8Array.concat([authDataBuffer, clientDataHash]),
|
|
60
|
+
credentialPublicKey: pk,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.PasskeySigner = PasskeySigner;
|
|
65
|
+
exports.default = new PasskeySigner();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.1",
|
|
4
4
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"crypto",
|
|
@@ -21,22 +21,34 @@
|
|
|
21
21
|
],
|
|
22
22
|
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/mcrypto",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
|
-
"main": "lib/index.js",
|
|
25
|
-
"
|
|
24
|
+
"main": "./lib/index.js",
|
|
25
|
+
"module": "./lib/index.js",
|
|
26
|
+
"types": "./esm/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./esm/index.js",
|
|
30
|
+
"require": "./lib/index.js",
|
|
31
|
+
"default": "./esm/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./lib/*": {
|
|
34
|
+
"require": "./lib/*.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
26
37
|
"files": [
|
|
27
|
-
"lib"
|
|
38
|
+
"lib",
|
|
39
|
+
"esm"
|
|
28
40
|
],
|
|
29
41
|
"devDependencies": {
|
|
30
|
-
"@arcblock/eslint-config-ts": "0.
|
|
42
|
+
"@arcblock/eslint-config-ts": "0.3.3",
|
|
31
43
|
"@types/crypto-js": "^4.2.2",
|
|
32
|
-
"@types/elliptic": "^6.4.
|
|
33
|
-
"@types/node": "^
|
|
34
|
-
"@types/randombytes": "^2.0.
|
|
35
|
-
"eslint": "^8.
|
|
44
|
+
"@types/elliptic": "^6.4.18",
|
|
45
|
+
"@types/node": "^22.7.5",
|
|
46
|
+
"@types/randombytes": "^2.0.3",
|
|
47
|
+
"eslint": "^8.57.0",
|
|
36
48
|
"jest": "^29.7.0",
|
|
37
49
|
"ts-jest": "^29.2.5",
|
|
38
50
|
"type-fest": "^3.1.0",
|
|
39
|
-
"typescript": "^
|
|
51
|
+
"typescript": "^5.6.2"
|
|
40
52
|
},
|
|
41
53
|
"repository": {
|
|
42
54
|
"type": "git",
|
|
@@ -48,16 +60,19 @@
|
|
|
48
60
|
"prepush": "CI=1 yarn test",
|
|
49
61
|
"test": "jest --forceExit --detectOpenHandles",
|
|
50
62
|
"coverage": "npm run test -- --coverage",
|
|
51
|
-
"clean": "rm -fr lib",
|
|
63
|
+
"clean": "rm -fr lib esm",
|
|
52
64
|
"prebuild": "npm run clean",
|
|
53
|
-
"build": "tsc",
|
|
65
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
66
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
67
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
54
68
|
"build:watch": "npm run build -- -w"
|
|
55
69
|
},
|
|
56
70
|
"bugs": {
|
|
57
71
|
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
58
72
|
},
|
|
59
73
|
"dependencies": {
|
|
60
|
-
"@ocap/util": "1.
|
|
74
|
+
"@ocap/util": "1.19.1",
|
|
75
|
+
"@simplewebauthn/server": "^13.0.0",
|
|
61
76
|
"bn.js": "5.2.1",
|
|
62
77
|
"crypto-js": "^4.2.0",
|
|
63
78
|
"elliptic": "^6.5.4",
|
|
@@ -72,5 +87,5 @@
|
|
|
72
87
|
"bn.js": "5.2.1",
|
|
73
88
|
"elliptic": "6.5.3"
|
|
74
89
|
},
|
|
75
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "21184488172c6c824ebd1714f728ff2aee4a3ac0"
|
|
76
91
|
}
|