@libp2p/crypto 2.0.8-6b6ba9ab7 → 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.
Files changed (72) hide show
  1. package/dist/index.min.js +9 -9
  2. package/dist/src/aes/cipher-mode.js.map +1 -1
  3. package/dist/src/aes/index.d.ts +1 -1
  4. package/dist/src/aes/index.d.ts.map +1 -1
  5. package/dist/src/aes/index.js +1 -1
  6. package/dist/src/aes/index.js.map +1 -1
  7. package/dist/src/ciphers/aes-gcm.browser.js.map +1 -1
  8. package/dist/src/ciphers/aes-gcm.js +3 -3
  9. package/dist/src/ciphers/aes-gcm.js.map +1 -1
  10. package/dist/src/keys/ecdh-browser.js +1 -1
  11. package/dist/src/keys/ecdh-browser.js.map +1 -1
  12. package/dist/src/keys/ecdh.js.map +1 -1
  13. package/dist/src/keys/ed25519-browser.d.ts +5 -4
  14. package/dist/src/keys/ed25519-browser.d.ts.map +1 -1
  15. package/dist/src/keys/ed25519-browser.js +6 -6
  16. package/dist/src/keys/ed25519-browser.js.map +1 -1
  17. package/dist/src/keys/ed25519-class.d.ts +4 -3
  18. package/dist/src/keys/ed25519-class.d.ts.map +1 -1
  19. package/dist/src/keys/ed25519-class.js +19 -8
  20. package/dist/src/keys/ed25519-class.js.map +1 -1
  21. package/dist/src/keys/ed25519.d.ts +5 -4
  22. package/dist/src/keys/ed25519.d.ts.map +1 -1
  23. package/dist/src/keys/ed25519.js +11 -19
  24. package/dist/src/keys/ed25519.js.map +1 -1
  25. package/dist/src/keys/index.js.map +1 -1
  26. package/dist/src/keys/key-stretcher.js.map +1 -1
  27. package/dist/src/keys/keys.js.map +1 -1
  28. package/dist/src/keys/rsa-browser.d.ts +5 -4
  29. package/dist/src/keys/rsa-browser.d.ts.map +1 -1
  30. package/dist/src/keys/rsa-browser.js +3 -3
  31. package/dist/src/keys/rsa-browser.js.map +1 -1
  32. package/dist/src/keys/rsa-class.d.ts +8 -7
  33. package/dist/src/keys/rsa-class.d.ts.map +1 -1
  34. package/dist/src/keys/rsa-class.js +15 -8
  35. package/dist/src/keys/rsa-class.js.map +1 -1
  36. package/dist/src/keys/rsa-utils.js.map +1 -1
  37. package/dist/src/keys/rsa.d.ts +5 -4
  38. package/dist/src/keys/rsa.d.ts.map +1 -1
  39. package/dist/src/keys/rsa.js +38 -12
  40. package/dist/src/keys/rsa.js.map +1 -1
  41. package/dist/src/keys/secp256k1-browser.d.ts +18 -0
  42. package/dist/src/keys/secp256k1-browser.d.ts.map +1 -0
  43. package/dist/src/keys/secp256k1-browser.js +78 -0
  44. package/dist/src/keys/secp256k1-browser.js.map +1 -0
  45. package/dist/src/keys/secp256k1-class.d.ts +4 -3
  46. package/dist/src/keys/secp256k1-class.d.ts.map +1 -1
  47. package/dist/src/keys/secp256k1-class.js +17 -6
  48. package/dist/src/keys/secp256k1-class.js.map +1 -1
  49. package/dist/src/keys/secp256k1.d.ts +3 -2
  50. package/dist/src/keys/secp256k1.d.ts.map +1 -1
  51. package/dist/src/keys/secp256k1.js +23 -5
  52. package/dist/src/keys/secp256k1.js.map +1 -1
  53. package/dist/src/pbkdf2.js.map +1 -1
  54. package/dist/src/random-bytes.js.map +1 -1
  55. package/dist/src/util.d.ts +1 -0
  56. package/dist/src/util.d.ts.map +1 -1
  57. package/dist/src/util.js +8 -0
  58. package/dist/src/util.js.map +1 -1
  59. package/dist/src/webcrypto.js.map +1 -1
  60. package/package.json +8 -3
  61. package/src/aes/index.ts +1 -1
  62. package/src/ciphers/aes-gcm.ts +3 -3
  63. package/src/keys/ed25519-browser.ts +7 -6
  64. package/src/keys/ed25519-class.ts +21 -8
  65. package/src/keys/ed25519.ts +12 -20
  66. package/src/keys/rsa-browser.ts +9 -8
  67. package/src/keys/rsa-class.ts +21 -11
  68. package/src/keys/rsa.ts +43 -16
  69. package/src/keys/secp256k1-browser.ts +87 -0
  70. package/src/keys/secp256k1-class.ts +19 -6
  71. package/src/keys/secp256k1.ts +28 -5
  72. package/src/util.ts +10 -0
@@ -2,10 +2,12 @@ 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'
8
9
  import type { Multibase } from 'multiformats'
10
+ import type { Uint8ArrayList } from 'uint8arraylist'
9
11
 
10
12
  export class Secp256k1PublicKey {
11
13
  private readonly _key: Uint8Array
@@ -15,7 +17,7 @@ export class Secp256k1PublicKey {
15
17
  this._key = key
16
18
  }
17
19
 
18
- async verify (data: Uint8Array, sig: Uint8Array): Promise<boolean> {
20
+ verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean {
19
21
  return crypto.hashAndVerify(this._key, sig, data)
20
22
  }
21
23
 
@@ -35,7 +37,14 @@ export class Secp256k1PublicKey {
35
37
  }
36
38
 
37
39
  async hash (): Promise<Uint8Array> {
38
- 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
+ }
39
48
 
40
49
  return bytes
41
50
  }
@@ -52,7 +61,7 @@ export class Secp256k1PrivateKey {
52
61
  crypto.validatePublicKey(this._publicKey)
53
62
  }
54
63
 
55
- async sign (message: Uint8Array): Promise<Uint8Array> {
64
+ sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
56
65
  return crypto.hashAndSign(this._key, message)
57
66
  }
58
67
 
@@ -75,10 +84,14 @@ export class Secp256k1PrivateKey {
75
84
  return uint8ArrayEquals(this.bytes, key.bytes)
76
85
  }
77
86
 
78
- async hash (): Promise<Uint8Array> {
79
- const { bytes } = await sha256.digest(this.bytes)
87
+ hash (): Uint8Array | Promise<Uint8Array> {
88
+ const p = sha256.digest(this.bytes)
80
89
 
81
- return bytes
90
+ if (isPromise(p)) {
91
+ return p.then(({ bytes }) => bytes)
92
+ }
93
+
94
+ return p.bytes
82
95
  }
83
96
 
84
97
  /**
@@ -1,6 +1,7 @@
1
+ import crypto from 'node:crypto'
1
2
  import { CodeError } from '@libp2p/interface/errors'
2
3
  import { secp256k1 as secp } from '@noble/curves/secp256k1'
3
- import { sha256 } from 'multiformats/hashes/sha2'
4
+ import type { Uint8ArrayList } from 'uint8arraylist'
4
5
 
5
6
  const PRIVATE_KEY_BYTE_LENGTH = 32
6
7
 
@@ -13,8 +14,19 @@ export function generateKey (): Uint8Array {
13
14
  /**
14
15
  * Hash and sign message with private key
15
16
  */
16
- export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Uint8Array> {
17
- const { digest } = await sha256.digest(msg)
17
+ export function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Uint8Array {
18
+ const hash = crypto.createHash('sha256')
19
+
20
+ if (msg instanceof Uint8Array) {
21
+ hash.update(msg)
22
+ } else {
23
+ for (const buf of msg) {
24
+ hash.update(buf)
25
+ }
26
+ }
27
+
28
+ const digest = hash.digest()
29
+
18
30
  try {
19
31
  const signature = secp.sign(digest, key)
20
32
  return signature.toDERRawBytes()
@@ -26,9 +38,20 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array): Promise<Ui
26
38
  /**
27
39
  * Hash message and verify signature with public key
28
40
  */
29
- export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean> {
41
+ export function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): boolean {
42
+ const hash = crypto.createHash('sha256')
43
+
44
+ if (msg instanceof Uint8Array) {
45
+ hash.update(msg)
46
+ } else {
47
+ for (const buf of msg) {
48
+ hash.update(buf)
49
+ }
50
+ }
51
+
52
+ const digest = hash.digest()
53
+
30
54
  try {
31
- const { digest } = await sha256.digest(msg)
32
55
  return secp.verify(sig, digest, key)
33
56
  } catch (err) {
34
57
  throw new CodeError(String(err), 'ERR_INVALID_INPUT')
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
+ }