ipfs-shipyard / js-did-ipid

The IPID DID method implementation in JavaScript
MIT License
21 stars 9 forks source link

Issue adding/resolving DID Docs #22

Open HLK65 opened 4 years ago

HLK65 commented 4 years ago

Hey,

I'm trying to create and add a DID Doc but it seems like it's not being added to the network. Maybe you can help me find the issue.

I got a node.js express server setup with the following code to create a did

var did = require('did-ipid');
...
ipid = did.default(ipfsNode, {lifetime: '24h'}); //IPFS.create({repo: "ipfs-server", pass: "}{.&PWPWPWPWPWPWPW000000"})
...
router.get('/register', function (req, res) { 
    let rsaKeyPair = generateRsaKeyPair(); //generates rsa keys with crypto 
    ipid.create(rsaKeyPair.privateKey, (document) => {
        const publicKey = document.addPublicKey({
            type: 'RsaVerificationKey2018',
            publicKeyHex: rsaKeyPair.publicKey
        }); 

        const authentication = document.addAuthentication(publicKey.id);

        const service = document.addService({
            id: 'hub',
            type: 'HubService',
            serviceEndpoint: 'https://hub.example.com/',
        });
    })
        .then(didDocument => res.send(didDocument))
        .catch(reason => res.status(500).send(reason));
});

This creates and responds the didDoc but when trying to resolve it I don't get anything.

router.get('/resolve', function (req, res) {
    let did = req.query["did"]
    if (did !== undefined && did.length > 0) {
        ipid.resolve(did)
            .then(r => res.send(r))
            .catch(reason => res.status(404).send(reason));
    } else {
        res.status(400).send("Missing did");
    }
});

Register response: {

    "publicKey": [
        {
            "type": "RsaVerificationKey2018",
            "publicKeyHex": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp2wSSx0XuixPRjE9bVjj\nBotpLmgQ4j6QTXjkgEO8db8woXTLXKyv7k0Qlg3/o+/BaCFoqq7eCgYPzuLQtsV4\nmKmUWGs8i783wtx9ChdsvNUJtQDYLvY60tdEredCxzr4+IocQEeAp6oqHiuuQ3mX\nAtEh1909vsootLcNinK74ysOiTKhNeUWGYCKIMtMqUSqSIGP617os+IJVcGbZ1fk\nf9ofrVcQMTW+9LAohs1KYuFuNACRUP2EoQTkM890Mv0hp0kTKfexsKd7bjxfazo0\nDebrtYCwnFJyHaA+AB6+n1eskCGIgmM6KECLKZoLq1CPXn+AHDWc5MHJNKNN2kqt\nuQIDAQAB\n-----END PUBLIC KEY-----\n",
            "id": "did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb#5y3dyogt2to",
            "controller": "did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb"
        }
    ],
    "authentication": [
        "did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb#5y3dyogt2to"
    ],
    "service": [
        {
            "id": "did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb;hub",
            "type": "HubService",
            "serviceEndpoint": "https://hub.example.com/"
        }
    ],
    "@context": "https://w3id.org/did/v1",
    "id": "did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb",
    "created": "2020-06-17T12:19:38.247Z",
    "updated": "2020-06-17T12:19:38.247Z"
}

Response to localhost:3000/did/resolve?did=did:ipid:QmWDmXuHmm2RDhFDrVpL7487MB26NpVMWvS8jv59pARMMb Code 404

{
    "originalError": "Cannot read property 'replace' of undefined",
    "code": "INVALID_DID",
    "name": "InvalidDid"
}

It's the replace on path in line 97 of the ipid index.js

const {
        path
      } = await _classPrivateFieldGet(this, _ipfs).name.resolve(identifier);
      const cidStr = path.replace(/^\/ipfs\//, '');

So it looks like it can't resolve the identifier

Manually resolving the did ipfs.name.resolve returns /ipfs/bafyreihnyf7hwzeo7gzfpxdys5kyz6v3o7pvh7dd3riird3krpfqyxx2lm but ipfs.cat returns Error: this dag node has no content. and the cloudflare gateway unknown node type

So I guess I'm doing something wrong but I can't figure it out. Maybe you can help.

Thanks, Lukas

HLK65 commented 4 years ago

Full file for a better understanding https://pastebin.com/tjyDTCBf

autonome commented 4 years ago

@satazor any ideas? Also filed on https://stackoverflow.com/questions/62430111/issue-adding-resolving-did-docs-with-js-did-ipid

patrickdet commented 4 years ago

I am running into the exact same issue that @HLK65 is describing. The part of await _classPrivateFieldGet(this, _ipfs).name.resolve(identifier); returns undefined in all cases I have tried.

patrickdet commented 4 years ago

So from the ipfs-js docs (https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/NAME.md#ipfsnameresolvevalue-options) it looks like the publish identifier needs to look like /ipns/....

The publish function of the js-did-ipid library calls ipfs.name.publish with a path that starts with /ipfs/... https://github.com/ipfs-shipyard/js-did-ipid/blob/a32bec225db9a6064785995318c40ed9eed39c8a/src/index.js#L68-L70

I might be wrong.

patrickdet commented 4 years ago

I have dug into this some more and it appears to me that the problem happens due to the switch to Async Iterables https://gist.github.com/alanshaw/04b2ddc35a6fff25c040c011ac6acf26#file-migration-md

I will have a look whether I can send a PR with the fix

autonome commented 4 years ago

Ahhhh. Thanks for investigating @patrickdet!

@satazor can you review or suggest a reviewer?

HLK65 commented 4 years ago

Thank you for digging in and fixing @patrickdet ! I'll hopefully find the time to test it tomorrow.

HLK65 commented 4 years ago

It's working, thank you so much!

satazor commented 4 years ago

Reviewed!