safe-recovery-service-sdk
Version:
Safe Recovery service SDK by Candidelabs
2 lines (1 loc) • 11.1 kB
JavaScript
import{SocialRecoveryModule as e}from"abstractionkit";import*as t from"isomorphic-unfetch";function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function n(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return r(e,t);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function i(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(i=function(){return!!e})()}function c(e,t){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},c(e,t)}function s(e){var t="function"==typeof Map?new Map:void 0;return s=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(i())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,t);var o=new(e.bind.apply(e,n));return r&&c(o,r.prototype),o}(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),c(r,e)},s(e)}function u(e,t,r){const n=t.split("|").map(e=>e.trim());for(let r=0;r<n.length;r++)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.${r}`,o.value=e,o}function a(e,t,r){for(let n in t){let o=t[n];const i=r?r[n]:null;i&&u(o,i,n),Object.defineProperty(e,n,{enumerable:!0,value:o,writable:!1})}}function f(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=>f(e,t)).join(", ")+" ]";if(e instanceof Uint8Array){const t="0123456789abcdef";let r="0x";for(let n=0;n<e.length;n++)r+=t[e[n]>>4],r+=t[15&e[n]];return r}if("object"==typeof e&&"function"==typeof e.toJSON)return f(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 r=Object.keys(e);return r.sort(),"{ "+r.map(r=>`${f(r,t)}: ${f(e[r],t)}`).join(", ")+" }"}}return"[ COULD NOT SERIALIZE ]"}function l(e,t,r,n){if(!e)throw function(e,t,r){let n,o=e;{const n=[];if(r){if("message"in r||"code"in r||"name"in r)throw new Error(`value will overwrite populated values: ${f(r)}`);for(const e in r)"shortMessage"!==e&&n.push(e+"="+f(r[e]))}n.push(`code=${t}`),n.push("version=6.15.0"),n.length&&(e+=" ("+n.join(", ")+")")}switch(t){case"INVALID_ARGUMENT":n=new TypeError(e);break;case"NUMERIC_FAULT":case"BUFFER_OVERRUN":n=new RangeError(e);break;default:n=new Error(e)}return a(n,{code:t}),r&&Object.assign(n,r),null==n.shortMessage&&a(n,{shortMessage:o}),n}(t,r,n)}function h(e,t,r,n){l(e,t,"INVALID_ARGUMENT",{argument:r,value:n})}["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 v="0123456789abcdef",d=BigInt(0);BigInt(1);const y=9007199254740991;function g(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 r=2;for(let n=0;n<t.length;n++)t[n]=parseInt(e.substring(r,r+2),16),r+=2;return t}h(!1,"invalid BytesLike value","value",e)}(e)}(e);let r="0x";for(let e=0;e<t.length;e++){const n=t[e];r+=v[(240&n)>>4]+v[15&n]}return r}(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 h(Number.isInteger(e),"underflow","value",e),h(e>=-y&&e<=y,"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){h(!1,`invalid BigNumberish string: ${t.message}`,"value",e)}}h(!1,"invalid BigNumberish value","value",e)}(e);return l(t>=d,"unsigned value cannot be negative","NUMERIC_FAULT",{fault:"overflow",operation:"getUint",value:e}),t}(e);if(t===d)return new Uint8Array([]);let r=t.toString(16);r.length%2&&(r="0"+r);const n=new Uint8Array(r.length/2);for(let e=0;e<n.length;e++){const t=2*e;n[e]=parseInt(r.substring(t,t+2),16)}return n}(e)).substring(2);for(;t.startsWith("0");)t=t.substring(1);return""===t&&(t="0"),"0x"+t}var p=/*#__PURE__*/function(e){function t(t,r,n){var o;void 0===n&&(n={});var i=n.errno,c=n.context;return(o=e.call(this,r,{cause:n.cause})||this).code=void 0,o.context=void 0,o.errno=void 0,o.name=o.constructor.name,o.code=t,o.errno=i,o.context=c,o}var r,n;return n=e,(r=t).prototype=Object.create(n.prototype),r.prototype.constructor=r,c(r,n),t.prototype.stringify=function(){return JSON.stringify(this,["name","code","message","cause","errno","context"])},t}(/*#__PURE__*/s(Error));function m(e){if(e instanceof Error)return e;var 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 b,w=function(e,r,n,o){void 0===n&&(n="post"),void 0===o&&(o={"Content-Type":"application/json"});try{var i,c,s,u=function(t){if("code"in i)throw new p("HTTP_ERROR",i.message,{errno:i.code,context:{url:e,requestOptions:JSON.stringify(c)}});return i},a=t.default||t,f=JSON.stringify(r,function(e,t){return"bigint"==typeof t?"0x"+t.toString(16):t});if("post"==n)s=e,c={method:"POST",headers:o,body:f,redirect:"follow"};else{var l=new URLSearchParams(r);s=e+"?"+l.toString(),c={method:"GET",headers:o,redirect:"follow"}}var h=function(e,t){try{var r=Promise.resolve(a(s,c)).then(function(e){return Promise.resolve(e.json()).then(function(e){i=e})})}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}(0,function(t){var r=m(t);throw new p("UNKNOWN_ERROR",r.message,{cause:r,context:{url:e,requestOptions:JSON.stringify(c)}})});return Promise.resolve(h&&h.then?h.then(u):u())}catch(e){return Promise.reject(e)}};!function(e){e.After3Minutes="0x949d01d424bE050D09C16025dd007CB59b3A8c66",e.After3Days="0x38275826E1933303E508433dD5f289315Da2541c",e.After7Days="0x088f6cfD8BB1dDb1BB069CCb3fc1A98927D233f2",e.After14Days="0x9BacD92F4687Db306D7ded5d4513a51EA05df25b"}(b||(b={}));var R=/*#__PURE__*/function(){function t(e,t,r){this.serviceEndpoint=void 0,this.chainId=void 0,this.recoveryModuleAddress=void 0,this.serviceEndpoint=e,this.chainId=t,this.recoveryModuleAddress=r}var r=t.prototype;return r.getPendingRecoveryRequestsForLatestNonce=function(e,t){try{return Promise.resolve(this.getRecoveryRequestsForLatestNonce(e,t,"PENDING"))}catch(e){return Promise.reject(e)}},r.getExecutedRecoveryRequestForLatestNonce=function(e,t){try{return Promise.resolve(this.getRecoveryRequestsForLatestNonce(e,t,"EXECUTED")).then(function(e){if(0==e.length)return null;if(1==e.length)return e[0];throw new p("BAD_DATA","getExecutedRecoveryRequestsForLatestNonce failed",{cause:m("there can be only one EXECUTED recovery per nonce")})})}catch(e){return Promise.reject(e)}},r.getFinalizedRecoveryRequestForLatestNonce=function(e,t){try{return Promise.resolve(this.getRecoveryRequestsForLatestNonce(e,t,"FINALIZED")).then(function(e){if(0==e.length)return null;if(1==e.length)return e[0];throw new p("BAD_DATA","getFinalizedRecoveryRequestsForLatestNonce failed",{cause:m("there can be only one FINALIZED recovery per nonce")})})}catch(e){return Promise.reject(e)}},r.getRecoveryRequestsForLatestNonce=function(t,r,o){try{var i,c=function(e){return 0n==i&&"PENDING"!=o?[]:("PENDING"!=o&&(i-=1n),Promise.resolve(s.getRecoveryRequests(r,i)).then(function(e){for(var t,r=[],i=n(e);!(t=i()).done;){var c=t.value;c.status==o&&r.push(c)}return r}))},s=this,u=function(n,o){try{var c=(u=new e(s.recoveryModuleAddress),Promise.resolve(u.nonce(t,r)).then(function(e){i=e}))}catch(e){return o(e)}var u;return c&&c.then?c.then(void 0,o):c}(0,function(e){var n=m(e);throw new p("UNKNOWN_ERROR","failed fetching nonce",{cause:n,context:{accountAddress:r,rpcNode:t}})});return Promise.resolve(u&&u.then?u.then(c):c())}catch(e){return Promise.reject(e)}},r.getRecoveryRequests=function(e,t){try{return Promise.resolve(w(this.serviceEndpoint+"/v1/recoveries/fetchByAddress",{account:e,chainId:parseInt(this.chainId.toString()),nonce:g(t)},"get")).then(function(e){for(var t,r=n(e);!(t=r()).done;){var o=t.value;o.nonce=BigInt(o.nonce)}return e})}catch(e){return Promise.reject(e)}},r.createRecoveryRequest=function(e,t,r,n,o){try{return Promise.resolve(w(this.serviceEndpoint+"/v1/recoveries/create",{account:e,newOwners:t,newThreshold:r,chainId:parseInt(this.chainId.toString()),signer:n,signature:o})).then(function(e){return e.nonce=BigInt(e.nonce),e})}catch(e){return Promise.reject(e)}},r.submitGuardianSignatureForRecoveryRequest=function(e,t,r){try{var n=this.serviceEndpoint+"/v1/recoveries/sign";return Promise.resolve(w(n,{id:e,signer:t,signature:r})).then(function(o){if("object"==typeof o&&null!==o&&"success"in o&&"boolean"==typeof o.success)return o.success;throw new p("BAD_DATA",n+" failed",{cause:m(o),context:{id:e,signer:t,signature:r}})})}catch(e){return Promise.reject(e)}},r.executeRecoveryRequest=function(e){try{var t=this.serviceEndpoint+"/v1/recoveries/execute";return Promise.resolve(w(t,{id:e})).then(function(r){if("object"==typeof r&&null!==r&&"success"in r&&"boolean"==typeof r.success)return r.success;throw new p("BAD_DATA",t+" failed",{cause:m(r),context:{id:e}})})}catch(e){return Promise.reject(e)}},r.finalizeRecoveryRequest=function(e){try{var t=this.serviceEndpoint+"/v1/recoveries/finalize";return Promise.resolve(w(t,{id:e})).then(function(r){if("object"==typeof r&&null!==r&&"success"in r&&"boolean"==typeof r.success)return r.success;throw new p("BAD_DATA",t+" failed",{cause:m(r),context:{id:e}})})}catch(e){return Promise.reject(e)}},t}(),A={__proto__:null,SafeRecoveryServiceSdkError:p,RecoveryByGuardianService:R};export{R as RecoveryByGuardianService,p as SafeRecoveryServiceSdkError,A as safeRecoverySdk};