@dao-dao/cosmiframe
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,68 +0,5 @@ | ||
import { OfflineAminoSigner, StdSignDoc, AminoSignResponse } from '@cosmjs/amino'; | ||
import { OfflineDirectSigner, AccountData, DirectSignResponse } from '@cosmjs/proto-signing'; | ||
import { RequestMethodCallMessage, ListenOptions } from './types.js'; | ||
declare class Cosmiframe { | ||
/** | ||
* Proxy object that can be used to call methods on the parent frame. This | ||
* serves as a passthrough and is a convenient alternative to using | ||
* `callParentMethod`. This should be used by the iframe. | ||
* | ||
* For example: | ||
* | ||
* const cosmiframe = new Cosmiframe() | ||
* const accounts = await cosmiframe.p.getAccounts() | ||
*/ | ||
p: { | ||
[key: string]: <T = any>(...params: any[]) => Promise<T>; | ||
}; | ||
constructor(); | ||
/** | ||
* Call a method on the parent frame. This should be used by the iframe. | ||
*/ | ||
callParentMethod<T = any>(options: Pick<RequestMethodCallMessage, 'method' | 'params'>): Promise<T>; | ||
/** | ||
* Get an offline signer with both direct and amino sign functions that | ||
* forwards requests to the parent frame. The parent frame must be listening | ||
* (using the `listen` function). This should be used by the iframe. | ||
*/ | ||
getOfflineSigner(chainId: string): IframeEitherSigner; | ||
/** | ||
* Get an offline amino signer that forwards requests to the parent frame. The | ||
* parent frame must be listening (using the `listen` function). This should | ||
* be used by the iframe. | ||
*/ | ||
getOfflineSignerAmino(chainId: string): IframeAminoSigner; | ||
/** | ||
* Get an offline direct signer that forwards requests to the parent frame. | ||
* The parent frame must be listening (using the `listen` function). This | ||
* should be used by the iframe. | ||
*/ | ||
getOfflineSignerDirect(chainId: string): IframeDirectSigner; | ||
/** | ||
* Listen for requests from the provided iframe. This should be used by the | ||
* parent. Returns a function that can be called to stop listening. | ||
*/ | ||
listen(options: ListenOptions): () => void; | ||
} | ||
declare class IframeDirectSigner implements OfflineDirectSigner { | ||
chainId: string; | ||
constructor(chainId: string); | ||
getAccounts(): Promise<readonly AccountData[]>; | ||
signDirect(signerAddress: string, signDoc: DirectSignResponse['signed']): Promise<DirectSignResponse>; | ||
} | ||
declare class IframeAminoSigner implements OfflineAminoSigner { | ||
chainId: string; | ||
constructor(chainId: string); | ||
getAccounts(): Promise<readonly AccountData[]>; | ||
signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse>; | ||
} | ||
declare class IframeEitherSigner implements OfflineDirectSigner, OfflineAminoSigner { | ||
chainId: string; | ||
constructor(chainId: string); | ||
getAccounts(): Promise<readonly AccountData[]>; | ||
signDirect(signerAddress: string, signDoc: DirectSignResponse['signed']): Promise<DirectSignResponse>; | ||
signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse>; | ||
} | ||
export { Cosmiframe, IframeAminoSigner, IframeDirectSigner, IframeEitherSigner }; | ||
import '@keplr-wallet/types'; | ||
export { C as Cosmiframe } from './client-BUACbOwI.js'; | ||
import './types.js'; | ||
import '@cosmjs/amino'; | ||
import '@cosmjs/proto-signing'; |
@@ -1,2 +0,2 @@ | ||
"use strict";var u=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var j=(t,e,n)=>e in t?u(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var d=(t,e)=>u(t,"name",{value:e,configurable:!0});var k=(t,e)=>{for(var n in e)u(t,n,{get:e[n],enumerable:!0})},z=(t,e,n,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of q(e))!S.call(t,i)&&i!==n&&u(t,i,{get:()=>e[i],enumerable:!(s=N(e,i))||s.enumerable});return t};var B=t=>z(u({},"__esModule",{value:!0}),t);var m=(t,e,n)=>(j(t,typeof e!="symbol"?e+"":e,n),n);var F={};k(F,{Cosmiframe:()=>O,IframeAminoSigner:()=>I,IframeDirectSigner:()=>l,IframeEitherSigner:()=>v});module.exports=B(F);var $=require("uuid");var c=d(t=>new Promise((e,n)=>{let s=(0,$.v4)(),i=d(({data:p})=>{if(p.id!==s)return!1;window.removeEventListener("message",i),p.type==="success"?e(p.response):n(new Error(p.error))},"listener");window.addEventListener("message",i);try{window.parent.postMessage({...t,id:s},"*")}catch(p){window.removeEventListener("message",i),n(p)}}),"callParentMethod"),E=d(t=>{if(!t||t.type==="error")return{type:"error",error:t&&t.type==="error"&&t.error||"Handled by outer wallet."};if(t.type==="success")return{type:"success",response:t.value}},"processOverrideHandler");var x=class x{constructor(){m(this,"p");this.p=new Proxy({},{get:(e,n)=>(...s)=>c({method:n.toString(),params:s})})}callParentMethod(e){return c(e)}getOfflineSigner(e){return new v(e)}getOfflineSignerAmino(e){return new I(e)}getOfflineSignerDirect(e){return new l(e)}listen(e){let{iframe:n,target:s,getOfflineSignerDirect:i,getOfflineSignerAmino:p,nonSignerOverrides:A,signerOverrides:T}=e,P=d(async({source:W,data:g})=>{if(!n.contentWindow)throw new Error("Iframe contentWindow does not exist.");if(W!==n.contentWindow||!g||typeof g!="object"||!("id"in g)||!("method"in g)||!("params"in g))return;let{id:H,params:w,chainId:y,signType:b}=g,{method:r,signerType:f}=g;f||(f=b),r=r.replace(/^signer:/,"");let h;try{if(f){if(!y)throw new Error("Missing chainId in signer message request");let o=typeof T=="function"?T():T;if(o&&r in o){let a=E(await o[r](...w));a&&(h=a)}if(!h){let a=f==="direct"?await i(y):await p(y);if(!(r in a)||typeof a[r]!="function")throw new Error(`No ${f} signer method '${r}' for chain ID '${y}'.`);h={type:"success",response:await a[r](...w)}}}else{let o=typeof A=="function"?A():A;if(o&&r in o){let a=E(await o[r](...w));a&&(h=a)}if(!h){if(!(r in s)||typeof s[r]!="function")throw new Error(`No method '${r}' on target.`);h={type:"success",response:await s[r](...w)}}}}catch(o){h={type:"error",error:o instanceof Error?o.message:`${o}`}}n.contentWindow.postMessage({...h,id:H},"*")},"listener");return window.addEventListener("message",P),()=>window.removeEventListener("message",P)}};d(x,"Cosmiframe");var O=x,M=class M{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){return c({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}async signDirect(e,n){return c({method:"signDirect",params:[e,n],chainId:this.chainId,signerType:"direct"})}};d(M,"IframeDirectSigner");var l=M,D=class D{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){return c({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}async signAmino(e,n){return c({method:"signAmino",params:[e,n],chainId:this.chainId,signerType:"amino"})}};d(D,"IframeAminoSigner");var I=D,L=class L{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){try{return await c({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}catch{return await c({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}}async signDirect(e,n){return c({method:"signDirect",params:[e,n],chainId:this.chainId,signerType:"direct"})}async signAmino(e,n){return c({method:"signAmino",params:[e,n],chainId:this.chainId,signerType:"amino"})}};d(L,"IframeEitherSigner");var v=L;0&&(module.exports={Cosmiframe,IframeAminoSigner,IframeDirectSigner,IframeEitherSigner}); | ||
"use strict";var h=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var U=(i,e,t)=>e in i?h(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var g=(i,e)=>h(i,"name",{value:e,configurable:!0});var q=(i,e)=>{for(var t in e)h(i,t,{get:e[t],enumerable:!0})},z=(i,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of k(e))!N.call(i,r)&&r!==t&&h(i,r,{get:()=>e[r],enumerable:!(n=H(e,r))||n.enumerable});return i};var B=i=>z(h({},"__esModule",{value:!0}),i);var f=(i,e,t)=>(U(i,typeof e!="symbol"?e+"":e,t),t);var F={};q(F,{Cosmiframe:()=>S});module.exports=B(F);var K=require("uuid");var o=g(i=>new Promise((e,t)=>{let n=(0,K.v4)(),r=g(({data:p})=>{if(p.id!==n)return!1;window.removeEventListener("message",r),p.type==="success"?e(p.response):t(new Error(p.error))},"listener");window.addEventListener("message",r);try{window.parent.postMessage({...i,id:n},"*")}catch(p){window.removeEventListener("message",r),t(p)}}),"callParentMethod"),x=g(i=>{if(!i||i.type==="error")return{type:"error",error:i&&i.type==="error"&&i.error||"Handled by outer wallet."};if(i.type==="success")return{type:"success",response:i.value}},"processOverrideHandler");var A=class A{constructor(e){f(this,"chainId");this.chainId=e}async getAccounts(){return o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}async signDirect(e,t){return o({method:"signDirect",params:[e,t],chainId:this.chainId,signerType:"direct"})}};g(A,"CosmiframeDirectSigner");var w=A,T=class T{constructor(e){f(this,"chainId");this.chainId=e}async getAccounts(){return o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}async signAmino(e,t){return o({method:"signAmino",params:[e,t],chainId:this.chainId,signerType:"amino"})}};g(T,"CosmiframeAminoSigner");var E=T,P=class P{constructor(e){f(this,"chainId");this.chainId=e}async getAccounts(){try{return await o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}catch{return await o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}}async signDirect(e,t){return o({method:"signDirect",params:[e,t],chainId:this.chainId,signerType:"direct"})}async signAmino(e,t){return o({method:"signAmino",params:[e,t],chainId:this.chainId,signerType:"amino"})}};g(P,"CosmiframeEitherSigner");var I=P;var M=class M{constructor(){f(this,"p");this.p=new Proxy({getEnigmaUtils:e=>({getPubkey:()=>this.p.getEnigmaPubKey(e),decrypt:(...t)=>this.p.enigmaDecrypt(e,...t),encrypt:(...t)=>this.p.enigmaEncrypt(e,...t),getTxEncryptionKey:(...t)=>this.p.getEnigmaTxEncryptionKey(e,...t)})},{get:(e,t)=>t in e&&typeof e[t]!="function"?e[t]:(...n)=>t in e&&typeof e[t]=="function"?e[t](...n):o({method:t.toString(),params:n})})}callParentMethod(e){return o(e)}getKeplrClient(){let e=new Proxy({version:"cosmiframe",mode:"extension",defaultOptions:{},getOfflineSigner:this.getOfflineSigner.bind(this),getOfflineSignerOnlyAmino:this.getOfflineSignerAmino.bind(this),getOfflineSignerAuto:t=>Promise.resolve(this.getOfflineSigner(t)),getEnigmaUtils:t=>({getPubkey:()=>e.getEnigmaPubKey(t),decrypt:(...n)=>e.enigmaDecrypt(t,...n),encrypt:(...n)=>e.enigmaEncrypt(t,...n),getTxEncryptionKey:(...n)=>e.getEnigmaTxEncryptionKey(t,...n)})},{get:(t,n)=>n in t&&typeof t[n]!="function"?t[n]:(...r)=>n in t&&typeof t[n]=="function"?t[n](...r):o({method:n.toString(),params:r})});return e}getOfflineSigner(e){return new I(e)}getOfflineSignerAmino(e){return new E(e)}getOfflineSignerDirect(e){return new w(e)}listen(e){let{iframe:t,target:n,getOfflineSignerDirect:r,getOfflineSignerAmino:p,nonSignerOverrides:v,signerOverrides:O}=e,D=g(async({source:L,data:y})=>{if(!t.contentWindow)throw new Error("Iframe contentWindow does not exist.");if(L!==t.contentWindow||!y||typeof y!="object"||!("id"in y)||!("method"in y)||!("params"in y))return;let{id:$,params:u,chainId:m,signType:W}=y,{method:s,signerType:l}=y;l||(l=W),s=s.replace(/^signer:/,"");let d;try{if(l){if(!m)throw new Error("Missing chainId in signer message request");let c=typeof O=="function"?await O():O;if(c&&s in c){let a=x(await c[s](...u));a&&(d=a)}if(!d){let a=l==="direct"?await r(m):await p(m);if(!(s in a)||typeof a[s]!="function")throw new Error(`No ${l} signer method '${s}' for chain ID '${m}'.`);d={type:"success",response:await a[s](...u)}}}else{let c=typeof v=="function"?await v():v;if(c&&s in c){let a=x(await c[s](...u));a&&(d=a)}if(!d){if(!(s in n)||typeof n[s]!="function")throw new Error(`No method '${s}' on target.`);d={type:"success",response:await n[s](...u)}}}}catch(c){d={type:"error",error:c instanceof Error?c.message:`${c}`}}t.contentWindow.postMessage({...d,id:$},"*")},"listener");return window.addEventListener("message",D),()=>window.removeEventListener("message",D)}};g(M,"Cosmiframe");var S=M;0&&(module.exports={Cosmiframe}); | ||
//# sourceMappingURL=client.js.map |
@@ -1,5 +0,6 @@ | ||
export { Cosmiframe, IframeAminoSigner, IframeDirectSigner, IframeEitherSigner } from './client.js'; | ||
export { C as Cosmiframe, b as CosmiframeAminoSigner, a as CosmiframeDirectSigner, c as CosmiframeEitherSigner } from './client-BUACbOwI.js'; | ||
export { ListenOptions, MethodCallResultMessage, MethodCallResultMessageNoId, OverrideHandler, OverrideHandlerCall, OverrideHandlerError, OverrideHandlerSuccess, Overrides, RequestMethodCallMessage, RequestMethodCallMessageNoId, SignerType } from './types.js'; | ||
export { callParentMethod, processOverrideHandler } from './utils.js'; | ||
import '@keplr-wallet/types'; | ||
import '@cosmjs/amino'; | ||
import '@cosmjs/proto-signing'; |
@@ -1,2 +0,2 @@ | ||
"use strict";var u=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var j=(r,e,n)=>e in r?u(r,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[e]=n;var d=(r,e)=>u(r,"name",{value:e,configurable:!0});var k=(r,e)=>{for(var n in e)u(r,n,{get:e[n],enumerable:!0})},z=(r,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of q(e))!S.call(r,o)&&o!==n&&u(r,o,{get:()=>e[o],enumerable:!(i=N(e,o))||i.enumerable});return r};var B=r=>z(u({},"__esModule",{value:!0}),r);var m=(r,e,n)=>(j(r,typeof e!="symbol"?e+"":e,n),n);var F={};k(F,{Cosmiframe:()=>E,IframeAminoSigner:()=>v,IframeDirectSigner:()=>I,IframeEitherSigner:()=>x,callParentMethod:()=>s,processOverrideHandler:()=>l});module.exports=B(F);var $=require("uuid");var s=d(r=>new Promise((e,n)=>{let i=(0,$.v4)(),o=d(({data:p})=>{if(p.id!==i)return!1;window.removeEventListener("message",o),p.type==="success"?e(p.response):n(new Error(p.error))},"listener");window.addEventListener("message",o);try{window.parent.postMessage({...r,id:i},"*")}catch(p){window.removeEventListener("message",o),n(p)}}),"callParentMethod"),l=d(r=>{if(!r||r.type==="error")return{type:"error",error:r&&r.type==="error"&&r.error||"Handled by outer wallet."};if(r.type==="success")return{type:"success",response:r.value}},"processOverrideHandler");var O=class O{constructor(){m(this,"p");this.p=new Proxy({},{get:(e,n)=>(...i)=>s({method:n.toString(),params:i})})}callParentMethod(e){return s(e)}getOfflineSigner(e){return new x(e)}getOfflineSignerAmino(e){return new v(e)}getOfflineSignerDirect(e){return new I(e)}listen(e){let{iframe:n,target:i,getOfflineSignerDirect:o,getOfflineSignerAmino:p,nonSignerOverrides:A,signerOverrides:T}=e,P=d(async({source:W,data:g})=>{if(!n.contentWindow)throw new Error("Iframe contentWindow does not exist.");if(W!==n.contentWindow||!g||typeof g!="object"||!("id"in g)||!("method"in g)||!("params"in g))return;let{id:H,params:w,chainId:f,signType:b}=g,{method:t,signerType:y}=g;y||(y=b),t=t.replace(/^signer:/,"");let h;try{if(y){if(!f)throw new Error("Missing chainId in signer message request");let c=typeof T=="function"?T():T;if(c&&t in c){let a=l(await c[t](...w));a&&(h=a)}if(!h){let a=y==="direct"?await o(f):await p(f);if(!(t in a)||typeof a[t]!="function")throw new Error(`No ${y} signer method '${t}' for chain ID '${f}'.`);h={type:"success",response:await a[t](...w)}}}else{let c=typeof A=="function"?A():A;if(c&&t in c){let a=l(await c[t](...w));a&&(h=a)}if(!h){if(!(t in i)||typeof i[t]!="function")throw new Error(`No method '${t}' on target.`);h={type:"success",response:await i[t](...w)}}}}catch(c){h={type:"error",error:c instanceof Error?c.message:`${c}`}}n.contentWindow.postMessage({...h,id:H},"*")},"listener");return window.addEventListener("message",P),()=>window.removeEventListener("message",P)}};d(O,"Cosmiframe");var E=O,M=class M{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){return s({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}async signDirect(e,n){return s({method:"signDirect",params:[e,n],chainId:this.chainId,signerType:"direct"})}};d(M,"IframeDirectSigner");var I=M,D=class D{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){return s({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}async signAmino(e,n){return s({method:"signAmino",params:[e,n],chainId:this.chainId,signerType:"amino"})}};d(D,"IframeAminoSigner");var v=D,L=class L{constructor(e){m(this,"chainId");this.chainId=e}async getAccounts(){try{return await s({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}catch{return await s({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}}async signDirect(e,n){return s({method:"signDirect",params:[e,n],chainId:this.chainId,signerType:"direct"})}async signAmino(e,n){return s({method:"signAmino",params:[e,n],chainId:this.chainId,signerType:"amino"})}};d(L,"IframeEitherSigner");var x=L;0&&(module.exports={Cosmiframe,IframeAminoSigner,IframeDirectSigner,IframeEitherSigner,callParentMethod,processOverrideHandler}); | ||
"use strict";var h=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var U=(r,e,t)=>e in r?h(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var g=(r,e)=>h(r,"name",{value:e,configurable:!0});var q=(r,e)=>{for(var t in e)h(r,t,{get:e[t],enumerable:!0})},z=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of k(e))!N.call(r,i)&&i!==t&&h(r,i,{get:()=>e[i],enumerable:!(n=H(e,i))||n.enumerable});return r};var B=r=>z(h({},"__esModule",{value:!0}),r);var y=(r,e,t)=>(U(r,typeof e!="symbol"?e+"":e,t),t);var F={};q(F,{Cosmiframe:()=>S,CosmiframeAminoSigner:()=>m,CosmiframeDirectSigner:()=>u,CosmiframeEitherSigner:()=>l,callParentMethod:()=>o,processOverrideHandler:()=>I});module.exports=B(F);var K=require("uuid");var o=g(r=>new Promise((e,t)=>{let n=(0,K.v4)(),i=g(({data:a})=>{if(a.id!==n)return!1;window.removeEventListener("message",i),a.type==="success"?e(a.response):t(new Error(a.error))},"listener");window.addEventListener("message",i);try{window.parent.postMessage({...r,id:n},"*")}catch(a){window.removeEventListener("message",i),t(a)}}),"callParentMethod"),I=g(r=>{if(!r||r.type==="error")return{type:"error",error:r&&r.type==="error"&&r.error||"Handled by outer wallet."};if(r.type==="success")return{type:"success",response:r.value}},"processOverrideHandler");var A=class A{constructor(e){y(this,"chainId");this.chainId=e}async getAccounts(){return o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}async signDirect(e,t){return o({method:"signDirect",params:[e,t],chainId:this.chainId,signerType:"direct"})}};g(A,"CosmiframeDirectSigner");var u=A,T=class T{constructor(e){y(this,"chainId");this.chainId=e}async getAccounts(){return o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}async signAmino(e,t){return o({method:"signAmino",params:[e,t],chainId:this.chainId,signerType:"amino"})}};g(T,"CosmiframeAminoSigner");var m=T,P=class P{constructor(e){y(this,"chainId");this.chainId=e}async getAccounts(){try{return await o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"amino"})}catch{return await o({method:"getAccounts",params:[],chainId:this.chainId,signerType:"direct"})}}async signDirect(e,t){return o({method:"signDirect",params:[e,t],chainId:this.chainId,signerType:"direct"})}async signAmino(e,t){return o({method:"signAmino",params:[e,t],chainId:this.chainId,signerType:"amino"})}};g(P,"CosmiframeEitherSigner");var l=P;var M=class M{constructor(){y(this,"p");this.p=new Proxy({getEnigmaUtils:e=>({getPubkey:()=>this.p.getEnigmaPubKey(e),decrypt:(...t)=>this.p.enigmaDecrypt(e,...t),encrypt:(...t)=>this.p.enigmaEncrypt(e,...t),getTxEncryptionKey:(...t)=>this.p.getEnigmaTxEncryptionKey(e,...t)})},{get:(e,t)=>t in e&&typeof e[t]!="function"?e[t]:(...n)=>t in e&&typeof e[t]=="function"?e[t](...n):o({method:t.toString(),params:n})})}callParentMethod(e){return o(e)}getKeplrClient(){let e=new Proxy({version:"cosmiframe",mode:"extension",defaultOptions:{},getOfflineSigner:this.getOfflineSigner.bind(this),getOfflineSignerOnlyAmino:this.getOfflineSignerAmino.bind(this),getOfflineSignerAuto:t=>Promise.resolve(this.getOfflineSigner(t)),getEnigmaUtils:t=>({getPubkey:()=>e.getEnigmaPubKey(t),decrypt:(...n)=>e.enigmaDecrypt(t,...n),encrypt:(...n)=>e.enigmaEncrypt(t,...n),getTxEncryptionKey:(...n)=>e.getEnigmaTxEncryptionKey(t,...n)})},{get:(t,n)=>n in t&&typeof t[n]!="function"?t[n]:(...i)=>n in t&&typeof t[n]=="function"?t[n](...i):o({method:n.toString(),params:i})});return e}getOfflineSigner(e){return new l(e)}getOfflineSignerAmino(e){return new m(e)}getOfflineSignerDirect(e){return new u(e)}listen(e){let{iframe:t,target:n,getOfflineSignerDirect:i,getOfflineSignerAmino:a,nonSignerOverrides:v,signerOverrides:O}=e,D=g(async({source:L,data:f})=>{if(!t.contentWindow)throw new Error("Iframe contentWindow does not exist.");if(L!==t.contentWindow||!f||typeof f!="object"||!("id"in f)||!("method"in f)||!("params"in f))return;let{id:$,params:w,chainId:E,signType:W}=f,{method:s,signerType:x}=f;x||(x=W),s=s.replace(/^signer:/,"");let d;try{if(x){if(!E)throw new Error("Missing chainId in signer message request");let c=typeof O=="function"?await O():O;if(c&&s in c){let p=I(await c[s](...w));p&&(d=p)}if(!d){let p=x==="direct"?await i(E):await a(E);if(!(s in p)||typeof p[s]!="function")throw new Error(`No ${x} signer method '${s}' for chain ID '${E}'.`);d={type:"success",response:await p[s](...w)}}}else{let c=typeof v=="function"?await v():v;if(c&&s in c){let p=I(await c[s](...w));p&&(d=p)}if(!d){if(!(s in n)||typeof n[s]!="function")throw new Error(`No method '${s}' on target.`);d={type:"success",response:await n[s](...w)}}}}catch(c){d={type:"error",error:c instanceof Error?c.message:`${c}`}}t.contentWindow.postMessage({...d,id:$},"*")},"listener");return window.addEventListener("message",D),()=>window.removeEventListener("message",D)}};g(M,"Cosmiframe");var S=M;0&&(module.exports={Cosmiframe,CosmiframeAminoSigner,CosmiframeDirectSigner,CosmiframeEitherSigner,callParentMethod,processOverrideHandler}); | ||
//# sourceMappingURL=index.js.map |
@@ -94,9 +94,9 @@ import { OfflineAminoSigner } from '@cosmjs/amino'; | ||
*/ | ||
nonSignerOverrides?: Overrides | (() => Overrides); | ||
nonSignerOverrides?: Overrides | (() => Overrides) | (() => Promise<Overrides>); | ||
/** | ||
* Overrides applied to signer message requests. | ||
*/ | ||
signerOverrides?: Overrides | (() => Overrides); | ||
signerOverrides?: Overrides | (() => Overrides) | (() => Promise<Overrides>); | ||
}; | ||
export type { ListenOptions, MethodCallResultMessage, MethodCallResultMessageNoId, OverrideHandler, OverrideHandlerCall, OverrideHandlerError, OverrideHandlerSuccess, Overrides, RequestMethodCallMessage, RequestMethodCallMessageNoId, SignerType }; |
{ | ||
"name": "@dao-dao/cosmiframe", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Facilitate iframe message passthrough for wallet connections", | ||
@@ -22,2 +22,3 @@ "author": "Noah Saso <noah@daodao.zone>", | ||
"@cosmjs/proto-signing": "^0.32.3", | ||
"@keplr-wallet/types": "^0.12.83", | ||
"@swc/core": "^1.4.16", | ||
@@ -24,0 +25,0 @@ "@types/uuid": "^9.0.8", |
@@ -12,5 +12,61 @@ # cosmiframe | ||
## Usage | ||
## App integration | ||
Integrating Cosmiframe to allow your app to be used inside iframes on other apps | ||
is very straightforward. To retrieve an offline signer for use in a signing | ||
client, which you would normally get from a wallet, get it from the `Cosmiframe` | ||
client instead: | ||
```ts | ||
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' | ||
import { Cosmiframe } from '@dao-dao/cosmiframe' | ||
const signer = new Cosmiframe().getOfflineSigner('cosmoshub-4') | ||
const client = await SigningCosmWasmClient.connectWithSigner( | ||
'https://rpc...', | ||
signer | ||
) | ||
await client.sendTokens(...) | ||
await client.execute(...) | ||
``` | ||
If you are used to interacting with Keplr's interface, which is shared by many | ||
wallets, you can retrieve a client that conforms to Keplr's interface and simply | ||
proxies requests to the parent. Then, any parents that add support for functions | ||
defined in Keplr's interface should be supported: | ||
```ts | ||
import { Cosmiframe } from '@dao-dao/cosmiframe' | ||
const keplr = new Cosmiframe().getKeplrClient() | ||
// Connect | ||
await keplr.experimentalSuggestChain(...) | ||
await keplr.enable('cosmoshub-4') | ||
const key = await keplr.getKey('cosmoshub-4') | ||
// Get signing client | ||
const signer = await keplr.getOfflineSignerOnlyAmino('cosmoshub-4') | ||
const client = await SigningCosmWasmClient.connectWithSigner( | ||
'https://rpc...', | ||
signer | ||
) | ||
await client.sendTokens(...) | ||
await client.execute(...) | ||
``` | ||
Because wallet interfaces vary slightly depending on wallet and adapter library, | ||
it's up to the parent to properly support and redirect requests. Some parents | ||
may pass requests directly through to a connected wallet, and some may handle | ||
message signature requests manually, wrapping them with other messages. | ||
## Example | ||
This example allows an iframe to interact with Keplr connected in the parent. | ||
The first step is performed in the parent app, and the second step is performed | ||
in the iframe app. | ||
@@ -37,2 +93,3 @@ 1. Begin listening from the parent window: | ||
```ts | ||
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' | ||
import { Cosmiframe } from '@dao-dao/cosmiframe' | ||
@@ -51,4 +108,6 @@ | ||
// another signing client, such as SigningCosmWasmClient. This is done through | ||
// the `getOfflineSignerAmino` and `getOfflineSignerDirect` functions, which | ||
// wrap the proxy calls above and implements the necessary interfaces. | ||
// the `getOfflineSigner`, `getOfflineSignerAmino`, and `getOfflineSignerDirect` | ||
// functions, which wrap the proxy calls above and implements the necessary | ||
// interfaces. | ||
const directAndAminoSigner = cosmiframe.getOfflineSigner('cosmoshub-4') | ||
const directSigner = cosmiframe.getOfflineSignerDirect('cosmoshub-4') | ||
@@ -59,4 +118,11 @@ const aminoSigner = cosmiframe.getOfflineSignerAmino('cosmoshub-4') | ||
'https://rpc...', | ||
directSigner | ||
aminoSigner | ||
) | ||
// If you're familiar with Keplr's interface... | ||
const keplr = cosmiframe.getKeplrClient() | ||
await keplr.experimentalSuggestChain(...) | ||
await keplr.enable('cosmoshub-4') | ||
const key = await keplr.getKey('cosmoshub-4') | ||
``` |
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
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
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
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
139165
29
279
125
15