How to use the bsv.encoding function in bsv

To help you get started, we’ve selected a few bsv 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 BitcoinFiles / bitcoinfiles-sdk / lib / utils.ts View on Github external
}
                selectedArg = Buffer.from(selectedArg, 'hex');
            } else if (Buffer.isBuffer(selectedArg)) {
                // Force adding a 0x00 into the data since that's how it will be encoded in OP_RETURN field
                if (selectedArg.length === 0) {
                    selectedArg = Buffer.from('00', 'hex');
                }
            } else {
                throw new Error('invalid argument type should be Buffer or a hex string: ' + selectedArg);
            }
            usedArgs.push(selectedArg);
        }
        if (!usedArgs || !usedArgs.length) {
            throw new Error('insufficient index args');
        }
        const bufferWriter = new bsv.encoding.BufferWriter();
        for (const field of usedArgs) {

            let bf = field;
            if (!Buffer.isBuffer(field)) {
                bf = new bsv.encoding.BufferReader(field);
                bf = bf.buf;
            }
            console.log('bf', bf);
            bufferWriter.write(bf);
        }
        const appData = bufferWriter.toBuffer();
        const signature = bsv.Message(appData).sign(bsv.PrivateKey(payload.key))
        const verified = bsv.Message(appData).verify(payload.address, signature);
        if (!verified) {
            throw new Error('signArguments - Signature verification failure: ' + signature);
        }
github BitcoinFiles / bitcoinfiles-sdk / lib / utils.ts View on Github external
selectedArg = Buffer.from('00', 'hex');
                }
            } else {
                throw new Error('invalid argument type should be Buffer or a hex string: ' + selectedArg);
            }
            usedArgs.push(selectedArg);
        }
        if (!usedArgs || !usedArgs.length) {
            throw new Error('insufficient index args');
        }
        const bufferWriter = new bsv.encoding.BufferWriter();
        for (const field of usedArgs) {

            let bf = field;
            if (!Buffer.isBuffer(field)) {
                bf = new bsv.encoding.BufferReader(field);
                bf = bf.buf;
            }
            console.log('bf', bf);
            bufferWriter.write(bf);
        }
        const appData = bufferWriter.toBuffer();
        const signature = bsv.Message(appData).sign(bsv.PrivateKey(payload.key))
        const verified = bsv.Message(appData).verify(payload.address, signature);
        if (!verified) {
            throw new Error('signArguments - Signature verification failure: ' + signature);
        }
        return signature;
    }
github deanmlittle / bsv-p2p / lib / inventory.js View on Github external
'use strict';

var bsv = require('bsv');
var $ = bsv.util.preconditions;
var Buffer = require('buffer').Buffer;
var reverse = require('buffer-reverse');
var BufferReader = bsv.encoding.BufferReader;
var BufferWriter = bsv.encoding.BufferWriter;
var _ = bsv.deps._;

/**
 * A constructor for inventory related Bitcoin messages such as
 * "getdata", "inv" and "notfound".
 * @param {Object} obj
 * @param {Number} obj.type - Inventory.TYPE
 * @param {Buffer} obj.hash - The hash for the inventory
 * @constructor
 */
function Inventory(obj) {
  this.type = obj.type;
  if (!Buffer.isBuffer(obj.hash)) {
    throw new TypeError('Unexpected hash, expected to be a buffer');
  }
  this.hash = obj.hash;
github deanmlittle / bsv-p2p / lib / inventory.js View on Github external
'use strict';

var bsv = require('bsv');
var $ = bsv.util.preconditions;
var Buffer = require('buffer').Buffer;
var reverse = require('buffer-reverse');
var BufferReader = bsv.encoding.BufferReader;
var BufferWriter = bsv.encoding.BufferWriter;
var _ = bsv.deps._;

/**
 * A constructor for inventory related Bitcoin messages such as
 * "getdata", "inv" and "notfound".
 * @param {Object} obj
 * @param {Number} obj.type - Inventory.TYPE
 * @param {Buffer} obj.hash - The hash for the inventory
 * @constructor
 */
function Inventory(obj) {
  this.type = obj.type;
  if (!Buffer.isBuffer(obj.hash)) {
    throw new TypeError('Unexpected hash, expected to be a buffer');
  }