You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3 lines
77 KiB
3 lines
77 KiB
11 years ago
|
!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.Bitcoin=e():"undefined"!=typeof global?global.Bitcoin=e():"undefined"!=typeof self&&(self.Bitcoin=e())}(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var base58=require("./base58");var Crypto=require("./crypto-js/crypto");var conv=require("./convert");var address_types={prod:0,testnet:111};var p2sh_types={prod:5,testnet:196};var Address=function(bytes){if(typeof bytes==="string"){this.hash=base58.decode(bytes);this.version=this.hash.version}else{this.hash=bytes;this.version=0}};Address.prototype.toString=function(){var hash=this.hash.slice(0);return base58.checkEncode(hash,this.version)};Address.prototype.getHashBase64=function(){return conv.bytesToBase64(this.hash)};Address.getVersion=function(string){return base58.decode(string)[0]};Address.validate=function(string){try{base58.checkDecode(string)}catch(e){return false}return true};Address.decodeString=function(string){return base58.checkDecode(string)};module.exports=Address},{"./base58":2,"./convert":3,"./crypto-js/crypto":4}],2:[function(require,module,exports){var BigInteger=require("./jsbn/jsbn");var alphabet="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";var base=BigInteger.valueOf(58);var positions={};for(var i=0;i<alphabet.length;++i){positions[alphabet[i]]=i}module.exports.encode=function(input){var bi=BigInteger.fromByteArrayUnsigned(input);var chars=[];while(bi.compareTo(base)>=0){var mod=bi.mod(base);chars.push(alphabet[mod.intValue()]);bi=bi.subtract(mod).divide(base)}chars.push(alphabet[bi.intValue()]);for(var i=0;i<input.length;i++){if(input[i]==0){chars.push(alphabet[0])}else break}return chars.reverse().join("")},module.exports.decode=function(input){var base=BigInteger.valueOf(58);var length=input.length;var num=BigInteger.valueOf(0);var leading_zero=0;var seen_other=false;for(var i=0;i<length;++i){var chr=input[i];var p=positions[chr];if(p===undefined){throw new Error("invalid base58 string: "+input)}num=num.multiply(base).add(BigInteger.valueOf(p));if(chr=="1"&&!seen_other){++leading_zero}else{seen_other=true}}var bytes=num.toByteArrayUnsigned();while(leading_zero-->0){bytes.unshift(0)}return bytes};module.exports.checkEncode=function(x,vbyte,format){vbyte=vbyte||0;var front=[vbyte].concat(x);var checksum=Crypto.SHA256(Crypto.SHA256(front,{asBytes:true}),{asBytes:true}).slice(0,4);return module.exports.encode(front.concat(checksum))};module.exports.checkDecode=function(x){var bytes=module.exports.decode(x),front=bytes.slice(0,bytes.length-4),back=bytes.slice(bytes.length-4);var checksum=Crypto.SHA256(Crypto.SHA256(front,{asBytes:true}),{asBytes:true}).slice(0,4);if(""+checksum!=""+back){throw new Error("Checksum failed")}var o=front.slice(1);o.version=front[0];return o}},{"./jsbn/jsbn":11}],3:[function(require,module,exports){var base64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";module.exports.bytesToHex=function(bytes){for(var hex=[],i=0;i<bytes.length;i++){hex.push((bytes[i]>>>4).toString(16));hex.push((bytes[i]&15).toString(16))}return hex.join("")};module.exports.hexToBytes=function(hex){for(var bytes=[],c=0;c<hex.length;c+=2)bytes.push(parseInt(hex.substr(c,2),16));return bytes};module.exports.bytesToBase64=function(bytes){if(typeof btoa=="function")return btoa(Binary.bytesToString(bytes));for(var base64=[],i=0;i<bytes.length;i+=3){var triplet=bytes[i]<<16|bytes[i+1]<<8|bytes[i+2];for(var j=0;j<4;j++){if(i*8+j*6<=bytes.length*8)base64.push(base64map.charAt(triplet>>>6*(3-j)&63));else base64.push("=")}}return base64.join("")};module.ex
|
||
|
return 0}function nbits(x){var r=1,t;if((t=x>>>16)!=0){x=t;r+=16}if((t=x>>8)!=0){x=t;r+=8}if((t=x>>4)!=0){x=t;r+=4}if((t=x>>2)!=0){x=t;r+=2}if((t=x>>1)!=0){x=t;r+=1}return r}function bnBitLength(){if(this.t<=0)return 0;return this.DB*(this.t-1)+nbits(this[this.t-1]^this.s&this.DM)}function bnpDLShiftTo(n,r){var i;for(i=this.t-1;i>=0;--i)r[i+n]=this[i];for(i=n-1;i>=0;--i)r[i]=0;r.t=this.t+n;r.s=this.s}function bnpDRShiftTo(n,r){for(var i=n;i<this.t;++i)r[i-n]=this[i];r.t=Math.max(this.t-n,0);r.s=this.s}function bnpLShiftTo(n,r){var self=this;var bs=n%self.DB;var cbs=self.DB-bs;var bm=(1<<cbs)-1;var ds=Math.floor(n/self.DB),c=self.s<<bs&self.DM,i;for(i=self.t-1;i>=0;--i){r[i+ds+1]=self[i]>>cbs|c;c=(self[i]&bm)<<bs}for(i=ds-1;i>=0;--i)r[i]=0;r[ds]=c;r.t=self.t+ds+1;r.s=self.s;r.clamp()}function bnpRShiftTo(n,r){var self=this;r.s=self.s;var ds=Math.floor(n/self.DB);if(ds>=self.t){r.t=0;return}var bs=n%self.DB;var cbs=self.DB-bs;var bm=(1<<bs)-1;r[0]=self[ds]>>bs;for(var i=ds+1;i<self.t;++i){r[i-ds-1]|=(self[i]&bm)<<cbs;r[i-ds]=self[i]>>bs}if(bs>0)r[self.t-ds-1]|=(self.s&bm)<<cbs;r.t=self.t-ds;r.clamp()}function bnpSubTo(a,r){var self=this;var i=0,c=0,m=Math.min(a.t,self.t);while(i<m){c+=self[i]-a[i];r[i++]=c&self.DM;c>>=self.DB}if(a.t<self.t){c-=a.s;while(i<self.t){c+=self[i];r[i++]=c&self.DM;c>>=self.DB}c+=self.s}else{c+=self.s;while(i<a.t){c-=a[i];r[i++]=c&self.DM;c>>=self.DB}c-=a.s}r.s=c<0?-1:0;if(c<-1)r[i++]=self.DV+c;else if(c>0)r[i++]=c;r.t=i;r.clamp()}function bnpMultiplyTo(a,r){var x=this.abs(),y=a.abs();var i=x.t;r.t=i+y.t;while(--i>=0)r[i]=0;for(i=0;i<y.t;++i)r[i+x.t]=x.am(0,y[i],r,i,0,x.t);r.s=0;r.clamp();if(this.s!=a.s)BigInteger.ZERO.subTo(r,r)}function bnpSquareTo(r){var x=this.abs();var i=r.t=2*x.t;while(--i>=0)r[i]=0;for(i=0;i<x.t-1;++i){var c=x.am(i,x[i],r,2*i,0,1);if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1))>=x.DV){r[i+x.t]-=x.DV;r[i+x.t+1]=1}}if(r.t>0)r[r.t-1]+=x.am(i,x[i],r,2*i,0,1);r.s=0;r.clamp()}function bnpDivRemTo(m,q,r){var self=this;var pm=m.abs();if(pm.t<=0)return;var pt=self.abs();if(pt.t<pm.t){if(q!=null)q.fromInt(0);if(r!=null)self.copyTo(r);return}if(r==null)r=nbi();var y=nbi(),ts=self.s,ms=m.s;var nsh=self.DB-nbits(pm[pm.t-1]);if(nsh>0){pm.lShiftTo(nsh,y);pt.lShiftTo(nsh,r)}else{pm.copyTo(y);pt.copyTo(r)}var ys=y.t;var y0=y[ys-1];if(y0==0)return;var yt=y0*(1<<self.F1)+(ys>1?y[ys-2]>>self.F2:0);var d1=self.FV/yt,d2=(1<<self.F1)/yt,e=1<<self.F2;var i=r.t,j=i-ys,t=q==null?nbi():q;y.dlShiftTo(j,t);if(r.compareTo(t)>=0){r[r.t++]=1;r.subTo(t,r)}BigInteger.ONE.dlShiftTo(ys,t);t.subTo(y,y);while(y.t<ys)y[y.t++]=0;while(--j>=0){var qd=r[--i]==y0?self.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);if((r[i]+=y.am(0,qd,r,j,0,ys))<qd){y.dlShiftTo(j,t);r.subTo(t,r);while(r[i]<--qd)r.subTo(t,r)}}if(q!=null){r.drShiftTo(ys,q);if(ts!=ms)BigInteger.ZERO.subTo(q,q)}r.t=ys;r.clamp();if(nsh>0)r.rShiftTo(nsh,r);if(ts<0)BigInteger.ZERO.subTo(r,r)}function bnMod(a){var r=nbi();this.abs().divRemTo(a,null,r);if(this.s<0&&r.compareTo(BigInteger.ZERO)>0)a.subTo(r,r);return r}function Classic(m){this.m=m}function cConvert(x){if(x.s<0||x.compareTo(this.m)>=0)return x.mod(this.m);else return x}function cRevert(x){return x}function cReduce(x){x.divRemTo(this.m,null,x)}function cMulTo(x,y,r){x.multiplyTo(y,r);this.reduce(r)}function cSqrTo(x,r){x.squareTo(r);this.reduce(r)}Classic.prototype.convert=cConvert;Classic.prototype.revert=cRevert;Classic.prototype.reduce=cReduce;Classic.prototype.mulTo=cMulTo;Classic.prototype.sqrTo=cSqrTo;function bnpInvDigit(){if(this.t<1)return 0;var x=this[0];if((x&1)==0)return 0;var y=x&3;y=y*(2-(x&15)*y)&15;y=y*(2-(x&255)*y)&255;y=y*(2-((x&65535)*y&65535))&65535;y=y*(2-x*y%this.DV)%this.DV;return y>0?this.DV-y:-y}function Montgomery(m){this.m=m;this.mp=m.invDigit();this.mpl=this.mp&32767;this.mph=this.mp>>15;this.um=(1<<m.DB-15)-1;this.mt2=2*m.t}function montConvert(x){var r=nbi();x.abs().dlShiftTo(this.m.t,r);r.divRemTo(this.m,null,r);if(x.s<0&&r.compareTo(BigInteger.ZERO)>0)this.m.subTo(r,r);return r}function montRevert(x){var r=nbi();x.copyTo(r);this.reduce(r);return r}function montReduce(x){w
|
||
|
this.ins=[];this.outs=[];this.timestamp=null;this.block=null;if(doc){if(doc.hash)this.hash=doc.hash;if(doc.version)this.version=doc.version;if(doc.lock_time)this.lock_time=doc.lock_time;if(doc.ins&&doc.ins.length){for(var i=0;i<doc.ins.length;i++){this.addInput(new TransactionIn(doc.ins[i]))}}if(doc.outs&&doc.outs.length){for(var i=0;i<doc.outs.length;i++){this.addOutput(new TransactionOut(doc.outs[i]))}}if(doc.timestamp)this.timestamp=doc.timestamp;if(doc.block)this.block=doc.block}};Transaction.objectify=function(txs){var objs=[];for(var i=0;i<txs.length;i++){objs.push(new Transaction(txs[i]))}return objs};Transaction.prototype.addInput=function(tx,outIndex){if(arguments[0]instanceof TransactionIn){this.ins.push(arguments[0])}else{this.ins.push(new TransactionIn({outpoint:{hash:tx.hash,index:outIndex},script:new Script,sequence:4294967295}))}};Transaction.prototype.addOutput=function(address,value){if(arguments[0]instanceof TransactionOut){this.outs.push(arguments[0])}else{if(value instanceof BigInteger){value=value.toByteArrayUnsigned().reverse();while(value.length<8)value.push(0)}else if(Bitcoin.Util.isArray(value)){}this.outs.push(new TransactionOut({value:value,script:Script.createOutputScript(address)}))}};var bytesToWords=function(bytes){for(var words=[],i=0,b=0;i<bytes.length;i++,b+=8)words[b>>>5]|=bytes[i]<<24-b%32;return words};var wordsToBytes=function(words){for(var bytes=[],b=0;b<words.length*32;b+=8)bytes.push(words[b>>>5]>>>24-b%32&255);return bytes};Transaction.prototype.serialize=function(){var buffer=[];buffer=buffer.concat(wordsToBytes([parseInt(this.version)]).reverse());buffer=buffer.concat(util.numToVarInt(this.ins.length));for(var i=0;i<this.ins.length;i++){var txin=this.ins[i];buffer=buffer.concat(conv.hexToBytes(txin.outpoint.hash));buffer=buffer.concat(wordsToBytes([parseInt(txin.outpoint.index)]).reverse());var scriptBytes=txin.script.buffer;buffer=buffer.concat(util.numToVarInt(scriptBytes.length));buffer=buffer.concat(scriptBytes);buffer=buffer.concat(wordsToBytes([parseInt(txin.sequence)]).reverse())}buffer=buffer.concat(util.numToVarInt(this.outs.length));for(var i=0;i<this.outs.length;i++){var txout=this.outs[i];buffer=buffer.concat(txout.value);var scriptBytes=txout.script.buffer;buffer=buffer.concat(util.numToVarInt(scriptBytes.length));buffer=buffer.concat(scriptBytes)}buffer=buffer.concat(wordsToBytes([parseInt(this.lock_time)]).reverse());return buffer};var OP_CODESEPARATOR=171;var SIGHASH_ALL=1;var SIGHASH_NONE=2;var SIGHASH_SINGLE=3;var SIGHASH_ANYONECANPAY=80;Transaction.prototype.hashTransactionForSignature=function(connectedScript,inIndex,hashType){var txTmp=this.clone();for(var i=0;i<txTmp.ins.length;i++){txTmp.ins[i].script=new Script}txTmp.ins[inIndex].script=connectedScript;if((hashType&31)==SIGHASH_NONE){txTmp.outs=[];for(var i=0;i<txTmp.ins.length;i++)if(i!=inIndex)txTmp.ins[i].sequence=0}else if((hashType&31)==SIGHASH_SINGLE){}if(hashType&SIGHASH_ANYONECANPAY){txTmp.ins=[txTmp.ins[inIndex]]}var buffer=txTmp.serialize();buffer=buffer.concat(wordsToBytes([parseInt(hashType)]).reverse());var hash1=Crypto.SHA256(buffer,{asBytes:true});return Crypto.SHA256(hash1,{asBytes:true})};Transaction.prototype.getHash=function(){var buffer=this.serialize();return Crypto.SHA256(Crypto.SHA256(buffer,{asBytes:true}),{asBytes:true})};Transaction.prototype.clone=function(){var newTx=new Transaction;newTx.version=this.version;newTx.lock_time=this.lock_time;for(var i=0;i<this.ins.length;i++){var txin=this.ins[i].clone();newTx.addInput(txin)}for(var i=0;i<this.outs.length;i++){var txout=this.outs[i].clone();newTx.addOutput(txout)}return newTx};Transaction.prototype.analyze=function(wallet){if(!(wallet instanceof Bitcoin.Wallet))return null;var allFromMe=true,allToMe=true,firstRecvHash=null,firstMeRecvHash=null,firstSendHash=null;for(var i=this.outs.length-1;i>=0;i--){var txout=this.outs[i];var hash=txout.script.simpleOutPubKeyHash();if(!wallet.hasHash(hash)){allToMe=false}else{firstMeRecvHash=hash}firstRecvHash=hash}for(var i=this.ins.length-1;i>=0;i--){var txin=this.ins[i];firstSendHash=txin.
|