How to use borc - 10 common examples

To help you get started, we’ve selected a few borc 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 vacuumlabs / cardano-crypto.js / index.js View on Github external
function getAddressHash(input) {
  // eslint-disable-next-line camelcase
  const firstHash = sha3_256(cbor.encode(input))
  return blake2b(firstHash, 28)
}
github vacuumlabs / cardano-crypto.js / index.js View on Github external
} else {
    addressPayload = Buffer.from([])
    addressAttributes = new Map()
  }

  const addressRoot = getAddressHash([
    0,
    [0, xpub],
    addressPayload.length > 0 ? new Map([[1, cbor.encode(addressPayload)]]) : new Map(),
  ])
  const addressType = 0 // Public key address
  const addressData = [addressRoot, addressAttributes, addressType]
  const addressDataEncoded = cbor.encode(addressData)

  return base58.encode(
    cbor.encode([new cbor.Tagged(24, addressDataEncoded), crc32(addressDataEncoded)])
  )
}
github vacuumlabs / cardano-crypto.js / index.js View on Github external
function unpackAddress(address, hdPassphrase) {
  // we decode the address from the base58 string
  // and then we strip the 24 CBOR data tags (the "[0].value" part)
  const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
  const addressData = cbor.decode(addressAsBuffer)
  const attributes = addressData[1]
  const payload = cbor.decode(attributes.get(1))
  let derivationPath

  try {
    derivationPath = decryptDerivationPath(payload, hdPassphrase)
  } catch (e) {
    throw new Error('Unable to get derivation path from address')
  }

  if (derivationPath && derivationPath.length > 2) {
    throw Error('Invalid derivation path length, should be at most 2')
  }

  return {
    derivationPath,
  }
}
github vacuumlabs / cardano-crypto.js / index.js View on Github external
function isValidAddress(address) {
  try {
    // we decode the address from the base58 string
    const addressAsArray = cbor.decode(base58.decode(address))
    // we strip the 24 CBOR data taga by taking the "value" attribute from the "Tagged" object
    const addressDataEncoded = addressAsArray[0].value
    const crc32Checksum = addressAsArray[1]
    
    if (crc32Checksum !== crc32(addressDataEncoded)) {
      return false
    }
    
  } catch (e) {
    return false
  }
  return true
}
github vacuumlabs / cardano-crypto.js / index.js View on Github external
function unpackAddress(address, hdPassphrase) {
  // we decode the address from the base58 string
  // and then we strip the 24 CBOR data tags (the "[0].value" part)
  const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
  const addressData = cbor.decode(addressAsBuffer)
  const attributes = addressData[1]
  const payload = cbor.decode(attributes.get(1))
  let derivationPath

  try {
    derivationPath = decryptDerivationPath(payload, hdPassphrase)
  } catch (e) {
    throw new Error('Unable to get derivation path from address')
  }

  if (derivationPath && derivationPath.length > 2) {
    throw Error('Invalid derivation path length, should be at most 2')
  }

  return {
    derivationPath,
github vacuumlabs / cardano-crypto.js / index.js View on Github external
function unpackAddress(address, hdPassphrase) {
  // we decode the address from the base58 string
  // and then we strip the 24 CBOR data tags (the "[0].value" part)
  const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
  const addressData = cbor.decode(addressAsBuffer)
  const attributes = addressData[1]
  const payload = cbor.decode(attributes.get(1))
  let derivationPath

  try {
    derivationPath = decryptDerivationPath(payload, hdPassphrase)
  } catch (e) {
    throw new Error('Unable to get derivation path from address')
  }

  if (derivationPath && derivationPath.length > 2) {
    throw Error('Invalid derivation path length, should be at most 2')
  }

  return {
github ipld / js-ipld-dag-cbor / src / util.js View on Github external
function tagCID (cid) {
  if (typeof cid === 'string') {
    cid = new CID(cid).buffer
  } else if (CID.isCID(cid)) {
    cid = cid.buffer
  }

  return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
    Buffer.from('00', 'hex'), // thanks jdag
    cid
  ]))
}
github ipld / js-ipld-dag-cbor / src / util.js View on Github external
}
    if (options.tags) {
      tags = Object.assign({}, defaultTags, options && options.tags)
    }
  } else {
    // no options, reset to defaults
    currentSize = defaultSize
    maxSize = defaultMaxSize
  }

  const decoderOptions = {
    tags: tags,
    size: currentSize
  }

  decoder = new cbor.Decoder(decoderOptions)
  // borc edits opts.size in-place so we can capture _actual_ size
  currentSize = decoderOptions.size
}
github zbo14 / constellate / src / dag-cbor.js View on Github external
return cb(new Error('the object passed has circular references'))
    }
    seen.push(val)
    if (!(cid = val['/'])) {
      return val
    }
    if (typeof cid === 'string') {
      cid = new CID(cid.split('/')[0]).buffer
    }
    return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
      Buffer.from('00', 'hex'), cid
    ]))
  })
  if (!stop) {
    try {
      const data = cbor.encode(tagged)
      cb(null, data)
    } catch (err) {
      cb(err)
    }
  }
}
github zbo14 / constellate / lib / dag-cbor.js View on Github external
const tagged = transform(elem, val => {
        if (!isObject(val)) {
            return val
        }
        if (seen.some(obj => orderStringify(obj) === orderStringify(val))) {
            return tasks.error('the object passed has circular references')
        }
        seen.push(val)
        if (!(cid = val['/'])) {
            return val
        }
        if (isString(cid)) {
            cid = new CID(cid.split('/')[0]).buffer
        }
        return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
            Buffer.from('00', 'hex'), cid
        ]))
    })
    try {

borc

Encode and parse data in the Concise Binary Object Representation (CBOR) data format (RFC7049).

MIT
Latest version published 3 years ago

Package Health Score

54 / 100
Full package analysis