From ba05bf2d0b461d1ef844aa71eaba49eb374a2d86 Mon Sep 17 00:00:00 2001 From: Oleh Hryb Date: Thu, 8 Jun 2023 21:23:52 +0100 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20limited=20event=20"to?= =?UTF-8?q?kensupdated"=20when=20token=20really=20updated?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/outlet/index.ts | 9 ++++----- src/outlet/ticketStorage.ts | 12 ++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/outlet/index.ts b/src/outlet/index.ts index de812253..8608f6a8 100644 --- a/src/outlet/index.ts +++ b/src/outlet/index.ts @@ -228,11 +228,10 @@ export class Outlet { public async readMagicLink() { try { - await this.ticketStorage.importTicketFromMagicLink(this.urlParams) - - const event = new Event('tokensupdated') - - document.body.dispatchEvent(event) + if (await this.ticketStorage.importTicketFromMagicLink(this.urlParams)) { + const event = new Event('tokensupdated') + document.body.dispatchEvent(event) + } } catch (e) { logger(2, e) } diff --git a/src/outlet/ticketStorage.ts b/src/outlet/ticketStorage.ts index fe6a17a2..6c3ec150 100644 --- a/src/outlet/ticketStorage.ts +++ b/src/outlet/ticketStorage.ts @@ -66,7 +66,7 @@ export class TicketStorage { this.loadTickets() } - public async importTicketFromMagicLink(urlParams: URLSearchParams) { + public async importTicketFromMagicLink(urlParams: URLSearchParams): Promise { const tokenFromQuery = decodeURIComponent(urlParams.get(this.config.tokenUrlName)) const secretFromQuery = urlParams.get(this.config.tokenSecretName) const idFromQuery = urlParams.has(this.config.tokenIdName) ? urlParams.get(this.config.tokenIdName) : '' @@ -76,7 +76,7 @@ export class TicketStorage { const tokenData = await this.decodeTokenData(typeFromQuery, tokenFromQuery, true) - await this.updateOrInsertTicket({ + return await this.updateOrInsertTicket({ type: typeFromQuery, token: tokenFromQuery, id: idFromQuery, @@ -181,7 +181,7 @@ export class TicketStorage { return obj } - private async updateOrInsertTicket(tokenRecord: StoredTicketRecord) { + private async updateOrInsertTicket(tokenRecord: StoredTicketRecord): Promise { for (const [index, ticket] of this.tickets.entries()) { // Backward compatibility with old data if (!ticket.tokenId || !ticket.type) { @@ -190,13 +190,17 @@ export class TicketStorage { } if (ticket.tokenId === tokenRecord.tokenId) { + if (JSON.stringify(tokenRecord) === JSON.stringify(this.tickets[index])) { + return false + } this.tickets[index] = tokenRecord this.storeTickets() - return + return true } } this.addTicket(tokenRecord) + return true } /** From 6599c53e2e068fbe01424d298694a56bb93b3622 Mon Sep 17 00:00:00 2001 From: Nick Taras Date: Fri, 9 Jun 2023 12:55:43 +1000 Subject: [PATCH 2/5] patch fix 2.7.1 for event hook tokensupdated --- CHANGELOG.md | 13 ++++++------- README.md | 11 +++++++++++ package-lock.json | 4 ++-- package.json | 2 +- src/outlet/index.ts | 3 +++ src/version.ts | 2 +- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea979aa9..200e307a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ > Description -This feature release adds support for Ethereum Attestation Service (EAS) off chain attestations. +This patch release addresses an off chain token management scenario. ### Upgrade Steps -* Update NPM package to version 2.7.0 +* Update NPM package to version 2.7.1 ### Breaking Changes @@ -12,14 +12,13 @@ This feature release adds support for Ethereum Attestation Service (EAS) off cha ### New Features -* EAS off chain attestation support -* Opening Start Screen default copy change -* User controlled off chain signed token support / enhanced user control of token +[none] ### Bug Fixes -* Added attestation backwards compatability -* CSS alignment correction for Wallect Connect Version 2 icon +* When off chain tokens are added to the application a fix has been added to stop the 'tokensupdated' event hook from triggering when these tokens already existed. + +* Addition of this hook to the README documentation. ### Performance Improvements diff --git a/README.md b/README.md index 05dcf2e5..b090de39 100644 --- a/README.md +++ b/README.md @@ -497,6 +497,17 @@ Changing the theme. negotiator.switchTheme("dark"); ``` +### Tokens Updated Hook + +Changing the theme. + +```javascript + // Note this is a temporary solution likely to change in the next major version. + document.body.addEventListener("tokensupdated", () => { + console.log("Tokens updated event fired!!"); + }); +``` + ### When working without NPM For projects where you are not using a Node.js work flow. Or would prefer to inject the library into the html (polyfills included). diff --git a/package-lock.json b/package-lock.json index 8e819f56..f7d6f3c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tokenscript/token-negotiator", - "version": "2.6.0", + "version": "2.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tokenscript/token-negotiator", - "version": "2.6.0", + "version": "2.7.0", "license": "MIT", "dependencies": { "@onflow/fcl": "^1.3.2", diff --git a/package.json b/package.json index 39b2fa28..ce99df35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tokenscript/token-negotiator", - "version": "2.7.0", + "version": "2.7.1", "description": "Token-negotiator a token attestation bridge between web 2.0 and 3.0.", "module": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/outlet/index.ts b/src/outlet/index.ts index 8608f6a8..1fcedf98 100644 --- a/src/outlet/index.ts +++ b/src/outlet/index.ts @@ -231,6 +231,9 @@ export class Outlet { if (await this.ticketStorage.importTicketFromMagicLink(this.urlParams)) { const event = new Event('tokensupdated') document.body.dispatchEvent(event) + // TODO: tokens could be read from the hooks "tokens" and "tokens-selected" by + // triggering await this.sendTokens(this.getDataFromQuery('evtid')) at this point instead. + // Let's review this approach to confirm if this hook is required. } } catch (e) { logger(2, e) diff --git a/src/version.ts b/src/version.ts index c1662f83..b87b671c 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,2 +1,2 @@ // modified by build process. -export const VERSION = '2.6.0' +export const VERSION = '2.7.1' From a6d9c23d95d239f5d11118f3577ba2a3c5e5d33d Mon Sep 17 00:00:00 2001 From: Nick Taras Date: Fri, 9 Jun 2023 13:06:21 +1000 Subject: [PATCH 3/5] patch fix 2.7.1 for event hook tokensupdated --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 200e307a..725b98ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,6 @@ This patch release addresses an off chain token management scenario. ### Bug Fixes * When off chain tokens are added to the application a fix has been added to stop the 'tokensupdated' event hook from triggering when these tokens already existed. - * Addition of this hook to the README documentation. ### Performance Improvements From 127d295d8d8534997e6e444d1575ac79df9bab83 Mon Sep 17 00:00:00 2001 From: Nick Taras Date: Fri, 9 Jun 2023 13:07:12 +1000 Subject: [PATCH 4/5] patch fix 2.7.1 for event hook tokensupdated --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b090de39..7d6a3dae 100644 --- a/README.md +++ b/README.md @@ -502,10 +502,12 @@ negotiator.switchTheme("dark"); Changing the theme. ```javascript - // Note this is a temporary solution likely to change in the next major version. - document.body.addEventListener("tokensupdated", () => { - console.log("Tokens updated event fired!!"); - }); + +// temporary solution likely to change in the next major release version. +document.body.addEventListener("tokensupdated", () => { + console.log("Tokens updated event fired!!"); +}); + ``` ### When working without NPM From e365cb548d311eb5a566f650c1b5733fcabaaa4f Mon Sep 17 00:00:00 2001 From: Nick Taras Date: Fri, 9 Jun 2023 13:08:58 +1000 Subject: [PATCH 5/5] patch fix 2.7.1 for event hook tokensupdated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d6a3dae..5c57b6b4 100644 --- a/README.md +++ b/README.md @@ -499,7 +499,7 @@ negotiator.switchTheme("dark"); ### Tokens Updated Hook -Changing the theme. +Detect when new tokens have been added to the Token Negotiator during an applications lifecycle (for off chain tokens at this time). ```javascript