Java Code Examples for com.google.common.io.BaseEncoding#base64()

The following examples show how to use com.google.common.io.BaseEncoding#base64() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FirebaseChannel.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Create a secure JWT token for the given userId. */
public String createFirebaseToken(Game game, String userId) {
  final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
  final BaseEncoding base64 = BaseEncoding.base64();

  String header = base64.encode("{\"typ\":\"JWT\",\"alg\":\"RS256\"}".getBytes());

  // Construct the claim
  String channelKey = game.getChannelKey(userId);
  String clientEmail = appIdentity.getServiceAccountName();
  long epochTime = System.currentTimeMillis() / 1000;
  long expire = epochTime + 60 * 60; // an hour from now

  Map<String, Object> claims = new HashMap<String, Object>();
  claims.put("iss", clientEmail);
  claims.put("sub", clientEmail);
  claims.put("aud", IDENTITY_ENDPOINT);
  claims.put("uid", channelKey);
  claims.put("iat", epochTime);
  claims.put("exp", expire);

  String payload = base64.encode(new Gson().toJson(claims).getBytes());
  String toSign = String.format("%s.%s", header, payload);
  AppIdentityService.SigningResult result = appIdentity.signForApp(toSign.getBytes());
  return String.format("%s.%s", toSign, base64.encode(result.getSignature()));
}
 
Example 2
Source File: SubmitTransactionResponse.java    From java-stellar-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Decoding "TransactionResult" from "resultXdr".
 * This will be <code>Optional.absent()</code> if transaction has failed.
 */
public Optional<TransactionResult> getDecodedTransactionResult() throws IOException {
    if(!this.isSuccess()) {
        return Optional.absent();
    }

    if (this.transactionResult == null) {
        Optional<String> resultXDR = this.getResultXdr();
        if (!resultXDR.isPresent()) {
            return Optional.absent();
        }
        BaseEncoding base64Encoding = BaseEncoding.base64();
        byte[] bytes = base64Encoding.decode(resultXDR.get());
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        XdrDataInputStream xdrInputStream = new XdrDataInputStream(inputStream);
        this.transactionResult = TransactionResult.decode(xdrInputStream);
    }

    return Optional.of(this.transactionResult);
}
 
Example 3
Source File: Operation.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns base64-encoded Operation XDR object.
 */
public String toXdrBase64() {
  try {
    org.stellar.sdk.xdr.Operation operation = this.toXdr();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream);
    org.stellar.sdk.xdr.Operation.encode(xdrOutputStream, operation);
    BaseEncoding base64Encoding = BaseEncoding.base64();
    return base64Encoding.encode(outputStream.toByteArray());
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}
 
Example 4
Source File: AbstractTransaction.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns base64-encoded TransactionEnvelope XDR object. Transaction need to have at least one signature.
 */
