Skip to content

Commit 4ef994e

Browse files
committed
feat: Add optional originId parameter onto links
This allows linking to a specific origin within a multi-origin tokenscript without requiring the contract & chain parameters matching that origin.
1 parent 225af35 commit 4ef994e

File tree

12 files changed

+51
-42
lines changed

12 files changed

+51
-42
lines changed

javascript/tokenscript-viewer/src/components/common/card-view/card-popover.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ export class CardPopover implements IViewBinding {
189189
currentParams.set("card", card.name);
190190

191191
const token = this.tokenScript.getCurrentTokenContext();
192-
if (token && "selectedTokenId" in token){
193-
currentParams.set("tokenId", token.selectedTokenId);
192+
if (token && card.type !== "onboarding"){
193+
currentParams.set("originId", token.originId);
194+
if ("selectedTokenId" in token)
195+
currentParams.set("tokenId", token.selectedTokenId);
194196
}
195197

196198
history.replaceState(undefined, undefined, "#" + currentParams.toString());
@@ -207,6 +209,7 @@ export class CardPopover implements IViewBinding {
207209
//this.iframe.contentWindow.location.replace("data:text/html;base64,PCFET0NUWVBFIGh0bWw+");
208210
const currentParams = new URLSearchParams(location.hash.substring(1));
209211
currentParams.delete("card");
212+
currentParams.delete("originId");
210213
currentParams.delete("tokenId");
211214
history.replaceState(undefined, undefined, "#" + currentParams.toString());
212215
}

javascript/tokenscript-viewer/src/components/viewers/alphawallet/alphawallet-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class SmartTokenStoreViewer {
8181

8282
async processUrlLoad(){
8383

84-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
84+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
8585

8686
this.app.showTsLoader();
8787

@@ -97,16 +97,16 @@ export class SmartTokenStoreViewer {
9797
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
9898
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
9999
connectEmulatorSocket(emulatorUrl, async() => {
100-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
100+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
101101
});
102102
}
103103

104-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
104+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
105105
}
106106

107-
private async loadTokenScript(chain: number, contract: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
107+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
108108

109-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, this.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
109+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, this.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
110110

111111
// Reload cards after the token is updated
112112
this.tokenScript.on("TOKENS_UPDATED", (data) => {

javascript/tokenscript-viewer/src/components/viewers/joyid-token/token-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class TokenViewer {
9797

9898
async processUrlLoad() {
9999

100-
let {query, chain, contract, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
100+
let {query, chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
101101

102102
if (!tokenId)
103103
throw new Error('Token ID was not provided in the URL');
@@ -134,17 +134,17 @@ export class TokenViewer {
134134
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
135135
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
136136
connectEmulatorSocket(emulatorUrl, async() => {
137-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
137+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
138138
});
139139
}
140140

141-
this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
141+
this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
142142
}
143143
}
144144

145-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string) {
145+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string) {
146146
try {
147-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, this.tokenDetails.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
147+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, this.tokenDetails.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
148148
this.description = await getHardcodedDescription(this.tokenScript, this.tokenDetails);
149149
} catch (e) {
150150
console.warn(e.message);

javascript/tokenscript-viewer/src/components/viewers/mooar/mooar-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class SmartTokenStoreViewer {
8282

8383
async processUrlLoad(){
8484

85-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
85+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
8686

8787
this.app.showTsLoader();
8888

@@ -98,16 +98,16 @@ export class SmartTokenStoreViewer {
9898
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
9999
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
100100
connectEmulatorSocket(emulatorUrl, async() => {
101-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
101+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
102102
});
103103
}
104104

105-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
105+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
106106
}
107107

108-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string){
108+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
109109

110-
this.tokenScript = await getTokenScriptWithProvidedTokenContext(this.app, chain, contract, scriptId, tokenId, tokenScriptUrl);
110+
this.tokenScript = await getTokenScriptWithProvidedTokenContext(this.app, chain, contract, scriptId, originId, tokenId, tokenScriptUrl);
111111

112112
// Reload cards after the token is updated
113113
this.tokenScript.on("TOKENS_UPDATED", (data) => {

javascript/tokenscript-viewer/src/components/viewers/opensea/opensea-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class OpenseaViewer {
9292

9393
async processUrlLoad(){
9494

95-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
95+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator} = getTokenUrlParams();
9696

9797
if (!tokenId)
9898
throw new Error('Token ID was not provided in the URL');
@@ -106,17 +106,17 @@ export class OpenseaViewer {
106106
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
107107
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
108108
connectEmulatorSocket(emulatorUrl, async() => {
109-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
109+
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
110110
});
111111
}
112112

113-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
113+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
114114
}
115115

116-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string){
116+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
117117

118118
try {
119-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, this.tokenDetails.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
119+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, this.tokenDetails.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
120120
} catch (e){
121121
console.error("Failed to load TokenScript", e);
122122
}

javascript/tokenscript-viewer/src/components/viewers/sts-token/sts-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class SmartTokenStoreViewer {
118118

119119
async processUrlLoad() {
120120

121-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, wallet, emulator} = getTokenUrlParams();
121+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, wallet, emulator} = getTokenUrlParams();
122122

123123
let slnAdapter;
124124

@@ -155,17 +155,17 @@ export class SmartTokenStoreViewer {
155155
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
156156
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
157157
connectEmulatorSocket(emulatorUrl, async() => {
158-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
158+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
159159
});
160160
}
161161

162-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
162+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
163163
}
164164
}
165165

166-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string) {
166+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string) {
167167

168-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, this.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
168+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, this.collectionDetails, this.tokenDetails, tokenId, tokenScriptUrl);
169169

170170
if (this.tokenScript.getMetadata().backgroundImageUrl){
171171
const body = document.getElementsByTagName("body")[0];

javascript/tokenscript-viewer/src/components/viewers/tlink-card/tlink-card-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class TlinkCardViewer {
7676

7777
async processUrlLoad(){
7878

79-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, emulator, card} = getTokenUrlParams();
79+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator, card} = getTokenUrlParams();
8080

8181
this.tokenId = tokenId;
8282
this.card = card;
@@ -87,18 +87,18 @@ export class TlinkCardViewer {
8787
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
8888
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
8989
connectEmulatorSocket(emulatorUrl, async() => {
90-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
90+
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
9191
});
9292
}
9393

94-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
94+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
9595

9696
this.app.hideTsLoader();
9797
}
9898

99-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string){
99+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
100100

101-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, null, null, tokenId, tokenScriptUrl);
101+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, null, null, tokenId, tokenScriptUrl);
102102

103103
// Reload cards after the token is updated
104104
this.tokenScript.on("TOKENS_UPDATED", (data) => {

javascript/tokenscript-viewer/src/components/viewers/tlink/tlink-viewer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class TlinkViewer {
8484

8585
async processUrlLoad(){
8686

87-
let {chain, contract, tokenId, scriptId, tokenscriptUrl, emulator, card} = getTokenUrlParams();
87+
let {chain, contract, originId, tokenId, scriptId, tokenscriptUrl, emulator, card} = getTokenUrlParams();
8888

8989
this.tokenId = tokenId;
9090
this.card = card;
@@ -95,18 +95,18 @@ export class TlinkViewer {
9595
const emulatorUrl = new URL(decodeURIComponent(emulator)).origin;
9696
tokenscriptUrl = emulatorUrl + "/tokenscript.tsml";
9797
connectEmulatorSocket(emulatorUrl, async() => {
98-
await this.loadTokenScript(chain, contract, tokenId, tokenscriptUrl);
98+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
9999
});
100100
}
101101

102-
await this.loadTokenScript(chain, contract, tokenId, scriptId, tokenscriptUrl);
102+
await this.loadTokenScript(chain, contract, originId, tokenId, scriptId, tokenscriptUrl);
103103

104104
this.app.hideTsLoader();
105105
}
106106

107-
private async loadTokenScript(chain: number, contract: string, tokenId: string, scriptId?: string, tokenScriptUrl?: string){
107+
private async loadTokenScript(chain: number, contract: string, originId?: string, tokenId?: string, scriptId?: string, tokenScriptUrl?: string){
108108

109-
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, null, null, tokenId, tokenScriptUrl);
109+
this.tokenScript = await getTokenScriptWithSingleTokenContext(this.app, chain, contract, scriptId, originId, null, null, tokenId, tokenScriptUrl);
110110

111111
// Reload cards after the token is updated
112112
this.tokenScript.on("TOKENS_UPDATED", (data) => {

javascript/tokenscript-viewer/src/components/viewers/util/getSelectedOriginTokenFromUrlParams.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import {TokenScript} from "@tokenscript/engine-js/src";
22

3-
export async function getSelectedOriginTokenFromUrlParams(tokenScript: TokenScript, chain: number, contract: string, tokenId: string|null, returnDefault = false){
3+
export async function getSelectedOriginTokenFromUrlParams(tokenScript: TokenScript, chain: number, contract: string, originId?: string, tokenId?: string, returnDefault = false){
44

55
const origins = tokenScript.getTokenOriginData();
66

7-
// TODO: Return the origin specified in the URL
8-
97
for (const origin of origins) {
8+
9+
if (originId && originId === origin.originId){
10+
return origin;
11+
}
12+
1013
if (
1114
origin.chainId === chain &&
1215
origin.contractAddress.toLowerCase() === contract.toLowerCase() &&

javascript/tokenscript-viewer/src/components/viewers/util/getTokenScriptWithProvidedTokenContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ export async function getTokenScriptWithProvidedTokenContext(
1212
chain: number,
1313
contract: string,
1414
scriptId?: string,
15+
originId?: string,
1516
tokenId?: string,
1617
tokenScriptUrl?: string
1718
){
1819

1920
let tokenScript = await getTokenScriptFromUrlParams(app, chain, contract, scriptId, tokenScriptUrl);
2021

21-
let selectedOrigin = await getSelectedOriginTokenFromUrlParams(tokenScript, chain, contract, tokenId, !tokenId);
22+
let selectedOrigin = await getSelectedOriginTokenFromUrlParams(tokenScript, chain, contract, originId, tokenId, !tokenId);
2223

2324
console.log("Selected origin: ", selectedOrigin);
2425

0 commit comments

Comments
 (0)