streamium
Version:
Decentralized trustless video streaming using bitcoin payment channels.
1 lines • 9.27 kB
JavaScript
require=function t(e,i,n){function r(o,u){if(!i[o]){if(!e[o]){var c="function"==typeof require&&require;if(!u&&c)return c(o,!0);if(s)return s(o,!0);var a=new Error("Cannot find module '"+o+"'");throw a.code="MODULE_NOT_FOUND",a}var p=i[o]={exports:{}};e[o][0].call(p.exports,function(t){var i=e[o][1][t];return r(i?i:t)},p,p.exports,t,e,i,n)}return i[o].exports}for(var s="function"==typeof require&&require,o=0;o<n.length;o++)r(n[o]);return r}({1:[function(t,e,i){"use strict";function n(t){t=t||{},this.network=o.get(t.network||"livenet"),this.expires=t.expires||Math.round((new Date).getTime()/1e3)+f,this.commitmentKey=new u(t.commitmentKey),this.providerPublicKey=new c(t.providerPublicKey),this.providerAddress=t.providerAddress?new a(t.providerAddress):this.providerPublicKey.toAddress(),r.checkArgument(t.fundingKey instanceof u,"fundingKey is expected to be a PrivateKey"),this.fundingKey=t.fundingKey,this.fundingAddress=this.fundingKey.toAddress(),t.refundAddress instanceof a?this.refundAddress=t.refundAddress:this.refundAddress=new a(t.refundAddress),this.commitmentTx=new d({publicKeys:[this.commitmentKey.publicKey,this.providerPublicKey],network:this.network})}var r=t("bitcore-lib").util.preconditions,s=t("bitcore-lib").Script,o=t("bitcore-lib").Networks,u=t("bitcore-lib").PrivateKey,c=t("bitcore-lib").PublicKey,a=t("bitcore-lib").Address,p=t("bitcore-lib").deps._,d=t("./transactions/commitment"),h=t("./transactions/payment"),m=t("./transactions/refund"),l=24,b=60,y=60,f=y*b*l;n.prototype.processFunding=function(t){r.checkArgument(p.isObject(t),"Can only process an array of objects or an object"),this.commitmentTx.from(t)},n.prototype.setupRefund=function(t){this.commitmentTx.sign(this.fundingKey),r.checkState(this.commitmentTx.isFullySigned());var e=this.commitmentTx.amount-this.commitmentTx.getFee(),i={txid:this.commitmentTx.id,outputIndex:0,satoshis:e,script:this.commitmentTx.outputs[0].script};return this.refundTx=(new m).from(i,this.commitmentTx.publicKeys,2),this.relayOutputAmount=0,t?(this.relayOutputAmount=t.amount,this.relayOutput=t,this.relayOutput.address=new a(t.address),this.refundTx.to(this.refundAddress,this.refundTx.inputAmount-t.amount),this.refundTx.to(t.address,t.amount)):this.refundTx.to(this.refundAddress,this.refundTx.inputAmount),this.refundTx.inputs[0].sequenceNumber=0,this.refundTx.nLockTime=this.expires,this.refundTx},n.prototype.validateRefund=function(t){t=new m(t),t.sign(this.commitmentKey);var e=new a(t.outputs[0].script,this.network).toString();r.checkState(e===this.refundAddress.toString());var i=t.outputs[0].satoshis;r.checkState(i===this.commitmentTx.amount-this.commitmentTx.getFee()-this.relayOutputAmount,"Refund amount must equal commitmentTx amount"),this.relayOutput&&(r.checkState(new a(t.outputs[1].script,this.network).toString()===this.relayOutput.address.toString(),"Invalid relay output address"),r.checkState(t.outputs[1].satoshis===this.relayOutputAmount,"Refund amount must equal commitmentTx amount")),r.checkState(t.outputs.length===1+(this.relayOutput?1:0),"More than expected outputs received"),r.checkState(t.isFullySigned(),"Refund was not fully signed"),r.checkState(s.Interpreter().verify(t.inputs[0].script,t.inputs[0].output.script,t,0,s.Interpreter.SCRIPT_VERIFY_P2SH|s.Interpreter.SCRIPT_VERIFY_STRICTENC|s.Interpreter.SCRIPT_VERIFY_MINIMALDATA|s.Interpreter.SCRIPT_VERIFY_SIGPUSHONLY),"Refund is incorrectly signed"),this.refundTx=t;var n={txid:this.commitmentTx.hash,outputIndex:0,satoshis:i,script:this.commitmentTx.outputs[0].script};return this.paymentTx=new h({multisigOut:n,amount:i,paymentAddress:this.providerAddress,changeAddress:this.refundAddress,publicKeys:this.commitmentTx.publicKeys,network:this.network}),this.paymentTx.sign(this.commitmentKey),!0},n.prototype.incrementPaymentBy=function(t){return this.paymentTx.updateValue(t),this.paymentTx.sign(this.commitmentKey),this.paymentTx.toObject()},n.prototype.getPaymentTx=function(){return this.paymentTx},e.exports=n},{"./transactions/commitment":4,"./transactions/payment":5,"./transactions/refund":6,"bitcore-lib":"bitcore-lib"}],2:[function(t,e,i){"use strict";var n={name:"Channel",message:"Internal Error on bitcore-channels Module {0}"};e.exports=t("bitcore-lib").errors.extend(n)},{"bitcore-lib":"bitcore-lib"}],3:[function(t,e,i){"use strict";function n(t){this.network=p.get(t.network)||p.defaultNetwork,t.paymentAddress?this.paymentAddress=new a(t.paymentAddress):(this.paymentKey=new u,this.paymentAddress=this.paymentKey.toAddress(this.network)),this.currentAmount=t.currentAmount||0,this.key=t.key||new u}var r=t("./transactions/payment"),s=t("./transactions/refund"),o=t("bitcore-lib").util.preconditions,u=t("bitcore-lib").PrivateKey,c=t("bitcore-lib").Script,a=t("bitcore-lib").Address,p=t("bitcore-lib").Networks,d=t("bitcore-lib").deps._;n.prototype.getPublicKey=function(){return this.key.publicKey},n.prototype.signRefund=function(t){var e=new s(t);return e.sign(this.key),this.refund=e,e},n.prototype.validPayment=function(t){var e,i=new r(t),n=this;return i.sign(this.key),i.outputs.map(function(t){t.script.toAddress(n.network).toString()===n.paymentAddress.toString()&&(e=t.satoshis)}),o.checkState(!d.isUndefined(e),"No output found corresponding to paymentAddress"),o.checkState(c.Interpreter().verify(i.inputs[0].script,i.inputs[0].output.script,i,0,c.Interpreter.SCRIPT_VERIFY_P2SH|c.Interpreter.SCRIPT_VERIFY_STRICTENC|c.Interpreter.SCRIPT_VERIFY_MINIMALDATA|c.Interpreter.SCRIPT_VERIFY_SIGPUSHONLY),"Script did not evaluate correctly (probably a bad signature received)"),o.checkState(e>this.currentAmount,"A payment for a greater amount was already received"),this.paymentTx=i,this.currentAmount=e,i},n.prototype.getPaymentTx=function(){return this.paymentTx.build()},e.exports=n},{"./transactions/payment":5,"./transactions/refund":6,"bitcore-lib":"bitcore-lib"}],4:[function(t,e,i){"use strict";function n(t){s.checkArgument(t.publicKeys&&2===t.publicKeys.length,"Must provide exactly two public keys"),u.call(this,t.transaction),this.network=t.network||"livenet",this.publicKeys=t.publicKeys,this.outscript=o.buildMultisigOut(this.publicKeys,2),this.address=this.outscript.toScriptHashOut().toAddress(),this.outputs.length||this.change(this.address),Object.defineProperty(this,"amount",{configurable:!1,get:function(){return this.inputAmount}})}var r=t("inherits"),s=t("bitcore-lib").util.preconditions,o=t("bitcore-lib").Script,u=t("bitcore-lib").Transaction,c=t("bitcore-lib").deps._;r(n,u),n.prototype.toJSON=function(){return JSON.stringify(this.toObject())},n.prototype.toObject=function(){var t=u.prototype.toObject.apply(this);return{transaction:t,publicKeys:c.map(this.publicKeys,function(t){return t.toString()}),network:this.network.toString()}},n.prototype.getAddress=function(){return this.address},e.exports=n},{"bitcore-lib":"bitcore-lib",inherits:7}],5:[function(t,e,i){"use strict";function n(t){return this instanceof n?(o.call(this,t.transaction),this.paymentAddress=new s(t.paymentAddress),this.changeAddress=new s(t.changeAddress),this.outputs.length||this.change(this.changeAddress),this.multisigOut=new o.UnspentOutput(t.multisigOut),this.publicKeys=a.map(t.publicKeys,u),this.inputs.length||this.from(this.multisigOut,this.publicKeys,2),this.amount=this.outputAmount,this.sequence=t.sequence||0,this.paid=t.paid||0,void c.checkArgument(a.isNumber(this.amount),"Amount must be a number")):new n(t)}var r=t("inherits"),s=t("bitcore-lib").Address,o=t("bitcore-lib").Transaction,u=t("bitcore-lib").Publickey,c=t("bitcore-lib").util.preconditions,a=t("bitcore-lib").deps._;r(n,o),n.prototype._updateTransaction=function(){this.clearOutputs(),this.to(this.paymentAddress,this.paid),this.inputs[0].sequence=this.sequence},n.prototype.updateValue=function(t){return this.paid+=t,this.sequence+=1,this._updateTransaction(),this},n.prototype.toJSON=function(){return JSON.stringify(this.toObject())},n.prototype.toObject=function(){return{publicKeys:a.map(this.publicKeys,function(t){return t.toString()}),multisigOut:this.multisigOut.toObject(),amount:this.amount,sequence:this.sequence,paymentAddress:this.paymentAddress.toString(),changeAddress:this.changeAddress.toString(),transaction:o.prototype.toObject.apply(this)}},n.fromObject=function(t){return new n(t)},e.exports=n},{"bitcore-lib":"bitcore-lib",inherits:7}],6:[function(t,e,i){"use strict";function n(){s.apply(this,arguments)}var r=t("inherits"),s=t("bitcore-lib").Transaction;r(n,s),e.exports=n},{"bitcore-lib":"bitcore-lib",inherits:7}],7:[function(t,e,i){"function"==typeof Object.create?e.exports=function(t,e){t.super_=e,t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})}:e.exports=function(t,e){t.super_=e;var i=function(){};i.prototype=e.prototype,t.prototype=new i,t.prototype.constructor=t}},{}],"bitcore-channel":[function(t,e,i){t("./lib/errors"),e.exports={Consumer:t("./lib/consumer"),Provider:t("./lib/provider"),Transactions:{Commitment:t("./lib/transactions/commitment"),Refund:t("./lib/transactions/refund"),Payment:t("./lib/transactions/payment")}}},{"./lib/consumer":1,"./lib/errors":2,"./lib/provider":3,"./lib/transactions/commitment":4,"./lib/transactions/payment":5,"./lib/transactions/refund":6}]},{},[]);