diff --git a/content/_code-samples/escrow/js/create-escrow.js b/content/_code-samples/escrow/js/create-escrow.js index 9c977a6c29..a777a29cf8 100644 --- a/content/_code-samples/escrow/js/create-escrow.js +++ b/content/_code-samples/escrow/js/create-escrow.js @@ -1,57 +1,57 @@ 'use strict' -const RippleAPI = require('ripple-lib').RippleAPI +if (typeof module !== "undefined") { + // Use var here because const/let are block-scoped to the if statement. + var xrpl = require('xrpl') +} + const cc = require('five-bells-condition') const crypto = require('crypto') -const myAddr = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn' -const mySecret = 's████████████████████████████' +const main = async () => { + try { -// Construct condition and fulfillment -const preimageData = crypto.randomBytes(32) -const myFulfillment = new cc.PreimageSha256() -myFulfillment.setPreimage(preimageData) -const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase() + // Construct condition and fulfillment --------------------------------------- + const preimageData = crypto.randomBytes(32) + const myFulfillment = new cc.PreimageSha256() + myFulfillment.setPreimage(preimageData) + const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase() -console.log('Condition:', conditionHex) -console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase()) + console.log('Condition:', conditionHex) + console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase()) -// Construct transaction -const currentTime = new Date() -const myEscrow = { - "destination": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", // Destination can be same as source - "destinationTag": 2017, - "amount": "0.1113", //decimal XRP - "condition": conditionHex, - "allowExecuteAfter": currentTime.toISOString() // can be executed right away if the condition is met -} -const myInstructions = { - maxLedgerVersionOffset: 5 + // Connect ------------------------------------------------------------------- + const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233') + await client.connect() + + // Get credentials from the Testnet Faucet ----------------------------------- + console.log("Requesting an address from the Testnet faucet...") + const { wallet, balance } = await client.fundWallet(); + console.log("Wallet: ",wallet.address); + + const firstRippleEpoch = 946684800; + const escrowCreateTransaction = { + "TransactionType": "EscrowCreate", + "Account": wallet.address, + "Destination": wallet.address, + "Amount": "6000000", //drops XRP + "DestinationTag": 2023, + "Condition": conditionHex, + "Fee": "12", + "FinishAfter": Math.floor((new Date().getTime() / 1000) + 120) - firstRippleEpoch, // 2 minutes from now + }; + + xrpl.validate(escrowCreateTransaction); + + // Sign and submit the transaction -------------------------------------------- + console.log('Signing and submitting the transaction:', escrowCreateTransaction); + const response = await client.submitAndWait(escrowCreateTransaction, { wallet }); + console.log(`Finished submitting! ${JSON.stringify(response.result)}`); + + await client.disconnect(); + + } catch (error) { + console.log(error); + } } -// Connect and submit -const api = new RippleAPI({server: 'wss://s2.ripple.com'}) - -function submitTransaction(lastClosedLedgerVersion, prepared, secret) { - const signedData = api.sign(prepared.txJSON, secret) - console.log('Transaction ID: ', signedData.id) - return api.submit(signedData.signedTransaction).then(data => { - console.log('Tentative Result: ', data.resultCode) - console.log('Tentative Message: ', data.resultMessage) - }) -} - -api.connect().then(() => { - console.log('Connected') - return api.prepareEscrowCreation(myAddr, myEscrow, myInstructions) -}).then(prepared => { - console.log('EscrowCreation Prepared') - return api.getLedger().then(ledger => { - console.log('Current Ledger', ledger.ledgerVersion) - return submitTransaction(ledger.ledgerVersion, prepared, mySecret) - }) -}).then(() => { - api.disconnect().then(() => { - console.log('api disconnected') - process.exit() - }) -}).catch(console.error) +main() diff --git a/content/_code-samples/escrow/js/package.json b/content/_code-samples/escrow/js/package.json index 4b11c93d87..242a5040f7 100644 --- a/content/_code-samples/escrow/js/package.json +++ b/content/_code-samples/escrow/js/package.json @@ -3,8 +3,9 @@ "version": "0.0.1", "license": "MIT", "dependencies": { + "five-bells-condition": "*", "ripple-lib": "^0.17.6", - "five-bells-condition": "*" + "xrpl": "^2.8.0-beta.0" }, "//": "Intended for Node.js version ?.? and higher" }