safe-recovery-service-sdk
Version:
Safe Recovery service SDK by Candidelabs
2 lines (1 loc) • 7.84 kB
JavaScript
import{SocialRecoveryModule as e}from"abstractionkit";import*as t from"isomorphic-unfetch";function n(e,t,n){const r=t.split("|").map(e=>e.trim());for(let n=0;n<r.length;n++)switch(t){case"any":return;case"bigint":case"boolean":case"number":case"string":if(typeof e===t)return}const o=new Error(`invalid value for type ${t}`);throw o.code="INVALID_ARGUMENT",o.argument=`value.${n}`,o.value=e,o}function r(e,t,r){for(let o in t){let s=t[o];const i=r?r[o]:null;i&&n(s,i,o),Object.defineProperty(e,o,{enumerable:!0,value:s,writable:!1})}}function o(e,t){if(null==e)return"null";if(null==t&&(t=new Set),"object"==typeof e){if(t.has(e))return"[Circular]";t.add(e)}if(Array.isArray(e))return"[ "+e.map(e=>o(e,t)).join(", ")+" ]";if(e instanceof Uint8Array){const t="0123456789abcdef";let n="0x";for(let r=0;r<e.length;r++)n+=t[e[r]>>4],n+=t[15&e[r]];return n}if("object"==typeof e&&"function"==typeof e.toJSON)return o(e.toJSON(),t);switch(typeof e){case"boolean":case"number":case"symbol":return e.toString();case"bigint":return BigInt(e).toString();case"string":return JSON.stringify(e);case"object":{const n=Object.keys(e);return n.sort(),"{ "+n.map(n=>`${o(n,t)}: ${o(e[n],t)}`).join(", ")+" }"}}return"[ COULD NOT SERIALIZE ]"}function s(e,t,n,s){if(!e)throw function(e,t,n){let s,i=e;{const r=[];if(n){if("message"in n||"code"in n||"name"in n)throw new Error(`value will overwrite populated values: ${o(n)}`);for(const e in n)"shortMessage"!==e&&r.push(e+"="+o(n[e]))}r.push(`code=${t}`),r.push("version=6.15.0"),r.length&&(e+=" ("+r.join(", ")+")")}switch(t){case"INVALID_ARGUMENT":s=new TypeError(e);break;case"NUMERIC_FAULT":case"BUFFER_OVERRUN":s=new RangeError(e);break;default:s=new Error(e)}return r(s,{code:t}),n&&Object.assign(s,n),null==s.shortMessage&&r(s,{shortMessage:i}),s}(t,n,s)}function i(e,t,n,r){s(e,t,"INVALID_ARGUMENT",{argument:n,value:r})}["NFD","NFC","NFKD","NFKC"].reduce((e,t)=>{try{if("test"!=="test".normalize(t))throw new Error("bad");if("NFD"===t&&String.fromCharCode(233).normalize("NFD")!==String.fromCharCode(101,769))throw new Error("broken");e.push(t)}catch(e){}return e},[]);const c="0123456789abcdef",a=BigInt(0);BigInt(1);const u=9007199254740991;function f(e){let t=function(e){const t=function(e){return function(e){if(e instanceof Uint8Array)return e;if("string"==typeof e&&e.match(/^0x(?:[0-9a-f][0-9a-f])*$/i)){const t=new Uint8Array((e.length-2)/2);let n=2;for(let r=0;r<t.length;r++)t[r]=parseInt(e.substring(n,n+2),16),n+=2;return t}i(!1,"invalid BytesLike value","value",e)}(e)}(e);let n="0x";for(let e=0;e<t.length;e++){const r=t[e];n+=c[(240&r)>>4]+c[15&r]}return n}(function(e){return function(e){return!("string"!=typeof e||!e.match(/^0x[0-9A-Fa-f]*$/)||e.length%2!=0)}(e)||e instanceof Uint8Array}(e)?e:function(e){const t=function(e){const t=function(e){switch(typeof e){case"bigint":return e;case"number":return i(Number.isInteger(e),"underflow","value",e),i(e>=-u&&e<=u,"overflow","value",e),BigInt(e);case"string":try{if(""===e)throw new Error("empty string");return"-"===e[0]&&"-"!==e[1]?-BigInt(e.substring(1)):BigInt(e)}catch(t){i(!1,`invalid BigNumberish string: ${t.message}`,"value",e)}}i(!1,"invalid BigNumberish value","value",e)}(e);return s(t>=a,"unsigned value cannot be negative","NUMERIC_FAULT",{fault:"overflow",operation:"getUint",value:e}),t}(e);if(t===a)return new Uint8Array([]);let n=t.toString(16);n.length%2&&(n="0"+n);const r=new Uint8Array(n.length/2);for(let e=0;e<r.length;e++){const t=2*e;r[e]=parseInt(n.substring(t,t+2),16)}return r}(e)).substring(2);for(;t.startsWith("0");)t=t.substring(1);return""===t&&(t="0"),"0x"+t}class l extends Error{constructor(e,t,n={}){const{cause:r,errno:o,context:s}=n;super(t,{cause:r}),this.code=void 0,this.context=void 0,this.errno=void 0,this.name=this.constructor.name,this.code=e,this.errno=o,this.context=s}stringify(){return JSON.stringify(this,["name","code","message","cause","errno","context"])}}function h(e){if(e instanceof Error)return e;let t="[Unable to stringify the thrown value]";try{t=JSON.stringify(e)}catch(e){}return new Error(`This value was thrown as is, not through an Error: ${t}`)}var d;async function g(e,n,r="post",o={"Content-Type":"application/json"}){const s=t.default||t;let i,c,a;const u=JSON.stringify(n,(e,t)=>"bigint"==typeof t?"0x"+t.toString(16):t);"post"==r?(a=e,c={method:"POST",headers:o,body:u,redirect:"follow"}):(a=`${e}?${new URLSearchParams(n).toString()}`,c={method:"GET",headers:o,redirect:"follow"});try{const e=await s(a,c);i=await e.json()}catch(t){const n=h(t);throw new l("UNKNOWN_ERROR",n.message,{cause:n,context:{url:e,requestOptions:JSON.stringify(c)}})}if("code"in i)throw new l("HTTP_ERROR",i.message,{errno:i.code,context:{url:e,requestOptions:JSON.stringify(c)}});return i}!function(e){e.After3Minutes="0x949d01d424bE050D09C16025dd007CB59b3A8c66",e.After3Days="0x38275826E1933303E508433dD5f289315Da2541c",e.After7Days="0x088f6cfD8BB1dDb1BB069CCb3fc1A98927D233f2",e.After14Days="0x9BacD92F4687Db306D7ded5d4513a51EA05df25b"}(d||(d={}));class y{constructor(e,t,n){this.serviceEndpoint=void 0,this.chainId=void 0,this.recoveryModuleAddress=void 0,this.serviceEndpoint=e,this.chainId=t,this.recoveryModuleAddress=n}async getPendingRecoveryRequestsForLatestNonce(e,t){return this.getRecoveryRequestsForLatestNonce(e,t,"PENDING")}async getExecutedRecoveryRequestForLatestNonce(e,t){const n=await this.getRecoveryRequestsForLatestNonce(e,t,"EXECUTED");if(0==n.length)return null;if(1==n.length)return n[0];throw new l("BAD_DATA","getExecutedRecoveryRequestsForLatestNonce failed",{cause:h("there can be only one EXECUTED recovery per nonce")})}async getFinalizedRecoveryRequestForLatestNonce(e,t){const n=await this.getRecoveryRequestsForLatestNonce(e,t,"FINALIZED");if(0==n.length)return null;if(1==n.length)return n[0];throw new l("BAD_DATA","getFinalizedRecoveryRequestsForLatestNonce failed",{cause:h("there can be only one FINALIZED recovery per nonce")})}async getRecoveryRequestsForLatestNonce(t,n,r){let o;try{const r=new e(this.recoveryModuleAddress);o=await r.nonce(t,n)}catch(e){const r=h(e);throw new l("UNKNOWN_ERROR","failed fetching nonce",{cause:r,context:{accountAddress:n,rpcNode:t}})}if(0n==o&&"PENDING"!=r)return[];"PENDING"!=r&&(o-=1n);const s=await this.getRecoveryRequests(n,o),i=[];for(const e of s)e.status==r&&i.push(e);return i}async getRecoveryRequests(e,t){const n=`${this.serviceEndpoint}/v1/recoveries/fetchByAddress`,r=this.chainId,o=await g(n,{account:e,chainId:parseInt(r.toString()),nonce:f(t)},"get");for(const e of o)e.nonce=BigInt(e.nonce);return o}async createRecoveryRequest(e,t,n,r,o){const s=`${this.serviceEndpoint}/v1/recoveries/create`,i=this.chainId,c=await g(s,{account:e,newOwners:t,newThreshold:n,chainId:parseInt(i.toString()),signer:r,signature:o});return c.nonce=BigInt(c.nonce),c}async submitGuardianSignatureForRecoveryRequest(e,t,n){const r=`${this.serviceEndpoint}/v1/recoveries/sign`,o=await g(r,{id:e,signer:t,signature:n});if("object"==typeof o&&null!==o&&"success"in o&&"boolean"==typeof o.success)return o.success;throw new l("BAD_DATA",`${r} failed`,{cause:h(o),context:{id:e,signer:t,signature:n}})}async executeRecoveryRequest(e){const t=`${this.serviceEndpoint}/v1/recoveries/execute`,n=await g(t,{id:e});if("object"==typeof n&&null!==n&&"success"in n&&"boolean"==typeof n.success)return n.success;throw new l("BAD_DATA",`${t} failed`,{cause:h(n),context:{id:e}})}async finalizeRecoveryRequest(e){const t=`${this.serviceEndpoint}/v1/recoveries/finalize`,n=await g(t,{id:e});if("object"==typeof n&&null!==n&&"success"in n&&"boolean"==typeof n.success)return n.success;throw new l("BAD_DATA",`${t} failed`,{cause:h(n),context:{id:e}})}}var v={__proto__:null,SafeRecoveryServiceSdkError:l,RecoveryByGuardianService:y};export{y as RecoveryByGuardianService,l as SafeRecoveryServiceSdkError,v as safeRecoverySdk};