Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
/*jshint immed: false */
var should = require('chai').should();
var bitcore = require('bitcore-lib');
var P2P = require('../');
var Inventory = P2P.Inventory;
var BufferUtils = bitcore.util.buffer;
var BufferWriter = bitcore.encoding.BufferWriter;
var BufferReader = bitcore.encoding.BufferReader;
describe('Inventory', function() {
var hash = new Buffer('eb951630aba498b9a0d10f72b5ea9e39d5ff04b03dc2231e662f52057f948aa1', 'hex');
var hashedStr = BufferUtils.reverse(new Buffer(hash, 'hex')).toString('hex');
var inventoryBuffer = new Buffer(
'01000000eb951630aba498b9a0d10f72b5ea9e39d5ff04b03dc2231e662f52057f948aa1',
'hex'
);
describe('@constructor', function() {
it('create inventory', function() {
var inventory = new Inventory({type: Inventory.TYPE.TX, hash: hash});
should.exist(inventory);
});
/**
* @file Contains the MessageBase class from which all messages inherit
* @module messages
*/
var bitcore = require('bitcore-lib')
var Schema = require('async-validate')
var merge = require('merge')
var extend = require('shallow-extend')
var $ = bitcore.util.preconditions
var Varint = bitcore.encoding.Varint
var BufferReader = bitcore.encoding.BufferReader
var DEFAULT_FEE = 40000
Schema.plugin([
require('async-validate/plugin/object'),
require('async-validate/plugin/string'),
require('async-validate/plugin/integer'),
require('async-validate/plugin/number'),
require('async-validate/plugin/util')
])
function toVarString (buf) {
return Buffer.concat([new Varint(buf.length).buf, new Buffer(buf)])
}
exports.encodeMessage = function(json) {
var writer = bitcore.encoding.BufferWriter();
writer.write(MAGIC_BUFFER);
writer.writeVarintNum(json.length);
writer.write(new Buffer(json, 'utf8'));
return writer.toBuffer();
};
'use strict';
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var PrivateKey = bitcore.PrivateKey;
var PublicKey = bitcore.PublicKey;
var Address = bitcore.Address;
var BufferWriter = bitcore.encoding.BufferWriter;
var ECDSA = bitcore.crypto.ECDSA;
var Signature = bitcore.crypto.Signature;
var sha256sha256 = bitcore.crypto.Hash.sha256sha256;
var JSUtil = bitcore.util.js;
var $ = bitcore.util.preconditions;
/**
* constructs a new message to sign and verify.
*
* @param {String} message
* @returns {Message}
*/
var Message = function Message(message) {
if (!(this instanceof Message)) {
return new Message(message);
}
function read() {
var reader = bitcore.encoding.BufferReader(buffer);
reader.read(MAGIC_BUFFER.length);
var length = reader.readVarintNum();
var json;
if (buffer.length >= length + MAGIC_BUFFER.length) {
var jsonBuffer = reader.read(length);
buffer = reader.readAll();
try {
json = JSON.parse(jsonBuffer.toString('utf8'));
} catch(e) {
log.error('Unable to JSON parse writer message');
}
}
if (json) {
callback(json);
API._buildSecret = function(walletId, walletPrivKey, coin, network) {
if (_.isString(walletPrivKey)) {
walletPrivKey = Bitcore.PrivateKey.fromString(walletPrivKey);
}
var widHex = new Buffer(walletId.replace(/-/g, ''), 'hex');
var widBase58 = new Bitcore.encoding.Base58(widHex).toString();
return widBase58.padEnd(22, '0') + walletPrivKey.toWIF() + (network == 'testnet' ? 'T' : 'L') + coin;
};
API._buildSecret = function(walletId, walletPrivKey, coin, network) {
if (_.isString(walletPrivKey)) {
walletPrivKey = Bitcore.PrivateKey.fromString(walletPrivKey);
}
var widHex = new Buffer(walletId.replace(/-/g, ''), 'hex');
var widBase58 = new Bitcore.encoding.Base58(widHex).toString();
return _.padEnd(widBase58, 22, '0') + walletPrivKey.toWIF() + (network == 'testnet' ? 'T' : 'L') + coin;
};
'use strict';
var bitcore = require('bitcore-lib');
var BufferReader = bitcore.encoding.BufferReader;
function Encoding(servicePrefix) {
this.servicePrefix = servicePrefix;
}
Encoding.prototype.encodeWalletTransactionKey = function(walletId, height) {
var buffers = [this.servicePrefix];
var walletIdSizeBuffer = new Buffer(1);
walletIdSizeBuffer.writeUInt8(walletId.length);
var walletIdBuffer = new Buffer(walletId, 'utf8');
buffers.push(walletIdSizeBuffer);
buffers.push(walletIdBuffer);
API.prototype.decryptBIP38PrivateKey = function(encryptedPrivateKeyBase58, passphrase, opts, cb) {
var Bip38 = require('bip38');
var bip38 = new Bip38();
var privateKeyWif;
try {
privateKeyWif = bip38.decrypt(encryptedPrivateKeyBase58, passphrase);
} catch (ex) {
return cb(new Error('Could not decrypt BIP38 private key', ex));
}
var privateKey = new Bitcore.PrivateKey(privateKeyWif);
var address = privateKey.publicKey.toAddress().toString();
var addrBuff = new Buffer(address, 'ascii');
var actualChecksum = Bitcore.crypto.Hash.sha256sha256(addrBuff).toString('hex').substring(0, 8);
var expectedChecksum = Bitcore.encoding.Base58Check.decode(encryptedPrivateKeyBase58).toString('hex').substring(6, 14);
if (actualChecksum != expectedChecksum)
return cb(new Error('Incorrect passphrase'));
return cb(null, privateKeyWif);
};