From 944679445a78e6405cff8b6e2e3d16a960504b2e Mon Sep 17 00:00:00 2001 From: ddawson Date: Tue, 26 Sep 2023 08:49:06 -0700 Subject: [PATCH] add escrow samples --- .../_code-samples/quickstart/js/8.escrow.html | 305 +++++++++++++++++ .../quickstart/js/9.escrow-condition.html | 306 ++++++++++++++++++ .../js/getConditionAndFulfillment.js | 17 + .../quickstart/js/ripplex8-escrow.js | 240 ++++++++++++++ .../js/ripplex9-escrow-condition.js | 101 ++++++ 5 files changed, 969 insertions(+) create mode 100644 content/_code-samples/quickstart/js/8.escrow.html create mode 100644 content/_code-samples/quickstart/js/9.escrow-condition.html create mode 100644 content/_code-samples/quickstart/js/getConditionAndFulfillment.js create mode 100644 content/_code-samples/quickstart/js/ripplex8-escrow.js create mode 100644 content/_code-samples/quickstart/js/ripplex9-escrow-condition.js diff --git a/content/_code-samples/quickstart/js/8.escrow.html b/content/_code-samples/quickstart/js/8.escrow.html new file mode 100644 index 0000000000..e7ba256b76 --- /dev/null +++ b/content/_code-samples/quickstart/js/8.escrow.html @@ -0,0 +1,305 @@ + + + Time-based Escrow Test Harness + + + + + + + + + + + + + +

Time-based Escrow Test Harness

+
+ Choose your ledger instance: +    + + +    + + +

+ +
+ +

+ + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Standby Account + + +
+
+ XRP Balance + + +
+
+ Amount + + +
+
+ Destination Account + + +
+
+ Escrow Finish (seconds) + + +
+
+ Escrow Cancel (seconds) + + +
+
+ Escrow Sequence Number + + +
+
+ + +
+ Public Key + + +
+
+ Private Key + + +
+
+ Seed + + +
+
+

+ +

+
+ + + + + + + +
+ +

+ +
+ +
+ +
+ +
+
+
+ + + + +
+ + + + + + + + + +
+ +

+ +
+ +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Operational Account + + +
+
+ XRP Balance + + +
+
+ Amount + + +
+
+ Destination + + +
+
+ Escrow Sequence Number + + +
+
+ Transaction to Look Up + + +
+
+ + + + +
+ Public Key + + +
+
+ Private Key + + +
+
+ Seed + + +
+
+

+ +

+
+
+
+
+ + \ No newline at end of file diff --git a/content/_code-samples/quickstart/js/9.escrow-condition.html b/content/_code-samples/quickstart/js/9.escrow-condition.html new file mode 100644 index 0000000000..c73d10b4d0 --- /dev/null +++ b/content/_code-samples/quickstart/js/9.escrow-condition.html @@ -0,0 +1,306 @@ + + + Conditional Escrow Test Harness + + + + + + + + + + + + + + +

Conditional Escrow Test Harness

+
+ Choose your ledger instance: +    + + +    + + +

+ +
+ +

+ + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Standby Account + + +
+
+ XRP Balance + + +
+
+ Amount + + +
+
+ Destination Account + + +
+
+ Escrow Condition + + +
+
+ Escrow Cancel (seconds) + + +
+
+ Escrow Sequence Number + + +
+
+ + +
+ Public Key + + +
+
+ Private Key + + +
+
+ Seed + + +
+
+

+ +

+
+ + + + + + + +
+ +

+ +
+ +
+ +
+ +
+
+
+ + + + +
+ + + + + + + + + +
+ +

+ +
+ +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Operational Account + + +
+
+ XRP Balance + + +
+
+ Amount + + +
+
+ Fulfillment Code + + +
+
+ Escrow Sequence Number + + +
+
+ Transaction to Look Up + + +
+
+ + + + +
+ Public Key + + +
+
+ Private Key + + +
+
+ Seed + + +
+
+