public String toEnvelopeXdrBase64() {
  try {
    TransactionEnvelope envelope = this.toEnvelopeXdr();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(outputStream);
    TransactionEnvelope.encode(xdrOutputStream, envelope);

    BaseEncoding base64Encoding = BaseEncoding.base64();
    return base64Encoding.encode(outputStream.toByteArray());
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}
 
Example 5
Source File: AbstractTransaction.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a <code>Transaction</code> instance from previously build <code>TransactionEnvelope</code>
 * @param envelope Base-64 encoded <code>TransactionEnvelope</code>
 * @return
 * @throws IOException
 */
public static AbstractTransaction fromEnvelopeXdr(String envelope, Network network) throws IOException {
  BaseEncoding base64Encoding = BaseEncoding.base64();
  byte[] bytes = base64Encoding.decode(envelope);

  TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
  return fromEnvelopeXdr(transactionEnvelope, network);
}
 
Example 6
Source File: Sep10Challenge.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a valid <a href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md#response" target="_blank">SEP 10</a> challenge, for use in web authentication.
 * @param signer           The server's signing account.
 * @param network          The Stellar network used by the server.
 * @param clientAccountId  The stellar account belonging to the client.
 * @param anchorName       The name of the anchor which will be included in the ManageData operation.
 * @param timebounds       The lifetime of the challenge token.
 */
public static Transaction newChallenge(
    KeyPair signer,
    Network network,
    String clientAccountId,
    String anchorName,
    TimeBounds timebounds
) throws InvalidSep10ChallengeException {
  byte[] nonce = new byte[48];
  SecureRandom random = new SecureRandom();
  random.nextBytes(nonce);
  BaseEncoding base64Encoding = BaseEncoding.base64();
  byte[] encodedNonce = base64Encoding.encode(nonce).getBytes();


  if (StrKey.decodeVersionByte(clientAccountId) != StrKey.VersionByte.ACCOUNT_ID) {
    throw new InvalidSep10ChallengeException(clientAccountId+" is not a valid account id");
  }

  Account sourceAccount = new Account(signer.getAccountId(), -1L);
  ManageDataOperation operation = new ManageDataOperation.Builder(anchorName + " auth", encodedNonce)
      .setSourceAccount(clientAccountId)
      .build();
  Transaction transaction = new Transaction.Builder(sourceAccount, network)
      .addTimeBounds(timebounds)
      .setBaseFee(100)
      .addOperation(operation)
      .build();

  transaction.sign(signer);

  return transaction;
}
 
Example 7
Source File: Sep10ChallengeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testChallenge() throws InvalidSep10ChallengeException {
  KeyPair server = KeyPair.random();
  KeyPair client = KeyPair.random();

  long now = System.currentTimeMillis() / 1000L;
  long end = now + 300;
  TimeBounds timeBounds = new TimeBounds(now, end);

  Transaction transaction = Sep10Challenge.newChallenge(
      server,
      Network.TESTNET,
      client.getAccountId(),
      "angkor wat",
      timeBounds
  );


  assertEquals(Network.TESTNET, transaction.getNetwork());
  assertEquals(100, transaction.getFee());
  assertEquals(timeBounds, transaction.getTimeBounds());
  assertEquals(server.getAccountId(), transaction.getSourceAccount());
  assertEquals(0, transaction.getSequenceNumber());

  assertEquals(1, transaction.getOperations().length);
  ManageDataOperation op = (ManageDataOperation) transaction.getOperations()[0];
  assertEquals(client.getAccountId(), op.getSourceAccount());
  assertEquals("angkor wat auth", op.getName());

  assertEquals(64, op.getValue().length);
  BaseEncoding base64Encoding = BaseEncoding.base64();
  assertTrue(base64Encoding.canDecode(new String(op.getValue())));

  assertEquals(1, transaction.getSignatures().size());
  assertTrue(
      server.verify(transaction.hash(), transaction.getSignatures().get(0).getSignature().getSignature())
  );
}
 
Example 8
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeTxBody() throws IOException {
    // pubnet - ledgerseq 5845058, txid  d5ec6645d86cdcae8212cbe60feaefb8d6b1a8b7d11aeea590608b0863ace4de

    String txBody = "AAAAAERmsKL73CyLV/HvjyQCERDXXpWE70Xhyb6MR5qPO3yQAAAAZAAIbkEAACD7AAAAAAAAAAN43bSwpXw8tSAhl7TBtQeOZTQAXwAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAP1qe44j+i4uIT+arbD4QDQBt8ryEeJd7a0jskQ3nwDeAAAAAAAAAADdVhDVFrUiS/jPrRpblXY4bAW9u4hbRI2Hhw+2ATsFpQAAAAAtPWvAAAAAAAAAAAGPO3yQAAAAQHGWVHCBsjTyap/OY9JjPHmzWtN2Y2sL98aMERc/xJ3hcWz6kdQAwjlEhilItCyokDHCrvALZy3v/1TlaDqprA0=";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(txBody);

    TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(new Long(2373025265623291L), transactionEnvelope.getV0().getTx().getSeqNum().getSequenceNumber().getInt64());
}
 
Example 9
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeTxResult() throws IOException {
    // pubnet - ledgerseq 5845058, txid  d5ec6645d86cdcae8212cbe60feaefb8d6b1a8b7d11aeea590608b0863ace4de

    String txResult = "1exmRdhs3K6CEsvmD+rvuNaxqLfRGu6lkGCLCGOs5N4AAAAAAAAAZAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAA==";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(txResult);

    TransactionResultPair transactionResult = TransactionResultPair.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(TransactionResultCode.txSUCCESS, transactionResult.getResult().getResult().getDiscriminant());
}
 
Example 10
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeTxMeta() throws IOException {
    // pubnet - ledgerseq 5845058, txid  d5ec6645d86cdcae8212cbe60feaefb8d6b1a8b7d11aeea590608b0863ace4de

    String txMeta = "AAAAAAAAAAEAAAADAAAAAABZMEIAAAAAAAAAAN1WENUWtSJL+M+tGluVdjhsBb27iFtEjYeHD7YBOwWlAAAAAC09a8AAWTBCAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAwBZL8QAAAAAAAAAAP1qe44j+i4uIT+arbD4QDQBt8ryEeJd7a0jskQ3nwDeAALU1gZ4V7UACD1BAAAAHgAAAAoAAAAAAAAAAAAAAAABAAAAAAAACgAAAAARC07BokpLTOF+/vVKBwiAlop7hHGJTNeGGlY4MoPykwAAAAEAAAAAK+Lzfd3yDD+Ov0GbYu1g7SaIBrKZeBUxoCunkLuI7aoAAAABAAAAAERmsKL73CyLV/HvjyQCERDXXpWE70Xhyb6MR5qPO3yQAAAAAQAAAABSORGwAdyuanN3sNOHqNSpACyYdkUM3L8VafUu69EvEgAAAAEAAAAAeCzqJNkMM/jLvyuMIfyFHljBlLCtDyj17RMycPuNtRMAAAABAAAAAIEi4R7juq15ymL00DNlAddunyFT4FyUD4muC4t3bobdAAAAAQAAAACaNpLL5YMfjOTdXVEqrAh99LM12sN6He6pHgCRAa1f1QAAAAEAAAAAqB+lfAPV9ak+Zkv4aTNZwGaFFAfui4+yhM3dGhoYJ+sAAAABAAAAAMNJrEvdMg6M+M+n4BDIdzsVSj/ZI9SvAp7mOOsvAD/WAAAAAQAAAADbHA6xiKB1+G79mVqpsHMOleOqKa5mxDpP5KEp/Xdz9wAAAAEAAAAAAAAAAAAAAAEAWTBCAAAAAAAAAAD9anuOI/ouLiE/mq2w+EA0AbfK8hHiXe2tI7JEN58A3gAC1NXZOuv1AAg9QQAAAB4AAAAKAAAAAAAAAAAAAAAAAQAAAAAAAAoAAAAAEQtOwaJKS0zhfv71SgcIgJaKe4RxiUzXhhpWODKD8pMAAAABAAAAACvi833d8gw/jr9Bm2LtYO0miAaymXgVMaArp5C7iO2qAAAAAQAAAABEZrCi+9wsi1fx748kAhEQ116VhO9F4cm+jEeajzt8kAAAAAEAAAAAUjkRsAHcrmpzd7DTh6jUqQAsmHZFDNy/FWn1LuvRLxIAAAABAAAAAHgs6iTZDDP4y78rjCH8hR5YwZSwrQ8o9e0TMnD7jbUTAAAAAQAAAACBIuEe47qtecpi9NAzZQHXbp8hU+BclA+JrguLd26G3QAAAAEAAAAAmjaSy+WDH4zk3V1RKqwIffSzNdrDeh3uqR4AkQGtX9UAAAABAAAAAKgfpXwD1fWpPmZL+GkzWcBmhRQH7ouPsoTN3RoaGCfrAAAAAQAAAADDSaxL3TIOjPjPp+AQyHc7FUo/2SPUrwKe5jjrLwA/1gAAAAEAAAAA2xwOsYigdfhu/ZlaqbBzDpXjqimuZsQ6T+ShKf13c/cAAAABAAAAAAAAAAA=";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(txMeta);

    TransactionMeta transactionMeta = TransactionMeta.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(1, transactionMeta.getOperations().length);
}
 
Example 11
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionEnvelopeWithMemo() throws IOException {
    String transactionEnvelopeToDecode = "AAAAACq1Ixcw1fchtF5aLTSw1zaYAYjb3WbBRd4jqYJKThB9AAAAZAA8tDoAAAALAAAAAAAAAAEAAAAZR29sZCBwYXltZW50IGZvciBzZXJ2aWNlcwAAAAAAAAEAAAAAAAAAAQAAAAARREGslec48mbJJygIwZoLvRtL6/gGL4ss2TOpnOUOhgAAAAFHT0xEAAAAACq1Ixcw1fchtF5aLTSw1zaYAYjb3WbBRd4jqYJKThB9AAAAADuaygAAAAAAAAAAAA==";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(transactionEnvelopeToDecode);

    TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(1, transactionEnvelope.getV0().getTx().getOperations().length);
    assertTrue(Arrays.equals(new byte[]{'G', 'O', 'L', 'D'}, transactionEnvelope.getV0().getTx().getOperations()[0].getBody().getPaymentOp().getAsset().getAlphaNum4().getAssetCode().getAssetCode4()));
}
 
Example 12
Source File: TransactionDecodeTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoundtrip() throws IOException {
    String txBody = "AAAAAM6jLgjKjuXxWkir4M7v0NqoOfODXcFnn6AGlP+d4RxAAAAAZAAIiE4AAAABAAAAAAAAAAEAAAAcyKMl+WDSzuttWkF2DvzKAkkEqeSZ4cZihjGJEAAAAAEAAAAAAAAAAQAAAAAgECmBaDwiRPE1z2vAE36J+45toU/ZxdvpR38tc0HvmgAAAAAAAAAAAJiWgAAAAAAAAAABneEcQAAAAECeXDKebJoAbST1T2AbDBui9K0TbSM8sfbhXUAZ2ROAoCRs5cG1pRvY+ityyPWFEKPd7+3qEupavkAZ/+L7/28G";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(txBody);

    TransactionEnvelope transactionEnvelope = TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

    transactionEnvelope.encode(new XdrDataOutputStream(byteOutputStream));
    String serialized = base64Encoding.encode(byteOutputStream.toByteArray());
    assertEquals(serialized, txBody);
}
 
Example 13
Source File: OperationTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageSellOfferOperation_BadArithmeticRegression() throws IOException {
    // from https://github.com/stellar/java-stellar-sdk/issues/183

    String transactionEnvelopeToDecode = "AAAAAButy5zasS3DLZ5uFpZHL25aiHUfKRwdv1+3Wp12Ce7XAAAAZAEyGwYAAAAOAAAAAAAAAAAAAAABAAAAAQAAAAAbrcuc2rEtwy2ebhaWRy9uWoh1HykcHb9ft1qddgnu1wAAAAMAAAAAAAAAAUtJTgAAAAAARkrT28ebM6YQyhVZi1ttlwq/dk6ijTpyTNuHIMgUp+EAAAAAAAARPSfDKZ0AAv7oAAAAAAAAAAAAAAAAAAAAAXYJ7tcAAABAbE8rEoFt0Hcv41iwVCl74C1Hyr+Lj8ZyaYn7zTJhezClbc+pTW1KgYFIZOJiGVth2xFnBT1pMXuQkVdTlB3FCw==";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(transactionEnvelopeToDecode);

    org.stellar.sdk.xdr.TransactionEnvelope transactionEnvelope = org.stellar.sdk.xdr.TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(1, transactionEnvelope.getV0().getTx().getOperations().length);

    ManageSellOfferOperation op = (ManageSellOfferOperation) Operation.fromXdr(transactionEnvelope.getV0().getTx().getOperations()[0]);

    assertEquals("3397.893306099996", op.getPrice());
}
 
Example 14
Source File: OperationTest.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testManageBuyOfferOperation_BadArithmeticRegression() throws IOException {
    // from https://github.com/stellar/java-stellar-sdk/issues/183

    String transactionEnvelopeToDecode = "AAAAAButy5zasS3DLZ5uFpZHL25aiHUfKRwdv1+3Wp12Ce7XAAAAZAEyGwYAAAAxAAAAAAAAAAAAAAABAAAAAQAAAAAbrcuc2rEtwy2ebhaWRy9uWoh1HykcHb9ft1qddgnu1wAAAAwAAAABS0lOAAAAAABGStPbx5szphDKFVmLW22XCr92TqKNOnJM24cgyBSn4QAAAAAAAAAAACNyOCfDKZ0AAv7oAAAAAAABv1IAAAAAAAAAAA==";
    BaseEncoding base64Encoding = BaseEncoding.base64();
    byte[] bytes = base64Encoding.decode(transactionEnvelopeToDecode);

    org.stellar.sdk.xdr.TransactionEnvelope transactionEnvelope = org.stellar.sdk.xdr.TransactionEnvelope.decode(new XdrDataInputStream(new ByteArrayInputStream(bytes)));
    assertEquals(1, transactionEnvelope.getV0().getTx().getOperations().length);

    ManageBuyOfferOperation op = (ManageBuyOfferOperation) Operation.fromXdr(transactionEnvelope.getV0().getTx().getOperations()[0]);

    assertEquals("3397.893306099996", op.getPrice());
}
 
Example 15
Source File: Sep10Challenge.java    From java-stellar-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Reads a SEP 10 challenge transaction and returns the decoded transaction envelope and client account ID contained within.
 * <p>
 * It also verifies that transaction is signed by the server.
 * <p>
 * It does not verify that the transaction has been signed by the client or
 * that any signatures other than the servers on the transaction are valid. Use
 * one of the following functions to completely verify the transaction:
 * {@link Sep10Challenge#verifyChallengeTransactionSigners(String, String, Network, Set)} or
 * {@link Sep10Challenge#verifyChallengeTransactionThreshold(String, String, Network, int, Set)}
 *
 * @param challengeXdr    SEP-0010 transaction challenge transaction in base64.
 * @param serverAccountId Account ID for server's account.
 * @param network         The network to connect to for verifying and retrieving.
 * @return {@link ChallengeTransaction}, the decoded transaction envelope and client account ID contained within.
 * @throws InvalidSep10ChallengeException If the SEP-0010 validation fails, the exception will be thrown.
 * @throws IOException                    If read XDR string fails, the exception will be thrown.
 */
public static ChallengeTransaction readChallengeTransaction(String challengeXdr, String serverAccountId, Network network) throws InvalidSep10ChallengeException, IOException {
  // decode the received input as a base64-urlencoded XDR representation of Stellar transaction envelope
  AbstractTransaction parsed = Transaction.fromEnvelopeXdr(challengeXdr, network);
  if (!(parsed instanceof Transaction)) {
    throw new InvalidSep10ChallengeException("Transaction cannot be a fee bump transaction");
  }
  Transaction transaction = (Transaction)parsed;

  if (StrKey.decodeVersionByte(serverAccountId) != StrKey.VersionByte.ACCOUNT_ID) {
    throw new InvalidSep10ChallengeException("serverAccountId: "+serverAccountId+" is not a valid account id");
  }

  // verify that transaction source account is equal to the server's signing key
  if (!serverAccountId.equals(transaction.getSourceAccount())) {
    throw new InvalidSep10ChallengeException("Transaction source account is not equal to server's account.");
  }

  // verify that transaction sequenceNumber is equal to zero
  if (transaction.getSequenceNumber() != 0L) {
    throw new InvalidSep10ChallengeException("The transaction sequence number should be zero.");
  }

  // verify that transaction has time bounds set, and that current time is between the minimum and maximum bounds.
  if (transaction.getTimeBounds() == null) {
    throw new InvalidSep10ChallengeException("Transaction requires timebounds.");
  }

  long maxTime = transaction.getTimeBounds().getMaxTime();
  long minTime = transaction.getTimeBounds().getMinTime();
  if (maxTime == 0L) {
    throw new InvalidSep10ChallengeException("Transaction requires non-infinite timebounds.");
  }

  long currentTime = System.currentTimeMillis() / 1000L;
  if (currentTime < minTime || currentTime > maxTime) {
    throw new InvalidSep10ChallengeException("Transaction is not within range of the specified timebounds.");
  }

  // verify that transaction contains a single Manage Data operation and its source account is not null
  if (transaction.getOperations().length != 1) {
    throw new InvalidSep10ChallengeException("Transaction requires a single ManageData operation.");
  }
  Operation operation = transaction.getOperations()[0];
  if (!(operation instanceof ManageDataOperation)) {
    throw new InvalidSep10ChallengeException("Operation type should be ManageData.");
  }
  ManageDataOperation manageDataOperation = (ManageDataOperation) operation;

  // verify that transaction envelope has a correct signature by server's signing key
  String clientAccountId = manageDataOperation.getSourceAccount();
  if (clientAccountId == null) {
    throw new InvalidSep10ChallengeException("Operation should have a source account.");
  }

  if (StrKey.decodeVersionByte(clientAccountId) != StrKey.VersionByte.ACCOUNT_ID) {
    throw new InvalidSep10ChallengeException("clientAccountId: "+clientAccountId+" is not a valid account id");
  }

  // verify manage data value
  if (manageDataOperation.getValue().length != 64) {
    throw new InvalidSep10ChallengeException("Random nonce encoded as base64 should be 64 bytes long.");
  }

  BaseEncoding base64Encoding = BaseEncoding.base64();
  byte[] nonce;
  try {
    nonce = base64Encoding.decode(new String(manageDataOperation.getValue()));
  } catch (IllegalArgumentException e) {
    throw new InvalidSep10ChallengeException("Failed to decode random nonce provided in ManageData operation.", e);
  }

  if (nonce.length != 48) {
    throw new InvalidSep10ChallengeException("Random nonce before encoding as base64 should be 48 bytes long.");
  }

  if (!verifyTransactionSignature(transaction, serverAccountId)) {
    throw new InvalidSep10ChallengeException(String.format("Transaction not signed by server: %s.", serverAccountId));
  }

  return new ChallengeTransaction(transaction, clientAccountId);
}
 
Example 16
Source File: AccountResponse.java    From java-stellar-sdk with Apache License 2.0 2 votes vote down vote up
/**
 * Gets raw value for a given key.
 * @param key Data entry name
 * @return raw value
 */
public byte[] getDecoded(String key) {
  BaseEncoding base64Encoding = BaseEncoding.base64();
  return base64Encoding.decode(this.get(key));
}