UNPKG

mnee

Version:

A simple package for interacting with the MNEE USD

3 lines (2 loc) 26.2 kB
import{Utils as r,LockingScript as e,OP as t,TransactionSignature as n,Hash as o,UnlockingScript as i,Transaction as s,PublicKey as u,Script as a,P2PKH as c,PrivateKey as f}from"@bsv/sdk";function d(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}function l(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return d(r,e);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?d(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h=/*#__PURE__*/function(){function s(){}var u=s.prototype;return u.lock=function(n,o){var i=[];if("string"==typeof n){var s=r.fromBase58Check(n);if(0!==s.prefix[0]&&111!==s.prefix[0])throw new Error("only P2PKH is supported");i=s.data}else i=n;var u=new e;return u.writeOpCode(t.OP_DUP).writeOpCode(t.OP_HASH160).writeBin(i).writeOpCode(t.OP_EQUALVERIFY).writeOpCode(t.OP_CHECKSIGVERIFY).writeBin(o.encode(!0)).writeOpCode(t.OP_CHECKSIG),u},u.userUnlock=function(r,e,t,s,u){return void 0===e&&(e="all"),void 0===t&&(t=!1),{sign:function(a,c){try{var f,d,l,h=n.SIGHASH_FORKID;"all"===e&&(h|=n.SIGHASH_ALL),"none"===e&&(h|=n.SIGHASH_NONE),"single"===e&&(h|=n.SIGHASH_SINGLE),t&&(h|=n.SIGHASH_ANYONECANPAY);var v=a.inputs[c],m=a.inputs.filter(function(r,e){return e!==c}),p=v.sourceTXID?v.sourceTXID:null==(f=v.sourceTransaction)?void 0:f.id("hex");if(!p)throw new Error("The input sourceTXID or sourceTransaction is required for transaction signing.");if(s||(s=null==(d=v.sourceTransaction)?void 0:d.outputs[v.sourceOutputIndex].satoshis),!s)throw new Error("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(u||(u=null==(l=v.sourceTransaction)?void 0:l.outputs[v.sourceOutputIndex].lockingScript),!u)throw new Error("The lockingScript or input sourceTransaction is required for transaction signing.");var g=n.format({sourceTXID:p,sourceOutputIndex:v.sourceOutputIndex,sourceSatoshis:s,transactionVersion:a.version,otherInputs:m,inputIndex:c,outputs:a.outputs,inputSequence:v.sequence||4294967295,subscript:u,lockTime:a.lockTime,scope:h}),y=r.sign(o.sha256(g)),P=new n(y.r,y.s,h),w=new i;return w.writeBin(P.toChecksigFormat()),w.writeBin(r.toPublicKey().encode(!0)),Promise.resolve(w)}catch(r){return Promise.reject(r)}},estimateLength:function(){return Promise.resolve(182)}}},u.unlock=function(r,e,t,s,u,a){return void 0===t&&(t="all"),void 0===s&&(s=!1),{sign:function(c,f){try{var d,l,h,v=n.SIGHASH_FORKID;"all"===t&&(v|=n.SIGHASH_ALL),"none"===t&&(v|=n.SIGHASH_NONE),"single"===t&&(v|=n.SIGHASH_SINGLE),s&&(v|=n.SIGHASH_ANYONECANPAY);var m=c.inputs[f],p=c.inputs.filter(function(r,e){return e!==f}),g=m.sourceTXID?m.sourceTXID:null==(d=m.sourceTransaction)?void 0:d.id("hex");if(!g)throw new Error("The input sourceTXID or sourceTransaction is required for transaction signing.");if(u||(u=null==(l=m.sourceTransaction)?void 0:l.outputs[m.sourceOutputIndex].satoshis),!u)throw new Error("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(a||(a=null==(h=m.sourceTransaction)?void 0:h.outputs[m.sourceOutputIndex].lockingScript),!a)throw new Error("The lockingScript or input sourceTransaction is required for transaction signing.");var y=n.format({sourceTXID:g,sourceOutputIndex:m.sourceOutputIndex,sourceSatoshis:u,transactionVersion:c.version,otherInputs:p,inputIndex:f,outputs:c.outputs,inputSequence:m.sequence||4294967295,subscript:a,lockTime:c.lockTime,scope:v}),P=r.sign(o.sha256(y)),w=new n(P.r,P.s,v),x=new i;return x.writeBin(w.toChecksigFormat()),x.writeScript(e),Promise.resolve(x)}catch(r){return Promise.reject(r)}},estimateLength:function(){return Promise.resolve(182)}}},s}(),v=function(r){return Buffer.from(r).toString("hex")},m=function(r,t,n,o){void 0===o&&(o=!1);var i="";if(void 0!==(null==t?void 0:t.dataB64)&&void 0!==(null==t?void 0:t.contentType)){var s=v("ord"),u=Buffer.from(t.dataB64,"base64").toString("hex").trim();if(!u)throw new Error("Invalid file data");var a=v(t.contentType);if(!a)throw new Error("Invalid media type");i="OP_0 OP_IF "+s+" OP_1 "+a+" OP_0 "+u+" OP_ENDIF"}var c=(i?i+" "+(o?"OP_CODESEPARATOR ":""):"")+r.toASM();if(n&&(!n.app||!n.type))throw new Error("MAP.app and MAP.type are required fields");if(null!=n&&n.app&&null!=n&&n.type){c=(c?c+" ":"")+"OP_RETURN "+v("1PuQa7K62MiKCtssSLKy1kh56WWU7MtUR5")+" "+v("SET");for(var f=0,d=Object.entries(n);f<d.length;f++){var l=d[f],h=l[0],m=l[1];"cmd"!==h&&(c=c+" "+v(h)+" "+v(m))}}return e.fromASM(c)},p=function(e){for(var n,i,s,u=0;u<e.chunks.length;u++){var a,c=e.chunks[u];u>=2&&3===(null==(a=c.data)?void 0:a.length)&&"ord"==r.toUTF8(c.data)&&e.chunks[u-1].op==t.OP_IF&&e.chunks[u-2].op==t.OP_FALSE&&(s=u+1)}if(void 0!==s){for(var f={file:{hash:"",size:0,type:""},fields:{}},d=s;d<e.chunks.length;d+=2){var l,h,v=e.chunks[d];if(v.op==t.OP_ENDIF)break;if(v.op>t.OP_16)return;var m=e.chunks[d+1];if(m.op>t.OP_PUSHDATA4)return;if(null==(l=v.data)||!l.length){var p=0;switch(v.op>t.OP_PUSHDATA4&&v.op<=t.OP_16?p=v.op-80:null!=(h=v.data)&&h.length&&(p=v.data[0]),p){case 0:if(f.file.size=(null==(n=m.data)?void 0:n.length)||0,null==(i=m.data)||!i.length)break;f.file.hash=r.toBase64(o.sha256(m.data)),f.file.content=m.data;break;case 1:f.file.type=Buffer.from(m.data||[]).toString()}}}return f}},g=function(e){return e.map(function(e){for(var n=e.chunks,o=0;o<=n.length-4;o++){var i,s,u;if(n.length>o+6&&n[0+o].op===t.OP_DUP&&n[1+o].op===t.OP_HASH160&&20===(null==(i=n[2+o].data)?void 0:i.length)&&n[3+o].op===t.OP_EQUALVERIFY&&n[4+o].op===t.OP_CHECKSIGVERIFY&&33===(null==(s=n[5+o].data)?void 0:s.length)&&n[6+o].op===t.OP_CHECKSIG)return{cosigner:r.toHex(n[5+o].data||[]),address:r.toBase58Check(n[2+o].data||[],[0])};if(n[0+o].op===t.OP_DUP&&n[1+o].op===t.OP_HASH160&&20===(null==(u=n[2+o].data)?void 0:u.length)&&n[3+o].op===t.OP_EQUALVERIFY&&n[4+o].op===t.OP_CHECKSIG)return{cosigner:"",address:r.toBase58Check(n[2+o].data||[],[0])}}})},y=function(e,t,n){var o=e.senders.includes(t)?"send":"receive",i=e.height>0?"confirmed":"unconfirmed";if(!e.rawtx)return null;var u=r.toArray(e.rawtx,"base64"),a=r.toHex(u),c=s.fromHex(a).outputs.map(function(r){return r.lockingScript}),f=g(c),d=c.map(p),l=f.map(function(r){return r.address}),h=l.indexOf(n.feeAddress),v=e.senders[0],m=0,y=new Map;d.forEach(function(e,o){var i,s=null==e||null==(i=e.file)?void 0:i.content;if(s){var u=r.toUTF8(s);if(u){var a;try{a=JSON.parse(u)}catch(r){return void console.error("Failed to parse inscription JSON:",r)}if("bsv-20"===a.p&&a.id===n.tokenId){var c=parseInt(a.amt,10);if(!Number.isNaN(c))if(h!==o||v!==t){var f=l[o],d=y.get(f)||0;y.set(f,d+c)}else m+=c}}}});var P=y.get(t)||0;if("send"===o){var w=y.get(v)||0;y.set(v,w-P)}var x=[],S=(x="receive"===o?[{address:v,amount:P}]:Array.from(y.entries()).map(function(r){return{address:r[0],amount:r[1]}}).filter(function(r){return r.address!==t&&r.address!==n.feeAddress&&r.amount>0})).reduce(function(r,e){return r+e.amount},0);return{txid:e.txid,height:e.height,type:o,status:i,amount:S,fee:m,score:e.score,counterparties:x}},P="ae59f3b898ec61acbdb6cc7a245fabeded0c094bf046f35206a3aec60ef88127_0",w="020a177d6a5e6f3a8689acd2e313bd1cf0dcf5a243d1cc67b7218602aee9e04b2f",x="1inHbiwj2jrEcZPiSYnfgJ8FmS1Bmk4Dh",S="833a7720966a2a435db28d967385e8aa7284b6150ebb39482cc5228b73e1703f_0",A="55cde0733049a226fdb6abc387ee9dcd036e859f7cbc69ab90050c0435139f00_0",b="833a7720966a2a435db28d967385e8aa7284b6150ebb39482cc5228b73e1703f_0";function T(r,e){try{var t=r()}catch(r){return e(r)}return t&&t.then?t.then(void 0,e):t}function I(r,e,t){if(!r.s){if(t instanceof E){if(!t.s)return void(t.o=I.bind(null,r,e));1&e&&(e=t.s),t=t.v}if(t&&t.then)return void t.then(I.bind(null,r,e),I.bind(null,r,2));r.s=e,r.v=t;var n=r.o;n&&n(r)}}var E=/*#__PURE__*/function(){function r(){}return r.prototype.then=function(e,t){var n=new r,o=this.s;if(o){var i=1&o?e:t;if(i){try{I(n,1,i(this.v))}catch(r){I(n,2,r)}return n}return this}return this.o=function(r){try{var o=r.v;1&r.s?I(n,1,e?e(o):o):t?I(n,1,t(o)):I(n,2,o)}catch(r){I(n,2,r)}},n},r}();function C(r){return r instanceof E&&1&r.s}var O="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function H(r,e,t){if("function"==typeof r[O]){var n,o,i,s=function(r){try{for(;!((n=u.next()).done||t&&t());)if((r=e(n.value))&&r.then){if(!C(r))return void r.then(s,i||(i=I.bind(null,o=new E,2)));r=r.v}o?I(o,1,r):o=r}catch(r){I(o||(o=new E),2,r)}},u=r[O]();if(s(),u.return){var a=function(r){try{n.done||u.return()}catch(r){}return r};if(o&&o.then)return o.then(a,function(r){throw a(r)});a()}return o}if(!("length"in r))throw new TypeError("Object is not iterable");for(var c=[],f=0;f<r.length;f++)c.push(r[f]);return function(r,e,t){var n,o,i=-1;return function s(u){try{for(;++i<r.length&&(!t||!t());)if((u=e(i))&&u.then){if(!C(u))return void u.then(s,o||(o=I.bind(null,n=new E,2)));u=u.v}n?I(n,1,u):n=u}catch(r){I(n||(n=new E),2,r)}}(),n}(c,function(r){return e(c[r])},t)}var k=/*#__PURE__*/function(){function e(r){if(this.mneeApiKey=void 0,this.mneeConfig=void 0,this.mneeApi=void 0,"production"!==r.environment&&"sandbox"!==r.environment)throw new Error('Invalid environment. Must be either "production" or "sandbox"');var e="production"===r.environment;this.mneeApiKey=null!=r&&r.apiKey?r.apiKey:e?"92982ec1c0975f31979da515d46bae9f":"54f1fd1688ba66a58a67675b82feb93e",this.mneeApi=e?"https://proxy-api.mnee.net":"https://sandbox-proxy-api.mnee.net",this.getCosignerConfig()}var t=e.prototype;return t.getCosignerConfig=function(){try{var r=this;return Promise.resolve(T(function(){return Promise.resolve(fetch(r.mneeApi+"/v1/config?auth_token="+r.mneeApiKey,{method:"GET"})).then(function(e){if(!e.ok)throw new Error("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){return r.mneeConfig=e,e})})},function(r){console.error("Failed to fetch config:",r)}))}catch(r){return Promise.reject(r)}},t.toAtomicAmount=function(r){if(!this.mneeConfig)throw new Error("Config not fetched");return Math.round(r*Math.pow(10,this.mneeConfig.decimals))},t.fromAtomicAmount=function(r){if(!this.mneeConfig)throw new Error("Config not fetched");return r/Math.pow(10,this.mneeConfig.decimals)},t.createInscription=function(r,e,t){try{var n={p:"bsv-20",op:"transfer",id:t.tokenId,amt:e.toString()};return Promise.resolve({lockingScript:m((new h).lock(r,u.fromString(t.approver)),{dataB64:Buffer.from(JSON.stringify(n)).toString("base64"),contentType:"application/bsv-20"}),satoshis:1})}catch(r){return Promise.reject(r)}},t.getUtxos=function(r,e){void 0===e&&(e=["transfer","deploy+mint"]);try{var t=this;return Promise.resolve(T(function(){var n=Array.isArray(r)?r:[r];return Promise.resolve(fetch(t.mneeApi+"/v1/utxos?auth_token="+t.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})).then(function(r){if(!r.ok)throw new Error("HTTP error! status: "+r.status);return Promise.resolve(r.json()).then(function(r){return e.length?r.filter(function(r){return e.includes(r.data.bsv21.op.toLowerCase())}):r})})},function(r){return console.error("Failed to fetch UTXOs:",r),[]}))}catch(r){return Promise.reject(r)}},t.fetchRawTx=function(r){try{return Promise.resolve(fetch(this.mneeApi+"/v1/tx/"+r+"?auth_token="+this.mneeApiKey)).then(function(e){if(404===e.status)throw new Error("Transaction not found");if(200!==e.status)throw new Error(e.status+" - Failed to fetch rawtx for txid: "+r);return Promise.resolve(e.json()).then(function(r){return s.fromHex(Buffer.from(r.rawtx,"base64").toString("hex"))})})}catch(r){return Promise.reject(r)}},t.getSignatures=function(e,t){try{try{var i;switch(e.format){case"beef":i=s.fromHexBEEF(e.rawtx);break;case"ef":i=s.fromHexEF(e.rawtx);break;default:i=s.fromHex(e.rawtx)}var u=e.sigRequests.flatMap(function(e){return[t].map(function(t){var s=n.format({sourceTXID:e.prevTxid,sourceOutputIndex:e.outputIndex,sourceSatoshis:e.satoshis,transactionVersion:i.version,otherInputs:i.inputs.filter(function(r,t){return t!==e.inputIndex}),inputIndex:e.inputIndex,outputs:i.outputs,inputSequence:i.inputs[e.inputIndex].sequence||0,subscript:e.script?a.fromHex(e.script):(new c).lock(t.toPublicKey().toAddress()),lockTime:i.lockTime,scope:e.sigHashType||65}),u=t.sign(o.sha256(s)),f=new n(u.r,u.s,e.sigHashType||65);return{sig:r.toHex(f.toChecksigFormat()),pubKey:t.toPublicKey().toString(),inputIndex:e.inputIndex,sigHashType:e.sigHashType||65,csIdx:e.csIdx}})});return Promise.resolve({sigResponses:u})}catch(r){var f;return console.error("getSignatures error",r),Promise.resolve({error:{message:null!=(f=r.message)?f:"unknown",cause:r.cause}})}}catch(r){return Promise.reject(r)}},t.transfer=function(e,t){try{var o=this;return Promise.resolve(T(function(){function u(u){if(!u)throw new Error("Config not fetched");var c=e.reduce(function(r,e){return r+e.amount},0);if(c<=0)return{error:"Invalid amount"};var d=o.toAtomicAmount(c),h=f.fromWif(t),v=h.toAddress();return Promise.resolve(o.getUtxos(v)).then(function(t){var c,f;function v(t){if(f)return t;function i(){function e(){function e(){var e=p.inputs.map(function(r,e){var t,o;if(!r.sourceTXID)throw new Error("Source TXID is undefined");return{prevTxid:r.sourceTXID,outputIndex:r.sourceOutputIndex,inputIndex:e,address:y[e],script:null==(t=r.sourceTransaction)?void 0:t.outputs[r.sourceOutputIndex].lockingScript.toHex(),satoshis:(null==(o=r.sourceTransaction)?void 0:o.outputs[r.sourceOutputIndex].satoshis)||1,sigHashType:n.SIGHASH_ALL|n.SIGHASH_ANYONECANPAY|n.SIGHASH_FORKID}}),t=p.toHex();return Promise.resolve(o.getSignatures({rawtx:t,sigRequests:e},h)).then(function(e){if(null==e||!e.sigResponses)return{error:"Failed to get signatures"};for(var t,n=l(e.sigResponses);!(t=n()).done;){var i=t.value;p.inputs[i.inputIndex].unlockingScript=(new a).writeBin(r.toArray(i.sig,"hex")).writeBin(r.toArray(i.pubKey,"hex"))}var u=r.toBase64(p.toBinary());return Promise.resolve(fetch(o.mneeApi+"/v1/transfer?auth_token="+o.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({rawtx:u})})).then(function(e){if(!e.ok)throw new Error("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){var t=e.rawtx;if(!t)return{error:"Failed to broadcast transaction"};var n=r.toArray(t,"base64");return{txid:s.fromBinary(n).id("hex"),rawtx:r.toHex(n)}})})})}var t=g-d-m,i=function(){if(t>0){var r=p.addOutput;return Promise.resolve(o.createInscription(P,t,u)).then(function(e){r.call(p,e)})}}();return i&&i.then?i.then(e):e()}var t=function(){if(m>0){var r=p.addOutput;return Promise.resolve(o.createInscription(u.feeAddress,m,u)).then(function(e){r.call(p,e)})}}();return t&&t.then?t.then(e):e()}var c=H(e,function(r){var e=p.addOutput;return Promise.resolve(o.createInscription(r.address,o.toAtomicAmount(r.amount),u)).then(function(r){e.call(p,r)})});return c&&c.then?c.then(i):i()}if(t.reduce(function(r,e){return r+(e.data.bsv21.amt||0)},0)<d)return{error:"Insufficient MNEE balance"};var m=void 0!==e.find(function(r){return r.address===u.burnAddress})?0:null==(c=u.fees.find(function(r){return d>=r.min&&d<=r.max}))?void 0:c.fee;if(void 0===m)return{error:"Fee ranges inadequate"};var p=new s(1,[],[],0),g=0,y=[],P="",w=function(r,e,t){for(var n;;){var o=r();if(C(o)&&(o=o.v),!o)return i;if(o.then){n=0;break}var i=t();if(i&&i.then){if(!C(i)){n=1;break}i=i.s}}var s=new E,u=I.bind(null,s,2);return(0===n?o.then(c):1===n?i.then(a):(void 0).then(function(){(o=r())?o.then?o.then(c).then(void 0,u):c(o):I(s,1,i)})).then(void 0,u),s;function a(e){i=e;do{if(!(o=r())||C(o)&&!o.v)return void I(s,1,i);if(o.then)return void o.then(c).then(void 0,u);C(i=t())&&(i=i.v)}while(!i||!i.then);i.then(a).then(void 0,u)}function c(r){r?(i=t())&&i.then?i.then(a).then(void 0,u):a(i):I(s,1,i)}}(function(){return!f&&g<d+m},0,function(){var r=t.shift();return r?Promise.resolve(o.fetchRawTx(r.txid)).then(function(e){if(!e)return f=1,{error:"Failed to fetch source transaction"};y.push(r.owners[0]),P=P||r.owners[0],p.addInput({sourceTXID:r.txid,sourceOutputIndex:r.vout,sourceTransaction:e,unlockingScript:new i}),g+=r.data.bsv21.amt}):(f=1,{error:"Insufficient MNEE balance"})});return w&&w.then?w.then(v):v(w)})}var c=o.mneeConfig;return c?u(c):Promise.resolve(o.getCosignerConfig()).then(u)},function(r){var e="Transaction submission failed";return r instanceof Error&&(e=r.message,r.message.includes("HTTP error")&&console.error("HTTP error details:",r)),console.error("Failed to transfer tokens:",e),{error:e}}))}catch(r){return Promise.reject(r)}},t.getBalance=function(r){try{var e=this;return Promise.resolve(T(function(){function t(t){if(!t)throw new Error("Config not fetched");return Promise.resolve(e.getUtxos(r)).then(function(t){var n=t.reduce(function(r,e){return"transfer"===e.data.bsv21.op&&(r+=e.data.bsv21.amt),r},0),o=e.fromAtomicAmount(n);return{address:r,amount:n,decimalAmount:o}})}var n=e.mneeConfig;return n?t(n):Promise.resolve(e.getCosignerConfig()).then(t)},function(e){return console.error("Failed to fetch balance:",e),{address:r,amount:0,decimalAmount:0}}))}catch(r){return Promise.reject(r)}},t.getBalances=function(r){try{var e=this;return Promise.resolve(T(function(){function t(t){if(!t)throw new Error("Config not fetched");return Promise.resolve(e.getUtxos(r)).then(function(t){return r.map(function(r){var n=t.filter(function(e){return e.owners.includes(r)}).reduce(function(r,e){return"transfer"===e.data.bsv21.op&&(r+=e.data.bsv21.amt),r},0);return{address:r,amount:n,decimalAmount:e.fromAtomicAmount(n)}})})}var n=e.mneeConfig;return n?t(n):Promise.resolve(e.getCosignerConfig()).then(t)},function(e){return console.error("Failed to fetch balances:",e),r.map(function(r){return{address:r,amount:0,decimalAmount:0}})}))}catch(r){return Promise.reject(r)}},t.validateMneeTx=function(e,t){try{var n=this;return Promise.resolve(T(function(){function o(o){if(!o)throw new Error("Config not fetched");var i=s.fromHex(e),u=i.outputs.map(function(r){return r.lockingScript}),a=g(u);return t?t.forEach(function(e,t){var s,u=e.address,c=e.amount;if(!a.find(function(r){return(null==r?void 0:r.cosigner)===o.approver}))throw new Error("Cosigner not found for address: "+u+" at index: "+t);if(!a.find(function(r){return(null==r?void 0:r.address)===u}))throw new Error("Address not found in script for address: "+u+" at index: "+t);var f=p(i.outputs[t].lockingScript),d=null==f||null==(s=f.file)?void 0:s.content;if(!d)throw new Error("Invalid inscription content");var l=r.toUTF8(d);if(!l)throw new Error("Invalid inscription content");var h=JSON.parse(l);if("bsv-20"!==h.p)throw new Error("Invalid bsv 20 protocol: "+h.p);if("transfer"!==h.op)throw new Error("Invalid operation: "+h.op);if(h.id!==o.tokenId)throw new Error("Invalid token id: "+h.id);if(h.amt!==n.toAtomicAmount(c).toString())throw new Error("Invalid amount: "+h.amt)}):a.forEach(function(r){if(""!==(null==r?void 0:r.cosigner)&&(null==r?void 0:r.cosigner)!==o.approver)throw new Error("Invalid or missing cosigner")}),!0}var i=n.mneeConfig;return i?o(i):Promise.resolve(n.getCosignerConfig()).then(o)},function(r){return console.error(r),!1}))}catch(r){return Promise.reject(r)}},t.getMneeSyncs=function(r,e,t){void 0===e&&(e=0),void 0===t&&(t=100);try{var n=this;return Promise.resolve(T(function(){var o=Array.isArray(r)?r:[r];return Promise.resolve(fetch(n.mneeApi+"/v1/sync?auth_token="+n.mneeApiKey+"&from="+e+"&limit="+t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).then(function(r){if(!r.ok)throw new Error("HTTP error! status: "+r.status);return Promise.resolve(r.json()).then(function(r){return o.map(function(e){var t=r.filter(function(r){return r.senders.includes(e)||r.receivers.includes(e)});return{address:e,syncs:t}})})})},function(e){return console.error("Failed to fetch syncs:",e),Array.isArray(r)?r.map(function(r){return{address:r,syncs:[]}}):[{address:r,syncs:[]}]}))}catch(r){return Promise.reject(r)}},t.getRecentTxHistory=function(r,e,t){try{var n=this;return Promise.resolve(T(function(){function o(o){if(!o)throw new Error("Config not fetched");return Promise.resolve(n.getMneeSyncs(r,e,t)).then(function(n){var i=n[0].syncs;if(!i||0===i.length)return{address:r,history:[],nextScore:e||0};for(var s,u=[],a=l(i);!(s=a()).done;){var c=y(s.value,r,o);c&&u.push(c)}var f=u.length>0?u[u.length-1].score:e||0;return t&&u.length>t?{address:r,history:u.slice(0,t),nextScore:f}:{address:r,history:u,nextScore:f}})}var i=n.mneeConfig;return i?o(i):Promise.resolve(n.getCosignerConfig()).then(o)},function(t){return console.error("Failed to fetch tx history:",t),{address:r,history:[],nextScore:e||0}}))}catch(r){return Promise.reject(r)}},t.getRecentTxHistories=function(r){try{var e=this;return Promise.resolve(T(function(){function t(t){if(!t)throw new Error("Config not fetched");var n={};r.forEach(function(r){var e=(r.fromScore||0)+":"+(r.limit||100);n[e]||(n[e]=[]),n[e].push(r)});var o=Object.entries(n).map(function(r){var n=r[0],o=r[1];try{var i=n.split(":").map(Number),s=i[0],u=i[1],a=o.map(function(r){return r.address});return Promise.resolve(e.getMneeSyncs(a,s,u)).then(function(r){return r.map(function(r){var e=r.address,n=r.syncs,i=o.find(function(r){return r.address===e});if(!n||0===n.length)return{address:e,history:[],nextScore:(null==i?void 0:i.fromScore)||0};for(var s,u=[],a=l(n);!(s=a()).done;){var c=y(s.value,e,t);c&&u.push(c)}var f=u.length>0?u[u.length-1].score:(null==i?void 0:i.fromScore)||0,d=null==i?void 0:i.limit;return d&&u.length>d?{address:e,history:u.slice(0,d),nextScore:f}:{address:e,history:u,nextScore:f}})})}catch(r){return Promise.reject(r)}});return Promise.resolve(Promise.all(o)).then(function(r){return r.flat()})}var n=e.mneeConfig;return n?t(n):Promise.resolve(e.getCosignerConfig()).then(t)},function(e){return console.error("Failed to fetch tx histories:",e),r.map(function(r){return{address:r.address,history:[],nextScore:r.fromScore||0}})}))}catch(r){return Promise.reject(r)}},t.parseTransaction=function(e,t){try{var n=function(){for(var e,t=l(s);!(e=t()).done;){var n,o=e.value,u=g([o])[0],m=p(o),y=null==m||null==(n=m.file)?void 0:n.content;if(y){var T=r.toUTF8(y);if(T){var I=JSON.parse(T);I&&("burn"===I.op&&(v="burn"),"deploy+mint"===I.op&&(v="deploy"),I.id===P&&u.cosigner===w||(h=""===u.cosigner&&u.address===x?"production":"sandbox"),d+=BigInt(I.amt),c.push({address:u.address,amount:parseInt(I.amt)}))}}}if("deploy"!==v&&f!==d)throw new Error("Inputs and outputs are not equal");return i===P.split("_")[0]?h="production":[S,A,b].some(function(r){return i===r.split("_")[0]})&&(h="sandbox"),{txid:i,environment:h,type:v,inputs:a,outputs:c}},o=this,i=e.id("hex"),s=e.outputs.map(function(r){return r.lockingScript}),u=e.inputs.map(function(r){return{txid:r.sourceTXID,vout:r.sourceOutputIndex}}),a=[],c=[],f=0n,d=0n,h="production",v="transfer",m=H(u,function(e){if(e.txid)return Promise.resolve(o.fetchRawTx(e.txid)).then(function(n){var o,s=n.outputs[e.vout],u=g([s.lockingScript])[0];(null==u?void 0:u.address)===t.mintAddress&&(v=i===t.tokenId.split("_")[0]?"deploy":"mint");var c=p(s.lockingScript),d=null==c||null==(o=c.file)?void 0:o.content;if(d){var l=r.toUTF8(d);if(l){var m=JSON.parse(l);if(m){var y=u.address===x;m.id===P&&u.cosigner===w||(""===u.cosigner&&"deploy+mint"===m.op&&y?(h="production",v="mint"):h="sandbox"),"transfer"===v&&(y||"1A1QNEkLuvAALsmG4Me3iubP8zb5C6jpv5"===u.address||"1BW7cejD27vDLiHsbK1Hvf1y4JTKvC1Yue"===u.address||"1AZNdbFYBDFTAEgzZMfPzANxyNrpGJZAUY"===u.address)&&(v="mint"),f+=BigInt(m.amt),a.push({address:u.address,amount:parseInt(m.amt)})}}}})});return Promise.resolve(m&&m.then?m.then(n):n())}catch(r){return Promise.reject(r)}},t.parseTx=function(r){try{var e=function(e){if(!e)throw new Error("Config not fetched");return Promise.resolve(t.fetchRawTx(r)).then(function(r){if(!r)throw new Error("Failed to fetch transaction");return Promise.resolve(t.parseTransaction(r,e))})},t=this,n=t.mneeConfig;return Promise.resolve(n?e(n):Promise.resolve(t.getCosignerConfig()).then(e))}catch(r){return Promise.reject(r)}},t.parseTxFromRawTx=function(r){try{var e=function(r){if(!r)throw new Error("Config not fetched");return Promise.resolve(t.parseTransaction(n,r))},t=this,n=s.fromHex(r),o=t.mneeConfig;return Promise.resolve(o?e(o):Promise.resolve(t.getCosignerConfig()).then(e))}catch(r){return Promise.reject(r)}},e}(),_=/*#__PURE__*/function(){function r(r){this.service=void 0,this.service=new k(r)}var e=r.prototype;return e.validateMneeTx=function(r,e){try{return Promise.resolve(this.service.validateMneeTx(r,e))}catch(r){return Promise.reject(r)}},e.toAtomicAmount=function(r){return this.service.toAtomicAmount(r)},e.fromAtomicAmount=function(r){return this.service.fromAtomicAmount(r)},e.config=function(){try{return Promise.resolve(this.service.getCosignerConfig())}catch(r){return Promise.reject(r)}},e.balance=function(r){try{return Promise.resolve(this.service.getBalance(r))}catch(r){return Promise.reject(r)}},e.balances=function(r){try{return Promise.resolve(this.service.getBalances(r))}catch(r){return Promise.reject(r)}},e.transfer=function(r,e){try{return Promise.resolve(this.service.transfer(r,e))}catch(r){return Promise.reject(r)}},e.recentTxHistory=function(r,e,t){try{return Promise.resolve(this.service.getRecentTxHistory(r,e,t))}catch(r){return Promise.reject(r)}},e.recentTxHistories=function(r){try{return Promise.resolve(this.service.getRecentTxHistories(r))}catch(r){return Promise.reject(r)}},e.parseTx=function(r){try{return Promise.resolve(this.service.parseTx(r))}catch(r){return Promise.reject(r)}},e.parseTxFromRawTx=function(r){try{return Promise.resolve(this.service.parseTxFromRawTx(r))}catch(r){return Promise.reject(r)}},r}();export{_ as default}; //# sourceMappingURL=index.module.js.map