Skip to content

Commit b08d7f0

Browse files
authored
Merge pull request #3329 from XRPLF/update_desktop_wallet_js_tutorial
Update Desktop Wallet (JS) tutorial
2 parents 18566fb + 56ac023 commit b08d7f0

File tree

13 files changed

+109
-305
lines changed

13 files changed

+109
-305
lines changed

_code-samples/build-a-desktop-wallet/js/0-hello/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ const { app, BrowserWindow } = require('electron')
33
const path = require('path')
44

55
/**
6-
* This is our main function, it creates our application window, preloads the code we will need to communicate
7-
* between the renderer Process and the main Process, loads a layout and performs the main logic
6+
* Main function: create application window, preload the code to communicate
7+
* between the renderer Process and the main Process, load a layout,
8+
* and perform the main logic.
89
*/
910
const createWindow = () => {
10-
11-
// Creates the application window
11+
// Create the application window
1212
const appWindow = new BrowserWindow({
1313
width: 1024,
1414
height: 768
1515
})
1616

17-
// Loads a layout
17+
// Load a layout
1818
appWindow.loadFile(path.join(__dirname, 'view', 'template.html'))
19-
2019
return appWindow
2120
}
2221

_code-samples/build-a-desktop-wallet/js/1-ledger-index/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
const { app, BrowserWindow } = require('electron')
22

33
const path = require('path')
4-
const xrpl = require("xrpl")
4+
// Ledger index code additions - start
5+
const xrpl = require('xrpl')
56

6-
const TESTNET_URL = "wss://s.altnet.rippletest.net:51233"
7+
const TESTNET_URL = 'wss://s.altnet.rippletest.net:51233'
78

89
/**
9-
* This function creates a WebService client, which connects to the XRPL and fetches the latest ledger index.
10+
* Create a WebSocket client, connect to the XRPL, and fetch the latest ledger index.
1011
*
1112
* @returns {Promise<number>}
1213
*/
1314
const getValidatedLedgerIndex = async () => {
1415
const client = new xrpl.Client(TESTNET_URL)
15-
1616
await client.connect()
17-
18-
// Reference: https://xrpl.org/ledger.html#ledger
17+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/ledger-methods/ledger
1918
const ledgerRequest = {
2019
"command": "ledger",
2120
"ledger_index": "validated"
2221
}
23-
2422
const ledgerResponse = await client.request(ledgerRequest)
25-
2623
await client.disconnect()
27-
2824
return ledgerResponse.result.ledger_index
2925
}
26+
// Ledger index code additions - end
3027

3128
/**
32-
* This is our main function, it creates our application window, preloads the code we will need to communicate
33-
* between the renderer Process and the main Process, loads a layout and performs the main logic
29+
* Main function: create application window, preload the code to communicate
30+
* between the renderer Process and the main Process, load a layout,
31+
* and perform the main logic.
3432
*/
3533
const createWindow = () => {
36-
37-
// Creates the application window
34+
// Create the application window
3835
const appWindow = new BrowserWindow({
3936
width: 1024,
4037
height: 768,
@@ -43,9 +40,8 @@ const createWindow = () => {
4340
},
4441
})
4542

46-
// Loads a layout
43+
// Load a layout
4744
appWindow.loadFile(path.join(__dirname, 'view', 'template.html'))
48-
4945
return appWindow
5046
}
5147

_code-samples/build-a-desktop-wallet/js/2-async/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const createWindow = () => {
2424
return appWindow
2525
}
2626

