Skip to content

Commit

Permalink
feat(EWM-353): tvm provider announcer
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Komarov committed Oct 17, 2024
1 parent 5a2ff15 commit 681c2bd
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
2 changes: 1 addition & 1 deletion assets/main.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion assets/notifications.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="node" />
import { EventEmitter } from "events";
export declare class NotificationEmitter {
emitter?: EventEmitter;
Expand Down
1 change: 0 additions & 1 deletion assets/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="node" />
import { EventEmitter } from 'events';
import { PendingJsonRpcResponse } from './jrpc';
export type Maybe<T> = Partial<T> | null | undefined;
Expand Down
53 changes: 34 additions & 19 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"clean": "rm -rf node_modules"
},
"devDependencies": {
"@types/events": "3.0.1",
"everscale-inpage-provider": "0.3.65",
"ts-loader": "9.4.4",
"typescript": "5.2.2",
"@types/events": "3.0.3",
"everscale-inpage-provider": "0.3.66",
"ts-loader": "9.5.1",
"typescript": "5.6.3",
"webpack-cli": "5.1.4"
},
"dependencies": {
Expand Down
33 changes: 33 additions & 0 deletions js/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ type InitializeProviderOptions = {
shouldSetOnWindow?: boolean
}

interface TVMAnnounceProviderEvent extends CustomEvent {
type: 'tvm:announceProvider';
detail: TVMProviderDetail;
}

interface TVMProviderDetail {
info: TVMProviderInfo;
provider: NekotonInpageProvider;
}

interface TVMProviderInfo {
name: string;
}

export const initializeProvider = ({
logger = console,
maxEventListeners = 100,
Expand Down Expand Up @@ -50,6 +64,25 @@ export function setGlobalProvider(
// TODO: remove later
;(window as Record<string, any>).ton = providerInstance
window.dispatchEvent(new Event('ton#initialized'))

const announceEvent = new CustomEvent<TVMProviderDetail>('tvm:announceProvider', {
detail: Object.freeze({
info: {
name: 'SparX Wallet',
},
provider: providerInstance,
}),
}) as TVMAnnounceProviderEvent

// The Wallet dispatches an announce event which is heard by
// the DApp code that had run earlier
window.dispatchEvent(announceEvent)

// The Wallet listens to the request events which may be
// dispatched later and re-dispatches the `TVMAnnounceProviderEvent`
window.addEventListener('tvm:requestProvider', () => {
window.dispatchEvent(announceEvent)
})
}

interface UnvalidatedJsonRpcRequest {
Expand Down

0 comments on commit 681c2bd

Please sign in to comment.