wietsewind's notebooks

  • Mini Promise Sample - /wietsewind/mini-promise-sample
    Last edited 8 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 8 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 8 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))
  • Check XRP Wallet addr. - /wietsewind/check-xrp-wallet-addr
    Last edited 8 months 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 8 months 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 8 months 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 8 months 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 8 months 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 8 months 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 8 months 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)