|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +import { expect } from 'aegir/chai' |
| 4 | +import { webSockets } from '@libp2p/websockets' |
| 5 | +import { plaintext } from '../../src/insecure/index.js' |
| 6 | +import { createLibp2p, Libp2p } from '../../src/index.js' |
| 7 | +import { MemoryDatastore } from 'datastore-core' |
| 8 | + |
| 9 | +describe('peer-id', () => { |
| 10 | + let libp2p: Libp2p |
| 11 | + |
| 12 | + afterEach(async () => { |
| 13 | + if (libp2p != null) { |
| 14 | + await libp2p.stop() |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + it('should create a PeerId if none is passed', async () => { |
| 19 | + libp2p = await createLibp2p({ |
| 20 | + transports: [ |
| 21 | + webSockets() |
| 22 | + ], |
| 23 | + connectionEncryption: [ |
| 24 | + plaintext() |
| 25 | + ] |
| 26 | + }) |
| 27 | + |
| 28 | + expect(libp2p.peerId).to.be.ok() |
| 29 | + }) |
| 30 | + |
| 31 | + it('should retrieve the PeerId from the datastore', async () => { |
| 32 | + const datastore = new MemoryDatastore() |
| 33 | + |
| 34 | + libp2p = await createLibp2p({ |
| 35 | + datastore, |
| 36 | + transports: [ |
| 37 | + webSockets() |
| 38 | + ], |
| 39 | + connectionEncryption: [ |
| 40 | + plaintext() |
| 41 | + ] |
| 42 | + }) |
| 43 | + |
| 44 | + // this PeerId was created by default |
| 45 | + const peerId = libp2p.peerId |
| 46 | + |
| 47 | + await libp2p.stop() |
| 48 | + |
| 49 | + // create a new node from the same datastore |
| 50 | + libp2p = await createLibp2p({ |
| 51 | + datastore, |
| 52 | + transports: [ |
| 53 | + webSockets() |
| 54 | + ], |
| 55 | + connectionEncryption: [ |
| 56 | + plaintext() |
| 57 | + ] |
| 58 | + }) |
| 59 | + |
| 60 | + // the new node should have read the PeerId from the datastore |
| 61 | + // instead of creating a new one |
| 62 | + expect(libp2p.peerId.toString()).to.equal(peerId.toString()) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should retrieve the PeerId from the datastore with a keychain password', async () => { |
| 66 | + const datastore = new MemoryDatastore() |
| 67 | + const keychain = { |
| 68 | + pass: 'very-long-password-must-be-over-twenty-characters-long', |
| 69 | + dek: { |
| 70 | + salt: 'CpjNIxMqAZ+aJg+ezLfuzG4a' |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + libp2p = await createLibp2p({ |
| 75 | + datastore, |
| 76 | + keychain, |
| 77 | + transports: [ |
| 78 | + webSockets() |
| 79 | + ], |
| 80 | + connectionEncryption: [ |
| 81 | + plaintext() |
| 82 | + ] |
| 83 | + }) |
| 84 | + |
| 85 | + // this PeerId was created by default |
| 86 | + const peerId = libp2p.peerId |
| 87 | + |
| 88 | + await libp2p.stop() |
| 89 | + |
| 90 | + // create a new node from the same datastore |
| 91 | + libp2p = await createLibp2p({ |
| 92 | + datastore, |
| 93 | + keychain, |
| 94 | + transports: [ |
| 95 | + webSockets() |
| 96 | + ], |
| 97 | + connectionEncryption: [ |
| 98 | + plaintext() |
| 99 | + ] |
| 100 | + }) |
| 101 | + |
| 102 | + // the new node should have read the PeerId from the datastore |
| 103 | + // instead of creating a new one |
| 104 | + expect(libp2p.peerId.toString()).to.equal(peerId.toString()) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should fail to start if retrieving the PeerId from the datastore fails', async () => { |
| 108 | + const datastore = new MemoryDatastore() |
| 109 | + const keychain = { |
| 110 | + pass: 'very-long-password-must-be-over-twenty-characters-long', |
| 111 | + dek: { |
| 112 | + salt: 'CpjNIxMqAZ+aJg+ezLfuzG4a' |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + libp2p = await createLibp2p({ |
| 117 | + datastore, |
| 118 | + keychain, |
| 119 | + transports: [ |
| 120 | + webSockets() |
| 121 | + ], |
| 122 | + connectionEncryption: [ |
| 123 | + plaintext() |
| 124 | + ] |
| 125 | + }) |
| 126 | + await libp2p.stop() |
| 127 | + |
| 128 | + // creating a new node from the same datastore but with the wrong keychain config should fail |
| 129 | + await expect(createLibp2p({ |
| 130 | + datastore, |
| 131 | + keychain: { |
| 132 | + pass: 'different-very-long-password-must-be-over-twenty-characters-long', |
| 133 | + dek: { |
| 134 | + salt: 'different-CpjNIxMqAZ+aJg+ezLfuzG4a' |
| 135 | + } |
| 136 | + }, |
| 137 | + transports: [ |
| 138 | + webSockets() |
| 139 | + ], |
| 140 | + connectionEncryption: [ |
| 141 | + plaintext() |
| 142 | + ] |
| 143 | + })).to.eventually.rejectedWith('Invalid PEM formatted message') |
| 144 | + }) |
| 145 | +}) |
0 commit comments