|
| 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 | +} |
| 48 | +/* TODO |
| 49 | + signAmino(chainId: string, signer: string, signDoc: StdSignDoc): Promise<AminoSignResponse> |
| 50 | + signDirect(chainId:string, signer:string, signDoc: { |
| 51 | + sendTx(chainId: string, tx: Uint8Array, mode: BroadcastMode): Promise<Uint8Array>; |
| 52 | + sendBack(result) |
| 53 | + signArbitrary(chainId: string, signer: string, data: string | Uint8Array): Promise<StdSignature>; |
| 54 | + verifyArbitrary(chainId: string, signer: string, data: string | Uint8Array, signature: StdSignature): Promise<boolean>; |
| 55 | + signEthereum(chainId: string, signer: string, // Bech32 address, not hex data: string | Uint8Array, type: 'message' | 'transaction' |
| 56 | +*/ |
0 commit comments