@libp2p/crypto 2.0.5 → 2.0.6-025c082a

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/src/keys/index.ts CHANGED
@@ -1,3 +1,15 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * **Supported Key Types**
5
+ *
6
+ * The {@link generateKeyPair}, {@link marshalPublicKey}, and {@link marshalPrivateKey} functions accept a string `type` argument.
7
+ *
8
+ * Currently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.
9
+ *
10
+ * For encryption / decryption support, RSA keys should be used.
11
+ */
12
+
1
13
  import 'node-forge/lib/asn1.js'
2
14
  import 'node-forge/lib/pbe.js'
3
15
  import { CodeError } from '@libp2p/interface/errors'
@@ -40,13 +52,21 @@ function typeToKey (type: string): typeof RSA | typeof Ed25519 | typeof Secp256k
40
52
  throw unsupportedKey(type)
41
53
  }
42
54
 
43
- // Generates a keypair of the given type and bitsize
55
+ /**
56
+ * Generates a keypair of the given type and bitsize
57
+ *
58
+ * @param type
59
+ * @param bits - Minimum of 1024
60
+ */
44
61
  export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<PrivateKey> {
45
62
  return typeToKey(type).generateKeyPair(bits ?? 2048)
46
63
  }
47
64
 
48
- // Generates a keypair of the given type and bitsize
49
- // seed is a 32 byte uint8array
65
+ /**
66
+ * Generates a keypair of the given type and bitsize.
67
+ *
68
+ * Seed is a 32 byte uint8array
69
+ */
50
70
  export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> {
51
71
  if (type.toLowerCase() !== 'ed25519') {
52
72
  throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
@@ -55,8 +75,9 @@ export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array,
55
75
  return Ed25519.generateKeyPairFromSeed(seed)
56
76
  }
57
77
 
58
- // Converts a protobuf serialized public key into its
59
- // representative object
78
+ /**
79
+ * Converts a protobuf serialized public key into its representative object
80
+ */
60
81
  export function unmarshalPublicKey (buf: Uint8Array): PublicKey {
61
82
  const decoded = keysPBM.PublicKey.decode(buf)
62
83
  const data = decoded.Data ?? new Uint8Array()
@@ -73,15 +94,18 @@ export function unmarshalPublicKey (buf: Uint8Array): PublicKey {
73
94
  }
74
95
  }
75
96
 
76
- // Converts a public key object into a protobuf serialized public key
97
+ /**
98
+ * Converts a public key object into a protobuf serialized public key
99
+ */
77
100
  export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
78
101
  type = (type ?? 'rsa').toLowerCase()
79
102
  typeToKey(type) // check type
80
103
  return key.bytes
81
104
  }
82
105
 
83
- // Converts a protobuf serialized private key into its
84
- // representative object
106
+ /**
107
+ * Converts a protobuf serialized private key into its representative object
108
+ */
85
109
  export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey> {
86
110
  const decoded = keysPBM.PrivateKey.decode(buf)
87
111
  const data = decoded.Data ?? new Uint8Array()
@@ -98,7 +122,9 @@ export async function unmarshalPrivateKey (buf: Uint8Array): Promise<PrivateKey>
98
122
  }
99
123
  }
100
124
 
101
- // Converts a private key object into a protobuf serialized private key
125
+ /**
126
+ * Converts a private key object into a protobuf serialized private key
127
+ */
102
128
  export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): Uint8Array {
103
129
  type = (type ?? 'rsa').toLowerCase()
104
130
  typeToKey(type) // check type
@@ -106,9 +132,9 @@ export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): U
106
132
  }
107
133
 
108
134
  /**
135
+ * Converts an exported private key into its representative object.
109
136
  *
110
- * @param {string} encryptedKey
111
- * @param {string} password
137
+ * Supported formats are 'pem' (RSA only) and 'libp2p-key'.
112
138
  */
113
139
  export async function importKey (encryptedKey: string, password: string): Promise<PrivateKey> {
114
140
  try {
@@ -1,6 +1,9 @@
1
1
  import { CodeError } from '@libp2p/interface/errors'
2
2
  import { randomBytes as randB } from '@noble/hashes/utils'
3
3
 
4
+ /**
5
+ * Generates a Uint8Array with length `number` populated by random bytes
6
+ */
4
7
  export default function randomBytes (length: number): Uint8Array {
5
8
  if (isNaN(length) || length <= 0) {
6
9
  throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH')
@@ -1,37 +0,0 @@
1
- {
2
- "AESCipher": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.aes.AESCipher.html",
3
- "./aes:AESCipher": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.aes.AESCipher.html",
4
- "create": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.aes.create.html",
5
- "./aes:create": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.aes.create.html",
6
- "HMAC": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.hmac.HMAC.html",
7
- "./hmac:HMAC": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.hmac.HMAC.html",
8
- "./hmac:create": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.hmac.create.html",
9
- "pbkdf2": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.index.pbkdf2.html",
10
- "randomBytes": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.index.randomBytes.html",
11
- "codec": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.KeyType.codec.html",
12
- "decode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.PrivateKey.decode.html",
13
- "encode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.PrivateKey.encode.html",
14
- "KeyType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_crypto.keys.keysPBM.KeyType-1.html",
15
- "PrivateKey": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.keys.keysPBM.PrivateKey-1.html",
16
- "PublicKey": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.keys.keysPBM.PublicKey-1.html",
17
- "KeyTypes": "https://libp2p.github.io/js-libp2p/types/_libp2p_crypto.keys.KeyTypes.html",
18
- "./keys:KeyTypes": "https://libp2p.github.io/js-libp2p/types/_libp2p_crypto.keys.KeyTypes.html",
19
- "supportedKeys": "https://libp2p.github.io/js-libp2p/variables/_libp2p_crypto.keys.supportedKeys.html",
20
- "./keys:supportedKeys": "https://libp2p.github.io/js-libp2p/variables/_libp2p_crypto.keys.supportedKeys.html",
21
- "generateEphemeralKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateEphemeralKeyPair.html",
22
- "generateKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPair.html",
23
- "./keys:generateKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPair.html",
24
- "generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPairFromSeed.html",
25
- "./keys:generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPairFromSeed.html",
26
- "importKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.importKey.html",
27
- "./keys:importKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.importKey.html",
28
- "keyStretcher": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keyStretcher.html",
29
- "marshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPrivateKey.html",
30
- "./keys:marshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPrivateKey.html",
31
- "marshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPublicKey.html",
32
- "./keys:marshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPublicKey.html",
33
- "unmarshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPrivateKey.html",
34
- "./keys:unmarshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPrivateKey.html",
35
- "unmarshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPublicKey.html",
36
- "./keys:unmarshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPublicKey.html"
37
- }