Skip to content

Commit

Permalink
Merge pull request #471 from stellar/contract-id
Browse files Browse the repository at this point in the history
Add strkey support for contract ids
  • Loading branch information
tamirms authored Mar 22, 2023
2 parents a0b27f2 + 076ff4a commit 0664195
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/java/org/stellar/sdk/StrKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public enum VersionByte {
SEED((byte)(18 << 3)), // S
PRE_AUTH_TX((byte)(19 << 3)), // T
SHA256_HASH((byte)(23 << 3)), // X
SIGNED_PAYLOAD((byte)(15 << 3)); // P
SIGNED_PAYLOAD((byte)(15 << 3)), // P

CONTRACT((byte)(2 << 3)); // C

private final byte value;
VersionByte(byte value) {
this.value = value;
Expand All @@ -53,6 +56,11 @@ public static Optional<VersionByte> findByValue(byte value) {

private static BaseEncoding base32Encoding = BaseEncoding.base32().upperCase().omitPadding();

public static String encodeContractId(byte[] data) {
char[] encoded = encodeCheck(VersionByte.CONTRACT, data);
return String.valueOf(encoded);
}

public static String encodeStellarAccountId(byte[] data) {
char[] encoded = encodeCheck(VersionByte.ACCOUNT_ID, data);
return String.valueOf(encoded);
Expand Down Expand Up @@ -176,6 +184,10 @@ public static byte[] decodeStellarAccountId(String data) {
return decodeCheck(VersionByte.ACCOUNT_ID, data.toCharArray());
}

public static byte[] decodeContractId(String data) {
return decodeCheck(VersionByte.CONTRACT, data.toCharArray());
}

public static char[] encodeStellarSecretSeed(byte[] data) {
return encodeCheck(VersionByte.SEED, data);
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/stellar/sdk/StrKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void testDecodedVersionByte() {
assertEquals(StrKey.decodeVersionByte("TAQCSRX2RIDJNHFIFHWD63X7D7D6TRT5Y2S6E3TEMXTG5W3OECHZ2OG4"), StrKey.VersionByte.PRE_AUTH_TX);
assertEquals(StrKey.decodeVersionByte("XDRPF6NZRR7EEVO7ESIWUDXHAOMM2QSKIQQBJK6I2FB7YKDZES5UCLWD"), StrKey.VersionByte.SHA256_HASH);
assertEquals(StrKey.decodeVersionByte("PDPYP7E6NEYZSVOTV6M23OFM2XRIMPDUJABHGHHH2Y67X7JL25GW6AAAAAAAAAAAAAAJEVA"), StrKey.VersionByte.SIGNED_PAYLOAD);
assertEquals(StrKey.decodeVersionByte("CA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUWDA"), StrKey.VersionByte.CONTRACT);
}

@Test()
Expand Down Expand Up @@ -173,6 +174,19 @@ public void testRoundTripSignedPayloadVersionByte() {
assertArrayEquals(data, StrKey.decodeCheck(StrKey.VersionByte.SIGNED_PAYLOAD, hashX.toCharArray()));
}

@Test
public void testRoundTripContract() {
byte[] data = rawBytes(
0x3f, 0x0c, 0x34, 0xbf, 0x93, 0xad, 0x0d, 0x99,
0x71, 0xd0, 0x4c, 0xcc, 0x90, 0xf7, 0x05, 0x51,
0x1c, 0x83, 0x8a, 0xad, 0x97, 0x34, 0xa4, 0xa2,
0xfb, 0x0d, 0x7a, 0x03, 0xfc, 0x7f, 0xe8, 0x9a);

String encoded = "CA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUWDA";
assertEquals(encoded, StrKey.encodeContractId(data));
assertArrayEquals(data, StrKey.decodeContractId(encoded));
}

@Test
public void testDecodeEmpty() {
try {
Expand Down

0 comments on commit 0664195

Please sign in to comment.