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

Boxes: Add convenience methods for encoding Box names #346

Merged
merged 3 commits into from
Jul 12, 2022
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.algorand.algosdk.transaction;

import com.algorand.algosdk.util.BoxQueryEncoding;

import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -46,4 +48,8 @@ public String toString() {
", name=" + Arrays.toString(name) +
'}';
}

public String nameQueryEncoded() {
return BoxQueryEncoding.encodeBytes(this.getName());
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/algorand/algosdk/util/BoxQueryEncoding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.algorand.algosdk.util;

import com.algorand.algosdk.transaction.AppBoxReference;
import com.algorand.algosdk.transaction.Transaction;
import com.algorand.algosdk.v2.client.model.Box;

/**
* BoxQueryEncoding provides convenience methods to String encode box names for use with Box search APIs (e.g. GetApplicationBoxByName).
*/
public final class BoxQueryEncoding {

private static final String ENCODING_BASE64_PREFIX = "b64:";

public static String encodeBytes(byte[] xs) {
return ENCODING_BASE64_PREFIX + Encoder.encodeToBase64(xs);
}

public static String encodeBox(Box b) {
return encodeBytes(b.name);
}

public static String encodeBoxReference(Transaction.BoxReference br) {
return encodeBytes(br.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class TestAppBoxReference {

private AppBoxReference genConstant() {
return new AppBoxReference(Long.valueOf(5), "example".getBytes(StandardCharsets.US_ASCII));
return new AppBoxReference(5, "example".getBytes(StandardCharsets.US_ASCII));
}

@Test
Expand All @@ -20,4 +20,17 @@ public void testEqualsByReference() {
public void testHashCode() {
Assert.assertEquals(genConstant().hashCode(), genConstant().hashCode());
}

@Test
public void testNameQueryEncoding() {
String example = "tkÿÿ";
String expectedEncoding = "b64:dGvDv8O/";

AppBoxReference abr = new AppBoxReference(0, example.getBytes(StandardCharsets.UTF_8));

Assert.assertEquals(
expectedEncoding,
abr.nameQueryEncoded()
);
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/algorand/algosdk/util/TestBoxQueryEncoding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.algorand.algosdk.util;

import com.algorand.algosdk.transaction.AppBoxReference;
import com.algorand.algosdk.transaction.Transaction;
import com.algorand.algosdk.v2.client.model.Box;
import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class TestBoxQueryEncoding {

private static class Example {
final String source;
final String expectedEncoding;

public Example(String source, String expectedEncoding) {
this.source = source;
this.expectedEncoding = "b64:" + expectedEncoding;
}
}

private final Example e = new Example("tkÿÿ", "dGvDv8O/");

@Test
public void testEncodeBytes() {
Assert.assertEquals(
e.expectedEncoding,
BoxQueryEncoding.encodeBytes(e.source.getBytes(StandardCharsets.UTF_8))
);
}

@Test
public void testEncodeBox() {
Box b = new Box();
b.name(Encoder.encodeToBase64(e.source.getBytes(StandardCharsets.UTF_8)));

Assert.assertEquals(
e.expectedEncoding,
BoxQueryEncoding.encodeBox(b)
);
}

@Test
public void testEncodeBoxReference() {
Transaction.BoxReference br = new Transaction.BoxReference(0, e.source.getBytes(StandardCharsets.UTF_8));

Assert.assertEquals(
e.expectedEncoding,
BoxQueryEncoding.encodeBoxReference(br)
);
}
}