Skip to content

Commit

Permalink
Improve IPFS path check
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Apr 3, 2024
1 parent 2076c58 commit 921e7c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/com/alphawallet/app/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ public static boolean isContractCall(Context context, String operationName)
private static final String IPFS_DESIGNATOR = "/ipfs/";
public static final String IPFS_INFURA_RESOLVER = "https://alphawallet.infura-ipfs.io";
public static final String IPFS_IO_RESOLVER = "https://ipfs.io";
public static final String IPFS_MATCHER = "^Qm[1-9A-Za-z]{44}(\\/.*)?$";

public static boolean isIPFS(String url)
{
Expand Down Expand Up @@ -977,9 +978,10 @@ else if (shouldBeIPFS(URL)) //have seen some NFTs designating only the IPFS hash
return parsed;
}

private static boolean shouldBeIPFS(String url)
public static boolean shouldBeIPFS(String url)
{
return url.startsWith("Qm") && url.length() == 46 && !url.contains(".") && !url.contains("/");
Matcher regexResult = Pattern.compile(IPFS_MATCHER).matcher(url);
return regexResult.find();
}

public static String loadFile(Context context, @RawRes int rawRes)
Expand Down
17 changes: 17 additions & 0 deletions app/src/test/java/com/alphawallet/app/IPFSServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.alphawallet.app.entity.QueryResponse;
import com.alphawallet.app.service.IPFSService;
import com.alphawallet.app.service.IPFSServiceType;
import com.alphawallet.app.util.Utils;

import org.junit.Test;

Expand Down Expand Up @@ -70,5 +71,21 @@ public void testUrls() throws Exception
assertTrue(qr.isSuccessful());

//TODO: Check update; pass an out of date header to TS repo endpoint

assertTrue(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq"));
assertTrue(Utils.isIPFS("ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq"));
assertTrue(Utils.isIPFS("ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/uouo"));
assertTrue(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq"));
assertTrue(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/"));
assertTrue(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/1"));
assertTrue(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/some/path"));
assertTrue(Utils.isIPFS("/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/uouo"));
assertTrue(Utils.isIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/some/path"));


assertFalse(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtqwooslkj"));
assertFalse(Utils.shouldBeIPFS("QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWt"));
assertFalse(Utils.shouldBeIPFS("eSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtdd"));
assertFalse(Utils.isIPFS("ipff://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/uouo"));
}
}

0 comments on commit 921e7c8

Please sign in to comment.