@libp2p/crypto 1.0.11 → 1.0.12

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 (50) hide show
  1. package/dist/index.min.js +7 -7
  2. package/dist/src/aes/cipher-mode.js +2 -2
  3. package/dist/src/aes/cipher-mode.js.map +1 -1
  4. package/dist/src/keys/ecdh-browser.js +6 -6
  5. package/dist/src/keys/ecdh-browser.js.map +1 -1
  6. package/dist/src/keys/ecdh.js +2 -2
  7. package/dist/src/keys/ecdh.js.map +1 -1
  8. package/dist/src/keys/ed25519-class.js +3 -3
  9. package/dist/src/keys/ed25519-class.js.map +1 -1
  10. package/dist/src/keys/ed25519.d.ts +1 -1
  11. package/dist/src/keys/ed25519.d.ts.map +1 -1
  12. package/dist/src/keys/ed25519.js +16 -3
  13. package/dist/src/keys/ed25519.js.map +1 -1
  14. package/dist/src/keys/index.js +4 -4
  15. package/dist/src/keys/index.js.map +1 -1
  16. package/dist/src/keys/key-stretcher.js +3 -3
  17. package/dist/src/keys/key-stretcher.js.map +1 -1
  18. package/dist/src/keys/rsa-browser.js +2 -2
  19. package/dist/src/keys/rsa-browser.js.map +1 -1
  20. package/dist/src/keys/rsa-class.js +3 -3
  21. package/dist/src/keys/rsa-class.js.map +1 -1
  22. package/dist/src/keys/rsa-utils.js +3 -3
  23. package/dist/src/keys/rsa-utils.js.map +1 -1
  24. package/dist/src/keys/rsa.js +2 -2
  25. package/dist/src/keys/rsa.js.map +1 -1
  26. package/dist/src/keys/secp256k1-class.js +2 -2
  27. package/dist/src/keys/secp256k1-class.js.map +1 -1
  28. package/dist/src/keys/secp256k1.js +6 -6
  29. package/dist/src/keys/secp256k1.js.map +1 -1
  30. package/dist/src/pbkdf2.js +2 -2
  31. package/dist/src/pbkdf2.js.map +1 -1
  32. package/dist/src/random-bytes.js +2 -2
  33. package/dist/src/random-bytes.js.map +1 -1
  34. package/dist/typedoc-urls.json +1 -0
  35. package/package.json +2 -2
  36. package/src/aes/cipher-mode.ts +2 -2
  37. package/src/keys/ecdh-browser.ts +6 -6
  38. package/src/keys/ecdh.ts +2 -2
  39. package/src/keys/ed25519-class.ts +3 -3
  40. package/src/keys/ed25519.ts +19 -4
  41. package/src/keys/index.ts +4 -4
  42. package/src/keys/key-stretcher.ts +3 -3
  43. package/src/keys/rsa-browser.ts +2 -2
  44. package/src/keys/rsa-class.ts +3 -3
  45. package/src/keys/rsa-utils.ts +3 -3
  46. package/src/keys/rsa.ts +2 -2
  47. package/src/keys/secp256k1-class.ts +2 -2
  48. package/src/keys/secp256k1.ts +6 -6
  49. package/src/pbkdf2.ts +2 -2
  50. package/src/random-bytes.ts +2 -2
@@ -1,8 +1,8 @@
1
1
  import { utils } from '@noble/secp256k1';
