This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -251,6 +251,7 @@ Instead of a boolean, you may provide an object with custom initialization optio
251
251
252
252
- ` emptyRepo ` (boolean) Whether to remove built-in assets, like the instructional tour and empty mutable file system, from the repo. (Default: ` false ` )
253
253
- ` 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 ` .**
254
255
- ` 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).
255
256
256
257
##### ` options.start `
Original file line number Diff line number Diff line change @@ -22,6 +22,11 @@ module.exports = {
22
22
type : 'boolean' ,
23
23
describe : "Don't add and pin help files to the local storage"
24
24
} )
25
+ . option ( 'privateKey' , {
26
+ alias : 'k' ,
27
+ type : 'string' ,
28
+ describe : 'Pre-generated private key to use for the repo'
29
+ } )
25
30
} ,
26
31
27
32
handler ( argv ) {
@@ -41,6 +46,7 @@ module.exports = {
41
46
42
47
node . init ( {
43
48
bits : argv . bits ,
49
+ privateKey : argv . privateKey ,
44
50
emptyRepo : argv . emptyRepo ,
45
51
pass : argv . pass ,
46
52
log : print
Original file line number Diff line number Diff line change @@ -61,10 +61,19 @@ module.exports = function init (self) {
61
61
return cb ( new Error ( 'repo already exists' ) )
62
62
}
63
63
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
+ }
68
77
} ,
69
78
( keys , cb ) => {
70
79
self . log ( 'identity generated' )
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ const multihash = require('multihashes')
20
20
const CID = require ( 'cids' )
21
21
const IPFS = require ( '../../src/core' )
22
22
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
+
23
25
// This gets replaced by `create-repo-browser.js` in the browser
24
26
const createTempRepo = require ( '../utils/create-repo-nodejs.js' )
25
27
@@ -73,6 +75,20 @@ describe('init', () => {
73
75
} )
74
76
} )
75
77
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
+
76
92
it ( 'init docs are written' , ( done ) => {
77
93
ipfs . init ( { bits : 512 , pass : hat ( ) } , ( err ) => {
78
94
expect ( err ) . to . not . exist ( )
You can’t perform that action at this time.
0 commit comments