How to use the bitcore-lib.util.buffer function in bitcore-lib

To help you get started, we’ve selected a few bitcore-lib 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 HathorNetwork / hathor-wallet / src / __tests__ / tokens_utils.js View on Github external
/**
 * Copyright (c) Hathor Labs and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import tokens from '../utils/tokens';
import { GAP_LIMIT, HATHOR_TOKEN_CONFIG } from '../constants';
import { HDPrivateKey } from 'bitcore-lib';
import wallet from '../utils/wallet';
import { util } from 'bitcore-lib';
import WebSocketHandler from '../WebSocketHandler';

const createdTxHash = '00034a15973117852c45520af9e4296c68adb9d39dc99a0342e23cd6686b295e';
const createdToken = util.buffer.bufferToHex(tokens.getTokenUID(createdTxHash, 0));

beforeEach(() => {
  WebSocketHandler.started = true;
});

// Mock any POST request to /thin_wallet/send_tokens
// arguments for reply are (status, data, headers)
mock.onPost('thin_wallet/send_tokens').reply((config) => {
  const ret = {
    'success': true,
    'tx': {
      'hash': createdTxHash,
      'tokens': [createdToken],
    }
  }
  return [200, ret];
github HathorNetwork / hathor-wallet / src / utils / transaction2.js View on Github external
txToBytes(txData) {
    const fundsArr = this.txToFundsBytes(txData);
    console.log('Funds arr', util.buffer.bufferToHex(fundsArr));
    const graphArr = this.txToGraphBytes(txData);
    console.log('Graph arr', util.buffer.bufferToHex(graphArr));

    let arr = [...fundsArr, ...graphArr];

    // Add nonce in the end
    arr.push(this.intToBytes(txData.nonce, 4));
    return util.buffer.concat(arr);
  },
github HathorNetwork / hathor-wallet / src / utils / transaction.js View on Github external
let addressBytes = this.decodeAddress(address);
    if (this.validateAddress(address, addressBytes)) {
      let addressHash = addressBytes.slice(1, -4);
      if (timelock) {
        let timelockBytes = this.intToBytes(timelock, 4);
        this.pushDataToStack(arr, timelockBytes);
        arr.push(OP_GREATERTHAN_TIMESTAMP);
      }
      arr.push(OP_DUP);
      arr.push(OP_HASH160);
      // addressHash has a fixed size of 20 bytes, so no need to push OP_PUSHDATA1
      arr.push(this.intToBytes(addressHash.length, 1));
      arr.push(addressHash);
      arr.push(OP_EQUALVERIFY);
      arr.push(OP_CHECKSIG);
      return util.buffer.concat(arr);
    }
  },
github HathorNetwork / hathor-wallet / src / utils / transaction2.js View on Github external
validateAddress(addressBytes) {
    // Validate address length
    if (addressBytes.length !== 25) {
      throw new AddressError('Address should have 25 bytes');
    }

    // Validate address checksum
    let checksum = addressBytes.slice(-4);
    let addressSlice = addressBytes.slice(0, -4);
    let correctChecksum = this.getChecksum(addressSlice);
    if (!util.buffer.equals(checksum, correctChecksum)) {
      throw new AddressError('Invalid checksum for address');
    }
    return true;
  },
github HathorNetwork / hathor-wallet / src / utils / transaction.js View on Github external
let arr = []
    // Tx version
    arr.push(this.intToBytes(DEFAULT_TX_VERSION, 2))
    // Len inputs
    arr.push(this.intToBytes(txData.inputs.length, 1))
    // Len outputs
    arr.push(this.intToBytes(txData.outputs.length, 1))
    // Len tokens
    arr.push(this.intToBytes(txData.tokens.length, 1))

    for (const token of txData.tokens) {
      arr.push(new encoding.BufferReader(token).buf);
    }

    for (let inputTx of txData.inputs) {
      arr.push(util.buffer.hexToBuffer(inputTx.tx_id));
      arr.push(this.intToBytes(inputTx.index, 1));
      // Input data will be fixed to 0 for now
      arr.push(this.intToBytes(0, 2));
    }

    for (let outputTx of txData.outputs) {
      arr.push(this.outputValueToBytes(outputTx.value));
      // Token data
      arr.push(this.intToBytes(outputTx.tokenData, 1));

      let outputScript = this.createOutputScript(outputTx.address, outputTx.timelock);
      arr.push(this.intToBytes(outputScript.length, 2));
      arr.push(outputScript);
    }
    return util.buffer.concat(arr);
  },
github HathorNetwork / hathor-wallet / src / utils / transaction2.js View on Github external
let addressBytes = this.decodeAddress(address);
    if (this.validateAddress(addressBytes)) {
      let addressHash = addressBytes.slice(1, -4);
      if (timelock) {
        let timelockBytes = this.intToBytes(timelock, 4);
        this.pushDataToStack(arr, timelockBytes);
        arr.push(OP_GREATERTHAN_TIMESTAMP);
      }
      arr.push(OP_DUP);
      arr.push(OP_HASH160);
      // addressHash has a fixed size of 20 bytes, so no need to push OP_PUSHDATA1
      arr.push(this.intToBytes(addressHash.length, 1));
      arr.push(addressHash);
      arr.push(OP_EQUALVERIFY);
      arr.push(OP_CHECKSIG);
      return util.buffer.concat(arr);
    }
  },
github HathorNetwork / hathor-wallet / src / __tests__ / tokens_utils.js View on Github external
test('Token UID', () => {
  const txID = '00034a15973117852c45520af9e4296c68adb9d39dc99a0342e23cd6686b295e';
  const txID2 = '00034a15973117852c45520af9e4296c68adb9d39dc99a0342e23cd6686b295c';
  const uid1 = util.buffer.bufferToHex(tokens.getTokenUID(txID, 0));
  const uid2 = util.buffer.bufferToHex(tokens.getTokenUID(txID, 1));
  const uid3 = util.buffer.bufferToHex(tokens.getTokenUID(txID2, 0));

  expect(uid1.length).toBe(64);
  expect(uid1).not.toBe(uid2);
  expect(uid1).not.toBe(uid3);
});
github HathorNetwork / hathor-wallet / src / utils / PowWorker.worker.js View on Github external
let txData = e.data;
  console.log('Message received from main script');
  console.log(txData);
  let powPart1 = transaction2.getPowPart1(txData);
  let lastTime = txData.timestamp;
  let found = false;
  txData.nonce = 0;
  const target = transaction2.getTarget(txData);
  console.log('Target', target)
  while (txData.nonce < MAX_NONCE) {
    const now = dateFormatter.now();
    if ((now - lastTime) > 2) {
      console.log('Updating nonce', txData.nonce);
      txData.timestamp = now;
      powPart1 = transaction2.getPowPart1(txData);
      console.log('Pow part 1 ', util.buffer.bufferToHex(_.cloneDeep(powPart1).digest()));
      console.log('Pow part 1 de novo', util.buffer.bufferToHex(transaction2.getPowPart1(txData).digest()));
      lastTime = now;
      txData.nonce = 0;
    }

    const result = transaction2.getPowPart2(_.cloneDeep(powPart1), txData.nonce);
    if (parseInt(util.buffer.bufferToHex(result), 16) < target) {
      console.log('Result', parseInt(util.buffer.bufferToHex(result), 16))
      console.log('Result2 ', util.buffer.bufferToHex(result))
      self.postMessage({hash: result, nonce: txData.nonce, timestamp: txData.timestamp});
      found = true;
      break;
    }
    txData.nonce += 1;
  }
  if (!found) {
github HathorNetwork / hathor-wallet / src / utils / PowWorker.worker.js View on Github external
console.log('Target', target)
  while (txData.nonce < MAX_NONCE) {
    const now = dateFormatter.now();
    if ((now - lastTime) > 2) {
      console.log('Updating nonce', txData.nonce);
      txData.timestamp = now;
      powPart1 = transaction2.getPowPart1(txData);
      console.log('Pow part 1 ', util.buffer.bufferToHex(_.cloneDeep(powPart1).digest()));
      console.log('Pow part 1 de novo', util.buffer.bufferToHex(transaction2.getPowPart1(txData).digest()));
      lastTime = now;
      txData.nonce = 0;
    }

    const result = transaction2.getPowPart2(_.cloneDeep(powPart1), txData.nonce);
    if (parseInt(util.buffer.bufferToHex(result), 16) < target) {
      console.log('Result', parseInt(util.buffer.bufferToHex(result), 16))
      console.log('Result2 ', util.buffer.bufferToHex(result))
      self.postMessage({hash: result, nonce: txData.nonce, timestamp: txData.timestamp});
      found = true;
      break;
    }
    txData.nonce += 1;
  }
  if (!found) {
    self.postMessage(null);
  }
});
github HathorNetwork / hathor-wallet / src / opcodes.js View on Github external
*/

/**
 * Checks if timestamp is greater than a value
 */
export const OP_GREATERTHAN_TIMESTAMP = util.buffer.hexToBuffer('6f');

/**
 * Duplicates value
 */
export const OP_DUP = util.buffer.hexToBuffer('76');

/**
 * Calculates hash160 of value
 */
export const OP_HASH160 = util.buffer.hexToBuffer('a9');

/**
 * Verifies if values are equal
 */
export const OP_EQUALVERIFY = util.buffer.hexToBuffer('88');

/**
 * Verifies signature
 */
export const OP_CHECKSIG = util.buffer.hexToBuffer('ac');

/**
 * Shows that pushdata will need length value
 */
export const OP_PUSHDATA1 = util.buffer.hexToBuffer('4c');