nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: canjs/can-zone
Pull request #69 (2016-04-01 12:31:16)

Allow not passing in function into Zone.waitFor
This way you can do stuff like:

```js
var done = Zone.waitFor();

// do some c++ stuff in node
done();
```


Patch:
diff --git a/lib/zone.js b/lib/zone.js
index 2483bf8..6bba5de 100644
--- a/lib/zone.js
+++ b/lib/zone.js
@@ -8,6 +8,7 @@ var forEach = require("./util").forEach;
 require("../register");
 
 var slice = Array.prototype.slice;
+var noop = function(){};
 
 function Deferred(){
 	var dfd = this;
@@ -130,6 +131,7 @@ function Zone(spec) {
 }
 
 Zone.waitFor = function(fn, catchErrors){
+	fn = fn || noop;
 	var zone = Zone.current;
 	if(!zone) return fn;
 	return zone.waitFor(fn, catchErrors);


Test patch:
diff --git a/test/test.js b/test/test.js
index b64706e..cde5c51 100644
--- a/test/test.js
+++ b/test/test.js
@@ -604,6 +604,19 @@ describe("Zone.waitFor", function(){
 			assert(!Zone.current, "There is no current Zone");
 		}).then(done, done);
 	});
+
+	it("do not need to pass in a function for it to work", function(done){
+		new Zone().run(function(){
+			var d = Zone.waitFor();
+
+			setTimeout(function(){
+				d();
+			});
+		})
+		.then(function(){
+			done();
+		}, done);
+	});
 });
 
 describe("Zone.error", function(){
Repository: canjs/can-zone
Pull request #70 (2016-04-04 15:37:56)

Properly handle promise rejection chains
This updates the Promise wrapping to properly handle rejection chains.
If some code throws a string we should wrap it in an Error and rethrow
it. This fixes a bug where `System.import`s that are rejected were not
having their rejection bubble up.


Patch:
diff --git a/lib/tasks.js b/lib/tasks.js
index 6fba59b..9534c19 100644
--- a/lib/tasks.js
+++ b/lib/tasks.js
@@ -52,22 +52,27 @@ exports.requestAnimationFrame = function(rAF){
 exports.then = function(then){
 	return function(onFulfilled, onRejected){
 		var fn;
+		var rejected;
 		var callback = Zone.current.waitFor(function(val){
 			if(fn) {
 				return fn.apply(this, arguments);
+			} else if(rejected) {
+				val = typeof val === "string" ? new Error(val) : val;
+				throw val;
 			}
 			return val;
 		}, false);
 
-		var callWith = function(cb){
+		var callWith = function(cb, isError){
 			return function(){
 				fn = cb;
+				rejected = !!isError;
 				return callback.apply(this, arguments);
 			};
 		};
 
 		return then.call(this, callWith(onFulfilled),
-						 callWith(onRejected));
+						 callWith(onRejected, true));
 	};
 };
 


Test patch:
diff --git a/test/test.js b/test/test.js
index cde5c51..07ca24d 100644
--- a/test/test.js
+++ b/test/test.js
@@ -579,6 +579,37 @@ describe("Promises", function(){
 			assert.equal(data.response, "hello", "got the value");
 		}).then(done);
 	});
+
+	it("Throwing a string works", function(done){
+		new Zone().run(function(){
+
+			Promise.reject("uh oh").then().then(null, function(err){
+				Zone.current.data.reason = err.message;
+			});
+
+		}).then(function(data){
+			assert.equal(data.reason, "uh oh", "got the reason for the rejection");
+			done();
+		});
+	});
+
+	if(typeof System === "object" && !!System.import) {
+		it("Rejects an import for a file that doesn't exist", function(done){
+			new Zone().run(function(){
+				var p = System.import("fake/module");
+
+				p.then(function(mod){
+					Zone.error(new Error("import worked when it should not have"));
+				}, function(err){
+					Zone.current.data.worked = true;
+				});
+
+			}).then(function(data){
+				assert.ok(data.worked, "it rejected like it should have");
+				done();
+			}, done);
+		});
+	}
 });
 
 describe("Zone.waitFor", function(){
Repository: canjs/can-zone
Pull request #82 (2016-07-07 18:18:34)

Fixing setTimeout so it passes extra parameters through to the callback function
can-zone's setTimeout only passes the callback and delay to `window.setTimeout` [here](https://github.com/canjs/can-zone/compare/pass-extra-params-to-settimeout?expand=1#diff-17a298a4fcdf2c8fee67743145edf5f1L51).

[`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout) can be passed parameters that should be passed through to the callback. 

This PR makes sure all arguments are passed to setTimeout; making sure the first argument is the `waitsFor` function can-zone creates.

**Example**
```js
var Zone = require("can-zone");

var zone = new Zone();

zone.run(function(){

  setTimeout(function(str1, str2){
		zone.data.foo = str1 + "-" + str2; //-> undefined-undefined
	}, 100, "can-zone", "canjs")

}).then(function(data){
  console.log(data.foo);
});
```




Patch:
diff --git a/lib/tasks.js b/lib/tasks.js
index 174d73c..0b447f0 100644
--- a/lib/tasks.js
+++ b/lib/tasks.js
@@ -39,16 +39,17 @@ var removeTimer = function(timeoutId, callback){
 
 exports.setTimeout = function(setTimeout){
 	return function(fn, timeout){
+		var args = Array.prototype.slice.call(arguments);
 		var zone = Zone.current;
 		var idInfo;
-		var callback = zone.waitFor(function(){
+		args[0] = zone.waitFor(function(){
 			delete zone.ids[idInfo.id];
 			return fn.apply(this, arguments);
 		});
 
 		var self = this;
 		idInfo = addTimer(function(){
-			return setTimeout.call(self, callback, timeout);
+			return setTimeout.apply(self, args);
 		});
 
 		return idInfo.timeoutId;


Test patch:
diff --git a/test/test.js b/test/test.js
index e93278b..e464116 100644
--- a/test/test.js
+++ b/test/test.js
@@ -252,6 +252,17 @@ describe("setTimeout", function(){
 		});
 	});
 
+	it("Passes extra parameters to callback", function(done){
+		var zone = new Zone();
+		zone.run(function(){
+			setTimeout(function(str1, str2){
+				zone.data.foo = str1 + "-" + str2;
+			}, 10, "can", "zone");
+		}).then(function(data){
+			assert.equal(data.foo, "can-zone", "Extra parameters are passed");
+		}).then(done, done);
+	});
+
 	it("Can be in nested Zones", function(done){
 		var zoneOne = new Zone();
 		zoneOne.run(function(){
Repository: canjs/can-zone
Pull request #100 (2017-01-04 19:00:34)

Reset zone.runningTask after running a task
When a task run, whether there was an error or not, we should be
resetting the `runningTask` variable. This is used to control that hooks
are not called multiple times.


Patch:
diff --git a/lib/zone.js b/lib/zone.js
index d2f0608..a7f3d78 100644
--- a/lib/zone.js
+++ b/lib/zone.js
@@ -47,10 +47,11 @@ Task.prototype.run = function(ctx, args){
 		} else {
 			throw err;
 		}
+	} finally {
+		// If this is a nested task (a task run synchronously then this will
+		// remain as true
+		zone.runningTask = this.nestedTask;
 	}
-	// If this is a nested task (a task run synchronously then this will
-	// remain as true
-	zone.runningTask = this.nestedTask;
 
 	return res;
 };


Test patch:
diff --git a/test/test.js b/test/test.js
index 474989a..2e1baea 100644
--- a/test/test.js
+++ b/test/test.js
@@ -565,7 +565,8 @@ describe("Promises", function(){
 
 	it("Throwing in a Promise chain is returned", function(done){
 		var caught;
-		new Zone().run(function(){
+		var zone = new Zone();
+		zone.run(function(){
 			Promise.resolve().then(function(){
 				throw new Error("oh no");
 			}).then(null, function(err){
@@ -576,6 +577,7 @@ describe("Promises", function(){
 					 "resolved wait promise");
 			assert(!!caught, "Called the Promise errback");
 			assert.equal(caught.message, "oh no", "Correct error object");
+			assert(!zone.runningTask, "Zone is not currently running a task");
 		}).then(done);
 	});
Repository: bitcoinjs/bitcoinjs-lib
Pull request #718 (2016-12-07 11:55:59)

Rename __hasWitnesses -> hasWitnesses, and add tests
Related issue: https://github.com/bitcoinjs/bitcoinjs-lib/issues/703

dcousens: ACK, waiting on travis

afk11: Just one integration test failed, will restart

Patch:
diff --git a/src/transaction.js b/src/transaction.js
index 008e7dc4..f2377a66 100644
--- a/src/transaction.js
+++ b/src/transaction.js
@@ -125,7 +125,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
     }
 
     // was this pointless?
-    if (!tx.__hasWitnesses()) throw new Error('Transaction has superfluous witness data')
+    if (!tx.hasWitnesses()) throw new Error('Transaction has superfluous witness data')
   }
 
   tx.locktime = readUInt32()
@@ -184,7 +184,7 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
   }) - 1)
 }
 
-Transaction.prototype.__hasWitnesses = function () {
+Transaction.prototype.hasWitnesses = function () {
   return this.ins.some(function (x) {
     return x.witness.length !== 0
   })
@@ -195,7 +195,7 @@ Transaction.prototype.byteLength = function () {
 }
 
 Transaction.prototype.__byteLength = function (__allowWitness) {
-  var hasWitnesses = __allowWitness && this.__hasWitnesses()
+  var hasWitnesses = __allowWitness && this.hasWitnesses()
 
   return (
     (hasWitnesses ? 10 : 8) +
@@ -417,7 +417,7 @@ Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitne
 
   writeInt32(this.version)
 
-  var hasWitnesses = __allowWitness && this.__hasWitnesses()
+  var hasWitnesses = __allowWitness && this.hasWitnesses()
 
   if (hasWitnesses) {
     writeUInt8(Transaction.ADVANCED_TRANSACTION_MARKER)


Test patch:
diff --git a/test/transaction.js b/test/transaction.js
index 1963d907..1881bd54 100644
--- a/test/transaction.js
+++ b/test/transaction.js
@@ -119,6 +119,14 @@ describe('Transaction', function () {
     })
   })
 
+  describe('hasWitnesses', function () {
+    fixtures.valid.forEach(function (f) {
+      it('detects if the transaction has witnesses: ' + (f.whex ? 'true' : 'false'), function () {
+        assert.strictEqual(Transaction.fromHex(f.whex ? f.whex : f.hex).hasWitnesses(), !!f.whex)
+      })
+    })
+  })
+
   describe('addInput', function () {
     var prevTxHash
     beforeEach(function () {
Repository: bitcoinjs/bitcoinjs-lib
Pull request #738 (2016-12-17 03:30:10)

script: add fromASM failing test case and fix

dcousens: In favour of https://github.com/bitcoinjs/bitcoinjs-lib/pull/690

Patch:
diff --git a/src/script.js b/src/script.js
index 62c5d3ae..4245bf1d 100644
--- a/src/script.js
+++ b/src/script.js
@@ -145,6 +145,7 @@ function fromASM (asm) {
   return compile(asm.split(' ').map(function (chunkStr) {
     // opcode?
     if (OPS[chunkStr] !== undefined) return OPS[chunkStr]
+    typeforce(types.Hex, chunkStr)
 
     // data!
     return new Buffer(chunkStr, 'hex')


Test patch:
diff --git a/test/fixtures/script.json b/test/fixtures/script.json
index c6a0eea8..e0ef536e 100644
--- a/test/fixtures/script.json
+++ b/test/fixtures/script.json
@@ -189,6 +189,12 @@
         "description": "Not enough data: OP_PUSHDATA4 0xffffffff",
         "script": "4effffffff01"
       }
+    ],
+    "fromASM": [
+      {
+        "description": "Expected Hex, got String \"0xff\"",
+        "script": "0xff OP_CHECKSIG"
+      }
     ]
   }
 }
diff --git a/test/script.js b/test/script.js
index e164cb7d..b9680bf3 100644
--- a/test/script.js
+++ b/test/script.js
@@ -28,6 +28,14 @@ describe('script', function () {
         assert.strictEqual(bscript.toASM(scriptSig), f.asm)
       })
     })
+
+    fixtures.invalid.fromASM.forEach(function (f) {
+      it('throws ' + f.description, function () {
+        assert.throws(function () {
+          bscript.fromASM(f.script)
+        }, new RegExp(f.description))
+      })
+    })
   })
 
   describe('isPushOnly', function () {
Repository: bitcoinjs/bitcoinjs-lib
Pull request #796 (2017-04-22 15:45:54)

Update Litecoin P2SH
### Litecoin Core 0.13.3 (reference client)

https://github.com/litecoin-project/litecoin/blob/master/src/chainparams.cpp#L129

### LoafWallet (iOS Wallet)

https://github.com/litecoin-association/LoafWallet/blob/master/BreadWallet/NSString%2BBitcoin.h#L30



dcousens: Do you have a citation or reference?
And can you provide some test fixtures maybe?
losh11: Sorry, I was about to do that, and got disturbed by stuff.

Sure, I can fix tests, but all tests seem to pass on my machine `2795 passing (12s)`.


dcousens: @losh11 then we need tests! :+1: 
dcousens: Not going to merge until we have some example Litecoin P2SH addresses added to tests to ensure the constant is actually valid...
dcousens: @junderw @losh11 if either of you have example Litecoin P2SH addresses... please either update this PR,  submit a different PR, or just post them.
afk11: Someone grab a LTC P2SH address and run base58 decode on it?

@losh11 as mentioned, the constant should be expressed as 0x32 or 50, but not 0x50.
afk11: Guessing everyones happy, whoever second passes this please merge?
dcousens: If someone just adds fixtures... We're good to merge.
afk11: will do!
losh11: That's great guys! Sorry got really busy, so I didn't notice this...


Patch:
diff --git a/src/networks.js b/src/networks.js
index b4ae5a9d..7515b098 100644
--- a/src/networks.js
+++ b/src/networks.js
@@ -29,7 +29,7 @@ module.exports = {
       private: 0x019d9cfe
     },
     pubKeyHash: 0x30,
-    scriptHash: 0x05,
+    scriptHash: 0x32,
     wif: 0xb0
   }
 }


Test patch:
diff --git a/test/fixtures/address.json b/test/fixtures/address.json
index be32141e..0d4a6324 100644
--- a/test/fixtures/address.json
+++ b/test/fixtures/address.json
@@ -14,6 +14,20 @@
       "base58check": "3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr",
       "script": "OP_HASH160 cd7b44d0b03f2d026d1e586d7ae18903b0d385f6 OP_EQUAL"
     },
+    {
+      "network": "litecoin",
+      "version": 50,
+      "hash": "332bdfb31f688c0be0137c7c038a6d0fea0de0b6",
+      "base58check": "MCZjFcwYJwwYqXAbd3bbnxaCVGs81cp43Z",
+      "script": "OP_HASH160 332bdfb31f688c0be0137c7c038a6d0fea0de0b6 OP_EQUAL"
+    },
+    {
+      "network": "litecoin",
+      "version": 48,
+      "hash": "6ac624143d19a3c91d2ac5605f0aebdfeac5b826",
+      "base58check": "LUxXFcwXFPpRZdMv4aYu6bDwPdC2skQ5YW",
+      "script": "OP_DUP OP_HASH160 6ac624143d19a3c91d2ac5605f0aebdfeac5b826 OP_EQUALVERIFY OP_CHECKSIG"
+    },
     {
       "network": "testnet",
       "version": 111,
@@ -62,3 +76,4 @@
     ]
   }
 }
+
Repository: bitcoinjs/bitcoinjs-lib
Pull request #1285 (2018-12-12 12:09:52)

Fix Block to allow regtest target (easiest possible target)
when exponent was 29 (32 - 3) the write was being performed in index -1 which caused an error, so Block could not parse blocks made on regtest (bits `0x207fffff`)

Added a test to cover it.


Patch:
diff --git a/src/block.js b/src/block.js
index 8c7f3a6e..eb7f7087 100644
--- a/src/block.js
+++ b/src/block.js
@@ -145,7 +145,7 @@ Block.calculateTarget = function (bits) {
   const exponent = ((bits & 0xff000000) >> 24) - 3
   const mantissa = bits & 0x007fffff
   const target = Buffer.alloc(32, 0)
-  target.writeUInt32BE(mantissa, 28 - exponent)
+  target.writeUIntBE(mantissa, 29 - exponent, 3)
   return target
 }
 


Test patch:
diff --git a/test/fixtures/block.json b/test/fixtures/block.json
index c7aa5f7a..b4a1cfed 100644
--- a/test/fixtures/block.json
+++ b/test/fixtures/block.json
@@ -19,6 +19,10 @@
     {
       "bits": "cffca00",
       "expected": "00000000000000000000000000000000000000007fca00000000000000000000"
+    },
+    {
+      "bits": "207fffff",
+      "expected": "7fffff0000000000000000000000000000000000000000000000000000000000"
     }
   ],
   "valid": [
Repository: bitcoinjs/bitcoinjs-lib
Pull request #463 (2015-09-09 02:27:37)

revert 4b825bf
Un-breaking change.

@weilu I think 4b825bf was a mistake.
We need to rethink how we want to expose these constants.
See https://github.com/bitcoinjs/bitcoinjs-lib/issues/464 for discussion of that.


Patch:
diff --git a/src/hdnode.js b/src/hdnode.js
index 22ccecf9..fbf0577e 100644
--- a/src/hdnode.js
+++ b/src/hdnode.js
@@ -11,9 +11,6 @@ var ECPair = require('./ecpair')
 var ecurve = require('ecurve')
 var curve = ecurve.getCurveByName('secp256k1')
 
-var MASTER_SECRET = new Buffer('Bitcoin seed')
-var HIGHEST_BIT = 0x80000000
-
 function HDNode (keyPair, chainCode) {
   typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)
 
@@ -26,13 +23,17 @@ function HDNode (keyPair, chainCode) {
   this.parentFingerprint = 0x00000000
 }
 
+HDNode.HIGHEST_BIT = 0x80000000
+HDNode.LENGTH = 78
+HDNode.MASTER_SECRET = new Buffer('Bitcoin seed')
+
 HDNode.fromSeedBuffer = function (seed, network) {
   typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
 
   if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits')
   if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits')
 
-  var I = createHmac('sha512', MASTER_SECRET).update(seed).digest()
+  var I = createHmac('sha512', HDNode.MASTER_SECRET).update(seed).digest()
   var IL = I.slice(0, 32)
   var IR = I.slice(32)
 
@@ -189,7 +190,7 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
 
 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
 HDNode.prototype.derive = function (index) {
-  var isHardened = index >= HIGHEST_BIT
+  var isHardened = index >= HDNode.HIGHEST_BIT
   var data = new Buffer(37)
 
   // Hardened child
@@ -261,7 +262,7 @@ HDNode.prototype.derive = function (index) {
 
 HDNode.prototype.deriveHardened = function (index) {
   // Only derives hardened private keys by default
-  return this.derive(index + HIGHEST_BIT)
+  return this.derive(index + HDNode.HIGHEST_BIT)
 }
 
 HDNode.prototype.toString = HDNode.prototype.toBase58


Test patch:
diff --git a/test/hdnode.js b/test/hdnode.js
index a14d89fc..3bfed8d2 100644
--- a/test/hdnode.js
+++ b/test/hdnode.js
@@ -193,7 +193,7 @@ describe('HDNode', function () {
       assert.strictEqual(hd.depth, depth || 0)
 
       if (v.hardened) {
-        assert.strictEqual(hd.index, v.m + 0x80000000)
+        assert.strictEqual(hd.index, v.m + HDNode.HIGHEST_BIT)
       } else {
         assert.strictEqual(hd.index, v.m)
       }
diff --git a/test/integration/crypto.js b/test/integration/crypto.js
index f34bd34c..a4946751 100644
--- a/test/integration/crypto.js
+++ b/test/integration/crypto.js
@@ -67,7 +67,7 @@ describe('bitcoinjs-lib (crypto)', function () {
       serQP.copy(data, 0)
 
       // search index space until we find it
-      for (var i = 0; i < 0x80000000; ++i) {
+      for (var i = 0; i < bitcoin.HDNode.HIGHEST_BIT; ++i) {
         data.writeUInt32BE(i, 33)
 
         // calculate I
Repository: bitcoinjs/bitcoinjs-lib
Pull request #484 (2015-10-01 20:15:17)

fix isMultisigOutput
Solve #483 
Change `n < pubKeys.length` to `n !== pubKeys.length` like in [bitcoin](https://github.com/bitcoin/bitcoin/blob/cd78c2a421ae5aa1773c8fb84dadc2c61a01e5e8/src/script/standard.cpp#L102)


Patch:
diff --git a/src/script.js b/src/script.js
index 56c75e98..d6d5c315 100644
--- a/src/script.js
+++ b/src/script.js
@@ -225,12 +225,10 @@ function isMultisigOutput (script) {
   if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
 
   var mOp = chunks[0]
-  if (mOp === OPS.OP_0) return false
   if (mOp < OPS.OP_1) return false
   if (mOp > OPS.OP_16) return false
 
   var nOp = chunks[chunks.length - 2]
-  if (nOp === OPS.OP_0) return false
   if (nOp < OPS.OP_1) return false
   if (nOp > OPS.OP_16) return false
 
@@ -239,7 +237,7 @@ function isMultisigOutput (script) {
   if (n < m) return false
 
   var pubKeys = chunks.slice(1, -2)
-  if (n < pubKeys.length) return false
+  if (n !== pubKeys.length) return false
 
   return pubKeys.every(isCanonicalPubKey)
 }


Test patch:
diff --git a/test/fixtures/script.json b/test/fixtures/script.json
index 59bb6e8e..81f905d5 100644
--- a/test/fixtures/script.json
+++ b/test/fixtures/script.json
@@ -241,6 +241,10 @@
         "description": "n < len(pubKeys)",
         "scriptPubKey": "OP_2 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_2 OP_CHECKMULTISIG"
       },
+      {
+        "description": "n > len(pubKeys)",
+        "scriptPubKey": "OP_1 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_2 OP_CHECKMULTISIG"
+      },
       {
         "description": "non-canonical pubKey (bad length)",
         "scriptPubKey": "OP_1 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffff OP_1 OP_CHECKMULTISIG"
Repository: bitcoinjs/bitcoinjs-lib
Pull request #504 (2015-11-23 14:13:25)

Fix TransactionBuilder.addOutput
#503 

dcousens: Thanks @fanatid, looking good.  Nits can be fixed post-merge if your out of time.

Patch:
diff --git a/src/transaction_builder.js b/src/transaction_builder.js
index e0dbc25f..503ee92a 100644
--- a/src/transaction_builder.js
+++ b/src/transaction_builder.js
@@ -227,10 +227,13 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
 }
 
 TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
-  var valid = this.inputs.every(function (input) {
+  var tx = this.tx
+  var valid = this.inputs.every(function (input, index) {
     if (input.hashType === undefined) return true
 
-    return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
+    var hashType = input.hashType & 0x1f
+    return hashType === Transaction.SIGHASH_NONE ||
+           (hashType === Transaction.SIGHASH_SINGLE && index < tx.outs.length)
   })
 
   if (!valid) throw new Error('No, this would invalidate signatures')
@@ -240,7 +243,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
     scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
   }
 
-  return this.tx.addOutput(scriptPubKey, value)
+  return tx.addOutput(scriptPubKey, value)
 }
 
 TransactionBuilder.prototype.build = function () {


Test patch:
diff --git a/test/transaction_builder.js b/test/transaction_builder.js
index dc1877a7..1ee696fd 100644
--- a/test/transaction_builder.js
+++ b/test/transaction_builder.js
@@ -202,6 +202,34 @@ describe('TransactionBuilder', function () {
       }, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/)
     })
 
+    it('add second output after signed first input with SIGHASH_NONE', function () {
+      txb.addInput(txHash, 0)
+      txb.addOutput(scripts[0], 2000)
+      txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
+      assert.equal(txb.addOutput(scripts[1], 9000), 1)
+    })
+
+    it('add first output after signed first input with SIGHASH_NONE', function () {
+      txb.addInput(txHash, 0)
+      txb.sign(0, keyPair, undefined, Transaction.SIGHASH_NONE)
+      assert.equal(txb.addOutput(scripts[0], 2000), 0)
+    })
+
+    it('add second output after signed first input with SIGHASH_SINGLE', function () {
+      txb.addInput(txHash, 0)
+      txb.addOutput(scripts[0], 2000)
+      txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
+      assert.equal(txb.addOutput(scripts[1], 9000), 1)
+    })
+
+    it('add first output after signed first input with SIGHASH_SINGLE', function () {
+      txb.addInput(txHash, 0)
+      txb.sign(0, keyPair, undefined, Transaction.SIGHASH_SINGLE)
+      assert.throws(function () {
+        txb.addOutput(scripts[0], 2000)
+      }, /No, this would invalidate signatures/)
+    })
+
     it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
       txb.addInput(txHash, 0)
       txb.addOutput(scripts[0], 2000)