(function(){functiong(a,b,c,d){return0<=a&&a<=15?b^c^d:16<=a&&a<=31?b&c|~b&d:32<=a&&a<=47?(b|~c)^d:48<=a&&a<=63?b&d|c&~d:64<=a&&a<=79?b^(c|~d):"rmd160_f: j out of range"}functionh(a){return0<=a&&a<=15?0:16<=a&&a<=31?1518500249:32<=a&&a<=47?1859775393:48<=a&&a<=63?2400959708:64<=a&&a<=79?2840853838:"rmd160_K1: j out of range"}functioni(a){return0<=a&&a<=15?1352829926:16<=a&&a<=31?1548603684:32<=a&&a<=47?1836072691:48<=a&&a<=63?2053994217:64<=a&&a<=79?0:"rmd160_K2: j out of range"}functionn(a,b){varc=(a&65535)+(b&65535),d=(a>>16)+(b>>16)+(c>>16);returnd<<16|c&65535}functiono(a,b){returna<<b|a>>>32-b}vara=Crypto,b=a.util,c=a.charenc,d=c.UTF8,e=c.Binary;b.bytesToLWords=function(a){varb=Array(a.length>>2);for(varc=0;c<b.length;c++)b[c]=0;for(varc=0;c<a.length*8;c+=8)b[c>>5]|=(a[c/8]&255)<<c%32;returnb},b.lWordsToBytes=function(a){varb=[];for(varc=0;c<a.length*32;c+=8)b.push(a[c>>5]>>>c%32&255);returnb};varf=a.RIPEMD160=function(a,c){vard=b.lWordsToBytes(f._rmd160(a));returnc&&c.asBytes?d:c&&c.asString?e.bytesToString(d):b.bytesToHex(d)};f._rmd160=function(a){a.constructor==String&&(a=d.stringToBytes(a));varc=b.bytesToLWords(a),e=a.length*8;c[e>>5]|=128<<e%32,c[(e+64>>>9<<4)+14]=e;varf=1732584193,p=4023233417,q=2562383102,r=271733878,s=3285377520;for(vart=0;t<c.length;t+=16){varu,v=f,w=p,x=q,y=r,z=s,A=f,B=p,C=q,D=r,E=s;for(varF=0;F<=79;++F)u=n(v,g(F,w,x,y)),u=n(u,c[t+j[F]]),u=n(u,h(F)),u=n(o(u,l[F]),z),v=z,z=y,y=o(x,10),x=w,w=u,u=n(A,g(79-F,B,C,D)),u=n(u,c[t+k[F]]),u=n(u,i(F)),u=n(o(u,m[F]),E),A=E,E=D,D=o(C,10),C=B,B=u;u=n(p,n(x,D)),p=n(q,n(y,E)),q=n(r,n(z,A)),r=n(s,n(v,B)),s=n(f,n(w,C)),f=u}return[f,p,q,r,s]};varj=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8,3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12,1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2,4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13],k=[5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12,6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2,15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13,8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14,12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11],l=[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8,7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12,11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5,11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12,9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6],m=[8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6,9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11,9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5,15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8,8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11]})();
functionintegerToBytes(a,b){varc=a.toByteArrayUnsigned();if(b<c.length)c=c.slice(c.length-b);elsewhile(b>c.length)c.unshift(0);returnc}functiondmp(a){returnainstanceofBigInteger||(a=a.toBigInteger()),Crypto.util.bytesToHex(a.toByteArrayUnsigned())}ECFieldElementFp.prototype.getByteLength=function(){returnMath.floor((this.toBigInteger().bitLength()+7)/8)},ECPointFp.prototype.getEncoded=function(a){varb=this.getX().toBigInteger(),c=this.getY().toBigInteger(),d=integerToBytes(b,32);returna?c.isEven()?d.unshift(2):d.unshift(3):(d.unshift(4),d=d.concat(integerToBytes(c,32))),d},ECPointFp.decodeFrom=function(a,b){varc=b[0],d=b.length-1,e=b.slice(1,1+d/2),f=b.slice(1+d/2,1+d);e.unshift(0),f.unshift(0);varg=newBigInteger(e),h=newBigInteger(f);returnnewECPointFp(a,a.fromBigInteger(g),a.fromBigInteger(h))},ECPointFp.prototype.add2D=function(a){if(this.isInfinity())returna;if(a.isInfinity())returnthis;if(this.x.equals(a.x))returnthis.y.equals(a.y)?this.twice():this.curve.getInfinity();varb=a.x.subtract(this.x),c=a.y.subtract(this.y),d=c.divide(b),e=d.square().subtract(this.x).subtract(a.x),f=d.multiply(this.x.subtract(e)).subtract(this.y);returnnewECPointFp(this.curve,e,f)},ECPointFp.prototype.twice2D=function(){if(this.isInfinity())returnthis;if(this.y.toBigInteger().signum()==0)returnthis.curve.getInfinity();vara=this.curve.fromBigInteger(BigInteger.valueOf(2)),b=this.curve.fromBigInteger(BigInteger.valueOf(3)),c=this.x.square().multiply(b).add(this.curve.a).divide(this.y.multiply(a)),d=c.square().subtract(this.x.multiply(a)),e=c.multiply(this.x.subtract(d)).subtract(this.y);returnnewECPointFp(this.curve,d,e)},ECPointFp.prototype.multiply2D=function(a){if(this.isInfinity())returnthis;if(a.signum()==0)returnthis.curve.getInfinity();varb=a,c=b.multiply(newBigInteger("3")),d=this.negate(),e=this,f;for(f=c.bitLength()-2;f>0;--f){e=e.twice();varg=c.testBit(f),h=b.testBit(f);g!=h&&(e=e.add2D(g?this:d))}returne},ECPointFp.prototype.isOnCurve=function(){vara=this.getX().toBigInteger(),b=this.getY().toBigInteger(),c=this.curve.getA().toBigInteger(),d=this.curve.getB().toBigInteger(),e=this.curve.getQ(),f=b.multiply(b).mod(e),g=a.multiply(a).multiply(a).add(c.multiply(a)).add(d).mod(e);returnf.equals(g)},ECPointFp.prototype.validate=function(){vara=this.curve.getQ();if(this.isInfinity())thrownewError("Point is at infinity.");varb=this.getX().toBigInteger(),c=this.getY().toBigInteger();if(b.compareTo(BigInteger.ONE)<0||b.compareTo(a.subtract(BigInteger.ONE))>0)thrownewError("x coordinate out of bounds");if(c.compareTo(BigInteger.ONE)<0||c.compareTo(a.subtract(BigInteger.ONE))>0)thrownewError("y coordinate out of bounds");if(!this.isOnCurve())thrownewError("Point is not on the curve.");if(this.multiply(a).isInfinity())thrownewError("Point is not a scalar multiple of G.");return!0},Bitcoin.ECDSA=function(){functionc(a,b,c,d){vare=Math.max(b.bitLength(),d.bitLength()),f=a.add2D(c),g=a.curve.getInfinity();for(varh=e-1;h>=0;--h)g=g.twice2D(),g.z=BigInteger.ONE,b.testBit(h)?d.testBit(h)?g=g.add2D(f):g=g.add2D(a):d.testBit(h)&&(g=g.add2D(c));returng}vara=getSECCurveByName("secp256k1"),b=newSecureRandom,d={getBigRandom:function(a){return(newBigInteger(a.bitLength(),b)).mod(a.subtract(BigInteger.ONE)).add(BigInteger.ONE)},sign:function(b,c){vare=c,f=a.getN(),g=BigInteger.fromByteArrayUnsigned(b);dovarh=d.getBigRandom(f),i=a.getG(),j=i.multiply(h),k=j.getX().toBigInteger().mod(f);while(k.compareTo(BigInteger.ZERO)<=0);varl=h.modInverse(f).multiply(g.add(e.multiply(k))).mod(f);returnd.serializeSig(k,l)},verify:function(b,e,f){varg=d.parseSig(e),h=g.r,i=g.s,j=a.getN(),k=BigInteger.fromByteArrayUnsigned(b);if(h.compareTo(BigInteger.ONE)<0||h.compareTo(j)>=0)return!1;if(i.compareTo(BigInteger.ONE)<0||i.compareTo(j)>=0)return!1;varl=i.modInverse(j),m=k.multiply(l).mod(j),n=h.multiply(l).mod(j),o=a.getG(),p=ECPointFp.decodeFrom(a.getCurve(),f),q=c(o,m,p,n),r=q.x.toBigInteger().mod(j);returnr.equals(h)},serializeSig:function(a,b){varc=a.toByteArrayUnsigned(),d=b.toByteArrayUnsigned(),e=[];retu
(function(){varOpcode=Bitcoin.Opcode;for(variinOpcode.map)eval("var "+i+" = "+Opcode.map[i]+";");varScript=Bitcoin.Script=function(a){if(!a)this.buffer=[];elseif("string"==typeofa)this.buffer=Crypto.util.base64ToBytes(a);elseif(Bitcoin.Util.isArray(a))this.buffer=a;elseif(ainstanceofScript)this.buffer=a.buffer;elsethrownewError("Invalid script");this.parse()};Script.prototype.parse=function(){functionc(c){a.chunks.push(a.buffer.slice(b,b+c)),b+=c}vara=this;this.chunks=[];varb=0;while(b<this.buffer.length){vard=this.buffer[b++];d>=240&&(d=d<<8|this.buffer[b++]);vare;d>0&&d<OP_PUSHDATA1?c(d):d==OP_PUSHDATA1?(e=this.buffer[b++],c(e)):d==OP_PUSHDATA2?(e=this.buffer[b++]<<8|this.buffer[b++],c(e)):d==OP_PUSHDATA4?(e=this.buffer[b++]<<24|this.buffer[b++]<<16|this.buffer[b++]<<8|this.buffer[b++],c(e)):this.chunks.push(d)}},Script.prototype.getOutType=function(){returnthis.chunks[this.chunks.length-1]==OP_CHECKMULTISIG&&this.chunks[this.chunks.length-2]<=3?"Multisig":this.chunks.length==5&&this.chunks[0]==OP_DUP&&this.chunks[1]==OP_HASH160&&this.chunks[3]==OP_EQUALVERIFY&&this.chunks[4]==OP_CHECKSIG?"Address":this.chunks.length==2&&this.chunks[1]==OP_CHECKSIG?"Pubkey":"Strange"},Script.prototype.simpleOutHash=function(){switch(this.getOutType()){case"Address":returnthis.chunks[2];case"Pubkey":returnBitcoin.Util.sha256ripe160(this.chunks[0]);default:thrownewError("Encountered non-standard scriptPubKey")}},Script.prototype.simpleOutPubKeyHash=Script.prototype.simpleOutHash,Script.prototype.getInType=function(){returnthis.chunks.length==1&&Bitcoin.Util.isArray(this.chunks[0])?"Pubkey":this.chunks.length==2&&Bitcoin.Util.isArray(this.chunks[0])&&Bitcoin.Util.isArray(this.chunks[1])?"Address":"Strange"},Script.prototype.simpleInPubKey=function(){switch(this.getInType()){case"Address":returnthis.chunks[1];case"Pubkey":thrownewError("Script does not contain pubkey.");default:thrownewError("Encountered non-standard scriptSig")}},Script.prototype.simpleInHash=function(){returnBitcoin.Util.sha256ripe160(this.simpleInPubKey())},Script.prototype.simpleInPubKeyHash=Script.prototype.simpleInHash,Script.prototype.writeOp=function(a){this.buffer.push(a),this.chunks.push(a)},Script.prototype.writeBytes=function(a){a.length<OP_PUSHDATA1?this.buffer.push(a.length):a.length<=255?(this.buffer.push(OP_PUSHDATA1),this.buffer.push(a.length)):a.length<=65535?(this.buffer.push(OP_PUSHDATA2),this.buffer.push(a.length&255),this.buffer.push(a.length>>>8&255)):(this.buffer.push(OP_PUSHDATA4),this.buffer.push(a.length&255),this.buffer.push(a.length>>>8&255),this.buffer.push(a.length>>>16&255),this.buffer.push(a.length>>>24&255)),this.buffer=this.buffer.concat(a),this.chunks.push(a)},Script.createOutputScript=function(a){varb=newScript;returnb.writeOp(OP_DUP),b.writeOp(OP_HASH160),b.writeBytes(a.hash),b.writeOp(OP_EQUALVERIFY),b.writeOp(OP_CHECKSIG),b},Script.prototype.extractAddresses=function(a){switch(this.getOutType()){case"Address":returna.push(newAddress(this.chunks[2])),1;case"Pubkey":returna.push(newAddress(Util.sha256ripe160(this.chunks[0]))),1;case"Multisig":for(varb=1;b<this.chunks.length-2;++b)a.push(newAddress(Util.sha256ripe160(this.chunks[b])));returnthis.chunks[0]-OP_1+1;default:thrownewError("Encountered non-standard scriptPubKey")}},Script.createMultiSigOutputScript=function(a,b){varc=newBitcoin.Script;c.writeOp(OP_1+a-1);for(vard=0;d<b.length;++d)c.writeBytes(b[d]);returnc.writeOp(OP_1+b.length-1),c.writeOp(OP_CHECKMULTISIG),c},Script.createInputScript=function(a,b){varc=newScript;returnc.writeBytes(a),c.writeBytes(b),c},Script.prototype.clone=function(){returnnewScript(this.buffer)}})();
(function(){vara=Bitcoin.Script,b=Bitcoin.Transaction=function(a){this.version=1,this.lock_time=0,this.ins=[],this.outs=[],this.timestamp=null,this.block=null;if(a){a.hash&&(this.hash=a.hash),a.version&&(this.version=a.version),a.lock_time&&(this.lock_time=a.lock_time);if(a.ins&&a.ins.length)for(varb=0;b<a.ins.length;b++)this.addInput(newh(a.ins[b]));if(a.outs&&a.outs.length)for(varb=0;b<a.outs.length;b++)this.addOutput(newi(a.outs[b]));a.timestamp&&(this.timestamp=a.timestamp),a.block&&(this.block=a.block)}};b.objectify=function(a){varc=[];for(vard=0;d<a.length;d++)c.push(newb(a[d]));returnc},b.prototype.addInput=function(a,b){arguments[0]instanceofh?this.ins.push(arguments[0]):this.ins.push(newh({outpoint:{hash:a.hash,index:b},script:newBitcoin.Script,sequence:4294967295}))},b.prototype.addOutput=function(b,c){if(arguments[0]instanceofi)this.outs.push(arguments[0]);else{if(cinstanceofBigInteger){c=c.toByteArrayUnsigned().reverse();while(c.length<8)c.push(0)}else!Bitcoin.Util.isArray(c);this.outs.push(newi({value:c,script:a.createOutputScript(b)}))}},b.prototype.serialize=function(){vara=[];a=a.concat(Crypto.util.wordsToBytes([parseInt(this.version)]).reverse()),a=a.concat(Bitcoin.Util.numToVarInt(this.ins.length));for(varb=0;b<this.ins.length;b++){varc=this.ins[b];a=a.concat(Crypto.util.base64ToBytes(c.outpoint.hash)),a=a.concat(Crypto.util.wordsToBytes([parseInt(c.outpoint.index)]).reverse());vard=c.script.buffer;a=a.concat(Bitcoin.Util.numToVarInt(d.length)),a=a.concat(d),a=a.concat(Crypto.util.wordsToBytes([parseInt(c.sequence)]).reverse())}a=a.concat(Bitcoin.Util.numToVarInt(this.outs.length));for(varb=0;b<this.outs.length;b++){vare=this.outs[b];a=a.concat(e.value);vard=e.script.buffer;a=a.concat(Bitcoin.Util.numToVarInt(d.length)),a=a.concat(d)}returna=a.concat(Crypto.util.wordsToBytes([parseInt(this.lock_time)]).reverse()),a};varc=171,d=1,e=2,f=3,g=80;b.prototype.hashTransactionForSignature=function(b,c,d){varh=this.clone();for(vari=0;i<h.ins.length;i++)h.ins[i].script=newa;h.ins[c].script=b;if((d&31)==e){h.outs=[];for(vari=0;i<h.ins.length;i++)i!=c&&(h.ins[i].sequence=0)}else(d&31)!=f;d&g&&(h.ins=[h.ins[c]]),console.log(h);varj=h.serialize();j=j.concat(Crypto.util.wordsToBytes([parseInt(d)]).reverse()),console.log("signtx: "+Crypto.util.bytesToHex(j));vark=Crypto.SHA256(j,{asBytes:!0});returnconsole.log("sha256_1: ",Crypto.util.bytesToHex(k)),Crypto.SHA256(k,{asBytes:!0})},b.prototype.getHash=function(){vara=this.serialize();returnCrypto.SHA256(Crypto.SHA256(a,{asBytes:!0}),{asBytes:!0})},b.prototype.clone=function(){vara=newb;a.version=this.version,a.lock_time=this.lock_time;for(varc=0;c<this.ins.length;c++){vard=this.ins[c].clone();a.addInput(d)}for(varc=0;c<this.outs.length;c++){vare=this.outs[c].clone();a.addOutput(e)}returna},b.prototype.analyze=function(a){if(ainstanceofBitcoin.Wallet){varb=!0,c=!0,d=null,e=null,f=null;for(varg=this.outs.length-1;g>=0;g--){varh=this.outs[g],i=h.script.simpleOutPubKeyHash();a.hasHash(i)?e=i:c=!1,d=i}for(varg=this.ins.length-1;g>=0;g--){varj=this.ins[g];f=j.script.simpleInPubKeyHash();if(!a.hasHash(f)){b=!1;break}}vark=this.calcImpact(a),l={};returnl.impact=k,k.sign>0&&k.value.compareTo(BigInteger.ZERO)>0?(l.type="recv",l.addr=newBitcoin.Address(e)):b&&c?l.type="self":b?(l.type="sent",l.addr=newBitcoin.Address(d)):l.type="other",l}returnnull},b.prototype.getDescription=function(a){varb=this.analyze(a);if(!b)return"";switch(b.type){case"recv":return"Received with "+b.addr;case"sent":return"Payment to "+b.addr;case"self":return"Payment to yourself";case"other":default:return""}},b.prototype.getTotalOutValue=function(){vara=BigInteger.ZERO;for(varb=0;b<this.outs.length;b++){varc=this.outs[b];a=a.add(Bitcoin.Util.valueToBigInt(c.value))}returna},b.prototype.getTotalValue=b.prototype.getTotalOutValue,b.prototype.calcImpact=function(a){if(ainstanceofBitcoin.Wallet){varb=BigInteger.ZERO;for(varc=0;c<this.outs.length;c++){vard=this.outs[c],e=Crypto.util.bytesToBase64(d.script.simpleOutPubKeyHash());a.hasHash(e)&&(b=b.ad
Bitcoin.Wallet=function(){vara=Bitcoin.Script,b=Bitcoin.TransactionIn,c=Bitcoin.TransactionOut,d=function(){vara=[];this.addressHashes=[],this.txIndex={},this.unspentOuts=[],this.addressPointer=0,this.addKey=function(b,c){binstanceofBitcoin.ECKey||(b=newBitcoin.ECKey(b)),a.push(b),c&&("string"==typeofc&&(c=Crypto.util.base64ToBytes(c)),b.pub=c),this.addressHashes.push(b.getBitcoinAddress().getHashBase64())},this.addKeys=function(a,b){"string"==typeofa&&(a=a.split(",")),"string"==typeofb&&(b=b.split(","));varc;if(Array.isArray(b)&&a.length==b.length)for(c=0;c<a.length;c++)this.addKey(a[c],b[c]);elsefor(c=0;c<a.length;c++)this.addKey(a[c])},this.getKeys=function(){varb=[];for(varc=0;c<a.length;c++)b.push(a[c].toString("base64"));returnb},this.getPubKeys=function(){varb=[];for(varc=0;c<a.length;c++)b.push(Crypto.util.bytesToBase64(a[c].getPub()));returnb},this.clear=function(){a=[]},this.getLength=function(){returna.length},this.getAllAddresses=function(){varb=[];for(varc=0;c<a.length;c++)b.push(a[c].getBitcoinAddress());returnb},this.getCurAddress=function(){returna[this.addressPointer]?a[this.addressPointer].getBitcoinAddress():null},this.getNextAddress=function(){returnthis.addressPointer++,a[this.addressPointer]||this.generateAddress(),a[this.addressPointer].getBitcoinAddress()},this.signWithKey=function(b,c){b=Crypto.util.bytesToBase64(b);for(vard=0;d<this.addressHashes.length;d++)if(this.addressHashes[d]==b)returna[d].sign(c);thrownewError("Missing key for signature")},this.getPubKeyFromHash=function(b){b=Crypto.util.bytesToBase64(b);for(varc=0;c<this.addressHashes.length;c++)if(this.addressHashes[c]==b)returna[c].getPub();thrownewError("Hash unknown")}};returnd.prototype.generateAddress=function(){this.addKey(newBitcoin.ECKey)},d.prototype.process=function(a){if(this.txIndex[a.hash])return;vard,e,f;for(d=0;d<a.outs.length;d++){varg=newc(a.outs[d]);f=Crypto.util.bytesToBase64(g.script.simpleOutPubKeyHash());for(e=0;e<this.addressHashes.length;e++)if(this.addressHashes[e]===f){this.unspentOuts.push({tx:a,index:d,out:g});break}}for(d=0;d<a.ins.length;d++){varh=newb(a.ins[d]),i=h.script.simpleInPubKey();f=Crypto.util.bytesToBase64(Bitcoin.Util.sha256ripe160(i));for(e=0;e<this.addressHashes.length;e++)if(this.addressHashes[e]===f){for(varj=0;j<this.unspentOuts.length;j++)h.outpoint.hash==this.unspentOuts[j].tx.hash&&h.outpoint.index==this.unspentOuts[j].index&&this.unspentOuts.splice(j,1);break}}this.txIndex[a.hash]=a},d.prototype.getBalance=function(){vara=BigInteger.valueOf(0);for(varb=0;b<this.unspentOuts.length;b++){varc=this.unspentOuts[b].out;a=a.add(Bitcoin.Util.valueToBigInt(c.value))}returna},d.prototype.createSend=function(b,c,d){vare=[],f=c.add(d),g=BigInteger.ZERO,h;for(h=0;h<this.unspentOuts.length;h++){e.push(this.unspentOuts[h]),g=g.add(Bitcoin.Util.valueToBigInt(this.unspentOuts[h].out.value));if(g.compareTo(f)>=0)break}if(g.compareTo(f)<0)thrownewError("Insufficient funds.");vari=g.subtract(f),j=newBitcoin.Transaction;for(h=0;h<e.length;h++)j.addInput(e[h].tx,e[h].index);j.addOutput(b,c),i.compareTo(BigInteger.ZERO)>0&&j.addOutput(this.getNextAddress(),i);vark=1;for(h=0;h<j.ins.length;h++){varl=j.hashTransactionForSignature(e[h].out.script,h,k),m=e[h].out.script.simpleOutPubKeyHash(),n=this.signWithKey(m,l);n.push(parseInt(k,10)),j.ins[h].script=a.createInputScript(n,this.getPubKeyFromHash(m))}returnj},d.prototype.clearTransactions=function(){this.txIndex={},this.unspentOuts=[]},d.prototype.hasHash=function(a){Bitcoin.Util.isArray(a)&&(a=Crypto.util.bytesToBase64(a));for(varb=0;b<this.addressHashes.length;b++)if(this.addressHashes[b]===a)return!0;return!1},d}();