How to use the cbor.decode function in cbor

To help you get started, we’ve selected a few cbor 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 dillpixel / fitbit-asap / source / companion.js View on Github external
function process() {
  // If the queue is not empty
  if (queue.length > 0) {
    // Get the next message ID
    const id = queue[0]
    // Attempt to read the message from disk
    try {
      const message = decode((new Uint8Array(JSON.parse(localStorage.getItem("_asap_" + id)))).buffer)
      const timeout = message[2]
      // If the message has expired
      if (timeout < Date.now()) {
        // Dequeue the message
        dequeue(id)
      }
      // If the message has not expired
      else {
        // Attempt to send the message
        try {
          peerSocket.send(message)
        } catch (error) {
          debug && console.log(error)
        }
      }
    }
github OpenZeppelin / openzeppelin-sdk / packages / lib / src / utils / Bytecode.ts View on Github external
if (!bytecode || bytecode.length <= 2) return bytecode;

  // Gather length of CBOR metadata from the end of the file
  const rawLength = bytecode.slice(bytecode.length - 4);
  const length = parseInt(rawLength, 16);

  // Bail on unreasonable values for length (meaning we read something else other than metadata length)
  if (length * 2 > bytecode.length - 4) return bytecode;

  // Gather what we assume is the CBOR encoded metadata, and try to parse it
  const metadataStart = bytecode.length - length * 2 - 4;
  const metadata = bytecode.slice(metadataStart, bytecode.length - 4);

  // Parse it to see if it is indeed valid metadata
  try {
    cbor.decode(Buffer.from(metadata, 'hex'));
  } catch (err) {
    Loggy.noSpin.warn(
      __filename,
      'tryRemoveMetadata',
      'parse-contract-metadata',
      `Error parsing contract metadata: ${err.message}. Ignoring.`,
    );

    return bytecode;
  }

  // Return bytecode without it
  return bytecode.slice(0, metadataStart);
}
github vacuumlabs / adalite / app / frontend / wallet / address.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))
  const derivationPath = decryptDerivationPath(payload, hdPassphrase)

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

  return {
    derivationPath,
  }
}
github vacuumlabs / adalite / withNode / address.js View on Github external
exports.deriveSecretStringFromAddressOrFail = function(address, rootSecretString) {
  // we decode the address from the base58 string
  // and then we strip the 24 CBOR data taga (the "[0].value" part)
  const addressAsBuffer = cbor.decode(base58.decode(address))[0].value
  const addressData = cbor.decode(addressAsBuffer)
  const addressAttributes = addressData[1]
  let childIndex

  if (addressAttributes.length === 0) {
    // the root address (derrived straight from the root secret key)
    childIndex = 0x80000000
  } else {
    // the remaining addresses have a nontrivial child index
    // therefore the derivation path is nonempty
    const addressPayload = cbor.decode(addressAttributes.get(1))
    const hdPassphrase = deriveHDPassphrase(rootSecretString)
    const derivationPath = decryptDerivationPathOrFail(addressPayload, hdPassphrase)
    childIndex = derivationPath[1]
  }
github vacuumlabs / adalite / app / frontend / wallet / address.js View on Github external
function decryptDerivationPath(addressPayload, hdPassphrase) {
  const decipheredDerivationPath = chacha20poly1305Decrypt(
    addressPayload,
    hdPassphrase,
    Buffer.from('serokellfore')
  )

  try {
    return cbor.decode(Buffer.from(decipheredDerivationPath))
  } catch (err) {
    debugLog(err)
    throw NamedError('AddressDecodingException', 'incorrect address or passphrase')
  }
}
github Emurgo / tangata-manu / src / entities / shelley-validator.js View on Github external
static deconstructAddress(address: string) {
    const [addressRoot, addrAttr, addressType] = cbor.decode(
      cbor.decode(bs58.decode(address))[0].value,
    )
    return { addressRoot, addrAttr, addressType }
  }
}
github Emurgo / tangata-manu / src / entities / shelley-validator.js View on Github external
static deconstructAddress(address: string) {
    const [addressRoot, addrAttr, addressType] = cbor.decode(
      cbor.decode(bs58.decode(address))[0].value,
    )
    return { addressRoot, addrAttr, addressType }
  }
}
github Emurgo / tangata-manu / src / blockchain / utils.js View on Github external
inputs: inputs.map(inp => {
      const [type, tagged] = inp
      const [inputTxId, idx] = cbor.decode(tagged.value)
      return { type, txId: inputTxId.toString('hex'), idx }
    }),
    outputs: outputs.map(out => {
github Emurgo / tangata-manu / src / blockchain / block.js View on Github external
static parseBlock(blob: Buffer, handleRegularBlock: number): Block {
    const [type, [header, body]] = cbor.decode(blob)
    const hash = utils.headerToId(header, type)
    const common = {
      hash,
      magic: header[0],
      prevHash: header[1].toString('hex'),
    }
    let blockData
    switch (type) {
      case 0:
        blockData = { ...common, ...Block.handleEpochBoundaryBlock(header) }
        break
      case 1:
        blockData = {
          ...common,
          ...Block.handleRegularBlock(header, body, hash, handleRegularBlock),
        }
github vacuumlabs / adalite / withNode / address.js View on Github external
function decryptDerivationPathOrFail(addressPayload, hdPassphrase) {
  const cipher = new chacha20.ChaCha20Poly1305(hdPassphrase)
  const decipheredDerivationPath = cipher.open(new Buffer('serokellfore'), addressPayload)

  try {
    return cbor.decode(new Buffer(decipheredDerivationPath))
  } catch (err) {
    throw new AddressDecodingException('incorrect address or passphrase')
  }
}

cbor

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

MIT
Latest version published 3 months ago

Package Health Score

78 / 100
Full package analysis