Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TokenScript handling #3411

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@
<activity
android:name=".ui.TokenFunctionActivity"
android:hardwareAccelerated="true"
android:label="@string/token_function" />
android:label="@string/token_function"
android:windowSoftInputMode="adjustResize"/>

<activity
android:name=".ui.FunctionActivity"
android:hardwareAccelerated="true"
android:label="@string/token_function" />
android:label="@string/token_function"
android:windowSoftInputMode="adjustResize"/>

<activity
android:name=".ui.WalletActionsActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2287,7 +2287,7 @@ public Single<Map<BigInteger, List<String>>> fetchFunctionMap(Token token, @NotN
ContractType type, UpdateType update)
{
return Single.fromCallable(() -> {
ActionModifier requiredActionModifier = type == ContractType.ATTESTATION ? ActionModifier.ATTESTATION : ActionModifier.NONE;
List<ActionModifier> modifiers = getAllowedTypes(type);
Map<BigInteger, List<String>> validActions = new HashMap<>();
TokenDefinition td = getAssetDefinition(token);
if (td != null)
Expand All @@ -2306,7 +2306,7 @@ public Single<Map<BigInteger, List<String>>> fetchFunctionMap(Token token, @NotN
for (String actionName : actions.keySet())
{
TSAction action = actions.get(actionName);
if (action == null || action.modifier != requiredActionModifier)
if (action == null || !modifiers.contains(action.modifier))
{
continue; //do not include attestations if this isn't an attestation fetch
}
Expand Down Expand Up @@ -2342,6 +2342,23 @@ public Single<Map<BigInteger, List<String>>> fetchFunctionMap(Token token, @NotN
});
}

private List<ActionModifier> getAllowedTypes(ContractType type)
{
List<ActionModifier> modifiers = new ArrayList<>();
switch (type)
{
case ATTESTATION -> {
modifiers.add(ActionModifier.ATTESTATION);
}
default -> {
modifiers.add(ActionModifier.NONE);
modifiers.add(ActionModifier.ACTIVITY);
}
}

return modifiers;
}

private void addIntrinsicAttributes(Map<String, TokenScriptResult.Attribute> attrs, Token token, BigInteger tokenId)
{
//add tokenId, ownerAddress & contractAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.alphawallet.app.widget.SignTransactionDialog;
import com.alphawallet.ethereum.EthereumNetworkBase;
import com.alphawallet.hardware.SignatureFromKey;
import com.alphawallet.token.entity.ActionModifier;
import com.alphawallet.token.entity.Attribute;
import com.alphawallet.token.entity.EthereumMessage;
import com.alphawallet.token.entity.MethodArg;
Expand Down Expand Up @@ -280,7 +281,6 @@ private void onAttr(TokenScriptResult.Attribute attribute)

private void fillEmpty()
{
findViewById(R.id.layout_webwrapper).setVisibility(View.VISIBLE);
tokenView.loadData("<html><body>No Data</body></html>", "text/html", "utf-8");
}

Expand Down Expand Up @@ -339,7 +339,10 @@ private void initViewModel()
private void setupFunctions()
{
FunctionButtonBar functionBar = findViewById(R.id.layoutButtons);
functionBar.setupFunctionList(this, actionMethod);
if (action != null && action.modifier != ActionModifier.ACTIVITY)
{
functionBar.setupFunctionList(this, actionMethod);
}
}

@Override
Expand Down Expand Up @@ -661,7 +664,6 @@ public void onPageLoaded(WebView view)
@Override
public void onPageRendered(WebView view)
{
findViewById(R.id.layout_webwrapper).setVisibility(View.VISIBLE);
if (parsePass == 1)
{
tokenView.reload();
Expand Down
28 changes: 6 additions & 22 deletions app/src/main/res/layout/activity_script_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,14 @@

<include layout="@layout/layout_simple_toolbar" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/layoutButtons"
android:layout_below="@id/toolbar">

<LinearLayout
android:id="@id/token_view"
<com.alphawallet.app.web3.Web3TokenView
android:id="@+id/web3_tokenview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">

<include layout="@layout/item_ticket" />

<com.alphawallet.app.widget.ActivityHistoryList
android:id="@+id/history_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />

</LinearLayout>
android:layout_height="match_parent"
android:layout_above="@id/layoutButtons"
android:layout_below="@id/toolbar">

</ScrollView>
</com.alphawallet.app.web3.Web3TokenView>

<LinearLayout
android:id="@+id/layout_success_overlay"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
public enum ActionModifier
{
NONE,
ATTESTATION
ATTESTATION,
ACTIVITY
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class TokenDefinition
public TokenscriptContext context;
public String holdingToken = null;
private int actionCount;
private TSOrigins defaultOrigin = null;

public static final String TOKENSCRIPT_MINIMUM_SCHEMA = "2020/06";
public static final String TOKENSCRIPT_CURRENT_SCHEMA = "2024/01";
Expand Down Expand Up @@ -542,8 +543,9 @@ private void extractTags(Element token) throws Exception
switch (element.getLocalName())
{
case "origins":
TSOrigins origin = parseOrigins(element); //parseOrigins(element);
TSOrigins origin = parseOrigins(element);
if (origin.isType(TSOriginType.Contract) || origin.isType(TSOriginType.Attestation)) holdingToken = origin.getOriginName();
defaultOrigin = origin;
break;
case "contract":
handleAddresses(element);
Expand Down Expand Up @@ -658,6 +660,7 @@ private TSActivityView processActivityView(Element card) throws Exception
{
NodeList ll = card.getChildNodes();
TSActivityView activityView = null;
String useName = "";

for (int j = 0; j < ll.getLength(); j++)
{
Expand All @@ -670,12 +673,25 @@ private TSActivityView processActivityView(Element card) throws Exception
{
case "origins":
TSOrigins origins = parseOrigins(element);
if (origins.isType(TSOriginType.Event)) activityView = new TSActivityView(origins);
if (origins.isType(TSOriginType.Event))
{
activityView = new TSActivityView(origins);
}
break;
case "view": //TODO: Localisation
case "item-view":
if (activityView == null) throw new SAXException("Activity card declared without origins tag");
activityView.addView(node.getLocalName(), new TSTokenView(element, this));
if (activityView == null)
{
activityView = new TSActivityView(defaultOrigin);
}
if (useName.isEmpty())
{
useName = node.getLocalName();
}
activityView.addView(useName, new TSTokenView(element, this));
break;
case "label":
useName = getLocalisedString(element);
break;
default:
throw new SAXException("Unknown tag <" + node.getLocalName() + "> tag in tokens");
Expand Down Expand Up @@ -767,7 +783,6 @@ else if (thisDate.before(schemaDate))

public boolean isSchemaLessThanMinimum()
{

if (nameSpace == null)
{
return true;
Expand Down Expand Up @@ -806,14 +821,15 @@ private void extractCard(Element card) throws Exception
tokenViews.views.put(tv.getLabel(), tv);
break;
case "action":
case "activity":
action = handleAction(card);
actions.put(action.name, action);
setModifier(action, card);
break;
case "activity":
/*case "activity":
activity = processActivityView(card);
activityCards.put(card.getAttribute("name"), activity);
break;
break;*/
case "onboarding":
// do not parse onboarding cards
break;
Expand All @@ -839,6 +855,16 @@ private void setModifier(TSAction action, Element card) throws Exception
default:
throw new SAXException("Unexpected modifier found: " + modifier);
}

String type = card.getAttribute("type");
switch (type)
{
case "activity":
action.modifier = ActionModifier.ACTIVITY;
break;
default:
break;
}
}

private TSAction handleAction(Element action) throws Exception
Expand Down
Loading