Skip to content

Commit

Permalink
chore: update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 9, 2024
1 parent 05c3261 commit 6e4d04d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/main/java/org/stellar/sdk/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.stellar.sdk.xdr.Uint256;
import org.stellar.sdk.xdr.XdrDataOutputStream;

// TODO: docs
/** Holds a Stellar keypair. */
@EqualsAndHashCode
public class KeyPair {
Expand Down Expand Up @@ -177,6 +176,9 @@ public byte[] getPublicKey() {
return publicKey.getAbyte();
}

/**
* Returns the signature hint for this keypair.
*/
public SignatureHint getSignatureHint() {
try {
ByteArrayOutputStream publicKeyBytesStream = new ByteArrayOutputStream();
Expand All @@ -193,6 +195,9 @@ public SignatureHint getSignatureHint() {
}
}

/**
* Returns the XDR {@link PublicKey} for this keypair.
*/
public PublicKey getXdrPublicKey() {
PublicKey publicKey = new PublicKey();
publicKey.setDiscriminant(PublicKeyType.PUBLIC_KEY_TYPE_ED25519);
Expand All @@ -202,12 +207,18 @@ public PublicKey getXdrPublicKey() {
return publicKey;
}

/**
* Returns the XDR {@link AccountID} for this keypair.
*/
public AccountID getXdrAccountId() {
AccountID accountID = new AccountID();
accountID.setAccountID(getXdrPublicKey());
return accountID;
}

/**
* Returns the XDR {@link SignerKey} for this keypair.
*/
public SignerKey getXdrSignerKey() {
SignerKey signerKey = new SignerKey();
signerKey.setDiscriminant(SignerKeyType.SIGNER_KEY_TYPE_ED25519);
Expand All @@ -217,10 +228,21 @@ public SignerKey getXdrSignerKey() {
return signerKey;
}

/**
* Creates a new KeyPair from an XDR {@link PublicKey}.
*
* @param key The XDR {@link PublicKey} object.
* @return KeyPair
*/
public static KeyPair fromXdrPublicKey(PublicKey key) {
return KeyPair.fromPublicKey(key.getEd25519().getUint256());
}

/**
* Creates a new KeyPair from an XDR {@link SignerKey}.
* @param key The XDR {@link SignerKey} object.
* @return KeyPair
*/
public static KeyPair fromXdrSignerKey(SignerKey key) {
return KeyPair.fromPublicKey(key.getEd25519().getUint256());
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/stellar/sdk/exception/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Exceptions that can be thrown by the Stellar SDK.
*
* <p>The base exception class is {@link org.stellar.sdk.exception.SdkException}, which extends {@link java.lang.RuntimeException}.
* Most exceptions thrown by the SDK extend {@link org.stellar.sdk.exception.SdkException}, while some extend
* {@link java.lang.IllegalArgumentException} directly.
*
* <p>The Stellar SDK uses unchecked exceptions (extending {@code RuntimeException})
* for the following reasons:
* <ul>
* <li>Stellar SDK methods may throw exceptions as a result of errors that occur
* in the Stellar network, which are out of the control of the SDK itself.
* Forcing developers to catch such exceptions would lead to cluttered code.</li>
* <li>Most methods in the SDK throw exceptions to indicate errors that cannot be
* handled by the method itself. Propagating these exceptions as unchecked
* exceptions allows developers to handle them at an appropriate point in their code.</li>
* <li>Unchecked exceptions are more suitable for an API like the Stellar SDK,
* where most exceptions indicate programming errors or errors in interaction
* with the Stellar network, rather than recoverable conditions.</li>
* </ul>
*
* <p>While the SDK uses unchecked exceptions, developers are still encouraged to
* catch and handle these exceptions appropriately in their code. The specific
* exception classes thrown by the SDK provide useful information about the nature
* of the error that occurred.
*
* @see org.stellar.sdk.exception.SdkException
* @see java.lang.RuntimeException
* @see java.lang.IllegalArgumentException
*/
package org.stellar.sdk.exception;

0 comments on commit 6e4d04d

Please sign in to comment.