ethr-did-resolver
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -0,1 +1,8 @@ | ||
# [4.2.0](https://github.com/decentralized-identity/ethr-did-resolver/compare/4.1.0...4.2.0) (2021-04-16) | ||
### Features | ||
* versioning ([#121](https://github.com/decentralized-identity/ethr-did-resolver/issues/121)) ([b794d69](https://github.com/decentralized-identity/ethr-did-resolver/commit/b794d6975cb92ea5c87882546951d5d0771bde4f)), closes [#119](https://github.com/decentralized-identity/ethr-did-resolver/issues/119) [#118](https://github.com/decentralized-identity/ethr-did-resolver/issues/118) [#119](https://github.com/decentralized-identity/ethr-did-resolver/issues/119) [#118](https://github.com/decentralized-identity/ethr-did-resolver/issues/118) | ||
# [4.1.0](https://github.com/decentralized-identity/ethr-did-resolver/compare/4.0.1...4.1.0) (2021-04-14) | ||
@@ -2,0 +9,0 @@ |
@@ -16,2 +16,3 @@ import { BigNumber } from '@ethersproject/bignumber'; | ||
_eventName: string; | ||
blockNumber: number; | ||
} | ||
@@ -18,0 +19,0 @@ export interface DIDOwnerChanged extends ERC1056Event { |
import { getResolver } from './resolver'; | ||
import { EthrDidController } from './controller'; | ||
import { bytes32toString, DEFAULT_REGISTRY_ADDRESS, identifierMatcher, legacyAlgoMap, legacyAttrTypes, stringToBytes32, verificationMethodTypes, Errors } from './helpers'; | ||
import { bytes32toString, DEFAULT_REGISTRY_ADDRESS, identifierMatcher, interpretIdentifier, legacyAlgoMap, legacyAttrTypes, stringToBytes32, verificationMethodTypes, Errors } from './helpers'; | ||
export { DEFAULT_REGISTRY_ADDRESS as REGISTRY, getResolver, bytes32toString, stringToBytes32, EthrDidController, | ||
@@ -8,2 +8,2 @@ /**@deprecated */ | ||
/**@deprecated */ | ||
legacyAttrTypes as attrTypes, verificationMethodTypes, identifierMatcher, Errors, }; | ||
legacyAttrTypes as attrTypes, verificationMethodTypes, identifierMatcher, interpretIdentifier, Errors, }; |
@@ -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"),c=require("@ethersproject/transactions");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=s(o);const d=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/;var u,h;exports.verificationMethodTypes=void 0,(u=exports.verificationMethodTypes||(exports.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",u.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",u.Ed25519VerificationKey2018="Ed25519VerificationKey2018",u.RSAVerificationKey2018="RSAVerificationKey2018",u.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(h||(h={}));const l={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},f={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 v(e){return("string"==typeof e?Buffer.from(e.slice(2),"hex"):Buffer.from(e)).toString("utf8").replace(/\0+$/,"")}function m(e){const t="0x"+Buffer.from(e).slice(0,32).toString("hex");return t+"0".repeat(66-t.length)}function p(e){let t,r=e;if(r.startsWith("did:ethr")){const e=r.split(":");r=e[e.length-1],4===e.length&&(t=e[2])}return r.length>42?{address:c.computeAddress(r),publicKey:r,network:t}:{address:i.getAddress(r),network:t}}const g={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},y={...g,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var b;function x(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:y[e.name||""],o=r?t.BigNumber.from(r).toNumber():r,s=g[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(a.default).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(i)}function w(e){const t={},r=e.chainId||y[e.name||""];return r?(e.name&&(t[e.name]=x(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=x(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=x(e)),t}function k(e){var t;return{...w(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...w(t)}),{})}}exports.Errors=void 0,(b=exports.Errors||(exports.Errors={})).notFound="notFound",b.invalidDid="invalidDid",b.unknownNetwork="unknownNetwork";class D{constructor(e,t,r,n="mainnet",o,i,c="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:s,publicKey:a,network:d}=p(e),u=d||n;if(t)this.contract=t;else{if(!(o||null!=r&&r.provider))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=x({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=m(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:m(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:m(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:m(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 P(e,t,r){if(!e.s){if(r instanceof K){if(!r.s)return void(r.o=P.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(P.bind(null,e,t),P.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{P(n,1,e(this.v))}catch(e){P(n,2,e)}return n}return this}return this.o=function(e){try{const o=e.v;1&e.s?P(n,1,t?t(o):o):r?P(n,1,r(o)):P(n,2,o)}catch(e){P(n,2,e)}},n},e}();function S(e){return e instanceof K&&1&e.s}class I{constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?k({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)}]}):{}),...k(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 D(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)}}changeLog(e,r,n="latest"){try{const o=this,i=o.contracts[r],c=i.provider,s=r.startsWith("0x")?r:y[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}=p(e);let m=f,g=l;return Promise.resolve(o.previousChange(l,r,n)).then(function(e){function s(){function r(){return{controller:g,history:h,controllerKey:m,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=P.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):P(c,1,i)})).then(void 0,s),c;function a(t){i=t;do{if(!(o=e())||S(o)&&!o.v)return void P(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):P(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){const r={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((n,o)=>{let i=e.args[o];"object"==typeof i&&(i=t.BigNumber.from(i)),"bytes32"===n.type&&(i=v(i)),r[n.name]=i}),r._eventName=e.name,r}(e.interface.parseLog(r)))}(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()}const a=function(){if(e)return Promise.resolve(o.getOwner(l,r,n)).then(function(e){e.toLowerCase()!==g.toLowerCase()&&(m=void 0),g=e})}();return a&&a.then?a.then(s):s()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(r,n,o,i,c){const s={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[]},a=t.BigNumber.from(Math.floor((new Date).getTime()/1e3)),d=[{id:`${r}#controller`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${n}@eip155:${c}`}],u=[`${r}#controller`];o&&(d.push({id:`${r}#controllerKey`,type:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:o}),u.push(`${r}#controllerKey`));let v=!1,m=0,p=0;const g={},y={},b={};for(const n of i){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===h.DIDDelegateChanged){const e=n;switch(m++,e.delegateType){case"sigAuth":g[i]=`${r}#delegate-${m}`;case"veriKey":y[i]={id:`${r}#delegate-${m}`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${c}`}}}else if(n._eventName===h.DIDAttributeChanged){const t=n,o=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(o){const n=o[2],c=l[o[4]]||o[4],s=o[6];switch(o[1]){case"pub":{m++;const a={id:`${r}#delegate-${m}`,type:`${n}${c}`,controller:r};switch(a.type=f[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}y[i]=a,"sigAuth"===o[4]&&(g[i]=a.id);break}case"svc":p++,b[i]={id:`${r}#service-${p}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===h.DIDDelegateChanged||n._eventName===h.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?m++:n._eventName===h.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&p++,delete g[i],delete y[i],delete b[i],n._eventName===h.DIDOwnerChanged&&"0x0000000000000000000000000000000000000000"===n.owner){v=!0;break}}const x={...s,verificationMethod:d.concat(Object.values(y)),authentication:u.concat(Object.values(g))};return Object.values(b).length>0&&(x.service=Object.values(b)),v?{didDocument:{...s,"@context":"https://www.w3.org/ns/did/v1"},deactivated:v}:{didDocument:x,deactivated:v}}resolve(e,t,r,n){try{const r=this,o=t.id.match(d);if(!o)return Promise.resolve({didResolutionMetadata:{error:exports.Errors.invalidDid,message:`Not a valid did:ethr: ${t.id}`},didDocumentMetadata:{},didDocument:null});const i=o[2],c=o[1]?o[1].slice(0,-1):"mainnet";return r.contracts[c]?Promise.resolve(r.changeLog(i,c,n.blockTag)).then(function({controller:t,history:n,controllerKey:o,chainId:i}){try{const{didDocument:c,deactivated:s}=r.wrapDidDocument(e,t,o,n,i);return{didDocumentMetadata:{...s?{deactivated:!0}:{}},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:c}}catch(e){return{didResolutionMetadata:{error:exports.Errors.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}}}):Promise.resolve({didResolutionMetadata:{error:exports.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${c}`},didDocumentMetadata:{},didDocument:null})}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}exports.EthrDidController=D,exports.REGISTRY="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b",exports.attrTypes=l,exports.bytes32toString=v,exports.delegateTypes=f,exports.getResolver=function(e){return new I(e).build()},exports.identifierMatcher=d,exports.stringToBytes32=m; | ||
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")){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))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()}})}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){const d={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[]};let u=n;const h=[`${r}#controller`];let m=0,p=Number.POSITIVE_INFINITY,g=!1,y=0,b=0;const x={},w={},k={};for(const n of i){if(-1!==s&&n.blockNumber>s){p>n.blockNumber&&(p=n.blockNumber);continue}m<n.blockNumber&&(m=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(y++,e.delegateType){case"sigAuth":x[i]=`${r}#delegate-${y}`;case"veriKey":w[i]={id:`${r}#delegate-${y}`,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":{y++;const a={id:`${r}#delegate-${y}`,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}w[i]=a,"sigAuth"===o[4]&&(x[i]=a.id);break}case"svc":b++,k[i]={id:`${r}#service-${b}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===l.DIDOwnerChanged){if(u=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){g=!0;break}}else n._eventName===l.DIDDelegateChanged||n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?y++:n._eventName===l.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&b++,delete x[i],delete w[i],delete k[i]}const D=[{id:`${r}#controller`,type:exports.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${u}@eip155:${c}`}];o&&u==n&&(D.push({id:`${r}#controllerKey`,type:exports.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:o}),h.push(`${r}#controllerKey`));const P={...d,verificationMethod:D.concat(Object.values(w)),authentication:h.concat(Object.values(x))};return Object.values(k).length>0&&(P.service=Object.values(k)),g?{didDocument:{...d,"@context":"https://www.w3.org/ns/did/v1"},deactivated:g,versionId:m,nextVersionId:p}:{didDocument:P,deactivated:g,versionId:m,nextVersionId:p}}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 a}from"@ethersproject/providers";import i from"ethr-did-registry";import{getAddress as o}from"@ethersproject/address";import{computeAddress as c}from"@ethersproject/transactions";function s(){return(s=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 d=/^(.*)?(0x[0-9a-fA-F]{40}|0x[0-9a-fA-F]{66})$/,l="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var u,h;!function(e){e.EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",e.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",e.Ed25519VerificationKey2018="Ed25519VerificationKey2018",e.RSAVerificationKey2018="RSAVerificationKey2018",e.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019"}(u||(u={})),function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(h||(h={}));const f={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},m={Secp256k1VerificationKey2018:u.EcdsaSecp256k1VerificationKey2019,Ed25519SignatureAuthentication2018:u.Ed25519VerificationKey2018,Secp256k1SignatureAuthentication2018:u.EcdsaSecp256k1VerificationKey2019,RSAVerificationKey2018:u.RSAVerificationKey2018,Ed25519VerificationKey2018:u.Ed25519VerificationKey2018,X25519KeyAgreementKey2019:u.X25519KeyAgreementKey2019};function g(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 v(e){let t,r=e;if(r.startsWith("did:ethr")){const e=r.split(":");r=e[e.length-1],4===e.length&&(t=e[2])}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"},y=s({},w,{rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"});var b;function k(e){var a;let o=e.provider||(null==(a=e.web3)?void 0:a.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:y[e.name||""],a=r?t.from(r).toNumber():r,i=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new n(e.rpcUrl,a||i)}}return r.fromSolidity(i).attach(e.registry||"0xdca7ef03e98e0dc2b855be647c39abe984fcf21b").connect(o)}function D(e){const t={},r=e.chainId||y[e.name||""];return r?(e.name&&(t[e.name]=k(e)),t["number"==typeof r?`0x${r.toString(16)}`:r]=k(e)):(e.provider||e.web3||e.rpcUrl)&&(t[e.name||""]=k(e)),t}function x(e){var t;return s({},D(e),null==(t=e.networks)?void 0:t.reduce((e,t)=>s({},e,D(t)),{}))}!function(e){e.notFound="notFound",e.invalidDid="invalidDid",e.unknownNetwork="unknownNetwork"}(b||(b={}));class K{constructor(e,t,r,n="mainnet",a,i,o="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:c,publicKey:s,network:d}=v(e),l=d||n;if(t)this.contract=t;else{if(!(a||null!=r&&r.provider))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=k({name:l,provider:a||(null==r?void 0:r.provider),registry:o,rpcUrl:i})}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=s({gasLimit:123456,gasPrice:1e9},t),n=await this.attachContract(r.from);delete r.from;const a=await n.functions.changeOwner(this.address,e,r);return await a.wait()}async addDelegate(e,t,r,n={}){const a=s({gasLimit:123456,gasPrice:1e9},n),i=await this.attachContract(a.from);delete a.from;const o=p(e),c=await i.functions.addDelegate(this.address,o,t,r,a);return await c.wait()}async revokeDelegate(e,t,r={}){const n=s({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:p(e);const a=await this.attachContract(n.from);delete n.from;const i=await a.functions.revokeDelegate(this.address,e,t,n);return await i.wait()}async setAttribute(e,t,r,n={}){const a=s({gasLimit:123456,gasPrice:1e9,controller:void 0},n);e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const i=await this.attachContract(a.from);delete a.from;const o=await i.functions.setAttribute(this.address,e,t,r,a);return await o.wait()}async revokeAttribute(e,t,r={}){const n=s({gasLimit:123456,gasPrice:1e9},r);e=e.startsWith("0x")?e:p(e),t=t.startsWith("0x")?t:"0x"+Buffer.from(t,"utf-8").toString("hex");const a=await this.attachContract(n.from);delete n.from;const i=await a.functions.revokeAttribute(this.address,e,t,n);return await i.wait()}}function S(e,r){return r.map(r=>function(e){const r={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((n,a)=>{let i=e.args[a];"object"==typeof i&&(i=t.from(i)),"bytes32"===n.type&&(i=g(i)),r[n.name]=i}),r._eventName=e.name,r}(e.interface.parseLog(r)))}function $(e){return new I(e).build()}class I{constructor(e){this.contracts=function(e={}){const t=s({},(r=e.infuraProjectId)?x({networks:[{name:"mainnet",chainId:"0x1",provider:new a("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new a("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new a("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new a("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new a("kovan",r)}]}):{},x(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 a=await this.contracts[r].functions.changed(e,{blockTag:n});return t.from(a[0])}async changeLog(e,r,n="latest"){const a=this.contracts[r],i=a.provider,o=r.startsWith("0x")?r:y[r],c=(await i.getNetwork()).chainId,s=o?t.from(o).toNumber():c,d=[],{address:l,publicKey:u}=v(e);let h=u,f=l,m=await this.previousChange(l,r,n);if(m){const e=await this.getOwner(l,r,n);e.toLowerCase()!==f.toLowerCase()&&(h=void 0),f=e}for(;m;){const e=m,t=S(a,await i.getLogs({address:a.address,topics:[null,`0x000000000000000000000000${l.slice(2)}`],fromBlock:m.toHexString(),toBlock:m.toHexString()}));t.reverse(),m=null;for(const r of t)d.unshift(r),r.previousChange.lt(e)&&(m=r.previousChange)}return{controller:f,history:d,controllerKey:h,chainId:s}}wrapDidDocument(r,n,a,i,o){const c={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[]},d=t.from(Math.floor((new Date).getTime()/1e3)),l=[{id:`${r}#controller`,type:u.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${n}@eip155:${o}`}],g=[`${r}#controller`];a&&(l.push({id:`${r}#controllerKey`,type:u.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:a}),g.push(`${r}#controllerKey`));let p=!1,v=0,w=0;const y={},b={},k={};for(const n of i){const a=n.validTo||t.from(0),i=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(a&&a.gte(d)){if(n._eventName===h.DIDDelegateChanged){const e=n;switch(v++,e.delegateType){case"sigAuth":y[i]=`${r}#delegate-${v}`;case"veriKey":b[i]={id:`${r}#delegate-${v}`,type:u.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${o}`}}}else if(n._eventName===h.DIDAttributeChanged){const t=n,a=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(a){const n=a[2],o=f[a[4]]||a[4],c=a[6];switch(a[1]){case"pub":{v++;const s={id:`${r}#delegate-${v}`,type:`${n}${o}`,controller:r};switch(s.type=m[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}b[i]=s,"sigAuth"===a[4]&&(y[i]=s.id);break}case"svc":w++,k[i]={id:`${r}#service-${w}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===h.DIDDelegateChanged||n._eventName===h.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?v++:n._eventName===h.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&w++,delete y[i],delete b[i],delete k[i],n._eventName===h.DIDOwnerChanged&&"0x0000000000000000000000000000000000000000"===n.owner){p=!0;break}}const D=s({},c,{verificationMethod:l.concat(Object.values(b)),authentication:g.concat(Object.values(y))});return Object.values(k).length>0&&(D.service=Object.values(k)),p?{didDocument:s({},c,{"@context":"https://www.w3.org/ns/did/v1"}),deactivated:p}:{didDocument:D,deactivated:p}}async resolve(e,t,r,n){const a=t.id.match(d);if(!a)return{didResolutionMetadata:{error:b.invalidDid,message:`Not a valid did:ethr: ${t.id}`},didDocumentMetadata:{},didDocument:null};const i=a[2],o=a[1]?a[1].slice(0,-1):"mainnet";if(!this.contracts[o])return{didResolutionMetadata:{error:b.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${o}`},didDocumentMetadata:{},didDocument:null};const{controller:c,history:l,controllerKey:u,chainId:h}=await this.changeLog(i,o,n.blockTag);try{const{didDocument:t,deactivated:r}=this.wrapDidDocument(e,c,u,l,h);return{didDocumentMetadata:s({},r?{deactivated:!0}:{}),didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:t}}catch(e){return{didResolutionMetadata:{error:b.notFound,message:e.toString()},didDocumentMetadata:{},didDocument:null}}}build(){return{ethr:this.resolve.bind(this)}}}export{b as Errors,K as EthrDidController,l as REGISTRY,f as attrTypes,g as bytes32toString,m as delegateTypes,$ as getResolver,d as identifierMatcher,p as stringToBytes32,u 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 a}from"@ethersproject/providers";import i 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")){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 a;let o=e.provider||(null==(a=e.web3)?void 0:a.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||""],a=r?t.from(r).toNumber():r,i=w[e.name||""]?null==(c=e.name)?void 0:c.replace("mainnet","homestead"):"any";o=new n(e.rpcUrl,a||i)}}return r.fromSolidity(i).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",a,i,o="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b"){const{address:c,publicKey:s,network:d}=y(e),l=d||n;if(t)this.contract=t;else{if(!(a||null!=r&&r.provider))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=D({name:l,provider:a||(null==r?void 0:r.provider),registry:o,rpcUrl:i})}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 a=await n.functions.changeOwner(this.address,e,r);return await a.wait()}async addDelegate(e,t,r,n={}){const a=d({gasLimit:123456,gasPrice:1e9},n),i=await this.attachContract(a.from);delete a.from;const o=v(e),c=await i.functions.addDelegate(this.address,o,t,r,a);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 a=await this.attachContract(n.from);delete n.from;const i=await a.functions.revokeDelegate(this.address,e,t,n);return await i.wait()}async setAttribute(e,t,r,n={}){const a=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 i=await this.attachContract(a.from);delete a.from;const o=await i.functions.setAttribute(this.address,e,t,r,a);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 a=await this.attachContract(n.from);delete n.from;const i=await a.functions.revokeAttribute(this.address,e,t,n);return await i.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,a)=>{let i=e.args[a];"object"==typeof i&&(i=t.from(i)),"bytes32"===r.type&&(i=p(i)),n[r.name]=i}),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 a("homestead",r)},{name:"ropsten",chainId:"0x3",provider:new a("ropsten",r)},{name:"rinkeby",chainId:"0x4",provider:new a("rinkeby",r)},{name:"goerli",chainId:"0x5",provider:new a("goerli",r)},{name:"kovan",chainId:"0x2a",provider:new a("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 a=await this.contracts[r].functions.changed(e,{blockTag:n});return t.from(a[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 a=this.contracts[r],i=a.provider,o=r.startsWith("0x")?r:b[r],c=(await i.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(a,await i.getLogs({address:a.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,a,i,o,c,s){const l={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:r,verificationMethod:[],authentication:[]};let u=n;const p=[`${r}#controller`];let v=0,y=Number.POSITIVE_INFINITY,w=!1,b=0,k=0;const D={},x={},I={};for(const n of i){if(-1!==c&&n.blockNumber>c){y>n.blockNumber&&(y=n.blockNumber);continue}v<n.blockNumber&&(v=n.blockNumber);const a=n.validTo||t.from(0),i=`${n._eventName}-${n.delegateType||n.name}-${n.delegate||n.value}`;if(a&&a.gte(s)){if(n._eventName===f.DIDDelegateChanged){const e=n;switch(b++,e.delegateType){case"sigAuth":D[i]=`${r}#delegate-${b}`;case"veriKey":x[i]={id:`${r}#delegate-${b}`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${e.delegate}@eip155:${o}`}}}else if(n._eventName===f.DIDAttributeChanged){const t=n,a=t.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(a){const n=a[2],o=m[a[4]]||a[4],c=a[6];switch(a[1]){case"pub":{b++;const s={id:`${r}#delegate-${b}`,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}x[i]=s,"sigAuth"===a[4]&&(D[i]=s.id);break}case"svc":k++,I[i]={id:`${r}#service-${k}`,type:n,serviceEndpoint:Buffer.from(t.value.slice(2),"hex").toString()}}}}}else if(n._eventName===f.DIDOwnerChanged){if(u=n.owner,"0x0000000000000000000000000000000000000000"===n.owner){w=!0;break}}else n._eventName===f.DIDDelegateChanged||n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/pub\//)?b++:n._eventName===f.DIDAttributeChanged&&n.name.match(/^did\/svc\//)&&k++,delete D[i],delete x[i],delete I[i]}const K=[{id:`${r}#controller`,type:h.EcdsaSecp256k1RecoveryMethod2020,controller:r,blockchainAccountId:`${u}@eip155:${o}`}];a&&u==n&&(K.push({id:`${r}#controllerKey`,type:h.EcdsaSecp256k1VerificationKey2019,controller:r,publicKeyHex:a}),p.push(`${r}#controllerKey`));const S=d({},l,{verificationMethod:K.concat(Object.values(x)),authentication:p.concat(Object.values(D))});return Object.values(I).length>0&&(S.service=Object.values(I)),w?{didDocument:d({},l,{"@context":"https://www.w3.org/ns/did/v1"}),deactivated:w,versionId:v,nextVersionId:y}:{didDocument:S,deactivated:w,versionId:v,nextVersionId:y}}async resolve(e,r,n,a){const i=r.id.match(l);if(!i)return{didResolutionMetadata:{error:k.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null};const o=i[2],c=i[1]?i[1].slice(0,-1):"mainnet";let u=a.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:a}=this.wrapDidDocument(e,f,g,m,p,u,h),i=r?{deactivated:!0}:{};let o={},s={};if(0!==n){const e=await this.getBlockMetadata(n,c);o={versionId:e.height,updated:e.isoDate}}if(a!==Number.POSITIVE_INFINITY){const e=await this.getBlockMetadata(a,c);s={nextVersionId:e.height,nextUpdate:e.isoDate}}return{didDocumentMetadata:d({},i,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")):"function"==typeof define&&define.amd?define(["exports","@ethersproject/basex","@ethersproject/bignumber","@ethersproject/contracts","@ethersproject/providers","ethr-did-registry","@ethersproject/address","@ethersproject/transactions"],t):t((e||self).ethrDidResolver={},e.basex,e.bignumber,e.contracts,e.providers,e.ethrDidRegistry,e.address,e.transactions)}(this,function(e,t,r,n,i,o,c,s){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})$/,h="0xdca7ef03e98e0dc2b855be647c39abe984fcf21b";var l,f;e.verificationMethodTypes=void 0,(l=e.verificationMethodTypes||(e.verificationMethodTypes={})).EcdsaSecp256k1VerificationKey2019="EcdsaSecp256k1VerificationKey2019",l.EcdsaSecp256k1RecoveryMethod2020="EcdsaSecp256k1RecoveryMethod2020",l.Ed25519VerificationKey2018="Ed25519VerificationKey2018",l.RSAVerificationKey2018="RSAVerificationKey2018",l.X25519KeyAgreementKey2019="X25519KeyAgreementKey2019",function(e){e.DIDOwnerChanged="DIDOwnerChanged",e.DIDAttributeChanged="DIDAttributeChanged",e.DIDDelegateChanged="DIDDelegateChanged"}(f||(f={}));const v={sigAuth:"SignatureAuthentication2018",veriKey:"VerificationKey2018",enc:"KeyAgreementKey2019"},m={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 g(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 y(e){let t,r=e;if(r.startsWith("did:ethr")){const e=r.split(":");r=e[e.length-1],4===e.length&&(t=e[2])}return r.length>42?{address:s.computeAddress(r),publicKey:r,network:t}:{address:c.getAddress(r),network:t}}const b={mainnet:"0x1",ropsten:"0x3",rinkeby:"0x4",goerli:"0x5",kovan:"0x2a"},w={...b,rsk:"0x1e","rsk:testnet":"0x1f",artis_t1:"0x03c401",artis_s1:"0x03c301",matic:"0x89",maticmum:"0x13881"};var k;function D(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:w[e.name||""],n=t?r.BigNumber.from(t).toNumber():t,s=b[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(d.default).attach(e.registry||h).connect(o)}function x(e){const t={},r=e.chainId||w[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 P(e){var t;return{...x(e),...null==(t=e.networks)?void 0:t.reduce((e,t)=>({...e,...x(t)}),{})}}e.Errors=void 0,(k=e.Errors||(e.Errors={})).notFound="notFound",k.invalidDid="invalidDid",k.unknownNetwork="unknownNetwork";class K{constructor(e,t,r,n="mainnet",i,o,c=h){const{address:s,publicKey:a,network:d}=y(e),u=d||n;if(t)this.contract=t;else{if(!(i||null!=r&&r.provider))throw new Error(" either a contract instance or a provider or rpcUrl is required to initialize");this.contract=D({name:u,provider:i||(null==r?void 0:r.provider),registry:c,rpcUrl:o})}this.signer=r,this.address=s;let l=u?`${u}:`:"";l in["mainnet:","0x1:"]&&(l=""),this.did=a?`did:ethr:${l}${a}`:`did:ethr:${l}${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=p(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:p(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:p(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:p(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 j){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 j=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 I(e){return e instanceof j&&1&e.s}class ${constructor(e){this.contracts=function(e={}){const t={...(r=e.infuraProjectId,r?P({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)}]}):{}),...P(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)}}changeLog(e,t,n="latest"){try{const i=this,o=i.contracts[t],c=o.provider,s=t.startsWith("0x")?t:w[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}=y(e);let v=f,m=l;return Promise.resolve(i.previousChange(l,t,n)).then(function(e){function s(){function t(){return{controller:m,history:h,controllerKey:v,chainId:u}}const n=function(e,t,r){for(var n;;){var i=e();if(I(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!I(o)){n=1;break}o=o.s}}var c=new j,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())||I(i)&&!i.v)return void S(c,1,o);if(i.then)return void i.then(d).then(void 0,s);I(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){const t={};if(e.eventFragment.inputs.length!==e.args.length)throw new TypeError("malformed event input. wrong number of arguments");return e.eventFragment.inputs.forEach((n,i)=>{let o=e.args[i];"object"==typeof o&&(o=r.BigNumber.from(o)),"bytes32"===n.type&&(o=g(o)),t[n.name]=o}),t._eventName=e.name,t}(e.interface.parseLog(t)))}(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()}const a=function(){if(e)return Promise.resolve(i.getOwner(l,t,n)).then(function(e){e.toLowerCase()!==m.toLowerCase()&&(v=void 0),m=e})}();return a&&a.then?a.then(s):s()})})}catch(e){return Promise.reject(e)}}wrapDidDocument(n,i,o,c,s){const a={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:n,verificationMethod:[],authentication:[]},d=r.BigNumber.from(Math.floor((new Date).getTime()/1e3)),u=[{id:`${n}#controller`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${i}@eip155:${s}`}],h=[`${n}#controller`];o&&(u.push({id:`${n}#controllerKey`,type:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:n,publicKeyHex:o}),h.push(`${n}#controllerKey`));let l=!1,g=0,p=0;const y={},b={},w={};for(const i of c){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===f.DIDDelegateChanged){const t=i;switch(g++,t.delegateType){case"sigAuth":y[c]=`${n}#delegate-${g}`;case"veriKey":b[c]={id:`${n}#delegate-${g}`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${t.delegate}@eip155:${s}`}}}else if(i._eventName===f.DIDAttributeChanged){const e=i,r=e.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(r){const i=r[2],o=v[r[4]]||r[4],s=r[6];switch(r[1]){case"pub":{g++;const a={id:`${n}#delegate-${g}`,type:`${i}${o}`,controller:n};switch(a.type=m[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}b[c]=a,"sigAuth"===r[4]&&(y[c]=a.id);break}case"svc":p++,w[c]={id:`${n}#service-${p}`,type:i,serviceEndpoint:Buffer.from(e.value.slice(2),"hex").toString()}}}}}else if(i._eventName===f.DIDDelegateChanged||i._eventName===f.DIDAttributeChanged&&i.name.match(/^did\/pub\//)?g++:i._eventName===f.DIDAttributeChanged&&i.name.match(/^did\/svc\//)&&p++,delete y[c],delete b[c],delete w[c],i._eventName===f.DIDOwnerChanged&&"0x0000000000000000000000000000000000000000"===i.owner){l=!0;break}}const k={...a,verificationMethod:u.concat(Object.values(b)),authentication:h.concat(Object.values(y))};return Object.values(w).length>0&&(k.service=Object.values(w)),l?{didDocument:{...a,"@context":"https://www.w3.org/ns/did/v1"},deactivated:l}:{didDocument:k,deactivated:l}}resolve(t,r,n,i){try{const n=this,o=r.id.match(u);if(!o)return Promise.resolve({didResolutionMetadata:{error:e.Errors.invalidDid,message:`Not a valid did:ethr: ${r.id}`},didDocumentMetadata:{},didDocument:null});const c=o[2],s=o[1]?o[1].slice(0,-1):"mainnet";return n.contracts[s]?Promise.resolve(n.changeLog(c,s,i.blockTag)).then(function({controller:r,history:i,controllerKey:o,chainId:c}){try{const{didDocument:e,deactivated:s}=n.wrapDidDocument(t,r,o,i,c);return{didDocumentMetadata:{...s?{deactivated:!0}:{}},didResolutionMetadata:{contentType:"application/did+ld+json"},didDocument:e}}catch(t){return{didResolutionMetadata:{error:e.Errors.notFound,message:t.toString()},didDocumentMetadata:{},didDocument:null}}}):Promise.resolve({didResolutionMetadata:{error:e.Errors.unknownNetwork,message:`The DID resolver does not have a configuration for network: ${s}`},didDocumentMetadata:{},didDocument:null})}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}e.EthrDidController=K,e.REGISTRY=h,e.attrTypes=v,e.bytes32toString=g,e.delegateTypes=m,e.getResolver=function(e){return new $(e).build()},e.identifierMatcher=u,e.stringToBytes32=p}); | ||
!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,m;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"}(m||(m={}));const v={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")){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))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 ${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),m=f;return Promise.resolve(i.previousChange(l,t,n)).then(function(e){function t(){return{address:l,history:h,controllerKey:m,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){const u={"@context":["https://www.w3.org/ns/did/v1","https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld"],id:n,verificationMethod:[],authentication:[]};let h=i;const l=[`${n}#controller`];let f=0,p=Number.POSITIVE_INFINITY,y=!1,b=0,w=0;const k={},D={},P={};for(const i of c){if(-1!==a&&i.blockNumber>a){p>i.blockNumber&&(p=i.blockNumber);continue}f<i.blockNumber&&(f=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===m.DIDDelegateChanged){const t=i;switch(b++,t.delegateType){case"sigAuth":k[c]=`${n}#delegate-${b}`;case"veriKey":D[c]={id:`${n}#delegate-${b}`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${t.delegate}@eip155:${s}`}}}else if(i._eventName===m.DIDAttributeChanged){const e=i,r=e.name.match(/^did\/(pub|svc)\/(\w+)(\/(\w+))?(\/(\w+))?$/);if(r){const i=r[2],o=v[r[4]]||r[4],s=r[6];switch(r[1]){case"pub":{b++;const a={id:`${n}#delegate-${b}`,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}D[c]=a,"sigAuth"===r[4]&&(k[c]=a.id);break}case"svc":w++,P[c]={id:`${n}#service-${w}`,type:i,serviceEndpoint:Buffer.from(e.value.slice(2),"hex").toString()}}}}}else if(i._eventName===m.DIDOwnerChanged){if(h=i.owner,"0x0000000000000000000000000000000000000000"===i.owner){y=!0;break}}else i._eventName===m.DIDDelegateChanged||i._eventName===m.DIDAttributeChanged&&i.name.match(/^did\/pub\//)?b++:i._eventName===m.DIDAttributeChanged&&i.name.match(/^did\/svc\//)&&w++,delete k[c],delete D[c],delete P[c]}const x=[{id:`${n}#controller`,type:e.verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020,controller:n,blockchainAccountId:`${h}@eip155:${s}`}];o&&h==i&&(x.push({id:`${n}#controllerKey`,type:e.verificationMethodTypes.EcdsaSecp256k1VerificationKey2019,controller:n,publicKeyHex:o}),l.push(`${n}#controllerKey`));const I={...u,verificationMethod:x.concat(Object.values(D)),authentication:l.concat(Object.values(k))};return Object.values(P).length>0&&(I.service=Object.values(P)),y?{didDocument:{...u,"@context":"https://www.w3.org/ns/did/v1"},deactivated:y,versionId:f,nextVersionId:p}:{didDocument:I,deactivated:y,versionId:f,nextVersionId:p}}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:{...m,...v,...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),m=a?{deactivated:!0}:{};let v={},g={};const p=function(){if(0!==d)return Promise.resolve(i.getBlockMetadata(d,u)).then(function(e){v={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 m=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(m&&m.then?m.then(c):c())}catch(e){return Promise.reject(e)}}build(){return{ethr:this.resolve.bind(this)}}}e.EthrDidController=K,e.REGISTRY=l,e.attrTypes=v,e.bytes32toString=p,e.delegateTypes=g,e.getResolver=function(e){return new $(e).build()},e.identifierMatcher=h,e.interpretIdentifier=b,e.stringToBytes32=y}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -22,4 +22,8 @@ import { BigNumber } from '@ethersproject/bignumber'; | ||
previousChange(address: string, networkId: string, blockTag?: BlockTag): Promise<BigNumber>; | ||
getBlockMetadata(blockHeight: number, networkId: string): Promise<{ | ||
height: string; | ||
isoDate: string; | ||
}>; | ||
changeLog(identity: string, networkId: string, blockTag?: BlockTag): Promise<{ | ||
controller: string; | ||
address: string; | ||
history: ERC1056Event[]; | ||
@@ -29,5 +33,7 @@ controllerKey?: string; | ||
}>; | ||
wrapDidDocument(did: string, controller: string, controllerKey: string | undefined, history: ERC1056Event[], chainId: number): { | ||
wrapDidDocument(did: string, address: string, controllerKey: string | undefined, history: ERC1056Event[], chainId: number, blockHeight: string | number, now: BigNumber): { | ||
didDocument: DIDDocument; | ||
deactivated: boolean; | ||
versionId: number; | ||
nextVersionId: number; | ||
}; | ||
@@ -34,0 +40,0 @@ resolve(did: string, parsed: ParsedDID, _unused: Resolvable, options: DIDResolutionOptions): Promise<DIDResolutionResult>; |
{ | ||
"name": "ethr-did-resolver", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Resolve DID documents around ethereum addresses", | ||
@@ -56,4 +56,4 @@ "main": "lib/index.umd.js", | ||
"devDependencies": { | ||
"@babel/core": "7.13.14", | ||
"@babel/preset-env": "7.13.12", | ||
"@babel/core": "7.13.15", | ||
"@babel/preset-env": "7.13.15", | ||
"@babel/preset-typescript": "7.13.0", | ||
@@ -63,8 +63,8 @@ "@semantic-release/changelog": "5.0.1", | ||
"@types/jest": "26.0.22", | ||
"@typescript-eslint/eslint-plugin": "4.21.0", | ||
"@typescript-eslint/parser": "4.21.0", | ||
"@typescript-eslint/eslint-plugin": "4.22.0", | ||
"@typescript-eslint/parser": "4.22.0", | ||
"babel-jest": "26.6.3", | ||
"codecov": "3.8.1", | ||
"eslint": "7.23.0", | ||
"eslint-config-prettier": "8.1.0", | ||
"eslint": "7.24.0", | ||
"eslint-config-prettier": "8.2.0", | ||
"ganache-cli": "6.12.2", | ||
@@ -76,23 +76,17 @@ "jest": "26.6.3", | ||
"semantic-release": "17.4.2", | ||
"typescript": "4.2.3", | ||
"uint8arrays": "2.1.3" | ||
"typescript": "4.2.4" | ||
}, | ||
"dependencies": { | ||
"@ethersproject/abi": "^5.0.13", | ||
"@ethersproject/abstract-signer": "^5.0.14", | ||
"@ethersproject/address": "^5.0.11", | ||
"@ethersproject/basex": "^5.0.9", | ||
"@ethersproject/bignumber": "^5.0.15", | ||
"@ethersproject/contracts": "^5.0.12", | ||
"@ethersproject/providers": "^5.0.24", | ||
"@ethersproject/transactions": "^5.0.11", | ||
"@ethersproject/abi": "^5.1.0", | ||
"@ethersproject/abstract-signer": "^5.1.0", | ||
"@ethersproject/address": "^5.1.0", | ||
"@ethersproject/basex": "^5.1.0", | ||
"@ethersproject/bignumber": "^5.1.0", | ||
"@ethersproject/contracts": "^5.1.0", | ||
"@ethersproject/providers": "^5.1.0", | ||
"@ethersproject/transactions": "^5.1.0", | ||
"did-resolver": "^3.1.0", | ||
"ethr-did-registry": "^0.0.3" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged", | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
} | ||
"ethr-did-registry": "^0.0.3", | ||
"querystring": "^0.2.1" | ||
} | ||
} |
@@ -5,2 +5,4 @@ import { Resolver } from 'did-resolver' | ||
jest.setTimeout(30000) | ||
describe('ethrResolver (alt-chains)', () => { | ||
@@ -7,0 +9,0 @@ const addr = '0xd0dbe9d3698738f899ccd8ee27ff2347a7faa4dd' |
@@ -6,3 +6,2 @@ import { Contract, ContractFactory } from '@ethersproject/contracts' | ||
import DidRegistryContract from 'ethr-did-registry' | ||
import * as u8a from 'uint8arrays' | ||
import { interpretIdentifier, stringToBytes32 } from '../helpers' | ||
@@ -110,4 +109,6 @@ import { createProvider, sleep, startMining, stopMining } from './testUtils' | ||
await new EthrDidController(identity, registryContract).changeOwner(controller, { from: identity }) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '2' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -163,7 +164,9 @@ didDocument: { | ||
expect.assertions(1) | ||
await new EthrDidController(identity, registryContract).addDelegate('veriKey', delegate1, 100, { | ||
await new EthrDidController(identity, registryContract).addDelegate('veriKey', delegate1, 86401, { | ||
from: controller, | ||
}) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
delete result.didDocumentMetadata.updated | ||
await expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '3' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -202,4 +205,7 @@ didDocument: { | ||
}) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '4' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -242,5 +248,8 @@ didDocument: { | ||
//key validity was set to less than 2 seconds | ||
await sleep(3) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
await sleep(4) | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '4' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -280,4 +289,7 @@ didDocument: { | ||
await sleep(1) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '5' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -307,7 +319,10 @@ didDocument: { | ||
expect.assertions(1) | ||
await new EthrDidController(identity, registryContract).addDelegate('sigAuth', delegate2, 86400, { | ||
await new EthrDidController(identity, registryContract).addDelegate('sigAuth', delegate2, 86402, { | ||
from: controller, | ||
}) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '6' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -348,3 +363,3 @@ didDocument: { | ||
'0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', | ||
10, | ||
86401, | ||
{ from: controller } | ||
@@ -388,3 +403,3 @@ ) | ||
'0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', | ||
10, | ||
86402, | ||
{ from: controller } | ||
@@ -437,3 +452,3 @@ ) | ||
'-----BEGIN PUBLIC KEY...END PUBLIC KEY-----\r\n', | ||
10, | ||
86403, | ||
{ from: controller } | ||
@@ -493,3 +508,3 @@ ) | ||
`0x${Buffer.from('MCowBQYDK2VuAyEAEYVXd3/7B4d0NxpSsA/tdVYdz5deYcR1U+ZkphdmEFI=', 'base64').toString('hex')}`, | ||
86400, | ||
86404, | ||
{ from: keyAgreementController } | ||
@@ -529,3 +544,3 @@ ) | ||
'https://hubs.uport.me', | ||
10, | ||
86405, | ||
{ from: controller } | ||
@@ -767,4 +782,8 @@ ) | ||
await Promise.all([ | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService'), 'https://test.uport.me', 10, { from: controller }), | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService'), 'https://test.uport.me', 10, { from: controller }), | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService'), 'https://test.uport.me', 86406, { | ||
from: controller, | ||
}), | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService'), 'https://test.uport.me', 86407, { | ||
from: controller, | ||
}), | ||
sleep(1).then(() => startMining(web3Provider)), | ||
@@ -776,4 +795,7 @@ ]) | ||
expect.assertions(1) | ||
expect(await didResolver.resolve(did)).toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '16' }, | ||
didResolutionMetadata: { | ||
@@ -820,3 +842,3 @@ contentType: 'application/did+ld+json', | ||
await Promise.all([ | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService2'), 'https://test2.uport.me', 10, { | ||
ethrDid.setAttribute(stringToBytes32('did/svc/TestService2'), 'https://test2.uport.me', 86408, { | ||
from: controller, | ||
@@ -835,4 +857,7 @@ }), | ||
expect.assertions(1) | ||
expect(await didResolver.resolve(did)).toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '18' }, | ||
didResolutionMetadata: { | ||
@@ -884,3 +909,3 @@ contentType: 'application/did+ld+json', | ||
'0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71', | ||
10, | ||
86409, | ||
{ from: lowAddress } | ||
@@ -904,3 +929,3 @@ ) | ||
`0x${authPubKey}`, | ||
86400, | ||
86410, | ||
{ from: identity } | ||
@@ -939,11 +964,14 @@ ) | ||
const publicKeyHex = `b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71` | ||
const expectedPublicKeyBase58 = u8a.toString(u8a.fromString(publicKeyHex, 'base16'), 'base58btc') | ||
const expectedPublicKeyBase58 = 'DV4G2kpBKjE6zxKor7Cj21iL9x9qyXb6emqjszBXcuhz' | ||
await new EthrDidController(identity, registryContract).setAttribute( | ||
'did/pub/Ed25519/veriKey/base58', | ||
`0x${publicKeyHex}`, | ||
86400, | ||
86411, | ||
{ from: identity } | ||
) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
didDocumentMetadata: {}, | ||
const result = await didResolver.resolve(did) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '21' }, | ||
didResolutionMetadata: { | ||
@@ -980,9 +1008,13 @@ contentType: 'application/did+ld+json', | ||
it('resolves deactivated document', async () => { | ||
expect.assertions(1) | ||
expect.assertions(2) | ||
const identity = accounts[6] | ||
const did = `did:ethr:dev:${identity}` | ||
await new EthrDidController(identity, registryContract).changeOwner(nullAddress, { from: identity }) | ||
await expect(didResolver.resolve(did)).resolves.toEqual({ | ||
const result = await didResolver.resolve(did) | ||
expect(result.didDocumentMetadata.updated).toBeDefined() | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { | ||
deactivated: true, | ||
versionId: '22', | ||
}, | ||
@@ -1001,3 +1033,234 @@ didResolutionMetadata: { | ||
}) | ||
describe('versioning', () => { | ||
it('can resolve virgin DID with versionId=latest', async () => { | ||
expect.assertions(1) | ||
const virginAddress = '0xce3080168EE293053bA33b235D7116a3263D29f1' | ||
const virginDID = `did:ethr:dev:${virginAddress}` | ||
const result = await didResolver.resolve(`${virginDID}?versionId=latest`) | ||
expect(result).toEqual({ | ||
didDocumentMetadata: {}, | ||
didResolutionMetadata: { | ||
contentType: 'application/did+ld+json', | ||
}, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: virginDID, | ||
verificationMethod: [ | ||
{ | ||
id: `${virginDID}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: virginDID, | ||
blockchainAccountId: `${virginAddress}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${virginDID}#controller`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve modified did with versionId=latest', async () => { | ||
expect.assertions(2) | ||
const identity = accounts[3] | ||
const modifiedDid = `did:ethr:dev:${identity}` | ||
const result = await didResolver.resolve(`${modifiedDid}?versionId=latest`) | ||
expect(result.didDocumentMetadata.updated).toBeDefined() | ||
delete result.didDocumentMetadata.updated | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '21' }, | ||
didResolutionMetadata: { | ||
contentType: 'application/did+ld+json', | ||
}, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: modifiedDid, | ||
verificationMethod: [ | ||
{ | ||
id: `${modifiedDid}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: modifiedDid, | ||
blockchainAccountId: `${identity}@eip155:1337`, | ||
}, | ||
{ | ||
id: `${modifiedDid}#delegate-1`, | ||
type: 'Ed25519VerificationKey2018', | ||
controller: modifiedDid, | ||
publicKeyBase58: 'DV4G2kpBKjE6zxKor7Cj21iL9x9qyXb6emqjszBXcuhz', | ||
}, | ||
], | ||
authentication: [`${modifiedDid}#controller`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve did with versionId before an attribute change', async () => { | ||
expect.assertions(3) | ||
const result = await didResolver.resolve(`${did}?versionId=6`) | ||
expect(result.didDocumentMetadata.updated).toBeDefined() | ||
delete result.didDocumentMetadata.updated | ||
expect(result.didDocumentMetadata.nextUpdate).toBeDefined() | ||
delete result.didDocumentMetadata.nextUpdate | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '6', nextVersionId: '7' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: did, | ||
verificationMethod: [ | ||
{ | ||
id: `${did}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${controller}@eip155:1337`, | ||
}, | ||
{ | ||
id: `${did}#delegate-4`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${delegate2}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${did}#controller`, `${did}#delegate-4`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve did with versionId before a delegate change', async () => { | ||
expect.assertions(3) | ||
const result = await didResolver.resolve(`${did}?versionId=2`) | ||
expect(result.didDocumentMetadata.updated).toBeDefined() | ||
delete result.didDocumentMetadata.updated | ||
expect(result.didDocumentMetadata.nextUpdate).toBeDefined() | ||
delete result.didDocumentMetadata.nextUpdate | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '2', nextVersionId: '3' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: did, | ||
verificationMethod: [ | ||
{ | ||
id: `${did}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${controller}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${did}#controller`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve did with versionId before an owner change', async () => { | ||
expect.assertions(2) | ||
const result = await didResolver.resolve(`${did}?versionId=1`) | ||
expect(result.didDocumentMetadata.nextUpdate).toBeDefined() | ||
delete result.didDocumentMetadata.nextUpdate | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { | ||
nextVersionId: '2', | ||
}, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: did, | ||
verificationMethod: [ | ||
{ | ||
id: `${did}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${identity}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${did}#controller`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve did with versionId before deactivation', async () => { | ||
expect.assertions(2) | ||
const deactivatedDid = `did:ethr:dev:${accounts[6]}` | ||
const result = await didResolver.resolve(`${deactivatedDid}?versionId=21`) | ||
expect(result.didDocumentMetadata.nextUpdate).toBeDefined() | ||
delete result.didDocumentMetadata.nextUpdate | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { | ||
nextVersionId: '22', | ||
}, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: deactivatedDid, | ||
verificationMethod: [ | ||
{ | ||
id: `${deactivatedDid}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: deactivatedDid, | ||
blockchainAccountId: `${accounts[6]}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${deactivatedDid}#controller`], | ||
}, | ||
}) | ||
}) | ||
it('can resolve did with versionId before an attribute expiration', async () => { | ||
expect.assertions(1) | ||
const result = await didResolver.resolve(`${did}?versionId=4`) | ||
//don't compare against hardcoded timestamps | ||
delete result.didDocumentMetadata.updated | ||
delete result.didDocumentMetadata.nextUpdate | ||
expect(result).toEqual({ | ||
didDocumentMetadata: { versionId: '4', nextVersionId: '5' }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
didDocument: { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/lds-ecdsa-secp256k1-recovery2020-0.0.jsonld', | ||
], | ||
id: did, | ||
verificationMethod: [ | ||
{ | ||
id: `${did}#controller`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${controller}@eip155:1337`, | ||
}, | ||
{ | ||
id: `${did}#delegate-1`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${delegate1}@eip155:1337`, | ||
}, | ||
{ | ||
id: `${did}#delegate-2`, | ||
type: 'EcdsaSecp256k1RecoveryMethod2020', | ||
controller: did, | ||
blockchainAccountId: `${delegate2}@eip155:1337`, | ||
}, | ||
], | ||
authentication: [`${did}#controller`, `${did}#delegate-2`], | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -21,2 +21,3 @@ import { getAddress } from '@ethersproject/address' | ||
_eventName: string | ||
blockNumber: number | ||
} | ||
@@ -95,15 +96,15 @@ | ||
export function interpretIdentifier(identifier: string): { address: string; publicKey?: string; network?: string } { | ||
let input = identifier | ||
let id = identifier | ||
let network = undefined | ||
if (input.startsWith('did:ethr')) { | ||
const components = input.split(':') | ||
input = components[components.length - 1] | ||
if (components.length === 4) { | ||
network = components[2] | ||
if (id.startsWith('did:ethr')) { | ||
const components = id.split(':') | ||
id = components[components.length - 1] | ||
if (components.length >= 4) { | ||
network = components.splice(2, components.length - 3).join(':') | ||
} | ||
} | ||
if (input.length > 42) { | ||
return { address: computeAddress(input), publicKey: input, network } | ||
if (id.length > 42) { | ||
return { address: computeAddress(id), publicKey: id, network } | ||
} else { | ||
return { address: getAddress(input), network } // checksum address | ||
return { address: getAddress(id), network } // checksum address | ||
} | ||
@@ -110,0 +111,0 @@ } |
@@ -7,2 +7,3 @@ import { getResolver } from './resolver' | ||
identifierMatcher, | ||
interpretIdentifier, | ||
legacyAlgoMap, | ||
@@ -27,3 +28,4 @@ legacyAttrTypes, | ||
identifierMatcher, | ||
interpretIdentifier, | ||
Errors, | ||
} |
@@ -7,3 +7,3 @@ import { BigNumber } from '@ethersproject/bignumber' | ||
function populateEventMetaClass(logResult: LogDescription): ERC1056Event { | ||
function populateEventMetaClass(logResult: LogDescription, blockNumber: number): ERC1056Event { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@@ -25,2 +25,3 @@ const result: Record<string, any> = {} | ||
result._eventName = logResult.name | ||
result.blockNumber = blockNumber | ||
return result as ERC1056Event | ||
@@ -32,3 +33,3 @@ } | ||
const res = contract.interface.parseLog(log) | ||
const event = populateEventMetaClass(res) | ||
const event = populateEventMetaClass(res, log.blockNumber) | ||
return event | ||
@@ -35,0 +36,0 @@ }) |
import { Base58 } from '@ethersproject/basex' | ||
import { BigNumber } from '@ethersproject/bignumber' | ||
import { BlockTag } from '@ethersproject/providers' | ||
import { Block, BlockTag } from '@ethersproject/providers' | ||
import { ConfigurationOptions, ConfiguredNetworks, configureResolverWithNetworks } from './configuration' | ||
@@ -33,2 +33,3 @@ import { EthrDidController } from './controller' | ||
import { logDecoder } from './logParser' | ||
import * as qs from 'querystring' | ||
@@ -67,2 +68,10 @@ export function getResolver(options: ConfigurationOptions): Record<string, DIDResolver> { | ||
async getBlockMetadata(blockHeight: number, networkId: string): Promise<{ height: string; isoDate: string }> { | ||
const block: Block = await this.contracts[networkId].provider.getBlock(blockHeight) | ||
return { | ||
height: block.number.toString(), | ||
isoDate: new Date(block.timestamp * 1000).toISOString(), | ||
} | ||
} | ||
async changeLog( | ||
@@ -72,3 +81,3 @@ identity: string, | ||
blockTag: BlockTag = 'latest' | ||
): Promise<{ controller: string; history: ERC1056Event[]; controllerKey?: string; chainId: number }> { | ||
): Promise<{ address: string; history: ERC1056Event[]; controllerKey?: string; chainId: number }> { | ||
const contract = this.contracts[networkId] | ||
@@ -82,13 +91,4 @@ const provider = contract.provider | ||
const { address, publicKey } = interpretIdentifier(identity) | ||
let controllerKey = publicKey | ||
let controller = address | ||
const controllerKey = publicKey | ||
let previousChange: BigNumber | null = await this.previousChange(address, networkId, blockTag) | ||
// console.log(`gigel 1 - '${previousChange}' - ${typeof previousChange}`) | ||
if (previousChange) { | ||
const newController = await this.getOwner(address, networkId, blockTag) | ||
if (newController.toLowerCase() !== controller.toLowerCase()) { | ||
controllerKey = undefined | ||
} | ||
controller = newController | ||
} | ||
while (previousChange) { | ||
@@ -114,3 +114,3 @@ const blockNumber = previousChange | ||
} | ||
return { controller, history, controllerKey, chainId } | ||
return { address, history, controllerKey, chainId } | ||
} | ||
@@ -120,7 +120,9 @@ | ||
did: string, | ||
controller: string, | ||
address: string, | ||
controllerKey: string | undefined, | ||
history: ERC1056Event[], | ||
chainId: number | ||
): { didDocument: DIDDocument; deactivated: boolean } { | ||
chainId: number, | ||
blockHeight: string | number, | ||
now: BigNumber | ||
): { didDocument: DIDDocument; deactivated: boolean; versionId: number; nextVersionId: number } { | ||
const baseDIDDocument: DIDDocument = { | ||
@@ -136,26 +138,8 @@ '@context': [ | ||
// const now = new BN(Math.floor(new Date().getTime() / 1000)) | ||
const now = BigNumber.from(Math.floor(new Date().getTime() / 1000)) | ||
// const expired = {} | ||
const publicKeys: VerificationMethod[] = [ | ||
{ | ||
id: `${did}#controller`, | ||
type: verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020, | ||
controller: did, | ||
blockchainAccountId: `${controller}@eip155:${chainId}`, | ||
}, | ||
] | ||
let controller = address | ||
const authentication = [`${did}#controller`] | ||
if (controllerKey) { | ||
publicKeys.push({ | ||
id: `${did}#controllerKey`, | ||
type: verificationMethodTypes.EcdsaSecp256k1VerificationKey2019, | ||
controller: did, | ||
publicKeyHex: controllerKey, | ||
}) | ||
authentication.push(`${did}#controllerKey`) | ||
} | ||
let versionId = 0 | ||
let nextVersionId = Number.POSITIVE_INFINITY | ||
let deactivated = false | ||
@@ -168,2 +152,12 @@ let delegateCount = 0 | ||
for (const event of history) { | ||
if (blockHeight !== -1 && event.blockNumber > blockHeight) { | ||
if (nextVersionId > event.blockNumber) { | ||
nextVersionId = event.blockNumber | ||
} | ||
continue | ||
} else { | ||
if (versionId < event.blockNumber) { | ||
versionId = event.blockNumber | ||
} | ||
} | ||
const validTo = event.validTo || BigNumber.from(0) | ||
@@ -244,2 +238,9 @@ const eventIndex = `${event._eventName}-${ | ||
} | ||
} else if (event._eventName === eventNames.DIDOwnerChanged) { | ||
const currentEvent = <DIDOwnerChanged>event | ||
controller = currentEvent.owner | ||
if (currentEvent.owner === nullAddress) { | ||
deactivated = true | ||
break | ||
} | ||
} else { | ||
@@ -261,12 +262,24 @@ if ( | ||
delete services[eventIndex] | ||
if (event._eventName === eventNames.DIDOwnerChanged) { | ||
if ((<DIDOwnerChanged>event).owner === nullAddress) { | ||
deactivated = true | ||
break | ||
} | ||
} | ||
} | ||
} | ||
const publicKeys: VerificationMethod[] = [ | ||
{ | ||
id: `${did}#controller`, | ||
type: verificationMethodTypes.EcdsaSecp256k1RecoveryMethod2020, | ||
controller: did, | ||
blockchainAccountId: `${controller}@eip155:${chainId}`, | ||
}, | ||
] | ||
if (controllerKey && controller == address) { | ||
publicKeys.push({ | ||
id: `${did}#controllerKey`, | ||
type: verificationMethodTypes.EcdsaSecp256k1VerificationKey2019, | ||
controller: did, | ||
publicKeyHex: controllerKey, | ||
}) | ||
authentication.push(`${did}#controllerKey`) | ||
} | ||
const doc: DIDDocument = { | ||
@@ -282,4 +295,9 @@ ...baseDIDDocument, | ||
return deactivated | ||
? { didDocument: { ...baseDIDDocument, '@context': 'https://www.w3.org/ns/did/v1' }, deactivated } | ||
: { didDocument: doc, deactivated } | ||
? { | ||
didDocument: { ...baseDIDDocument, '@context': 'https://www.w3.org/ns/did/v1' }, | ||
deactivated, | ||
versionId, | ||
nextVersionId, | ||
} | ||
: { didDocument: doc, deactivated, versionId, nextVersionId } | ||
} | ||
@@ -290,2 +308,3 @@ | ||
parsed: ParsedDID, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
_unused: Resolvable, | ||
@@ -307,2 +326,14 @@ options: DIDResolutionOptions | ||
const networkId = !fullId[1] ? 'mainnet' : fullId[1].slice(0, -1) | ||
let blockTag: string | number = options.blockTag || 'latest' | ||
// let versionAtMost: string | number = -1 | ||
if (typeof parsed.query === 'string') { | ||
const qParams = qs.decode(parsed.query) | ||
blockTag = typeof qParams['versionId'] === 'string' ? qParams['versionId'] : blockTag | ||
try { | ||
blockTag = Number.parseInt(<string>blockTag) | ||
} catch (e) { | ||
blockTag = 'latest' | ||
// invalid versionId parameters are ignored | ||
} | ||
} | ||
@@ -320,8 +351,41 @@ if (!this.contracts[networkId]) { | ||
const { controller, history, controllerKey, chainId } = await this.changeLog(id, networkId, options.blockTag) | ||
let now = BigNumber.from(Math.floor(new Date().getTime() / 1000)) | ||
if (typeof blockTag === 'number') { | ||
const block = await this.getBlockMetadata(blockTag, networkId) | ||
now = BigNumber.from(Date.parse(block.isoDate) / 1000) | ||
} else { | ||
// 'latest' | ||
} | ||
const { address, history, controllerKey, chainId } = await this.changeLog(id, networkId, 'latest') | ||
try { | ||
const { didDocument, deactivated } = this.wrapDidDocument(did, controller, controllerKey, history, chainId) | ||
const { didDocument, deactivated, versionId, nextVersionId } = this.wrapDidDocument( | ||
did, | ||
address, | ||
controllerKey, | ||
history, | ||
chainId, | ||
blockTag, | ||
now | ||
) | ||
const status = deactivated ? { deactivated: true } : {} | ||
let versionMeta = {} | ||
let versionMetaNext = {} | ||
if (versionId !== 0) { | ||
const block = await this.getBlockMetadata(versionId, networkId) | ||
versionMeta = { | ||
versionId: block.height, | ||
updated: block.isoDate, | ||
} | ||
} | ||
if (nextVersionId !== Number.POSITIVE_INFINITY) { | ||
const block = await this.getBlockMetadata(nextVersionId, networkId) | ||
versionMetaNext = { | ||
nextVersionId: block.height, | ||
nextUpdate: block.isoDate, | ||
} | ||
} | ||
return { | ||
didDocumentMetadata: { ...status }, | ||
didDocumentMetadata: { ...status, ...versionMeta, ...versionMetaNext }, | ||
didResolutionMetadata: { contentType: 'application/did+ld+json' }, | ||
@@ -328,0 +392,0 @@ didDocument, |
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
322914
19
2802
11
+ Addedquerystring@^0.2.1
+ Addedquerystring@0.2.1(transitive)
Updated@ethersproject/abi@^5.1.0
Updated@ethersproject/basex@^5.1.0