From bd6c06b164abbed655a3dc1f7920e1e85b8c500a Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Fri, 20 Mar 2026 15:33:48 +0800 Subject: [PATCH] refactor: remove deprecated StrKey APIs --- CHANGELOG.md | 1 + src/main/java/org/stellar/sdk/StrKey.java | 141 ----------------- .../org/stellar/sdk/DeprecatedStrKeyTest.java | 142 ------------------ .../CreateClaimableBalanceOperationTest.java | 18 +-- 4 files changed, 5 insertions(+), 297 deletions(-) delete mode 100644 src/test/java/org/stellar/sdk/DeprecatedStrKeyTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a78fd7ad6..667d89f5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - feat: sort `ScMap` entries by key in `Scv.toMap` following Soroban runtime ordering rules, as the network requires ScMap keys to be in ascending order. `Scv.toMap` now accepts `Map`; the previous `toMap(LinkedHashMap)` overload is deprecated. - feat: add `closeTime`, `headerXdr`, and `metadataXdr` to `GetLatestLedgerResponse`. - chore: bump [stellar-xdr](https://github.com/stellar/stellar-xdr) to v25.0. +- refactor!: remove deprecated `StrKey` helpers `encodeEd25519PublicKey(AccountID)`, `encodeMuxedAccount(MuxedAccount)`, `decodeMuxedAccount(String)`, `encodeToXDRAccountId(String)`, and `encodeToXDRMuxedAccount(String)`; use `StrKey.encodeEd25519PublicKey(byte[])`, `org.stellar.sdk.MuxedAccount`, and `KeyPair#getXdrAccountId()` instead. ## 2.2.3 diff --git a/src/main/java/org/stellar/sdk/StrKey.java b/src/main/java/org/stellar/sdk/StrKey.java index 25e2c8799..5173d6455 100644 --- a/src/main/java/org/stellar/sdk/StrKey.java +++ b/src/main/java/org/stellar/sdk/StrKey.java @@ -10,11 +10,6 @@ import lombok.NonNull; import lombok.Value; import org.stellar.sdk.exception.UnexpectedException; -import org.stellar.sdk.xdr.AccountID; -import org.stellar.sdk.xdr.CryptoKeyType; -import org.stellar.sdk.xdr.MuxedAccount; -import org.stellar.sdk.xdr.PublicKey; -import org.stellar.sdk.xdr.PublicKeyType; import org.stellar.sdk.xdr.Uint256; import org.stellar.sdk.xdr.Uint64; import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; @@ -351,127 +346,6 @@ public static boolean isValidMed25519PublicKey(String med25519PublicKey) { } } - /** - * Encodes raw data to strkey Stellar account ID (G...) - * - *

This method is deprecated and will be removed in future versions, use {@link - * #encodeEd25519PublicKey(byte[])} instead. - * - * @param accountID data to encode - * @return "G..." representation of the key - */ - @Deprecated - public static String encodeEd25519PublicKey(AccountID accountID) { - char[] encoded = - encodeCheck(VersionByte.ACCOUNT_ID, accountID.getAccountID().getEd25519().getUint256()); - return String.valueOf(encoded); - } - - /** - * Encodes raw data to strkey Stellar muxed account ID (M... or G...) - * - *

This method is deprecated and will be removed in future versions, use {@link - * org.stellar.sdk.MuxedAccount} instead. - * - * @param muxedAccount the muxed account to encode - * @return "M..." or "G..." representation of the key - */ - @Deprecated - public static String encodeMuxedAccount(MuxedAccount muxedAccount) { - switch (muxedAccount.getDiscriminant()) { - case KEY_TYPE_MUXED_ED25519: - return String.valueOf( - encodeCheck( - VersionByte.MED25519_PUBLIC_KEY, getMuxedEd25519AccountBytes(muxedAccount))); - case KEY_TYPE_ED25519: - return String.valueOf( - encodeCheck(VersionByte.ACCOUNT_ID, muxedAccount.getEd25519().getUint256())); - default: - throw new IllegalArgumentException("invalid discriminant"); - } - } - - /** - * Decodes strkey Stellar account ID (G...) or muxed account ID (M...) to {@link MuxedAccount}. - * - *

This method is deprecated and will be removed in future versions, use {@link - * org.stellar.sdk.MuxedAccount} instead. - * - * @param address the address to decode - * @return {@link MuxedAccount} representation of the key - */ - @Deprecated - public static MuxedAccount decodeMuxedAccount(String address) { - return encodeToXDRMuxedAccount(address); - } - - /** - * Encodes strkey Stellar account ID (G...) to {@link AccountID}. - * - *

This method is deprecated and will be removed in future versions, use {@link - * KeyPair#getXdrAccountId()} instead. - * - * @param data the data to encode - * @return {@link AccountID} representation of the key - */ - @Deprecated - public static AccountID encodeToXDRAccountId(String data) { - AccountID accountID = new AccountID(); - PublicKey publicKey = new PublicKey(); - publicKey.setDiscriminant(PublicKeyType.PUBLIC_KEY_TYPE_ED25519); - try { - publicKey.setEd25519(Uint256.fromXdrByteArray(decodeEd25519PublicKey(data))); - } catch (IOException e) { - throw new IllegalArgumentException("invalid address: " + data, e); - } - accountID.setAccountID(publicKey); - return accountID; - } - - /** - * Encodes strkey Stellar account ID (G...) or muxed account ID (M...) to {@link MuxedAccount}. - * - *

This method is deprecated and will be removed in future versions, use {@link - * org.stellar.sdk.MuxedAccount} instead. - * - * @param data the data to encode - * @return {@link MuxedAccount} representation of the key - */ - @Deprecated - public static MuxedAccount encodeToXDRMuxedAccount(String data) { - MuxedAccount muxed = new MuxedAccount(); - - if (data.isEmpty()) { - throw new IllegalArgumentException("address is empty"); - } - switch (decodeVersionByte(data)) { - case ACCOUNT_ID: - muxed.setDiscriminant(CryptoKeyType.KEY_TYPE_ED25519); - byte[] rawEd25519PublicKey = decodeEd25519PublicKey(data); - try { - muxed.setEd25519(Uint256.fromXdrByteArray(rawEd25519PublicKey)); - } catch (IOException e) { - throw new IllegalArgumentException("invalid address: " + data, e); - } - break; - case MED25519_PUBLIC_KEY: - byte[] input = decodeCheck(VersionByte.MED25519_PUBLIC_KEY, data.toCharArray()); - muxed.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519); - MuxedAccount.MuxedAccountMed25519 med = new MuxedAccount.MuxedAccountMed25519(); - try { - med.setEd25519(Uint256.fromXdrByteArray(Arrays.copyOfRange(input, 0, 32))); - med.setId(Uint64.fromXdrByteArray(Arrays.copyOfRange(input, 32, 40))); - } catch (IOException e) { - throw new IllegalArgumentException("invalid address: " + data, e); - } - muxed.setMed25519(med); - break; - default: - throw new IllegalArgumentException("Version byte is invalid"); - } - return muxed; - } - static VersionByte decodeVersionByte(String data) { byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8); byte[] decoded = base32decode(dataBytes); @@ -671,21 +545,6 @@ private static byte[] decodingTable() { return table; } - @Deprecated - private static byte[] getMuxedEd25519AccountBytes(MuxedAccount muxedAccount) { - byte[] accountBytes = muxedAccount.getMed25519().getEd25519().getUint256(); - byte[] idBytes = muxedAccount.getMed25519().getId().getUint64().getNumber().toByteArray(); - byte[] idPaddedBytes = new byte[8]; - int idNumBytesToCopy = Math.min(idBytes.length, 8); - int idCopyStartIndex = idBytes.length - idNumBytesToCopy; - System.arraycopy( - idBytes, idCopyStartIndex, idPaddedBytes, 8 - idNumBytesToCopy, idNumBytesToCopy); - byte[] result = new byte[accountBytes.length + idPaddedBytes.length]; - System.arraycopy(accountBytes, 0, result, 0, accountBytes.length); - System.arraycopy(idPaddedBytes, 0, result, accountBytes.length, idPaddedBytes.length); - return result; - } - private static byte[] removeBase32Padding(byte[] data) { // Calculate the length of unpadded data int unpaddedLength = data.length; diff --git a/src/test/java/org/stellar/sdk/DeprecatedStrKeyTest.java b/src/test/java/org/stellar/sdk/DeprecatedStrKeyTest.java deleted file mode 100644 index 78ae75ae1..000000000 --- a/src/test/java/org/stellar/sdk/DeprecatedStrKeyTest.java +++ /dev/null @@ -1,142 +0,0 @@ -package org.stellar.sdk; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.stellar.sdk.xdr.AccountID; -import org.stellar.sdk.xdr.CryptoKeyType; -import org.stellar.sdk.xdr.MuxedAccount; - -public class DeprecatedStrKeyTest { - @Test - public void testEncodeToXDRMuxedAccountMAddress() { - String unmuxedAddress = "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ"; - AccountID account = StrKey.encodeToXDRAccountId(unmuxedAddress); - - String muxedAddress = "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"; - MuxedAccount muxedAccount = StrKey.encodeToXDRMuxedAccount(muxedAddress); - assertEquals(CryptoKeyType.KEY_TYPE_MUXED_ED25519, muxedAccount.getDiscriminant()); - assertEquals(account.getAccountID().getEd25519(), muxedAccount.getMed25519().getEd25519()); - assertEquals( - -9223372036854775808L, - muxedAccount.getMed25519().getId().getUint64().getNumber().longValue()); - - assertEquals(muxedAddress, StrKey.encodeMuxedAccount(muxedAccount)); - } - - @Test - public void testEncodeAccountIdToMuxed() { - String unmuxedAddress = "GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZ"; - AccountID account = StrKey.encodeToXDRAccountId(unmuxedAddress); - - MuxedAccount muxedAccount = StrKey.encodeToXDRMuxedAccount(unmuxedAddress); - assertEquals(CryptoKeyType.KEY_TYPE_ED25519, muxedAccount.getDiscriminant()); - assertEquals(account.getAccountID().getEd25519(), muxedAccount.getEd25519()); - } - - @Test - public void testEncodeToXDRMuxedAccountInvalidAddress() { - // https://github.com/stellar/go/blob/2b876cd781b6dd0c218dcdd4f300900f87b3889e/strkey/main_test.go#L86 - try { - StrKey.encodeToXDRMuxedAccount("XBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWGTOG"); - fail(); - } catch (IllegalArgumentException e) { - assertEquals("Version byte is invalid", e.getMessage()); - } - - try { - StrKey.encodeToXDRMuxedAccount("MBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWGTOG"); - fail(); - } catch (IllegalArgumentException e) { - assertEquals("Invalid data length, expected 40 bytes, got 32", e.getMessage()); - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUR"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount("GA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVSGZA"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount("G47QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVP2I"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLKA"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "M47QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUQ"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "M47QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUQ"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUK"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJUAAAAAAAAAAAACJUO"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount(""); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount("SBCVMMCBEDB64TVJZFYJOJAERZC4YVVUOE6SYR2Y76CBTENGUSGWRRVO"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MCEO75Y6YKE53HM6N46IJYH3LK3YYFZ4QWGNUKCSSIQSH3KOAD7BEAAAAAAAAAAAPNT2W___"); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - "MDWZCOEQRODFCH6ISYQPWY67L3ULLWS5ISXYYL5GH43W7YFMTLB64AAAAAAAAAAAAHGLW==="); - fail(); - } catch (IllegalArgumentException ignored) { - } - - try { - StrKey.encodeToXDRMuxedAccount( - " MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"); - fail(); - } catch (IllegalArgumentException ignored) { - } - } -} diff --git a/src/test/java/org/stellar/sdk/operations/CreateClaimableBalanceOperationTest.java b/src/test/java/org/stellar/sdk/operations/CreateClaimableBalanceOperationTest.java index 1a83869a5..4aee32435 100644 --- a/src/test/java/org/stellar/sdk/operations/CreateClaimableBalanceOperationTest.java +++ b/src/test/java/org/stellar/sdk/operations/CreateClaimableBalanceOperationTest.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -15,16 +16,12 @@ import org.stellar.sdk.AssetTypeNative; import org.stellar.sdk.Claimant; import org.stellar.sdk.KeyPair; +import org.stellar.sdk.MuxedAccount; import org.stellar.sdk.Network; import org.stellar.sdk.Predicate; -import org.stellar.sdk.StrKey; import org.stellar.sdk.Transaction; import org.stellar.sdk.TransactionBuilder; import org.stellar.sdk.TransactionPreconditions; -import org.stellar.sdk.xdr.CryptoKeyType; -import org.stellar.sdk.xdr.MuxedAccount; -import org.stellar.sdk.xdr.Uint64; -import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; // TODO: refactor the test public class CreateClaimableBalanceOperationTest { @@ -136,17 +133,10 @@ public void testClaimableBalanceIds() throws IOException { // different sequence number changes the claimable balance id assertEquals(expectedIdSeq124, transaction.getClaimableBalanceId(0)); - // MuxedAccount muxedAccount = ; - MuxedAccount muxedAccount = new MuxedAccount(); - muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519); - MuxedAccount.MuxedAccountMed25519 med = new MuxedAccount.MuxedAccountMed25519(); - med.setEd25519(StrKey.encodeToXDRMuxedAccount(sourceAccount).getEd25519()); - med.setId(new Uint64(new XdrUnsignedHyperInteger(41l))); - muxedAccount.setMed25519(med); + MuxedAccount muxedAccount = new MuxedAccount(sourceAccount, BigInteger.valueOf(41L)); transaction = - new TransactionBuilder( - new Account(StrKey.encodeMuxedAccount(muxedAccount), 123l), Network.TESTNET) + new TransactionBuilder(new Account(muxedAccount.getAddress(), 123l), Network.TESTNET) .addOperation(op0) .addOperation(BumpSequenceOperation.builder().bumpTo(2).build()) .setTimeout(TransactionPreconditions.TIMEOUT_INFINITE)