Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Address -> AccountId
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekn committed Jan 14, 2016
1 parent 251bbc8 commit a752001
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'ivy-publish'

sourceCompatibility = 1.6
version = '0.0.6'
version = '0.0.7'
group = 'stellar'

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public boolean equals(Object object) {
AssetTypeCreditAlphaNum o = (AssetTypeCreditAlphaNum) object;

return this.getCode().equals(o.getCode()) &&
this.getIssuer().getAddress().equals(o.getIssuer().getAddress());
this.getIssuer().getAccountId().equals(o.getIssuer().getAccountId());
}
}
36 changes: 27 additions & 9 deletions src/main/java/org/stellar/base/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,32 @@ public static KeyPair fromSecretSeed(byte[] seed) {
}

/**
* Creates a new Stellar KeyPair from a strkey encoded Stellar address.
* @param address The strkey encoded Stellar address.
* Creates a new Stellar KeyPair from a strkey encoded Stellar account ID.
* @param accountId The strkey encoded Stellar account ID.
* @return {@link KeyPair}
*/
public static KeyPair fromAddress(String address) {
byte[] decoded = StrKey.decodeStellarAddress(address);
public static KeyPair fromAccountId(String accountId) {
byte[] decoded = StrKey.decodeStellarAccountId(accountId);
return fromPublicKey(decoded);
}

/**
* Creates a new Stellar KeyPair from a strkey encoded Stellar account ID.
* @param accountId The strkey encoded Stellar account ID.
* @deprecated Use {@link KeyPair#fromAccountId}
* @return {@link KeyPair}
*/
public static KeyPair fromAddress(String accountId) {
return fromAccountId(accountId);
}

/**
* Creates a new Stellar keypair from a 32 byte address.
* @param address The 32 byte address.
* @param publicKey The 32 byte public key.
* @return {@link KeyPair}
*/
public static KeyPair fromPublicKey(byte[] address) {
EdDSAPublicKeySpec publicKeySpec = new EdDSAPublicKeySpec(address, ed25519);
public static KeyPair fromPublicKey(byte[] publicKey) {
EdDSAPublicKeySpec publicKeySpec = new EdDSAPublicKeySpec(publicKey, ed25519);
return new KeyPair(new EdDSAPublicKey(publicKeySpec));
}

Expand All @@ -124,10 +134,18 @@ public static KeyPair random() {
}

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

/**
* Returns the human readable account ID encoded in strkey.
* @deprecated Use {@link KeyPair#getAccountId}
*/
public String getAddress() {
return StrKey.encodeStellarAddress(mPublicKey.getAbyte());
return StrKey.encodeStellarAccountId(mPublicKey.getAbyte());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/base/StrKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public int getValue() {
}
}

public static String encodeStellarAddress(byte[] data) {
public static String encodeStellarAccountId(byte[] data) {
char[] encoded = encodeCheck(VersionByte.ACCOUNT_ID, data);
return String.valueOf(encoded);
}
Expand All @@ -28,7 +28,7 @@ public static char[] encodeStellarSecretSeed(byte[] data) {
return encodeCheck(VersionByte.SEED, data);
}

public static byte[] decodeStellarAddress(String data) {
public static byte[] decodeStellarAccountId(String data) {
return decodeCheck(VersionByte.ACCOUNT_ID, data.toCharArray());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/stellar/base/AccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testIncrementSequenceNumber() {
public void testGetters() {
KeyPair keypair = KeyPair.random();
Account account = new Account(keypair, 100L);
assertEquals(account.getKeypair().getAddress(), keypair.getAddress());
assertEquals(account.getKeypair().getAccountId(), keypair.getAccountId());
assertEquals(account.getSequenceNumber(), new Long(100L));
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/stellar/base/AssetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testAssetTypeCreditAlphaNum4() {
org.stellar.base.xdr.Asset xdr = asset.toXdr();
AssetTypeCreditAlphaNum4 parsedAsset = (AssetTypeCreditAlphaNum4) Asset.fromXdr(xdr);
assertEquals(code, asset.getCode());
assertEquals(issuer.getAddress(), parsedAsset.getIssuer().getAddress());
assertEquals(issuer.getAccountId(), parsedAsset.getIssuer().getAccountId());
}

@Test
Expand All @@ -38,7 +38,7 @@ public void testAssetTypeCreditAlphaNum12() {
org.stellar.base.xdr.Asset xdr = asset.toXdr();
AssetTypeCreditAlphaNum12 parsedAsset = (AssetTypeCreditAlphaNum12) Asset.fromXdr(xdr);
assertEquals(code, asset.getCode());
assertEquals(issuer.getAddress(), parsedAsset.getIssuer().getAddress());
assertEquals(issuer.getAccountId(), parsedAsset.getIssuer().getAccountId());
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/stellar/base/KeyPairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public void testFromSecretSeed() throws Exception {
keypairs.put("SDYZ5IYOML3LTWJ6WIAC2YWORKVO7GJRTPPGGNJQERH72I6ZCQHDAJZN", "GABXJTV7ELEB2TQZKJYEGXBUIG6QODJULKJDI65KZMIZZG2EACJU5EA7");

for (String seed : keypairs.keySet()) {
String address = keypairs.get(seed);
String accountId = keypairs.get(seed);
KeyPair keypair = KeyPair.fromSecretSeed(seed);
assertEquals(address, keypair.getAddress());
assertEquals(accountId, keypair.getAccountId());
}
}
}
28 changes: 14 additions & 14 deletions src/test/java/org/stellar/base/OperationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void testCreateAccountOperation() throws FormatException, IOException, As
CreateAccountOperation parsedOperation = (CreateAccountOperation) Operation.fromXdr(xdr);

assertEquals(10000000000L, xdr.getBody().getCreateAccountOp().getStartingBalance().getInt64().longValue());
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(destination.getAddress(), parsedOperation.getDestination().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());
assertEquals(destination.getAccountId(), parsedOperation.getDestination().getAccountId());
assertEquals(startingAmount, parsedOperation.getStartingBalance());

assertEquals(
Expand All @@ -53,8 +53,8 @@ public void testPaymentOperation() throws FormatException, IOException, AssetCod
PaymentOperation parsedOperation = (PaymentOperation) Operation.fromXdr(xdr);

assertEquals(10000000000L, xdr.getBody().getPaymentOp().getAmount().getInt64().longValue());
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(destination.getAddress(), parsedOperation.getDestination().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());
assertEquals(destination.getAccountId(), parsedOperation.getDestination().getAccountId());
assertTrue(parsedOperation.getAsset() instanceof AssetTypeNative);
assertEquals(amount, parsedOperation.getAmount());

Expand Down Expand Up @@ -94,8 +94,8 @@ public void testPathPaymentOperation() throws FormatException, IOException, Asse
assertEquals(1000L, xdr.getBody().getPathPaymentOp().getSendMax().getInt64().longValue());
assertEquals(1000L, xdr.getBody().getPathPaymentOp().getDestAmount().getInt64().longValue());
assertTrue(parsedOperation.getSendAsset() instanceof AssetTypeNative);
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(destination.getAddress(), parsedOperation.getDestination().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());
assertEquals(destination.getAccountId(), parsedOperation.getDestination().getAccountId());
assertEquals(sendMax, parsedOperation.getSendMax());
assertTrue(parsedOperation.getDestAsset() instanceof AssetTypeCreditAlphaNum4);
assertEquals(destAmount, parsedOperation.getDestAmount());
Expand All @@ -122,7 +122,7 @@ public void testChangeTrustOperation() throws FormatException, IOException {
ChangeTrustOperation parsedOperation = (ChangeTrustOperation) Operation.fromXdr(xdr);

assertEquals(9223372036854775807L, xdr.getBody().getChangeTrustOp().getLimit().getInt64().longValue());
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());
assertTrue(parsedOperation.getAsset() instanceof AssetTypeNative);
assertEquals(limit, parsedOperation.getLimit());

Expand All @@ -148,8 +148,8 @@ public void testAllowTrustOperation() throws IOException, FormatException {
org.stellar.base.xdr.Operation xdr = operation.toXdr();
AllowTrustOperation parsedOperation = (AllowTrustOperation) Operation.fromXdr(xdr);

assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(trustor.getAddress(), parsedOperation.getTrustor().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());
assertEquals(trustor.getAccountId(), parsedOperation.getTrustor().getAccountId());
assertEquals(assetCode, parsedOperation.getAssetCode());
assertEquals(authorize, parsedOperation.getAuthorize());

Expand Down Expand Up @@ -192,17 +192,17 @@ public void testSetOptionsOperation() throws FormatException {
org.stellar.base.xdr.Operation xdr = operation.toXdr();
SetOptionsOperation parsedOperation = (SetOptionsOperation) SetOptionsOperation.fromXdr(xdr);

assertEquals(inflationDestination.getAddress(), parsedOperation.getInflationDestination().getAddress());
assertEquals(inflationDestination.getAccountId(), parsedOperation.getInflationDestination().getAccountId());
assertEquals(clearFlags, parsedOperation.getClearFlags());
assertEquals(setFlags, parsedOperation.getSetFlags());
assertEquals(masterKeyWeight, parsedOperation.getMasterKeyWeight());
assertEquals(lowThreshold, parsedOperation.getLowThreshold());
assertEquals(mediumThreshold, parsedOperation.getMediumThreshold());
assertEquals(highThreshold, parsedOperation.getHighThreshold());
assertEquals(homeDomain, parsedOperation.getHomeDomain());
assertEquals(signer.getAddress(), parsedOperation.getSigner().getAddress());
assertEquals(signer.getAccountId(), parsedOperation.getSigner().getAccountId());
assertEquals(signerWeight, parsedOperation.getSignerWeight());
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());

assertEquals(
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAABAAAAAO3gUmG83C+VCqO6FztuMtXJF/l7grZA7MjRzqdZ9W8QAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAIAAAABAAAAAwAAAAEAAAAEAAAAAQAAAAtzdGVsbGFyLm9yZwAAAAABAAAAAET+21WXwEtXRyxb/GBe1tc5V/WUzIOW4yJp+XQgNUUiAAAAAQ==",
Expand Down Expand Up @@ -234,7 +234,7 @@ public void testSetOptionsOperationSingleField() {
assertEquals(homeDomain, parsedOperation.getHomeDomain());
assertEquals(null, parsedOperation.getSigner());
assertEquals(null, parsedOperation.getSignerWeight());
assertEquals(source.getAddress(), parsedOperation.getSourceAccount().getAddress());
assertEquals(source.getAccountId(), parsedOperation.getSourceAccount().getAccountId());

assertEquals(
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAtzdGVsbGFyLm9yZwAAAAAA",
Expand Down Expand Up @@ -327,7 +327,7 @@ public void testAccountMergeOperation() throws IOException, FormatException {

AccountMergeOperation parsedOperation = (AccountMergeOperation) Operation.fromXdr(xdr);

assertEquals(destination.getAddress(), parsedOperation.getDestination().getAddress());
assertEquals(destination.getAccountId(), parsedOperation.getDestination().getAccountId());

assertEquals(
"AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAgAAAAA7eBSYbzcL5UKo7oXO24y1ckX+XuCtkDsyNHOp1n1bxA=",
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/stellar/base/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void tearDown() {
public void testBuilderSuccessTestnet() throws FormatException {
// GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4
KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");
KeyPair destination = KeyPair.fromAddress("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");
KeyPair destination = KeyPair.fromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");

long sequenceNumber = 2908908335136768L;
Account account = new Account(source, sequenceNumber);
Expand All @@ -41,7 +41,7 @@ public void testBuilderSuccessTestnet() throws FormatException {
public void testBuilderMemoText() throws FormatException {
// GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4
KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");
KeyPair destination = KeyPair.fromAddress("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");
KeyPair destination = KeyPair.fromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");

Account account = new Account(source, 2908908335136768L);
Transaction transaction = new Transaction.Builder(account)
Expand All @@ -62,7 +62,7 @@ public void testBuilderSuccessPublic() throws FormatException {

// GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4
KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");
KeyPair destination = KeyPair.fromAddress("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");
KeyPair destination = KeyPair.fromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");

Account account = new Account(source, 2908908335136768L);
Transaction transaction = new Transaction.Builder(account)
Expand All @@ -80,7 +80,7 @@ public void testBuilderSuccessPublic() throws FormatException {
public void testToBase64EnvelopeXdrBuilderNoSignatures() throws FormatException, IOException {
// GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4
KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");
KeyPair destination = KeyPair.fromAddress("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");
KeyPair destination = KeyPair.fromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");

Account account = new Account(source, 2908908335136768L);
Transaction transaction = new Transaction.Builder(account)
Expand Down Expand Up @@ -113,7 +113,7 @@ public void testNoOperations() throws FormatException, IOException {
public void testTryingToAddMemoTwice() throws FormatException, IOException {
// GBPMKIRA2OQW2XZZQUCQILI5TMVZ6JNRKM423BSAISDM7ZFWQ6KWEBC4
KeyPair source = KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");
KeyPair destination = KeyPair.fromAddress("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");
KeyPair destination = KeyPair.fromAccountId("GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR");

try {
Account account = new Account(source, 2908908335136768L);
Expand Down

0 comments on commit a752001

Please sign in to comment.