Skip to content

Commit

Permalink
feat: support eip-6963 (#1446)
Browse files Browse the repository at this point in the history
* feat: support eip-6963

* feat: version

* fix: test case

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mosshqq and mergify[bot] authored Nov 29, 2024
1 parent a3ab734 commit 1bd0ee2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .yarn/versions/b7e895ef.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
releases:
browser-extension: patch
helios-inpage: patch

declined:
- helios
18 changes: 18 additions & 0 deletions packages/inpage/eip-6963.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const EIP6963EventNames = {
Announce: 'eip6963:announceProvider',
Request: 'eip6963:requestProvider',
}

export function announceProvider(providerDetail) {
const _announceProvider = () =>
window.dispatchEvent(
new CustomEvent(EIP6963EventNames.Announce, {
detail: Object.freeze(providerDetail),
}),
)

_announceProvider()
window.addEventListener(EIP6963EventNames.Request, _announceProvider)
return () =>
window.removeEventListener(EIP6963EventNames.Request, _announceProvider)
}
1 change: 1 addition & 0 deletions packages/inpage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@fluent-wallet/extension-runtime": "workspace:packages/extension-runtime",
"@fluent-wallet/provider-api": "workspace:packages/ui/provider-api",
"@thi.ng/rstream": "6.0.21",
"uuid": "^8.3.2",
"webextension-polyfill": "0.8.0"
}
}
28 changes: 23 additions & 5 deletions packages/inpage/setup-inpage-provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {rpcStream} from '@fluent-wallet/extension-runtime/rpc-stream.js'
import {initProvider} from '@fluent-wallet/provider-api'
import {takeOver} from './take-over-portal'
import {announceProvider} from './eip-6963'
import {v4 as uuid} from 'uuid'

const FLUENT_SVG =
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iOTYiIGhlaWdodD0iOTYiIHZpZXdCb3g9IjAgMCA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 +79,31 @@ function setupProvider() {
})

PROVIDER = initProvider(stream, sendToBg)
announceProvider({
info: {
uuid: uuid(),
name: 'Fluent',
icon: FLUENT_SVG,
rdns: 'com.fluentwallet',
},
provider: PROVIDER,
})

window.fluent = PROVIDER
if (!window.ethereum) window.ethereum = PROVIDER
Object.defineProperty(window, 'conflux', {value: PROVIDER, writable: false})
takeOver(PROVIDER, 'cfx')

if (window.localStorage.getItem(FLUENT_OVERRIDE_WINDOW_DOT_ETHEREUM)) {
Object.defineProperty(window, 'ethereum', {
value: PROVIDER,
writable: false,
})
takeOver(PROVIDER, 'eth')
try {
Object.defineProperty(window, 'ethereum', {
value: PROVIDER,
writable: false,
})
takeOver(PROVIDER, 'eth')
} catch (error) {
console.log('Failed to override window.ethereum', error)
}
}
return PROVIDER
}
Expand Down
13 changes: 8 additions & 5 deletions packages/inpage/setup-inpage-provider.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import {expect, describe, it, vi} from 'vitest'
import {EIP6963EventNames} from './eip-6963.js'
describe('inpage', () => {
describe('setupProvider', () => {
it('should setup the provider on window.conflux', async () => {
let listener, eventName
let listeners = [],
eventNames = []
window.addEventListener = vi.fn((e, l) => {
eventName = e
listener = l
eventNames.push(e)
listeners.push(l)
})
expect(window.conflux).toBe(undefined)
await import('./index.js')
expect(typeof listener).toBe('function')
expect(eventName).toBe('message')
listeners.forEach(l => expect(typeof l).toBe('function'))
expect(eventNames[0]).toBe('message')
expect(eventNames[1]).toBe(EIP6963EventNames.Request)
expect(window.conflux).toBeDefined()
})

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18252,6 +18252,7 @@ __metadata:
"@fluent-wallet/extension-runtime": "workspace:packages/extension-runtime"
"@fluent-wallet/provider-api": "workspace:packages/ui/provider-api"
"@thi.ng/rstream": "npm:6.0.21"
uuid: "npm:^8.3.2"
webextension-polyfill: "npm:0.8.0"
languageName: unknown
linkType: soft
Expand Down

0 comments on commit 1bd0ee2

Please sign in to comment.