|
| 1 | +package website.tbd.developer.site.java.docs.web5.build.decentralizedidentifiers; |
| 2 | + |
| 3 | +import foundation.identity.did.DIDDocument; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import web5.sdk.crypto.InMemoryKeyManager; |
| 6 | +import web5.sdk.dids.DidResolutionResult; |
| 7 | +import web5.sdk.dids.methods.dht.CreateDidDhtOptions; |
| 8 | +import web5.sdk.dids.methods.dht.DidDht; |
| 9 | +import web5.sdk.dids.methods.jwk.DidJwk; |
| 10 | +import web5.sdk.dids.methods.key.DidKey; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +class HowToCreateDidTest |
| 15 | +{ |
| 16 | + |
| 17 | + @Test |
| 18 | + void createDidDht(){ |
| 19 | + |
| 20 | + // :snippet-start: createDidDhtJava |
| 21 | + // Creates a DID using the DHT method and publishes the DID Document to the DHT |
| 22 | + final DidDht didDht = DidDht.Default.create( |
| 23 | + new InMemoryKeyManager(), |
| 24 | + new CreateDidDhtOptions(null,null,true,null,null)); |
| 25 | + |
| 26 | + // DID String |
| 27 | + final String did = didDht.getUri(); |
| 28 | + |
| 29 | + // DID and its associated data which can be exported and used in different contexts/apps |
| 30 | + final DidResolutionResult portableDid = DidDht.Default.resolve(did,null); |
| 31 | + |
| 32 | + // DID Document |
| 33 | + final DIDDocument didDocument = portableDid.getDidDocument(); |
| 34 | + // :snippet-end: |
| 35 | + |
| 36 | + assertNotNull(did,"DID should not be null"); |
| 37 | + assertTrue(did.startsWith("did:dht"),"Did should start with 'did:dht'"); |
| 38 | + assertEquals(did, didDocument.getId().toString(),"ID of DID Document should match DID"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void createDidJwt() { |
| 43 | + // :snippet-start: createDidJwkJava |
| 44 | + // Creates a DID using the did:jwk method |
| 45 | + final DidJwk didJwk = DidJwk.Companion.create(new InMemoryKeyManager(), null); |
| 46 | + |
| 47 | + // DID and its associated data which can be exported and used in different contexts/apps |
| 48 | + final DidResolutionResult portableDid = didJwk.resolve(); |
| 49 | + |
| 50 | + // DID String |
| 51 | + final String did = didJwk.getUri(); |
| 52 | + |
| 53 | + // DID Document |
| 54 | + final DIDDocument didDocument = portableDid.getDidDocument(); |
| 55 | + // :snippet-end: |
| 56 | + |
| 57 | + assertNotNull(did, "DID should not be null"); |
| 58 | + assertTrue(did.startsWith("did:jwk"), "DID should start with 'did:jwk'"); |
| 59 | + assertNotNull(didDocument, "DID Document should not be null"); |
| 60 | + assertEquals(did, didDocument.getId().toString(),"ID od DID Document should match DID"); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments