Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 61b91f8

Browse files
mkg20001alanshaw
authored andcommitted
feat: allow pregenerated private key (#1485)
1 parent f339eba commit 61b91f8

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Instead of a boolean, you may provide an object with custom initialization optio
251251

252252
- `emptyRepo` (boolean) Whether to remove built-in assets, like the instructional tour and empty mutable file system, from the repo. (Default: `false`)
253253
- `bits` (number) Number of bits to use in the generated key pair. (Default: `2048`)
254+
- `privateKey` (string/PeerId) A pre-generated private key to use. Can be either a base64 string or a [PeerId](https://github.com/libp2p/js-peer-id) instance. **NOTE: This overrides `bits`.**
254255
- `pass` (string) A passphrase to encrypt keys. You should generally use the [top-level `pass` option](#optionspass) instead of the `init.pass` option (this one will take its value from the top-level option if not set).
255256

256257
##### `options.start`

src/cli/commands/init.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ module.exports = {
2222
type: 'boolean',
2323
describe: "Don't add and pin help files to the local storage"
2424
})
25+
.option('privateKey', {
26+
alias: 'k',
27+
type: 'string',
28+
describe: 'Pre-generated private key to use for the repo'
29+
})
2530
},
2631

2732
handler (argv) {
@@ -41,6 +46,7 @@ module.exports = {
4146

4247
node.init({
4348
bits: argv.bits,
49+
privateKey: argv.privateKey,
4450
emptyRepo: argv.emptyRepo,
4551
pass: argv.pass,
4652
log: print

src/core/components/init.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,19 @@ module.exports = function init (self) {
6161
return cb(new Error('repo already exists'))
6262
}
6363

64-
// Generate peer identity keypair + transform to desired format + add to config.
65-
opts.log(`generating ${opts.bits}-bit RSA keypair...`, false)
66-
self.log('generating peer id: %s bits', opts.bits)
67-
peerId.create({ bits: opts.bits }, cb)
64+
if (opts.privateKey) {
65+
self.log('using user-supplied private-key')
66+
if (typeof opts.privateKey === 'object') {
67+
cb(null, opts.privateKey)
68+
} else {
69+
peerId.createFromPrivKey(Buffer.from(opts.privateKey, 'base64'), cb)
70+
}
71+
} else {
72+
// Generate peer identity keypair + transform to desired format + add to config.
73+
opts.log(`generating ${opts.bits}-bit RSA keypair...`, false)
74+
self.log('generating peer id: %s bits', opts.bits)
75+
peerId.create({ bits: opts.bits }, cb)
76+
}
6877
},
6978
(keys, cb) => {
7079
self.log('identity generated')

test/core/init.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const multihash = require('multihashes')
2020
const CID = require('cids')
2121
const IPFS = require('../../src/core')
2222

23+
const privateKey = 'CAASqAkwggSkAgEAAoIBAQChVmiObYo6pkKrMSd3OzW1cTL+RDmX1rkETYGKWV9TPXMNgElFTYoYHqT9QZomj5RI8iUmHccjzqr4J0mV+E0NpvHHOLlmDZ82lAw2Zx7saUkeQWvC0S9Z0o3aTx2sSubZV53rSomkZgQH4fYTs4RERejV4ltzLFdzQQBwWrBvlagpPHUCxKDUCnE5oIzdbD26ltWViPBWr7TfotzC8Lyi/tceqCpHMUJGMbsVgypnlgpey07MBvs71dVh5LcRen/ztsQO6Yju4D3QgWoyD0SIUdJFvBzEwL9bSiA3QjUc/fkGd7EcdN5bebYOqAi4ZIiAMLp3i4+B8Tzq/acull43AgMBAAECggEBAIDgZE75o4SsEO9tKWht7L5OeXxxBUyMImkUfJkGQUZd/MzZIC5y/Q+9UvBW+gs5gCsw+onTGaM50Iq/32Ej4nE4XURVxIuH8BmJ86N1hlc010qK2cjajqeCsPulXT+m6XbOLYCpnv+q2idt0cL1EH/1FEPeOEztK8ION4qIdw36SoykfTx/RqtkKHtS01AwN82EOPbWk7huyQT5R5MsCZmRJXBFkpNtiL+8619BH2aVlghHO4NouF9wQjdz/ysVuyYg+3rX2cpGjuHDTZ6hVQiJD1lF6D+dua7UPyHYAG2iRQiKZmCjitt9ywzPxiRaYF/aZ02FEMWckZulR09axskCgYEAzjl6ER8WwxYHn4tHse+CrIIF2z5cscdrh7KSwd3Rse9hIIBDJ/0KkvoYd1IcWrS8ywLrRfSLIjEU9u7IN1m+IRVWJ61fXNqOHm9clAu6qNhCN6W2+JfxDkUygTwmsq0v3huO+qkiMQz+a4nAXJe8Utd36ywgPhVGxFa/7x1v1N0CgYEAyEdiYRFf1aQZcO7+B2FH+tkGJsB30VIBhcpG9EukuQUUulLHhScc/KRj+EFAACLdkTqlVI0xVYIWaaCXwoQCWKixjZ5mYPC+bBLgn4IoDS6XTdHtR7Vn3UUvGTKsM0/z4e8/0eSzGNCHoYez9IoBlPNic0sQuST4jzgS2RYnFCMCgYASWSzSLyjwTJp7CIJlg4Dl5l+tBRxsOOkJVssV8q2AnmLO6HqRKUNylkvs+eJJ88DEc0sJm1txvFo4KkCoJBT1jpduyk8szMlOTew3w99kvHEP0G+6KJKrCV8X/okW5q/WnC8ZgEjpglV0rfnugxWfbUpfIzrvKydzuqAzHzRfBQKBgQDANtKSeoxRjEbmfljLWHAure8bbgkQmfXgI7xpZdfXwqqcECpw/pLxXgycDHOSLeQcJ/7Y4RGCEXHVOk2sX+mokW6mjmmPjD4VlyCBtfcef6KzC1EBS3c9g9KqCln+fTOBmY7UsPu6SxiAzK7HeVP/Un8gS+Dm8DalrZlZQ8uJpQKBgF6mL/Xo/XUOiz2jAD18l8Y6s49bA9H2CoLpBGTV1LfY5yTFxRy4R3qnX/IzsKy567sbtkEFKJxplc/RzCQfrgbdj7k26SbKtHR3yERaFGRYq8UeAHeYC1/N19LF5BMQL4y5R4PJ1SFPeJCL/wXiMqs1maTqvKqtc4bbegNdwlxn'
24+
2325
// This gets replaced by `create-repo-browser.js` in the browser
2426
const createTempRepo = require('../utils/create-repo-nodejs.js')
2527

@@ -73,6 +75,20 @@ describe('init', () => {
7375
})
7476
})
7577

78+
it('pregenerated key is being used', function (done) {
79+
this.timeout(10 * 1000)
80+
81+
ipfs.init({ privateKey }, (err) => {
82+
expect(err).to.not.exist()
83+
84+
repo.config.get((err, config) => {
85+
expect(err).to.not.exist()
86+
expect(config.Identity.PeerID).is.equal('QmRsooYQasV5f5r834NSpdUtmejdQcpxXkK6qsozZWEihC')
87+
done()
88+
})
89+
})
90+
})
91+
7692
it('init docs are written', (done) => {
7793
ipfs.init({ bits: 512, pass: hat() }, (err) => {
7894
expect(err).to.not.exist()

0 commit comments

Comments
 (0)