PageSourceSearch

https://elastos-wiki.netlify.app/assets/js/fa968b2f.890b00ab.js

js elastos-wiki.netlify.app collected 2026-10-03 10:44:55 UTC 21,938 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[7943],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>u});var i=n(7294);function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,i)}return n}function s(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t){if(null==e)return{};var n,i,o=function(e,t){if(null==e)return{};var n,i,o={},r=Object.keys(e);for(i=0;i<r.length;i++)n=r[i],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(i=0;i<r.length;i++)n=r[i],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var l=i.createContext({}),d=function(e){var t=i.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):s(s({},t),e)),n},c=function(e){var t=d(e.components);return i.createElement(l.Provider,{value:t},e.children)},D="mdxType",h={inlineCode:"code",wrapper:function(e){var t=e.children;return i.createElement(i.Fragment,{},t)}},p=i.forwardRef((function(e,t){var n=e.components,o=e.mdxType,r=e.originalType,l=e.parentName,c=a(e,["components","mdxType","originalType","parentName"]),D=d(n),p=o,u=D["".concat(l,".").concat(p)]||D[p]||h[p]||r;return n?i.createElement(u,s(s({ref:t},c),{},{components:n})):i.createElement(u,s({ref:t},c))}));function u(e,t){var n=arguments,o=t&&t.mdxType;if("string"==typeof e||o){var r=n.length,s=new Array(r);s[0]=p;var a={};for(var l in t)hasOwnProperty.call(t,l)&&(a[l]=t[l]);a.originalType=e,a[D]="string"==typeof e?e:o,s[1]=a;for(var d=2;d<r;d++)s[d]=n[d];return i.createElement.apply(null,s)}return i.createElement.apply(null,n)}p.displayName="MDXCreateElement"},9687:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>l,default:()=>u,frontMatter:()=>a,metadata:()=>d,toc:()=>D});var i=n(7462),o=n(3366),r=(n(7294),n(3905)),s=["components"],a={title:"Access DIDStore"},l=void 0,d={unversionedId:"sdk/did/js/guide/store/access",id:"sdk/did/js/guide/store/access",title:"Access DIDStore",description:"Read the Objects in the DID Store",source:"@site/../docs/7.sdk/did/js/guide/store/access.md",sourceDirName:"7.sdk/did/js/guide/store",slug:"/sdk/did/js/guide/store/access",permalink:"/sdk/did/js/guide/store/access",draft:!1,editUrl:"https://github.com/elastos/Elastos.Wiki/edit/master/website/../docs/7.sdk/did/js/guide/store/access.md",tags:[],version:"current",lastUpdatedBy:"racollette",lastUpdatedAt:1677260880,formattedLastUpdatedAt:"Feb 24, 2023",frontMatter:{title:"Access DIDStore"},sidebar:"sdkSidebar",previous:{title:"Create and Open DIDStore",permalink:"/sdk/did/js/guide/store/create"},next:{title:"Import/Export",permalink:"/sdk/did/js/guide/store/import-export"}},c={},D=[{value:"Read the Objects in the DID Store",id:"read-the-objects-in-the-did-store",level:2},{value:"Example",id:"example",level:4},{value:"Usage",id:"usage",level:4},{value:"Save DID Information in the DID Store",id:"save-did-information-in-the-did-store",level:2},{value:"Example",id:"example-1",level:4},{value:"Usage",id:"usage-1",level:4},{value:"List the Objects in DID Store",id:"list-the-objects-in-did-store",level:2},{value:"Example",id:"example-2",level:4},{value:"Usage",id:"usage-2",level:4},{value:"Select the Objects in the DID Store",id:"select-the-objects-in-the-did-store",level:2},{value:"Example",id:"example-3",level:4},{value:"Usage",id:"usage-3",level:4},{value:"Delete the Objects in the DID Store",id:"delete-the-objects-in-the-did-store",level:2},{value:"Example",id:"example-4",level:4},{value:"Usage",id:"usage-4",level:4}
1,{value:"Check whether there are Objects in the DID Store",id:"check-whether-there-are-objects-in-the-did-store",level:2},{value:"Example",id:"example-5",level:4},{value:"Usage",id:"usage-5",level:4}],h={toc:D},p="wrapper";function u(e){var t=e.components,n=(0,o.Z)(e,s);return(0,r.kt)(p,(0,i.Z)({},h,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h2",{id:"read-the-objects-in-the-did-store"},"Read the Objects in the DID Store"),(0,r.kt)("p",null,"DID Store offers a series of load API for reading objects in the store, which supports the reading of RootIdentity, DIDDocument, and VerifiableCredential."),(0,r.kt)("p",null,"However, for security reasons, instead of being read directly, the private key stored in the store can only be used transparently through the signature or encrypted API."),(0,r.kt)("h4",{id:"example"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n// Load RootIdentity\nlet rootidentity = await store.loadRootIdentity();\nif (rootidentity) {\n    ... ... ...\n}\nlet did = new DID("did:elastos:iXcRhYB38gMt1phi5JXJMjeXL2TL8cg58y");\n// Load DIDDocuemnt\nlet doc = await store.loadDid(did);\n... ... ... ...\nlet id = DIDURL.from("#cred-1", did);\n// Load VerifiableCredential\nlet vc = await store.loadCredential(id);\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage"},"Usage"),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"RootIdentity Object")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async loadRootIdentity(\n    id: string = undefined\n): Promise<RootIdentity>;\n")),(0,r.kt)("p",null,"id is an identifying string of RootIdentity. If this DIDStore has only one RootIdentity, you can use the API without identifying the ID string, such as the example above. Otherwise, the acquisition will fail."),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"DID Document")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async loadDid(\n    didOrString: DID | string\n): Promise<DIDDocument>;\n")),(0,r.kt)("p",null,"The parameter can be the DID object or the identifying string of DID, the name of which is consistent with the file name saved in DIDStore. If the DID content does not exist or the DID document is saved in the wrong format, the acquisition will fail."),(0,r.kt)("p",null,"The DID document is also the DID content saved in the DIDStore."),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Credential Object")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async loadCredential(\n    id: DIDURL | string\n): Promise<VerifiableCredential>\uff1b\n")),(0,r.kt)("p",null,"id is the unique identifier of the credential, so the credential object should be obtained through it. id can be a DIDURL object or an ID character string. If the credential does not exist or the format is incorrect, the acquisition will fail."),(0,r.kt)("admonition",{title:"Disclaimer",type:"caution"},(0,r.kt)("p",{parentName:"admonition"},"There are many ways to get the DID object - the way shown in the example is just one of them, and users can use API according to their own needs. See DID/Create DID and DIDDocument for details.")),(0,r.kt)("h2",{id:"save-did-information-in-the-did-store"},"Save DID Information in the DID Store"),(0,r.kt)("p",null,"DID store provides a series of APIs, mainly including DID document, verifiable credential and private key, for saving DID information to the DID store."),(0,r.kt)("p",null,"Among them, saving the private key is mainly used when DIDDocument adds the key, and the public key in the key pair is added to the document, while the private key is encrypted and saved to the DID store for authorization and entrustment of DID."),(0,r.kt)("h4",{id:"example-1"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n... ... ... ...\nlet did = new DID("did:elastos:iXcRhYB38gMt1phi5JXJMjeXL2TL8cg58y");\nlet doc = await did.resolve();
1\nif (doc)\n    // store DIDDocument\n    await store.storeDid(doc);\n... ... ... ...\nlet storePass = "pwd";\nlet id = DIDURL.from("#key-1", did);\nlet sk = HDKey.deserializeBase58("xprv9s21ZrQH143K4biiQbUq8369meTb1R8KnstYFAKtfwk3vF8uvFd1EC2s49bMQsbdbmdJxUWRkuC48CXPutFfynYFVGnoeq8LJZhfd9QjvUt").serialize();\n//store PrivateKey\nstore.storePrivateKey(id, sk, storePass);\n... ... ... ...\nlet issuer = new Issuer(doc);\nlet vc = issuer.issueFor(did)\n                    .id("#test")\n                    .type("SelfProclaimedCredential")\n                    .properties({"name": "littlefish"})\n                    .seal(storePass);\nif (vc)\n    //store Credential\n    await store.storeCredential(vc);\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage-1"},"Usage"),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Store DID")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async storeDid(\n    doc: DIDDocument\n): Promise<void>;\n")),(0,r.kt)("p",null,"SDK saves the credential object in the DID store."),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Store Credential")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public storePrivateKey(\n    idOrString: DIDURL | string,\n    privateKey: Buffer,\n    storepass: string\n):void;\n")),(0,r.kt)("blockquote",null,(0,r.kt)("p",{parentName:"blockquote"},"Parameter privateKey: 82-bit extended private key")),(0,r.kt)("blockquote",null,(0,r.kt)("p",{parentName:"blockquote"},"Parameter storepass: for the encryption of private keys")),(0,r.kt)("admonition",{type:"tip"},(0,r.kt)("p",{parentName:"admonition"},"There are many ways to obtain DIDDocument, and the way shown in the example is only one of them.")),(0,r.kt)("h2",{id:"list-the-objects-in-did-store"},"List the Objects in DID Store"),(0,r.kt)("p",null,"DID store refers to background storage. By enumerating, users can get to know all DID information stored in DID Store. DID Store offers ways of enumerating RootIdentity, DIDs, and verifiable credentials."),(0,r.kt)("p",null,"For security reasons, the private key cannot be listed."),(0,r.kt)("h4",{id:"example-2"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n... ... ... ...\n//list RootIdentities\nlet ids = await store.listRootIdentities();\nif (ids.length > 0)\n    console.log("there are {} root identities in the DID store.", ids.length);\nelse\n    console.log("there are no root identity in the DID store.");\n... ... ... ...\n//list DIDs\nlet remains = await store.listDids();\nif (remains.length > 0) {\n    console.log("there are {} dids in the DID store.", remains.length);\n    //list Credentials\n    let vcs = await store.listCredentials(remains[0]);\n    if (vcs.length > 0)\n        console.log("did: {} has {} credentials.", remains[0], vcs.length);\n    else\n        console.log("did: {} has no credential.", remains[0]);\n} else {\n    console.log("there are no did in the DID store");\n}\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage-2"},"Usage"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async listRootIdentities(): Promise<RootIdentity[]>;\n")),(0,r.kt)("p",null,"List the RootIdentity saved in the DID Store and return the RootIdentity object in an array."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async listDids(): Promise<DID[]>;\n")),(0,r.kt)("p",null,"List the DID saved in the DID Store and return the DID in an array. To gain more detailed DID content, users can obtain the DID document by loading DID."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async listCredentials(didOrString: DID | string): Promise<DIDURL[]>;\n")),(0,r.kt)("p",null,"List all the credentials of the specified DID and return the ID in an array. A more detailed credential object can be obtained by loading credential."),(0,r.kt)("h2",{id:"select-the-objects-in-the-did-store"},"Select the Objects in the DID Store"),(0,r.kt)("p",null,"With list function, all objects of the same type in the DID store can be listed, and the select function allows users to list DID and verifiable credential objects that match the specified conditions."),(0,r.kt)("h4",{id:"example-3"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n... ... ... ...\n//select DIDs\nawait store.selectDids(new class implements DIDFilter {\n    public select(d: DID): boolean {\n        return d.resolve() ? true : false;\n    }\n});\n... ... ... ...\n//select credentials\nawait store.selectCredentials(did, new class implements DIDFilter {\n    public select(id: DIDURL): boolean {\n        return true;\n    }\n});\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage-3"},"Usage"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"export interface DIDFilter {\n    select(did: DID): boolean;\n}\n\npublic async selectDids(filter: DIDStore.DIDFilter): Promise<DID[]>;\n")),(0,r.kt)("p",null,"Users implement the DIDFilter to provide a method for selecting DID. See \u201cSample\u201d for concrete usage."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"export interface CredentialFilter {\n    select(id: DIDURL): boolean;\n}\npublic async selectCredentials(\n    didOrString: DID | string,\n    filter: DIDStore.CredentialFilter\n): Promise<DIDURL[]>;\n")),(0,r.kt)("p",null,"Users implement CredentialFilter to provide the method of selecting the credentials and returning the qualified Credential Id identifier."),(0,r.kt)("h2",{id:"delete-the-objects-in-the-did-store"},"Delete the Objects in the DID Store"),(0,r.kt)("p",null,"The DID Store provides a method of saving DID objects, so it esentially offers a way of deleting the DID objects that have been saved. RootIdentity, DID, verifiable credential and private key can be deleted first."),(0,r.kt)("h4",{id:"example-4"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n... ... ... ...\nlet ids = await store.listRootIdentities();\nif (ids.length > 0) {\n    //delete RootIdentity\n    if (store.deleteRootIdentity(ids[0].getId()))\n        console.log("delete root identity {} sucessfully.", ids[0].getId());\n    else\n        console.log("delete root identity {} failed.", ids[0].getId());\n}\n... ... ... ...\nlet dids = await store.listDids();\nif (dids.length > 0) {\n    console.log("there are {} dids in the DID store.", dids.length);\n    let vcs = await store.listCredentials(dids[0]);\n    if (vcs.length > 0) {\n        console.log("did: {} has {} credentials.", dids[0], vcs.length);\n        //list Verifiable Credential\n        if (store.deleteCredential(vcs[0]))\n            console.log("delete credential {} successfully.", vcs[0]);\n        else\n            console.log("delete credential {} failed.", vcs[0]);\n    } else {\n        console.log("did: {} has no credential.", dids[0]);\n    }\n\n    //delete DID\n    if (store.deleteDid(dids[0]))\n        console.log("delete did {} successfully.", dids[0]);\n    else\n        console.log("delete did {} failed.", dids[0]);\n} else {\n    console.log("there are no did in the DID store");\n}\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage-4"},"Usage"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public deleteRootIdentity(id: string): boolean;\n")),(0,r.kt)("p",null,"Delete RootIdentity according to the ID string identifier. If there is no such RootIdentity, or the RootIdentity is the only object in DID store and cannot be deleted, then return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public deleteDid(didOrString: DID | string): boolean;\n")),(0,r.kt)("p",null,"According to the DID identifier, delete all c
1ontent related to didOrString stored in DIDStore, e.g. documents, credentials and private keys. If there is no such DID information or an error occurs, then return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public deleteCredential(idOrString: DIDURL | string): boolean;\n")),(0,r.kt)("p",null,"Delete the credential according to the ID identifier. If there is no such DID information or an error occurs, then return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public deletePrivateKey(idOrString: DIDURL | string): boolean;\n")),(0,r.kt)("p",null,"Delete the corresponding private key according to the ID identifier. If there is no information about such private key or an error occurs, then return false."),(0,r.kt)("h2",{id:"check-whether-there-are-objects-in-the-did-store"},"Check whether there are Objects in the DID Store"),(0,r.kt)("p",null,"Sometimes users only need to know whether there is any object stored in the DID Store without having to provide any instance of this object. Therefore, DID Store provides methods to check the existence of RootIdentity, Mnemonic, DID, Verifiable Credential, and Private key."),(0,r.kt)("h4",{id:"example-5"},"Example"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},'let rootPath = "root/store";\nlet store = await DIDStore.open(rootPath);\n//contains RootIdentities\nif (store.containsRootIdentities()) {\n    console.log("there are root identities in the DID store.");\n    let ids = await store.listRootIdentities();\n    let id = ids[0].getId();\n    //contains RootIdentity\n    if (store.containsRootIdentity(id)) {\n        console.log("root identity {} is in the DID store.", id);\n        //contains RootIdentity Mnemonic\n        if (store.containsRootIdentityMnemonic(id)\n             console.log("root identity {} has mnemonic.", id);\n         else\n             console.log("root identity {} doesn\'t have mnemonic.", id);\n    } else {\n        console.log("root identity {} isn\'t in the DID store.", id);\n    }\n} else {\n    console.log("there are no root identity in the DID store.");\n}\n//contains DIDs\nif (store.containsDids()) {\n    console.log("there are DIDs in the DID store.");\n    let dids = await store.listDIDs();\n    let did = dids[0];\n    //contains DID\n    if (store.containsDid(id))\n        console.log("DID {} is in the DID store.", did);\n    else\n        console.log("DID {} isn\'t in the DID store.", did);\n} else {\n    console.log("there are no DID in the DID store.");\n}\n//contains Credentials\nif (store.containsCredentials(did)) {\n    console.log("there are some credentials in the DID store.");\n    let vcs = await store.listCredentials();\n    let vc = vcs[0];\n    //contains Credential\n    if (store.containsCredential(vc))\n        console.log("Credential {} is in the DID store.", vc);\n    else\n        console.log("Credential {} isn\'t in the DID store.", vc);\n} else {\n    console.log("there are no credential in the DID store.");\n}\n... ... ... ...\nstore.close();\n')),(0,r.kt)("h4",{id:"usage-5"},"Usage"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsRootIdentity(id: string): boolean;\n")),(0,r.kt)("p",null,"id is the identifying string of RootIdentity. If there is no such RootIdentity in the DID store or any other error occurs, then return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsRootIdentities(): boolean;\n")),(0,r.kt)("p",null,"Check if there is any RootIdentity in the DID store. If the DID store contains any RootIdentity, then return true; otherwise, return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsRootIdentityMnemonic(id: string): boolean;\n")),(0,r.kt)("p",null,"Check if there is a mnemonic in the specified RootIdentity."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsDid(did: DID | string): boolean;\n")),(0,r.kt)("p",null,"Check if there is a specified DID in the DID store."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsDids(): boolean;\n")),(0,r.kt)("p",null,"Check if there is a DID in the DID Store. If any DID is found in the DID Store, then return true; otherwise, return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public async containsCredential(id: DIDURL | string): Promise<boolean>;\n")),(0,r.kt)("p",null,"Check if there is the credential of the specified ID in the DID Store."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsCredentials(did : DID) : boolean;\n")),(0,r.kt)("p",null,"Check if there is the credential of the specified DID in the DID Store. If any credential is found, then return true; otherwise, return false."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsPrivateKey(id: DIDURL | string): boolean;\n")),(0,r.kt)("p",null,"Check if there is the private key of the specified id in the DID Store."),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"public containsPrivateKeys(did: DID | string): boolean;\n")),(0,r.kt)("p",null,"Check if there is the private key of the specified DID in the DID Store. If any private key if found, then return true; otherwise, return false."))}u.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.