Skip to content

Commit 73a28ed

Browse files
authored
feat!: keychain and configurable crypto (#1041)
Adds a configuration option to allow dynamically loading crypto implementations on-demand. Ships with only webcrypto-supported crypto implementations (e.g. `Ed25519`, `RSA`, `ECDSA`), anything else (e.g. `secp256k1`) must be configured separately. Imports [ipns](https://www.npmjs.com/package/ipns) code into `@helia/ipns` for ease of maintenance. Removes `@libp2p/keychain` dep and adds a `.keychain` property to Helia for securely storing private keys. BREAKING CHANGE: `secp256k1` support has been removed from the default config, it must now be configured separately
1 parent 347116a commit 73a28ed

66 files changed

Lines changed: 2680 additions & 709 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
"packages/*"
4848
],
4949
"overrides": {
50-
"playwright-core": "1.55.1"
50+
"multiformats": "^14.0.0",
51+
"uint8arrays": "^6.0.0",
52+
"interface-datastore": "^10.0.1",
53+
"uint8-varint": "^3.0.0"
5154
}
5255
}

packages/helia/.aegir.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
22
import { identify } from '@libp2p/identify'
33
import { WebSockets } from '@multiformats/mafmt'
44
import { CID } from 'multiformats/cid'
5-
import { sha256 } from 'multiformats/hashes/sha2'
65
import * as raw from 'multiformats/codecs/raw'
6+
import { sha256 } from 'multiformats/hashes/sha2'
77

