How to use the buffer/.Buffer.alloc function in buffer

To help you get started, we’ve selected a few buffer 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 GridPlus / gridplus-sdk / src / client.js View on Github external
const { currency, startIndex, n, version="SEGWIT" } = opts;
    if (currency === undefined || startIndex == undefined || n == undefined) {
      return cb('Please provide `currency`, `startIndex`, and `n` options');
    } else if (currencyCodes[currency] === undefined) {
      return cb('Unsupported currency');
    }
    // Bitcoin requires a version byte
    if (currency === 'BTC' && bitcoin.addressVersion[version] === undefined) {
      return cb('Unsupported Bitcoin version. Options are: LEGACY, SEGWIT, TESTNET, SEGWIT_TESTNET');
    }
    // Bitcoin segwit addresses require a P2SH_P2WPKH script type -- the script type
    // is otherwise ignored and is only needed for BTC
    const bitcoinScriptType = (version === 'SEGWIT' || version === 'SEGWIT_TESTNET') ? 
                              bitcoin.scriptTypes.P2SH_P2WPKH: 0;

    const payload = Buffer.alloc(7);
    payload.writeUInt8(currencyCodes[currency]);
    payload.writeUInt32BE(startIndex, 1);
    payload.writeUInt8(n, 5);
    payload.writeUInt8(bitcoinScriptType || 0, 6);
    const param = this._buildEncRequest(encReqCodes.GET_ADDRESSES, payload);
    return this._request(param, (err, res) => {
      if (err) return cb(err);
      const parsedRes = this._handleGetAddresses(res, currency, n, bitcoin.addressVersion[version]);
      if (parsedRes.err) return cb(parsedRes.err);
      return cb(null, parsedRes.data);
    })
  }
github GridPlus / gridplus-sdk / src / bitcoin.js View on Github external
function buildWitness(sigs, pubkeys) {
  let witness = Buffer.alloc(0);
  // Two items in each vector (sig, pubkey)
  const len = Buffer.alloc(1); len.writeUInt8(2);
  for (let i = 0; i < sigs.length; i++) {
    const sig = Buffer.concat([sigs[i], DEFAULT_SIGHASH_BUFFER]);
    const sigLen = getVarInt(sig.length);
    const pubkey = pubkeys[i];
    const pubkeyLen = getVarInt(pubkey.length);
    witness = Buffer.concat([witness, len, sigLen, sig, pubkeyLen, pubkey]);
  }
  return witness;
}
github GridPlus / gridplus-sdk / src / client.js View on Github external
const tx = txBuildingResolver[currency](data);
    if (tx.err !== undefined) return cb({ err: tx.err });
    // All transaction requests must be put into the same sized buffer
    // so that checksums may be validated. The full size is 530 bytes,
    // but that includes a 1-byte prefix (`SIGN_TRANSACTION`), 2 bytes
    // indicating the schema type, and 4 bytes for a checksum.
    // That leaves 514 bytes for the transaction request. It will be
    // deserialized according to the schema type and extra zeros will be
    // discarded.
    const MAX_TX_REQ_DATA_SIZE = 557;
    if (tx.payload.length > MAX_TX_REQ_DATA_SIZE) {
      return cb('Transaction is too large');
    }

    // Build the payload
    const payload = Buffer.alloc(2 + MAX_TX_REQ_DATA_SIZE);
    payload.writeUInt16BE(tx.schema, 0);
    tx.payload.copy(payload, 2);

    // Construct the encrypted request and send it
    const param = this._buildEncRequest(encReqCodes.SIGN_TRANSACTION, payload);
    return this._request(param, (err, res) => {
      if (err) return cb(err);
      const parsedRes = this._handleSign(res, currency, tx);
      if (parsedRes.err) return cb(parsedRes.err);
      return cb(null, parsedRes.data);
    })
  }
