Skip to content

Commit

Permalink
fix: connect dapp
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Jan 30, 2023
1 parent a191a9e commit 1858b3d
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import { ProviderBase } from '@onekeyfe/cross-inpage-provider-core'
import { ProviderCardanoBase } from './ProviderCardanoBase'
import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
Expand All @@ -8,6 +11,8 @@ import { Cbor, Bytes, Cip30DataSignature, Cip30Wallet, NetworkId, Paginate, Wall
import * as TypeUtils from './type-utils'


export const NAMI_TARGET = 'nami-wallet';

export type CardanoRequest = WalletApi & {
connect: () => Promise<{ account: string }>
// override the type of the request method
Expand Down Expand Up @@ -187,7 +192,13 @@ class ProviderCardano extends ProviderCardanoBase implements IProviderCardano {

signData: (addr: Cbor, payload: Bytes) => this.signData(addr, payload),

submitTx: (tx: Cbor) => this.submitTx(tx)
submitTx: (tx: Cbor) => this.submitTx(tx),

experimental: {
on: (eventName: string, callback: (detail: any) => void) => this.namiOn(eventName, callback),
off: () => this.namiOff(),
getCollateral: () => this.getCollateral(),
},
}

if (!this.account) {
Expand Down Expand Up @@ -285,6 +296,26 @@ class ProviderCardano extends ProviderCardanoBase implements IProviderCardano {
params: tx
})
}

/**
* @param {string} eventName
* @param {Function} callback
*/
namiOn(eventName: string, callback: (detail: any) => void) {
const handler = (event: any) => callback(event.detail);

// @ts-ignore
const events = window.cardano.nami._events[eventName] || [];

// @ts-ignore
window.cardano.nami._events[eventName] = [...events, [callback, handler]];

window.addEventListener(`${NAMI_TARGET}${eventName}`, handler);
}

namiOff() {
// empty
}
}

export {ProviderCardano}

0 comments on commit 1858b3d

Please sign in to comment.