Table of Contents for
Ripple Quick Start Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Ripple Quick Start Guide by Febin John James Published by Packt Publishing, 2018
  1. Ripple Quick Start Guide
  2. Title Page
  3. Copyright and Credits
  4. Ripple Quick Start Guide
  5. Dedication
  6. About Packt
  7. Why subscribe?
  8. Packt.com
  9. Contributors
  10. About the author
  11. About the reviewer
  12. Packt is searching for authors like you
  13. Table of Contents
  14. Preface
  15. Who this book is for
  16. What this book covers
  17. To get the most out of this book
  18. Download the example code files
  19. Conventions used
  20. Get in touch
  21. Reviews
  22. Getting Started with Ripple
  23. The need for decentralization
  24. Introduction to blockchain
  25. Introduction to Bitcoin
  26. Inefficiencies in payment systems
  27. International money transfer through Bitcoin
  28. Disadvantages of Bitcoin
  29. Ripple
  30. International money transfer through Ripple
  31. The Ripple Protocol
  32. Account creation
  33. Reserve
  34. Transactions
  35. Multisigning
  36. Consensus
  37. Important properties of the consensus protocol
  38. Ledger versions
  39. Validation
  40. Advantages of Ripple
  41. Currency agnostic
  42. Simplified consensus
  43. Low fee
  44. Reduced foreign exchange cost
  45. Pathfinding algorithm
  46. Adaptable cryptography
  47. Anti-spam mechanism
  48. Potential risks of Ripple
  49. Regulatory issues
  50. Trust Issues
  51. Security vulnerabilities
  52. Problems of being an open protocol
  53. Summary
  54. Working with Ripple Currency XRP
  55. Types of wallets
  56. Online wallets
  57. Desktop/mobile wallets
  58. Offline wallets
  59. Hardware wallets
  60. Paper wallets
  61. How do I choose my wallet?
  62. Setting up a Ripple account
  63. Activating the Ripple account
  64. Making an international transfer
  65. Trading XRP
  66. Importing an existing wallet
  67. Setting up an offline wallet
  68. Protecting your Ripples
  69. Don't leave your Ripples on centralized exchanges
  70. Make backups 
  71. Use antivirus software
  72. Disable browser plugins
  73. Store Ripples in multiple wallets 
  74. For big sums, use cold wallets
  75. Use reputable wallets
  76. Important things you must remember
  77. Summary
  78. Applications of Ripple
  79. High speed and low-cost payments 
  80. xCurrent
  81. How does it work?
  82. Advanced payment applications
  83. Cross-currency payments
  84. How does it work?
  85. Checks
  86. How does it work?
  87. Payment channels
  88. How does it work?
  89. Escrow
  90. How does it work?
  91. Initial coin offering
  92. Decentralized exchange
  93. Debunking misconceptions about Ripple
  94. Ripple and XRP are not the same
  95. Funds lockup
  96. No mining
  97. Limited smart contracts
  98. Important things to remember
  99. Summary
  100. Getting Started with the Ripple API
  101. Connecting to the Ripple test network
  102. Setting up the development environment
  103. First Ripple application
  104. Sending money 
  105. Prepare transaction
  106. Sign transaction
  107. Submit transaction
  108. Summary
  109. Developing Applications Using the Ripple API
  110. Sending checks
  111. Cashing checks
  112. Creating a time-held escrow
  113. Creating a conditionally-held escrow
  114. Important things you must remember
  115. Summary
  116. Other Books You May Enjoy
  117. Leave a review - let other readers know what you think

First Ripple application

In our first application, we'll write a simple program to connect to the network and fetch the account details of a specific Ripple address.

Here's the code that connects to the Ripple test network and fetches the account details of the address: 'r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2'. Let's run this code first and then get into the details to learn how it works. Save the following code into a file and give it the name 'get_account.js'

'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;

const api = new RippleAPI({
server: 'wss://s.altnet.rippletest.net:51233' // Ripple Test Network Address
});

api.connect().then(() => {
const accountAddress = 'r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2';

console.log('Fetching account details of', accountAddress);
return api.getAccountInfo(accountAddress);

}).then(info => {
console.log(info);
console.log('Account Details Fetched');
}).then(() => {
return api.disconnect();
}).then(() => {
console.log('Disconnected from the testnet.');
}).catch(console.error);

In order to execute the code, use the following command: 

./node_modules/.bin/babel-node get_account.js

You'll see the following output. As you can see, the XRP balance says 10,000:

Now, let's understand how the code works. 

First, we import the Ripple library, so that we can use its functions. For importing the library, we use this command:

const RippleAPI = require('ripple-lib').RippleAPI;

Then, we define which network we want to connect to. We are connecting to the test network in the following line of code; you can connect to the main network or even a local server. If you want to connect to the main network, it's recommended you run your own rippled server since the public servers are not meant for business use. You can get more information about this in the Ripple Developer Docs. The following line of code connects us to the test network:

