ethr-did-resolver
Advanced tools
Comparing version 4.3.2 to 4.3.3
@@ -0,1 +1,8 @@ | ||
## [4.3.3](https://github.com/decentralized-identity/ethr-did-resolver/compare/4.3.2...4.3.3) (2021-04-23) | ||
### Bug Fixes | ||
* strip milliseconds from dateTime strings ([#129](https://github.com/decentralized-identity/ethr-did-resolver/issues/129)) ([3e958af](https://github.com/decentralized-identity/ethr-did-resolver/commit/3e958afc37916aa3f6de3c7e7a8cf41e0718df34)), closes [#126](https://github.com/decentralized-identity/ethr-did-resolver/issues/126) | ||
## [4.3.2](https://github.com/decentralized-identity/ethr-did-resolver/compare/4.3.1...4.3.2) (2021-04-22) | ||
@@ -2,0 +9,0 @@ |
@@ -1,2 +0,2 @@ | ||
var e=require("@ethersproject/basex"),t=require("@ethersproject/bignumber"),r=require("@ethersproject/contracts"),n=require("@ethersproject/providers"),o=require("ethr-did-registry"),i=require("@ethersproject/address"),s=require("@ethersproject/transactions"),c=require("querystring");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var d=a(o);const u=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/;var h,l;exports.verificationMethodTypes=void 0,(h=exports.verificationMethodTypes||(exports.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",h.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",h.Ed25519VerificationKey2018="Ed25519VerificationKey2018",h.RSAVerificationKey2018="RSAVerificationKey2018",h.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(l||(l={}));const f={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},v={Secp256k1VerificationKey2018:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:exports.verificationMethodTypes.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:exports.verificationMethodTypes.RSAVerificationKey2018,Ed25519VerificationKey2018:exports.verificationMethodTypes.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:exports.verificationMethodTypes.X25519KeyAgreementKey2019};function m(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function p(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function g(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:s.computeAddress(r),publicKey:r,network:t}:{address:i.getAddress(r),network:t}}const y={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},b={...y,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var x;function w(e){var o;let i=e.provider||(null==(o=e.web3)?void 0:o.currentProvider);if(!i){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var s;const r=e.chainId?e.chainId:b[e.name||""],o=r?t.BigNumber.from(r).toNumber():r,c=y[e.name||""]?null==(s=e.name)?void 0:s.replace("mainnet","homestead"):"any";i=new n.JsonRpcProvider(e.rpcUrl,o||c)}}return r.ContractFactory.fromSolidity(d.default).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(i)}function k(e){const t={},r=e.chainId||b[e.name||""];return r?(e.name&&(t[e.name]=w(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=w(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=w(e)),t}function D(e){var t;return{...k(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...k(t)}),{})}}exports.Errors=void 0,(x=exports.Errors||(exports.Errors={})).notFound="notFound",x.invalidDid="invalidDid",x.unknownNetwork="unknownNetwork";class P{constructor(e,t,r,n="mainnet",o,i,s="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:c,publicKey:a,network:d}=g(e),u=d||n;if(t)this.contract=t;else{if(!(o||null!=r&&r.provider||i))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=w({name:u,provider:o||(null==r?void 0:r.provider),registry:s,rpcUrl:i})}this.signer=r,this.address=c;let h=u?`${u}:`:"";h in["mainnet:","0x1:"]&&(h=""),this.did=a?`did:ethr:${h}${a}`:`did:ethr:${h}${c}`}getOwner(e,t){try{return Promise.resolve(this.contract.functions.identityOwner(e,{blockTag:t})).then(function(e){return e[0]})}catch(e){return Promise.reject(e)}}attachContract(e){try{const t=this;return Promise.resolve(e||t.getOwner(t.address,"latest")).then(function(e){const r=t.signer?t.signer:t.contract.provider.getSigner(e)||t.contract.signer;return t.contract.connect(r)})}catch(e){return Promise.reject(e)}}changeOwner(e,t={}){try{const r=this,n={gasLimit:123456,gasPrice:1e9,...t};return Promise.resolve(r.attachContract(n.from)).then(function(t){return delete n.from,Promise.resolve(t.functions.changeOwner(r.address,e,n)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}addDelegate(e,t,r,n={}){try{const o=this,i={gasLimit:123456,gasPrice:1e9,...n};return Promise.resolve(o.attachContract(i.from)).then(function(n){delete i.from;const s=p(e);return Promise.resolve(n.functions.addDelegate(o.address,s,t,r,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeDelegate(e,t,r={}){try{const n=this,o={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:p(e),Promise.resolve(n.attachContract(o.from)).then(function(r){return delete o.from,Promise.resolve(r.functions.revokeDelegate(n.address,e,t,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}setAttribute(e,t,r,n={}){try{const o=this,i={gasLimit:123456,gasPrice:1e9,controller:void 0,...n};return e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(o.attachContract(i.from)).then(function(n){return delete i.from,Promise.resolve(n.functions.setAttribute(o.address,e,t,r,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeAttribute(e,t,r={}){try{const n=this,o={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(n.attachContract(o.from)).then(function(r){return delete o.from,Promise.resolve(r.functions.revokeAttribute(n.address,e,t,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}}function I(e,t,r){if(!e.s){if(r instanceof K){if(!r.s)return void(r.o=I.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(I.bind(null,e,t),I.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}const K=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,o=this.s;if(o){const e=1&o?t:r;if(e){try{I(n,1,e(this.v))}catch(e){I(n,2,e)}return n}return this}return this.o=function(e){try{const o=e.v;1&e.s?I(n,1,t?t(o):o):r?I(n,1,r(o)):I(n,2,o)}catch(e){I(n,2,e)}},n},e}();function S(e){return e instanceof K&&1&e.s}class N{constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?D({networks:[{name:"mainnet",chainId:"0x1",provider:new n.InfuraProvider("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new n.InfuraProvider("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new n.InfuraProvider("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new n.InfuraProvider("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new n.InfuraProvider("kovan",r)}]}):{}),...D(e)};var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}getOwner(e,t,r){try{return Promise.resolve(new P(e,this.contracts[t]).getOwner(e,r))}catch(e){return Promise.reject(e)}}previousChange(e,r,n){try{return Promise.resolve(this.contracts[r].functions.changed(e,{blockTag:n})).then(function(e){return t.BigNumber.from(e[0])})}catch(e){return Promise.reject(e)}}getBlockMetadata(e,t){try{return Promise.resolve(this.contracts[t].provider.getBlock(e)).then(function(e){return{height:e.number.toString(),isoDate:new Date(1e3*e.timestamp).toISOString()}})}catch(e){return Promise.reject(e)}}changeLog(e,r,n="latest"){try{const o=this,i=o.contracts[r],s=i.provider,c=r.startsWith("0x")?r:b[r];return Promise.resolve(s.getNetwork()).then(function(a){const d=a.chainId,u=c?t.BigNumber.from(c).toNumber():d,h=[],{address:l,publicKey:f}=g(e),v=f;return Promise.resolve(o.previousChange(l,r,n)).then(function(e){function r(){return{address:l,history:h,controllerKey:v,chainId:u}}const n=function(e,t,r){for(var n;;){var o=e();if(S(o)&&(o=o.v),!o)return i;if(o.then){n=0;break}var i=r();if(i&&i.then){if(!S(i)){n=1;break}i=i.s}}var s=new K,c=I.bind(null,s,2);return(0===n?o.then(d):1===n?i.then(a):(void 0).then(function(){(o=e())?o.then?o.then(d).then(void 0,c):d(o):I(s,1,i)})).then(void 0,c),s;function a(t){i=t;do{if(!(o=e())||S(o)&&!o.v)return void I(s,1,i);if(o.then)return void o.then(d).then(void 0,c);S(i=r())&&(i=i.v)}while(!i||!i.then);i.then(a).then(void 0,c)}function d(e){e?(i=r())&&i.then?i.then(a).then(void 0,c):a(i):I(s,1,i)}}(function(){return!!e},0,function(){const r=e;return Promise.resolve(s.getLogs({address:i.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:e.toHexString(),toBlock:e.toHexString()})).then(function(n){const o=function(e,r){return r.map(r=>function(e,r){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((r,o)=>{let i=e.args[o];"object"==typeof i&&(i=t.BigNumber.from(i)),"bytes32"===r.type&&(i=m(i)),n[r.name]=i}),n._eventName=e.name,n.blockNumber=r,n}(e.interface.parseLog(r),r.blockNumber))}(i,n);o.reverse(),e=null;for(const t of o)h.unshift(t),t.previousChange.lt(r)&&(e=t.previousChange)})});return n&&n.then?n.then(r):r()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(r,n,o,i,s,c,a){var d;const u={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[],assertionMethod:[]};let h=n;const m=[`${r}#controller`];let p=0,g=Number.POSITIVE_INFINITY,y=!1,b=0,x=0;const w={},k={},D={};for(const n of i){if(-1!==c&&n.blockNumber>c){g>n.blockNumber&&(g=n.blockNumber);continue}p<n.blockNumber&&(p=n.blockNumber);const o=n.validTo||t.BigNumber.from(0),i=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(o&&o.gte(a)){if(n._eventName===l.DIDDelegateChanged){const e=n;switch(b++,e.delegateType){case"sigAuth":w[i]=`${r}#delegate-${b}`;case"veriKey":k[i]={id:`${r}#delegate-${b}`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${s}`}}}else if(n._eventName===l.DIDAttributeChanged){const t=n,o=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(o){const n=o[2],s=f[o[4]]||o[4],c=o[6];switch(o[1]){case"pub":{b++;const a={id:`${r}#delegate-${b}`,type:`${n}${s}`,controller:r};switch(a.type=v[a.type]||n,c){case null:case void 0:case"hex":a.publicKeyHex=t.value.slice(2);break;case"base64":a.publicKeyBase64=Buffer.from(t.value.slice(2),"hex").toString("base64");break;case"base58":a.publicKeyBase58=e.Base58.encode(Buffer.from(t.value.slice(2),"hex"));break;case"pem":a.publicKeyPem=Buffer.from(t.value.slice(2),"hex").toString();break;default:a.value=t.value}k[i]=a,"sigAuth"===o[4]&&(w[i]=a.id);break}case"svc":x++,D[i]={id:`${r}#service-${x}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===l.DIDOwnerChanged){if(h=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){y=!0;break}}else n._eventName===l.DIDDelegateChanged||n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?b++:n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&x++,delete w[i],delete k[i],delete D[i]}const P=[{id:`${r}#controller`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${h}@eip155:${s}`}];o&&h==n&&(P.push({id:`${r}#controllerKey`,type:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:o}),m.push(`${r}#controllerKey`));const I={...u,verificationMethod:P.concat(Object.values(k)),authentication:m.concat(Object.values(w))};return Object.values(D).length>0&&(I.service=Object.values(D)),I.assertionMethod=[...(null==(d=I.verificationMethod)?void 0:d.map(e=>e.id))||[]],y?{didDocument:{...u,"@context":"https://www.w3.org/ns/did/v1"},deactivated:y,versionId:p,nextVersionId:g}:{didDocument:I,deactivated:y,versionId:p,nextVersionId:g}}resolve(e,r,n,o){try{const n=this;function i(){return Promise.resolve(n.changeLog(a,d,"latest")).then(function({address:t,history:r,controllerKey:o,chainId:i}){return function(s,c){try{var a=function(){function s(){function e(){return{didDocumentMetadata:{...v,...m,...p},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:c}}const t=function(){if(f!==Number.POSITIVE_INFINITY)return Promise.resolve(n.getBlockMetadata(f,d)).then(function(e){p={nextVersionId:e.height,nextUpdate:e.isoDate}})}();return t&&t.then?t.then(e):e()}const{didDocument:c,deactivated:a,versionId:u,nextVersionId:f}=n.wrapDidDocument(e,t,o,r,i,h,l),v=a?{deactivated:!0}:{};let m={},p={};const g=function(){if(0!==u)return Promise.resolve(n.getBlockMetadata(u,d)).then(function(e){m={versionId:e.height,updated:e.isoDate}})}();return g&&g.then?g.then(s):s()}()}catch(e){return c(e)}return a&&a.then?a.then(void 0,c):a}(0,function(e){return{didResolutionMetadata:{error:exports.Errors.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}})})}const s=r.id.match(u);if(!s)return Promise.resolve({didResolutionMetadata:{error:exports.Errors.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null});const a=s[2],d=s[1]?s[1].slice(0,-1):"mainnet";let h=o.blockTag||"latest";if("string"==typeof r.query){const e=c.decode(r.query);h="string"==typeof e.versionId?e.versionId:h;try{h=Number.parseInt(h)}catch(e){h="latest"}}if(!n.contracts[d])return Promise.resolve({didResolutionMetadata:{error:exports.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${d}`},didDocumentMetadata:{},didDocument:null});let l=t.BigNumber.from(Math.floor((new Date).getTime()/1e3));const f=function(){if("number"==typeof h)return Promise.resolve(n.getBlockMetadata(h,d)).then(function(e){l=t.BigNumber.from(Date.parse(e.isoDate)/1e3)})}();return Promise.resolve(f&&f.then?f.then(i):i())}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}exports.EthrDidController=P,exports.REGISTRY="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",exports.attrTypes=f,exports.bytes32toString=m,exports.delegateTypes=v,exports.getResolver=function(e){return new N(e).build()},exports.identifierMatcher=u,exports.interpretIdentifier=g,exports.stringToBytes32=p; | ||
var e=require("@ethersproject/basex"),t=require("@ethersproject/bignumber"),r=require("@ethersproject/contracts"),n=require("@ethersproject/providers"),o=require("ethr-did-registry"),i=require("@ethersproject/address"),c=require("@ethersproject/transactions"),s=require("querystring");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var d=a(o);const u=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/;var h,l;exports.verificationMethodTypes=void 0,(h=exports.verificationMethodTypes||(exports.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",h.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",h.Ed25519VerificationKey2018="Ed25519VerificationKey2018",h.RSAVerificationKey2018="RSAVerificationKey2018",h.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(l||(l={}));const f={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},v={Secp256k1VerificationKey2018:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:exports.verificationMethodTypes.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:exports.verificationMethodTypes.RSAVerificationKey2018,Ed25519VerificationKey2018:exports.verificationMethodTypes.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:exports.verificationMethodTypes.X25519KeyAgreementKey2019};function m(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function p(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function g(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:c.computeAddress(r),publicKey:r,network:t}:{address:i.getAddress(r),network:t}}const y={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},b={...y,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var x;function w(e){var o;let i=e.provider||(null==(o=e.web3)?void 0:o.currentProvider);if(!i){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var c;const r=e.chainId?e.chainId:b[e.name||""],o=r?t.BigNumber.from(r).toNumber():r,s=y[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";i=new n.JsonRpcProvider(e.rpcUrl,o||s)}}return r.ContractFactory.fromSolidity(d.default).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(i)}function k(e){const t={},r=e.chainId||b[e.name||""];return r?(e.name&&(t[e.name]=w(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=w(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=w(e)),t}function D(e){var t;return{...k(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...k(t)}),{})}}exports.Errors=void 0,(x=exports.Errors||(exports.Errors={})).notFound="notFound",x.invalidDid="invalidDid",x.unknownNetwork="unknownNetwork";class P{constructor(e,t,r,n="mainnet",o,i,c="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:s,publicKey:a,network:d}=g(e),u=d||n;if(t)this.contract=t;else{if(!(o||null!=r&&r.provider||i))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=w({name:u,provider:o||(null==r?void 0:r.provider),registry:c,rpcUrl:i})}this.signer=r,this.address=s;let h=u?`${u}:`:"";h in["mainnet:","0x1:"]&&(h=""),this.did=a?`did:ethr:${h}${a}`:`did:ethr:${h}${s}`}getOwner(e,t){try{return Promise.resolve(this.contract.functions.identityOwner(e,{blockTag:t})).then(function(e){return e[0]})}catch(e){return Promise.reject(e)}}attachContract(e){try{const t=this;return Promise.resolve(e||t.getOwner(t.address,"latest")).then(function(e){const r=t.signer?t.signer:t.contract.provider.getSigner(e)||t.contract.signer;return t.contract.connect(r)})}catch(e){return Promise.reject(e)}}changeOwner(e,t={}){try{const r=this,n={gasLimit:123456,gasPrice:1e9,...t};return Promise.resolve(r.attachContract(n.from)).then(function(t){return delete n.from,Promise.resolve(t.functions.changeOwner(r.address,e,n)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}addDelegate(e,t,r,n={}){try{const o=this,i={gasLimit:123456,gasPrice:1e9,...n};return Promise.resolve(o.attachContract(i.from)).then(function(n){delete i.from;const c=p(e);return Promise.resolve(n.functions.addDelegate(o.address,c,t,r,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeDelegate(e,t,r={}){try{const n=this,o={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:p(e),Promise.resolve(n.attachContract(o.from)).then(function(r){return delete o.from,Promise.resolve(r.functions.revokeDelegate(n.address,e,t,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}setAttribute(e,t,r,n={}){try{const o=this,i={gasLimit:123456,gasPrice:1e9,controller:void 0,...n};return e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(o.attachContract(i.from)).then(function(n){return delete i.from,Promise.resolve(n.functions.setAttribute(o.address,e,t,r,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeAttribute(e,t,r={}){try{const n=this,o={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(n.attachContract(o.from)).then(function(r){return delete o.from,Promise.resolve(r.functions.revokeAttribute(n.address,e,t,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}}function I(e,t,r){if(!e.s){if(r instanceof K){if(!r.s)return void(r.o=I.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(I.bind(null,e,t),I.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}const K=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,o=this.s;if(o){const e=1&o?t:r;if(e){try{I(n,1,e(this.v))}catch(e){I(n,2,e)}return n}return this}return this.o=function(e){try{const o=e.v;1&e.s?I(n,1,t?t(o):o):r?I(n,1,r(o)):I(n,2,o)}catch(e){I(n,2,e)}},n},e}();function S(e){return e instanceof K&&1&e.s}class N{constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?D({networks:[{name:"mainnet",chainId:"0x1",provider:new n.InfuraProvider("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new n.InfuraProvider("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new n.InfuraProvider("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new n.InfuraProvider("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new n.InfuraProvider("kovan",r)}]}):{}),...D(e)};var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}getOwner(e,t,r){try{return Promise.resolve(new P(e,this.contracts[t]).getOwner(e,r))}catch(e){return Promise.reject(e)}}previousChange(e,r,n){try{return Promise.resolve(this.contracts[r].functions.changed(e,{blockTag:n})).then(function(e){return t.BigNumber.from(e[0])})}catch(e){return Promise.reject(e)}}getBlockMetadata(e,t){try{return Promise.resolve(this.contracts[t].provider.getBlock(e)).then(function(e){return{height:e.number.toString(),isoDate:new Date(1e3*e.timestamp).toISOString().replace(".000","")}})}catch(e){return Promise.reject(e)}}changeLog(e,r,n="latest"){try{const o=this,i=o.contracts[r],c=i.provider,s=r.startsWith("0x")?r:b[r];return Promise.resolve(c.getNetwork()).then(function(a){const d=a.chainId,u=s?t.BigNumber.from(s).toNumber():d,h=[],{address:l,publicKey:f}=g(e),v=f;return Promise.resolve(o.previousChange(l,r,n)).then(function(e){function r(){return{address:l,history:h,controllerKey:v,chainId:u}}const n=function(e,t,r){for(var n;;){var o=e();if(S(o)&&(o=o.v),!o)return i;if(o.then){n=0;break}var i=r();if(i&&i.then){if(!S(i)){n=1;break}i=i.s}}var c=new K,s=I.bind(null,c,2);return(0===n?o.then(d):1===n?i.then(a):(void 0).then(function(){(o=e())?o.then?o.then(d).then(void 0,s):d(o):I(c,1,i)})).then(void 0,s),c;function a(t){i=t;do{if(!(o=e())||S(o)&&!o.v)return void I(c,1,i);if(o.then)return void o.then(d).then(void 0,s);S(i=r())&&(i=i.v)}while(!i||!i.then);i.then(a).then(void 0,s)}function d(e){e?(i=r())&&i.then?i.then(a).then(void 0,s):a(i):I(c,1,i)}}(function(){return!!e},0,function(){const r=e;return Promise.resolve(c.getLogs({address:i.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:e.toHexString(),toBlock:e.toHexString()})).then(function(n){const o=function(e,r){return r.map(r=>function(e,r){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((r,o)=>{let i=e.args[o];"object"==typeof i&&(i=t.BigNumber.from(i)),"bytes32"===r.type&&(i=m(i)),n[r.name]=i}),n._eventName=e.name,n.blockNumber=r,n}(e.interface.parseLog(r),r.blockNumber))}(i,n);o.reverse(),e=null;for(const t of o)h.unshift(t),t.previousChange.lt(r)&&(e=t.previousChange)})});return n&&n.then?n.then(r):r()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(r,n,o,i,c,s,a){var d;const u={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[],assertionMethod:[]};let h=n;const m=[`${r}#controller`];let p=0,g=Number.POSITIVE_INFINITY,y=!1,b=0,x=0;const w={},k={},D={};for(const n of i){if(-1!==s&&n.blockNumber>s){g>n.blockNumber&&(g=n.blockNumber);continue}p<n.blockNumber&&(p=n.blockNumber);const o=n.validTo||t.BigNumber.from(0),i=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(o&&o.gte(a)){if(n._eventName===l.DIDDelegateChanged){const e=n;switch(b++,e.delegateType){case"sigAuth":w[i]=`${r}#delegate-${b}`;case"veriKey":k[i]={id:`${r}#delegate-${b}`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${c}`}}}else if(n._eventName===l.DIDAttributeChanged){const t=n,o=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(o){const n=o[2],c=f[o[4]]||o[4],s=o[6];switch(o[1]){case"pub":{b++;const a={id:`${r}#delegate-${b}`,type:`${n}${c}`,controller:r};switch(a.type=v[a.type]||n,s){case null:case void 0:case"hex":a.publicKeyHex=t.value.slice(2);break;case"base64":a.publicKeyBase64=Buffer.from(t.value.slice(2),"hex").toString("base64");break;case"base58":a.publicKeyBase58=e.Base58.encode(Buffer.from(t.value.slice(2),"hex"));break;case"pem":a.publicKeyPem=Buffer.from(t.value.slice(2),"hex").toString();break;default:a.value=t.value}k[i]=a,"sigAuth"===o[4]&&(w[i]=a.id);break}case"svc":x++,D[i]={id:`${r}#service-${x}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===l.DIDOwnerChanged){if(h=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){y=!0;break}}else n._eventName===l.DIDDelegateChanged||n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?b++:n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&x++,delete w[i],delete k[i],delete D[i]}const P=[{id:`${r}#controller`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${h}@eip155:${c}`}];o&&h==n&&(P.push({id:`${r}#controllerKey`,type:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:o}),m.push(`${r}#controllerKey`));const I={...u,verificationMethod:P.concat(Object.values(k)),authentication:m.concat(Object.values(w))};return Object.values(D).length>0&&(I.service=Object.values(D)),I.assertionMethod=[...(null==(d=I.verificationMethod)?void 0:d.map(e=>e.id))||[]],y?{didDocument:{...u,"@context":"https://www.w3.org/ns/did/v1"},deactivated:y,versionId:p,nextVersionId:g}:{didDocument:I,deactivated:y,versionId:p,nextVersionId:g}}resolve(e,r,n,o){try{const n=this;function i(){return Promise.resolve(n.changeLog(a,d,"latest")).then(function({address:t,history:r,controllerKey:o,chainId:i}){return function(c,s){try{var a=function(){function c(){function e(){return{didDocumentMetadata:{...v,...m,...p},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:s}}const t=function(){if(f!==Number.POSITIVE_INFINITY)return Promise.resolve(n.getBlockMetadata(f,d)).then(function(e){p={nextVersionId:e.height,nextUpdate:e.isoDate}})}();return t&&t.then?t.then(e):e()}const{didDocument:s,deactivated:a,versionId:u,nextVersionId:f}=n.wrapDidDocument(e,t,o,r,i,h,l),v=a?{deactivated:!0}:{};let m={},p={};const g=function(){if(0!==u)return Promise.resolve(n.getBlockMetadata(u,d)).then(function(e){m={versionId:e.height,updated:e.isoDate}})}();return g&&g.then?g.then(c):c()}()}catch(e){return s(e)}return a&&a.then?a.then(void 0,s):a}(0,function(e){return{didResolutionMetadata:{error:exports.Errors.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}})})}const c=r.id.match(u);if(!c)return Promise.resolve({didResolutionMetadata:{error:exports.Errors.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null});const a=c[2],d=c[1]?c[1].slice(0,-1):"mainnet";let h=o.blockTag||"latest";if("string"==typeof r.query){const e=s.decode(r.query);h="string"==typeof e.versionId?e.versionId:h;try{h=Number.parseInt(h)}catch(e){h="latest"}}if(!n.contracts[d])return Promise.resolve({didResolutionMetadata:{error:exports.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${d}`},didDocumentMetadata:{},didDocument:null});let l=t.BigNumber.from(Math.floor((new Date).getTime()/1e3));const f=function(){if("number"==typeof h)return Promise.resolve(n.getBlockMetadata(h,d)).then(function(e){l=t.BigNumber.from(Date.parse(e.isoDate)/1e3)})}();return Promise.resolve(f&&f.then?f.then(i):i())}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}exports.EthrDidController=P,exports.REGISTRY="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",exports.attrTypes=f,exports.bytes32toString=m,exports.delegateTypes=v,exports.getResolver=function(e){return new N(e).build()},exports.identifierMatcher=u,exports.interpretIdentifier=g,exports.stringToBytes32=p; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import{Base58 as e}from"@ethersproject/basex";import{BigNumber as t}from"@ethersproject/bignumber";import{ContractFactory as r}from"@ethersproject/contracts";import{JsonRpcProvider as n,InfuraProvider as i}from"@ethersproject/providers";import a from"ethr-did-registry";import{getAddress as o}from"@ethersproject/address";import{computeAddress as c}from"@ethersproject/transactions";import{decode as s}from"querystring";function d(){return(d=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}const l=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/,u="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var h,f;!function(e){e.EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",e.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",e.Ed25519VerificationKey2018="Ed25519VerificationKey2018",e.RSAVerificationKey2018="RSAVerificationKey2018",e.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019"}(h||(h={})),function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(f||(f={}));const m={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},g={Secp256k1VerificationKey2018:h.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:h.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:h.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:h.RSAVerificationKey2018,Ed25519VerificationKey2018:h.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:h.X25519KeyAgreementKey2019};function p(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function v(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function y(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:c(r),publicKey:r,network:t}:{address:o(r),network:t}}const w={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},b=d({},w,{rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"});var k;function D(e){var i;let o=e.provider||(null==(i=e.web3)?void 0:i.currentProvider);if(!o){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var c;const r=e.chainId?e.chainId:b[e.name||""],i=r?t.from(r).toNumber():r,a=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new n(e.rpcUrl,i||a)}}return r.fromSolidity(a).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(o)}function x(e){const t={},r=e.chainId||b[e.name||""];return r?(e.name&&(t[e.name]=D(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=D(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=D(e)),t}function I(e){var t;return d({},x(e),null==(t=e.networks)?void 0:t.reduce((e,t)=>d({},e,x(t)),{}))}!function(e){e.notFound="notFound",e.invalidDid="invalidDid",e.unknownNetwork="unknownNetwork"}(k||(k={}));class K{constructor(e,t,r,n="mainnet",i,a,o="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:c,publicKey:s,network:d}=y(e),l=d||n;if(t)this.contract=t;else{if(!(i||null!=r&&r.provider||a))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=D({name:l,provider:i||(null==r?void 0:r.provider),registry:o,rpcUrl:a})}this.signer=r,this.address=c;let u=l?`${l}:`:"";u in["mainnet:","0x1:"]&&(u=""),this.did=s?`did:ethr:${u}${s}`:`did:ethr:${u}${c}`}async getOwner(e,t){return(await this.contract.functions.identityOwner(e,{blockTag:t}))[0]}async attachContract(e){const t=e?await e:await this.getOwner(this.address,"latest"),r=this.signer?this.signer:this.contract.provider.getSigner(t)||this.contract.signer;return this.contract.connect(r)}async changeOwner(e,t={}){const r=d({gasLimit:123456,gasPrice:1e9},t),n=await this.attachContract(r.from);delete r.from;const i=await n.functions.changeOwner(this.address,e,r);return await i.wait()}async addDelegate(e,t,r,n={}){const i=d({gasLimit:123456,gasPrice:1e9},n),a=await this.attachContract(i.from);delete i.from;const o=v(e),c=await a.functions.addDelegate(this.address,o,t,r,i);return await c.wait()}async revokeDelegate(e,t,r={}){const n=d({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:v(e);const i=await this.attachContract(n.from);delete n.from;const a=await i.functions.revokeDelegate(this.address,e,t,n);return await a.wait()}async setAttribute(e,t,r,n={}){const i=d({gasLimit:123456,gasPrice:1e9,controller:void 0},n);e=e.startsWith("0x")?e:v(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const a=await this.attachContract(i.from);delete i.from;const o=await a.functions.setAttribute(this.address,e,t,r,i);return await o.wait()}async revokeAttribute(e,t,r={}){const n=d({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:v(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const i=await this.attachContract(n.from);delete n.from;const a=await i.functions.revokeAttribute(this.address,e,t,n);return await a.wait()}}function S(e,r){return r.map(r=>function(e,r){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((r,i)=>{let a=e.args[i];"object"==typeof a&&(a=t.from(a)),"bytes32"===r.type&&(a=p(a)),n[r.name]=a}),n._eventName=e.name,n.blockNumber=r,n}(e.interface.parseLog(r),r.blockNumber))}function $(e){return new N(e).build()}class N{constructor(e){this.contracts=function(e={}){const t=d({},(r=e.infuraProjectId)?I({networks:[{name:"mainnet",chainId:"0x1",provider:new i("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new i("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new i("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new i("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new i("kovan",r)}]}):{},I(e));var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}async getOwner(e,t,r){return new K(e,this.contracts[t]).getOwner(e,r)}async previousChange(e,r,n){const i=await this.contracts[r].functions.changed(e,{blockTag:n});return t.from(i[0])}async getBlockMetadata(e,t){const r=await this.contracts[t].provider.getBlock(e);return{height:r.number.toString(),isoDate:new Date(1e3*r.timestamp).toISOString()}}async changeLog(e,r,n="latest"){const i=this.contracts[r],a=i.provider,o=r.startsWith("0x")?r:b[r],c=(await a.getNetwork()).chainId,s=o?t.from(o).toNumber():c,d=[],{address:l,publicKey:u}=y(e),h=u;let f=await this.previousChange(l,r,n);for(;f;){const e=f,t=S(i,await a.getLogs({address:i.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:f.toHexString(),toBlock:f.toHexString()}));t.reverse(),f=null;for(const r of t)d.unshift(r),r.previousChange.lt(e)&&(f=r.previousChange)}return{address:l,history:d,controllerKey:h,chainId:s}}wrapDidDocument(r,n,i,a,o,c,s){var l;const u={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[],assertionMethod:[]};let p=n;const v=[`${r}#controller`];let y=0,w=Number.POSITIVE_INFINITY,b=!1,k=0,D=0;const x={},I={},K={};for(const n of a){if(-1!==c&&n.blockNumber>c){w>n.blockNumber&&(w=n.blockNumber);continue}y<n.blockNumber&&(y=n.blockNumber);const i=n.validTo||t.from(0),a=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(i&&i.gte(s)){if(n._eventName===f.DIDDelegateChanged){const e=n;switch(k++,e.delegateType){case"sigAuth":x[a]=`${r}#delegate-${k}`;case"veriKey":I[a]={id:`${r}#delegate-${k}`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${o}`}}}else if(n._eventName===f.DIDAttributeChanged){const t=n,i=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(i){const n=i[2],o=m[i[4]]||i[4],c=i[6];switch(i[1]){case"pub":{k++;const s={id:`${r}#delegate-${k}`,type:`${n}${o}`,controller:r};switch(s.type=g[s.type]||n,c){case null:case void 0:case"hex":s.publicKeyHex=t.value.slice(2);break;case"base64":s.publicKeyBase64=Buffer.from(t.value.slice(2),"hex").toString("base64");break;case"base58":s.publicKeyBase58=e.encode(Buffer.from(t.value.slice(2),"hex"));break;case"pem":s.publicKeyPem=Buffer.from(t.value.slice(2),"hex").toString();break;default:s.value=t.value}I[a]=s,"sigAuth"===i[4]&&(x[a]=s.id);break}case"svc":D++,K[a]={id:`${r}#service-${D}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===f.DIDOwnerChanged){if(p=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){b=!0;break}}else n._eventName===f.DIDDelegateChanged||n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?k++:n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&D++,delete x[a],delete I[a],delete K[a]}const S=[{id:`${r}#controller`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${p}@eip155:${o}`}];i&&p==n&&(S.push({id:`${r}#controllerKey`,type:h.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:i}),v.push(`${r}#controllerKey`));const $=d({},u,{verificationMethod:S.concat(Object.values(I)),authentication:v.concat(Object.values(x))});return Object.values(K).length>0&&($.service=Object.values(K)),$.assertionMethod=[...(null==(l=$.verificationMethod)?void 0:l.map(e=>e.id))||[]],b?{didDocument:d({},u,{"@context":"https://www.w3.org/ns/did/v1"}),deactivated:b,versionId:y,nextVersionId:w}:{didDocument:$,deactivated:b,versionId:y,nextVersionId:w}}async resolve(e,r,n,i){const a=r.id.match(l);if(!a)return{didResolutionMetadata:{error:k.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null};const o=a[2],c=a[1]?a[1].slice(0,-1):"mainnet";let u=i.blockTag||"latest";if("string"==typeof r.query){const e=s(r.query);u="string"==typeof e.versionId?e.versionId:u;try{u=Number.parseInt(u)}catch(e){u="latest"}}if(!this.contracts[c])return{didResolutionMetadata:{error:k.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${c}`},didDocumentMetadata:{},didDocument:null};let h=t.from(Math.floor((new Date).getTime()/1e3));if("number"==typeof u){const e=await this.getBlockMetadata(u,c);h=t.from(Date.parse(e.isoDate)/1e3)}const{address:f,history:m,controllerKey:g,chainId:p}=await this.changeLog(o,c,"latest");try{const{didDocument:t,deactivated:r,versionId:n,nextVersionId:i}=this.wrapDidDocument(e,f,g,m,p,u,h),a=r?{deactivated:!0}:{};let o={},s={};if(0!==n){const e=await this.getBlockMetadata(n,c);o={versionId:e.height,updated:e.isoDate}}if(i!==Number.POSITIVE_INFINITY){const e=await this.getBlockMetadata(i,c);s={nextVersionId:e.height,nextUpdate:e.isoDate}}return{didDocumentMetadata:d({},a,o,s),didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:t}}catch(e){return{didResolutionMetadata:{error:k.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}}}build(){return{ethr:this.resolve.bind(this)}}}export{k as Errors,K as EthrDidController,u as REGISTRY,m as attrTypes,p as bytes32toString,g as delegateTypes,$ as getResolver,l as identifierMatcher,y as interpretIdentifier,v as stringToBytes32,h as verificationMethodTypes}; | ||
import{Base58 as e}from"@ethersproject/basex";import{BigNumber as t}from"@ethersproject/bignumber";import{ContractFactory as r}from"@ethersproject/contracts";import{JsonRpcProvider as n,InfuraProvider as i}from"@ethersproject/providers";import a from"ethr-did-registry";import{getAddress as o}from"@ethersproject/address";import{computeAddress as c}from"@ethersproject/transactions";import{decode as s}from"querystring";function d(){return(d=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}const l=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/,u="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var h,f;!function(e){e.EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",e.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",e.Ed25519VerificationKey2018="Ed25519VerificationKey2018",e.RSAVerificationKey2018="RSAVerificationKey2018",e.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019"}(h||(h={})),function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(f||(f={}));const m={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},g={Secp256k1VerificationKey2018:h.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:h.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:h.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:h.RSAVerificationKey2018,Ed25519VerificationKey2018:h.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:h.X25519KeyAgreementKey2019};function p(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function v(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function y(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:c(r),publicKey:r,network:t}:{address:o(r),network:t}}const w={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},b=d({},w,{rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"});var k;function D(e){var i;let o=e.provider||(null==(i=e.web3)?void 0:i.currentProvider);if(!o){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var c;const r=e.chainId?e.chainId:b[e.name||""],i=r?t.from(r).toNumber():r,a=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new n(e.rpcUrl,i||a)}}return r.fromSolidity(a).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(o)}function x(e){const t={},r=e.chainId||b[e.name||""];return r?(e.name&&(t[e.name]=D(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=D(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=D(e)),t}function I(e){var t;return d({},x(e),null==(t=e.networks)?void 0:t.reduce((e,t)=>d({},e,x(t)),{}))}!function(e){e.notFound="notFound",e.invalidDid="invalidDid",e.unknownNetwork="unknownNetwork"}(k||(k={}));class K{constructor(e,t,r,n="mainnet",i,a,o="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:c,publicKey:s,network:d}=y(e),l=d||n;if(t)this.contract=t;else{if(!(i||null!=r&&r.provider||a))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=D({name:l,provider:i||(null==r?void 0:r.provider),registry:o,rpcUrl:a})}this.signer=r,this.address=c;let u=l?`${l}:`:"";u in["mainnet:","0x1:"]&&(u=""),this.did=s?`did:ethr:${u}${s}`:`did:ethr:${u}${c}`}async getOwner(e,t){return(await this.contract.functions.identityOwner(e,{blockTag:t}))[0]}async attachContract(e){const t=e?await e:await this.getOwner(this.address,"latest"),r=this.signer?this.signer:this.contract.provider.getSigner(t)||this.contract.signer;return this.contract.connect(r)}async changeOwner(e,t={}){const r=d({gasLimit:123456,gasPrice:1e9},t),n=await this.attachContract(r.from);delete r.from;const i=await n.functions.changeOwner(this.address,e,r);return await i.wait()}async addDelegate(e,t,r,n={}){const i=d({gasLimit:123456,gasPrice:1e9},n),a=await this.attachContract(i.from);delete i.from;const o=v(e),c=await a.functions.addDelegate(this.address,o,t,r,i);return await c.wait()}async revokeDelegate(e,t,r={}){const n=d({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:v(e);const i=await this.attachContract(n.from);delete n.from;const a=await i.functions.revokeDelegate(this.address,e,t,n);return await a.wait()}async setAttribute(e,t,r,n={}){const i=d({gasLimit:123456,gasPrice:1e9,controller:void 0},n);e=e.startsWith("0x")?e:v(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const a=await this.attachContract(i.from);delete i.from;const o=await a.functions.setAttribute(this.address,e,t,r,i);return await o.wait()}async revokeAttribute(e,t,r={}){const n=d({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:v(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const i=await this.attachContract(n.from);delete n.from;const a=await i.functions.revokeAttribute(this.address,e,t,n);return await a.wait()}}function S(e,r){return r.map(r=>function(e,r){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((r,i)=>{let a=e.args[i];"object"==typeof a&&(a=t.from(a)),"bytes32"===r.type&&(a=p(a)),n[r.name]=a}),n._eventName=e.name,n.blockNumber=r,n}(e.interface.parseLog(r),r.blockNumber))}function $(e){return new N(e).build()}class N{constructor(e){this.contracts=function(e={}){const t=d({},(r=e.infuraProjectId)?I({networks:[{name:"mainnet",chainId:"0x1",provider:new i("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new i("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new i("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new i("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new i("kovan",r)}]}):{},I(e));var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}async getOwner(e,t,r){return new K(e,this.contracts[t]).getOwner(e,r)}async previousChange(e,r,n){const i=await this.contracts[r].functions.changed(e,{blockTag:n});return t.from(i[0])}async getBlockMetadata(e,t){const r=await this.contracts[t].provider.getBlock(e);return{height:r.number.toString(),isoDate:new Date(1e3*r.timestamp).toISOString().replace(".000","")}}async changeLog(e,r,n="latest"){const i=this.contracts[r],a=i.provider,o=r.startsWith("0x")?r:b[r],c=(await a.getNetwork()).chainId,s=o?t.from(o).toNumber():c,d=[],{address:l,publicKey:u}=y(e),h=u;let f=await this.previousChange(l,r,n);for(;f;){const e=f,t=S(i,await a.getLogs({address:i.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:f.toHexString(),toBlock:f.toHexString()}));t.reverse(),f=null;for(const r of t)d.unshift(r),r.previousChange.lt(e)&&(f=r.previousChange)}return{address:l,history:d,controllerKey:h,chainId:s}}wrapDidDocument(r,n,i,a,o,c,s){var l;const u={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[],assertionMethod:[]};let p=n;const v=[`${r}#controller`];let y=0,w=Number.POSITIVE_INFINITY,b=!1,k=0,D=0;const x={},I={},K={};for(const n of a){if(-1!==c&&n.blockNumber>c){w>n.blockNumber&&(w=n.blockNumber);continue}y<n.blockNumber&&(y=n.blockNumber);const i=n.validTo||t.from(0),a=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(i&&i.gte(s)){if(n._eventName===f.DIDDelegateChanged){const e=n;switch(k++,e.delegateType){case"sigAuth":x[a]=`${r}#delegate-${k}`;case"veriKey":I[a]={id:`${r}#delegate-${k}`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${o}`}}}else if(n._eventName===f.DIDAttributeChanged){const t=n,i=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(i){const n=i[2],o=m[i[4]]||i[4],c=i[6];switch(i[1]){case"pub":{k++;const s={id:`${r}#delegate-${k}`,type:`${n}${o}`,controller:r};switch(s.type=g[s.type]||n,c){case null:case void 0:case"hex":s.publicKeyHex=t.value.slice(2);break;case"base64":s.publicKeyBase64=Buffer.from(t.value.slice(2),"hex").toString("base64");break;case"base58":s.publicKeyBase58=e.encode(Buffer.from(t.value.slice(2),"hex"));break;case"pem":s.publicKeyPem=Buffer.from(t.value.slice(2),"hex").toString();break;default:s.value=t.value}I[a]=s,"sigAuth"===i[4]&&(x[a]=s.id);break}case"svc":D++,K[a]={id:`${r}#service-${D}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===f.DIDOwnerChanged){if(p=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){b=!0;break}}else n._eventName===f.DIDDelegateChanged||n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?k++:n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&D++,delete x[a],delete I[a],delete K[a]}const S=[{id:`${r}#controller`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${p}@eip155:${o}`}];i&&p==n&&(S.push({id:`${r}#controllerKey`,type:h.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:i}),v.push(`${r}#controllerKey`));const $=d({},u,{verificationMethod:S.concat(Object.values(I)),authentication:v.concat(Object.values(x))});return Object.values(K).length>0&&($.service=Object.values(K)),$.assertionMethod=[...(null==(l=$.verificationMethod)?void 0:l.map(e=>e.id))||[]],b?{didDocument:d({},u,{"@context":"https://www.w3.org/ns/did/v1"}),deactivated:b,versionId:y,nextVersionId:w}:{didDocument:$,deactivated:b,versionId:y,nextVersionId:w}}async resolve(e,r,n,i){const a=r.id.match(l);if(!a)return{didResolutionMetadata:{error:k.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null};const o=a[2],c=a[1]?a[1].slice(0,-1):"mainnet";let u=i.blockTag||"latest";if("string"==typeof r.query){const e=s(r.query);u="string"==typeof e.versionId?e.versionId:u;try{u=Number.parseInt(u)}catch(e){u="latest"}}if(!this.contracts[c])return{didResolutionMetadata:{error:k.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${c}`},didDocumentMetadata:{},didDocument:null};let h=t.from(Math.floor((new Date).getTime()/1e3));if("number"==typeof u){const e=await this.getBlockMetadata(u,c);h=t.from(Date.parse(e.isoDate)/1e3)}const{address:f,history:m,controllerKey:g,chainId:p}=await this.changeLog(o,c,"latest");try{const{didDocument:t,deactivated:r,versionId:n,nextVersionId:i}=this.wrapDidDocument(e,f,g,m,p,u,h),a=r?{deactivated:!0}:{};let o={},s={};if(0!==n){const e=await this.getBlockMetadata(n,c);o={versionId:e.height,updated:e.isoDate}}if(i!==Number.POSITIVE_INFINITY){const e=await this.getBlockMetadata(i,c);s={nextVersionId:e.height,nextUpdate:e.isoDate}}return{didDocumentMetadata:d({},a,o,s),didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:t}}catch(e){return{didResolutionMetadata:{error:k.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}}}build(){return{ethr:this.resolve.bind(this)}}}export{k as Errors,K as EthrDidController,u as REGISTRY,m as attrTypes,p as bytes32toString,g as delegateTypes,$ as getResolver,l as identifierMatcher,y as interpretIdentifier,v as stringToBytes32,h as verificationMethodTypes}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@ethersproject/basex"),require("@ethersproject/bignumber"),require("@ethersproject/contracts"),require("@ethersproject/providers"),require("ethr-did-registry"),require("@ethersproject/address"),require("@ethersproject/transactions"),require("querystring")):"function"==typeof define&&define.amd?define(["exports","@ethersproject/basex","@ethersproject/bignumber","@ethersproject/contracts","@ethersproject/providers","ethr-did-registry","@ethersproject/address","@ethersproject/transactions","querystring"],t):t((e||self).ethrDidResolver={},e.basex,e.bignumber,e.contracts,e.providers,e.ethrDidRegistry,e.address,e.transactions,e.querystring)}(this,function(e,t,r,n,i,o,c,s,a){function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=d(o);const h=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/,l="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var f,v;e.verificationMethodTypes=void 0,(f=e.verificationMethodTypes||(e.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",f.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",f.Ed25519VerificationKey2018="Ed25519VerificationKey2018",f.RSAVerificationKey2018="RSAVerificationKey2018",f.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(v||(v={}));const m={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},g={Secp256k1VerificationKey2018:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:e.verificationMethodTypes.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:e.verificationMethodTypes.RSAVerificationKey2018,Ed25519VerificationKey2018:e.verificationMethodTypes.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:e.verificationMethodTypes.X25519KeyAgreementKey2019};function p(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function y(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function b(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:s.computeAddress(r),publicKey:r,network:t}:{address:c.getAddress(r),network:t}}const w={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},k={...w,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var D;function P(e){var t;let o=e.provider||(null==(t=e.web3)?void 0:t.currentProvider);if(!o){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var c;const t=e.chainId?e.chainId:k[e.name||""],n=t?r.BigNumber.from(t).toNumber():t,s=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new i.JsonRpcProvider(e.rpcUrl,n||s)}}return n.ContractFactory.fromSolidity(u.default).attach(e.registry||l).connect(o)}function x(e){const t={},r=e.chainId||k[e.name||""];return r?(e.name&&(t[e.name]=P(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=P(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=P(e)),t}function I(e){var t;return{...x(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...x(t)}),{})}}e.Errors=void 0,(D=e.Errors||(e.Errors={})).notFound="notFound",D.invalidDid="invalidDid",D.unknownNetwork="unknownNetwork";class K{constructor(e,t,r,n="mainnet",i,o,c=l){const{address:s,publicKey:a,network:d}=b(e),u=d||n;if(t)this.contract=t;else{if(!(i||null!=r&&r.provider||o))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=P({name:u,provider:i||(null==r?void 0:r.provider),registry:c,rpcUrl:o})}this.signer=r,this.address=s;let h=u?`${u}:`:"";h in["mainnet:","0x1:"]&&(h=""),this.did=a?`did:ethr:${h}${a}`:`did:ethr:${h}${s}`}getOwner(e,t){try{return Promise.resolve(this.contract.functions.identityOwner(e,{blockTag:t})).then(function(e){return e[0]})}catch(e){return Promise.reject(e)}}attachContract(e){try{const t=this;return Promise.resolve(e||t.getOwner(t.address,"latest")).then(function(e){const r=t.signer?t.signer:t.contract.provider.getSigner(e)||t.contract.signer;return t.contract.connect(r)})}catch(e){return Promise.reject(e)}}changeOwner(e,t={}){try{const r=this,n={gasLimit:123456,gasPrice:1e9,...t};return Promise.resolve(r.attachContract(n.from)).then(function(t){return delete n.from,Promise.resolve(t.functions.changeOwner(r.address,e,n)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}addDelegate(e,t,r,n={}){try{const i=this,o={gasLimit:123456,gasPrice:1e9,...n};return Promise.resolve(i.attachContract(o.from)).then(function(n){delete o.from;const c=y(e);return Promise.resolve(n.functions.addDelegate(i.address,c,t,r,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeDelegate(e,t,r={}){try{const n=this,i={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:y(e),Promise.resolve(n.attachContract(i.from)).then(function(r){return delete i.from,Promise.resolve(r.functions.revokeDelegate(n.address,e,t,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}setAttribute(e,t,r,n={}){try{const i=this,o={gasLimit:123456,gasPrice:1e9,controller:void 0,...n};return e=e.startsWith("0x")?e:y(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(i.attachContract(o.from)).then(function(n){return delete o.from,Promise.resolve(n.functions.setAttribute(i.address,e,t,r,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeAttribute(e,t,r={}){try{const n=this,i={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:y(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(n.attachContract(i.from)).then(function(r){return delete i.from,Promise.resolve(r.functions.revokeAttribute(n.address,e,t,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}}function S(e,t,r){if(!e.s){if(r instanceof N){if(!r.s)return void(r.o=S.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(S.bind(null,e,t),S.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}const N=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,i=this.s;if(i){const e=1&i?t:r;if(e){try{S(n,1,e(this.v))}catch(e){S(n,2,e)}return n}return this}return this.o=function(e){try{const i=e.v;1&e.s?S(n,1,t?t(i):i):r?S(n,1,r(i)):S(n,2,i)}catch(e){S(n,2,e)}},n},e}();function j(e){return e instanceof N&&1&e.s}class M{constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?I({networks:[{name:"mainnet",chainId:"0x1",provider:new i.InfuraProvider("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new i.InfuraProvider("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new i.InfuraProvider("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new i.InfuraProvider("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new i.InfuraProvider("kovan",r)}]}):{}),...I(e)};var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}getOwner(e,t,r){try{return Promise.resolve(new K(e,this.contracts[t]).getOwner(e,r))}catch(e){return Promise.reject(e)}}previousChange(e,t,n){try{return Promise.resolve(this.contracts[t].functions.changed(e,{blockTag:n})).then(function(e){return r.BigNumber.from(e[0])})}catch(e){return Promise.reject(e)}}getBlockMetadata(e,t){try{return Promise.resolve(this.contracts[t].provider.getBlock(e)).then(function(e){return{height:e.number.toString(),isoDate:new Date(1e3*e.timestamp).toISOString()}})}catch(e){return Promise.reject(e)}}changeLog(e,t,n="latest"){try{const i=this,o=i.contracts[t],c=o.provider,s=t.startsWith("0x")?t:k[t];return Promise.resolve(c.getNetwork()).then(function(a){const d=a.chainId,u=s?r.BigNumber.from(s).toNumber():d,h=[],{address:l,publicKey:f}=b(e),v=f;return Promise.resolve(i.previousChange(l,t,n)).then(function(e){function t(){return{address:l,history:h,controllerKey:v,chainId:u}}const n=function(e,t,r){for(var n;;){var i=e();if(j(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!j(o)){n=1;break}o=o.s}}var c=new N,s=S.bind(null,c,2);return(0===n?i.then(d):1===n?o.then(a):(void 0).then(function(){(i=e())?i.then?i.then(d).then(void 0,s):d(i):S(c,1,o)})).then(void 0,s),c;function a(t){o=t;do{if(!(i=e())||j(i)&&!i.v)return void S(c,1,o);if(i.then)return void i.then(d).then(void 0,s);j(o=r())&&(o=o.v)}while(!o||!o.then);o.then(a).then(void 0,s)}function d(e){e?(o=r())&&o.then?o.then(a).then(void 0,s):a(o):S(c,1,o)}}(function(){return!!e},0,function(){const t=e;return Promise.resolve(c.getLogs({address:o.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:e.toHexString(),toBlock:e.toHexString()})).then(function(n){const i=function(e,t){return t.map(t=>function(e,t){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((t,i)=>{let o=e.args[i];"object"==typeof o&&(o=r.BigNumber.from(o)),"bytes32"===t.type&&(o=p(o)),n[t.name]=o}),n._eventName=e.name,n.blockNumber=t,n}(e.interface.parseLog(t),t.blockNumber))}(o,n);i.reverse(),e=null;for(const r of i)h.unshift(r),r.previousChange.lt(t)&&(e=r.previousChange)})});return n&&n.then?n.then(t):t()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(n,i,o,c,s,a,d){var u;const h={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:n,verificationMethod:[],authentication:[],assertionMethod:[]};let l=i;const f=[`${n}#controller`];let p=0,y=Number.POSITIVE_INFINITY,b=!1,w=0,k=0;const D={},P={},x={};for(const i of c){if(-1!==a&&i.blockNumber>a){y>i.blockNumber&&(y=i.blockNumber);continue}p<i.blockNumber&&(p=i.blockNumber);const o=i.validTo||r.BigNumber.from(0),c=`${i._eventName}-${i.delegateType||i.name}-${i.delegate||i.value}`;if(o&&o.gte(d)){if(i._eventName===v.DIDDelegateChanged){const t=i;switch(w++,t.delegateType){case"sigAuth":D[c]=`${n}#delegate-${w}`;case"veriKey":P[c]={id:`${n}#delegate-${w}`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${t.delegate}@eip155:${s}`}}}else if(i._eventName===v.DIDAttributeChanged){const e=i,r=e.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(r){const i=r[2],o=m[r[4]]||r[4],s=r[6];switch(r[1]){case"pub":{w++;const a={id:`${n}#delegate-${w}`,type:`${i}${o}`,controller:n};switch(a.type=g[a.type]||i,s){case null:case void 0:case"hex":a.publicKeyHex=e.value.slice(2);break;case"base64":a.publicKeyBase64=Buffer.from(e.value.slice(2),"hex").toString("base64");break;case"base58":a.publicKeyBase58=t.Base58.encode(Buffer.from(e.value.slice(2),"hex"));break;case"pem":a.publicKeyPem=Buffer.from(e.value.slice(2),"hex").toString();break;default:a.value=e.value}P[c]=a,"sigAuth"===r[4]&&(D[c]=a.id);break}case"svc":k++,x[c]={id:`${n}#service-${k}`,type:i,serviceEndpoint:Buffer.from(e.value.slice(2),"hex").toString()}}}}}else if(i._eventName===v.DIDOwnerChanged){if(l=i.owner,"0x0000000000000000000000000000000000000000"===i.owner){b=!0;break}}else i._eventName===v.DIDDelegateChanged||i._eventName===v.DIDAttributeChanged&&i.name.match(/^did\/pub\//)?w++:i._eventName===v.DIDAttributeChanged&&i.name.match(/^did\/svc\//)&&k++,delete D[c],delete P[c],delete x[c]}const I=[{id:`${n}#controller`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${l}@eip155:${s}`}];o&&l==i&&(I.push({id:`${n}#controllerKey`,type:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:n,publicKeyHex:o}),f.push(`${n}#controllerKey`));const K={...h,verificationMethod:I.concat(Object.values(P)),authentication:f.concat(Object.values(D))};return Object.values(x).length>0&&(K.service=Object.values(x)),K.assertionMethod=[...(null==(u=K.verificationMethod)?void 0:u.map(e=>e.id))||[]],b?{didDocument:{...h,"@context":"https://www.w3.org/ns/did/v1"},deactivated:b,versionId:p,nextVersionId:y}:{didDocument:K,deactivated:b,versionId:p,nextVersionId:y}}resolve(t,n,i,o){try{const i=this;function c(){return Promise.resolve(i.changeLog(d,u,"latest")).then(function({address:r,history:n,controllerKey:o,chainId:c}){return function(e,s){try{var a=function(){function e(){function e(){return{didDocumentMetadata:{...v,...m,...g},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:s}}const t=function(){if(h!==Number.POSITIVE_INFINITY)return Promise.resolve(i.getBlockMetadata(h,u)).then(function(e){g={nextVersionId:e.height,nextUpdate:e.isoDate}})}();return t&&t.then?t.then(e):e()}const{didDocument:s,deactivated:a,versionId:d,nextVersionId:h}=i.wrapDidDocument(t,r,o,n,c,l,f),v=a?{deactivated:!0}:{};let m={},g={};const p=function(){if(0!==d)return Promise.resolve(i.getBlockMetadata(d,u)).then(function(e){m={versionId:e.height,updated:e.isoDate}})}();return p&&p.then?p.then(e):e()}()}catch(e){return s(e)}return a&&a.then?a.then(void 0,s):a}(0,function(t){return{didResolutionMetadata:{error:e.Errors.notFound,message:t.toString()},didDocumentMetadata:{},didDocument:null}})})}const s=n.id.match(h);if(!s)return Promise.resolve({didResolutionMetadata:{error:e.Errors.invalidDid,message:`Not a valid did:ethr: ${n.id}`},didDocumentMetadata:{},didDocument:null});const d=s[2],u=s[1]?s[1].slice(0,-1):"mainnet";let l=o.blockTag||"latest";if("string"==typeof n.query){const e=a.decode(n.query);l="string"==typeof e.versionId?e.versionId:l;try{l=Number.parseInt(l)}catch(e){l="latest"}}if(!i.contracts[u])return Promise.resolve({didResolutionMetadata:{error:e.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${u}`},didDocumentMetadata:{},didDocument:null});let f=r.BigNumber.from(Math.floor((new Date).getTime()/1e3));const v=function(){if("number"==typeof l)return Promise.resolve(i.getBlockMetadata(l,u)).then(function(e){f=r.BigNumber.from(Date.parse(e.isoDate)/1e3)})}();return Promise.resolve(v&&v.then?v.then(c):c())}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}e.EthrDidController=K,e.REGISTRY=l,e.attrTypes=m,e.bytes32toString=p,e.delegateTypes=g,e.getResolver=function(e){return new M(e).build()},e.identifierMatcher=h,e.interpretIdentifier=b,e.stringToBytes32=y}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@ethersproject/basex"),require("@ethersproject/bignumber"),require("@ethersproject/contracts"),require("@ethersproject/providers"),require("ethr-did-registry"),require("@ethersproject/address"),require("@ethersproject/transactions"),require("querystring")):"function"==typeof define&&define.amd?define(["exports","@ethersproject/basex","@ethersproject/bignumber","@ethersproject/contracts","@ethersproject/providers","ethr-did-registry","@ethersproject/address","@ethersproject/transactions","querystring"],t):t((e||self).ethrDidResolver={},e.basex,e.bignumber,e.contracts,e.providers,e.ethrDidRegistry,e.address,e.transactions,e.querystring)}(this,function(e,t,r,n,i,o,c,s,a){function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=d(o);const h=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/,l="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var f,v;e.verificationMethodTypes=void 0,(f=e.verificationMethodTypes||(e.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",f.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",f.Ed25519VerificationKey2018="Ed25519VerificationKey2018",f.RSAVerificationKey2018="RSAVerificationKey2018",f.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(v||(v={}));const m={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},g={Secp256k1VerificationKey2018:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:e.verificationMethodTypes.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:e.verificationMethodTypes.RSAVerificationKey2018,Ed25519VerificationKey2018:e.verificationMethodTypes.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:e.verificationMethodTypes.X25519KeyAgreementKey2019};function p(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function y(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function b(e){let t,r=e;if(r.startsWith("did:ethr")){r=r.split("?")[0];const e=r.split(":");r=e[e.length-1],e.length>=4&&(t=e.splice(2,e.length-3).join(":"))}return r.length>42?{address:s.computeAddress(r),publicKey:r,network:t}:{address:c.getAddress(r),network:t}}const w={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},k={...w,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var D;function P(e){var t;let o=e.provider||(null==(t=e.web3)?void 0:t.currentProvider);if(!o){if(!e.rpcUrl)throw new Error(`invalid_config: No web3 provider could be determined for network ${e.name||e.chainId}`);{var c;const t=e.chainId?e.chainId:k[e.name||""],n=t?r.BigNumber.from(t).toNumber():t,s=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new i.JsonRpcProvider(e.rpcUrl,n||s)}}return n.ContractFactory.fromSolidity(u.default).attach(e.registry||l).connect(o)}function x(e){const t={},r=e.chainId||k[e.name||""];return r?(e.name&&(t[e.name]=P(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=P(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=P(e)),t}function I(e){var t;return{...x(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...x(t)}),{})}}e.Errors=void 0,(D=e.Errors||(e.Errors={})).notFound="notFound",D.invalidDid="invalidDid",D.unknownNetwork="unknownNetwork";class K{constructor(e,t,r,n="mainnet",i,o,c=l){const{address:s,publicKey:a,network:d}=b(e),u=d||n;if(t)this.contract=t;else{if(!(i||null!=r&&r.provider||o))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=P({name:u,provider:i||(null==r?void 0:r.provider),registry:c,rpcUrl:o})}this.signer=r,this.address=s;let h=u?`${u}:`:"";h in["mainnet:","0x1:"]&&(h=""),this.did=a?`did:ethr:${h}${a}`:`did:ethr:${h}${s}`}getOwner(e,t){try{return Promise.resolve(this.contract.functions.identityOwner(e,{blockTag:t})).then(function(e){return e[0]})}catch(e){return Promise.reject(e)}}attachContract(e){try{const t=this;return Promise.resolve(e||t.getOwner(t.address,"latest")).then(function(e){const r=t.signer?t.signer:t.contract.provider.getSigner(e)||t.contract.signer;return t.contract.connect(r)})}catch(e){return Promise.reject(e)}}changeOwner(e,t={}){try{const r=this,n={gasLimit:123456,gasPrice:1e9,...t};return Promise.resolve(r.attachContract(n.from)).then(function(t){return delete n.from,Promise.resolve(t.functions.changeOwner(r.address,e,n)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}addDelegate(e,t,r,n={}){try{const i=this,o={gasLimit:123456,gasPrice:1e9,...n};return Promise.resolve(i.attachContract(o.from)).then(function(n){delete o.from;const c=y(e);return Promise.resolve(n.functions.addDelegate(i.address,c,t,r,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeDelegate(e,t,r={}){try{const n=this,i={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:y(e),Promise.resolve(n.attachContract(i.from)).then(function(r){return delete i.from,Promise.resolve(r.functions.revokeDelegate(n.address,e,t,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}setAttribute(e,t,r,n={}){try{const i=this,o={gasLimit:123456,gasPrice:1e9,controller:void 0,...n};return e=e.startsWith("0x")?e:y(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(i.attachContract(o.from)).then(function(n){return delete o.from,Promise.resolve(n.functions.setAttribute(i.address,e,t,r,o)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}revokeAttribute(e,t,r={}){try{const n=this,i={gasLimit:123456,gasPrice:1e9,...r};return e=e.startsWith("0x")?e:y(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex"),Promise.resolve(n.attachContract(i.from)).then(function(r){return delete i.from,Promise.resolve(r.functions.revokeAttribute(n.address,e,t,i)).then(function(e){return Promise.resolve(e.wait())})})}catch(e){return Promise.reject(e)}}}function S(e,t,r){if(!e.s){if(r instanceof N){if(!r.s)return void(r.o=S.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(S.bind(null,e,t),S.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}const N=function(){function e(){}return e.prototype.then=function(t,r){const n=new e,i=this.s;if(i){const e=1&i?t:r;if(e){try{S(n,1,e(this.v))}catch(e){S(n,2,e)}return n}return this}return this.o=function(e){try{const i=e.v;1&e.s?S(n,1,t?t(i):i):r?S(n,1,r(i)):S(n,2,i)}catch(e){S(n,2,e)}},n},e}();function j(e){return e instanceof N&&1&e.s}class M{constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?I({networks:[{name:"mainnet",chainId:"0x1",provider:new i.InfuraProvider("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new i.InfuraProvider("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new i.InfuraProvider("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new i.InfuraProvider("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new i.InfuraProvider("kovan",r)}]}):{}),...I(e)};var r;if(0===Object.keys(t).length)throw new Error("invalid_config: Please make sure to have at least one network");return t}(e)}getOwner(e,t,r){try{return Promise.resolve(new K(e,this.contracts[t]).getOwner(e,r))}catch(e){return Promise.reject(e)}}previousChange(e,t,n){try{return Promise.resolve(this.contracts[t].functions.changed(e,{blockTag:n})).then(function(e){return r.BigNumber.from(e[0])})}catch(e){return Promise.reject(e)}}getBlockMetadata(e,t){try{return Promise.resolve(this.contracts[t].provider.getBlock(e)).then(function(e){return{height:e.number.toString(),isoDate:new Date(1e3*e.timestamp).toISOString().replace(".000","")}})}catch(e){return Promise.reject(e)}}changeLog(e,t,n="latest"){try{const i=this,o=i.contracts[t],c=o.provider,s=t.startsWith("0x")?t:k[t];return Promise.resolve(c.getNetwork()).then(function(a){const d=a.chainId,u=s?r.BigNumber.from(s).toNumber():d,h=[],{address:l,publicKey:f}=b(e),v=f;return Promise.resolve(i.previousChange(l,t,n)).then(function(e){function t(){return{address:l,history:h,controllerKey:v,chainId:u}}const n=function(e,t,r){for(var n;;){var i=e();if(j(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!j(o)){n=1;break}o=o.s}}var c=new N,s=S.bind(null,c,2);return(0===n?i.then(d):1===n?o.then(a):(void 0).then(function(){(i=e())?i.then?i.then(d).then(void 0,s):d(i):S(c,1,o)})).then(void 0,s),c;function a(t){o=t;do{if(!(i=e())||j(i)&&!i.v)return void S(c,1,o);if(i.then)return void i.then(d).then(void 0,s);j(o=r())&&(o=o.v)}while(!o||!o.then);o.then(a).then(void 0,s)}function d(e){e?(o=r())&&o.then?o.then(a).then(void 0,s):a(o):S(c,1,o)}}(function(){return!!e},0,function(){const t=e;return Promise.resolve(c.getLogs({address:o.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:e.toHexString(),toBlock:e.toHexString()})).then(function(n){const i=function(e,t){return t.map(t=>function(e,t){const n={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((t,i)=>{let o=e.args[i];"object"==typeof o&&(o=r.BigNumber.from(o)),"bytes32"===t.type&&(o=p(o)),n[t.name]=o}),n._eventName=e.name,n.blockNumber=t,n}(e.interface.parseLog(t),t.blockNumber))}(o,n);i.reverse(),e=null;for(const r of i)h.unshift(r),r.previousChange.lt(t)&&(e=r.previousChange)})});return n&&n.then?n.then(t):t()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(n,i,o,c,s,a,d){var u;const h={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:n,verificationMethod:[],authentication:[],assertionMethod:[]};let l=i;const f=[`${n}#controller`];let p=0,y=Number.POSITIVE_INFINITY,b=!1,w=0,k=0;const D={},P={},x={};for(const i of c){if(-1!==a&&i.blockNumber>a){y>i.blockNumber&&(y=i.blockNumber);continue}p<i.blockNumber&&(p=i.blockNumber);const o=i.validTo||r.BigNumber.from(0),c=`${i._eventName}-${i.delegateType||i.name}-${i.delegate||i.value}`;if(o&&o.gte(d)){if(i._eventName===v.DIDDelegateChanged){const t=i;switch(w++,t.delegateType){case"sigAuth":D[c]=`${n}#delegate-${w}`;case"veriKey":P[c]={id:`${n}#delegate-${w}`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${t.delegate}@eip155:${s}`}}}else if(i._eventName===v.DIDAttributeChanged){const e=i,r=e.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(r){const i=r[2],o=m[r[4]]||r[4],s=r[6];switch(r[1]){case"pub":{w++;const a={id:`${n}#delegate-${w}`,type:`${i}${o}`,controller:n};switch(a.type=g[a.type]||i,s){case null:case void 0:case"hex":a.publicKeyHex=e.value.slice(2);break;case"base64":a.publicKeyBase64=Buffer.from(e.value.slice(2),"hex").toString("base64");break;case"base58":a.publicKeyBase58=t.Base58.encode(Buffer.from(e.value.slice(2),"hex"));break;case"pem":a.publicKeyPem=Buffer.from(e.value.slice(2),"hex").toString();break;default:a.value=e.value}P[c]=a,"sigAuth"===r[4]&&(D[c]=a.id);break}case"svc":k++,x[c]={id:`${n}#service-${k}`,type:i,serviceEndpoint:Buffer.from(e.value.slice(2),"hex").toString()}}}}}else if(i._eventName===v.DIDOwnerChanged){if(l=i.owner,"0x0000000000000000000000000000000000000000"===i.owner){b=!0;break}}else i._eventName===v.DIDDelegateChanged||i._eventName===v.DIDAttributeChanged&&i.name.match(/^did\/pub\//)?w++:i._eventName===v.DIDAttributeChanged&&i.name.match(/^did\/svc\//)&&k++,delete D[c],delete P[c],delete x[c]}const I=[{id:`${n}#controller`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${l}@eip155:${s}`}];o&&l==i&&(I.push({id:`${n}#controllerKey`,type:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:n,publicKeyHex:o}),f.push(`${n}#controllerKey`));const K={...h,verificationMethod:I.concat(Object.values(P)),authentication:f.concat(Object.values(D))};return Object.values(x).length>0&&(K.service=Object.values(x)),K.assertionMethod=[...(null==(u=K.verificationMethod)?void 0:u.map(e=>e.id))||[]],b?{didDocument:{...h,"@context":"https://www.w3.org/ns/did/v1"},deactivated:b,versionId:p,nextVersionId:y}:{didDocument:K,deactivated:b,versionId:p,nextVersionId:y}}resolve(t,n,i,o){try{const i=this;function c(){return Promise.resolve(i.changeLog(d,u,"latest")).then(function({address:r,history:n,controllerKey:o,chainId:c}){return function(e,s){try{var a=function(){function e(){function e(){return{didDocumentMetadata:{...v,...m,...g},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:s}}const t=function(){if(h!==Number.POSITIVE_INFINITY)return Promise.resolve(i.getBlockMetadata(h,u)).then(function(e){g={nextVersionId:e.height,nextUpdate:e.isoDate}})}();return t&&t.then?t.then(e):e()}const{didDocument:s,deactivated:a,versionId:d,nextVersionId:h}=i.wrapDidDocument(t,r,o,n,c,l,f),v=a?{deactivated:!0}:{};let m={},g={};const p=function(){if(0!==d)return Promise.resolve(i.getBlockMetadata(d,u)).then(function(e){m={versionId:e.height,updated:e.isoDate}})}();return p&&p.then?p.then(e):e()}()}catch(e){return s(e)}return a&&a.then?a.then(void 0,s):a}(0,function(t){return{didResolutionMetadata:{error:e.Errors.notFound,message:t.toString()},didDocumentMetadata:{},didDocument:null}})})}const s=n.id.match(h);if(!s)return Promise.resolve({didResolutionMetadata:{error:e.Errors.invalidDid,message:`Not a valid did:ethr: ${n.id}`},didDocumentMetadata:{},didDocument:null});const d=s[2],u=s[1]?s[1].slice(0,-1):"mainnet";let l=o.blockTag||"latest";if("string"==typeof n.query){const e=a.decode(n.query);l="string"==typeof e.versionId?e.versionId:l;try{l=Number.parseInt(l)}catch(e){l="latest"}}if(!i.contracts[u])return Promise.resolve({didResolutionMetadata:{error:e.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${u}`},didDocumentMetadata:{},didDocument:null});let f=r.BigNumber.from(Math.floor((new Date).getTime()/1e3));const v=function(){if("number"==typeof l)return Promise.resolve(i.getBlockMetadata(l,u)).then(function(e){f=r.BigNumber.from(Date.parse(e.isoDate)/1e3)})}();return Promise.resolve(v&&v.then?v.then(c):c())}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}e.EthrDidController=K,e.REGISTRY=l,e.attrTypes=m,e.bytes32toString=p,e.delegateTypes=g,e.getResolver=function(e){return new M(e).build()},e.identifierMatcher=h,e.interpretIdentifier=b,e.stringToBytes32=y}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "ethr-did-resolver", | ||
"version": "4.3.2", | ||
"version": "4.3.3", | ||
"description": "Resolve DID documents around ethereum addresses", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.umd.js", |
214
README.md
@@ -14,3 +14,3 @@ --- | ||
This library is intended to use ethereum addresses as fully self managed | ||
This library is intended to use ethereum addresses or secp256k1 publicKeys as fully self-managed | ||
[Decentralized Identifiers](https://w3c-ccg.github.io/did-spec/#decentralized-identifiers-dids) and wrap them in a | ||
@@ -38,10 +38,11 @@ [DID Document](https://w3c-ccg.github.io/did-spec/#did-documents) | ||
`did:ethr:0x4:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` gets resolved on the rinkeby testnet (chainID=0x4), and | ||
represents a distinct identifier than the generic one. | ||
represents a distinct identifier than the generic one, with different DID documents and different key rotation history. | ||
## DID Document | ||
The did resolver takes the ethereum address, checks for the current controller, looks at contract events and builds a | ||
simple DID document. | ||
The did resolver takes the ethereum address, looks at contract events and builds a DID document based on the ERC1056 Events | ||
corresponding to the address. When an identifier is a full `publicKey`, the corresponding `ethereumAddress` is computed | ||
and checked in the same manner. | ||
The minimal DID document for a an ethereum address `0xb9c5714089478a327f09197987f16f9e5d936e8a` with no transactions to | ||
The minimal DID document for an ethereum address `0xb9c5714089478a327f09197987f16f9e5d936e8a` with no transactions to | ||
the registry looks like this: | ||
@@ -64,198 +65,29 @@ | ||
], | ||
"authentication": ["did:ethr:0xb9c5714089478a327f09197987f16f9e5d936e8a#controller"] | ||
"authentication": ["did:ethr:0xb9c5714089478a327f09197987f16f9e5d936e8a#controller"], | ||
"assertionMethod": ["did:ethr:0xb9c5714089478a327f09197987f16f9e5d936e8a#controller"] | ||
} | ||
``` | ||
Note this uses the `EcdsaSecp256k1RecoveryMethod2020` type and an `blockchainAccountId` to represent the default | ||
verification method and authentication entry. | ||
Note this resolver uses the `EcdsaSecp256k1RecoveryMethod2020` type and an `blockchainAccountId` to represent the default | ||
`verificationMethod`, `assertionMethod`, and `authentication` entry. | ||
Any value from the registry that returns an ethereum address will be added to the `verificationMethod` array of the DID | ||
document with type `EcdsaSecp256k1RecoveryMethod2020` and an `blockchainAccountId` attribute containing the address. | ||
## Building a DID document | ||
The DID document is built by using read only functions and contract events on | ||
The DID document is not stored as a file, but is built by using read only functions and contract events on | ||
the [ethr-did-registry](https://github.com/uport-project/ethr-did-registry) Ethereum smart contract. | ||
Any value from the registry that returns an ethereum address will be added to the `verificationMethod` array of the DID document | ||
with type `EcdsaSecp256k1RecoveryMethod2020` and an `blockchainAccountId` attribute containing the address. | ||
Please see the [spec](doc/did-method-spec.md) for details of how the DID document and corresponding metadata are | ||
computed. | ||
### Controller Address | ||
## Resolving a DID document | ||
Each identity always has a controller address. By default it's the same as the identity address, but check the read only | ||
contract function `identityOwner(address identity)` on the deployed version of the EthrDIDRegistry contract. | ||
The library presents a `resolve()` function that returns a `Promise` returning the DID document. It is not meant to | ||
be used directly but through the [`did-resolver`](https://github.com/decentralized-identity/did-resolver) aggregator. | ||
The Identity controller will always have a `verificationMethod` with the id set as the DID with the fragment `#controller` | ||
appended. | ||
You can use the `getResolver(config)` method to produce an entry that can be used with the `Resolver` | ||
constructor: | ||
An entry is also added to the `authentication` array of the DID document with the id of the controller publicKey. | ||
### Enumerating contract events for an identity | ||
The `EthereumDIDRegistry` contract publishes 3 types of events for each identity. | ||
- `DIDOwnerChanged` (indicating a change of controller) | ||
- `DIDDelegateChanged` | ||
- `DIDAttributeChanged` | ||
If a change has ever been made for an identity the block number is stored in the `changed` mapping. | ||
The latest event can be efficiently looked up by checking for one of the 3 above events at that exact block. | ||
Each event contains a `previousChange` value which contains the block number of the previous change (if any) | ||
To see all changes in history for an identity use the following pseudo code: | ||
1. call `changed(address identity)` contract | ||
2. if result is null return | ||
3. filter for events for all the above types with the contracts address on the specified block | ||
4. if event has a previous change then go to 3 | ||
### Delegate Keys | ||
Delegate Keys are ethereum addresses that can either be general signing keys or optionally also perform authentication. | ||
They are also verifiable from solidity (see [ethr-did-registry](https://github.com/uport-project/ethr-did-registry) for | ||
more info). | ||
A `DIDDelegateChanged` event is published that is used to build a DID. | ||
```solidity | ||
event DIDDelegateChanged( | ||
address indexed identity, | ||
bytes32 delegateType, | ||
address delegate, | ||
uint validTo, | ||
uint previousChange | ||
); | ||
``` | ||
The only 2 delegateTypes that are currently published in the DID Document are: | ||
- `veriKey` Which adds a `EcdsaSecp256k1RecoveryMethod2020` to the `verificationMethod` section of document | ||
- `sigAuth` Which adds a `EcdsaSecp256k1RecoveryMethod2020` to the `verificationMEthod` section of document and then | ||
references it in the 'authentication` section of document. | ||
**Note** The `delegateType` is a `bytes32` type for Ethereum gas efficiency reasons and not a string. This restricts us | ||
to 32 bytes, which is why we use the short hand versions above. | ||
Only events with a `validTo` in seconds greater or equal to current time should be included in the DID document. | ||
### Non Ethereum Attributes | ||
Non ethereum keys, service elements etc can be added using attributes. Attributes only exist on the blockchain as | ||
contract events of type `DIDAttributeChanged` and can thus not be queried from within solidity code. | ||
```solidity | ||
event DIDAttributeChanged( | ||
address indexed identity, | ||
bytes32 name, | ||
bytes value, | ||
uint validTo, | ||
uint previousChange | ||
); | ||
``` | ||
**Note** The `name` is a `bytes32` type for Ethereum gas efficiency reasons and not a string. This restricts us to 32 | ||
bytes, which is why we use the short hand attribute versions below. | ||
While any attribute can be stored. For the DID document we currently support adding to each of these sections of the DID | ||
document: | ||
- [`Public Keys`](https://w3c-ccg.github.io/did-spec/#public-keys) | ||
- [`Service Endpoints`](https://w3c-ccg.github.io/did-spec/#service-endpoints) | ||
### Public Keys | ||
The name of the attribute should follow this format: | ||
`did/pub/(Secp256k1|RSA|Ed25519|X25519)/(veriKey|sigAuth|enc)/(hex|base64|base58)` | ||
(Essentially `did/pub/<key algorithm>/<key purpose>/<encoding>`) | ||
#### Key purposes | ||
- `veriKey` adds the corresponding verification key to the `verificationMethod` section of document | ||
- `sigAuth` adds the corresponding verification key to the `verificationMethod` section of document and adds an entry to | ||
the `authentication` section of document. | ||
- `enc` adds a key agreement key to the `verificationMethod` section. This is used to perform a Diffie-Hellman key | ||
exchange and derive a secret key for encrypting messages to the DID that lists such a key. | ||
> **Note** The `<encoding>` only refers to the key encoding in the resolved DID document. | ||
> Attribute values sent to the ERC1056 registry should always be hex encoded. | ||
#### Example hex encoded Secp256k1 Verification Key | ||
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name | ||
`did/pub/Secp256k1/veriKey/hex` and the value of `0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` | ||
generates a `verificationMethod` entry like this: | ||
```json | ||
{ | ||
"id": "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#delegate-1", | ||
"type": "EcdsaSecp256k1VerificationKey2019", | ||
"controller": "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74", | ||
"publicKeyHex": "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71" | ||
} | ||
``` | ||
#### Base64 encoded Ed25519 Verification Key | ||
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name | ||
`did/pub/Ed25519/veriKey/base64` and the value of | ||
`0xb97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` generates a `verificationMethod` entry like this: | ||
```javascript | ||
{ | ||
id: "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#delegate-1", | ||
type: "Ed25519VerificationKey2018", | ||
controller: "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74", | ||
publicKeyBase64: "uXww3nZ/CEzjCAFo7ikwU7ozsjXXEWoyY9KfFFCTa3E=" | ||
} | ||
``` | ||
#### Base64 encoded X25519 Encryption Key | ||
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name | ||
`did/pub/X25519/enc/base64` and the value of | ||
`0x302a300506032b656e032100118557777ffb078774371a52b00fed75561dcf975e61c47553e664a617661052` | ||
generates a `verificationMethod` entry like this: | ||
```json | ||
{ | ||
"id": "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#delegate-1", | ||
"type": "X25519KeyAgreementKey2019", | ||
"controller": "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74", | ||
"publicKeyBase64": "MCowBQYDK2VuAyEAEYVXd3/7B4d0NxpSsA/tdVYdz5deYcR1U+ZkphdmEFI=" | ||
} | ||
``` | ||
We are looking for people to submit support for `pem`, `base58` and `jwk` key formats as well. | ||
### Service Endpoints | ||
The name of the attribute should follow this format: | ||
`did/svc/[ServiceName]` | ||
Example: | ||
A `DIDAttributeChanged` event for the identity `0xf3beac30c498d9e26865f34fcaa57dbb935b0d74` with the name | ||
`did/svc/HubService` and value of the URL `https://hubs.uport.me` hex encoded as | ||
`0x68747470733a2f2f687562732e75706f72742e6d65` generates a service endpoint entry like the following: | ||
```json | ||
{ | ||
"id": "did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74#service-1", | ||
"type": "HubService", | ||
"serviceEndpoint": "https://hubs.uport.me" | ||
} | ||
``` | ||
## Resolving a DID document | ||
The library presents a `resolver()` function that returns a ES6 Promise returning the DID document. It is not meant to | ||
be used directly but through the | ||
[`did-resolver`](https://github.com/decentralized-identity/did-resolver) aggregator. You can use the `getResolver(conf)` | ||
method to produce an entry that can be used with the `Resolver` | ||
constructor. | ||
```javascript | ||
import { Resolver } from 'did-resolver' | ||
@@ -300,3 +132,3 @@ import { getResolver } from 'ethr-did-resolver' | ||
The configuration from above allows you to resolve ethr-did's of the formats: | ||
The configuration from above allows you to resolve ethr-did's of the following formats: | ||
@@ -307,7 +139,9 @@ - `did:ethr:mainnet:0xabcabc03e98e0dc2b855be647c39abe984193675` | ||
- `did:ethr:rsk:testnet:0xabcabc03e98e0dc2b855be647c39abe984193675` | ||
- `did:ethr:0x1f:0xabcabc03e98e0dc2b855be647c39abe984193675` (refer to the rsk:testnet by chainID) | ||
- `did:ethr:development:0xabcabc03e98e0dc2b855be647c39abe984193675` | ||
- `did:ethr:myprivatenet:0xabcabc03e98e0dc2b855be647c39abe984193675` | ||
- `did:ethr:0x1e240:0xabcabc03e98e0dc2b855be647c39abe984193675` (refer to `myprivatenet` by chainID) | ||
For each network you can specify either an `rpcUrl`, a `provider` or a `web3` instance that can be used to access that | ||
particular network. | ||
particular network. At least one of `name` or `chainId` must be specified per network. | ||
@@ -314,0 +148,0 @@ These providers will have to support `eth_call` and `eth_getLogs` to be able to resolve DIDs specific to that network. |
@@ -19,3 +19,3 @@ import { Resolver } from 'did-resolver' | ||
nextVersionId: '12090175', | ||
nextUpdate: '2021-03-22T18:14:29.000Z', | ||
nextUpdate: '2021-03-22T18:14:29Z', | ||
}, | ||
@@ -22,0 +22,0 @@ didResolutionMetadata: { |
@@ -71,3 +71,3 @@ import { Base58 } from '@ethersproject/basex' | ||
height: block.number.toString(), | ||
isoDate: new Date(block.timestamp * 1000).toISOString(), | ||
isoDate: new Date(block.timestamp * 1000).toISOString().replace('.000', ''), | ||
} | ||
@@ -74,0 +74,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2973
328541
146