|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 3 | + |
| 4 | +export default function getKeplrProvider() { |
| 5 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 6 | + const handleRequest = (request: Record<string, any>) => |
| 7 | + new Promise((resolve, reject) => { |
| 8 | + const id = Math.random(); |
| 9 | + const handler = (e: MessageEvent) => { |
| 10 | + if (e.data.__page_req_id === id && e.data.__direction === 'to_page') { |
| 11 | + window.removeEventListener('message', handler); |
| 12 | + if (e.data.__error) { |
| 13 | + const {module, code, message} = e.data.__error; |
| 14 | + return reject({module, code, message}); |
| 15 | + } |
| 16 | + delete e.data.__page_req_id; |
| 17 | + delete e.data.__direction; |
| 18 | + resolve(Object.keys(e.data).length > 0 ? e.data : undefined); |
| 19 | + } |
| 20 | + }; |
| 21 | + |
| 22 | + window.addEventListener('message', handler); |
| 23 | + |
| 24 | + window.postMessage({ |
| 25 | + ...request, |
| 26 | + __message_type: 'rpc', |
| 27 | + __provider: 'keplr', |
| 28 | + __page_req_id: id, |
| 29 | + __direction: 'to_bg' |
| 30 | + }); |
| 31 | + }); |
| 32 | + return { |
| 33 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 34 | + enable: async function (chainIds: string | string[]) { |
| 35 | + return handleRequest({ |
| 36 | + method: `keplr_enable as string}`, |
| 37 | + params: [chainIds] |
| 38 | + }); |
| 39 | + }, |
| 40 | + getKey: async function (chainId: string) { |
| 41 | + return handleRequest({ |
| 42 | + method: 'keplr_getKey', |
| 43 | + params: [chainId] |
| 44 | + }); |
| 45 | + } |
| 46 | + }; |
| 47 | + // enable(chainIds: string | string[]): Promise<void>, |
| 48 | + // getKey(chainId: string): Promise<{ |
| 49 | + // signAmino(chainId: string, signer: string, signDoc: StdSignDoc): Promise<AminoSignResponse> |
| 50 | + // signDirect(chainId:string, signer:string, signDoc: { |
| 51 | + // sendTx( |
| 52 | + // chainId: string, |
| 53 | + // tx: Uint8Array, |
| 54 | + // mode: BroadcastMode |
| 55 | + // ): Promise<Uint8Array>; { |
| 56 | + // handleRequest({ |
| 57 | + // method: 'keplr_sendTx', |
| 58 | + // params: [[chainId, tx, mode].toJSON()] |
| 59 | + // }) |
| 60 | + // resutl =await keplerClient[method.split("_")[1]] (...params) |
| 61 | + |
| 62 | + // sendBack(result) |
| 63 | + |
| 64 | + // } |
| 65 | + // signArbitrary( |
| 66 | + // chainId: string, |
| 67 | + // signer: string, |
| 68 | + // data: string | Uint8Array |
| 69 | + // ): Promise<StdSignature>; |
| 70 | + // verifyArbitrary( |
| 71 | + // chainId: string, |
| 72 | + // signer: string, |
| 73 | + // data: string | Uint8Array, |
| 74 | + // signature: StdSignature |
| 75 | + // ): Promise<boolean>; |
| 76 | + // }; |
| 77 | + // signEthereum( |
| 78 | + // chainId: string, |
| 79 | + // signer: string, // Bech32 address, not hex |
| 80 | + // data: string | Uint8Array, |
| 81 | + // type: 'message' | 'transaction' |
| 82 | + // ) |
| 83 | +} |
0 commit comments