How to use base-x - 6 common examples

To help you get started, we’ve selected a few base-x examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nusmodifications / nusmods / api / hasura / src / utils / base58.ts View on Github external
import basex from 'base-x';

// https://en.wikipedia.org/wiki/Base58
const BASE_58_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
const base58 = basex(BASE_58_CHARACTERS);

export default base58;
github bitpay / bitcore / packages / crypto-wallet-core / src / validation / xrp / index.ts View on Github external
validateAddress(_network: string, address: string): boolean {
    // First ensure it matches regex
    const RippleAddressRegex = new RegExp(/^r[rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz]{27,35}$/);
    if (!address.match(RippleAddressRegex)) {
      return false;
    }

    // Then ensure it is a valid base58check encoding
    const base58 = baseX(RIPPLE_ALPHABET);
    const buffer = new Buffer(base58.decode(address));
    let prefix = buffer.slice(0, 1);
    let data = buffer.slice(1, -4);
    let hash = Buffer.concat([prefix, data]);
    hash = Bitcore.crypto.Hash.sha256(hash);
    hash = Bitcore.crypto.Hash.sha256(hash);
    let checksum = buffer.slice(-4).reduce((acc, check, index) => {
      if (check !== hash[index]) {
        // Invalid checksum
        return 0;
      } else return acc || 1;
    });
    if (checksum === 0) {
      return false;
    }
github EdgeApp / edge-core-js / src / util / encoding.js View on Github external
// @flow

import baseX from 'base-x'

const base58Codec = baseX(
  '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
)

function assertString(text) {
  if (typeof text !== 'string') {
    throw new Error('Input is not a string')
  }
}

function assertData(data) {
  if (typeof data === 'string' || data.length == null) {
    throw new Error('Input is not data')
  }
}

export const base58 = {
github sebastian-software / babel-plugin-smart-webpack-import / src / index.js View on Github external
/* eslint-disable filenames/match-exported */
import crypto from "crypto"
import { basename, dirname, extname, relative, sep } from "path"

import appRoot from "app-root-dir"
import basex from "base-x"
import json5 from "json5"

const base62 = basex("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
const root = appRoot.get()

const DEFAULT_LENGTH = 5
function hashString(input, precision = DEFAULT_LENGTH) {
  return base62
    .encode(
      crypto
        .createHash("sha256")
        .update(input)
        .digest()
    )
    .slice(0, precision)
}

function collectImportCallPaths(startPath) {
  const imports = []
github ruigomeseu / bitcoin-address-validation / src / index.js View on Github external
import baseX from 'base-x';
import bech32 from 'bech32';
import sha from 'sha.js';
import { Buffer } from 'buffer';

const base58 = baseX('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');

const sha256 = payload => Buffer.from(sha('sha256').update(payload).digest());

const addressTypes = {
  0x00: {
    type: 'p2pkh',
    network: 'mainnet'
  },

  0x6f: {
    type: 'p2pkh',
    network: 'testnet'
  },

  0x05: {
    type: 'p2sh',
github go-faast / faast-web / src / utilities / helpers.js View on Github external
const getDate = (item, dateField) => {
  let d = typeof item === 'object' && item !== null && item[dateField]
  if (!d) { return 0 }
  if (!(d instanceof Date)) {
    d = new Date(d)
  }
  return d.getTime()
}

export const dateSort = (items, dir, dateField) => items
  .sort((a, b) => (dir === 'desc' ? -1 : 1) * (getDate(a, dateField) - getDate(b, dateField)))

export const numericalSort = (items, dir, field) => items
  .sort((a, b) => (dir === 'desc' ? -1 : 1) * (toBigNumber(a[field]).comparedTo(toBigNumber(b[field]))))

export const bs62 = baseX('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')

export const toHashId = (s) => bs62.encode(Buffer.from(sha256().update(s).digest().slice(0, 16)))

export const routerPathToUri = (routerPath) => { 
  if (typeof window !== 'undefined') {
    return urlJoin(window.location.origin, process.env.ROUTER_BASE_NAME, routerPath)
  } 
}

export const splice = (arr, ...args) => {
  const spliced = [...arr]
  spliced.splice(...args)
  return spliced
}

export const uppercase = (str) => {

base-x

Fast base encoding / decoding of any given alphabet

MIT
Latest version published 5 months ago

Package Health Score

77 / 100
Full package analysis

Popular base-x functions