shorten code sample line lengths to < 80

This commit is contained in:
nkramer44
2021-05-05 10:30:51 -04:00
parent 571ea65c2a
commit f9b92f1b33
3 changed files with 62 additions and 33 deletions

View File

@@ -11,8 +11,11 @@ WalletFactory walletFactory = DefaultWalletFactory.getInstance();
Wallet wallet = walletFactory.randomWallet(true).wallet(); Wallet wallet = walletFactory.randomWallet(true).wallet();
// Construct a SingleKeySignatureService from the Wallet private key // Construct a SingleKeySignatureService from the Wallet private key
PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(wallet.privateKey().get()); PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(
SingleKeySignatureService signatureService = new SingleKeySignatureService(privateKey); wallet.privateKey().get()
);
SingleKeySignatureService signatureService =
new SingleKeySignatureService(privateKey);
// Construct and sign the Payment // Construct and sign the Payment
Payment payment = Payment.builder() Payment payment = Payment.builder()
@@ -23,8 +26,10 @@ Payment payment = Payment.builder()
.sequence(UnsignedInteger.valueOf(16126889)) .sequence(UnsignedInteger.valueOf(16126889))
.signingPublicKey(signatureService.getPublicKey(KeyMetadata.EMPTY)) .signingPublicKey(signatureService.getPublicKey(KeyMetadata.EMPTY))
.build(); .build();
Payment payment = constructPayment(wallet.classicAddress(), signatureService.getPublicKey(KeyMetadata.EMPTY)); SignedTransaction<Payment> signedPayment = signatureService.sign(
SignedTransaction<Payment> signedPayment = signatureService.sign(KeyMetadata.EMPTY, payment); KeyMetadata.EMPTY,
payment
);
System.out.println("Signed Payment: " + signedPayment.signedTransaction()); System.out.println("Signed Payment: " + signedPayment.signedTransaction());
@@ -38,11 +43,14 @@ System.out.println("Signed Payment: " + signedPayment.signedTransaction());
// //
// Though this implementation is more secure than SingleKeySignatureService // Though this implementation is more secure than SingleKeySignatureService
// and better suited for server-side applications, keys are still held // and better suited for server-side applications, keys are still held
// in memory. For the best security, we suggest using a HSM-based implementation. // in memory. For the best security, we suggest using a HSM-based
// implementation.
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Construct a DerivedKeysSignatureService with a server secret (in this case "shh") // Construct a DerivedKeysSignatureService with a server secret
DerivedKeysSignatureService signatureService = new DerivedKeysSignatureService("shh"::getBytes, VersionType.ED25519); // (in this case "shh")
DerivedKeysSignatureService signatureService =
new DerivedKeysSignatureService("shh"::getBytes, VersionType.ED25519);
// Choose a walletId. This can be anything as long as it is unique to your system. // Choose a walletId. This can be anything as long as it is unique to your system.
String walletId = "sample-wallet"; String walletId = "sample-wallet";
@@ -56,7 +64,8 @@ KeyMetadata keyMetadata = KeyMetadata.builder()
// Get the public key and classic address for the given walletId // Get the public key and classic address for the given walletId
PublicKey publicKey = signatureService.getPublicKey(keyMetadata); PublicKey publicKey = signatureService.getPublicKey(keyMetadata);
Address classicAddress = DefaultKeyPairService.getInstance().deriveAddress(publicKey.value()); Address classicAddress = DefaultKeyPairService.getInstance()
.deriveAddress(publicKey.value());
// Construct and sign the Payment // Construct and sign the Payment
Payment payment = Payment.builder() Payment payment = Payment.builder()
@@ -68,5 +77,6 @@ Payment payment = Payment.builder()
.signingPublicKey(publicKey.base16Encoded()) .signingPublicKey(publicKey.base16Encoded())
.build(); .build();
SignedTransaction<Payment> signedPayment = signatureService.sign(keyMetadata, payment); SignedTransaction<Payment> signedPayment = signatureService
.sign(keyMetadata, payment);
System.out.println("Signed Payment: " + signedPayment.signedTransaction()); System.out.println("Signed Payment: " + signedPayment.signedTransaction());

View File

