Skip to content

Commit

Permalink
Transaction event of provider(6/6)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Apr 10, 2024
1 parent 07bfa8e commit f1ade97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ window.addEventListener('kaspa:requestProviders', () => {
})

window.addEventListener('kaspa:connect', (event) => {
// TODO: Dont trust to events from clients
const extensionId = (event as CustomEvent<string>).detail

if (chrome.runtime.id !== extensionId) return
Expand All @@ -37,7 +38,7 @@ window.addEventListener('kaspa:connect', (event) => {
})

port.onDisconnect.addListener(() => {
console.error('port disconnected wtf...')
window.dispatchEvent(new CustomEvent("kaspa:disconnect"))
})
})

Expand Down
2 changes: 2 additions & 0 deletions src/wallet/kaspa/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export default class Account extends EventEmitter {
for (const transaction of this.pendingTxs) {
await transaction.submit(this.processor.rpc)
}

this.emit('transaction', "") // waiting for aspects changes
}

private registerProcessor() {
Expand Down
22 changes: 11 additions & 11 deletions src/wallet/messaging/wallet/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import browser from "webextension-polyfill"
import type { Request } from "@/provider/protocol"
import type { Request, Event, EventMappings } from "@/provider/protocol"
import Account from "@/wallet/kaspa/account"
import { EventEmitter } from "events"

Expand All @@ -12,6 +12,8 @@ export default class Provider extends EventEmitter {
super()

this.account = account

this.account.on('transaction', (hash) => this.handleEvent('transaction', hash))
}

get connectedURL () {
Expand Down Expand Up @@ -40,17 +42,11 @@ export default class Provider extends EventEmitter {
if (!this.ports.get(url)) throw Error('No port found')

this.connection = this.ports.get(url)!

const onMessageListener = (request: Request) => {
// TODO: Dont trust calls

this.handleMessage(request)
}

this.connection.onMessage.addListener(onMessageListener)
this.connection.onMessage.addListener(this.handleMessage)

this.connection.onDisconnect.addListener(() => {
this.connection!.onMessage.removeListener(onMessageListener)
this.connection!.onMessage.removeListener(this.handleMessage)
})

this.connection.postMessage({
Expand All @@ -73,14 +69,18 @@ export default class Provider extends EventEmitter {
delete this.connection
}

private handleEvent <E extends keyof EventMappings>(event: E, data: EventMappings[E]) {
if (!this.connection) return

this.connection.postMessage(event)
}

private async handleMessage (request: Request) {
if (request.method === 'send') {
await this.openPopup('send', {
'recipient': request.params[0],
'amount': request.params[1]
})

// TODO: notifications...
}
}

Expand Down

0 comments on commit f1ade97

Please sign in to comment.