Skip to content

Commit

Permalink
MWA 2.0 Library RPC Changes (#560)
Browse files Browse the repository at this point in the history
* auth/reauth refactor pass 1

* get_capabilities refactor

* chains + account metadata refactor

* revert fakewallet changes (apps will updated separately)

* derp
  • Loading branch information
Funkatronics authored Oct 6, 2023
1 parent e2671f5 commit 4562678
Show file tree
Hide file tree
Showing 11 changed files with 484 additions and 93 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
public class ProtocolContract {
public static final String METHOD_AUTHORIZE = "authorize";
// METHOD_AUTHORIZE takes an optional PARAMETER_IDENTITY
public static final String PARAMETER_CLUSTER = "cluster"; // type: String (one of the CLUSTER_* values)
// METHOD_AUTHORIZE takes an optional PARAMETER_AUTH_TOKEN
// METHOD_AUTHORIZE takes an optional PARAMETER_CHAIN
// METHOD_AUTHORIZE returns a RESULT_AUTH_TOKEN
// METHOD_AUTHORIZE returns a RESULT_ACCOUNTS
// METHOD_AUTHORIZE returns an optional RESULT_WALLET_URI_BASE

public static final String METHOD_DEAUTHORIZE = "deauthorize";
// METHOD_DEAUTHORIZE takes a PARAMETER_AUTH_TOKEN

@Deprecated
public static final String METHOD_REAUTHORIZE = "reauthorize";
// METHOD_REAUTHORIZE takes an optional PARAMETER_IDENTITY
// METHOD_REAUTHORIZE takes a PARAMETER_AUTH_TOKEN
Expand All @@ -26,11 +28,14 @@ public class ProtocolContract {
// METHOD_CLONE_AUTHORIZATION returns a RESULT_AUTH_TOKEN

public static final String METHOD_GET_CAPABILITIES = "get_capabilities";
public static final String RESULT_SUPPORTS_CLONE_AUTHORIZATION = "supports_clone_authorization"; // type: Boolean
public static final String RESULT_SUPPORTS_SIGN_AND_SEND_TRANSACTIONS = "supports_sign_and_send_transactions"; // type: Boolean
public static final String RESULT_MAX_TRANSACTIONS_PER_REQUEST = "max_transactions_per_request"; // type: Number
public static final String RESULT_MAX_MESSAGES_PER_REQUEST = "max_messages_per_request"; // type: Number
public static final String RESULT_SUPPORTED_TRANSACTION_VERSIONS = "supported_transaction_versions"; // type: JSON array of any primitive datatype
public static final String RESULT_SUPPORTED_FEATURES = "features"; // type: JSON array of String (feature identifiers)
@Deprecated
public static final String RESULT_SUPPORTS_CLONE_AUTHORIZATION = "supports_clone_authorization"; // type: Boolean
@Deprecated
public static final String RESULT_SUPPORTS_SIGN_AND_SEND_TRANSACTIONS = "supports_sign_and_send_transactions"; // type: Boolean

public static final String METHOD_SIGN_TRANSACTIONS = "sign_transactions";
// METHOD_SIGN_TRANSACTIONS takes a PARAMETER_PAYLOADS
Expand All @@ -52,6 +57,11 @@ public class ProtocolContract {
public static final String PARAMETER_IDENTITY_ICON = "icon"; // type: String (relative URI)
public static final String PARAMETER_IDENTITY_NAME = "name"; // type: String

@Deprecated // alias for PARAMETER_CHAIN
public static final String PARAMETER_CLUSTER = "cluster"; // type: String (one of the CLUSTER_* values)

public static final String PARAMETER_CHAIN = "chain"; // type: String (one of the CHAIN_* values)

public static final String PARAMETER_AUTH_TOKEN = "auth_token"; // type: String

public static final String PARAMETER_PAYLOADS = "payloads"; // type: JSON array of String (base64-encoded payloads)
Expand All @@ -60,6 +70,8 @@ public class ProtocolContract {
public static final String RESULT_ACCOUNTS = "accounts"; // type: JSON array of Account
public static final String RESULT_ACCOUNTS_ADDRESS = "address"; // type: String (base64-encoded addresses)
public static final String RESULT_ACCOUNTS_LABEL = "label"; // type: String
public static final String RESULT_ACCOUNTS_CHAINS = "chains"; // type: String
// RESULT_ACCOUNTS optionally includes a RESULT_SUPPORTED_FEATURES

public static final String RESULT_WALLET_URI_BASE = "wallet_uri_base"; // type: String (absolute URI)

Expand All @@ -83,5 +95,20 @@ public class ProtocolContract {
public static final String CLUSTER_TESTNET = "testnet";
public static final String CLUSTER_DEVNET = "devnet";

public static final String CHAIN_SOLANA_MAINNET = "solana:mainnet";
public static final String CHAIN_SOLANA_TESTNET = "solana:testnet";
public static final String CHAIN_SOLANA_DEVNET = "solana:devnet";

public static final String NAMESPACE_SOLANA = "solana";

// Mandatory Features
public static final String FEATURE_ID_SIGN_MESSAGES = "solana:signAndSendTransaction";
public static final String FEATURE_ID_SIGN_TRANSACTIONS = "solana:signTransactions";

// Optional Features
public static final String FEATURE_ID_SIGN_AND_SEND_TRANSACTIONS = "solana:signAndSendTransaction";
public static final String FEATURE_ID_SIGN_IN_WITH_SOLANA = "solana:signInWithSolana";
public static final String FEATURE_ID_CLONE_AUTHORIZATION = "solana:cloneAuthorization";

private ProtocolContract() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.solana.mobilewalletadapter.common.util;

import androidx.annotation.NonNull;

import com.solana.mobilewalletadapter.common.ProtocolContract;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Identifier {

// matches "{namespace}:{reference}", no whitespace
private static final Pattern namespacedIdentifierPattern = Pattern.compile("^\\S+:\\S+$");

public static boolean isValidIdentifier(@NonNull String input) {
Matcher matcher = namespacedIdentifierPattern.matcher(input);
return matcher.find();
}

public static @NonNull String clusterToChainIdentifier(@NonNull String cluster)
throws IllegalArgumentException {
switch (cluster) {
case ProtocolContract.CLUSTER_MAINNET_BETA:
return ProtocolContract.CHAIN_SOLANA_MAINNET;
case ProtocolContract.CLUSTER_TESTNET:
return ProtocolContract.CHAIN_SOLANA_TESTNET;
case ProtocolContract.CLUSTER_DEVNET:
return ProtocolContract.CHAIN_SOLANA_DEVNET;
default:
throw new IllegalArgumentException("input is not a valid solana cluster");
}
}

private Identifier() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class AuthRecord {
public final String accountLabel;

@NonNull
public final String chain;

@NonNull @Deprecated
public final String cluster;

@NonNull
Expand All @@ -53,7 +56,7 @@ public class AuthRecord {
@NonNull IdentityRecord identity,
@NonNull byte[] publicKey,
@Nullable String accountLabel,
@NonNull String cluster,
@NonNull String chain,
@NonNull byte[] scope,
@Nullable Uri walletUriBase,
@IntRange(from = 1) int publicKeyId,
Expand All @@ -66,7 +69,8 @@ public class AuthRecord {
this.identity = identity;
this.publicKey = publicKey;
this.accountLabel = accountLabel;
this.cluster = cluster;
this.chain = chain;
this.cluster = chain;
this.scope = scope;
this.walletUriBase = walletUriBase;
this.publicKeyId = publicKeyId;
Expand Down Expand Up @@ -112,7 +116,7 @@ public String toString() {
"id=" + id +
", identity=" + identity +
", publicKey=" + Arrays.toString(publicKey) +
", cluster='" + cluster + '\'' +
", chain='" + chain + '\'' +
", scope=" + Arrays.toString(scope) +
", walletUriBase='" + walletUriBase + '\'' +
", issued=" + issued +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import androidx.annotation.NonNull;
import androidx.annotation.Size;

import com.solana.mobilewalletadapter.common.ProtocolContract;
import com.solana.mobilewalletadapter.common.util.Identifier;

public class MobileWalletAdapterConfig {
public static final String LEGACY_TRANSACTION_VERSION = "legacy";

@Deprecated
public final boolean supportsSignAndSendTransactions;

@IntRange(from = 0)
Expand All @@ -27,6 +31,10 @@ public class MobileWalletAdapterConfig {
@Size(min = 1)
public final Object[] supportedTransactionVersions;

@NonNull
public final String[] optionalFeatures;

@Deprecated
public MobileWalletAdapterConfig(boolean supportsSignAndSendTransactions,
@IntRange(from = 0) int maxTransactionsPerSigningRequest,
@IntRange(from = 0) int maxMessagesPerSigningRequest,
Expand All @@ -36,6 +44,8 @@ public MobileWalletAdapterConfig(boolean supportsSignAndSendTransactions,
this.maxTransactionsPerSigningRequest = maxTransactionsPerSigningRequest;
this.maxMessagesPerSigningRequest = maxMessagesPerSigningRequest;
this.noConnectionWarningTimeoutMs = noConnectionWarningTimeoutMs;
this.optionalFeatures = supportsSignAndSendTransactions
? new String[] { ProtocolContract.FEATURE_ID_SIGN_AND_SEND_TRANSACTIONS } : new String[] {};

for (Object o : supportedTransactionVersions) {
if (!((o instanceof String) && LEGACY_TRANSACTION_VERSION.equals((String)o)) &&
Expand All @@ -45,4 +55,35 @@ public MobileWalletAdapterConfig(boolean supportsSignAndSendTransactions,
}
this.supportedTransactionVersions = supportedTransactionVersions;
}

public MobileWalletAdapterConfig(@IntRange(from = 0) int maxTransactionsPerSigningRequest,
@IntRange(from = 0) int maxMessagesPerSigningRequest,
@NonNull @Size(min = 1) Object[] supportedTransactionVersions,
@IntRange(from = 0) long noConnectionWarningTimeoutMs,
@NonNull String[] supportedFeatures) {
this.maxTransactionsPerSigningRequest = maxTransactionsPerSigningRequest;
this.maxMessagesPerSigningRequest = maxMessagesPerSigningRequest;
this.noConnectionWarningTimeoutMs = noConnectionWarningTimeoutMs;

for (Object o : supportedTransactionVersions) {
if (!((o instanceof String) && LEGACY_TRANSACTION_VERSION.equals((String)o)) &&
!((o instanceof Integer) && ((Integer)o >= 0))) {
throw new IllegalArgumentException("supportedTransactionVersions must be either the string \"legacy\" or a non-negative integer");
}
}
this.supportedTransactionVersions = supportedTransactionVersions;

boolean supportsSignAndSendTransactions = false;
for (String featureId : supportedFeatures) {
if (!Identifier.isValidIdentifier(featureId)) {
throw new IllegalArgumentException("supportedFeatures must be a valid namespaced feature identifier of the form '{namespace}:{reference}'");
}
if (featureId.equals(ProtocolContract.FEATURE_ID_SIGN_AND_SEND_TRANSACTIONS)) {
supportsSignAndSendTransactions = true;
break;
}
}
this.supportsSignAndSendTransactions = supportsSignAndSendTransactions;
this.optionalFeatures = supportedFeatures;
}
}
Loading

0 comments on commit 4562678

Please sign in to comment.