@libp2p/crypto 2.0.8-68db79f6b → 2.0.8-74e84bc29

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.
@@ -5,6 +5,7 @@ import forge from 'node-forge/lib/forge.js'
5
5
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
6
6
  import 'node-forge/lib/sha512.js'
7
7
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
+ import { isPromise } from '../util.js'
8
9
  import { exporter } from './exporter.js'
9
10
  import * as pbm from './keys.js'
10
11
  import * as crypto from './rsa.js'
@@ -20,7 +21,7 @@ export class RsaPublicKey {
20
21
  this._key = key
21
22
  }
22
23
 
23
- async verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): Promise<boolean> {
24
+ verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> {
24
25
  return crypto.hashAndVerify(this._key, sig, data)
25
26
  }
26
27
 
@@ -39,14 +40,18 @@ export class RsaPublicKey {
39
40
  return crypto.encrypt(this._key, bytes)
40
41
  }
41
42
 
42
- equals (key: any): boolean {
43
+ equals (key: any): boolean | boolean {
43
44
  return uint8ArrayEquals(this.bytes, key.bytes)
44
45
  }
45
46
 
46
- async hash (): Promise<Uint8Array> {
47
- const { bytes } = await sha256.digest(this.bytes)
47
+ hash (): Uint8Array | Promise<Uint8Array> {
48
+ const p = sha256.digest(this.bytes)
49
+
50
+ if (isPromise(p)) {
51
+ return p.then(({ bytes }) => bytes)
52
+ }
48
53
 
49
- return bytes
54
+ return p.bytes
50
55
  }
51
56
  }
52
57
 
@@ -63,7 +68,7 @@ export class RsaPrivateKey {
63
68
  return crypto.getRandomValues(16)
64
69
  }
65
70
 
66
- async sign (message: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
71
+ sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
67
72
  return crypto.hashAndSign(this._key, message)
68
73
  }
69
74
 
@@ -94,10 +99,14 @@ export class RsaPrivateKey {
94
99
  return uint8ArrayEquals(this.bytes, key.bytes)
95
100
  }
96
101
 
97
- async hash (): Promise<Uint8Array> {
98
- const { bytes } = await sha256.digest(this.bytes)
102
+ hash (): Uint8Array | Promise<Uint8Array> {
103
+ const p = sha256.digest(this.bytes)
104
+
105
+ if (isPromise(p)) {
106
+ return p.then(({ bytes }) => bytes)
107
+ }
99
108
 
100
- return bytes
109
+ return p.bytes
101
110
  }
102
111
 
103
112
  /**
@@ -1,6 +1,7 @@
1
1
  import { CodeError } from '@libp2p/interface/errors'
2
2
  import { secp256k1 as secp } from '@noble/curves/secp256k1'
3
3
  import { sha256 } from 'multiformats/hashes/sha2'
4
+ import { isPromise } from '../util.js'
4
5
  import type { Uint8ArrayList } from 'uint8arraylist'
5
6
 
6
7
  const PRIVATE_KEY_BYTE_LENGTH = 32
@@ -14,11 +15,18 @@ export function generateKey (): Uint8Array {
14
15
  /**
15
16
  * Hash and sign message with private key
16
17
  */
17
- export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
18
- const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
18
+ export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
19
+ const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
20
+
21
+ if (isPromise(p)) {
22
+ return p.then(({ digest }) => secp.sign(digest, key).toDERRawBytes())
23
+ .catch(err => {
24
+ throw new CodeError(String(err), 'ERR_INVALID_INPUT')
25
+ })
26
+ }
27
+
19
28
  try {
20
- const signature = secp.sign(digest, key)
21
- return signature.toDERRawBytes()
29
+ return secp.sign(p.digest, key).toDERRawBytes()
22
30
  } catch (err) {
23
31
  throw new CodeError(String(err), 'ERR_INVALID_INPUT')
24
32
  }
@@ -27,10 +35,18 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
27
35
  /**
28
36
  * Hash message and verify signature with public key
29
37
  */
30
- export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
38
+ export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean | Promise<boolean> {
39
+ const p = sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
40
+
41
+ if (isPromise(p)) {
42
+ return p.then(({ digest }) => secp.verify(sig, digest, key))
43
+ .catch(err => {
44
+ throw new CodeError(String(err), 'ERR_INVALID_INPUT')
45
+ })
46
+ }
47
+
31
48
  try {
32
- const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray())
33
- return secp.verify(sig, digest, key)
49
+ return secp.verify(sig, p.digest, key)
34
50
  } catch (err) {
35
51
  throw new CodeError(String(err), 'ERR_INVALID_INPUT')
36
52
  }
@@ -2,6 +2,7 @@ import { CodeError } from '@libp2p/interface/errors'
2
2
  import { sha256 } from 'multiformats/hashes/sha2'
3
3
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
4
4
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5
+ import { isPromise } from '../util.js'
5
6
  import { exporter } from './exporter.js'
6
7
  import * as keysProtobuf from './keys.js'
7
8
  import * as crypto from './secp256k1.js'
@@ -16,7 +17,7 @@ export class Secp256k1PublicKey {
16
17
  this._key = key
17
18
  }
18
19
 
19
- async verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): Promise<boolean> {
20
+ verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean {
20
21
  return crypto.hashAndVerify(this._key, sig, data)
21
22
  }
22
23
 
@@ -36,7 +37,14 @@ export class Secp256k1PublicKey {
36
37
  }
37
38
 
38
39
  async hash (): Promise<Uint8Array> {
39
- const { bytes } = await sha256.digest(this.bytes)
40
+ const p = sha256.digest(this.bytes)
41
+ let bytes: Uint8Array
42
+
43
+ if (isPromise(p)) {
44
+ ({ bytes } = await p)
45
+ } else {
46
+ bytes = p.bytes
47
+ }
40
48
 
41
49
  return bytes
42
50
  }
@@ -53,7 +61,7 @@ export class Secp256k1PrivateKey {
53
61
  crypto.validatePublicKey(this._publicKey)
54
62
  }
55
63
 
56
- async sign (message: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
64
+ sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
57
65
  return crypto.hashAndSign(this._key, message)
58
66
  }
59
67
 
@@ -76,10 +84,14 @@ export class Secp256k1PrivateKey {
76
84
  return uint8ArrayEquals(this.bytes, key.bytes)
77
85
  }
78
86
 
79
- async hash (): Promise<Uint8Array> {
80
- const { bytes } = await sha256.digest(this.bytes)
87
+ hash (): Uint8Array | Promise<Uint8Array> {
88
+ const p = sha256.digest(this.bytes)
81
89
 
82
- return bytes
90
+ if (isPromise(p)) {
91
+ return p.then(({ bytes }) => bytes)
92
+ }
93
+
94
+ return p.bytes
83
95
  }
84
96
 
85
97
  /**
@@ -14,7 +14,7 @@ export function generateKey (): Uint8Array {
14
14
  /**
15
15
  * Hash and sign message with private key
16
16
  */
17
- export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> {
17
+ export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
18
18
  const hash = crypto.createHash('sha256')
19
19
 
20
20
  if (msg instanceof Uint8Array) {
@@ -38,7 +38,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8Array
38
38
  /**
39
39
  * Hash message and verify signature with public key
40
40
  */
41
- export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> {
41
+ export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
42
42
  const hash = crypto.createHash('sha256')
43
43
 
44
44
  if (msg instanceof Uint8Array) {
package/src/util.ts CHANGED
@@ -40,3 +40,13 @@ export function base64urlToBuffer (str: string, len?: number): Uint8Array {
40
40
 
41
41
  return buf
42
42
  }
43
+
44
+ export function isPromise <T = unknown> (thing: any): thing is Promise<T> {
45
+ if (thing == null) {
46
+ return false
47
+ }
48
+
49
+ return typeof thing.then === 'function' &&
50
+ typeof thing.catch === 'function' &&
51
+ typeof thing.finally === 'function'
52
+ }