const api = new RippleAPI({
server: 'wss://s.altnet.rippletest.net:51233' // Ripple Test Network Address
});

Once we have defined which network we want to connect to, we'll make the connection by calling the connect function. Once the connection is made, we'll define the Ripple address for which the account details must be fetched. We're also making the calling to fetch account details using the function getAccountInfo as seen in the following code:

api.connect().then(() => {
const accountAddress = 'r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2';

console.log('Fetching account details of', accountAddress);
return api.getAccountInfo(accountAddress);

})

If the function call is successfully done, then we'll log the output from the function using the following code:

.then(info => {
console.log(info);
console.log('Account Details Fetched');
})

Since we've opened a WebSocket connection, we need to disconnect. If anything goes wrong during the entire process, we also need to catch the error. The following code takes care of these things:

.then(() => {
return api.disconnect();
}).then(() => {
console.log('Disconnected from the testnet.');
}).catch(console.error);

Now, let's use the following code to build a simple web app. Let's get this code running and then we'll get into the details of how it works. Some browsers allow execution of this code without giving issues. If your browser is throwing permission errors, please install the XAMP or WAMP server and place these files in the htdocs folder. Then, navigate to the respective URL:

<!DOCTYPE html>
<html>
<title> Get Account </title>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Get Account</a>
</nav>

<br/><br/><br/>

<center>
<p class="lead">Ripple Address : r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2 </p>
<p id="balance"> Please wait, fetching account details...</p>
</center>

</body>

<script src="js/lodash.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/ripple.min.js"></script>

<script>
var api = new ripple.RippleAPI({server:'wss://s.altnet.rippletest.net:51233'});

$('document').ready(function(){
updateAccountDetails();
});

function updateAccountDetails(){

api.connect().then(() => {
const accountAddress = 'r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2';
return api.getAccountInfo(accountAddress);
}).then(info => {
$('#balance').text("Account Balance : " + info.xrpBalance+ " XRP");
}).then(() => {
return api.disconnect();
}).catch(console.error);
}

</script>
</html>

Save this file as get_account.html. You also need to create two folders named js and css and place them in the same directory.

You will need the following javascript and css libraries for the app to work. You can download them from the following links:

Download these files and save them in the js and css folders. Make sure you rename the filenames to jquery.min.js and ripple.min.js

Alternatively, you can use the files in this book's GitHub repository. Now, you can open get_account.html on your browser. If everything goes well, you should be able to see the following screen: 

Let's now try to understand the code. 

We need the lodash and Ripple library to interface with the Ripple network as shown in the following. We'll be using jquery to manipulate the DOM elements:

<script src="js/lodash.js"></script>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/ripple.min.js"></script>

First, we wait for the document to get ready. Then, we connect to Ripple's test network and fetch the account details. The following lines of code parse and update the frontend using jquery as it receives data from Ripple:

var api = new ripple.RippleAPI({server:'wss://s.altnet.rippletest.net:51233'});

$('document').ready(function(){
updateAccountDetails();
});

function updateAccountDetails(){

api.connect().then(() => {
const accountAddress = 'r41sFTd4rftxY1VCn5ZDDipb4KaV5VLFy2';
return api.getAccountInfo(accountAddress);
}).then(info => {
$('#balance').text("Account Balance : " + info.xrpBalance+ " XRP");
}).then(() => {
return api.disconnect();
}).catch(console.error);
}

</script>

In the previous code, we hard-coded the Ripple address. Let's make it dynamic so that users can input the address and fetch balance of any Ripple account. To achieve this we need to make a few modifications. 

First, we need a form to input the Ripple address. Here's the code for the input form. When the user clicks on the fetch balance button, the update account details function mentioned in the previous block is triggered:

<form style="width:50%">
<div class="form-group">
<label for="inputRippleAddress">Ripple Address</label>
<input type="text" class="form-control" id="inputRippleAddress" aria-describedby="rippleAddressHelp" placeholder="Enter Ripple Address">
</div>
<button onclick="updateAccountDetails()" class="btn btn-primary">Fetch Balance</button>
</form>

Later, we disable the default form submit using the following lines of code:

 $("form").submit(function(e) {
e.preventDefault();
});

In the update account details function, we'll add the following line to fetch the address from the input form:

const accountAddress = $("#inputRippleAddress").val();

If you need help, you can refer to the source code in our GitHub repository

After making these modifications, you can run it on a browser. If everything goes well, you should be able to see the screen similar to this one:

Now any user can use this app to fetch the balance of any account on Ripple. 

Remember, we're still on the test network. If you want to fetch the account balance of addresses on the main network, you need to change the server URL. 

Congratulations, you have completed making your first app using Ripple's APIs.