wietsewind's notebooks

  • Get Account Transactions with ripple-lib - /wietsewind/get-account-transactions-with-ripple-lib
    Last edited 4 months ago
    const RippleAPI = require('ripple-lib').RippleAPI const api = new RippleAPI({ server: 'wss://rippled.xrptipbot.com' }) api.connect().then(() => { api.getServerInfo().then(server => { console.log('Connected', server) api.getTransactions('rf8kg7r5Fc8cCszGdD2jeUZt2FrgQd76BS', { limit: 10, earliestFirst: true }).then(transactions => { console.log(transactions) api.disconnect() }).catch(console.error) }) }).catch(console.error)
  • Base64 URL - /wietsewind/base64-url
    Last edited 4 months ago
    const base64url = require('base64url') let data = 'd2lldHNld2luZCE' console.log(base64url.decode(data))
  • Test rippled instance (Endpoint) - /wietsewind/test-rippled-instance-endpoint
    Last edited 4 months ago
    const WebSocket = require('websocket').client const ServersToTest = [ 'rippled-dev.xrpayments.co', 'rippled.xrptipbot.com', 's1.ripple.com' ] exports.endpoint = (request, response) => { Promise.all(ServersToTest.map(Server => { return new Promise((resolve, reject) => { let WsInstance = new WebSocket() WsInstance.connect('wss://' + Server) WsInstance.on('connectFailed', error => { console.log('Connect Error ' + Server, error.toString()) resolve({ server: Server, status: 'ConnectError', data: null, errorData: error.toString() }) }) WsInstance.on('connect', c => { // console.log('Connected ' + Server, c) c.on('message', m => { let data = JSON.parse(m.utf8Data) // console.log('Message ' + Server, data) resolve({ server: Server, status: 'Response', data: data, errorData: null }) c.close() }) c.on('close', () => { // console.log('Closed ' + Server) }) c.on('error', e => { console.log('Error ' + Server, e.toString()) resolve({ server: Server, status: 'MsgError', data: null, errorData: e.toString() }) }) c.send(JSON.stringify({ command: 'server_info' })) setTimeout(() => { c.close() resolve({ server: Server, status: 'Timeout', data: null, errorData: null }) }, 5000) }) }) })).then(Results => { // Running in the browser, respond let highestLedger = Math.max.apply(Math, Results.filter(r => { return r.status === 'Response' && r.data !== null && r.data.result.info.complete_ledgers.match(/[0-9]/) }).map(r => { return parseInt(r.data.result.info.complete_ledgers.split(',').reverse()[0].split('-').reverse()[0]) }).filter(r => { return !isNaN(r) })) let OutputData = { highestLedger: highestLedger, responses: Object.assign({}, ...Results.map(r => { let lastLedger = 0 if (r.data !== null) { lastLedger = parseInt(r.data.result.info.complete_ledgers.split(',').reverse()[0].split('-').reverse()[0]) if (isNaN(lastLedger)) lastLedger = 0 } return { [r.server.replace(/[^a-z0-9]/g, '')]: Object.assign(r, { lastLedger: lastLedger, ledgerOffset: highestLedger - lastLedger }) } })) } if (response !== null) { response.end(JSON.stringify(OutputData)) } else { // Running in the console. Log. console.log('Done', OutputData) } }).catch(Error => { }) } // When in the browser... exports.endpoint(null, null)
  • Axios multiple calls, one promise - /wietsewind/axios-multiple-calls-one-promise
    Last edited 4 months ago
    const axios = require('axios') const checkMultipleAddresses = (addresses) => { return Promise.all( addresses.map((address, index) => { return new Promise((resolve, reject) => { axios.get('https://bithomp.com/api/v1/userinfo/' + address).then(result => { resolve(Object.assign(result.data, { __index: index })) }).catch(e => { // Got an Error from the HTTP call, but we handle it as OK (resolve instead of reject) resolve({ address: address, error: true, message: e.toString(), __index: index }) }) }) }) ) } checkMultipleAddresses([ 'rDpsdD9vTwkf1GiNkxunLLDBXsEXG9GNmk', 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', 'rPEPPER7kfTD9w2To4CQk6UCfuHM9c6GDY' ]).then(results => { results.sort((a, b) => { return a.__index - b.__index }) console.log('Multiple addresses result:', results) })
  • Ripple-lib valid addr. (promise) - /wietsewind/ripple-lib-valid-addr-promise
    Last edited 4 months ago
    const RippleAPI = require('ripple-lib').RippleAPI const RippleAddressCodec = require('ripple-address-codec') const api = new RippleAPI({ server: 'wss://s2.ripple.com:443' }) /** * Note: isValidAddress will probably be exposed in a future ripple-lib release at api level. */ api.connect().then(() => { return new Promise((resolve, reject) => { if (RippleAddressCodec.isValidAddress("rBwdSqfFMBYUazUsv9pV1ZpHTG2nZcaNJv")) { resolve(true) } else { reject(new Error('Invalid XRPL account')) } }) }).then(r => { console.log('Valid?', r) }).then(() => { console.log('Disconnect') api.disconnect() }) .catch(e => console.log("Caught error", e))
  • Promise sequence sample - /wietsewind/promise-sequence-sample
    Last edited 4 months ago
    let MySampleFunction = () => { return new Promise((resolve, reject) => { setTimeout(() => { let theText = 'initial function' console.log(theText) resolve(theText) }, 1000) }) } MySampleFunction() .then((input) => { return new Promise((resolve, reject) => { let theText = 'second function' setTimeout(() => { console.log(Math.round(new Date() / 1000) + ': ' + input + ', ' + theText) resolve(theText) }, 1000) }) }) .then((input) => { return new Promise((resolve, reject) => { let theText = 'third function' setTimeout(() => { console.log(Math.round(new Date() / 1000) + ': ' + input + ', ' + theText) resolve(theText) // reject(new Error('Sucks to be you ;)')) }, 1000) }) }) .then((input) => { return new Promise((resolve, reject) => { let theText = 'fourth function' setTimeout(() => { console.log(Math.round(new Date() / 1000) + ': ' + input + ', ' + theText) resolve(theText) }, 1000) }) }) .catch((e) => { console.log('Shit hit the fan', e) })
  • XRP Tx gt 20.000 XRP - /wietsewind/xrp-tx-gt-20-000-xrp
    Last edited 4 months ago
    const websocket = require('websocket') const RippledWsClient = require('rippled-ws-client') // Set these values and then check the magic at the bottom of this script const minAmount = 20000 // Min amount in XRP const account = 'rEb8TK3gBgk5auZkwc6sHnwrGVJH8DuaLh' const fetchData = function (connection, lastSeenLedgerAndTx) { return new Promise((resolve, reject) => { let hasSeenOverlappingTx = typeof lastSeenLedgerAndTx === 'undefined' let txs = [] let command = { command: 'account_tx', account: account, ledger_index_max: (typeof lastSeenLedgerAndTx !== 'undefined' ? lastSeenLedgerAndTx.Ledger : -1) } connection.send(command).then((r) => { let lastSeen = null r.transactions.forEach((i) => { if (i.tx.TransactionType === 'Payment' && parseInt(i.tx.Amount) >= minAmount * 1000 * 1000) { if (hasSeenOverlappingTx) { txs.push({ Amount: parseInt(i.tx.Amount) / 1000 / 1000, From: i.tx.Account, To: i.tx.Destination, Ledger: i.tx.inLedger, Hash: i.tx.hash }) lastSeen = { Ledger: i.tx.inLedger, Hash: i.tx.hash } } } if (!hasSeenOverlappingTx && i.tx.hash === lastSeenLedgerAndTx.Hash) { hasSeenOverlappingTx = true } }) resolve({ // let nextSeq = r.marker.seq txs: txs, next () { return fetchData(connection, lastSeen) } }) }) }) } new RippledWsClient('wss://s2.ripple.com').then(function (connection) { fetchData(connection).then((data) => { console.log(data.txs) // data.txs = array with transactions, // data.next = promise with the next set of results data.next().then((moreData) => { console.log(moreData.txs) }) }) }).catch(function (error) { // Oops! })
  • Mini Promise Sample - /wietsewind/mini-promise-sample
    Last edited 4 months ago
    const times2 = (i) => { return new Promise((resolve, reject) => { // setTimeout(() => { // reject(new Error('Timeout, duurde te lang')) // }, 900) setTimeout(() => { let o = (i || 1) * 2 console.log(o) resolve(o) }, 1000) }) } times2(10) .then(times2).then((i) => { return new Promise((resolve, reject) => { let o = i * 10 resolve(o) // Nothing happens from here since the promise resolved // console.log(o) let myError = new Error('Error thingy') myError.dummyValue = 'Pepper & Dino' reject(myError) }) }) .then(times2) .catch((c) => { console.log('Stuff broke', c.dummyValue) })
  • Recover 1char Ripple Secret - /wietsewind/recover-1char-ripple-secret
    Last edited 4 months ago
    const kp = require('ripple-keypairs') // The secret below is a secret with one char missing. let secret = 'shx31Co5jta7Jp2ky1govCC8DSRb' let len = secret.length + 1 let alph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890' let alphlen = alph.length for(let i = 1; i < len; i++) { for(let l = 0; l < alphlen; l++){ let gensecret = secret.substring(0, i) + alph.substring(l, l + 1) + secret.substring(i) try { let pair = kp.deriveKeypair(gensecret) let wallet = kp.deriveAddress(pair.publicKey) console.log('Secret', gensecret) console.log('Address', wallet) process.exit(0) } catch (e) { } } } return
  • HEX Private key to address with ripple-lib - /wietsewind/hex-private-key-to-address-with-ripple-lib
    Last edited 4 months ago
    const keypairs = require('ripple-keypairs') const elliptic = require('elliptic') const secp256k1 = elliptic.ec('secp256k1') const ed25519 = elliptic.eddsa('ed25519') function bytesToHex(a) { return a.map(function(byteValue) { const hex = byteValue.toString(16).toUpperCase() return hex.length > 1 ? hex : '0' + hex }).join('') } /* secp256k1 */ let secp256k1_privateKey = '00' + '0762EED5BA4F378FFA60621C6DEF72F4A0A579112ADA5F5D6B2A35EC27E893A5' let secp256k1_keypair = { privateKey: secp256k1_privateKey, publicKey: bytesToHex(secp256k1.keyFromPrivate(secp256k1_privateKey.slice(2)).getPublic().encodeCompressed()), } console.log('secp256k1 Keypair, Address ', secp256k1_keypair, keypairs.deriveAddress(secp256k1_keypair.publicKey)) /* ed25519 */ let ed25519_privateKey = 'ED' + '3AD7B2EC2CFD134C500867164E4E257FD703C2CF4915E93A8CFC0CC7C2DBA46B' let ed25519_keypair = { privateKey: ed25519_privateKey, publicKey: 'ED' + bytesToHex(ed25519.keyFromSecret(ed25519_privateKey.slice(2)).pubBytes()), } console.log('ed25519 Keypair, Address ', ed25519_keypair, keypairs.deriveAddress(ed25519_keypair.publicKey))