Skip to content

Commit

Permalink
Merge pull request #227 from alephium/announce-provider
Browse files Browse the repository at this point in the history
Announce the injected provider
  • Loading branch information
Lbqds authored Nov 19, 2024
2 parents 3793ee9 + aef76e0 commit f16a74d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 0 additions & 6 deletions packages/extension/src/inpage/alephiumWindowObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ export const alephiumWindowObject: AlephiumWindowObject =
throw Error("User aborted")
}

const { alephiumProviders } = window
const alephium = alephiumProviders?.alephium
if (!alephium) {
throw Error("No alephium object detected")
}

const account = {
address: walletAccount.address,
publicKey: walletAccount.signer.publicKey,
Expand Down
19 changes: 11 additions & 8 deletions packages/extension/src/inpage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ function attachNostrProvider() {
}
}

function announceProvider() {
const event = new CustomEvent('announceAlephiumProvider', {
detail: Object.freeze({ provider: alephiumWindowObject })
})
const handler = () => window.dispatchEvent(event)
handler()
window.addEventListener('requestAlephiumProvider', handler)
}

function attach() {
announceProvider()
attachAlephiumProvider()
attachNostrProvider()
}
Expand All @@ -62,15 +72,8 @@ attach()
window.addEventListener(
"message",
async ({ data }: MessageEvent<WindowMessageType>) => {
const { alephiumProviders } = window
const alephium = alephiumProviders?.alephium

if (!alephium) {
return
}

if (data.type === "ALPH_DISCONNECT_ACCOUNT") {
await alephium.disconnect()
await alephiumWindowObject.disconnect()
}
},
)
5 changes: 5 additions & 0 deletions packages/extension/src/inpage/inpage.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ declare global {
alephiumProviders?: Record<string, AlephiumWindowObject>
nostr?: NostrObject
}

interface WindowEventMap {
'announceAlephiumProvider': CustomEvent,
'requestAlephiumProvider': Event
}
}

export interface NostrObject {
Expand Down

0 comments on commit f16a74d

Please sign in to comment.