wietsewind's notebooks

  • Axios multiple calls, one promise - /wietsewind/axios-multiple-calls-one-promise
    Last edited 5 years 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 5 years 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 5 years 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 5 years 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 5 years 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) })
  • Check XRP Wallet addr. - /wietsewind/check-xrp-wallet-addr
    Last edited 5 years ago
    var keypairs = require('ripple-keypairs'); let secret = keypairs.generateSeed({ algorithm: 'secp256k1' // or: 'ed25519' }) // var secret = 'shobxZSHdQaQ4EmyicWzV3K7PQj1U' var keypair = keypairs.deriveKeypair(secret); var address = keypairs.deriveAddress(keypair.publicKey); console.log( keypair ) console.log( { secret: secret, address: address } )
  • secp256k1 Sign & Verify signature - /wietsewind/secp256k1-sign-verify-signature
    Last edited 5 years ago
    const elliptic = require('elliptic') const secp256k1 = elliptic.ec('secp256k1') const hash = require('hash.js') const keypairs = require('ripple-keypairs') const hexToDecimal = (x) => secp256k1.keyFromPrivate(x, 'hex').getPrivate().toString(10) module.exports = { sign (message, familySeed) { const digest = hash.sha256().update(message).digest('hex').toUpperCase() const keypair = keypairs.deriveKeypair(familySeed) const signature = secp256k1.sign(digest, keypair.privateKey.substr(2)) return { hash: digest, signature: (signature.r.toJSON() + signature.s.toJSON() + signature.recoveryParam.toString(16)).toUpperCase() } }, unserializeSignature (signature) { return { r: signature.slice(0, 64).toLowerCase(), s: signature.slice(64, 128).toLowerCase(), recoveryParam: parseInt(signature.slice(128), 16) } }, deriveAddress (familySeed) { const keypair = keypairs.deriveKeypair(familySeed) return keypairs.deriveAddress(keypair.publicKey) }, recoverPubKey (hash, signature) { const unserializedSignature = this.unserializeSignature(signature) const pubKeyRecovered = secp256k1.recoverPubKey(hexToDecimal(hash), unserializedSignature, unserializedSignature.recoveryParam, 'hex') return pubKeyRecovered }, recoverAddress (hash, signature) { return keypairs.deriveAddress(this.recoverPubKey(hash, signature).encodeCompressed('hex')) }, verify (message, signature) { const digest = hash.sha256().update(message).digest('hex') const unserializedSignature = this.unserializeSignature(signature) return secp256k1.verify(digest, unserializedSignature, this.recoverPubKey(digest, signature)) } }
  • Safe Char number encoding / decoding - /wietsewind/safe-char-number-encoding-decoding
    Last edited 5 years ago
    const chars = 'abcdehlkmrtwxyz3469'.toUpperCase() // const i = 19880108151231 const i = 1002558 console.log(i) const to = (decimal) => { let out = '' while (true) { let remainder = (decimal - 1) % chars.length out = chars[remainder] + out; decimal = Math.floor((decimal - 1) / chars.length); if (decimal === 0) break } return out; } let t = to(i) console.log(i + ' (' + to(i % 13) + ') ' + t) console.log('eg. \'' + i + '.' + to(i % 13) + t + '\'') const from = (alpha) => { const crs = chars.split('') const letters = alpha.split('') let out = 0 for (let i = 0; i < letters.length; i++) { let indexPos = crs.indexOf(letters[letters.length - 1 - i]) out += (indexPos + 1) * Math.pow(crs.length, i) } return out } let f = from(t) console.log(t + ' = ' + f + ' (' + to(f % 13) + ')')
  • Generate Ripple Wallet - /wietsewind/generate-ripple-wallet
    Last edited 5 years ago
    const keypairs = require('ripple-keypairs') const secret = keypairs.generateSeed() const keypair = keypairs.deriveKeypair(secret) const address = keypairs.deriveAddress(keypair.publicKey) console.log(secret, keypair, address)
  • Decode Toast Blue Code - /wietsewind/decode-toast-blue-code
    Last edited 5 years ago
    const BN = require('bn.js') const hexToBytes = (a) => { return new BN(a, 16).toArray(null, a.length / 2) } const decodeToastOfflineCode = (code) => { let offlinecode = code.trim().toUpperCase().replace(/[^A-Z0-9]/g, '') let accID let ledID let fee if (/^[A-F0-9]+$/m.test(offlinecode)) { try { let compression = hexToBytes(offlinecode.slice(2, 4)) offlinecode = offlinecode.slice(4) let removedZerosLedId = compression & 7 let removedZerosAccId = compression >> 3 accID = '0'.repeat(removedZerosAccId) + offlinecode.slice(0, 8 - removedZerosAccId) offlinecode = offlinecode.slice(8 - removedZerosAccId) ledID = '0'.repeat(removedZerosLedId) + offlinecode.slice(0, 8 - removedZerosLedId) offlinecode = offlinecode.slice(8 - removedZerosLedId) while (offlinecode.length < 8) offlinecode = '0' + offlinecode fee = offlinecode offlinecode = accID + ledID + fee } catch (e) { console.log(e.message) } } return { accID: parseInt('0x' + accID), ledID: parseInt('0x' + ledID), fee: parseInt('0x' + fee) / 1000000 } } console.log(decodeToastOfflineCode('703A 12C5 173C'))