@libp2p/config 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -0
- package/dist/src/index.d.ts +39 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +38 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/load-private-key.d.ts +13 -0
- package/dist/src/load-private-key.d.ts.map +1 -0
- package/dist/src/load-private-key.js +22 -0
- package/dist/src/load-private-key.js.map +1 -0
- package/dist/typedoc-urls.json +4 -0
- package/package.json +59 -0
- package/src/index.ts +39 -0
- package/src/load-private-key.ts +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @libp2p/config
|
|
2
|
+
|
|
3
|
+
[](http://libp2p.io/)
|
|
4
|
+
[](https://discuss.libp2p.io)
|
|
5
|
+
[](https://codecov.io/gh/libp2p/js-libp2p)
|
|
6
|
+
[](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
|
|
7
|
+
|
|
8
|
+
> Helper functions to make dealing with libp2p config easier
|
|
9
|
+
|
|
10
|
+
# About
|
|
11
|
+
|
|
12
|
+
<!--
|
|
13
|
+
|
|
14
|
+
!IMPORTANT!
|
|
15
|
+
|
|
16
|
+
Everything in this README between "# About" and "# Install" is automatically
|
|
17
|
+
generated and will be overwritten the next time the doc generator is run.
|
|
18
|
+
|
|
19
|
+
To make changes to this section, please update the @packageDocumentation section
|
|
20
|
+
of src/index.js or src/index.ts
|
|
21
|
+
|
|
22
|
+
To experiment with formatting, please run "npm run docs" from the root of this
|
|
23
|
+
repo and examine the changes made.
|
|
24
|
+
|
|
25
|
+
-->
|
|
26
|
+
|
|
27
|
+
## Example - Load or create the "self" private key in a datastore
|
|
28
|
+
|
|
29
|
+
Most nodes will want to persist the same private key between restarts so this
|
|
30
|
+
function helps you extract one from a datastore if it exists, otherwise it
|
|
31
|
+
will create a new one and save it in the keystore.
|
|
32
|
+
|
|
33
|
+
The options you pass to this function should be the same as those passed to
|
|
34
|
+
the `@libp2p/keychain` service you configure your node with.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { loadOrCreateSelfKey } from '@libp2p/config'
|
|
38
|
+
import { keychain } from '@libp2p/keychain'
|
|
39
|
+
import { LevelDatastore } from 'datastore-level'
|
|
40
|
+
import { createLibp2p } from 'libp2p'
|
|
41
|
+
|
|
42
|
+
const datastore = new LevelDatastore('/path/to/db')
|
|
43
|
+
await datastore.open()
|
|
44
|
+
|
|
45
|
+
const keychainInit = {
|
|
46
|
+
pass: 'yes-yes-very-secure'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const privateKey = await loadOrCreateSelfKey(datastore, keychainInit)
|
|
50
|
+
|
|
51
|
+
const node = await createLibp2p({
|
|
52
|
+
privateKey,
|
|
53
|
+
services: {
|
|
54
|
+
datastore,
|
|
55
|
+
keychain: keychain(keychainInit)
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
# Install
|
|
61
|
+
|
|
62
|
+
```console
|
|
63
|
+
$ npm i @libp2p/config
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
# API Docs
|
|
67
|
+
|
|
68
|
+
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_config.html>
|
|
69
|
+
|
|
70
|
+
# License
|
|
71
|
+
|
|
72
|
+
Licensed under either of
|
|
73
|
+
|
|
74
|
+
- Apache 2.0, ([LICENSE-APACHE](https://github.com/libp2p/js-libp2p/blob/main/packages/config/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
|
75
|
+
- MIT ([LICENSE-MIT](https://github.com/libp2p/js-libp2p/blob/main/packages/config/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
|
76
|
+
|
|
77
|
+
# Contribution
|
|
78
|
+
|
|
79
|
+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* @example Load or create a private key in a datastore
|
|
5
|
+
*
|
|
6
|
+
* Most nodes will want to persist the same private key between restarts so this
|
|
7
|
+
* function helps you extract one from a datastore if it exists, otherwise it
|
|
8
|
+
* will create a new one and save it in the keystore.
|
|
9
|
+
*
|
|
10
|
+
* The options you pass to this function should be the same as those passed to
|
|
11
|
+
* the `@libp2p/keychain` service you configure your node with.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { loadOrCreateSelfKey } from '@libp2p/config'
|
|
15
|
+
* import { keychain } from '@libp2p/keychain'
|
|
16
|
+
* import { LevelDatastore } from 'datastore-level'
|
|
17
|
+
* import { createLibp2p } from 'libp2p'
|
|
18
|
+
*
|
|
19
|
+
* const datastore = new LevelDatastore('/path/to/db')
|
|
20
|
+
* await datastore.open()
|
|
21
|
+
*
|
|
22
|
+
* const keychainInit = {
|
|
23
|
+
* pass: 'yes-yes-very-secure'
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const privateKey = await loadOrCreateSelfKey(datastore, keychainInit)
|
|
27
|
+
*
|
|
28
|
+
* const node = await createLibp2p({
|
|
29
|
+
* privateKey,
|
|
30
|
+
* services: {
|
|
31
|
+
* datastore,
|
|
32
|
+
* keychain: keychain(keychainInit)
|
|
33
|
+
* }
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export { loadOrCreateSelfKey } from './load-private-key.js';
|
|
38
|
+
export type { LoadOrCreateSelfKeyOptions } from './load-private-key.js';
|
|
39
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,YAAY,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* @example Load or create a private key in a datastore
|
|
5
|
+
*
|
|
6
|
+
* Most nodes will want to persist the same private key between restarts so this
|
|
7
|
+
* function helps you extract one from a datastore if it exists, otherwise it
|
|
8
|
+
* will create a new one and save it in the keystore.
|
|
9
|
+
*
|
|
10
|
+
* The options you pass to this function should be the same as those passed to
|
|
11
|
+
* the `@libp2p/keychain` service you configure your node with.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { loadOrCreateSelfKey } from '@libp2p/config'
|
|
15
|
+
* import { keychain } from '@libp2p/keychain'
|
|
16
|
+
* import { LevelDatastore } from 'datastore-level'
|
|
17
|
+
* import { createLibp2p } from 'libp2p'
|
|
18
|
+
*
|
|
19
|
+
* const datastore = new LevelDatastore('/path/to/db')
|
|
20
|
+
* await datastore.open()
|
|
21
|
+
*
|
|
22
|
+
* const keychainInit = {
|
|
23
|
+
* pass: 'yes-yes-very-secure'
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const privateKey = await loadOrCreateSelfKey(datastore, keychainInit)
|
|
27
|
+
*
|
|
28
|
+
* const node = await createLibp2p({
|
|
29
|
+
* privateKey,
|
|
30
|
+
* services: {
|
|
31
|
+
* datastore,
|
|
32
|
+
* keychain: keychain(keychainInit)
|
|
33
|
+
* }
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export { loadOrCreateSelfKey } from './load-private-key.js';
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PrivateKey, KeyType } from '@libp2p/interface';
|
|
2
|
+
import type { KeychainInit } from '@libp2p/keychain';
|
|
3
|
+
import type { Datastore } from 'interface-datastore';
|
|
4
|
+
export interface LoadOrCreateSelfKeyOptions extends KeychainInit {
|
|
5
|
+
/**
|
|
6
|
+
* If no private key is found in the datastore, create one with this type
|
|
7
|
+
*
|
|
8
|
+
* @default 'Ed25519'
|
|
9
|
+
*/
|
|
10
|
+
keyType?: KeyType;
|
|
11
|
+
}
|
|
12
|
+
export declare function loadOrCreateSelfKey(datastore: Datastore, init?: LoadOrCreateSelfKeyOptions): Promise<PrivateKey>;
|
|
13
|
+
//# sourceMappingURL=load-private-key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-private-key.d.ts","sourceRoot":"","sources":["../../src/load-private-key.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAEpD,MAAM,WAAW,0BAA2B,SAAQ,YAAY;IAC9D;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,wBAAsB,mBAAmB,CAAE,SAAS,EAAE,SAAS,EAAE,IAAI,GAAE,0BAA+B,GAAG,OAAO,CAAC,UAAU,CAAC,CAmB3H"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { generateKeyPair } from '@libp2p/crypto/keys';
|
|
2
|
+
import { keychain } from '@libp2p/keychain';
|
|
3
|
+
import { defaultLogger } from '@libp2p/logger';
|
|
4
|
+
import { Key } from 'interface-datastore';
|
|
5
|
+
export async function loadOrCreateSelfKey(datastore, init = {}) {
|
|
6
|
+
const chain = keychain(init)({
|
|
7
|
+
datastore,
|
|
8
|
+
logger: defaultLogger()
|
|
9
|
+
});
|
|
10
|
+
const selfKey = new Key('/pkcs8/self');
|
|
11
|
+
let privateKey;
|
|
12
|
+
if (await datastore.has(selfKey)) {
|
|
13
|
+
privateKey = await chain.exportKey('self');
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
privateKey = await generateKeyPair(init.keyType ?? 'Ed25519');
|
|
17
|
+
// persist the peer id in the keychain for next time
|
|
18
|
+
await chain.importKey('self', privateKey);
|
|
19
|
+
}
|
|
20
|
+
return privateKey;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=load-private-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-private-key.js","sourceRoot":"","sources":["../../src/load-private-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AAczC,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAE,SAAoB,EAAE,OAAmC,EAAE;IACpG,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC3B,SAAS;QACT,MAAM,EAAE,aAAa,EAAE;KACxB,CAAC,CAAA;IAEF,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAA;IACtC,IAAI,UAAU,CAAA;IAEd,IAAI,MAAM,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,UAAU,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAA;IAC5C,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,OAAO,IAAI,SAAS,CAAC,CAAA;QAE7D,oDAAoD;QACpD,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;IAC3C,CAAC;IAED,OAAO,UAAU,CAAA;AACnB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@libp2p/config",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Helper functions to make dealing with libp2p config easier",
|
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/config#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "./dist/src/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"dist",
|
|
22
|
+
"!dist/test",
|
|
23
|
+
"!**/*.tsbuildinfo"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/src/index.d.ts",
|
|
28
|
+
"import": "./dist/src/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"eslintConfig": {
|
|
32
|
+
"extends": "ipfs",
|
|
33
|
+
"parserOptions": {
|
|
34
|
+
"project": true,
|
|
35
|
+
"sourceType": "module"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "aegir build --bundle false",
|
|
40
|
+
"test": "aegir test -t node",
|
|
41
|
+
"clean": "aegir clean",
|
|
42
|
+
"lint": "aegir lint",
|
|
43
|
+
"test:node": "aegir test -t node --cov",
|
|
44
|
+
"dep-check": "aegir dep-check",
|
|
45
|
+
"doc-check": "aegir doc-check"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@libp2p/crypto": "^5.0.7",
|
|
49
|
+
"@libp2p/interface": "^2.3.0",
|
|
50
|
+
"@libp2p/keychain": "^5.0.10",
|
|
51
|
+
"@libp2p/logger": "^5.1.4",
|
|
52
|
+
"interface-datastore": "^8.3.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"aegir": "^45.0.5",
|
|
56
|
+
"datastore-core": "^10.0.2"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false
|
|
59
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* @example Load or create the "self" private key in a datastore
|
|
5
|
+
*
|
|
6
|
+
* Most nodes will want to persist the same private key between restarts so this
|
|
7
|
+
* function helps you extract one from a datastore if it exists, otherwise it
|
|
8
|
+
* will create a new one and save it in the keystore.
|
|
9
|
+
*
|
|
10
|
+
* The options you pass to this function should be the same as those passed to
|
|
11
|
+
* the `@libp2p/keychain` service you configure your node with.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { loadOrCreateSelfKey } from '@libp2p/config'
|
|
15
|
+
* import { keychain } from '@libp2p/keychain'
|
|
16
|
+
* import { LevelDatastore } from 'datastore-level'
|
|
17
|
+
* import { createLibp2p } from 'libp2p'
|
|
18
|
+
*
|
|
19
|
+
* const datastore = new LevelDatastore('/path/to/db')
|
|
20
|
+
* await datastore.open()
|
|
21
|
+
*
|
|
22
|
+
* const keychainInit = {
|
|
23
|
+
* pass: 'yes-yes-very-secure'
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const privateKey = await loadOrCreateSelfKey(datastore, keychainInit)
|
|
27
|
+
*
|
|
28
|
+
* const node = await createLibp2p({
|
|
29
|
+
* privateKey,
|
|
30
|
+
* services: {
|
|
31
|
+
* datastore,
|
|
32
|
+
* keychain: keychain(keychainInit)
|
|
33
|
+
* }
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export { loadOrCreateSelfKey } from './load-private-key.js'
|
|
39
|
+
export type { LoadOrCreateSelfKeyOptions } from './load-private-key.js'
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { generateKeyPair } from '@libp2p/crypto/keys'
|
|
2
|
+
import { keychain } from '@libp2p/keychain'
|
|
3
|
+
import { defaultLogger } from '@libp2p/logger'
|
|
4
|
+
import { Key } from 'interface-datastore'
|
|
5
|
+
import type { PrivateKey, KeyType } from '@libp2p/interface'
|
|
6
|
+
import type { KeychainInit } from '@libp2p/keychain'
|
|
7
|
+
import type { Datastore } from 'interface-datastore'
|
|
8
|
+
|
|
9
|
+
export interface LoadOrCreateSelfKeyOptions extends KeychainInit {
|
|
10
|
+
/**
|
|
11
|
+
* If no private key is found in the datastore, create one with this type
|
|
12
|
+
*
|
|
13
|
+
* @default 'Ed25519'
|
|
14
|
+
*/
|
|
15
|
+
keyType?: KeyType
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function loadOrCreateSelfKey (datastore: Datastore, init: LoadOrCreateSelfKeyOptions = {}): Promise<PrivateKey> {
|
|
19
|
+
const chain = keychain(init)({
|
|
20
|
+
datastore,
|
|
21
|
+
logger: defaultLogger()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const selfKey = new Key('/pkcs8/self')
|
|
25
|
+
let privateKey
|
|
26
|
+
|
|
27
|
+
if (await datastore.has(selfKey)) {
|
|
28
|
+
privateKey = await chain.exportKey('self')
|
|
29
|
+
} else {
|
|
30
|
+
privateKey = await generateKeyPair(init.keyType ?? 'Ed25519')
|
|
31
|
+
|
|
32
|
+
// persist the peer id in the keychain for next time
|
|
33
|
+
await chain.importKey('self', privateKey)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return privateKey
|
|
37
|
+
}
|