Skip to content

Commit

Permalink
make StrKey public. (#548)
Browse files Browse the repository at this point in the history
We will prudently make the following methods public:

encodeEd25519PublicKey
decodeEd25519PublicKey
encodeEd25519SecretSeed
decodeEd25519SecretSeed
encodePreAuthTx
decodePreAuthTx
encodeSha256Hash
decodeSha256Hash
encodeSignedPayload
decodeSignedPayload
encodeContract
decodeContract
  • Loading branch information
overcat authored Oct 7, 2023
1 parent eb8eae8 commit 1831c4e
Show file tree
Hide file tree
Showing 27 changed files with 472 additions and 148 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
As this project is pre 1.0, breaking changes may happen for minor version bumps. A breaking change will get clearly notified in this log.

## Pending
* Make `StrKey` public, this allows users to conveniently encode and decode Stellar keys to/from strings. ([#548](https://github.com/stellar/java-stellar-sdk/pull/548))

## 0.41.1
* Add `org.stellar.sdk.spi.SdkProvider`, users can implement this interface to provide their own implementation of the SDK. We provide an [Android specific implementation](https://github.com/stellar/java-stellar-sdk-android-spi), if you are integrating this SDK into an Android project, be sure to check it out. ([#543](https://github.com/stellar/java-stellar-sdk/pull/543))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/AccountConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public String decode(MuxedAccount account) {
return StrKey.encodeStellarMuxedAccount(account);
}

return StrKey.encodeStellarAccountId(StrKey.muxedAccountToAccountId(account));
return StrKey.encodeEd25519PublicKey(StrKey.muxedAccountToAccountId(account));
}
}
20 changes: 10 additions & 10 deletions src/main/java/org/stellar/sdk/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class Address {
* @param address the StrKey encoded format of Stellar public key or contract ID.
*/
public Address(String address) {
if (StrKey.isValidStellarAccountId(address)) {
if (StrKey.isValidEd25519PublicKey(address)) {
this.type = AddressType.ACCOUNT;
this.key = StrKey.decodeStellarAccountId(address);
} else if (StrKey.isValidContractId(address)) {
this.key = StrKey.decodeEd25519PublicKey(address);
} else if (StrKey.isValidContract(address)) {
this.type = AddressType.CONTRACT;
this.key = StrKey.decodeContractId(address);
this.key = StrKey.decodeContract(address);
} else {
throw new IllegalArgumentException("Unsupported address type");
}
Expand All @@ -41,7 +41,7 @@ public Address(String address) {
* @return a new {@link Address} object from the given Stellar public key.
*/
public static Address fromAccount(byte[] accountId) {
return new Address(StrKey.encodeStellarAccountId(accountId));
return new Address(StrKey.encodeEd25519PublicKey(accountId));
}

/**
Expand All @@ -51,7 +51,7 @@ public static Address fromAccount(byte[] accountId) {
* @return a new {@link Address} object from the given Stellar Contract ID.
*/
public static Address fromContract(byte[] contractId) {
return new Address(StrKey.encodeContractId(contractId));
return new Address(StrKey.encodeContract(contractId));
}

/**
Expand All @@ -63,9 +63,9 @@ public static Address fromContract(byte[] contractId) {
public static Address fromSCAddress(SCAddress scAddress) {
switch (scAddress.getDiscriminant()) {
case SC_ADDRESS_TYPE_ACCOUNT:
return new Address(StrKey.encodeStellarAccountId(scAddress.getAccountId()));
return new Address(StrKey.encodeEd25519PublicKey(scAddress.getAccountId()));
case SC_ADDRESS_TYPE_CONTRACT:
return new Address(StrKey.encodeContractId(scAddress.getContractId().getHash()));
return new Address(StrKey.encodeContract(scAddress.getContractId().getHash()));
default:
throw new IllegalArgumentException("Unsupported address type");
}
Expand Down Expand Up @@ -142,9 +142,9 @@ public AddressType getAddressType() {
public String toString() {
switch (this.type) {
case ACCOUNT:
return StrKey.encodeStellarAccountId(this.key);
return StrKey.encodeEd25519PublicKey(this.key);
case CONTRACT:
return StrKey.encodeContractId(this.key);
return StrKey.encodeContract(this.key);
default:
throw new IllegalArgumentException("Unsupported address type");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static class Builder {
private String mSourceAccount;

Builder(AllowTrustOp op) {
trustor = StrKey.encodeStellarAccountId(op.getTrustor());
trustor = StrKey.encodeEd25519PublicKey(op.getTrustor());
switch (op.getAsset().getDiscriminant()) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 =
Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum4().getIssuer());
accountId = StrKey.encodeEd25519PublicKey(xdr.getAlphaNum4().getIssuer());
return new AssetTypeCreditAlphaNum4(assetCode4, accountId);
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 =
Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum12().getIssuer());
accountId = StrKey.encodeEd25519PublicKey(xdr.getAlphaNum12().getIssuer());
return new AssetTypeCreditAlphaNum12(assetCode12, accountId);
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static class Builder {
* @param op {@link BeginSponsoringFutureReservesOp}
*/
Builder(BeginSponsoringFutureReservesOp op) {
sponsoredId = StrKey.encodeStellarAccountId(op.getSponsoredID());
sponsoredId = StrKey.encodeEd25519PublicKey(op.getSponsoredID());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/ChangeTrustAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public static ChangeTrustAsset fromXdr(org.stellar.sdk.xdr.ChangeTrustAsset xdr)
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 =
Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum4().getIssuer());
accountId = StrKey.encodeEd25519PublicKey(xdr.getAlphaNum4().getIssuer());
return ChangeTrustAsset.create(new AssetTypeCreditAlphaNum4(assetCode4, accountId));
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 =
Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
accountId = StrKey.encodeStellarAccountId(xdr.getAlphaNum12().getIssuer());
accountId = StrKey.encodeEd25519PublicKey(xdr.getAlphaNum12().getIssuer());
return ChangeTrustAsset.create(new AssetTypeCreditAlphaNum12(assetCode12, accountId));
case ASSET_TYPE_POOL_SHARE:
return new LiquidityPoolShareChangeTrustAsset(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/CreateAccountOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static class Builder {
* @param op {@link CreateAccountOp}
*/
Builder(CreateAccountOp op) {
destination = StrKey.encodeStellarAccountId(op.getDestination());
destination = StrKey.encodeEd25519PublicKey(op.getDestination());
startingBalance = Operation.fromXdrAmount(op.getStartingBalance().getInt64().longValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static class Builder {
for (org.stellar.sdk.xdr.Claimant c : op.getClaimants()) {
claimants.add(
new Claimant(
StrKey.encodeStellarAccountId(c.getV0().getDestination()),
StrKey.encodeEd25519PublicKey(c.getV0().getDestination()),
Predicate.fromXdr(c.getV0().getPredicate())));
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/stellar/sdk/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean canSign() {
* @return {@link KeyPair}
*/
public static KeyPair fromSecretSeed(char[] seed) {
byte[] decoded = StrKey.decodeStellarSecretSeed(seed);
byte[] decoded = StrKey.decodeEd25519SecretSeed(seed);
KeyPair keypair = fromSecretSeed(decoded);
return keypair;
}
Expand All @@ -87,7 +87,7 @@ public static KeyPair fromSecretSeed(char[] seed) {
*/
public static KeyPair fromSecretSeed(String seed) {
char[] charSeed = seed.toCharArray();
byte[] decoded = StrKey.decodeStellarSecretSeed(charSeed);
byte[] decoded = StrKey.decodeEd25519SecretSeed(charSeed);
KeyPair keypair = fromSecretSeed(decoded);
Arrays.fill(charSeed, ' ');
return keypair;
Expand All @@ -113,7 +113,7 @@ public static KeyPair fromSecretSeed(byte[] seed) {
* @return {@link KeyPair}
*/
public static KeyPair fromAccountId(String accountId) {
byte[] decoded = StrKey.decodeStellarAccountId(accountId);
byte[] decoded = StrKey.decodeEd25519PublicKey(accountId);
return fromPublicKey(decoded);
}

Expand Down Expand Up @@ -163,12 +163,12 @@ public static KeyPair random() {

/** Returns the human readable account ID encoded in strkey. */
public String getAccountId() {
return StrKey.encodeStellarAccountId(mPublicKey.getAbyte());
return StrKey.encodeEd25519PublicKey(mPublicKey.getAbyte());
}

/** Returns the human readable secret seed encoded in strkey. */
public char[] getSecretSeed() {
return StrKey.encodeStellarSecretSeed(mPrivateKey.getSeed());
return StrKey.encodeEd25519SecretSeed(mPrivateKey.getSeed());
}

public byte[] getPublicKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static class Builder {
* @param op {@link RevokeSponsorshipOp}
*/
Builder(RevokeSponsorshipOp op) {
accountId = StrKey.encodeStellarAccountId(op.getLedgerKey().getAccount().getAccountID());
accountId = StrKey.encodeEd25519PublicKey(op.getLedgerKey().getAccount().getAccountID());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static class Builder {
* @param op {@link RevokeSponsorshipOp}
*/
Builder(RevokeSponsorshipOp op) {
accountId = StrKey.encodeStellarAccountId(op.getLedgerKey().getData().getAccountID());
accountId = StrKey.encodeEd25519PublicKey(op.getLedgerKey().getData().getAccountID());
dataName = op.getLedgerKey().getData().getDataName().getString64().toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static class Builder {
*/
Builder(RevokeSponsorshipOp op) {
offerId = op.getLedgerKey().getOffer().getOfferID().getInt64();
seller = StrKey.encodeStellarAccountId(op.getLedgerKey().getOffer().getSellerID());
seller = StrKey.encodeEd25519PublicKey(op.getLedgerKey().getOffer().getSellerID());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static class Builder {
* @param op {@link RevokeSponsorshipOp}
*/
Builder(RevokeSponsorshipOp op) {
accountId = StrKey.encodeStellarAccountId(op.getSigner().getAccountID());
accountId = StrKey.encodeEd25519PublicKey(op.getSigner().getAccountID());
signer = op.getSigner().getSignerKey();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static class Builder {
* @param op {@link RevokeSponsorshipOp}
*/
Builder(RevokeSponsorshipOp op) {
accountId = StrKey.encodeStellarAccountId(op.getLedgerKey().getTrustLine().getAccountID());
accountId = StrKey.encodeEd25519PublicKey(op.getLedgerKey().getTrustLine().getAccountID());
asset = TrustLineAsset.fromXdr(op.getLedgerKey().getTrustLine().getAsset());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/SetOptionsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static class Builder {

Builder(SetOptionsOp op) {
if (op.getInflationDest() != null) {
inflationDestination = StrKey.encodeStellarAccountId(op.getInflationDest());
inflationDestination = StrKey.encodeEd25519PublicKey(op.getInflationDest());
}
if (op.getClearFlags() != null) {
clearFlags = op.getClearFlags().getUint32().getNumber().intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static class Builder {
private String mSourceAccount;

Builder(SetTrustLineFlagsOp op) {
trustor = StrKey.encodeStellarAccountId(op.getTrustor());
trustor = StrKey.encodeEd25519PublicKey(op.getTrustor());
asset = Util.assertNonNativeAsset(Asset.fromXdr(op.getAsset()));
clearFlags = flagSetFromInt(op.getClearFlags().getUint32().getNumber().intValue());
setFlags = flagSetFromInt(op.getSetFlags().getUint32().getNumber().intValue());
Expand Down
Loading

0 comments on commit 1831c4e

Please sign in to comment.