27+
// Step 2 changes - main whenReady function - start
2728
/**
28-
* This function creates a XRPL client, subscribes to 'ledger' events from the XRPL and broadcasts those by
29-
* dispatching the 'update-ledger-data' event which will be picked up by the frontend
29+
* Create an XRPL client, subscribe to 'ledger' events, and broadcast those by
30+
* dispatching an 'update-ledger-data' event to the frontend.
3031
*
3132
* @returns {Promise<void>}
3233
*/
@@ -38,7 +39,7 @@ const main = async () => {
3839
await client.connect()
3940

4041
// Subscribe client to 'ledger' events
41-
// Reference: https://xrpl.org/subscribe.html
42+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe
4243
await client.request({
4344
"command": "subscribe",
4445
"streams": ["ledger"]
@@ -51,3 +52,4 @@ const main = async () => {
5152
}
5253

5354
app.whenReady().then(main)
55+
// Step 2 changes - main whenReady function - end

_code-samples/build-a-desktop-wallet/js/3-account/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,30 @@ const main = async () => {
2929

3030
await client.connect()
3131

32-
// Reference: https://xrpl.org/subscribe.html
32+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe
3333
await client.request({
3434
"command": "subscribe",
3535
"streams": ["ledger"],
3636
"accounts": [address]
3737
})
3838

39-
// Reference: https://xrpl.org/subscribe.html#ledger-stream
39+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe#ledger-stream
4040
client.on("ledgerClosed", async (rawLedgerData) => {
4141
const ledger = prepareLedgerData(rawLedgerData)
4242
appWindow.webContents.send('update-ledger-data', ledger)
4343
})
4444

4545
// Initial Ledger Request -> Get account details on startup
46-
// Reference: https://xrpl.org/ledger.html
46+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/ledger-methods/ledger
4747
const ledgerResponse = await client.request({
4848
"command": "ledger"
4949
})
5050
const initialLedgerData = prepareLedgerData(ledgerResponse.result.closed.ledger)
5151
appWindow.webContents.send('update-ledger-data', initialLedgerData)
5252

53-
// Reference: https://xrpl.org/subscribe.html#transaction-streams
53+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe#transaction-streams
5454
client.on("transaction", async (transaction) => {
55-
// Reference: https://xrpl.org/account_info.html
55+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
5656
const accountInfoRequest = {
5757
"command": "account_info",
5858
"account": address,
@@ -64,7 +64,7 @@ const main = async () => {
6464
})
6565

6666
// Initial Account Request -> Get account details on startup
67-
// Reference: https://xrpl.org/account_info.html
67+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
6868
const accountInfoResponse = await client.request({
6969
"command": "account_info",
7070
"account": address,

_code-samples/build-a-desktop-wallet/js/4-tx-history/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ const main = async () => {
3030

3131
await client.connect()
3232

33-
// Reference: https://xrpl.org/subscribe.html
33+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe
3434
await client.request({
3535
"command": "subscribe",
3636
"streams": ["ledger"],
3737
"accounts": [address]
3838
})
3939

40-
// Reference: https://xrpl.org/subscribe.html#ledger-stream
40+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe#ledger-stream
4141
client.on("ledgerClosed", async (rawLedgerData) => {
4242
const ledger = prepareLedgerData(rawLedgerData)
4343
appWindow.webContents.send('update-ledger-data', ledger)
4444
})
4545

4646
// Wait for transaction on subscribed account and re-request account data
4747
client.on("transaction", async (transaction) => {
48-
// Reference: https://xrpl.org/account_info.html
48+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
4949
const accountInfoRequest = {
5050
"command": "account_info",
5151
"account": address,
@@ -61,7 +61,7 @@ const main = async () => {
6161
})
6262

6363
// Initial Account Request -> Get account details on startup
64-
// Reference: https://xrpl.org/account_info.html
64+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
6565
const accountInfoResponse = await client.request({
6666
"command": "account_info",
6767
"account": address,
@@ -71,7 +71,7 @@ const main = async () => {
7171
appWindow.webContents.send('update-account-data', accountData)
7272

7373
// Initial Transaction Request -> List account transactions on startup
74-
// Reference: https://xrpl.org/account_tx.html
74+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_tx
7575
const txResponse = await client.request({
7676
"command": "account_tx",
7777
"account": address

_code-samples/build-a-desktop-wallet/js/5-password/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const createWindow = () => {
2323
return appWindow
2424
}
2525

26+
// Step 5 - new main function - start
2627
const main = async () => {
2728
const appWindow = createWindow()
2829

@@ -51,6 +52,9 @@ const main = async () => {
5152
}
5253

5354
const wallet = xrpl.Wallet.fromSeed(seed)
55+
// For compatibility with seeds generated using secp256k1
56+
// (the old default algorithm), use the following instead:
57+
// const wallet = xrpl.Wallet.fromSeed(seed, {algorithm: "secp256k1"})
5458

5559
const client = new xrpl.Client(TESTNET_URL)
5660

@@ -80,5 +84,6 @@ const main = async () => {
8084
}
8185
})
8286
}
87+
// Step 5 - new main function - end
8388

8489
app.whenReady().then(main)

_code-samples/build-a-desktop-wallet/js/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Build a non-custodial XRP Ledger wallet application in JavaScript that runs on the desktop using Electron.
44

5-
For the full documentation, refer to the [Build a Wallet in JavaScript tutorial](https://xrpl.org/build-a-wallet-in-javascript.html).
5+
For the full documentation, refer to the [Build a Desktop Wallet in JavaScript tutorial](https://xrpl.org/docs/tutorials/javascript/build-apps/build-a-desktop-wallet-in-javascript).
66

77
## TL;DR
88

_code-samples/build-a-desktop-wallet/js/library/3_helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const xrpl = require("xrpl");
33
// The rippled server and its APIs represent time as an unsigned integer.
44
// This number measures the number of seconds since the "Ripple Epoch" of
55
// January 1, 2000 (00:00 UTC). This is like the way the Unix epoch works,
6-
// Reference: https://xrpl.org/basic-data-types.html
6+
// Reference: https://xrpl.org/docs/references/protocol/data-types/basic-data-types#specifying-time
77
const RIPPLE_EPOCH = 946684800;
88

99
const prepareAccountData = (rawAccountData) => {

_code-samples/build-a-desktop-wallet/js/library/5_helpers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const fernet = require("fernet");
1414
* @returns {Promise<void>}
1515
*/
1616
const initialize = async (client, wallet, appWindow) => {
17-
// Reference: https://xrpl.org/account_info.html
17+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
1818
const accountInfoResponse = await client.request({
1919
"command": "account_info",
2020
"account": wallet.address,
@@ -23,7 +23,7 @@ const initialize = async (client, wallet, appWindow) => {
2323
const accountData = prepareAccountData(accountInfoResponse.result.account_data)
2424
appWindow.webContents.send('update-account-data', accountData)
2525

26-
// Reference: https://xrpl.org/account_tx.html
26+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_tx
2727
const txResponse = await client.request({
2828
"command": "account_tx",
2929
"account": wallet.address
@@ -42,22 +42,22 @@ const initialize = async (client, wallet, appWindow) => {
4242
*/
4343
const subscribe = async (client, wallet, appWindow) => {
4444

45-
// Reference: https://xrpl.org/subscribe.html
45+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe
4646
await client.request({
4747
"command": "subscribe",
4848
"streams": ["ledger"],
4949
"accounts": [wallet.address]
5050
})
5151

52-
// Reference: https://xrpl.org/subscribe.html#ledger-stream
52+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/subscription-methods/subscribe#ledger-stream
5353
client.on("ledgerClosed", async (rawLedgerData) => {
5454
const ledger = prepareLedgerData(rawLedgerData)
5555
appWindow.webContents.send('update-ledger-data', ledger)
5656
})
5757

5858
// Wait for transaction on subscribed account and re-request account data
5959
client.on("transaction", async (transaction) => {
60-
// Reference: https://xrpl.org/account_info.html
60+
// Reference: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/account-methods/account_info
6161
const accountInfoRequest = {
6262
"command": "account_info",
6363
"account": wallet.address,

_code-samples/build-a-desktop-wallet/js/library/7_helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const xrpl = require("xrpl");
99
* @returns {Promise<*>}
1010
*/
1111
const sendXrp = async (paymentData, client, wallet) => {
12-
// Reference: https://xrpl.org/submit.html#request-format-1
12+
// Reference: https://xrpl.org/docs/references/protocol/transactions/types/payment
1313
const paymentTx = {
1414
"TransactionType": "Payment",
1515
"Account": wallet.address,

0 commit comments

Comments
 (0)