Browse Source

Merge pull request #904 from braydonf/bug/safari-address

Fixed bugs in Safari and IE. Closes #837 and #784
patch-2
Esteban Ordano 10 years ago
parent
commit
cfecfa2d8d
  1. 12
      lib/address.js
  2. 5
      lib/script/script.js
  3. 1
      lib/transaction/output.js
  4. 4
      lib/transaction/transaction.js
  5. 2
      test/hdkeycache.js
  6. 1
      test/mocha.opts
  7. 2
      test/paymentprotocol/index.js
  8. 6
      test/script/interpreter.js
  9. 1
      test/transaction/transaction.js
  10. 3
      test/util/preconditions.js

12
lib/address.js

@ -100,15 +100,17 @@ function Address(data, network, type) {
* @returns {Object} An "info" object with "type", "network", and "hashBuffer"
*/
Address.prototype._classifyArguments = function(data, network, type) {
var PublicKey = require('./publickey');
var Script = require('./script');
/* jshint maxcomplexity: 10 */
// transform and validate input data
if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
return Address._transformHash(data);
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
return Address._transformBuffer(data, network, type);
} else if (data.constructor && (data.constructor.name && data.constructor.name === 'PublicKey')) {
} else if (data instanceof PublicKey) {
return Address._transformPublicKey(data);
} else if (data.constructor && (data.constructor.name && data.constructor.name === 'Script')) {
} else if (data instanceof Script) {
return Address._transformScript(data, network);
} else if (typeof(data) === 'string') {
return Address._transformString(data, network, type);
@ -213,8 +215,9 @@ Address._transformBuffer = function(buffer, network, type){
* @private
*/
Address._transformPublicKey = function(pubkey){
var PublicKey = require('./publickey');
var info = {};
if (!pubkey.constructor || (pubkey.constructor.name && pubkey.constructor.name !== 'PublicKey')) {
if (!(pubkey instanceof PublicKey)) {
throw new TypeError('Address must be an instance of PublicKey.');
}
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
@ -230,8 +233,9 @@ Address._transformPublicKey = function(pubkey){
* @private
*/
Address._transformScript = function(script, network){
var Script = require('./script');
var info = {};
if (!script.constructor || (script.constructor.name && script.constructor.name !== 'Script')) {
if (!(script instanceof Script)) {
throw new TypeError('Address must be an instance of Script.');
}
if (script.isScriptHashOut()) {

5
lib/script/script.js

@ -177,7 +177,6 @@ Script.fromString = function(str) {
Script.prototype.toString = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
var opcodenum = chunk.opcodenum;
@ -459,7 +458,7 @@ Script.prototype._addByType = function(obj, prepend) {
this._addOpcode(obj, prepend);
} else if (typeof obj === 'number') {
this._addOpcode(obj, prepend);
} else if (obj.constructor && obj.constructor.name && obj.constructor.name === 'Opcode') {
} else if (obj instanceof Opcode) {
this._addOpcode(obj, prepend);
} else if (BufferUtil.isBuffer(obj)) {
this._addBuffer(obj, prepend);
@ -484,7 +483,7 @@ Script.prototype._addOpcode = function(opcode, prepend) {
var op;
if (typeof opcode === 'number') {
op = opcode;
} else if (opcode.constructor && opcode.constructor.name && opcode.constructor.name === 'Opcode') {
} else if (opcode instanceof Opcode) {
op = opcode.toNumber();
} else {
op = Opcode(opcode).toNumber();

1
lib/transaction/output.js

@ -83,7 +83,6 @@ Output.prototype.setScript = function(script) {
this._scriptBuffer = script;
this._script = null;
} else {
console.log(script);
throw new TypeError('Unrecognized Argument');
}
return this;

4
lib/transaction/transaction.js

@ -656,11 +656,11 @@ Transaction.prototype.verify = function() {
var valueoutbn = BN(0);
for (var i = 0; i < this.outputs.length; i++) {
var txout = this.outputs[i];
var valuebn = BN(txout.satoshis.toString(16));
var valuebn = txout._satoshis;
if (valuebn.lt(0)) {
return 'transaction txout ' + i + ' negative';
}
if (valuebn.gt(Transaction.MAX_MONEY)) {
if (valuebn.gt(BN(Transaction.MAX_MONEY, 10))) {
return 'transaction txout ' + i + ' greater than MAX_MONEY';
}
valueoutbn = valueoutbn.add(valuebn);

2
test/hdkeycache.js

@ -8,6 +8,8 @@ var HDPrivateKey = bitcore.HDPrivateKey;
var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
describe('HDKey cache', function() {
this.timeout(10000);
/* jshint unused: false */
var cache = bitcore._HDKeyCache;
var master = new HDPrivateKey(xprivkey);

1
test/mocha.opts

@ -1 +1,2 @@
--recursive
--timeout 5000

2
test/paymentprotocol/index.js

@ -279,6 +279,8 @@ var bitpayRequest = new Buffer(''
describe('PaymentProtocol', function() {
this.timeout(15000);
it('should be able to create class', function() {
should.exist(PaymentProtocol);
});

6
test/script/interpreter.js

@ -259,7 +259,8 @@ describe('Interpreter', function() {
return;
}
c++;
it('should pass tx_' + (expected ? '' : 'in') + 'valid vector ' + c, function() {
var cc = c; //copy to local
it('should pass tx_' + (expected ? '' : 'in') + 'valid vector ' + cc, function() {
var inputs = vector[0];
var txhex = vector[1];
var flags = getFlags(vector[2]);
@ -291,9 +292,10 @@ describe('Interpreter', function() {
}
});
var txVerified = tx.verify();
txVerified = _.isBoolean(txVerified);
txVerified = (txVerified === true) ? true : false;
allInputsVerified = allInputsVerified && txVerified;
allInputsVerified.should.equal(expected);
});
});
};

1
test/transaction/transaction.js

@ -84,6 +84,7 @@ describe('Transaction', function() {
});
describe('transaction creation test vector', function() {
this.timeout(5000);
var index = 0;
transactionVector.forEach(function(vector) {
index++;

3
test/util/preconditions.js

@ -52,7 +52,8 @@ describe('preconditions', function() {
$.checkArgumentType(1, PrivateKey);
} catch (e) {
error = e;
e.message.should.equal('Invalid Argument for (unknown name), expected PrivateKey but got number');
var fail = !(~e.message.indexOf('Invalid Argument for (unknown name)'));
fail.should.equal(false);
}
should.exist(error);
});

Loading…
Cancel
Save