Skip to content

Commit 51e800f

Browse files
committed
refactor: Fix eslint errors
1 parent ab92062 commit 51e800f

14 files changed

+31
-20
lines changed

src/assert.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Assert {
77
* @param data
88
* @throws TypeError
99
*/
10-
public static isSession(data: any): asserts data is graphene.Session {
10+
public static isSession(data: unknown): asserts data is graphene.Session {
1111
if (!(data instanceof graphene.Session)) {
1212
throw new TypeError("PKCS#11 session is not initialized");
1313
}
@@ -18,7 +18,7 @@ export class Assert {
1818
* @param data
1919
* @throws TypeError
2020
*/
21-
public static isModule(data: any): asserts data is graphene.Module {
21+
public static isModule(data: unknown): asserts data is graphene.Module {
2222
if (!(data instanceof graphene.Module)) {
2323
throw new TypeError("PKCS#11 module is not initialized");
2424
}
@@ -29,13 +29,13 @@ export class Assert {
2929
* @param data
3030
* @throws TypeError
3131
*/
32-
public static isCryptoKey(data: any): asserts data is CryptoKey {
32+
public static isCryptoKey(data: unknown): asserts data is CryptoKey {
3333
if (!(data instanceof CryptoKey)) {
3434
throw new TypeError("Object is not an instance of PKCS#11 CryptoKey");
3535
}
3636
}
3737

38-
public static requiredParameter(parameter: any, parameterName: string): asserts parameter {
38+
public static requiredParameter(parameter: unknown, parameterName: string): asserts parameter {
3939
if (!parameter) {
4040
throw new Error(`Absent mandatory parameter "${parameterName}"`);
4141
}

src/cert_storage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class CertificateStorage implements core.CryptoCertificateStorage, IGetVa
5454
const keys: string[] = [];
5555
TEMPLATES.forEach((template) => {
5656
this.crypto.session!.find(template, (obj) => {
57-
const item = obj.toType<any>();
57+
const item = obj.toType<graphene.X509Certificate>();
5858
const id = certs.CryptoCertificate.getID(item);
5959
keys.push(id);
6060
});
@@ -134,7 +134,7 @@ export class CertificateStorage implements core.CryptoCertificateStorage, IGetVa
134134
}
135135
});
136136
const obj = this.crypto.session.copy(data.p11Object, template);
137-
return certs.CryptoCertificate.getID(obj.toType<any>());
137+
return certs.CryptoCertificate.getID(obj.toType<graphene.X509Certificate>());
138138
} else {
139139
return data.id;
140140
}
@@ -230,7 +230,7 @@ export class CertificateStorage implements core.CryptoCertificateStorage, IGetVa
230230
let object: graphene.SessionObject | null = null;
231231
TEMPLATES.forEach((template) => {
232232
this.crypto.session!.find(template, (obj) => {
233-
const item = obj.toType<any>();
233+
const item = obj.toType<graphene.X509Certificate>();
234234
if (id === certs.CryptoCertificate.getID(item)) {
235235
object = item;
236236
return false;

src/key.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class CryptoKey<T extends Pkcs11KeyAlgorithm = Pkcs11KeyAlgorithm> extend
8282
}
8383
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8484
const { name, ...defaultAlg } = CryptoKey.defaultKeyAlgorithm();
85-
this.algorithm = { ...alg, ...defaultAlg } as any;
85+
this.algorithm = { ...alg, ...defaultAlg } as T;
8686
this.id = CryptoKey.getID(key);
8787

8888
if (usages) {

src/key_storage.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class KeyStorage implements core.CryptoKeyStorage {
2121
const keys: string[] = [];
2222
OBJECT_TYPES.forEach((objectClass) => {
2323
this.crypto.session!.find({ class: objectClass, token: true }, (obj) => {
24-
const item = obj.toType<any>();
24+
const item = obj.toType<graphene.Key>();
2525
keys.push(CryptoKey.getID(item));
2626
});
2727
});
@@ -51,6 +51,7 @@ export class KeyStorage implements core.CryptoKeyStorage {
5151
/** @deprecated Use getItem(index, algorithm, extractable, keyUsages) */
5252
public async getItem(key: string, algorithm: Algorithm, usages: KeyUsage[]): Promise<CryptoKey>;
5353
public async getItem(index: string, algorithm: core.ImportAlgorithms, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
54+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5455
public async getItem(key: string, ...args: any[]): Promise<CryptoKey> {
5556
const subjectObject = this.getItemById(key);
5657
if (subjectObject) {
@@ -86,6 +87,7 @@ export class KeyStorage implements core.CryptoKeyStorage {
8687
} else {
8788
alg.name = "RSA-OAEP";
8889
}
90+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8991
(alg as any).hash = { name: "SHA-256" };
9092
break;
9193
}
@@ -172,7 +174,7 @@ export class KeyStorage implements core.CryptoKeyStorage {
172174
}
173175
});
174176
const obj = this.crypto.session.copy(p11Key.key, template);
175-
return CryptoKey.getID(obj.toType<any>());
177+
return CryptoKey.getID(obj.toType<graphene.Key>());
176178
} else {
177179
return data.id;
178180
}
@@ -189,7 +191,7 @@ export class KeyStorage implements core.CryptoKeyStorage {
189191
let key: graphene.SessionObject | null = null;
190192
OBJECT_TYPES.forEach((objectClass) => {
191193
this.crypto.session!.find({ class: objectClass, token: true }, (obj) => {
192-
const item = obj.toType<any>();
194+
const item = obj.toType<graphene.Key>();
193195
if (id === CryptoKey.getID(item)) {
194196
key = item;
195197
return false;

src/mechs/ec/crypto.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class EcCrypto implements types.IContainer {
6363
privateKey: new EcCryptoKey(keys.privateKey, algorithm),
6464
publicKey: new EcCryptoKey(keys.publicKey, algorithm),
6565
};
66-
resolve(wcKeyPair as any);
66+
resolve(wcKeyPair);
6767
}
6868
} catch (e) {
6969
reject(e);
@@ -107,7 +107,7 @@ export class EcCrypto implements types.IContainer {
107107
public async importKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: types.Pkcs11EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
108108
switch (format.toLowerCase()) {
109109
case "jwk": {
110-
const jwk: any = keyData;
110+
const jwk = keyData as JsonWebKey;
111111
if (jwk.d) {
112112
return this.importJwkPrivateKey(jwk, algorithm, extractable, keyUsages);
113113
} else {

src/mechs/ec/ec_dh.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class EcdhProvider extends core.EcdhProvider implements types.IContainer
8484
name: "ECDH1_DERIVE",
8585
params: new graphene.EcdhParams(
8686
graphene.EcKdf.NULL,
87-
null as any,
87+
null,
8888
ecPoint, // CKA_EC_POINT
8989
),
9090
},

src/mechs/hmac/hmac.ts

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export class HmacProvider extends core.HmacProvider implements types.IContainer
101101
const hmacAlg = {
102102
...algorithm,
103103
name: this.name,
104+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104105
length: value.byteLength * 8 || this.getDefaultLength((algorithm as any).hash.name),
105106
} as types.Pkcs11HmacKeyAlgorithm;
106107
const template: graphene.ITemplate = this.createTemplate({

src/mechs/rsa/crypto.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class RsaCrypto implements types.IContainer {
7070
privateKey: new RsaCryptoKey(keys.privateKey, algorithm),
7171
publicKey: new RsaCryptoKey(keys.publicKey, algorithm),
7272
};
73-
resolve(wcKeyPair as any);
73+
resolve(wcKeyPair);
7474
}
7575
} catch (e) {
7676
reject(e);
@@ -109,7 +109,7 @@ export class RsaCrypto implements types.IContainer {
109109
public async importKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
110110
switch (format.toLowerCase()) {
111111
case "jwk": {
112-
const jwk: any = keyData;
112+
const jwk = keyData as JsonWebKey;
113113
if (jwk.d) {
114114
return this.importJwkPrivateKey(jwk, algorithm as RsaHashedKeyGenParams, extractable, keyUsages);
115115
} else {

src/mechs/rsa/rsa-pss.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class RsaPssProvider extends core.RsaPssProvider implements types.IContai
3636
const mechanism = this.wc2pk11(algorithm, key.algorithm as RsaHashedKeyAlgorithm);
3737
mechanism.name = this.crypto.getAlgorithm(this.name, mechanism.name);
3838
if (mechanism.name === "RSA_PKCS_PSS") {
39+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3940
buf = this.crypto.prepareData((key as any).algorithm.hash.name, buf);
4041
}
4142
const signer = this.container.session.createSign(mechanism, key.key);
@@ -67,6 +68,7 @@ export class RsaPssProvider extends core.RsaPssProvider implements types.IContai
6768
const mechanism = this.wc2pk11(algorithm, key.algorithm as RsaHashedKeyAlgorithm);
6869
mechanism.name = this.crypto.getAlgorithm(this.name, mechanism.name);
6970
if (mechanism.name === "RSA_PKCS_PSS") {
71+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7072
buf = this.crypto.prepareData((key as any).algorithm.hash.name, buf);
7173
}
7274
this.container.session.createVerify(mechanism, key.key).once(buf, Buffer.from(signature), (err, data2) => {

src/mechs/rsa/rsa-ssa.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class RsaSsaProvider extends core.RsaSsaProvider implements types.IContai
3535
const mechanism = this.wc2pk11(algorithm, key.algorithm as RsaHashedKeyAlgorithm);
3636
mechanism.name = this.crypto.getAlgorithm(this.name, mechanism.name);
3737
if (mechanism.name === "RSA_PKCS") {
38+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3839
buf = this.crypto.prepareData((key as any).algorithm.hash.name, buf);
3940
}
4041
const signer = this.container.session.createSign(mechanism, key.key);
@@ -66,6 +67,7 @@ export class RsaSsaProvider extends core.RsaSsaProvider implements types.IContai
6667
const mechanism = this.wc2pk11(algorithm, key.algorithm as RsaHashedKeyAlgorithm);
6768
mechanism.name = this.crypto.getAlgorithm(this.name, mechanism.name);
6869
if (mechanism.name === "RSA_PKCS") {
70+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6971
buf = this.crypto.prepareData((key as any).algorithm.hash.name, buf);
7072
}
7173
this.container.session.createVerify(mechanism, key.key).once(buf, Buffer.from(signature), (err, data2) => {

src/utils.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ export function prepareData(data: BufferSource): Buffer {
2626
return Buffer.from(pvtsutils.BufferSourceConverter.toArrayBuffer(data));
2727
}
2828

29-
export function isHashedAlgorithm(data: any): data is HashedAlgorithm {
29+
export function isHashedAlgorithm(data: unknown): data is HashedAlgorithm {
3030
return data instanceof Object
3131
&& "name" in data
3232
&& "hash" in data;
3333
}
3434

35-
export function isCryptoKeyPair(data: any): data is CryptoKeyPair {
36-
return data && data.privateKey && data.publicKey;
35+
export function isCryptoKeyPair(data: unknown): data is CryptoKeyPair {
36+
return data instanceof Object
37+
&& "privateKey" in data
38+
&& "publicKey" in data;
3739
}
3840

3941
export function prepareAlgorithm(algorithm: AlgorithmIdentifier): Algorithm {

test/cert_storage.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import * as assert from "assert";
23
import * as graphene from "graphene-pk11";
34
import * as x509 from "@peculiar/x509";

test/helper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { config } from "./config";
88
*/
99
export function isKeyEqual(a: CryptoKey, b: CryptoKey): boolean {
1010
if (a instanceof CryptoKey && b instanceof CryptoKey) {
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112
return (a as any).data.equals((b as any).data);
1213
}
1314
return false;

test/key_storage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ctx("KeyStorage", () => {
2727
modulusLength: 1024,
2828
};
2929
const keys = await crypto.subtle.generateKey(algorithm, false, ["sign", "verify"]);
30-
const key = (keys as any)[type] as CryptoKey;
30+
const key = (keys as unknown as Record<string, CryptoKey>)[type] as CryptoKey;
3131

3232
const index = await crypto.keyStorage.setItem(key);
3333
const found = await crypto.keyStorage.indexOf(key);

0 commit comments

Comments
 (0)