Skip to content

Commit

Permalink
feat: support in app browser injected provider
Browse files Browse the repository at this point in the history
  • Loading branch information
andybin-cdc committed May 31, 2022
1 parent 843378e commit 7a91f57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/mobile-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IDeFiConnectProvider, IDeFiConnectSessionAddresses, IJsonRpcMessage, IJsonRpcResponse, NetworkConfig } from '@deficonnect/types'
import type { IDeFiConnectProvider, IDeFiConnectSessionAddresses, IJsonRpcMessage, NetworkConfig } from '@deficonnect/types'
import { isJsonRpcResponseError, isJsonRpcResponseSuccess, payloadId, signingMethods } from '@deficonnect/utils'
import Emitter from 'events'
import { ConnectorClient, ProviderRpcError, RequestArguments } from './connect-client'
Expand Down
16 changes: 10 additions & 6 deletions packages/provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,33 @@ export class DeFiConnectProvider implements IDeFiConnectProvider {
})
}
async getProvider(): Promise<IDeFiConnectProvider> {
async function checkIsReady(times = 0) {
async function checkInjectProvider(times = 0): Promise<any> {
return new Promise((resolve) => {
function check() {
if(isDeFiConnectProvider(window.deficonnectProvider)) {
resolve(true)
resolve(window.deficonnectProvider)
return
}
if(navigator?.userAgent?.includes('DeFiWallet') && window.ethereum) {
resolve(window.ethereum)
return
}
if (times > 0 ) {
setTimeout(async () => {
--times
check()
}, 100)
return
}
resolve(false)
resolve(undefined)
}
check()
})
}
if(!this.deficonnectProvider) {
await checkIsReady(10)
if(isDeFiConnectProvider(window.deficonnectProvider)) {
this.deficonnectProvider = window.deficonnectProvider
const injectProvider = await checkInjectProvider(10)
if(injectProvider) {
this.deficonnectProvider = injectProvider
this.setupProviderEvent()
}
}
Expand Down

0 comments on commit 7a91f57

Please sign in to comment.