mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-15 23:20:51 +00:00
fix: simplify pnet exports (#1213)
Export the key generator from `libp2p/pnet`
This commit is contained in:
parent
4837430d8b
commit
31480603f3
@ -1,6 +1,6 @@
|
||||
/* eslint no-console: ["off"] */
|
||||
|
||||
import { generate } from 'libp2p/pnet/generate'
|
||||
import { generateKey } from 'libp2p/pnet'
|
||||
import { privateLibp2pNode } from './libp2p-node.js'
|
||||
import { pipe } from 'it-pipe'
|
||||
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
|
||||
const swarmKey = new Uint8Array(95)
|
||||
generate(swarmKey)
|
||||
generateKey(swarmKey)
|
||||
|
||||
// This key is for testing a different key not working
|
||||
const otherSwarmKey = new Uint8Array(95)
|
||||
generate(otherSwarmKey)
|
||||
generateKey(otherSwarmKey)
|
||||
|
||||
;(async () => {
|
||||
const node1 = await privateLibp2pNode(swarmKey)
|
||||
|
@ -57,9 +57,6 @@
|
||||
"./pnet": {
|
||||
"import": "./dist/src/pnet/index.js"
|
||||
},
|
||||
"./pnet/generate": {
|
||||
"import": "./dist/src/pnet/key-generator.js"
|
||||
},
|
||||
"./transport-manager": {
|
||||
"import": "./dist/src/transport-manager.js"
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import errCode from 'err-code'
|
||||
import { codes } from '../errors.js'
|
||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
import 'node-forge/lib/sha512.js'
|
||||
import { generateKeyPair, importKey, unmarshalPrivateKey } from '@libp2p/crypto/keys'
|
||||
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
||||
import type { Components } from '@libp2p/interfaces/components'
|
||||
|
@ -17,6 +17,8 @@ import type { ConnectionProtector } from '@libp2p/interfaces/connection'
|
||||
|
||||
const log = logger('libp2p:pnet')
|
||||
|
||||
export { generateKey } from './key-generator.js'
|
||||
|
||||
export interface ProtectorInit {
|
||||
enabled?: boolean
|
||||
psk: Uint8Array
|
||||
|
@ -8,7 +8,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
* @param {Uint8Array} bytes - An object to write the psk into
|
||||
* @returns {void}
|
||||
*/
|
||||
export function generate (bytes: Uint8Array) {
|
||||
export function generateKey (bytes: Uint8Array) {
|
||||
const psk = uint8ArrayToString(randomBytes(KEY_LENGTH), 'base16')
|
||||
const key = uint8ArrayFromString('/key/swarm/psk/1.0.0/\n/base16/\n' + psk)
|
||||
|
||||
|
@ -3,8 +3,7 @@ import { expect } from 'aegir/chai'
|
||||
import { pipe } from 'it-pipe'
|
||||
import all from 'it-all'
|
||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
import { PreSharedKeyConnectionProtector } from '../../src/pnet/index.js'
|
||||
import { generate } from '../../src/pnet/key-generator.js'
|
||||
import { PreSharedKeyConnectionProtector, generateKey } from '../../src/pnet/index.js'
|
||||
import { INVALID_PSK } from '../../src/pnet/errors.js'
|
||||
import { mockMultiaddrConnPair } from '@libp2p/interface-compliance-tests/mocks'
|
||||
import { Multiaddr } from '@multiformats/multiaddr'
|
||||
@ -14,8 +13,8 @@ const swarmKeyBuffer = new Uint8Array(95)
|
||||
const wrongSwarmKeyBuffer = new Uint8Array(95)
|
||||
|
||||
// Write new psk files to the buffers
|
||||
generate(swarmKeyBuffer)
|
||||
generate(wrongSwarmKeyBuffer)
|
||||
generateKey(swarmKeyBuffer)
|
||||
generateKey(wrongSwarmKeyBuffer)
|
||||
|
||||
describe('private network', () => {
|
||||
it('should accept a valid psk buffer', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user