@@ -1,16 +1,18 @@
// Example Credentials ---------------------------------------------------------- // Example Credentials --------------------------------------------------------
WalletFactory walletFactory = DefaultWalletFactory.getInstance(); WalletFactory walletFactory = DefaultWalletFactory.getInstance();
Wallet testWallet = walletFactory.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", true).wallet(); Wallet testWallet = walletFactory
.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", true)
.wallet();
// Get the Classic address from testWallet // Get the Classic address from testWallet
Address classicAddress = testWallet.classicAddress(); Address classicAddress = testWallet.classicAddress();
System.out.println(classicAddress); // "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH" System.out.println(classicAddress); // "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
// Connect ---------------------------------------------------------------------- // Connect --------------------------------------------------------------------
HttpUrl rippledUrl = HttpUrl.get("https://s.altnet.rippletest.net:51234/"); HttpUrl rippledUrl = HttpUrl.get("https://s.altnet.rippletest.net:51234/");
XrplClient xrplClient = new XrplClient(rippledUrl); XrplClient xrplClient = new XrplClient(rippledUrl);
// Prepare transaction ---------------------------------------------------------- // Prepare transaction --------------------------------------------------------
// Look up your Account Info // Look up your Account Info
AccountInfoRequestParams requestParams = AccountInfoRequestParams.builder() AccountInfoRequestParams requestParams = AccountInfoRequestParams.builder()
.ledgerIndex(LedgerIndex.VALIDATED) .ledgerIndex(LedgerIndex.VALIDATED)
@@ -24,7 +26,11 @@ FeeResult feeResult = xrplClient.fee();
XrpCurrencyAmount openLedgerFee = feeResult.drops().openLedgerFee(); XrpCurrencyAmount openLedgerFee = feeResult.drops().openLedgerFee();
// Get the latest validated ledger index // Get the latest validated ledger index
LedgerIndex validatedLedger = xrplClient.ledger(LedgerRequestParams.builder().ledgerIndex(LedgerIndex.VALIDATED).build()) LedgerIndex validatedLedger = xrplClient.ledger(
LedgerRequestParams.builder()
.ledgerIndex(LedgerIndex.VALIDATED)
.build()
)
.ledgerIndex() .ledgerIndex()
.orElseThrow(() -> new RuntimeException("LedgerIndex not available.")); .orElseThrow(() -> new RuntimeException("LedgerIndex not available."));
@@ -45,20 +51,25 @@ Payment payment = Payment.builder()
.build(); .build();
System.out.println("Constructed Payment: " + payment); System.out.println("Constructed Payment: " + payment);
// Sign transaction ------------------------------------------------------------- // Sign transaction -----------------------------------------------------------
// Construct a SignatureService to sign the Payment // Construct a SignatureService to sign the Payment
PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(testWallet.privateKey().get()); PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(
testWallet.privateKey().get()
);
SignatureService signatureService = new SingleKeySignatureService(privateKey); SignatureService signatureService = new SingleKeySignatureService(privateKey);
// Sign the Payment // Sign the Payment
SignedTransaction<Payment> signedPayment = signatureService.sign(KeyMetadata.EMPTY, payment); SignedTransaction<Payment> signedPayment = signatureService.sign(
KeyMetadata.EMPTY,
payment
);
System.out.println("Signed Payment: " + signedPayment.signedTransaction()); System.out.println("Signed Payment: " + signedPayment.signedTransaction());
// Submit transaction ----------------------------------------------------------- // Submit transaction ---------------------------------------------------------
SubmitResult<Transaction> submitResult = xrplClient.submit(signedPayment); SubmitResult<Transaction> submitResult = xrplClient.submit(signedPayment);
System.out.println(submitResult); System.out.println(submitResult);
// Wait for validation ---------------------------------------------------------- // Wait for validation --------------------------------------------------------
boolean transactionValidated = false; boolean transactionValidated = false;
boolean transactionExpired = false; boolean transactionExpired = false;
while (!transactionValidated && !transactionExpired) { while (!transactionValidated && !transactionExpired) {
@@ -67,7 +78,9 @@ while (!transactionValidated && !transactionExpired) {
LedgerRequestParams.builder().ledgerIndex(LedgerIndex.VALIDATED).build() LedgerRequestParams.builder().ledgerIndex(LedgerIndex.VALIDATED).build()
) )
.ledgerIndex() .ledgerIndex()
.orElseThrow(() -> new RuntimeException("Ledger response did not contain a LedgerIndex.")); .orElseThrow(() ->
new RuntimeException("Ledger response did not contain a LedgerIndex.")
);
TransactionResult<Payment> transactionResult = xrplClient.transaction( TransactionResult<Payment> transactionResult = xrplClient.transaction(
TransactionRequestParams.of(signedPayment.hash()), TransactionRequestParams.of(signedPayment.hash()),
@@ -75,7 +88,8 @@ while (!transactionValidated && !transactionExpired) {
); );
if (transactionResult.validated()) { if (transactionResult.validated()) {
System.out.println("Payment was validated with result code " + transactionResult.metadata().get().transactionResult()); System.out.println("Payment was validated with result code " +
transactionResult.metadata().get().transactionResult());
transactionValidated = true; transactionValidated = true;
} else { } else {
boolean lastLedgerSequenceHasPassed = FluentCompareTo. boolean lastLedgerSequenceHasPassed = FluentCompareTo.
@@ -90,13 +104,15 @@ while (!transactionValidated && !transactionExpired) {
} }
} }
// Check transaction results ---------------------------------------------------- // Check transaction results --------------------------------------------------
System.out.println(transactionResult); System.out.println(transactionResult);
System.out.println("Explorer link: https://testnet.xrpl.org/transactions/" + signedPayment.hash()); System.out.println("Explorer link: https://testnet.xrpl.org/transactions/" +
signedPayment.hash());
transactionResult.metadata().ifPresent(metadata -> { transactionResult.metadata().ifPresent(metadata -> {
System.out.println("Result code: " + metadata.transactionResult()); System.out.println("Result code: " + metadata.transactionResult());
metadata.deliveredAmount().ifPresent(deliveredAmount -> metadata.deliveredAmount().ifPresent(deliveredAmount ->
System.out.println("XRP Delivered: " + ((XrpCurrencyAmount) deliveredAmount).toXrp()) System.out.println("XRP Delivered: " +
((XrpCurrencyAmount) deliveredAmount).toXrp())
); );
}); });

View File

@@ -1,25 +1,28 @@
// Construct a network client // Construct a network client
final HttpUrl rippledUrl = HttpUrl.get("https://s.altnet.rippletest.net:51234/"); HttpUrl rippledUrl = HttpUrl
.get("https://s.altnet.rippletest.net:51234/");
XrplClient xrplClient = new XrplClient(rippledUrl); XrplClient xrplClient = new XrplClient(rippledUrl);
// Create a Wallet using a WalletFactory // Create a Wallet using a WalletFactory
final WalletFactory walletFactory = DefaultWalletFactory.getInstance(); WalletFactory walletFactory = DefaultWalletFactory.getInstance();
final Wallet testWallet = walletFactory.randomWallet(true).wallet(); Wallet testWallet = walletFactory.randomWallet(true).wallet();
// Get the Classic and X-Addresses from testWallet // Get the Classic and X-Addresses from testWallet
final Address classicAddress = testWallet.classicAddress(); Address classicAddress = testWallet.classicAddress();
final XAddress xAddress = testWallet.xAddress(); XAddress xAddress = testWallet.xAddress();
System.out.println("Classic Address: " + classicAddress); System.out.println("Classic Address: " + classicAddress);
System.out.println("X-Address: " + xAddress); System.out.println("X-Address: " + xAddress);
// Fund the account using the testnet Faucet // Fund the account using the testnet Faucet
final FaucetClient faucetClient = FaucetClient FaucetClient faucetClient = FaucetClient
.construct(HttpUrl.get("https://faucet.altnet.rippletest.net")); .construct(HttpUrl.get("https://faucet.altnet.rippletest.net"));
faucetClient.fundAccount(FundAccountRequest.of(classicAddress)); faucetClient.fundAccount(FundAccountRequest.of(classicAddress));
// Look up your Account Info // Look up your Account Info
final AccountInfoRequestParams requestParams = AccountInfoRequestParams.of(classicAddress); AccountInfoRequestParams requestParams =
final AccountInfoResult accountInfoResult = xrplClient.accountInfo(requestParams); AccountInfoRequestParams.of(classicAddress);
AccountInfoResult accountInfoResult =
xrplClient.accountInfo(requestParams);
// Print the result // Print the result
System.out.println(accountInfoResult); System.out.println(accountInfoResult);