fix: simplify pnet exports (#1213)

Export the key generator from `libp2p/pnet`
This commit is contained in:
Alex Potsides 2022-05-16 10:07:32 +01:00 committed by GitHub
parent 4837430d8b
commit 31480603f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 12 deletions

View File

@ -1,6 +1,6 @@
/* eslint no-console: ["off"] */ /* eslint no-console: ["off"] */
import { generate } from 'libp2p/pnet/generate' import { generateKey } from 'libp2p/pnet'
import { privateLibp2pNode } from './libp2p-node.js' import { privateLibp2pNode } from './libp2p-node.js'
import { pipe } from 'it-pipe' import { pipe } from 'it-pipe'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@ -8,11 +8,11 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
// Create a Uint8Array and write the swarm key to it // Create a Uint8Array and write the swarm key to it
const swarmKey = new Uint8Array(95) const swarmKey = new Uint8Array(95)
generate(swarmKey) generateKey(swarmKey)
// This key is for testing a different key not working // This key is for testing a different key not working
const otherSwarmKey = new Uint8Array(95) const otherSwarmKey = new Uint8Array(95)
generate(otherSwarmKey) generateKey(otherSwarmKey)
;(async () => { ;(async () => {
const node1 = await privateLibp2pNode(swarmKey) const node1 = await privateLibp2pNode(swarmKey)

View File

@ -57,9 +57,6 @@
"./pnet": { "./pnet": {
"import": "./dist/src/pnet/index.js" "import": "./dist/src/pnet/index.js"
}, },
"./pnet/generate": {
"import": "./dist/src/pnet/key-generator.js"
},
"./transport-manager": { "./transport-manager": {
"import": "./dist/src/transport-manager.js" "import": "./dist/src/transport-manager.js"
} }

View File

@ -9,7 +9,6 @@ import errCode from 'err-code'
import { codes } from '../errors.js' import { codes } from '../errors.js'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import 'node-forge/lib/sha512.js'
import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys' import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys'
import type { PeerId } from '@libp2p/interfaces/peer-id' import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { Components } from '@libp2p/interfaces/components' import type { Components } from '@libp2p/interfaces/components'

View File

@ -17,6 +17,8 @@ import type { ConnectionProtector } from '@libp2p/interfaces/connection'
const log = logger('libp2p:pnet') const log = logger('libp2p:pnet')
export { generateKey } from './key-generator.js'
export interface ProtectorInit { export interface ProtectorInit {
enabled?: boolean enabled?: boolean
psk: Uint8Array psk: Uint8Array

View File

@ -8,7 +8,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
* @param {Uint8Array} bytes - An object to write the psk into * @param {Uint8Array} bytes - An object to write the psk into
* @returns {void} * @returns {void}
*/ */
export function generate (bytes: Uint8Array) { export function generateKey (bytes: Uint8Array) {
const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16') const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk) const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)

View File

@ -3,8 +3,7 @@ import { expect } from 'aegir/chai'
import { pipe } from 'it-pipe' import { pipe } from 'it-pipe'
import all from 'it-all' import all from 'it-all'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { PreSharedKeyConnectionProtector } from '../../src/pnet/index.js' import { PreSharedKeyConnectionProtector, generateKey } from '../../src/pnet/index.js'
import { generate } from '../../src/pnet/key-generator.js'
import { INVALID_PSK } from '../../src/pnet/errors.js' import { INVALID_PSK } from '../../src/pnet/errors.js'
import { mockMultiaddrConnPair } from '@libp2p/interface-compliance-tests/mocks' import { mockMultiaddrConnPair } from '@libp2p/interface-compliance-tests/mocks'
import { Multiaddr } from '@multiformats/multiaddr' import { Multiaddr } from '@multiformats/multiaddr'
@ -14,8 +13,8 @@ const swarmKeyBuffer = new Uint8Array(95)
const wrongSwarmKeyBuffer = new Uint8Array(95) const wrongSwarmKeyBuffer = new Uint8Array(95)
// Write new psk files to the buffers // Write new psk files to the buffers
generate(swarmKeyBuffer) generateKey(swarmKeyBuffer)
generate(wrongSwarmKeyBuffer) generateKey(wrongSwarmKeyBuffer)
describe('private network', () => { describe('private network', () => {
it('should accept a valid psk buffer', () => { it('should accept a valid psk buffer', () => {