npm package: @jccdex/did
Description: DID for wodecards
Author: JCCDex
Latest version: 0.2.5

# DID

Simple Profile Did文档解析.

## Install

```shell
yarn add @jccdex/did @jccdex/ipfs-rpc-client
```

### Usage in DAPP

```javascript
import { 
  EthrDid, 
  EthrDidPublish, 
  BaseNftVC, 
  EthrDidResolver, 
  EthrDidDocument, 
  SwtcDid, 
  SwtcDidPublish,
  SwtcDidResolver, 
  SwtcDidDocument,
  DidService,
  VC_TYPE,
  ETHER_NFTOWNERSHIP_CONTEXT,
  SWTC_NFTOWNERSHIP_CONTEXT
} from "@jccdex/did";
import { IpfsClient } from "@jccdex/ipfs-rpc-client";

const ethereum = window.ethereum;

// CCDAOConnector授权以太坊地址
const account = "";
// CCDAOConnector授权swtc地址
const swtcAccount = "";

const client = new IpfsClient({
  baseURL: "https://wodecards.wh.jccdex.cn:8550",
  sign: async (data, address) => {
    const sign = await ethereum.request({
      method: 'ipfs_personalSign',
      params: [data, address],
    });
    return sign;
  },
  getPublicKey: async (address) => {
    const pub = await ethereum.request({
      method: 'ipfs_getPublicKey',
      params: [address]
    });
    return pub;
  }
});

// 创建did
const did = EthrDid.fromIdentifier(account);

const id = `${did.toString()}#key-1`;

// 创建NFT VC
const vc = new BaseNftVC({
  sign: async (data) => {
    return await ethereum.request({
      method: 'did_issueCredential',
      params: [data],
    });
  }
});

vc.addType(VC_TYPE.NFT_OWNERSHIP);
vc.addContext(ETHER_NFTOWNERSHIP_CONTEXT);
vc.setId("")
vc.setSubject({
    id: did.toString(),
    contractAddress: "0xED5AF388653567Af2F388E6224dC7C4b3241C544",
    chainId: 1,
    tokenId: "443",
    owner: "0xAWalletAddress",
    status: "Active"
})

// vc签名
await vc.sign({
    keyDoc: {
        address: account,
        did: did.toString(),
        // 可选, 默认值 did.toString() + "#keys-1"
        id
    }
});

// vc验证签名
const resolver = new EthrDidResolver(client);
const verified = await EthrNftVC.fromJSON(vc.toJSON()).verify({ resolver });


// 创建simple profile DID文档
const didDoc = new EthrDidDocument(did.toString());
const profile = DidService.generateProfile({
    id: did.toString() + "#profile",
    nickname: "张三",
    preferredAvatar: "https://example.com/avatar.png"
});

// 现在ipfs service的ipns固定值
const ipns = "ipns://k2k4r8ntjlp1cmgped39eq1fi4yze6fsr8og1kcmjhamgs3ubwkfldei"

// 获取did文档cid, 如果是原始文件, 则不用获取, 为空字符串.
const { cid } = await resolver.stat(did.toString());

const ipfsStorage = DidService.generateIpfsStorage({
    id: did.toString() + "#ipfs-storage",
    ipns,
    previousCid: cid
});

// 从插件获取base58公钥及加密类型
const { publicKeyBase58, type } = await ethereum.request({
    method: 'did_getBase58PublicKey',
    params: [account],
});

didDoc.setVersion("1.0.0")
    .addAuthentication(id)
    .addAssertionMethod(id)
    .addVerificationMethod({
        id,
        type,
        controller: did.toString(),
        publicKeyBase58
    })
    .addService(profile)
    .addService(ipfsStorage)
    .addCredential(vc.toJSON());
    .setUpdated();

// 将did doc上传到ipfs service
const didPublisher = new EthrDidPublish(client);
const res = await didPublisher.upload(did.toString(), didDoc.toJSON(), account);
console.log("did publish result: ", res);

// 创建swtc did
const swtcDid = SwtcDid.fromIdentifier(swtcAccount);

const swtcId = `${swtcDid.toString()}#key-1`;

// 创建SWTC NFT VC
const swtcVc = new BaseNftVC({
  sign: async (data) => {
    return await ethereum.request({
      method: 'did_issueCredential',
      params: [data],
    });
  }
});
swtcVc.setId("")
swtcVC.addContext(SWTC_NFTOWNERSHIP_CONTEXT);
swtcVC.addType(VC_TYPE.NFT_OWNERSHIP)
swtcVc.setSubject({
    id: swtcDid.toString(),
    chainId: 315,
    tokenName: "Golden Sands",
    tokenId: "2",
    owner: "swtcWalletAddress",
    status: "Active"
})

// swtc vc签名
await swtcVc.sign({
    keyDoc: {
        address: swtcAccount,
        did: swtcDid.toString(),
        // 可选, 默认值 swtcDid.toString() + "#keys-1"
        id: swtcId
    }
});

// swtc vc验证签名
const swtcResolver = new SwtcDidResolver(client);
const swtcVerified = await SwtcNftVC.fromJSON(swtcVc.toJSON()).verify({ resolver: swtcResolver });

// 创建swtc simple profile DID文档
const swtcDidDoc = new SwtcDidDocument(swtcDid.toString());
const swtcProfile = DidService.generateProfile({
    id: swtcDid.toString() + "#profile",
    nickname: "李四",
    preferredAvatar: "https://example.com/swtc-avatar.png"
});


// 获取swtc did文档cid, 如果是原始文件, 则不用获取, 为空字符串.
const { cid: swtcCid } = await swtcResolver.stat(swtcDid.toString());

const swtcIpfsStorage = DidService.generateIpfsStorage({
    id: swtcDid.toString() + "#ipfs-storage",
    ipns,
    previousCid: swtcCid
});


// 从插件获取base58公钥及加密类型
const { publicKeyBase58, type  } = await ethereum.request({
    method: 'did_getBase58PublicKey',
    params: [swtcAccount],
});

swtcDidDoc.setVersion("1.0.0")
    .addAuthentication(swtcId)
    .addAssertionMethod(swtcId)
    .addVerificationMethod({
        id: swtcId,
        type,
        controller: swtcDid.toString(),
        publicKeyBase58
    })
    .addService(swtcProfile)
    .addService(swtcIpfsStorage)
    .addCredential(swtcVc.toJSON())
    .setUpdated();

// 将swtc did doc上传到ipfs service
const swtcDidPublisher = new SwtcDidPublish(client);
const swtcRes = await swtcDidPublisher.upload(swtcDid.toString(), swtcDidDoc.toJSON(), swtcAccount);
console.log("swtc did publish result: ", swtcRes);


```