Create and authenticate DID given the string

Hi guys,
How do I create an instance of DID and authenticate it if I have the did string (e.g: did:key:z6Mkf6nQra6iJRC1p345uXfgnPUwm6Jgxj4yFrZ6DPxGwzcp)

this code from the documentation works, but how do I authenticate it? Thank you

// `seed` must be a 32-byte long Uint8Array
import { DID } from 'dids'
import { getResolver } from 'key-did-resolver'

// See https://github.com/decentralized-identity/did-resolver
const did = new DID({ resolver: getResolver() })

// Resolve a DID document
await did.resolve('did:key:...')

Your code shows an example of how to resolve a DID to it’s DID document. Sounds like you want to authenticate a DID for use with Ceramic?

If you want to use did:pkh to autenticate with a users wallet use this guide:

If you just want to use a did:key directly you can use this guide:

2 Likes

Thank you for helping me

I’m new to this kind of stuff. Could you suggest which library or function I should use for this line? I checked the getAuthMethod , but the first parameter type is ‘any,’ so I have no direction on where to look. I’m using node-express environment. Thanks again!

// import/get your web3 eth provider
const ethProvider =...
import { DIDSession } from 'did-session'
import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'

const ethProvider = // import/get your web3 eth provider
const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
const accountId = await getAccountId(ethProvider, addresses[0])
const authMethod = await EthereumWebAuth.getAuthMethod(ethprovider, accountId)

const session = await DIDSession.get(accountId, authMethod, { resources: [...]})

Hello @Rizky !

I was recently messing around with putting together a simple library POC that authors Ceramic documents with did:key, so this should be helpful for you: point-demo-ts/src/points.ts at main · mzkrasner/point-demo-ts · GitHub

So for you, something like the following:

const key = fromString("<your private seed string>", "base16");
const provider = new Ed25519Provider(key) as DIDProvider;
const staticDid = new DID({
      resolver: KeyResolver.getResolver(),
      provider,
    });
const did = staticDid;
await did.authenticate();

//if you're using composedb
const composeClient = new ComposeClient({
  ceramic: <your endpoint>,
  definition: definition as RuntimeCompositeDefinition,
});
composeClient.setDID(did);
1 Like

You might also find our Walletconnect tutorial useful if you’re looking to work with did:pkh and user sessions and use the wallet client provided by web3modal.

In your case, you could simply use window.ethereum for your ethProvider.

check out how lines 75-112 accomplish this in our ComposeDB example app

1 Like

Thank you,
I was so close, and your code gave me the “aha” moment, for some reason, I added an unnecessary call (commented line) :sweat_smile:

    // const seed = crypto.getRandomValues(seedArray);
    const provider = new Ed25519Provider(new Uint8Array(seedArray));
    const did = new DID({ provider, resolver: getResolver() });
    await did.authenticate();
1 Like