expo-aes-vetkeys
Version:
Hooks and components for using AES & vetkeys in Expo Web/Native applications
31 lines (28 loc) • 18.9 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const B=require("react"),ue=require("vetkeys-client-utils"),V=require("react-native"),mr=async({backend:o,transportPublicKey:n})=>{const d=performance.now(),g=await o.asymmetricKeys(n);return console.log(`Getting asymmetric keys took: ${performance.now()-d}ms`),g},Er=async({backend:o,tskSeed:n})=>{const d=new ue.TransportSecretKey(n),g=d.public_key(),{publicKey:E,encryptedAesKey:S,encryptedKey:w}=await mr({backend:o,transportPublicKey:g});return{tsk:d,publicKey:E,encryptedAesKey:S,encryptedKey:w}},br=async({encryptedAesKey:o,principal:n,encryptedKey:d,publicKey:g,tsk:E})=>{console.log("Decrypting existing AES key");const S=performance.now(),w=await ue.ibeDecrypt({ciphertext:o,principal:n,encryptedKey:d,publicKey:g,tsk:E});return console.log(`Decrypting existing AES key took: ${performance.now()-S}ms`),w},Rr=async({aesRawKey:o,principal:n,publicKey:d,seed:g})=>{console.log("Encrypting AES key");const E=performance.now(),S=await ue.ibeEncrypt({data:o,principal:n,publicKey:d,seed:g});return console.log(`Encrypting AES key took: ${performance.now()-E}ms`),S},_r=async({encryptedAesKey:o,backend:n})=>{console.log("Saving encrypted AES key to backend");const d=performance.now();await n.asymmetricSaveEncryptedAesKey(o),console.log(`Saving encrypted AES key to backend took: ${performance.now()-d}ms`)},wr=({cryptoModule:o,aesRawKeyStorage:n,identity:d,backend:g})=>{const[E,S]=B.useState(!1),w=B.useRef(void 0),O=B.useCallback(async()=>{try{if(w.current=void 0,S(!0),!d){console.log("Generating an AES key");const s=o.getRandomBytes(32);console.log("Saving locally an AES key"),await n.save(s);return}const p=d.getPrincipal().toUint8Array(),D=o.getRandomBytes(32),{tsk:R,publicKey:A,encryptedAesKey:P,encryptedKey:i}=await Er({backend:g,tskSeed:D});if(P&&i){const s=await br({encryptedAesKey:P,principal:p,encryptedKey:i,publicKey:A,tsk:R});console.log("Saving locally existing AES key"),await n.save(s);return}else{console.log("Generating an AES key");const s=o.getRandomBytes(32);console.log("Saving locally an AES key"),await n.save(s);const y=await Rr({aesRawKey:s,principal:p,publicKey:A,seed:D});await _r({encryptedAesKey:y,backend:g})}}catch(p){console.error("Failed to initialize AES key:",p),w.current=p}finally{S(!1)}},[d]);return B.useEffect(()=>{O()},[d,O]),{isProcessingAes:E,aesError:w.current}};var Q={exports:{}},M={};/**
* @license React
* react-jsx-runtime.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var Fe;function Sr(){if(Fe)return M;Fe=1;var o=B,n=Symbol.for("react.element"),d=Symbol.for("react.fragment"),g=Object.prototype.hasOwnProperty,E=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,S={key:!0,ref:!0,__self:!0,__source:!0};function w(O,p,D){var R,A={},P=null,i=null;D!==void 0&&(P=""+D),p.key!==void 0&&(P=""+p.key),p.ref!==void 0&&(i=p.ref);for(R in p)g.call(p,R)&&!S.hasOwnProperty(R)&&(A[R]=p[R]);if(O&&O.defaultProps)for(R in p=O.defaultProps,p)A[R]===void 0&&(A[R]=p[R]);return{$$typeof:n,type:O,key:P,ref:i,props:A,_owner:E.current}}return M.Fragment=d,M.jsx=w,M.jsxs=w,M}var N={};/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var Ke;function Tr(){return Ke||(Ke=1,process.env.NODE_ENV!=="production"&&function(){var o=B,n=Symbol.for("react.element"),d=Symbol.for("react.portal"),g=Symbol.for("react.fragment"),E=Symbol.for("react.strict_mode"),S=Symbol.for("react.profiler"),w=Symbol.for("react.provider"),O=Symbol.for("react.context"),p=Symbol.for("react.forward_ref"),D=Symbol.for("react.suspense"),R=Symbol.for("react.suspense_list"),A=Symbol.for("react.memo"),P=Symbol.for("react.lazy"),i=Symbol.for("react.offscreen"),s=Symbol.iterator,y="@@iterator";function _(e){if(e===null||typeof e!="object")return null;var r=s&&e[s]||e[y];return typeof r=="function"?r:null}var b=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function l(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),a=1;a<r;a++)t[a-1]=arguments[a];k("error",e,t)}}function k(e,r,t){{var a=b.ReactDebugCurrentFrame,f=a.getStackAddendum();f!==""&&(r+="%s",t=t.concat([f]));var v=t.map(function(c){return String(c)});v.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,v)}}var K=!1,C=!1,Ve=!1,Be=!1,Le=!1,ce;ce=Symbol.for("react.module.reference");function Ue(e){return!!(typeof e=="string"||typeof e=="function"||e===g||e===S||Le||e===E||e===D||e===R||Be||e===i||K||C||Ve||typeof e=="object"&&e!==null&&(e.$$typeof===P||e.$$typeof===A||e.$$typeof===w||e.$$typeof===O||e.$$typeof===p||e.$$typeof===ce||e.getModuleId!==void 0))}function Me(e,r,t){var a=e.displayName;if(a)return a;var f=r.displayName||r.name||"";return f!==""?t+"("+f+")":t}function le(e){return e.displayName||"Context"}function F(e){if(e==null)return null;if(typeof e.tag=="number"&&l("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case g:return"Fragment";case d:return"Portal";case S:return"Profiler";case E:return"StrictMode";case D:return"Suspense";case R:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case O:var r=e;return le(r)+".Consumer";case w:var t=e;return le(t._context)+".Provider";case p:return Me(e,e.render,"ForwardRef");case A:var a=e.displayName||null;return a!==null?a:F(e.type)||"Memo";case P:{var f=e,v=f._payload,c=f._init;try{return F(c(v))}catch{return null}}}return null}var I=Object.assign,L=0,fe,de,ve,ye,pe,ge,he;function me(){}me.__reactDisabledLog=!0;function Ne(){{if(L===0){fe=console.log,de=console.info,ve=console.warn,ye=console.error,pe=console.group,ge=console.groupCollapsed,he=console.groupEnd;var e={configurable:!0,enumerable:!0,value:me,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}L++}}function qe(){{if(L--,L===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:I({},e,{value:fe}),info:I({},e,{value:de}),warn:I({},e,{value:ve}),error:I({},e,{value:ye}),group:I({},e,{value:pe}),groupCollapsed:I({},e,{value:ge}),groupEnd:I({},e,{value:he})})}L<0&&l("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var ee=b.ReactCurrentDispatcher,re;function G(e,r,t){{if(re===void 0)try{throw Error()}catch(f){var a=f.stack.trim().match(/\n( *(at )?)/);re=a&&a[1]||""}return`
`+re+e}}var te=!1,X;{var Je=typeof WeakMap=="function"?WeakMap:Map;X=new Je}function Ee(e,r){if(!e||te)return"";{var t=X.get(e);if(t!==void 0)return t}var a;te=!0;var f=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var v;v=ee.current,ee.current=null,Ne();try{if(r){var c=function(){throw Error()};if(Object.defineProperty(c.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(c,[])}catch(x){a=x}Reflect.construct(e,[],c)}else{try{c.call()}catch(x){a=x}e.call(c.prototype)}}else{try{throw Error()}catch(x){a=x}e()}}catch(x){if(x&&a&&typeof x.stack=="string"){for(var u=x.stack.split(`
`),T=a.stack.split(`
`),h=u.length-1,m=T.length-1;h>=1&&m>=0&&u[h]!==T[m];)m--;for(;h>=1&&m>=0;h--,m--)if(u[h]!==T[m]){if(h!==1||m!==1)do if(h--,m--,m<0||u[h]!==T[m]){var j=`
`+u[h].replace(" at new "," at ");return e.displayName&&j.includes("<anonymous>")&&(j=j.replace("<anonymous>",e.displayName)),typeof e=="function"&&X.set(e,j),j}while(h>=1&&m>=0);break}}}finally{te=!1,ee.current=v,qe(),Error.prepareStackTrace=f}var Y=e?e.displayName||e.name:"",$=Y?G(Y):"";return typeof e=="function"&&X.set(e,$),$}function ze(e,r,t){return Ee(e,!1)}function Ge(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function H(e,r,t){if(e==null)return"";if(typeof e=="function")return Ee(e,Ge(e));if(typeof e=="string")return G(e);switch(e){case D:return G("Suspense");case R:return G("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case p:return ze(e.render);case A:return H(e.type,r,t);case P:{var a=e,f=a._payload,v=a._init;try{return H(v(f),r,t)}catch{}}}return""}var U=Object.prototype.hasOwnProperty,be={},Re=b.ReactDebugCurrentFrame;function Z(e){if(e){var r=e._owner,t=H(e.type,e._source,r?r.type:null);Re.setExtraStackFrame(t)}else Re.setExtraStackFrame(null)}function Xe(e,r,t,a,f){{var v=Function.call.bind(U);for(var c in e)if(v(e,c)){var u=void 0;try{if(typeof e[c]!="function"){var T=Error((a||"React class")+": "+t+" type `"+c+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[c]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw T.name="Invariant Violation",T}u=e[c](r,c,a,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(h){u=h}u&&!(u instanceof Error)&&(Z(f),l("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",a||"React class",t,c,typeof u),Z(null)),u instanceof Error&&!(u.message in be)&&(be[u.message]=!0,Z(f),l("Failed %s type: %s",t,u.message),Z(null))}}}var He=Array.isArray;function ne(e){return He(e)}function Ze(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function Qe(e){try{return _e(e),!1}catch{return!0}}function _e(e){return""+e}function we(e){if(Qe(e))return l("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Ze(e)),_e(e)}var Se=b.ReactCurrentOwner,er={key:!0,ref:!0,__self:!0,__source:!0},Te,Ae;function rr(e){if(U.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function tr(e){if(U.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function nr(e,r){typeof e.ref=="string"&&Se.current}function ar(e,r){{var t=function(){Te||(Te=!0,l("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function or(e,r){{var t=function(){Ae||(Ae=!0,l("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var ir=function(e,r,t,a,f,v,c){var u={$$typeof:n,type:e,key:r,ref:t,props:c,_owner:v};return u._store={},Object.defineProperty(u._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(u,"_self",{configurable:!1,enumerable:!1,writable:!1,value:a}),Object.defineProperty(u,"_source",{configurable:!1,enumerable:!1,writable:!1,value:f}),Object.freeze&&(Object.freeze(u.props),Object.freeze(u)),u};function sr(e,r,t,a,f){{var v,c={},u=null,T=null;t!==void 0&&(we(t),u=""+t),tr(r)&&(we(r.key),u=""+r.key),rr(r)&&(T=r.ref,nr(r,f));for(v in r)U.call(r,v)&&!er.hasOwnProperty(v)&&(c[v]=r[v]);if(e&&e.defaultProps){var h=e.defaultProps;for(v in h)c[v]===void 0&&(c[v]=h[v])}if(u||T){var m=typeof e=="function"?e.displayName||e.name||"Unknown":e;u&&ar(c,m),T&&or(c,m)}return ir(e,u,T,f,a,Se.current,c)}}var ae=b.ReactCurrentOwner,ke=b.ReactDebugCurrentFrame;function W(e){if(e){var r=e._owner,t=H(e.type,e._source,r?r.type:null);ke.setExtraStackFrame(t)}else ke.setExtraStackFrame(null)}var oe;oe=!1;function ie(e){return typeof e=="object"&&e!==null&&e.$$typeof===n}function Ce(){{if(ae.current){var e=F(ae.current.type);if(e)return`
Check the render method of \``+e+"`."}return""}}function ur(e){return""}var xe={};function cr(e){{var r=Ce();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=`
Check the top-level render call using <`+t+">.")}return r}}function Oe(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=cr(r);if(xe[t])return;xe[t]=!0;var a="";e&&e._owner&&e._owner!==ae.current&&(a=" It was passed a child from "+F(e._owner.type)+"."),W(e),l('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,a),W(null)}}function Pe(e,r){{if(typeof e!="object")return;if(ne(e))for(var t=0;t<e.length;t++){var a=e[t];ie(a)&&Oe(a,r)}else if(ie(e))e._store&&(e._store.validated=!0);else if(e){var f=_(e);if(typeof f=="function"&&f!==e.entries)for(var v=f.call(e),c;!(c=v.next()).done;)ie(c.value)&&Oe(c.value,r)}}}function lr(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===p||r.$$typeof===A))t=r.propTypes;else return;if(t){var a=F(r);Xe(t,e.props,"prop",a,e)}else if(r.PropTypes!==void 0&&!oe){oe=!0;var f=F(r);l("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",f||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&l("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function fr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var a=r[t];if(a!=="children"&&a!=="key"){W(e),l("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",a),W(null);break}}e.ref!==null&&(W(e),l("Invalid attribute `ref` supplied to `React.Fragment`."),W(null))}}var je={};function De(e,r,t,a,f,v){{var c=Ue(e);if(!c){var u="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(u+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var T=ur();T?u+=T:u+=Ce();var h;e===null?h="null":ne(e)?h="array":e!==void 0&&e.$$typeof===n?(h="<"+(F(e.type)||"Unknown")+" />",u=" Did you accidentally export a JSX literal instead of a component?"):h=typeof e,l("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",h,u)}var m=sr(e,r,t,f,v);if(m==null)return m;if(c){var j=r.children;if(j!==void 0)if(a)if(ne(j)){for(var Y=0;Y<j.length;Y++)Pe(j[Y],e);Object.freeze&&Object.freeze(j)}else l("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Pe(j,e)}if(U.call(r,"key")){var $=F(e),x=Object.keys(r).filter(function(hr){return hr!=="key"}),se=x.length>0?"{key: someKey, "+x.join(": ..., ")+": ...}":"{key: someKey}";if(!je[$+se]){var gr=x.length>0?"{"+x.join(": ..., ")+": ...}":"{}";l(`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,se,$,gr,$),je[$+se]=!0}}return e===g?fr(m):lr(m),m}}function dr(e,r,t){return De(e,r,t,!0)}function vr(e,r,t){return De(e,r,t,!1)}var yr=vr,pr=dr;N.Fragment=g,N.jsx=yr,N.jsxs=pr}()),N}var Ie;function Ar(){return Ie||(Ie=1,process.env.NODE_ENV==="production"?Q.exports=Sr():Q.exports=Tr()),Q.exports}var q=Ar();const kr=()=>q.jsx(V.View,{style:J.loadingContainer,children:q.jsxs(V.View,{style:J.contentContainer,children:[q.jsx(V.ActivityIndicator,{size:"large",color:"#007AFF",style:J.indicator}),q.jsx(V.Text,{style:J.loadingText,children:"Preparing Encryption..."}),q.jsx(V.Text,{style:J.hintText,children:"This may take a moment..."})]})}),J=V.StyleSheet.create({loadingContainer:{flex:1,justifyContent:"center",alignItems:"center",backgroundColor:"#fff",padding:20},contentContainer:{width:"100%",maxWidth:400,alignItems:"center",marginTop:-80,paddingBottom:20},indicator:{marginBottom:24},loadingText:{fontSize:18,fontWeight:"bold",marginBottom:16},hintText:{fontSize:16,color:"#666",fontStyle:"italic",textAlign:"center"}});var Cr=Object.defineProperty,xr=(o,n,d)=>n in o?Cr(o,n,{enumerable:!0,configurable:!0,writable:!0,value:d}):o[n]=d,$e=(o,n,d)=>xr(o,typeof n!="symbol"?n+"":n,d);function Or(o){return o&&o.__esModule&&Object.prototype.hasOwnProperty.call(o,"default")?o.default:o}var z={},We;function Pr(){if(We)return z;We=1,z.byteLength=O,z.toByteArray=D,z.fromByteArray=P;for(var o=[],n=[],d=typeof Uint8Array<"u"?Uint8Array:Array,g="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",E=0,S=g.length;E<S;++E)o[E]=g[E],n[g.charCodeAt(E)]=E;n[45]=62,n[95]=63;function w(i){var s=i.length;if(s%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var y=i.indexOf("=");y===-1&&(y=s);var _=y===s?0:4-y%4;return[y,_]}function O(i){var s=w(i),y=s[0],_=s[1];return(y+_)*3/4-_}function p(i,s,y){return(s+y)*3/4-y}function D(i){var s,y=w(i),_=y[0],b=y[1],l=new d(p(i,_,b)),k=0,K=b>0?_-4:_,C;for(C=0;C<K;C+=4)s=n[i.charCodeAt(C)]<<18|n[i.charCodeAt(C+1)]<<12|n[i.charCodeAt(C+2)]<<6|n[i.charCodeAt(C+3)],l[k++]=s>>16&255,l[k++]=s>>8&255,l[k++]=s&255;return b===2&&(s=n[i.charCodeAt(C)]<<2|n[i.charCodeAt(C+1)]>>4,l[k++]=s&255),b===1&&(s=n[i.charCodeAt(C)]<<10|n[i.charCodeAt(C+1)]<<4|n[i.charCodeAt(C+2)]>>2,l[k++]=s>>8&255,l[k++]=s&255),l}function R(i){return o[i>>18&63]+o[i>>12&63]+o[i>>6&63]+o[i&63]}function A(i,s,y){for(var _,b=[],l=s;l<y;l+=3)_=(i[l]<<16&16711680)+(i[l+1]<<8&65280)+(i[l+2]&255),b.push(R(_));return b.join("")}function P(i){for(var s,y=i.length,_=y%3,b=[],l=16383,k=0,K=y-_;k<K;k+=l)b.push(A(i,k,k+l>K?K:k+l));return _===1?(s=i[y-1],b.push(o[s>>2]+o[s<<4&63]+"==")):_===2&&(s=(i[y-2]<<8)+i[y-1],b.push(o[s>>10]+o[s>>4&63]+o[s<<2&63]+"=")),b.join("")}return z}var jr=Pr();const Ye=Or(jr);class Dr{constructor(n,d){$e(this,"storage"),$e(this,"key"),this.storage=n,this.key=d}async find(){const n=await this.storage.find(this.key);if(n)return Ye.toByteArray(n)}async retrieve(){const n=await this.find();if(!n)throw new Error(`No value found for key ${this.key}`);return n}async save(n){return this.storage.save(this.key,Ye.fromByteArray(n))}async remove(){return this.storage.remove(this.key)}}class Fr extends Dr{constructor(n){super(n,"aesRawKey")}}exports.AesProcessingView=kr;exports.AesRawKeyStorage=Fr;exports.useAesKey=wr;