Skip to main content

Use an external DID in Web5

Although Web5 offers everything you need to create your own did, you may want to use an externally-created did in your application instead. For example, you could use a module like ssi-service to generate your own did outside of your Web5 application and then bring it over to use in your app.

Using Web5, you’d normally create a did and then save it using the following code:

import {Web5} from '@tbd54566975/web5'

const web5 = new Web5();
const myDid = await web5.did.create('ion');

// Save your newly created DID in your DWN
await web5.did.manager.set(myDid.id, {
connected: true,
endpoint: 'app://dwn', //this points to the user's local DWN
keys: {
['#dwn']: {
keyPair: myDid.keys.find(key => key.id === 'dwn').keyPair,
},
},
});

In order to bring an external DID into your Web5 project, simply replace the myDid object you created using Web5 with the did data of the did you’d like to import. The structure of myDid is as follows:

{
"id":"did:ion:EiD5iTx5H9P_LKxfKkAx4...",
"internalId":"did:ion:EiD5iTx5H9P_LKxfKkAx4J5oRIAsko9RSsbOoL2sURMsSg",
...
"keys":[
{
"id":"key-1",
"type":"JsonWebKey2020",
"keypair":{
"publicJwk":{
"kty":"EC",
"crv":"secp256k1",
"x":"_fhttpkgTd-icwBqoHoWrzIsULDLoM02Mv2ewFEl9_k",
"y":"wFNaCJ27Yu7Yh4yCgYxWHI2tcWsSZ7ugnc0UYeKn2uM"
},
"privateJwk":{
"kty":"EC",
"crv":"secp256k1",
"x":"_fhttpkgTd-icwBqoHoWrzIsULDLoM02Mv2ewFEl9_k",
"y":"wFNaCJ27Yu7Yh4yCgYxWHI2tcWsSZ7ugnc0UYeKn2uM",
"d":"dhQUKGtPgiVs0WOgY0frP8_5WR-tTgNbB1UI93h2iTw"
}
},
"purposes":[
"authentication"
]
}
],
...
}