2
- import errcode from 'err-code';
2
+ import { CodeError } from '@libp2p/interfaces/errors';
3
3
  export default function randomBytes(length) {
4
4
  if (isNaN(length) || length <= 0) {
5
- throw errcode(new Error('random bytes length must be a Number bigger than 0'), 'ERR_INVALID_LENGTH');
5
+ throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH');
6
6
  }
7
7
  return utils.randomBytes(length);
8
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,OAAO,MAAM,UAAU,CAAA;AAE9B,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAc;IACjD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;QAChC,MAAM,OAAO,CAAC,IAAI,KAAK,CAAC,oDAAoD,CAAC,EAAE,oBAAoB,CAAC,CAAA;KACrG;IACD,OAAO,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;AAClC,CAAC"}
1
+ {"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAErD,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAc;IACjD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;QAChC,MAAM,IAAI,SAAS,CAAC,oDAAoD,EAAE,oBAAoB,CAAC,CAAA;KAChG;IACD,OAAO,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;AAClC,CAAC"}
@@ -23,6 +23,7 @@
23
23
  "codec": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.KeyType.codec.html",
24
24
  "decode": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.PrivateKey.decode.html",
25
25
  "encode": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.PrivateKey.encode.html",
26
+ "KeyType": "https://libp2p.github.io/js-libp2p-crypto/enums/keys.keysPBM.KeyType-1.html",
26
27
  "PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys.keysPBM.PrivateKey-1.html",
27
28
  "PublicKey": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys.keysPBM.PublicKey-1.html",
28
29
  "KeyTypes": "https://libp2p.github.io/js-libp2p-crypto/types/keys.KeyTypes.html",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/crypto",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Crypto primitives for libp2p",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p-crypto#readme",
@@ -180,9 +180,9 @@
180
180
  },
