wietsewind's notebooks

  • Check XRP Wallet addr. - /wietsewind/check-xrp-wallet-addr
    Last edited a month 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 } )
  • Sign XRP TX using HEX privateKey - /wietsewind/sign-xrp-tx-using-hex-privatekey
    Last edited a month ago
    const ripple = require('ripplelib') const sign = require('ripple-sign-keypairs') // NOTE! ripplelib isn't working in Safari 10.1 (wrong public key generated from private key) - use // ripple-keypairs, as displayed in: // > https://runkit.com/wietsewind/hex-private-key-to-address-with-ripple-lib // > https://runkit.com/wietsewind/mnemonic-with-ripple-keypairs-w-o-ripplelib // var privateKey = '0762EED5BA4F378FFA60621C6DEF72F4A0A579112ADA5F5D6B2A35EC27E893A5' // const key = ripple.KeyPair.from_json(privateKey) // const keyPair = { privateKey: '00' + key.to_hex_pri(), publicKey: key.to_hex_pub() } // console.log('privateKey: ' + keyPair.privateKey) // console.log('privateKeyWif: ' + key.to_pri_string()) // to_wif // console.log('publicKey: ' + keyPair.publicKey) // console.log('address: ' + key.to_address_string()) // // Now sign a transaction... // var tx = { // TransactionType: 'Payment', // Account: key.to_address_string(), // Fee : (0.000012 * 1000 * 1000) + '', // Destination: 'rPEPPER7kfTD9w2To4CQk6UCfuHM9c6GDY', // DestinationTag : 2, // Amount: (1 * 1000 * 1000) + '', // Sequence: 0 // } // var txJSON = JSON.stringify(tx) // var txSign = sign(txJSON, keyPair) // console.log(txSign) // return txSign
  • secp256k1 Sign & Verify signature - /wietsewind/secp256k1-sign-verify-signature
    Last edited a month 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 a month 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 a month 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 a month 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'))
  • Ripple account_info (specific Ledger) - /wietsewind/ripple-account-info-specific-ledger
    Last edited a month ago
    const websocket = require('websocket') const RippleClient = require('rippled-ws-client') new RippleClient('wss://s2.ripple.com').then(connection => { connection.send({ command: 'account_info', account: 'rPEPPER7kfTD9w2To4CQk6UCfuHM9c6GDY', ledger_index: 35664185 }).then(response => { console.log('XRP Balance', parseInt(response.account_data.Balance) / 1000000) // Convert drops to XRP connection.close() }) }).catch(console.log)
  • Recover Ripple Minimalist Rekeyed account - /wietsewind/recover-ripple-minimalist-rekeyed-account
    Last edited a month ago
    const ripple = require('ripplelib') const minimalistRekeyed = '3QBGZnmsg5UY2EoPFZXwo9238Yp6C9N9ztaRbURcYa49PhvCAtF' let b = ripple.Base.decode_check(ripple.Base.VER_REKEYED, minimalistRekeyed) let y = ripple.sjcl.codec.bytes.fromBits(b.toBits()) let rks = ripple.Base.encode_check(ripple.Base.VER_FAMILY_SEED, y.slice(0,16)) let familySeed = ripple.Base.encode_check(ripple.Base.VER_FAMILY_SEED, y.slice(16)) let seed = ripple.Seed.from_json(familySeed) console.log('Address', seed.get_key().get_address().to_json()) console.log('Secret', familySeed)
  • Sample filter / map / reduce (XRP Transaction Sum) - /wietsewind/sample-filter-map-reduce-xrp-transaction-sum
    Last edited a month ago
    /* Filter / Map / Reduce on arrays: Imagine a funnel `filter`, `map`, `reduce`: They take an array as input and at the other send the input is returned processed. filter = decide what should be in the output array map = modify the content of each object that will be in the output array reduce = aggregate the input to some output */ const RippleAPI = require('ripple-lib').RippleAPI const api = new RippleAPI({ server: 'wss://rippled.xrptipbot.com' }) api.connect().then(() => { return api.getTransactions('rPdvC6ccq8hCdPKSPJkPmyZ4Mi1oG2FFkT', { minLedgerVersion: 36542093 }).then(transactions => { // `transactions` is an array with transaction objects. let sum = transactions .filter(tx => { // The .filter runs for each tx in transactions, we call each // transaction (each array member) 'tx'. Only return true // (so: array object to next part of the chain) if this is valid return tx.type === 'payment' && tx.outcome.result === 'tesSUCCESS' && tx.outcome.deliveredAmount.currency === 'XRP' }) .map(tx => { // Convert to drops, prevent rounding errors let value = parseFloat(tx.outcome.deliveredAmount.value) * 1000000 return Math.round(value) }) .reduce((existingTotal, thisValue) => { return existingTotal + thisValue }, 0) // 0 = 2nd argument = startValue console.log('Sum in XRP:', sum / 1000000) }) }).then(() => { }).catch(console.error)
  • google-libphonenumber - /wietsewind/google-libphonenumber
    Last edited a month ago - from: https://npm.runkit.com/google-libphonenumber
    let nr = '06 - 83164677' let country = 'NL' const phoneno = '+' + nr.replace(/[^0-9\-\(\)+]+/g, '').trim().replace(/^[0+]+/, '') console.log(phoneno) const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance() const number = phoneUtil.parseAndKeepRawInput(phoneno) console.log(number.getCountryCode()) if (phoneUtil.isValidNumber(number)) { console.log("valid") console.log(phoneUtil.format(number, require('google-libphonenumber').PhoneNumberFormat.E164)) } else { console.log("invalid, try with country", country) const numbercc = phoneUtil.parseAndKeepRawInput(nr, country) console.log(phoneUtil.format(numbercc, require('google-libphonenumber').PhoneNumberFormat.E164)) } //