This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathinit.spec.js
188 lines (146 loc) · 7.32 KB
/
init.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* eslint max-nested-callbacks: ["error", 8] */
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import { isNode } from 'ipfs-utils/src/env.js'
import { CID } from 'multiformats/cid'
import { peerIdFromKeys } from '@libp2p/peer-id'
import createNode from './utils/create-node.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays'
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'
const privateKey = 'CAASqAkwggSkAgEAAoIBAQChVmiObYo6pkKrMSd3OzW1cTL+RDmX1rkETYGKWV9TPXMNgElFTYoYHqT9QZomj5RI8iUmHccjzqr4J0mV+E0NpvHHOLlmDZ82lAw2Zx7saUkeQWvC0S9Z0o3aTx2sSubZV53rSomkZgQH4fYTs4RERejV4ltzLFdzQQBwWrBvlagpPHUCxKDUCnE5oIzdbD26ltWViPBWr7TfotzC8Lyi/tceqCpHMUJGMbsVgypnlgpey07MBvs71dVh5LcRen/ztsQO6Yju4D3QgWoyD0SIUdJFvBzEwL9bSiA3QjUc/fkGd7EcdN5bebYOqAi4ZIiAMLp3i4+B8Tzq/acull43AgMBAA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49bA9H2CoLpBGTV1LfY5yTFxRy4R3qnX/IzsKy567sbtkEFKJxplc/RzCQfrgbdj7k26SbKtHR3yERaFGRYq8UeAHeYC1/N19LF5BMQL4y5R4PJ1SFPeJCL/wXiMqs1maTqvKqtc4bbegNdwlxn'
const edPrivateKey = 'CAESYFeZamw+9QdwHgSmcvPmfLUpmWTtYpUeycbXcfnkTnDI7OaPmE6V8i+Lw7FNB5CtYuDFKUsOS5h+AogyF/Dft4Ds5o+YTpXyL4vDsU0HkK1i4MUpSw5LmH4CiDIX8N+3gA=='
const secpPrivateKey = 'CAISIKCfwZsMEwmzLxGv9duM6j6YQzMx2V46+Yl3laV24Qus'
describe('init', function () {
if (!isNode) return
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/** @type {import('ipfs-repo').IPFSRepo} */
let repo
/** @type {() => Promise<void>} */
let cleanup
/**
* @param {import('../src/types').Options} [options]
*/
const init = async (options) => {
const res = await createNode({
...options,
start: false
})
ipfs = res.ipfs
repo = res.repo
cleanup = res.cleanup
return ipfs
}
afterEach(() => cleanup())
it('should init successfully', async () => {
await init()
const res = await repo.exists()
expect(res).to.equal(true)
const config = await repo.config.getAll()
expect(config).to.have.property('Identity')
expect(config).to.have.nested.property('Keychain.DEK')
})
it('should init successfully with a keychain pass', async () => {
await init({
pass: 'super-super-secure-1234',
init: {
algorithm: 'RSA',
bits: 512
}
})
const res = await repo.exists()
expect(res).to.equal(true)
const config = await repo.config.getAll()
expect(config.Keychain).to.exist()
const { ipfs: ipfs2, repo: repo2 } = await createNode({
repo: repo,
pass: 'something-else-that-is-long-enough',
start: false,
init: {
algorithm: 'RSA',
bits: 512
}
})
// same repo, same peer id
expect(repo.path).to.equal(repo2.path)
expect(await ipfs2.id()).to.deep.equal(await ipfs.id())
// opened with correct password
await expect(ipfs.key.export('self', 'some-other-password')).to.eventually.be.ok()
// opened with incorrect password
await expect(ipfs2.key.export('self', 'some-other-password')).to.eventually.be.rejected()
})
it('should init with a key algorithm (RSA)', async () => {
await init({ init: { algorithm: 'RSA' } })
const config = await repo.config.getAll()
const buf = uint8ArrayFromString(`${config.Identity?.PrivKey}`, 'base64pad')
const key = await unmarshalPrivateKey(buf)
const peerId = await peerIdFromKeys(key.public.bytes, key.bytes)
expect(peerId.type).equals('RSA')
})
it('should init with a key algorithm (Ed25519)', async () => {
await init({ init: { algorithm: 'Ed25519' } })
const config = await repo.config.getAll()
const buf = uint8ArrayFromString(`${config.Identity?.PrivKey}`, 'base64pad')
const key = await unmarshalPrivateKey(buf)
const peerId = await peerIdFromKeys(key.public.bytes, key.bytes)
expect(peerId.type).equals('Ed25519')
})
// not supported any more
it.skip('should init with a key algorithm (secp256k1)', async () => {
await init({ init: { algorithm: 'secp256k1' } })
const config = await repo.config.getAll()
const buf = uint8ArrayFromString(`${config.Identity?.PrivKey}`, 'base64pad')
const key = await unmarshalPrivateKey(buf)
const peerId = await peerIdFromKeys(key.public.bytes, key.bytes)
expect(peerId.type).equals('secp256k1')
})
it('should set # of bits in key', async function () {
this.timeout(120 * 1000)
await init({
init: {
algorithm: 'RSA',
bits: 1024
}
})
const config = await repo.config.getAll()
expect(config.Identity?.PrivKey.length).is.above(256)
})
it('should allow a pregenerated RSA key to be used', async () => {
await init({ init: { privateKey } })
const config = await repo.config.getAll()
expect(config.Identity?.PeerID).is.equal('QmRsooYQasV5f5r834NSpdUtmejdQcpxXkK6qsozZWEihC')
})
it('should allow a pregenerated Ed25519 key to be used', async () => {
await init({ init: { privateKey: edPrivateKey } })
const config = await repo.config.getAll()
expect(config.Identity?.PeerID).is.equal('12D3KooWRm8J3iL796zPFi2EtGGtUJn58AG67gcqzMFHZnnsTzqD')
})
it('should allow a pregenerated secp256k1 key to be used', async () => {
await init({ init: { privateKey: secpPrivateKey } })
const config = await repo.config.getAll()
expect(config.Identity?.PeerID).is.equal('16Uiu2HAm5qw8UyXP2RLxQUx5KvtSN8DsTKz8quRGqGNC3SYiaB8E')
})
it('should write init docs', async () => {
await init()
const multihash = CID.parse('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
const node = await ipfs.object.get(multihash, { enc: 'base58' })
expect(node.Links).to.exist()
})
it('should allow init with an empty repo', async () => {
await init({ init: { emptyRepo: true } })
// Should not have default assets
const multihash = CID.parse('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
await expect(ipfs.object.get(multihash, {})).to.eventually.be.rejected().with.property('code', 'ERR_NOT_FOUND')
})
it('should apply one profile', async () => {
await init({ init: { profiles: ['test'] } })
const config = await repo.config.getAll()
expect(config.Bootstrap).to.be.empty()
})
it('should apply multiple profiles', async () => {
await init({ init: { profiles: ['test', 'local-discovery'] } })
const config = await repo.config.getAll()
expect(config.Bootstrap).to.be.empty()
expect(config.Discovery?.MDNS?.Enabled).to.be.true()
})
})