|
@ -3,6 +3,10 @@ var Script = require('./script'); |
|
|
var util = require('./util'); |
|
|
var util = require('./util'); |
|
|
var conv = require('./convert'); |
|
|
var conv = require('./convert'); |
|
|
var Crypto = require('./crypto-js/crypto'); |
|
|
var Crypto = require('./crypto-js/crypto'); |
|
|
|
|
|
var Wallet = require('./wallet'); |
|
|
|
|
|
var ECKey = require('./eckey'); |
|
|
|
|
|
var ECDSA = require('./ecdsa'); |
|
|
|
|
|
var Address = require('./address'); |
|
|
|
|
|
|
|
|
var Transaction = function (doc) { |
|
|
var Transaction = function (doc) { |
|
|
this.version = 1; |
|
|
this.version = 1; |
|
@ -265,7 +269,7 @@ Transaction.prototype.clone = function () |
|
|
* This method was unable to detect what the transaction does. Either it |
|
|
* This method was unable to detect what the transaction does. Either it |
|
|
*/ |
|
|
*/ |
|
|
Transaction.prototype.analyze = function (wallet) { |
|
|
Transaction.prototype.analyze = function (wallet) { |
|
|
if (!(wallet instanceof Bitcoin.Wallet)) return null; |
|
|
if (!(wallet instanceof Wallet)) return null; |
|
|
|
|
|
|
|
|
var allFromMe = true, |
|
|
var allFromMe = true, |
|
|
allToMe = true, |
|
|
allToMe = true, |
|
@ -300,7 +304,7 @@ Transaction.prototype.analyze = function (wallet) { |
|
|
|
|
|
|
|
|
if (impact.sign > 0 && impact.value.compareTo(BigInteger.ZERO) > 0) { |
|
|
if (impact.sign > 0 && impact.value.compareTo(BigInteger.ZERO) > 0) { |
|
|
analysis.type = 'recv'; |
|
|
analysis.type = 'recv'; |
|
|
analysis.addr = new Bitcoin.Address(firstMeRecvHash); |
|
|
analysis.addr = new Address(firstMeRecvHash); |
|
|
} else if (allFromMe && allToMe) { |
|
|
} else if (allFromMe && allToMe) { |
|
|
analysis.type = 'self'; |
|
|
analysis.type = 'self'; |
|
|
} else if (allFromMe) { |
|
|
} else if (allFromMe) { |
|
@ -308,7 +312,7 @@ Transaction.prototype.analyze = function (wallet) { |
|
|
// TODO: Right now, firstRecvHash is the first output, which - if the
|
|
|
// TODO: Right now, firstRecvHash is the first output, which - if the
|
|
|
// transaction was not generated by this library could be the
|
|
|
// transaction was not generated by this library could be the
|
|
|
// change address.
|
|
|
// change address.
|
|
|
analysis.addr = new Bitcoin.Address(firstRecvHash); |
|
|
analysis.addr = new Address(firstRecvHash); |
|
|
} else { |
|
|
} else { |
|
|
analysis.type = "other"; |
|
|
analysis.type = "other"; |
|
|
} |
|
|
} |
|
@ -378,7 +382,7 @@ Transaction.prototype.getTotalOutValue = function () { |
|
|
* @returns Object Impact on wallet |
|
|
* @returns Object Impact on wallet |
|
|
*/ |
|
|
*/ |
|
|
Transaction.prototype.calcImpact = function (wallet) { |
|
|
Transaction.prototype.calcImpact = function (wallet) { |
|
|
if (!(wallet instanceof Bitcoin.Wallet)) return BigInteger.ZERO; |
|
|
if (!(wallet instanceof Wallet)) return BigInteger.ZERO; |
|
|
|
|
|
|
|
|
// Calculate credit to us from all outputs
|
|
|
// Calculate credit to us from all outputs
|
|
|
var valueOut = BigInteger.ZERO; |
|
|
var valueOut = BigInteger.ZERO; |
|
@ -474,13 +478,13 @@ Transaction.deserialize = function(buffer) { |
|
|
|
|
|
|
|
|
Transaction.prototype.sign = function(index, key, type) { |
|
|
Transaction.prototype.sign = function(index, key, type) { |
|
|
type = type || SIGHASH_ALL; |
|
|
type = type || SIGHASH_ALL; |
|
|
key = new Bitcoin.ECKey(key); |
|
|
key = new ECKey(key); |
|
|
var pub = key.getPub(), |
|
|
var pub = key.getPub(), |
|
|
hash160 = Bitcoin.Util.sha256ripe160(pub), |
|
|
hash160 = util.sha256ripe160(pub), |
|
|
script = Bitcoin.Script.createOutputScript(new Bitcoin.Address(hash160)), |
|
|
script = Script.createOutputScript(new Address(hash160)), |
|
|
hash = this.hashTransactionForSignature( script, index, type), |
|
|
hash = this.hashTransactionForSignature( script, index, type), |
|
|
sig = key.sign(hash).concat([type]); |
|
|
sig = key.sign(hash).concat([type]); |
|
|
this.ins[i].script = Bitcoin.Script.createInputScript(sig,pub); |
|
|
this.ins[i].script = Script.createInputScript(sig,pub); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -488,8 +492,8 @@ Transaction.prototype.sign = function(index, key, type) { |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
Transaction.prototype.p2shsign = function(index, script, key, type) { |
|
|
Transaction.prototype.p2shsign = function(index, script, key, type) { |
|
|
script = new Bitcoin.Script(script); |
|
|
script = new Script(script); |
|
|
key = new Bitcoin.ECKey(key); |
|
|
key = new ECKey(key); |
|
|
type = type || SIGHASH_ALL; |
|
|
type = type || SIGHASH_ALL; |
|
|
var hash = this.hashTransactionForSignature(script, index, type), |
|
|
var hash = this.hashTransactionForSignature(script, index, type), |
|
|
sig = key.sign(hash).concat([type]); |
|
|
sig = key.sign(hash).concat([type]); |
|
@ -499,9 +503,9 @@ Transaction.prototype.p2shsign = function(index, script, key, type) { |
|
|
Transaction.prototype.multisign = Transaction.prototype.p2shsign; |
|
|
Transaction.prototype.multisign = Transaction.prototype.p2shsign; |
|
|
|
|
|
|
|
|
Transaction.prototype.validateSig = function(index,script,sig,pub) { |
|
|
Transaction.prototype.validateSig = function(index,script,sig,pub) { |
|
|
script = new Bitcoin.Script(script); |
|
|
script = new Script(script); |
|
|
var hash = this.hashTransactionForSignature(script,i,1); |
|
|
var hash = this.hashTransactionForSignature(script,i,1); |
|
|
return Bitcoin.ECDSA.verify(hash, conv.coerceToBytes(sig), |
|
|
return ECDSA.verify(hash, conv.coerceToBytes(sig), |
|
|
conv.coerceToBytes(pub)); |
|
|
conv.coerceToBytes(pub)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|