did-sdk-js
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

did-sdk-js

js sdk for did and vc according to mcps did spec

Description

This library is js sdk according to mcps did/vc spec

Usage

DID

creatDID

import * as did from "did-sdk-js"
let regionId = "sxcu"  // region id for the did

let myDid = await did.DidDocument.creatDID(did.AlgoType.AlgoTypeSecp256k1, did.IDGenType.IDGenTypeBech32, regionId)
let myDoc = myDid.document  // document for the did
let mainPrivateInfo = myDid.didPrivateInfo[0]  // main private key for the did
let appPrivateInfo = myDid.didPrivateInfo[1]  // app private key for the did

console.log(myDoc.toString())  // document to string

console.log(mainPrivateInfo.keyId) // key id for the publickey of did. such as "#keys-main"/"#keys-app"
console.log(mainPrivateInfo.algo)  // algo type for the publickey of did. such as sm2/secp256k1
console.log(mainPrivateInfo.privateKeyHex)  // private key for the publickey in hex 
console.log(mainPrivateInfo.mnemonic)  // mnemonic for the private key
console.log(mainPrivateInfo.publicKeyHex)  // publickey in hex

resetPublicKey

import * as did from "did-sdk-js"

let myNewDid = await myDid.resetPublicKey(did.AlgoType.AlgoTypeSm2, did.DIDUrl.fragmentKeyIdApp, mainPrivateInfo)
let myDoc = myNewDid.document  // new document for the did
let newPrivateInfo = myNewDid.privateInfo  // main private key info for the app key of did

console.log(myDoc.toString())  // document to string

revoke

import * as did from "did-sdk-js"

let myRevokeDid = await myNewDid.resetPublicKey(did.AlgoType.AlgoTypeSm2, did.DIDUrl.fragmentKeyIdApp, mainPrivateInfo)

console.log(myRevokeDid.toString())  // document to string

console.log(myRevokeDid.isRevoked())  // true

VC

凭证创建/签发/加密/解密

  
  let issuerDid = await did.DidDocument.creatDID(did.AlgoType.AlgoTypeSecp256k1, did.IDGenType.IDGenTypeBech32, regionId)
  let issuerDoc = issuerDid.document
  let issuerMainPrivateKey = issuerDid.didPrivateInfo[0]
  let holderDid = await did.DidDocument.creatDID(did.AlgoType.AlgoTypeSecp256k1, did.IDGenType.IDGenTypeBech32, regionId)
  let holderDoc = holderDid.document
  let holderAppPrivateKey = holderDid.didPrivateInfo[1]

  // new a real name claim
  let rnc = new did.RealNameClaim()
  rnc.setPublicFamilyName("张")
  rnc.setPrivateGivenName("三")
  rnc.setPrivatePhone("12345678909")

  let validYears = 10 // valid time for the vc
  // create a vc
  let newVC = did.VerifiableCredential.creatVC([issuerDoc.id], [holderDoc.id], [rnc], validYears)
  console.log(newVC.toString())

  // issuer issue the vc
  await newVC.issue(issuerDoc, issuerMainPrivateKey)
  console.log(newVC.toString())

  // issuer encrypt the vc to itself and the holder
  let vcCipher = await newVC.encrypt(did.EncryptLevel.EncryptLevel1, [issuerDoc, holderDoc])
  console.log(vcCipher.toString())
 
  // the holder decrypt the vc
  let vcDecrypt = await vcCipher.decrypt(holderDoc.id, holderAppPrivateKey)
  console.log(vcDecrypt.toString())

自定义声明

 // 按照规范定义凭证类型、公开信息、私有信息等字段
 
 // 实现声明接口方法:集成common/Claim 类
 // 实现以下方法
 type(): string // 声明类型

 newPrivateData(): ClaimPrivateDataBase // 隐私信息对象创建方法

 newPublicData(): ClaimPublicDataBase // 公开信息对象创建方法

 newMetaData(): ClaimMetaBase // 元数据对象创建方法


// 注册声明
// 1:声明类型
// 2:声明的类名称
registerClaimType(type: string, claim: object)  

Readme

Keywords

Package Sidebar

Install

npm i did-sdk-js

Weekly Downloads

2

Version

0.0.5

License

Apache-2.0

Unpacked Size

266 kB

Total Files

93

Last publish

Collaborators

  • mcps