+ +

+
+
+
+
+ + \ No newline at end of file diff --git a/content/_code-samples/quickstart/js/getConditionAndFulfillment.js b/content/_code-samples/quickstart/js/getConditionAndFulfillment.js new file mode 100644 index 0000000000..3032187d62 --- /dev/null +++ b/content/_code-samples/quickstart/js/getConditionAndFulfillment.js @@ -0,0 +1,17 @@ +function getConditionAndFulfillment() { + + const cc = require('five-bells-condition') + const crypto = require('crypto') + + const preimageData = crypto.randomBytes(32) + const fulfillment = new cc.PreimageSha256() + fulfillment.setPreimage(preimageData) + + const condition = fulfillment.getConditionBinary().toString('hex').toUpperCase() + console.log('Condition:', condition) + + // Keep secret until you want to finish the escrow + const fulfillment_hex = fulfillment.serializeBinary().toString('hex').toUpperCase() + console.log('Fulfillment:', fulfillment_hex) +} +getConditionAndFulfillment() \ No newline at end of file diff --git a/content/_code-samples/quickstart/js/ripplex8-escrow.js b/content/_code-samples/quickstart/js/ripplex8-escrow.js new file mode 100644 index 0000000000..0066b6c5d9 --- /dev/null +++ b/content/_code-samples/quickstart/js/ripplex8-escrow.js @@ -0,0 +1,240 @@ +// ******************************************************* +// ************* Add Seconds to Current Date ************* +// ******************************************************* + +function addSeconds(numOfSeconds, date = new Date()) { + date.setSeconds(date.getSeconds() + numOfSeconds); + date = Math.floor(date / 1000) + date = date - 946684800 + + return date; +} + +// ******************************************************* +// ***************** Create Time Escrow ****************** +// ******************************************************* + +async function createTimeEscrow() { + + //-------------------------------------------- Prepare Finish and Cancel Dates + + let escrow_finish_date = new Date() + let escrow_cancel_date = new Date() + escrow_finish_date = addSeconds(parseInt(standbyEscrowFinishDateField.value)) + escrow_cancel_date = addSeconds(parseInt(standbyEscrowCancelDateField.value)) + + //------------------------------------------------------Connect to the Ledger + results = "Connecting to the selected ledger.\n" + standbyResultField.value = results + let net = getNet() + results = "Connecting to " + net + "....\n" + const client = new xrpl.Client(net) + await client.connect() + + results += "Connected. Creating time-based escrow.\n" + standbyResultField.value = results + + const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + const sendAmount = standbyAmountField.value + + results += "\nstandby_wallet.address: = " + standby_wallet.address + standbyResultField.value = results + + // ------------------------------------------------------- Prepare transaction + + const escrowTx = await client.autofill({ + "TransactionType": "EscrowCreate", + "Account": standby_wallet.address, + "Amount": xrpl.xrpToDrops(sendAmount), + "Destination": standbyDestinationField.value, + "FinishAfter": escrow_finish_date, + "CancelAfter": escrow_cancel_date + }) + + // ------------------------------------------------ Sign prepared instructions + const signed = standby_wallet.sign(escrowTx) + + // -------------------------------------------------------- Submit signed blob + const tx = await client.submitAndWait(signed.tx_blob) + results += "\nSequence Number (Save!): " + JSON.stringify(tx.result.Sequence) + results += "\n\nBalance changes: " + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) + operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) + standbyResultField.value = results + + // ----------------------------------------------Disconnect from the XRP Ledger + client.disconnect() + +} // End of createTimeEscrow() + +// ******************************************************* +// ***************** Finish Time Escrow ****************** +// ******************************************************* + +async function finishEscrow() { + + results = "Connecting to the selected ledger.\n" + operationalResultField.value = results + let net = getNet() + results = 'Connecting to ' + getNet() + '....' + const client = new xrpl.Client(net) + await client.connect() + + results += "\nConnected. Finishing escrow.\n" + operationalResultField.value = results + + const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const sendAmount = operationalAmountField.value + + results += "\noperational_wallet.address: = " + operational_wallet.address + operationalResultField.value = results + + // ------------------------------------------------------- Prepare transaction + // Note that the destination is hard coded. + const prepared = await client.autofill({ + "TransactionType": "EscrowFinish", + "Account": operationalAccountField.value, + "Owner": standbyAccountField.value, + "OfferSequence": parseInt(operationalEscrowSequenceField.value) + }) + + // ------------------------------------------------ Sign prepared instructions + const signed = operational_wallet.sign(prepared) + + // -------------------------------------------------------- Submit signed blob + const tx = await client.submitAndWait(signed.tx_blob) + results += "\nBalance changes: " + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + operationalResultField.value = results + standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) + operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) + + client.disconnect() +} // End of finishEscrow() + +// ******************************************************* +// ************** Get Standby Escrows ******************** +// ******************************************************* + +async function getStandbyEscrows() { + let net = getNet() + const client = new xrpl.Client(net) + results = 'Connecting to ' + getNet() + '....' + standbyResultField.value = results + + await client.connect() + results += '\nConnected.' + standbyResultField.value = results + + results= "\nGetting standby account escrows...\n" + const escrow_objects = await client.request({ + "id": 5, + "command": "account_objects", + "account": standbyAccountField.value, + "ledger_index": "validated", + "type": "escrow" + }) + results += JSON.stringify(escrow_objects.result, null, 2) + standbyResultField.value = results + + client.disconnect() +} // End of getStandbyEscrows() + +// ******************************************************* +// ***************** Get Op Escrows ********************** +// ******************************************************* + +async function getOperationalEscrows() { + let net = getNet() + const client = new xrpl.Client(net) + results = 'Connecting to ' + getNet() + '....' + operationalResultField.value = results + + await client.connect() + results += '\nConnected.' + operationalResultField.value = results + + results= "\nGetting operational account escrows...\n" + const escrow_objects = await client.request({ + "id": 5, + "command": "account_objects", + "account": operationalAccountField.value, + "ledger_index": "validated", + "type": "escrow" + }) + results += JSON.stringify(escrow_objects.result, null, 2) + operationalResultField.value = results + client.disconnect() + +} // End of getOperationalEscrows() + +// ******************************************************* +// ************** Get Transaction Info ******************* +// ******************************************************* + +async function getTransaction() { + let net = getNet() + const client = new xrpl.Client(net) + results = 'Connecting to ' + getNet() + '....' + operationalResultField.value = results + + await client.connect() + results += '\nConnected.' + operationalResultField.value = results + + results= "\nGetting transaction information...\n" + const tx_info = await client.request({ + "id": 1, + "command": "tx", + "transaction": operationalTransactionField.value, + }) + results += JSON.stringify(tx_info.result, null, 2) + operationalResultField.value = results + client.disconnect() + +} // End of getTransaction() + +// ******************************************************* +// ****************** Cancel Escrow ********************** +// ******************************************************* + +async function cancelEscrow() { + let net = getNet() + const client = new xrpl.Client(net) + results = 'Connecting to ' + getNet() + '....' + standbyResultField.value = results + + await client.connect() + results += '\nConnected.' + standbyResultField.value = results + + const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + + // ------------------------------------------------------- Prepare transaction + + const prepared = await client.autofill({ + "TransactionType": "EscrowCancel", + "Account": standby_wallet.address, + "Owner": standbyAccountField.value, + "OfferSequence": parseInt(standbyEscrowSequenceNumberField.value) + }) + + // ------------------------------------------------ Sign prepared instructions + const signed = standby_wallet.sign(prepared) + + // -------------------------------------------------------- Submit signed blob + const tx = await client.submitAndWait(signed.tx_blob) + + results += "\nBalance changes: " + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + standbyResultField.value = results + + standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) + operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) + + client.disconnect() +} \ No newline at end of file diff --git a/content/_code-samples/quickstart/js/ripplex9-escrow-condition.js b/content/_code-samples/quickstart/js/ripplex9-escrow-condition.js new file mode 100644 index 0000000000..8b12dbb905 --- /dev/null +++ b/content/_code-samples/quickstart/js/ripplex9-escrow-condition.js @@ -0,0 +1,101 @@ +// ******************************************************* +// ************* Create Conditional Escrow *************** +// ******************************************************* + +async function createConditionalEscrow() { + + //------------------------------------------------------Connect to the Ledger + results = "Connecting to the selected ledger.\n" + standbyResultField.value = results + let net = getNet() + results = "Connecting to " + net + "....\n" + const client = new xrpl.Client(net) + await client.connect() + + results += "Connected. Creating conditional escrow.\n" + standbyResultField.value = results + + const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + const sendAmount = standbyAmountField.value + + results += "\nstandby_wallet.address: = " + standby_wallet.address + standbyResultField.value = results + + let escrow_cancel_date = new Date() + escrow_cancel_date = addSeconds(parseInt(standbyEscrowCancelDateField.value)) + + // ------------------------------------------------------- Prepare transaction + + const escrowTx = await client.autofill({ + "TransactionType": "EscrowCreate", + "Account": standby_wallet.address, + "Amount": xrpl.xrpToDrops(sendAmount), + "Destination": standbyDestinationField.value, + "CancelAfter": escrow_cancel_date, + "Condition": standbyEscrowConditionField.value + }) + + // ------------------------------------------------ Sign prepared instructions + const signed = standby_wallet.sign(escrowTx) + + // -------------------------------------------------------- Submit signed blob + const tx = await client.submitAndWait(signed.tx_blob) + results += "\nSequence Number (Save!): " + JSON.stringify(tx.result.Sequence) + results += "\n\nBalance changes: " + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) + operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) + standbyResultField.value = results + + // ----------------------------------------------Disconnect from the XRP Ledger + client.disconnect() + +} // End of createTimeEscrow() + +// ******************************************************* +// ************** Finish Conditional Escrow ************** +// ******************************************************* + +async function finishConditionalEscrow() { + results = "Connecting to the selected ledger.\n" + operationalResultField.value = results + let net = getNet() + results += 'Connecting to ' + getNet() + '....' + const client = new xrpl.Client(net) + await client.connect() + results += "\nConnected. Finishing escrow.\n" + operationalResultField.value = results + + const operational_wallet = xrpl.Wallet.fromSeed(operationalSeedField.value) + const standby_wallet = xrpl.Wallet.fromSeed(standbySeedField.value) + const sendAmount = operationalAmountField.value + + results += "\noperational_wallet.address: = " + operational_wallet.address + operationalResultField.value = results + + // ------------------------------------------------------- Prepare transaction + + const prepared = await client.autofill({ + "TransactionType": "EscrowFinish", + "Account": operationalAccountField.value, + "Owner": standbyAccountField.value, + "OfferSequence": parseInt(operationalEscrowSequenceField.value), + "Condition": standbyEscrowConditionField.value, + "Fulfillment": operationalFulfillmentField.value + }) + + // ------------------------------------------------ Sign prepared instructions + const signed = operational_wallet.sign(prepared) + + // -------------------------------------------------------- Submit signed blob + const tx = await client.submitAndWait(signed.tx_blob) + results += "\nBalance changes: " + + JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2) + operationalResultField.value = results + standbyBalanceField.value = (await client.getXrpBalance(standby_wallet.address)) + operationalBalanceField.value = (await client.getXrpBalance(operational_wallet.address)) + + client.disconnect() + +} // End of finishEscrow()`` \ No newline at end of file