github GridPlus / gridplus-sdk / src / bitcoin.js View on Github external
exports.buildBitcoinTxRequest = function(data) {
  try {
    const { prevOuts, recipient, value, changeIndex=0, fee, isSegwit, changeVersion='SEGWIT' } = data;
    // Serialize the request
    const payload = Buffer.alloc(37 + (51 * prevOuts.length));
    let off = 0;
    // Build the change data
    payload.writeUInt32LE(changeIndex, off); off += 4;
    const scriptType = isSegwit === true ? 
                        scriptTypes.P2SH_P2WPKH :  // Only support p2sh(p2wpkh) for segwit spends for now
                        scriptTypes.P2PKH; // No support for multisig p2sh in v1 (p2sh == segwit here)
    payload.writeUInt8(scriptType, off); off++; // change script type
    // Fee is a param
    payload.writeUInt32LE(fee, off); off += 4;
    const recipientVersionByte = bs58.decode(recipient)[0];
    const recipientPubkeyhash = bs58check.decode(recipient).slice(1);
    // Parameterize the recipient output
    payload.writeUInt8(recipientVersionByte, off); off++;
    recipientPubkeyhash.copy(payload, off); off += recipientPubkeyhash.length;
    writeUInt64LE(value, payload, off); off += 8;
    // Build the inputs from the previous outputs
github GridPlus / gridplus-sdk / src / bitcoin.js View on Github external
function getU32LE(x) {
  let buffer = Buffer.alloc(4);
  buffer.writeUInt32LE(x);
  return buffer;
}
github GridPlus / gridplus-sdk / src / bitcoin.js View on Github external
function getVarInt (x) {
  let buffer;
  if (x < 0xfd) {
    buffer = Buffer.alloc(1);
    buffer.writeUInt8(x);
  } else if (x <= 0xffff) {
    buffer = Buffer.alloc(3);
    buffer.writeUInt8(0xfd, 0);
    buffer.writeUInt16LE(x, 1);
  } else if (x < 0xffffffff) {
    buffer = Buffer.alloc(5);
    buffer.writeUInt8(0xfe, 0);
    buffer.writeUInt32LE(x, 1);
  } else {
    buffer = Buffer.alloc(9);
    buffer.writeUInt8(0xff, 0);
    buffer.writeUInt32LE(x >>> 0, 1);
    buffer.writeUInt32LE((x / 0x100000000) | 0, 5);
  }
  return buffer;
github paragonie / sodium-plus / lib / backend / sodiumnative.js View on Github external
async crypto_secretbox(plaintext, nonce, key) {
        let encrypted = Buffer.alloc(plaintext.length + 16);
        this.sodium.crypto_secretbox_easy(
            encrypted,
            await Util.toBuffer(plaintext),
            nonce,
            key.getBuffer()
        );
        return encrypted;
    }
github GridPlus / gridplus-sdk / src / ethereum.js View on Github external
function fixLen(msg, length) {
  const buf = Buffer.alloc(length)
  if (msg.length < length) {
    msg.copy(buf, length - msg.length)
    return buf
  }
  return msg.slice(-length)
}
github GridPlus / gridplus-sdk / src / bitcoin.js View on Github external
function getU64LE(x) {
  let buffer = Buffer.alloc(8);
  writeUInt64LE(x, buffer, 0);
  return buffer;
}
github node-a-team / ts-amino / src / binary-encode.ts View on Github external
codec: Codec,
  info: TypeInfo,
  value: any[],
  fopts: FieldOptions,
  bare: boolean
): Uint8Array {
  if (!info.arrayOf) {
    throw new Error("should set a type of array element");
  }
  const etype = info.arrayOf!;
  let einfo: TypeInfo = {
    type: etype.type,
    arrayOf: etype.arrayOf
  };

  let buf = Buffer.alloc(0);

  if (value.length > 0) {
    const [deferedInfo] = deferTypeInfo(codec, einfo, value[0], "");
    einfo = deferedInfo;

    const typ3 = typeToTyp3(deferedInfo.type, fopts);
    if (typ3 !== Typ3.ByteLength) {
      for (const v of value) {
        buf = Buffer.concat([
          buf,
          Buffer.from(encodeReflectBinary(codec, einfo, v, fopts, false))
        ]);
      }
    } else {
      for (const v of value) {
        buf = Buffer.concat([