Escrow bounty create-escrow tx

This commit is contained in:
Tushar Pardhe
2023-01-24 22:23:42 +00:00
parent 61e0902d78
commit 858e19dfb7
2 changed files with 50 additions and 49 deletions

View File

@@ -1,57 +1,57 @@
'use strict' '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 cc = require('five-bells-condition')
const crypto = require('crypto') const crypto = require('crypto')
const myAddr = 'rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn' const main = async () => {
const mySecret = 's████████████████████████████' try {
// Construct condition and fulfillment // Construct condition and fulfillment ---------------------------------------
const preimageData = crypto.randomBytes(32) const preimageData = crypto.randomBytes(32)
const myFulfillment = new cc.PreimageSha256() const myFulfillment = new cc.PreimageSha256()
myFulfillment.setPreimage(preimageData) myFulfillment.setPreimage(preimageData)
const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase() const conditionHex = myFulfillment.getConditionBinary().toString('hex').toUpperCase()
console.log('Condition:', conditionHex) console.log('Condition:', conditionHex)
console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase()) console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase())
// Construct transaction // Connect -------------------------------------------------------------------
const currentTime = new Date() const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
const myEscrow = { await client.connect()
"destination": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn", // Destination can be same as source
"destinationTag": 2017, // Get credentials from the Testnet Faucet -----------------------------------
"amount": "0.1113", //decimal XRP console.log("Requesting an address from the Testnet faucet...")
"condition": conditionHex, const { wallet, balance } = await client.fundWallet();
"allowExecuteAfter": currentTime.toISOString() // can be executed right away if the condition is met console.log("Wallet: ",wallet.address);
}
const myInstructions = { const firstRippleEpoch = 946684800;
maxLedgerVersionOffset: 5 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 main()
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)

View File

@@ -3,8 +3,9 @@
"version": "0.0.1", "version": "0.0.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"five-bells-condition": "*",
"ripple-lib": "^0.17.6", "ripple-lib": "^0.17.6",
"five-bells-condition": "*" "xrpl": "^2.8.0-beta.0"
}, },
"//": "Intended for Node.js version ?.? and higher" "//": "Intended for Node.js version ?.? and higher"
} }