-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
1 lines (1 loc) · 12.9 KB
/
index.js
1
module.exports=function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=3)}([function(t,e){t.exports=require("bitcoin-core")},function(t,e){t.exports=require("safe-buffer")},function(t,e){t.exports=require("base58check")},function(t,e,n){t.exports={bitcoin:n(4),bitcoincash:n(8)}},function(t,e,n){const r=n(5),i=n(0),{Buffer:s}=(n(6),n(1)),o=n(2),u=n(7);class c{constructor(t){this.network="mainnet"===t?r.networks.bitcoin:r.networks.testnet}static coinSelect(t,e,n){if(t=t.concat().sort((t,e)=>t.value-e.value),!isFinite(u.uintOrNaN(n)))return{};let r=0,i=[],s=u.sumOrNaN(e);for(var o=0;o<t.length;++o){let c=t[o];if(r+=u.uintOrNaN(c.value),i.push(c),!(r<s+n))return{inputs:i,outputs:e,changeValue:r-(s+n)}}return{fee:n}}generateAccount(){const t=new r.ECPair.makeRandom({network:this.network});return{privateKey:t.toWIF(),publicKey:t.Q.getEncoded().toString("hex"),address:t.getAddress()}}createMultisigAddress(t,e){const n=e.map(t=>s.from(t,"hex")),i=r.script.multisig.output.encode(t,n,this.network),o=r.script.scriptHash.output.encode(r.crypto.hash160(i),this.network);return{address:r.address.fromOutputScript(o,this.network),accountExtrsInfo:{redeemScript:i}}}isValidAddress(t){try{const e=o.decode(t).prefix.toString("hex"),n="6f"===e||"c4"===e?r.networks.testnet:r.networks.bitcoin;return this.network===n}catch(t){return!1}}signTransaction(t,e,n){const i=e.privateKey,o=r.TransactionBuilder.fromTransaction(r.Transaction.fromHex(t.txhex),this.network),u=r.ECPair.fromWIF(i,this.network);for(let e=0;e<t.input.length;e++){const r=s.from(n[t.input[e]],"hex");o.sign(e,u,r)}const c=o.inputs.map(t=>t.signatures),a=[];for(let t=0;t<c.length;t++){let e={},n=c[t].filter(t=>Boolean(t))[0];e[String(c[t].indexOf(n))]=n.toString("hex"),a.push(e)}return a}buildSignatures(t){let e=[];for(let n=0;n<t.length;n++)for(let r=0;r<t[0].length;r++){e[r]=e[r]||[];let i=Number(Object.keys(t[n][r])[0]);e[r][i]=t[n][r][i]}return e}buildTransaction(t,e,n){let i=this.buildSignatures(e),o=[];for(let u=0;u<e[0].length;u++){let e=t.input[u];o.push({redeemScript:s.from(n[e],"hex"),redeemScriptType:"multisig",pubKeys:r.script.multisig.output.decode(s.from(n[e],"hex")).pubKeys,signatures:i[u].map(t=>s.from(t,"hex")),signScript:s.from(n[t.input[u]],"hex"),signType:"multisig",prevOutScript:r.crypto.hash160(s.from(n[e],"hex")),prevOutType:"scripthash",witness:!1})}const u=r.TransactionBuilder.fromTransaction(r.Transaction.fromHex(t.txhex),this.network);return u.inputs=o,u.build().toHex()}getSpentTidsFromRawTransaction(t){const e=r.Transaction.fromHex(t);let n=[];return e.ins.forEach((t,e)=>{let r={txid:t.hash.reverse().toString("hex")};n.push(r.txid)}),n}}t.exports={Utils:c,Client:class{constructor(t,e,n,s,o){this.network="mainnet"===n?r.networks.bitcoin:r.networks.testnet,this.client=new i({username:t,password:e,network:n,port:s,host:o})}async getAddress(t,e){const n=await this.client.getRawTransaction(t);return(await this.client.decodeRawTransaction(n)).vout[e].scriptPubKey.addresses[0]}importPrivKeyToWallet(t,e){this.client.importPrivKey(t.privateKey,"",!1,e)}getUTXOs(t,e){this.client.listUnspent(0,(n,r)=>{if(n)return e(n);const i=r.sort((t,e)=>t.amount-e.amount).filter(e=>t.indexOf(e.txid)<0).filter(t=>""===t.account).map(t=>({txid:t.txid,vout:t.vout,value:Math.round(1e8*t.amount)}));e(null,i)})}createNewTransaction(t,e,n,i,s){i=i||1e4;let o={};this.getUTXOs(n,(n,u)=>{if(n)return s(n);let{inputs:a,outputs:l,changeValue:d}=c.coinSelect(u,e,i);if(!a||!l)return s("No inputs or outputs");let h=new r.TransactionBuilder(this.network);try{a.forEach(t=>h.addInput(t.txid,t.vout)),l.forEach(t=>h.addOutput(t.address,t.value)),h.addOutput(t.address,d)}catch(t){return s(t.message)}o.txhex=h.buildIncomplete().toHex();let p=[];for(let t=0;t<a.length;t++)p.push(this.getAddress(a[t].txid,a[t].vout));Promise.all(p).then(t=>{o.txhex=h.buildIncomplete().toHex(),o.input=t,s(null,o)})})}sendRawTransaction(t,e){this.client.sendRawTransaction(t,e)}getTransactionStatus(t,e){this.client.getTransaction(t,e)}getTransactionHistory(t,e){this.client.listTransactions((n,r)=>{if(n)return e(n);const i=r.filter(e=>e.address===t);e(null,i)})}importAddress(t,e){this.client.importAddress(t,"",!1,e)}listSinceBlock(t,e){this.client.listSinceBlock(t,1,!0,e)}getTransactionsFromBlockHeight(t,e){this.client.getBlockchainInfo((n,r)=>{if(n)return e(n);this.client.getBlockHash(t,(t,n)=>{if(t)return e(t);this.client.listSinceBlock(n,1,!0,(t,n)=>{if(t)return e(t);const i=r.blocks;n.transactions.map(t=>{t.height=i-t.confirmations+1}),e(null,n)})})})}getCurrentHeight(t){this.client.getBlockchainInfo((e,n)=>{if(e)return t(e);t(null,n.blocks)})}}}},function(t,e){t.exports=require("bitcoinjs-lib")},function(t,e){t.exports=require("coinselect")},function(t,e){var n=10,r=41,i=107,s=9,o=25;function u(t){return r+(t.script?t.script.length:i)}function c(t){return s+(t.script?t.script.length:o)}function a(t,e){return u({})*e}function l(t,e){return n+t.reduce(function(t,e){return t+u(e)},0)+e.reduce(function(t,e){return t+c(e)},0)}function d(t){return"number"!=typeof t?NaN:isFinite(t)?Math.floor(t)!==t?NaN:t<0?NaN:t:NaN}function h(t){return t.reduce(function(t,e){return t+d(e.value)},0)}var p=c({});t.exports={dustThreshold:a,finalize:function(t,e,n){var r=l(t,e),i=n*(r+p),s=h(t)-(h(e)+i);s>a(0,n)&&(e=e.concat({value:s}));var o=h(t)-h(e);return isFinite(o)?{inputs:t,outputs:e,fee:o}:{fee:n*r}},inputBytes:u,outputBytes:c,sumOrNaN:h,sumForgiving:function(t){return t.reduce(function(t,e){return t+(isFinite(e.value)?e.value:0)},0)},transactionBytes:l,uintOrNaN:d}},function(t,e,n){const r=n(9),i=n(0),{Buffer:s}=n(1),o=n(2),u=n(10),c=n(11);class a{constructor(t){this.network="mainnet"===t?r.networks.bitcoin:r.networks.testnet}static toCashAddrFormat(t){const e=c.Address,n=e.CashAddrFormat;return new e(t).toString(n)}static toNormalAddrFormat(t){const e=c.Address,n=e.fromString,r=e.CashAddrFormat;let i;return console.log("addr",t),(i=t.split(":")[1].startsWith("p")?n(t,"testnet","scripthash",r):n(t,"testnet","pubkeyhash",r)).toString()}static coinSelect(t,e,n){if(t=t.concat().sort((t,e)=>t.value-e.value),!isFinite(u.uintOrNaN(n)))return{};let r=0;const i=[],s=u.sumOrNaN(e);if(u.sumOrNaN(t)<s)throw new Error("Not enough coins");for(let o=0;o<t.length;++o){const c=t[o];if(r+=u.uintOrNaN(c.value),i.push(c),!(r<s+n))return{inputs:i,outputs:e,changeValue:r-(s+n)}}}generateAccount(){const t=new bitcoin.ECPair.makeRandom({network:this.network});return{privateKey:t.toWIF(),publicKey:t.Q.getEncoded().toString("hex"),address:t.getAddress()}}createMultisigAddress(t,e){const n=e.map(t=>s.from(t,"hex")),i=r.script.multisig.output.encode(t,n,this.network),o=r.script.scriptHash.output.encode(r.crypto.hash160(i),this.network);return{address:r.address.fromOutputScript(o,this.network),accountExtrsInfo:{redeemScript:i}}}isValidAddress(t){try{const e=o.decode(t).prefix.toString("hex"),n=["6f","c4","ef","043587cf","04358394"].includes(e)?r.networks.testnet:r.networks.bitcoin;return this.network===n}catch(t){return!1}}static buildNewTransaction(t,e){const n=new r.TransactionBuilder(r.networks.testnet);for(let e=0;e<t.length;e++){const r=t[e];n.addInput(r.txid,r.vout)}for(let t=0;t<e.length;t++){const r=e[t];n.addOutput(r.address,r.amount)}return n.setVersion(2),n.buildIncomplete().toHex()}signTransactionWithUTXO(t,e,n,i){const o=r.Transaction.fromHex(t.txhex,this.network),u=r.TransactionBuilder.fromTransaction(o,this.network),c=r.ECPair.fromWIF(e.privateKey,this.network),a=r.Transaction.SIGHASH_ALL|r.Transaction.SIGHASH_BITCOINCASHBIP143;u.enableBitcoinCash(!0),u.setVersion(2);for(let e=0;e<t.input.length;e++){const r=s.from(n[t.input[e]],"hex"),o=s.from(u.tx.ins[e].hash,"hex").reverse().toString("hex"),l=u.tx.ins[e].index,d=i[e][o][String(l)];u.sign(e,c,r,a,Number(d))}const l=u.inputs.map(t=>t.signatures),d=[];for(let t=0;t<l.length;t++){const e={},n=l[t].filter(t=>Boolean(t))[0];e[String(l[t].indexOf(n))]=n.toString("hex"),d.push(e)}return d}buildTransaction(t,e,n){this.network;const i=r.Transaction.fromHex(t.txhex),o=r.TransactionBuilder.fromTransaction(i);for(let e=0;e<o.inputs.length;e++){const i=t.input[e];o.inputs[e]={redeemScript:s.from(n[i],"hex"),redeemScriptType:"multisig",pubKeys:r.script.multisig.output.decode(s.from(n[i],"hex")).pubKeys,signatures:new Array(r.script.multisig.output.decode(s.from(n[i],"hex")).pubKeys.length),signScript:s.from(n[t.input[e]],"hex"),signType:"multisig",prevOutScript:r.crypto.hash160(s.from(n[i],"hex")),prevOutType:"scripthash",witness:!1}}for(let t=0;t<e.length;t++)for(let n=0;n<e[t].length;n++){const r=e[t][n],i=Object.keys(r)[0],u=Object.values(r)[0];o.inputs[n].signatures[i]=s.from(u,"hex")}return o.build().toHex()}getSpentTidsFromRawTransaction(t){this.network;const e=r.Transaction.fromHex(t),n=[];return e.ins.forEach(t=>{const e={txid:t.hash.reverse().toString("hex")};n.push(e.txid)}),n}}t.exports={Utils:a,Client:class{constructor(t,e,n,s,o){this.network="mainnet"===n?r.networks.bitcoin:r.networks.testnet,this.client=new i({username:t,password:e,network:n,port:s,host:o})}async getAddress(t,e){const n=await this.client.getRawTransaction(t);return(await this.client.decodeRawTransaction(n)).vout[e].scriptPubKey.addresses[0]}importPrivKeyToWallet(t,e){this.client.importPrivKey(t.privateKey,"asch-test",!1,e)}async getUTXOsByTransaction(t){const e=r.Transaction.fromHex(t),n=[];for(let t=0;t<e.ins.length;t++){const r={},i=e.ins[t].hash.reverse().toString("hex"),s=await this.client.getRawTransaction(i),o=(await this.client.decodeRawTransaction(s)).vout.map(t=>({value:1e8*t.value,n:t.n})).reduce((t,e)=>(t[e.n]=e.value,t),{});r[i]=o,n.push(r)}return n}getUTXOs(t,e){this.client.listUnspent(0,(n,r)=>{if(n)return e(n);const i=r.sort((t,e)=>t.amount-e.amount).filter(e=>t.indexOf(e.txid)<0).filter(t=>"asch-test"===t.account).map(t=>({txid:t.txid,vout:t.vout,value:Math.round(1e8*t.amount)}));e(null,i)})}createNewTransaction(t,e,n,i,s){i=i||1e4;const o={};this.getUTXOs(n,(n,u)=>{if(n)return s(n);const{inputs:c,outputs:l,changeValue:d}=a.coinSelect(u,e,i);if(console.log(c,l,d),!c||!l)return s("No inputs or outputs");const h=new r.TransactionBuilder(this.network);h.setVersion(2);try{c.forEach(t=>h.addInput(t.txid,t.vout)),l.forEach(t=>h.addOutput(t.address,t.value)),h.addOutput(t.address,d)}catch(t){return s(t.message)}o.txhex=h.buildIncomplete().toHex();const p=[];for(let t=0;t<c.length;t++)p.push(this.getAddress(c[t].txid,c[t].vout));Promise.all(p).then(t=>{o.txhex=h.buildIncomplete().toHex(),o.input=t.map(t=>a.toNormalAddrFormat(t)),s(null,o)})})}sendRawTransaction(t,e){this.client.sendRawTransaction(t,e)}getTransactionStatus(t,e){this.client.getTransaction(t,e)}getTransactionHistory(t,e){this.client.listTransactions((n,r)=>{if(n)return e(n);const i=r.filter(e=>e.address===a.toCashAddrFormat(t));e(null,i)})}importAddress(t,e){this.client.importAddress(t,"asch-test",!1,e)}listSinceBlock(t,e){this.client.listSinceBlock(t,1,!0,e)}getTransactionsFromBlockHeight(t,e){this.client.getBlockchainInfo((n,r)=>{if(n)return e(n);this.client.getBlockHash(t,(t,n)=>{if(t)return e(t);this.client.listSinceBlock(n,1,!0,(t,n)=>{if(t)return e(t);const i=r.blocks;n.transactions.map(t=>{t.height=i-t.confirmations+1}),n.transactions.map(t=>{t.address=a.toNormalAddrFormat(t.address)}),e(null,n)})})})}getCurrentHeight(t){this.client.getBlockchainInfo((e,n)=>{if(e)return t(e);t(null,n.blocks)})}}}},function(t,e){t.exports=require("bitgo-bitcoinjs-lib")},function(t,e){var n=10,r=41,i=107,s=9,o=25;function u(t){return r+(t.script?t.script.length:i)}function c(t){return s+(t.script?t.script.length:o)}function a(t,e){return u({})*e}function l(t,e){return n+t.reduce(function(t,e){return t+u(e)},0)+e.reduce(function(t,e){return t+c(e)},0)}function d(t){return"number"!=typeof t?NaN:isFinite(t)?Math.floor(t)!==t?NaN:t<0?NaN:t:NaN}function h(t){return t.reduce(function(t,e){return t+d(e.value)},0)}var p=c({});t.exports={dustThreshold:a,finalize:function(t,e,n){var r=l(t,e),i=n*(r+p),s=h(t)-(h(e)+i);s>a(0,n)&&(e=e.concat({value:s}));var o=h(t)-h(e);return isFinite(o)?{inputs:t,outputs:e,fee:o}:{fee:n*r}},inputBytes:u,outputBytes:c,sumOrNaN:h,sumForgiving:function(t){return t.reduce(function(t,e){return t+(isFinite(e.value)?e.value:0)},0)},transactionBytes:l,uintOrNaN:d}},function(t,e){t.exports=require("bitcore-lib-cash")}]);