181
181
  "dependencies": {
182
182
  "@libp2p/interface-keys": "^1.0.2",
183
+ "@libp2p/interfaces": "^3.2.0",
183
184
  "@noble/ed25519": "^1.6.0",
184
185
  "@noble/secp256k1": "^1.5.4",
185
- "err-code": "^3.0.1",
186
186
  "multiformats": "^11.0.0",
187
187
  "node-forge": "^1.1.0",
188
188
  "protons-runtime": "^4.0.1",
@@ -1,4 +1,4 @@
1
- import errcode from 'err-code'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
2
 
3
3
  const CIPHER_MODES = {
4
4
  16: 'aes-128-ctr',
@@ -11,5 +11,5 @@ export function cipherMode (key: Uint8Array) {
11
11
  }
12
12
 
13
13
  const modes = Object.entries(CIPHER_MODES).map(([k, v]) => `${k} (${v})`).join(' / ')
14
- throw errcode(new Error(`Invalid key length ${key.length} bytes. Must be ${modes}`), 'ERR_INVALID_KEY_LENGTH')
14
+ throw new CodeError(`Invalid key length ${key.length} bytes. Must be ${modes}`, 'ERR_INVALID_KEY_LENGTH')
15
15
  }
@@ -1,4 +1,4 @@
1
- import errcode from 'err-code'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
2
  import webcrypto from '../webcrypto.js'
3
3
  import { base64urlToBuffer } from '../util.js'
4
4
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -17,7 +17,7 @@ const names = curveTypes.join(' / ')
17
17
 
18
18
  export async function generateEphmeralKeyPair (curve: string) {
19
19
  if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
20
- throw errcode(new Error(`Unknown curve: ${curve}. Must be ${names}`), 'ERR_INVALID_CURVE')
20
+ throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
21
21
  }
22
22
 
23
23
  const pair = await webcrypto.get().subtle.generateKey(
@@ -94,11 +94,11 @@ const curveLengths = {
94
94
  // go-ipfs uses)
95
95
  function marshalPublicKey (jwk: JsonWebKey) {
96
96
  if (jwk.crv == null || jwk.x == null || jwk.y == null) {
97
- throw errcode(new Error('JWK was missing components'), 'ERR_INVALID_PARAMETERS')
97
+ throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
98
98
  }
99
99
 
100
100
  if (jwk.crv !== 'P-256' && jwk.crv !== 'P-384' && jwk.crv !== 'P-521') {
101
- throw errcode(new Error(`Unknown curve: ${jwk.crv}. Must be ${names}`), 'ERR_INVALID_CURVE')
101
+ throw new CodeError(`Unknown curve: ${jwk.crv}. Must be ${names}`, 'ERR_INVALID_CURVE')
102
102
  }
103
103
 
104
104
  const byteLen = curveLengths[jwk.crv]
@@ -113,13 +113,13 @@ function marshalPublicKey (jwk: JsonWebKey) {
113
113
  // Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
114
114
  function unmarshalPublicKey (curve: string, key: Uint8Array) {
115
115
  if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
116
- throw errcode(new Error(`Unknown curve: ${curve}. Must be ${names}`), 'ERR_INVALID_CURVE')
116
+ throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
117
117
  }
118
118
 
119
119
  const byteLen = curveLengths[curve]
120
120
 
121
121
  if (!uint8ArrayEquals(key.subarray(0, 1), Uint8Array.from([4]))) {
122
- throw errcode(new Error('Cannot unmarshal public key - invalid key format'), 'ERR_INVALID_KEY_FORMAT')
122
+ throw new CodeError('Cannot unmarshal public key - invalid key format', 'ERR_INVALID_KEY_FORMAT')
123
123
  }
124
124
 
125
125
  return {
package/src/keys/ecdh.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import crypto from 'crypto'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import type { ECDHKey, ECDHKeyPair } from './interface.js'
4
4
 
5
5
  const curves = {
@@ -13,7 +13,7 @@ const names = curveTypes.join(' / ')
13
13
 
14
14
  export async function generateEphmeralKeyPair (curve: string): Promise<ECDHKey> { // eslint-disable-line require-await
15
15
  if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
16
- throw errcode(new Error(`Unknown curve: ${curve}. Must be ${names}`), 'ERR_INVALID_CURVE')
16
+ throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
17
17
  }
18
18
 
19
19
  const ecdh = crypto.createECDH(curves[curve])
@@ -1,4 +1,4 @@
1
- import errcode from 'err-code'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
2
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
3
3
  import { sha256 } from 'multiformats/hashes/sha2'
4
4
  import { base58btc } from 'multiformats/bases/base58'
@@ -101,7 +101,7 @@ export class Ed25519PrivateKey {
101
101
  if (format === 'libp2p-key') {
102
102
  return await exporter(this.bytes, password)
103
103
  } else {
104
- throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT')
104
+ throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
105
105
  }
106
106
  }
107
107
  }
@@ -139,7 +139,7 @@ export async function generateKeyPairFromSeed (seed: Uint8Array) {
139
139
  function ensureKey (key: Uint8Array, length: number) {
140
140
  key = Uint8Array.from(key ?? [])
141
141
  if (key.length !== length) {
142
- throw errcode(new Error(`Key must be a Uint8Array of length ${length}, got ${key.length}`), 'ERR_INVALID_KEY_TYPE')
142
+ throw new CodeError(`Key must be a Uint8Array of length ${length}, got ${key.length}`, 'ERR_INVALID_KEY_TYPE')
143
143
  }
144
144
  return key
145
145
  }
@@ -13,10 +13,25 @@ const SIGNATURE_BYTE_LENGTH = 64
13
13
  export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
14
14
  export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
15
15
 
16
- function derivePublicKey (privateKey: Uint8Array) {
17
- const hash = crypto.createHash('sha512')
18
- hash.update(privateKey)
19
- return hash.digest().subarray(32)
16
+ function derivePublicKey (privateKey: Uint8Array): Uint8Array {
17
+ const keyObject = crypto.createPrivateKey({
18
+ format: 'jwk',
19
+ key: {
20
+ crv: 'Ed25519',
21
+ x: '',
22
+ d: uint8arrayToString(privateKey, 'base64url'),
23
+ kty: 'OKP'
24
+ }
25
+ })
26
+ const jwk = keyObject.export({
27
+ format: 'jwk'
28
+ })
29
+
30
+ if (jwk.x == null || jwk.x === '') {
31
+ throw new Error('Could not export JWK public key')
32
+ }
33
+
34
+ return uint8arrayFromString(jwk.x, 'base64url')
20
35
  }
21
36
 
22
37
  export async function generateKey () {
package/src/keys/index.ts CHANGED
@@ -3,7 +3,7 @@ import 'node-forge/lib/asn1.js'
3
3
  import 'node-forge/lib/pbe.js'
4
4
  // @ts-expect-error types are missing
5
5
  import forge from 'node-forge/lib/forge.js'
6
- import errcode from 'err-code'
6
+ import { CodeError } from '@libp2p/interfaces/errors'
7
7
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
8
8
  import { keyStretcher } from './key-stretcher.js'
9
9
  import generateEphemeralKeyPair from './ephemeral-keys.js'
@@ -27,7 +27,7 @@ export const supportedKeys = {
27
27
 
28
28
  function unsupportedKey (type: string) {
29
29
  const supported = Object.keys(supportedKeys).join(' / ')
30
- return errcode(new Error(`invalid or unsupported key type ${type}. Must be ${supported}`), 'ERR_UNSUPPORTED_KEY_TYPE')
30
+ return new CodeError(`invalid or unsupported key type ${type}. Must be ${supported}`, 'ERR_UNSUPPORTED_KEY_TYPE')
31
31
  }
32
32
 
33
33
  function typeToKey (type: string) {
@@ -49,7 +49,7 @@ export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<P
49
49
  // seed is a 32 byte uint8array
50
50
  export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
51
51
  if (type.toLowerCase() !== 'ed25519') {
52
- throw errcode(new Error('Seed key derivation is unimplemented for RSA or secp256k1'), 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
52
+ throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
53
53
  }
54
54
 
55
55
  return await Ed25519.generateKeyPairFromSeed(seed)
@@ -121,7 +121,7 @@ export async function importKey (encryptedKey: string, password: string): Promis
121
121
  // Only rsa supports pem right now
122
122
  const key = forge.pki.decryptRsaPrivateKey(encryptedKey, password)
123
123
  if (key === null) {
124
- throw errcode(new Error('Cannot read the key, most likely the password is wrong or not a RSA key'), 'ERR_CANNOT_DECRYPT_PEM')
124
+ throw new CodeError('Cannot read the key, most likely the password is wrong or not a RSA key', 'ERR_CANNOT_DECRYPT_PEM')
125
125
  }
126
126
  let der = forge.asn1.toDer(forge.pki.privateKeyToAsn1(key))
127
127
  der = uint8ArrayFromString(der.getBytes(), 'ascii')
@@ -1,4 +1,4 @@
1
- import errcode from 'err-code'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
2
  import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
3
3
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
4
  import * as hmac from '../hmac/index.js'
@@ -27,11 +27,11 @@ export async function keyStretcher (cipherType: 'AES-128' | 'AES-256' | 'Blowfis
27
27
 
28
28
  if (cipher == null) {
29
29
  const allowed = Object.keys(cipherMap).join(' / ')
30
- throw errcode(new Error(`unknown cipher type '${cipherType}'. Must be ${allowed}`), 'ERR_INVALID_CIPHER_TYPE')
30
+ throw new CodeError(`unknown cipher type '${cipherType}'. Must be ${allowed}`, 'ERR_INVALID_CIPHER_TYPE')
31
31
  }
32
32
 
33
33
  if (hash == null) {
34
- throw errcode(new Error('missing hash type'), 'ERR_MISSING_HASH_TYPE')
34
+ throw new CodeError('missing hash type', 'ERR_MISSING_HASH_TYPE')
35
35
  }
36
36
 
37
37
  const cipherKeySize = cipher.keySize
@@ -4,7 +4,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
4
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
5
5
  import * as utils from './rsa-utils.js'
6
6
  import { jwk2pub, jwk2priv } from './jwk2pem.js'
7
- import errcode from 'err-code'
7
+ import { CodeError } from '@libp2p/interfaces/errors'
8
8
 
9
9
  export { utils }
10
10
 
@@ -102,7 +102,7 @@ export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint
102
102
 
103
103
  async function exportKey (pair: CryptoKeyPair) {
104
104
  if (pair.privateKey == null || pair.publicKey == null) {
105
- throw errcode(new Error('Private and public key are required'), 'ERR_INVALID_PARAMETERS')
105
+ throw new CodeError('Private and public key are required', 'ERR_INVALID_PARAMETERS')
106
106
  }
107
107
 
108
108
  return await Promise.all([
@@ -1,6 +1,6 @@
1
1
 
2
2
  import { sha256 } from 'multiformats/hashes/sha2'
3
- import errcode from 'err-code'
3
+ import { CodeError } from '@libp2p/interfaces/errors'
4
4
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
5
5
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
6
6
  import 'node-forge/lib/sha512.js'
@@ -66,7 +66,7 @@ export class RsaPrivateKey {
66
66
 
67
67
  get public () {
68
68
  if (this._publicKey == null) {
69
- throw errcode(new Error('public key not provided'), 'ERR_PUBKEY_NOT_PROVIDED')
69
+ throw new CodeError('public key not provided', 'ERR_PUBKEY_NOT_PROVIDED')
70
70
  }
71
71
 
72
72
  return new RsaPublicKey(this._publicKey)
@@ -128,7 +128,7 @@ export class RsaPrivateKey {
128
128
  } else if (format === 'libp2p-key') {
129
129
  return await exporter(this.bytes, password)
130
130
  } else {
131
- throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT')
131
+ throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
132
132
  }
133
133
  }
134
134
  }
@@ -5,7 +5,7 @@ import forge from 'node-forge/lib/forge.js'
5
5
  import { bigIntegerToUintBase64url, base64urlToBigInteger } from './../util.js'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
7
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
- import errcode from 'err-code'
8
+ import { CodeError } from '@libp2p/interfaces/errors'
9
9
 
10
10
  // Convert a PKCS#1 in ASN1 DER format to a JWK key
11
11
  export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
@@ -30,7 +30,7 @@ export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
30
30
  // Convert a JWK key into PKCS#1 in ASN1 DER format
31
31
  export function jwkToPkcs1 (jwk: JsonWebKey) {
32
32
  if (jwk.n == null || jwk.e == null || jwk.d == null || jwk.p == null || jwk.q == null || jwk.dp == null || jwk.dq == null || jwk.qi == null) {
33
- throw errcode(new Error('JWK was missing components'), 'ERR_INVALID_PARAMETERS')
33
+ throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
34
34
  }
35
35
 
36
36
  const asn1 = forge.pki.privateKeyToAsn1({
@@ -62,7 +62,7 @@ export function pkixToJwk (bytes: Uint8Array): JsonWebKey {
62
62
  // Convert a JWK key to PKCIX in ASN1 DER format
63
63
  export function jwkToPkix (jwk: JsonWebKey) {
64
64
  if (jwk.n == null || jwk.e == null) {
65
- throw errcode(new Error('JWK was missing components'), 'ERR_INVALID_PARAMETERS')
65
+ throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
66
66
  }
67
67
 
68
68
  const asn1 = forge.pki.publicKeyToAsn1({
package/src/keys/rsa.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import crypto from 'crypto'
2
2
  import { promisify } from 'util'
3
- import errcode from 'err-code'
3
+ import { CodeError } from '@libp2p/interfaces/errors'
4
4
  import randomBytes from '../random-bytes.js'
5
5
  import * as utils from './rsa-utils.js'
6
6
  import type { JWKKeyPair } from './interface.js'
@@ -28,7 +28,7 @@ export async function generateKey (bits: number): Promise<JWKKeyPair> { // eslin
28
28
  // Takes a jwk key
29
29
  export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair> { // eslint-disable-line require-await
30
30
  if (key == null) {
31
- throw errcode(new Error('Missing key parameter'), 'ERR_MISSING_KEY')
31
+ throw new CodeError('Missing key parameter', 'ERR_MISSING_KEY')
32
32
  }
33
33
  return {
34
34
  privateKey: key,
@@ -1,5 +1,5 @@
1
1
  import { sha256 } from 'multiformats/hashes/sha2'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
4
4
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5
5
  import * as crypto from './secp256k1.js'
@@ -99,7 +99,7 @@ export class Secp256k1PrivateKey {
99
99
  if (format === 'libp2p-key') {
100
100
  return await exporter(this.bytes, password)
101
101
  } else {
102
- throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT')
102
+ throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
103
103
  }
104
104
  }
105
105
  }
@@ -1,4 +1,4 @@
1
- import errcode from 'err-code'
1
+ import { CodeError } from '@libp2p/interfaces/errors'
2
2
  import * as secp from '@noble/secp256k1'
3
3
  import { sha256 } from 'multiformats/hashes/sha2'
4
4
 
@@ -18,7 +18,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array) {
18
18
  try {
19
19
  return await secp.sign(digest, key)
20
20
  } catch (err) {
21
- throw errcode(err, 'ERR_INVALID_INPUT')
21
+ throw new CodeError(String(err), 'ERR_INVALID_INPUT')
22
22
  }
23
23
  }
24
24
 
@@ -30,7 +30,7 @@ export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint
30
30
  const { digest } = await sha256.digest(msg)
31
31
  return secp.verify(sig, digest, key)
32
32
  } catch (err) {
33
- throw errcode(err, 'ERR_INVALID_INPUT')
33
+ throw new CodeError(String(err), 'ERR_INVALID_INPUT')
34
34
  }
35
35
  }
36
36
 
@@ -48,7 +48,7 @@ export function validatePrivateKey (key: Uint8Array) {
48
48
  try {
49
49
  secp.getPublicKey(key, true)
50
50
  } catch (err) {
51
- throw errcode(err, 'ERR_INVALID_PRIVATE_KEY')
51
+ throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
52
52
  }
53
53
  }
54
54
 
@@ -56,7 +56,7 @@ export function validatePublicKey (key: Uint8Array) {
56
56
  try {
57
57
  secp.Point.fromHex(key)
58
58
  } catch (err) {
59
- throw errcode(err, 'ERR_INVALID_PUBLIC_KEY')
59
+ throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY')
60
60
  }
61
61
  }
62
62
 
@@ -64,6 +64,6 @@ export function computePublicKey (privateKey: Uint8Array) {
64
64
  try {
65
65
  return secp.getPublicKey(privateKey, true)
66
66
  } catch (err) {
67
- throw errcode(err, 'ERR_INVALID_PRIVATE_KEY')
67
+ throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
68
68
  }
69
69
  }
package/src/pbkdf2.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import forgePbkdf2 from 'node-forge/lib/pbkdf2.js'
3
3
  // @ts-expect-error types are missing
4
4
  import forgeUtil from 'node-forge/lib/util.js'
5
- import errcode from 'err-code'
5
+ import { CodeError } from '@libp2p/interfaces/errors'
6
6
 
7
7
  /**
8
8
  * Maps an IPFS hash name to its node-forge equivalent.
@@ -23,7 +23,7 @@ const hashName = {
23
23
  export default function pbkdf2 (password: string, salt: string, iterations: number, keySize: number, hash: string): string {
24
24
  if (hash !== 'sha1' && hash !== 'sha2-256' && hash !== 'sha2-512') {
25
25
  const types = Object.keys(hashName).join(' / ')
26
- throw errcode(new Error(`Hash '${hash}' is unknown or not supported. Must be ${types}`), 'ERR_UNSUPPORTED_HASH_TYPE')
26
+ throw new CodeError(`Hash '${hash}' is unknown or not supported. Must be ${types}`, 'ERR_UNSUPPORTED_HASH_TYPE')
27
27
  }
28
28
 
29
29
  const hasher = hashName[hash]
@@ -1,9 +1,9 @@
1
1
  import { utils } from '@noble/secp256k1'
2
- import errcode from 'err-code'
2
+ import { CodeError } from '@libp2p/interfaces/errors'
3
3
 
4
4
  export default function randomBytes (length: number): Uint8Array {
5
5
  if (isNaN(length) || length <= 0) {
6
- throw errcode(new Error('random bytes length must be a Number bigger than 0'), 'ERR_INVALID_LENGTH')
6
+ throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH')
7
7
  }
8
8
  return utils.randomBytes(length)
9
9
  }