Skip to content

Commit

Permalink
feat: TS - Add URL attribute functionality to load remote views
Browse files Browse the repository at this point in the history
This change allows TokenScripts to use the url attribute of a view to
specify that the view should be loaded from a remote location such as IPFS.
For bigger applications, it becomes hard for the developer to embed all required
assets inside a small TokenScript; such as fonts, large graphics and other media.

This provides a different route that make it easier to make richer applications
while ensuring TokenScript TSML size remains small. It also demonstrates the
potential for existing DApps to be easily ported to TokenScript, provided
that Web3TokenView.java is updated to provide the full ethereum provider to the client.
Access to the full provider can be specified as a TokenScript view attribute,
adding an extra layer of security.
  • Loading branch information
micwallace committed Sep 25, 2023
1 parent 50fadb0 commit 229f857
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 10 additions & 5 deletions app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -166,12 +167,16 @@ private void displayFunction(String tokenAttrs)
TSAction action = functions.get(actionMethod);
String magicValues = viewModel.getAssetDefinitionService().getMagicValuesForInjection(token.tokenInfo.chainId);

String injectedView = tokenView.injectWeb3TokenInit(action.view.getTokenView(), tokenAttrs, tokenId);
injectedView = tokenView.injectJSAtEnd(injectedView, magicValues);
injectedView = tokenView.injectStyleAndWrapper(injectedView, action.style + "\n" + action.view.getStyle());
if (Objects.equals(action.view.getUrl(), "")){
String injectedView = tokenView.injectWeb3TokenInit(action.view.getTokenView(), tokenAttrs, tokenId);
injectedView = tokenView.injectJSAtEnd(injectedView, magicValues);
injectedView = tokenView.injectStyleAndWrapper(injectedView, action.style + "\n" + action.view.getStyle());

String base64 = Base64.encodeToString(injectedView.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
tokenView.loadData(base64 + (action.view.getUrlFragment() != null ? "#"+action.view.getUrlFragment() : ""), "text/html; charset=utf-8", "base64");
String base64 = Base64.encodeToString(injectedView.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
tokenView.loadData(base64 + (!Objects.equals(action.view.getUrlFragment(), "") ? "#"+action.view.getUrlFragment() : ""), "text/html; charset=utf-8", "base64");
} else {
tokenView.loadUrl(action.view.getUrl());
}
}
catch (Exception e)
{
Expand Down
10 changes: 10 additions & 0 deletions lib/src/main/java/com/alphawallet/token/entity/TSTokenView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.alphawallet.token.tools.TokenDefinition;

import java.util.Objects;

/**
* Holds an individual Token View which consists of style and HTML view code
*
Expand All @@ -30,6 +32,10 @@ public TSTokenView(Element element, TokenDefinition tokenDef)

private void generateTokenView(Element element){

if (!Objects.equals(this.getUrl(), "")){
return;
}

String lStyle = "";
String lView = "";
for (int i = 0; i < element.getChildNodes().getLength(); i++)
Expand Down Expand Up @@ -87,6 +93,10 @@ public String getStyle(){
return style;
}

public String getUrl(){
return this.element.getAttribute("url");
}

public String getUrlFragment(){
return this.element.getAttribute("urlFragment");
}
Expand Down

0 comments on commit 229f857

Please sign in to comment.