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();
// Construct a SingleKeySignatureService from the Wallet private key
PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(wallet.privateKey().get());
SingleKeySignatureService signatureService = new SingleKeySignatureService(privateKey);
PrivateKey privateKey = PrivateKey.fromBase16EncodedPrivateKey(
wallet.privateKey().get()
);
SingleKeySignatureService signatureService =
new SingleKeySignatureService(privateKey);
// Construct and sign the Payment
Payment payment = Payment.builder()
@@ -23,8 +26,10 @@ Payment payment = Payment.builder()
.sequence(UnsignedInteger.valueOf(16126889))
.signingPublicKey(signatureService.getPublicKey(KeyMetadata.EMPTY))
.build();
Payment payment = constructPayment(wallet.classicAddress(), signatureService.getPublicKey(KeyMetadata.EMPTY));
SignedTransaction<Payment> signedPayment = signatureService.sign(KeyMetadata.EMPTY, payment);
SignedTransaction<Payment> signedPayment = signatureService.sign(
KeyMetadata.EMPTY,
payment
);
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
// 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")
DerivedKeysSignatureService signatureService = new DerivedKeysSignatureService("shh"::getBytes, VersionType.ED25519);
// Construct a DerivedKeysSignatureService with a server secret
// (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.
String walletId = "sample-wallet";
@@ -56,7 +64,8 @@ KeyMetadata keyMetadata = KeyMetadata.builder()
// Get the public key and classic address for the given walletId
PublicKey publicKey = signatureService.getPublicKey(keyMetadata);
Address classicAddress = DefaultKeyPairService.getInstance().deriveAddress(publicKey.value());
Address classicAddress = DefaultKeyPairService.getInstance()
.deriveAddress(publicKey.value());
// Construct and sign the Payment
Payment payment = Payment.builder()
@@ -68,5 +77,6 @@ Payment payment = Payment.builder()
.signingPublicKey(publicKey.base16Encoded())
.build();
SignedTransaction<Payment> signedPayment = signatureService.sign(keyMetadata, payment);
SignedTransaction<Payment> signedPayment = signatureService
.sign(keyMetadata, payment);
System.out.println("Signed Payment: " + signedPayment.signedTransaction());

View File

@@ -1,16 +1,18 @@
// Example Credentials ----------------------------------------------------------
// Example Credentials --------------------------------------------------------
WalletFactory walletFactory = DefaultWalletFactory.getInstance();
Wallet testWallet = walletFactory.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", true).wallet();
Wallet testWallet = walletFactory
.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9", true)
.wallet();
// Get the Classic address from testWallet
Address classicAddress = testWallet.classicAddress();
System.out.println(classicAddress); // "rMCcNuTcajgw7YTgBy1sys3b89QqjUrMpH"
// Connect ----------------------------------------------------------------------
// Connect --------------------------------------------------------------------
HttpUrl rippledUrl = HttpUrl.get("https://s.altnet.rippletest.net:51234/");
XrplClient xrplClient = new XrplClient(rippledUrl);
// Prepare transaction ----------------------------------------------------------
// Prepare transaction --------------------------------------------------------
// Look up your Account Info
AccountInfoRequestParams requestParams = AccountInfoRequestParams.builder()
.ledgerIndex(LedgerIndex.VALIDATED)
@@ -24,14 +26,18 @@ FeeResult feeResult = xrplClient.fee();
XrpCurrencyAmount openLedgerFee = feeResult.drops().openLedgerFee();
// 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()
.orElseThrow(() -> new RuntimeException("LedgerIndex not available."));
// Workaround for https://github.com/XRPLF/xrpl4j/issues/84
UnsignedInteger lastLedgerSequence = UnsignedInteger.valueOf(
validatedLedger.plus(UnsignedLong.valueOf(4)).unsignedLongValue().intValue()
);
);
// Construct a Payment
Payment payment = Payment.builder()
@@ -45,20 +51,25 @@ Payment payment = Payment.builder()
.build();
System.out.println("Constructed Payment: " + payment);
// Sign transaction -------------------------------------------------------------
// Sign transaction -----------------------------------------------------------
// 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);
// 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());
// Submit transaction -----------------------------------------------------------
// Submit transaction ---------------------------------------------------------
SubmitResult<Transaction> submitResult = xrplClient.submit(signedPayment);
System.out.println(submitResult);
// Wait for validation ----------------------------------------------------------
// Wait for validation --------------------------------------------------------
boolean transactionValidated = false;
boolean transactionExpired = false;
while (!transactionValidated && !transactionExpired) {
@@ -67,7 +78,9 @@ while (!transactionValidated && !transactionExpired) {
LedgerRequestParams.builder().ledgerIndex(LedgerIndex.VALIDATED).build()
)
.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(
TransactionRequestParams.of(signedPayment.hash()),
@@ -75,7 +88,8 @@ while (!transactionValidated && !transactionExpired) {
);
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;
} else {
boolean lastLedgerSequenceHasPassed = FluentCompareTo.
@@ -90,13 +104,15 @@ while (!transactionValidated && !transactionExpired) {
}
}
// Check transaction results ----------------------------------------------------
// Check transaction results --------------------------------------------------
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 -> {
System.out.println("Result code: " + metadata.transactionResult());
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
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);
// Create a Wallet using a WalletFactory
final WalletFactory walletFactory = DefaultWalletFactory.getInstance();
final Wallet testWallet = walletFactory.randomWallet(true).wallet();
WalletFactory walletFactory = DefaultWalletFactory.getInstance();
Wallet testWallet = walletFactory.randomWallet(true).wallet();
// Get the Classic and X-Addresses from testWallet
final Address classicAddress = testWallet.classicAddress();
final XAddress xAddress = testWallet.xAddress();
Address classicAddress = testWallet.classicAddress();
XAddress xAddress = testWallet.xAddress();
System.out.println("Classic Address: " + classicAddress);
System.out.println("X-Address: " + xAddress);
// Fund the account using the testnet Faucet
final FaucetClient faucetClient = FaucetClient
FaucetClient faucetClient = FaucetClient
.construct(HttpUrl.get("https://faucet.altnet.rippletest.net"));
faucetClient.fundAccount(FundAccountRequest.of(classicAddress));
// Look up your Account Info
final AccountInfoRequestParams requestParams = AccountInfoRequestParams.of(classicAddress);
final AccountInfoResult accountInfoResult = xrplClient.accountInfo(requestParams);
AccountInfoRequestParams requestParams =
AccountInfoRequestParams.of(classicAddress);
AccountInfoResult accountInfoResult =
xrplClient.accountInfo(requestParams);
// Print the result
System.out.println(accountInfoResult);