Skip to content

Commit

Permalink
Merge pull request #231 from tamirms/key-pair-fix
Browse files Browse the repository at this point in the history
Remove KeyPair dependency from requests and responses
  • Loading branch information
tamirms authored Jul 16, 2019
2 parents 1899af4 + 0cb3c7e commit 18db78b
Show file tree
Hide file tree
Showing 93 changed files with 489 additions and 549 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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.

## 0.9.0
* Use strings to represent account ids instead of KeyPair instances because account ids will not necessarily be valid
public keys. If you try to parse an invalid public key into a KeyPair you will encounter an exception. To prevent
exceptions when parsing horizon responses it is better to represent account ids as strings

## 0.8.0

* Removed deprecated methods and classes listed in the 0.7.0 changelog entry
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apply plugin: 'com.github.ben-manes.versions' // gradle dependencyUpdates -Drevi
apply plugin: 'project-report' // gradle htmlDependencyReport

sourceCompatibility = 1.6
version = '0.7.0'
version = '0.9.0'
group = 'stellar'

jar {
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/stellar/sdk/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@
* @see org.stellar.sdk.Transaction.Builder
*/
public class Account implements TransactionBuilderAccount {
private final KeyPair mKeyPair;
private final String mAccountId;
private Long mSequenceNumber;

/**
* Class constructor.
* @param keypair KeyPair associated with this Account
* @param accountId ID associated with this Account
* @param sequenceNumber Current sequence number of the account (can be obtained using java-stellar-sdk or horizon server)
*/
public Account(KeyPair keypair, Long sequenceNumber) {
mKeyPair = checkNotNull(keypair, "keypair cannot be null");
public Account(String accountId, Long sequenceNumber) {
mAccountId = checkNotNull(accountId, "accountId cannot be null");
mSequenceNumber = checkNotNull(sequenceNumber, "sequenceNumber cannot be null");
}

@Override
public KeyPair getKeypair() {
return mKeyPair;
public String getAccountId() {
return mAccountId;
}

@Override
public KeyPair getKeyPair() {
return KeyPair.fromAccountId(mAccountId);
}

@Override
Expand All @@ -46,7 +51,7 @@ public void incrementSequenceNumber() {
}

public int hashCode() {
return Objects.hashCode(this.mKeyPair, this.mSequenceNumber);
return Objects.hashCode(this.mAccountId, this.mSequenceNumber);
}

@Override
Expand All @@ -56,7 +61,7 @@ public boolean equals(Object object) {
}

Account other = (Account) object;
return Objects.equal(this.mKeyPair, other.mKeyPair) &&
return Objects.equal(this.mAccountId, other.mAccountId) &&
Objects.equal(this.mSequenceNumber, other.mSequenceNumber);

}
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/org/stellar/sdk/AccountMergeOperation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.Operation.OperationBody;
import org.stellar.sdk.xdr.OperationType;

Expand All @@ -13,25 +12,23 @@
*/
public class AccountMergeOperation extends Operation {

private final KeyPair destination;
private final String destination;

private AccountMergeOperation(KeyPair destination) {
private AccountMergeOperation(String destination) {
this.destination = checkNotNull(destination, "destination cannot be null");
}

/**
* The account that receives the remaining XLM balance of the source account.
*/
public KeyPair getDestination() {
public String getDestination() {
return destination;
}

@Override
OperationBody toOperationBody() {
OperationBody body = new org.stellar.sdk.xdr.Operation.OperationBody();
AccountID destination = new AccountID();
destination.setAccountID(this.destination.getXdrPublicKey());
body.setDestination(destination);
body.setDestination(StrKey.encodeToXDRAccountId(this.destination));
body.setDiscriminant(OperationType.ACCOUNT_MERGE);
return body;
}
Expand All @@ -41,19 +38,21 @@ OperationBody toOperationBody() {
* @see AccountMergeOperation
*/
public static class Builder {
private final KeyPair destination;
private final String destination;

private KeyPair mSourceAccount;
private String mSourceAccount;

Builder(OperationBody op) {
destination = KeyPair.fromXdrPublicKey(op.getDestination().getAccountID());
destination = StrKey.encodeStellarAccountId(
op.getDestination().getAccountID().getEd25519().getUint256()
);
}

/**
* Creates a new AccountMerge builder.
* @param destination The account that receives the remaining XLM balance of the source account.
*/
public Builder(KeyPair destination) {
public Builder(String destination) {
this.destination = destination;
}

Expand All @@ -62,7 +61,7 @@ public Builder(KeyPair destination) {
* @param sourceAccount Source account
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
public Builder setSourceAccount(String sourceAccount) {
mSourceAccount = sourceAccount;
return this;
}
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
public class AllowTrustOperation extends Operation {

private final KeyPair trustor;
private final String trustor;
private final String assetCode;
private final boolean authorize;

private AllowTrustOperation(KeyPair trustor, String assetCode, boolean authorize) {
private AllowTrustOperation(String trustor, String assetCode, boolean authorize) {
this.trustor = checkNotNull(trustor, "trustor cannot be null");
this.assetCode = checkNotNull(assetCode, "assetCode cannot be null");
this.authorize = authorize;
Expand All @@ -24,7 +24,7 @@ private AllowTrustOperation(KeyPair trustor, String assetCode, boolean authorize
/**
* The account of the recipient of the trustline.
*/
public KeyPair getTrustor() {
public String getTrustor() {
return trustor;
}

Expand All @@ -47,9 +47,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
AllowTrustOp op = new AllowTrustOp();

// trustor
AccountID trustor = new AccountID();
trustor.setAccountID(this.trustor.getXdrPublicKey());
op.setTrustor(trustor);
op.setTrustor(StrKey.encodeToXDRAccountId(this.trustor));
// asset
AllowTrustOp.AllowTrustOpAsset asset = new AllowTrustOp.AllowTrustOpAsset();
if (assetCode.length() <= 4) {
Expand Down Expand Up @@ -78,14 +76,14 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
* @see AllowTrustOperation
*/
public static class Builder {
private final KeyPair trustor;
private final String trustor;
private final String assetCode;
private final boolean authorize;

private KeyPair mSourceAccount;
private String mSourceAccount;

Builder(AllowTrustOp op) {
trustor = KeyPair.fromXdrPublicKey(op.getTrustor().getAccountID());
trustor = StrKey.encodeStellarAccountId(op.getTrustor().getAccountID().getEd25519().getUint256());
switch (op.getAsset().getDiscriminant()) {
case ASSET_TYPE_CREDIT_ALPHANUM4:
assetCode = new String(op.getAsset().getAssetCode4().getAssetCode4()).trim();
Expand All @@ -105,7 +103,7 @@ public static class Builder {
* @param assetCode The asset of the trustline the source account is authorizing. For example, if a gateway wants to allow another account to hold its USD credit, the type is USD.
* @param authorize Flag indicating whether the trustline is authorized.
*/
public Builder(KeyPair trustor, String assetCode, boolean authorize) {
public Builder(String trustor, String assetCode, boolean authorize) {
this.trustor = trustor;
this.assetCode = assetCode;
this.authorize = authorize;
Expand All @@ -116,7 +114,7 @@ public Builder(KeyPair trustor, String assetCode, boolean authorize) {
* @param sourceAccount Source account
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
public Builder setSourceAccount(String sourceAccount) {
mSourceAccount = sourceAccount;
return this;
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static Asset create(String type, String code, String issuer) {
if (type.equals("native")) {
return new AssetTypeNative();
} else {
return Asset.createNonNativeAsset(code, KeyPair.fromAccountId(issuer));
return Asset.createNonNativeAsset(code, issuer);
}
}

Expand All @@ -20,7 +20,7 @@ public static Asset create(String type, String code, String issuer) {
* @param code Asset code
* @param issuer Asset issuer
*/
public static Asset createNonNativeAsset(String code, KeyPair issuer) {
public static Asset createNonNativeAsset(String code, String issuer) {
if (code.length() >= 1 && code.length() <= 4) {
return new AssetTypeCreditAlphaNum4(code, issuer);
} else if (code.length() >= 5 && code.length() <= 12) {
Expand All @@ -35,18 +35,22 @@ public static Asset createNonNativeAsset(String code, KeyPair issuer) {
* @param xdr XDR object
*/
public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
String accountId;
switch (xdr.getDiscriminant()) {
case ASSET_TYPE_NATIVE:
return new AssetTypeNative();
case ASSET_TYPE_CREDIT_ALPHANUM4:
String assetCode4 = Util.paddedByteArrayToString(xdr.getAlphaNum4().getAssetCode().getAssetCode4());
KeyPair issuer4 = KeyPair.fromXdrPublicKey(
xdr.getAlphaNum4().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum4(assetCode4, issuer4);
accountId = StrKey.encodeStellarAccountId(
xdr.getAlphaNum4().getIssuer().getAccountID().getEd25519().getUint256()
);
return new AssetTypeCreditAlphaNum4(assetCode4, accountId);
case ASSET_TYPE_CREDIT_ALPHANUM12:
String assetCode12 = Util.paddedByteArrayToString(xdr.getAlphaNum12().getAssetCode().getAssetCode12());
KeyPair issuer12 = KeyPair.fromXdrPublicKey(xdr.getAlphaNum12().getIssuer().getAccountID());
return new AssetTypeCreditAlphaNum12(assetCode12, issuer12);
accountId = StrKey.encodeStellarAccountId(
xdr.getAlphaNum12().getIssuer().getAccountID().getEd25519().getUint256()
);
return new AssetTypeCreditAlphaNum12(assetCode12, accountId);
default:
throw new IllegalArgumentException("Unknown asset type " + xdr.getDiscriminant());
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*/
public abstract class AssetTypeCreditAlphaNum extends Asset {
protected final String mCode;
protected final KeyPair mIssuer;
protected final String mIssuer;

public AssetTypeCreditAlphaNum(String code, KeyPair issuer) {
public AssetTypeCreditAlphaNum(String code, String issuer) {
checkNotNull(code, "code cannot be null");
checkNotNull(issuer, "issuer cannot be null");
mCode = new String(code);
mIssuer = KeyPair.fromAccountId(issuer.getAccountId());
mIssuer = new String(issuer);
}

/**
Expand All @@ -30,8 +30,8 @@ public String getCode() {
/**
* Returns asset issuer
*/
public KeyPair getIssuer() {
return KeyPair.fromAccountId(mIssuer.getAccountId());
public String getIssuer() {
return new String(mIssuer);
}

@Override
Expand All @@ -48,6 +48,6 @@ public boolean equals(Object object) {
AssetTypeCreditAlphaNum o = (AssetTypeCreditAlphaNum) object;

return this.getCode().equals(o.getCode()) &&
this.getIssuer().getAccountId().equals(o.getIssuer().getAccountId());
this.getIssuer().equals(o.getIssuer());
}
}
10 changes: 3 additions & 7 deletions src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum12.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.AssetCode12;
import org.stellar.sdk.xdr.AssetType;
import org.stellar.sdk.xdr.*;

/**
* Represents all assets with codes 5-12 characters long.
Expand All @@ -15,7 +13,7 @@ public final class AssetTypeCreditAlphaNum12 extends AssetTypeCreditAlphaNum {
* @param code Asset code
* @param issuer Asset issuer
*/
public AssetTypeCreditAlphaNum12(String code, KeyPair issuer) {
public AssetTypeCreditAlphaNum12(String code, String issuer) {
super(code, issuer);
if (code.length() < 5 || code.length() > 12) {
throw new AssetCodeLengthInvalidException();
Expand All @@ -35,9 +33,7 @@ public org.stellar.sdk.xdr.Asset toXdr() {
AssetCode12 assetCode12 = new AssetCode12();
assetCode12.setAssetCode12(Util.paddedByteArray(mCode, 12));
credit.setAssetCode(assetCode12);
AccountID accountID = new AccountID();
accountID.setAccountID(mIssuer.getXdrPublicKey());
credit.setIssuer(accountID);
credit.setIssuer(StrKey.encodeToXDRAccountId(mIssuer));
xdr.setAlphaNum12(credit);
return xdr;
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/org/stellar/sdk/AssetTypeCreditAlphaNum4.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.stellar.sdk;

import org.stellar.sdk.xdr.AccountID;
import org.stellar.sdk.xdr.AssetCode4;
import org.stellar.sdk.xdr.AssetType;
import org.stellar.sdk.xdr.*;

/**
* Represents all assets with codes 1-4 characters long.
Expand All @@ -15,7 +13,7 @@ public final class AssetTypeCreditAlphaNum4 extends AssetTypeCreditAlphaNum {
* @param code Asset code
* @param issuer Asset issuer
*/
public AssetTypeCreditAlphaNum4(String code, KeyPair issuer) {
public AssetTypeCreditAlphaNum4(String code, String issuer) {
super(code, issuer);
if (code.length() < 1 || code.length() > 4) {
throw new AssetCodeLengthInvalidException();
Expand All @@ -35,9 +33,7 @@ public org.stellar.sdk.xdr.Asset toXdr() {
AssetCode4 assetCode4 = new AssetCode4();
assetCode4.setAssetCode4(Util.paddedByteArray(mCode, 4));
credit.setAssetCode(assetCode4);
AccountID accountID = new AccountID();
accountID.setAccountID(mIssuer.getXdrPublicKey());
credit.setIssuer(accountID);
credit.setIssuer(StrKey.encodeToXDRAccountId(mIssuer));
xdr.setAlphaNum4(credit);
return xdr;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/BumpSequenceOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
public static class Builder {
private final long bumpTo;

private KeyPair mSourceAccount;
private String mSourceAccount;

/**
* Construct a new BumpSequence builder from a BumpSequence XDR.
Expand All @@ -61,7 +61,7 @@ public Builder(long bumpTo) {
* @param sourceAccount The operation's source account.
* @return Builder object so you can chain methods.
*/
public BumpSequenceOperation.Builder setSourceAccount(KeyPair sourceAccount) {
public BumpSequenceOperation.Builder setSourceAccount(String sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/ChangeTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static class Builder {
private final Asset asset;
private final String limit;

private KeyPair mSourceAccount;
private String mSourceAccount;

Builder(ChangeTrustOp op) {
asset = Asset.fromXdr(op.getLine());
Expand All @@ -81,7 +81,7 @@ public Builder(Asset asset, String limit) {
* @param sourceAccount Source account
* @return Builder object so you can chain methods.
*/
public Builder setSourceAccount(KeyPair sourceAccount) {
public Builder setSourceAccount(String sourceAccount) {
mSourceAccount = checkNotNull(sourceAccount, "sourceAccount cannot be null");
return this;
}
Expand Down
Loading

0 comments on commit 18db78b

Please sign in to comment.