88
/** @type {import('aegir').PartialOptions} */
99
const options = {
@@ -20,7 +20,7 @@ const options = {
2020
libp2p: {
2121
addresses: {
2222
listen: [
23-
`/ip4/127.0.0.1/tcp/0/ws`
23+
'/ip4/127.0.0.1/tcp/0/ws'
2424
]
2525
},
2626
connectionManager: {

packages/helia/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@chainsafe/libp2p-noise": "^17.0.0",
5353
"@chainsafe/libp2p-yamux": "^8.0.1",
5454
"@helia/block-brokers": "^5.2.4",
55-
"@helia/delegated-routing-v1-http-api-client": "^6.0.1",
55+
"@helia/delegated-routing-v1-http-api-client": "^7.0.1",
5656
"@helia/interface": "^6.2.1",
5757
"@helia/routers": "^5.1.1",
5858
"@helia/utils": "^2.5.2",
@@ -79,7 +79,6 @@
7979
"blockstore-core": "^7.0.1",
8080
"datastore-core": "^12.0.1",
8181
"interface-datastore": "^10.0.1",
82-
"ipns": "^11.0.0",
8382
"libp2p": "^3.2.0",
8483
"multiformats": "^14.0.0"
8584
},

packages/helia/src/utils/libp2p-defaults.browser.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ import { dcutr } from '@libp2p/dcutr'
99
import { http } from '@libp2p/http'
1010
import { identify, identifyPush } from '@libp2p/identify'
1111
import { kadDHT } from '@libp2p/kad-dht'
12-
import { keychain } from '@libp2p/keychain'
1312
import { mplex } from '@libp2p/mplex'
1413
import { ping } from '@libp2p/ping'
1514
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
1615
import { webSockets } from '@libp2p/websockets'
17-
import { ipnsSelector } from 'ipns/selector'
18-
import { ipnsValidator } from 'ipns/validator'
1916
import { userAgent } from 'libp2p/user-agent'
2017
import { name, version } from '../version.ts'
2118
import { bootstrapConfig } from './bootstrappers.ts'
2219
import type { Libp2pDefaultsOptions } from './libp2p.ts'
2320
import type { HTTP } from '@libp2p/http'
2421
import type { Identify } from '@libp2p/identify'
2522
import type { KadDHT } from '@libp2p/kad-dht'
26-
import type { Keychain } from '@libp2p/keychain'
2723
import type { Ping } from '@libp2p/ping'
2824
import type { Libp2pOptions } from 'libp2p'
2925

@@ -33,7 +29,6 @@ export interface DefaultLibp2pServices extends Record<string, unknown> {
3329
delegatedRouting: unknown
3430
dht: KadDHT
3531
identify: Identify
36-
keychain: Keychain
3732
ping: Ping
3833
http: HTTP
3934
}
@@ -74,17 +69,10 @@ export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOpti
7469
dcutr: dcutr(),
7570
delegatedRouting: delegatedRoutingV1HttpApiClient(delegatedHTTPRoutingDefaults()),
7671
dht: kadDHT({
77-
clientMode: true,
78-
validators: {
79-
ipns: ipnsValidator
80-
},
81-
selectors: {
82-
ipns: ipnsSelector
83-
}
72+
clientMode: true
8473
}),
8574
identify: identify(),
8675
identifyPush: identifyPush(),
87-
keychain: keychain(options.keychain),
8876
ping: ping(),
8977
http: http()
9078
}

packages/helia/src/utils/libp2p-defaults.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { noise } from '@chainsafe/libp2p-noise'
22
import { yamux } from '@chainsafe/libp2p-yamux'
3-
import { delegatedRoutingV1HttpApiClient } from '@helia/delegated-routing-v1-http-api-client'
3+
import { delegatedRoutingV1HttpApiClientContentRouting, delegatedRoutingV1HttpApiClientPeerRouting } from '@helia/delegated-routing-v1-http-api-client'
44
import { delegatedHTTPRoutingDefaults } from '@helia/routers'
55
import { autoTLS } from '@ipshipyard/libp2p-auto-tls'
66
import { autoNAT } from '@libp2p/autonat'
@@ -19,8 +19,6 @@ import { tls } from '@libp2p/tls'
1919
import { uPnPNAT } from '@libp2p/upnp-nat'
2020
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
2121
import { webSockets } from '@libp2p/websockets'
22-
import { ipnsSelector } from 'ipns/selector'
23-
import { ipnsValidator } from 'ipns/validator'
2422
import { userAgent } from 'libp2p/user-agent'
2523
import { name, version } from '../version.ts'
2624
import { bootstrapConfig } from './bootstrappers.ts'
@@ -38,7 +36,8 @@ export interface DefaultLibp2pServices extends Record<string, unknown> {
3836
autoNAT: unknown
3937
autoTLS: AutoTLS
4038
dcutr: unknown
41-
delegatedRouting: unknown
39+
delegatedContentRouting: unknown
40+
delegatedPeerRouting: unknown
4241
dht: KadDHT
4342
identify: Identify
4443
keychain: Keychain
@@ -116,15 +115,9 @@ export function libp2pDefaults (options: Libp2pDefaultsOptions = {}): Libp2pOpti
116115
autoNAT: autoNAT(),
117116
autoTLS: autoTLS(),
118117
dcutr: dcutr(),
119-
delegatedRouting: delegatedRoutingV1HttpApiClient(delegatedHTTPRoutingDefaults()),
120-
dht: kadDHT({
121-
validators: {
122-
ipns: ipnsValidator
123-
},
124-
selectors: {
125-
ipns: ipnsSelector
126-
}
127-
}),
118+
delegatedPeerRouting: delegatedRoutingV1HttpApiClientPeerRouting(delegatedHTTPRoutingDefaults()),
119+
delegatedContentRouting: delegatedRoutingV1HttpApiClientContentRouting(delegatedHTTPRoutingDefaults()),
120+
dht: kadDHT(),
128121
identify: identify(),
129122
identifyPush: identifyPush(),
130123
keychain: keychain(options.keychain),

packages/helia/src/utils/libp2p.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export async function createLibp2p <T extends Record<string, unknown>> (options:
2626

2727
// if no peer id was passed, try to load it from the keychain
2828
if (libp2pOptions.privateKey == null && options.datastore != null) {
29-
// @ts-expect-error libp2p needs dep updates
3029
libp2pOptions.privateKey = await loadOrCreateSelfKey(options.datastore, options.keychain)
3130
}
3231

packages/http/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"dependencies": {
5151
"@helia/block-brokers": "^5.2.4",
52-
"@helia/delegated-routing-v1-http-api-client": "^6.0.1",
52+
"@helia/delegated-routing-v1-http-api-client": "^7.0.1",
5353
"@helia/interface": "^6.2.1",
5454
"@helia/routers": "^5.1.1",
5555
"@helia/utils": "^2.5.2",

packages/http/src/utils/libp2p.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export async function createLibp2p <T extends Record<string, unknown>> (options:
2626

2727
// if no peer id was passed, try to load it from the keychain
2828
if (libp2pOptions.privateKey == null && options.datastore != null) {
29-
// @ts-expect-error libp2p needs dep updates
3029
libp2pOptions.privateKey = await loadOrCreateSelfKey(options.datastore, options.keychain)
3130
}
3231

packages/interface/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@
7272
"build": "aegir build"
7373
},
7474
"dependencies": {
75+
"@ipshipyard/crypto": "^1.1.0",
76+
"@ipshipyard/keychain": "^1.0.2",
7577
"@libp2p/interface": "^3.2.0",
7678
"@multiformats/dns": "^1.0.13",
7779
"@multiformats/multiaddr": "^13.0.1",
80+
"abort-error": "^1.0.2",
7881
"interface-blockstore": "^7.0.1",
7982
"interface-datastore": "^10.0.1",
8083
"multiformats": "^14.0.0",

packages/interface/src/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ export class InvalidCodecError extends Error {
4242
this.name = 'InvalidCodecError'
4343
}
4444
}
45+
46+
export class UnknownCryptoError extends Error {
47+
static name = 'UnknownCryptoError'
48+
name = 'UnknownCryptoError'
49+
}

0 commit comments

Comments
 (0)