Peer DID Integration
This guide covers the did:peer method integration for TrustWeave. The did:peer plugin provides peer-to-peer DIDs without external registries or blockchains.
Overview
The did/plugins/peer module provides an implementation of TrustWeave’s DidMethod interface using the peer DID method. This integration enables you to:
Create and resolve peer DIDs for P2P communication
Store DID documents locally (no external registry)
Support numalgo 0, 1, and 2
Embedded document resolution
No blockchain or HTTP dependencies
Installation
Add the did:peer module to your dependencies:
1
2
3
4
5
6
dependencies {
implementation ( "org.trustweave:did-plugins-peer:0.6.0" )
implementation ( "org.trustweave:did-did-core:0.6.0" )
implementation ( "org.trustweave:did-plugins-base:0.6.0" )
implementation ( "org.trustweave:common:0.6.0" )
}
Configuration
Basic Configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.trustweave.peerdid.*
import org.trustweave.kms.*
// Create configuration
val config = PeerDidConfig . builder ()
. numalgo ( 2 ) // Use numalgo 2 (recommended)
. includeServices ( true )
. build ()
// Create KMS
val kms = InMemoryKeyManagementService ()
// Create did:peer method
val method = PeerDidMethod ( kms , config )
1
2
3
4
5
6
7
8
// Numalgo 0 (static numeric)
val config0 = PeerDidConfig . numalgo0 ()
// Numalgo 1 (short-form with inception key)
val config1 = PeerDidConfig . numalgo1 ()
// Numalgo 2 (short-form with multibase, recommended)
val config2 = PeerDidConfig . numalgo2 ()
SPI Auto-Discovery
When the module is on the classpath, did:peer is automatically available:
1
2
3
4
5
6
7
8
9
10
import org.trustweave.did.*
import java.util.ServiceLoader
// Discover did:peer provider
val providers = ServiceLoader . load ( DidMethodProvider :: class . java )
val peerProvider = providers . find { it . supportedMethods . contains ( "peer" ) }
// Create method
val options = DidCreationOptions ()
val method = peerProvider ?. create ( "peer" , options )
Usage Examples
Creating a did:peer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
val config = PeerDidConfig . numalgo2 ()
val kms = InMemoryKeyManagementService ()
val method = PeerDidMethod ( kms , config )
// Create DID
val options = didCreationOptions {
algorithm = KeyAlgorithm . ED25519
purpose ( KeyPurpose . AUTHENTICATION )
purpose ( KeyPurpose . ASSERTION )
property ( "serviceEndpoint" , "https://example.com/didcomm" )
}
val document = method . createDid ( options )
println ( "Created: ${document.id}" ) // did:peer:2...
Resolving a did:peer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import org.trustweave.did.identifiers.Did
import org.trustweave.did.resolver.DidResolutionResult
val did = Did ( "did:peer:2..." )
val result = method . resolveDid ( did )
when ( result ) {
is DidResolutionResult . Success -> {
println ( "Resolved: ${result.document.id}" )
println ( "Verification methods: ${result.document.verificationMethod.size}" )
}
is DidResolutionResult . Failure . NotFound -> {
println ( "DID not found: ${result.did.value}" )
}
else -> println ( "Resolution failed" )
}
Updating a did:peer
1
2
3
4
5
6
7
8
9
10
11
12
import org.trustweave.did.identifiers.Did
val did = Did ( "did:peer:2..." )
val document = method . updateDid ( did ) { currentDoc ->
currentDoc . copy (
service = currentDoc . service + Service (
id = "${currentDoc.id}#didcomm" ,
type = "DIDCommMessaging" ,
serviceEndpoint = "https://example.com/didcomm"
)
)
}
Deactivating a did:peer
1
2
3
4
5
import org.trustweave.did.identifiers.Did
val did = Did ( "did:peer:2..." )
val deactivated = method . deactivateDid ( did )
println ( "Deactivated: $deactivated" )
Numalgo 0 (Static Numeric)
Numalgo Versions
Numalgo 0 : Static numeric algorithm (legacy)
Numalgo 1 : Short-form with inception key
Numalgo 2 : Short-form with multibase encoding (recommended)
Local Storage
Peer DIDs don’t use external registries or blockchains:
Documents are stored locally in memory or persistent storage
No external dependencies required
Fast resolution from local cache
No network calls needed
Embedded Documents
Long-form peer DIDs can embed documents:
Documents can be encoded in the DID itself
Useful for offline or P2P scenarios
No external resolution needed
Configuration Options
PeerDidConfig
1
2
3
4
val config = PeerDidConfig . builder ()
. numalgo ( 2 ) // Numalgo version (0, 1, or 2)
. includeServices ( true ) // Include service endpoints
. build ()
Integration with TrustWeave
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
import org.trustweave.trust.TrustWeave
import org.trustweave.trust.types.getOrThrowDid
import org.trustweave.did.KeyAlgorithm
import org.trustweave.did.resolver.DidResolutionResult
import org.trustweave.kms.InMemoryKeyManagementService
import org.trustweave.peerdid.*
val config = PeerDidConfig . numalgo2 ()
val kms = InMemoryKeyManagementService ()
val trustWeave = TrustWeave . build {
customKms ( kms )
did {
method ( "peer" ) {
algorithm ( "Ed25519" )
option ( "peerConfig" , config )
}
}
}
val did = trustWeave . createDid {
method ( "peer" )
algorithm ( KeyAlgorithm . ED25519 )
}. getOrThrowDid ()
when ( val resolved = trustWeave . resolveDid ( did )) {
is DidResolutionResult . Success -> println ( "Resolved: ${resolved.document.id}" )
else -> println ( "Resolve failed: $resolved" )
}
Error Handling
Common errors and solutions:
Error
Cause
Solution
Unsupported numalgo
Invalid numalgo version
Use 0, 1, or 2
DID document not found
Document not stored locally
Create DID first
Public key multibase required
Missing multibase key
Ensure key has multibase format
Testing
Peer DIDs are ideal for testing since they don’t require external services:
1
2
3
4
5
6
val config = PeerDidConfig . numalgo2 ()
val method = PeerDidMethod ( kms , config )
// Create and resolve (stored locally)
val document = method . createDid ( options )
val result = method . resolveDid ( document . id )
Best Practices
Use numalgo 2 : Recommended for new implementations
Local storage : Consider persistent storage for peer DIDs
Service endpoints : Include service endpoints for P2P communication
Key management : Securely store keys for peer DIDs
Document sharing : Share documents explicitly in P2P scenarios
Advantages
No external dependencies : No blockchain or HTTP needed
Fast resolution : Local storage provides instant resolution
P2P ready : Designed for peer-to-peer communication
Privacy : No external registry tracks DIDs
Offline support : Works without network connectivity
Use Cases
P2P messaging : Direct communication between peers
Offline scenarios : No external services required
Privacy-sensitive : No external registry tracking
Testing : Fast, local-only DIDs for testing
Temporary DIDs : Short-lived identifiers
Next Steps
References
Peer DID Method Specification](https://identity.foundation/peer-did-method-spec/)
DID Core Specification](https://www.w3.org/TR/did-core/)
TrustWeave Core API](../api-reference/core-api.md)