@mnee/ts-sdk 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.wags-tags.json +24 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +80 -2
- package/dist/index.modern.js +1 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/mnee.types.d.ts +14 -0
- package/dist/mneeService.d.ts +17 -5
- package/package.json +1 -1
package/.wags-tags.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"environments": [
|
|
3
|
+
{
|
|
4
|
+
"name": "dev",
|
|
5
|
+
"branch": "develop",
|
|
6
|
+
"isProduction": false
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"name": "qa",
|
|
10
|
+
"branch": "qa",
|
|
11
|
+
"isProduction": false
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "stg",
|
|
15
|
+
"branch": "stg",
|
|
16
|
+
"isProduction": false
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"name": "prod",
|
|
20
|
+
"branch": "main",
|
|
21
|
+
"isProduction": true
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("@bsv/sdk");function r(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach(function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}}),r.default=e,r}var t=/*#__PURE__*/r(require("bip39"));function n(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=Array(r);t<r;t++)n[t]=e[t];return n}function i(e,r){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,r){if(e){if("string"==typeof e)return n(e,r);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?n(e,r):void 0}}(e))||r&&e&&"number"==typeof e.length){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function o(){return o=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},o.apply(null,arguments)}function a(e){return a=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},a(e)}function s(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(s=function(){return!!e})()}function u(e,r){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,r){return e.__proto__=r,e},u(e,r)}function c(e){var r="function"==typeof Map?new Map:void 0;return c=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(r){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return function(e,r,t){if(s())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,r);var i=new(e.bind.apply(e,n));return t&&u(i,t.prototype),i}(e,arguments,a(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),u(t,e)},c(e)}function f(e){var r=new Error(e);return r.stack=void 0,r}var d=/*#__PURE__*/function(){function r(){}var t=r.prototype;return t.lock=function(r,t){var n=[];if("string"==typeof r){var i=e.Utils.fromBase58Check(r);if(0!==i.prefix[0]&&111!==i.prefix[0])throw f("only P2PKH is supported");n=i.data}else n=r;var o=new e.LockingScript;return o.writeOpCode(e.OP.OP_DUP).writeOpCode(e.OP.OP_HASH160).writeBin(n).writeOpCode(e.OP.OP_EQUALVERIFY).writeOpCode(e.OP.OP_CHECKSIGVERIFY).writeBin(t.encode(!0)).writeOpCode(e.OP.OP_CHECKSIG),o},t.userUnlock=function(r,t,n,i,o){return void 0===t&&(t="all"),void 0===n&&(n=!1),{sign:function(a,s){try{var u,c,d,l=e.TransactionSignature.SIGHASH_FORKID;"all"===t&&(l|=e.TransactionSignature.SIGHASH_ALL),"none"===t&&(l|=e.TransactionSignature.SIGHASH_NONE),"single"===t&&(l|=e.TransactionSignature.SIGHASH_SINGLE),n&&(l|=e.TransactionSignature.SIGHASH_ANYONECANPAY);var h=a.inputs[s],v=a.inputs.filter(function(e,r){return r!==s}),m=h.sourceTXID?h.sourceTXID:null==(u=h.sourceTransaction)?void 0:u.id("hex");if(!m)throw f("The input sourceTXID or sourceTransaction is required for transaction signing.");if(i||(i=null==(c=h.sourceTransaction)?void 0:c.outputs[h.sourceOutputIndex].satoshis),!i)throw f("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(o||(o=null==(d=h.sourceTransaction)?void 0:d.outputs[h.sourceOutputIndex].lockingScript),!o)throw f("The lockingScript or input sourceTransaction is required for transaction signing.");var p=e.TransactionSignature.format({sourceTXID:m,sourceOutputIndex:h.sourceOutputIndex,sourceSatoshis:i,transactionVersion:a.version,otherInputs:v,inputIndex:s,outputs:a.outputs,inputSequence:h.sequence||4294967295,subscript:o,lockTime:a.lockTime,scope:l}),g=r.sign(e.Hash.sha256(p)),y=new e.TransactionSignature(g.r,g.s,l),P=new e.UnlockingScript;return P.writeBin(y.toChecksigFormat()),P.writeBin(r.toPublicKey().encode(!0)),Promise.resolve(P)}catch(e){return Promise.reject(e)}},estimateLength:function(){return Promise.resolve(182)}}},t.unlock=function(r,t,n,i,o,a){return void 0===n&&(n="all"),void 0===i&&(i=!1),{sign:function(s,u){try{var c,d,l,h=e.TransactionSignature.SIGHASH_FORKID;"all"===n&&(h|=e.TransactionSignature.SIGHASH_ALL),"none"===n&&(h|=e.TransactionSignature.SIGHASH_NONE),"single"===n&&(h|=e.TransactionSignature.SIGHASH_SINGLE),i&&(h|=e.TransactionSignature.SIGHASH_ANYONECANPAY);var v=s.inputs[u],m=s.inputs.filter(function(e,r){return r!==u}),p=v.sourceTXID?v.sourceTXID:null==(c=v.sourceTransaction)?void 0:c.id("hex");if(!p)throw f("The input sourceTXID or sourceTransaction is required for transaction signing.");if(o||(o=null==(d=v.sourceTransaction)?void 0:d.outputs[v.sourceOutputIndex].satoshis),!o)throw f("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(a||(a=null==(l=v.sourceTransaction)?void 0:l.outputs[v.sourceOutputIndex].lockingScript),!a)throw f("The lockingScript or input sourceTransaction is required for transaction signing.");var g=e.TransactionSignature.format({sourceTXID:p,sourceOutputIndex:v.sourceOutputIndex,sourceSatoshis:o,transactionVersion:s.version,otherInputs:m,inputIndex:u,outputs:s.outputs,inputSequence:v.sequence||4294967295,subscript:a,lockTime:s.lockTime,scope:h}),y=r.sign(e.Hash.sha256(g)),P=new e.TransactionSignature(y.r,y.s,h),w=new e.UnlockingScript;return w.writeBin(P.toChecksigFormat()),w.writeScript(t),Promise.resolve(w)}catch(e){return Promise.reject(e)}},estimateLength:function(){return Promise.resolve(182)}}},r}(),l=function(e){return Buffer.from(e).toString("hex")},h=function(r,t,n,i){void 0===i&&(i=!1);var o="";if(void 0!==(null==t?void 0:t.dataB64)&&void 0!==(null==t?void 0:t.contentType)){var a=l("ord"),s=Buffer.from(t.dataB64,"base64").toString("hex").trim();if(!s)throw f("Invalid file data");var u=l(t.contentType);if(!u)throw f("Invalid media type");o="OP_0 OP_IF "+a+" OP_1 "+u+" OP_0 "+s+" OP_ENDIF"}var c=(o?o+" "+(i?"OP_CODESEPARATOR ":""):"")+r.toASM();if(n&&(!n.app||!n.type))throw f("MAP.app and MAP.type are required fields");if(null!=n&&n.app&&null!=n&&n.type){c=(c?c+" ":"")+"OP_RETURN "+l("1PuQa7K62MiKCtssSLKy1kh56WWU7MtUR5")+" "+l("SET");for(var d=0,h=Object.entries(n);d<h.length;d++){var v=h[d],m=v[0],p=v[1];"cmd"!==m&&(c=c+" "+l(m)+" "+l(p))}}return e.LockingScript.fromASM(c)},v=1e-5,m="ae59f3b898ec61acbdb6cc7a245fabeded0c094bf046f35206a3aec60ef88127_0",p="1inHbiwj2jrEcZPiSYnfgJ8FmS1Bmk4Dh",g="833a7720966a2a435db28d967385e8aa7284b6150ebb39482cc5228b73e1703f_0",y="1AZNdbFYBDFTAEgzZMfPzANxyNrpGJZAUY",P=function(r){for(var t,n,i,o=0;o<r.chunks.length;o++){var a,s=r.chunks[o];o>=2&&3===(null==(a=s.data)?void 0:a.length)&&"ord"==e.Utils.toUTF8(s.data)&&r.chunks[o-1].op==e.OP.OP_IF&&r.chunks[o-2].op==e.OP.OP_FALSE&&(i=o+1)}if(void 0!==i){for(var u={file:{hash:"",size:0,type:""},fields:{}},c=i;c<r.chunks.length;c+=2){var f,d,l=r.chunks[c];if(l.op==e.OP.OP_ENDIF)break;if(l.op>e.OP.OP_16)return;var h=r.chunks[c+1];if(h.op>e.OP.OP_PUSHDATA4)return;if(null==(f=l.data)||!f.length){var v=0;switch(l.op>e.OP.OP_PUSHDATA4&&l.op<=e.OP.OP_16?v=l.op-80:null!=(d=l.data)&&d.length&&(v=l.data[0]),v){case 0:if(u.file.size=(null==(t=h.data)?void 0:t.length)||0,null==(n=h.data)||!n.length)break;u.file.hash=e.Utils.toBase64(e.Hash.sha256(h.data)),u.file.content=h.data;break;case 1:u.file.type=Buffer.from(h.data||[]).toString()}}}return u}},w=function(r){return r.map(function(r){for(var t=r.chunks,n=0;n<=t.length-4;n++){var i,o,a;if(t.length>n+6&&t[0+n].op===e.OP.OP_DUP&&t[1+n].op===e.OP.OP_HASH160&&20===(null==(i=t[2+n].data)?void 0:i.length)&&t[3+n].op===e.OP.OP_EQUALVERIFY&&t[4+n].op===e.OP.OP_CHECKSIGVERIFY&&33===(null==(o=t[5+n].data)?void 0:o.length)&&t[6+n].op===e.OP.OP_CHECKSIG)return{cosigner:e.Utils.toHex(t[5+n].data||[]),address:e.Utils.toBase58Check(t[2+n].data||[],[0])};if(t[0+n].op===e.OP.OP_DUP&&t[1+n].op===e.OP.OP_HASH160&&20===(null==(a=t[2+n].data)?void 0:a.length)&&t[3+n].op===e.OP.OP_EQUALVERIFY&&t[4+n].op===e.OP.OP_CHECKSIG)return{cosigner:"",address:e.Utils.toBase58Check(t[2+n].data||[],[0])}}}).filter(function(e){return void 0!==e})},b=function(r,t,n){var i=r.senders.includes(t)?"send":"receive",o=r.height>0?"confirmed":"unconfirmed";if(!r.rawtx)return null;var a=e.Utils.toArray(r.rawtx,"base64"),s=e.Utils.toHex(a),u=e.Transaction.fromHex(s).outputs.map(function(e){return e.lockingScript}),c=w(u),f=u.map(P),d=c.map(function(e){return e.address}),l=d.indexOf(n.feeAddress),h=r.senders[0],v=0,m=new Map;f.forEach(function(r,i){var o,a=null==r||null==(o=r.file)?void 0:o.content;if(a){var s=e.Utils.toUTF8(a);if(s){var u;try{u=JSON.parse(s)}catch(e){return void console.error("Failed to parse inscription JSON:",e)}if("bsv-20"===u.p&&u.id===n.tokenId){var c=parseInt(u.amt,10);if(!Number.isNaN(c))if(l!==i||h!==t){var f=d[i],p=m.get(f)||0;m.set(f,p+c)}else v+=c}}}});var p=m.get(t)||0;if("send"===i){var g=m.get(h)||0;m.set(h,g-p)}var y=[],b=(y="receive"===i?[{address:h,amount:p}]:Array.from(m.entries()).map(function(e){return{address:e[0],amount:e[1]}}).filter(function(e){return e.address!==t&&e.address!==n.feeAddress&&e.amount>0})).reduce(function(e,r){return e+r.amount},0);return{txid:r.txid,height:r.height,type:i,status:o,amount:b,fee:v,score:r.score,counterparties:y}},x=function(r){try{var t=e.Utils.fromBase58Check(r),n=t.prefix[0];if("number"!=typeof n||![0].includes(n))throw f("Invalid address prefix: "+n);if(20!==t.data.length)throw f("Invalid address payload length: "+t.data.length);return!0}catch(e){return!1}},I=function(r){try{return{isValid:!0,privateKey:e.PrivateKey.fromWif(r)}}catch(e){if(e instanceof Error){var t=e.message.toLowerCase();if(t.includes("invalid base58 character"))return{isValid:!1,error:"Invalid WIF key: contains invalid characters"};if(t.includes("invalid checksum"))return{isValid:!1,error:"Invalid WIF key: checksum verification failed"};if(t.includes("expected base58 string"))return{isValid:!1,error:"Invalid WIF key: must be a valid base58 encoded string"}}return{isValid:!1,error:"Invalid WIF key provided"}}},A=/*#__PURE__*/function(e){function r(r){var t;return(t=e.call(this,r.message)||this).code=void 0,t.hostname=void 0,t.originalError=void 0,t.name="NetworkError",t.code=r.code,t.hostname=r.hostname,t.originalError=r.originalError,t}var t,n;return n=e,(t=r).prototype=Object.create(n.prototype),t.prototype.constructor=t,u(t,n),r}(/*#__PURE__*/c(Error));function T(e){var r,t,n;if(!e)return!1;var i=["ENOTFOUND","ECONNREFUSED","ETIMEDOUT","ECONNRESET","ENETUNREACH","EHOSTUNREACH","EPIPE","ECONNABORTED"];return!!i.includes(e.code)||!(!e.cause||!i.includes(null==(r=e.cause)?void 0:r.code))||!!(null!=(t=e.message)&&t.includes("fetch failed")||null!=(n=e.message)&&n.includes("getaddrinfo"))}function S(e,r){if(T(e)){var t=function(e){if(e.cause&&e.cause.code){var r=e.cause,t="Network connection failed";switch(r.code){case"ENOTFOUND":t="Unable to connect to MNEE network. Please check your internet connection.";break;case"ECONNREFUSED":t="Connection refused by MNEE server. The service may be temporarily unavailable.";break;case"ETIMEDOUT":t="Request timed out. Please check your internet connection and try again.";break;case"ECONNRESET":t="Connection was reset. Please try again.";break;case"ENETUNREACH":case"EHOSTUNREACH":t="Network unreachable. Please check your internet connection.";break;default:t="Network error: "+r.code+". Please check your connection and try again."}return new A({code:r.code,message:t,hostname:r.hostname,originalError:e})}if(e.code){var n="Network error occurred";switch(e.code){case"ENOTFOUND":n="Unable to connect to MNEE network. Please check your internet connection.";break;case"ECONNREFUSED":n="Connection refused by MNEE server. The service may be temporarily unavailable.";break;case"ETIMEDOUT":n="Request timed out. Please check your internet connection and try again.";break;default:n="Network error: "+e.code+". Please check your connection and try again."}return new A({code:e.code,message:n,hostname:e.hostname,originalError:e})}return new A({code:"NETWORK_ERROR",message:"Network error occurred. Please check your internet connection and try again.",originalError:e})}(e);return console.error("Network error during "+r+": "+t.message),t.message}console.error("Failed to "+r+":",e)}var O=["parseOptions"];function k(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}const E="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function C(e,r,t){if(!e.s){if(t instanceof N){if(!t.s)return void(t.o=C.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(C.bind(null,e,r),C.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}var N=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{C(n,1,o(this.v))}catch(e){C(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?C(n,1,r?r(i):i):t?C(n,1,t(i)):C(n,2,i)}catch(e){C(n,2,e)}},n},e}();function H(e){return e instanceof N&&1&e.s}var j=/*#__PURE__*/function(){function e(e){this.service=void 0,this.service=e}var r=e.prototype;return r.getUtxos=function(e,r){void 0===r&&(r={});try{var t=this;if(!Array.isArray(e))throw f("Input must be an array of addresses");var n=[];return Promise.resolve(t.processBatch(e,function(e){try{for(var o,a=[],s=[],u=i(e);!(o=u()).done;){var c=o.value;c&&"string"==typeof c&&""!==c.trim()?/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^bc1[a-z0-9]{39,59}$/.test(c)?a.push(c):(s.push(c),n.push({items:[c],error:{message:"Invalid address format: "+c},retryCount:0})):(s.push(c),n.push({items:[c],error:{message:"Invalid address: empty or not a string"},retryCount:0}))}if(!r.continueOnError&&s.length>0)throw new Error(n[0].error.message);return 0===a.length?Promise.resolve([]):Promise.resolve(t.service.getUtxos(a)).then(function(r){return e.map(function(e){return{address:e,utxos:a.includes(e)?r.filter(function(r){return r.owners.includes(e)}):[]}})})}catch(e){return Promise.reject(e)}},r)).then(function(e){return o({},e,{errors:[].concat(e.errors,n),totalErrors:e.errors.length+n.length})})}catch(e){return Promise.reject(e)}},r.getBalances=function(e,r){void 0===r&&(r={});try{var t=this;if(!Array.isArray(e))throw f("Input must be an array of addresses");return Promise.resolve(t.processBatch(e,function(e){try{return Promise.resolve(t.service.getBalances(e))}catch(e){return Promise.reject(e)}},r))}catch(e){return Promise.reject(e)}},r.getTxHistories=function(e,r){void 0===r&&(r={});try{var t=this;if(!Array.isArray(e))throw f("Input must be an array of address history parameters");return Promise.resolve(t.processBatch(e,function(e){try{return Promise.resolve(t.service.getRecentTxHistories(e))}catch(e){return Promise.reject(e)}},r,function(e){return e.address}))}catch(e){return Promise.reject(e)}},r.parseTx=function(e,r){void 0===r&&(r={});try{var t=this;if(!Array.isArray(e))throw f("Input must be an array of transaction IDs");var n=r.parseOptions,i=function(e,r){if(null==e)return{};var t={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==r.indexOf(n))continue;t[n]=e[n]}return t}(r,O),a=[];return Promise.resolve(t.processBatch(e,function(e){try{return Promise.resolve(Promise.allSettled(e.map(function(e){try{if(!e||"string"!=typeof e||""===e.trim())throw new Error("Invalid transaction ID: empty or not a string");if(!/^[a-fA-F0-9]{64}$/.test(e))throw new Error("Invalid transaction ID format: "+e);return Promise.resolve(t.service.parseTx(e,n)).then(function(r){return{txid:e,parsed:r}})}catch(e){return Promise.reject(e)}}))).then(function(r){var t=[];if(r.forEach(function(r,n){var o=e[n];if("fulfilled"===r.status)t.push(r.value);else{var s=r.reason instanceof Error?r.reason.message:String(r.reason);a.push({items:[o],error:{message:s},retryCount:i.maxRetries||3})}}),!i.continueOnError&&a.length>0)throw new Error(a[0].error.message);return t})}catch(e){return Promise.reject(e)}},i)).then(function(e){return o({},e,{errors:[].concat(e.errors,a),totalErrors:e.errors.length+a.length})})}catch(e){return Promise.reject(e)}},r.processBatch=function(e,r,t,n){try{var i=this,o=t.chunkSize,a=void 0===o?20:o,s=t.continueOnError,u=void 0!==s&&s,c=t.onProgress,f=t.maxRetries,d=void 0===f?3:f,l=t.retryDelay,h=void 0===l?1e3:l,v=t.requestsPerSecond,m=void 0===v?3:v,p=a>0?a:20,g=m>0?m:3,y=Math.ceil(1e3/g),P=Math.max(1,Math.floor(g)),w=new D(P,y);if(0===e.length)return Promise.resolve({results:[],errors:[],totalProcessed:0,totalErrors:0});var b=[],x=[],I=0,A=i.chunkArray(e,p),T=A.length,S=A.map(function(e){try{return Promise.resolve(k(function(){return Promise.resolve(i.processWithRetry(function(){return r(e)},d,h,w)).then(function(e){return b.push.apply(b,e),I++,c&&c(I,T,x.length),e})},function(t){function o(){if(b.push.apply(b,a),s.length>0){var e=s.map(function(e){var r=e.item;return n?n(r):r}),r=s[0].error;x.push({items:e,error:{message:r.message,code:r.code},retryCount:d})}return I++,c&&c(I,T,x.length),a}if(!u)throw t;var a=[],s=[],f=function(e,r){if("function"==typeof e[E]){var t,n,i,o=function(e){try{for(;!(t=a.next()).done;)if((e=r(t.value))&&e.then){if(!H(e))return void e.then(o,i||(i=C.bind(null,n=new N,2)));e=e.v}n?C(n,1,e):n=e}catch(e){C(n||(n=new N),2,e)}},a=e[E]();if(o(),a.return){var s=function(e){try{t.done||a.return()}catch(e){}return e};if(n&&n.then)return n.then(s,function(e){throw s(e)});s()}return n}if(!("length"in e))throw new TypeError("Object is not iterable");for(var u=[],c=0;c<e.length;c++)u.push(e[c]);return function(e,r){var t,n,i=-1;return function o(a){try{for(;++i<e.length;)if((a=r(i))&&a.then){if(!H(a))return void a.then(o,n||(n=C.bind(null,t=new N,2)));a=a.v}t?C(t,1,a):t=a}catch(e){C(t||(t=new N),2,e)}}(),t}(u,function(e){return r(u[e])})}(e,function(e){var t=k(function(){return Promise.resolve(i.processWithRetry(function(){return r([e])},d,h,w)).then(function(e){e.length>0&&a.push.apply(a,e)})},function(r){var t=r instanceof Error?r:new Error(String(r));s.push({item:e,error:t})});if(t&&t.then)return t.then(function(){})});return f&&f.then?f.then(o):o()}))}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(S)).then(function(){return{results:b,errors:x,totalProcessed:I,totalErrors:x.length}})}catch(e){return Promise.reject(e)}},r.processWithRetry=function(e,r,t,n){try{var i,o,a=function(e){if(i)return e;throw o||new Error("Max retries exceeded")},s=this,u=0,c=function(e,r,t){for(var n;;){var i=e();if(H(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!H(o)){n=1;break}o=o.s}if(r){var a=r();if(a&&a.then&&!H(a)){n=2;break}}}var s=new N,u=C.bind(null,s,2);return(0===n?i.then(f):1===n?o.then(c):a.then(d)).then(void 0,u),s;function c(n){o=n;do{if(r&&(a=r())&&a.then&&!H(a))return void a.then(d).then(void 0,u);if(!(i=e())||H(i)&&!i.v)return void C(s,1,o);if(i.then)return void i.then(f).then(void 0,u);H(o=t())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=t())&&o.then?o.then(c).then(void 0,u):c(o):C(s,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):C(s,1,o)}}(function(){return!i&&u<r},function(){return u++},function(){return k(function(){return Promise.resolve(n.execute(e)).then(function(e){return i=1,e})},function(e){o=e;var n=function(){if(u<r-1)return Promise.resolve(s.delay(t*(u+1))).then(function(){})}();return n&&n.then?n.then(function(){}):void 0})});return Promise.resolve(c&&c.then?c.then(a):a(c))}catch(e){return Promise.reject(e)}},r.chunkArray=function(e,r){for(var t=[],n=Math.max(1,r),i=0;i<e.length;i+=n)t.push(e.slice(i,i+n));return t},r.delay=function(e){return new Promise(function(r){return setTimeout(r,e)})},e}(),D=/*#__PURE__*/function(){function e(e,r){this.maxConcurrent=void 0,this.minDelay=void 0,this.queue=[],this.running=0,this.maxConcurrent=e,this.minDelay=r}var r=e.prototype;return r.execute=function(e){try{var r=this;return Promise.resolve(r.waitForSlot()).then(function(){return function(t,n){try{var i=function(){r.running++;var t=Date.now();return Promise.resolve(e()).then(function(e){var n=Date.now()-t,i=function(){if(n<r.minDelay)return Promise.resolve(r.delay(r.minDelay-n)).then(function(){})}();return i&&i.then?i.then(function(){return e}):e})}()}catch(e){return n(!0,e)}return i&&i.then?i.then(n.bind(null,!1),n.bind(null,!0)):n(!1,i)}(0,function(e,t){if(r.running--,r.processQueue(),e)throw t;return t})})}catch(e){return Promise.reject(e)}},r.waitForSlot=function(){var e=this;return this.running<this.maxConcurrent?Promise.resolve():new Promise(function(r){e.queue.push(r)})},r.processQueue=function(){if(this.queue.length>0&&this.running<this.maxConcurrent){var e=this.queue.shift();e&&e()}},r.delay=function(e){return new Promise(function(r){return setTimeout(r,e)})},e}();function M(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}function U(e,r,t){if(!e.s){if(t instanceof _){if(!t.s)return void(t.o=U.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(U.bind(null,e,r),U.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}var _=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{U(n,1,o(this.v))}catch(e){U(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?U(n,1,r?r(i):i):t?U(n,1,t(i)):U(n,2,i)}catch(e){U(n,2,e)}},n},e}();function F(e){return e instanceof _&&1&e.s}function R(e,r,t){for(var n;;){var i=e();if(F(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!F(o)){n=1;break}o=o.s}if(r){var a=r();if(a&&a.then&&!F(a)){n=2;break}}}var s=new _,u=U.bind(null,s,2);return(0===n?i.then(f):1===n?o.then(c):a.then(d)).then(void 0,u),s;function c(n){o=n;do{if(r&&(a=r())&&a.then&&!F(a))return void a.then(d).then(void 0,u);if(!(i=e())||F(i)&&!i.v)return void U(s,1,o);if(i.then)return void i.then(f).then(void 0,u);F(o=t())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=t())&&o.then?o.then(c).then(void 0,u):c(o):U(s,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):U(s,1,o)}}var B="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function K(e,r,t){var n,i,o=-1;return function a(s){try{for(;++o<e.length&&(!t||!t());)if((s=r(o))&&s.then){if(!F(s))return void s.then(a,i||(i=U.bind(null,n=new _,2)));s=s.v}n?U(n,1,s):n=s}catch(e){U(n||(n=new _),2,e)}}(),n}function V(e,r,t){if("function"==typeof e[B]){var n,i,o,a=function(e){try{for(;!((n=s.next()).done||t&&t());)if((e=r(n.value))&&e.then){if(!F(e))return void e.then(a,o||(o=U.bind(null,i=new _,2)));e=e.v}i?U(i,1,e):i=e}catch(e){U(i||(i=new _),2,e)}},s=e[B]();if(a(),s.return){var u=function(e){try{n.done||s.return()}catch(e){}return e};if(i&&i.then)return i.then(u,function(e){throw u(e)});u()}return i}if(!("length"in e))throw new TypeError("Object is not iterable");for(var c=[],f=0;f<e.length;f++)c.push(e[f]);return K(c,function(e){return r(c[e])},t)}var q=/*#__PURE__*/function(){function r(e){if(this.mneeApiKey=void 0,this.mneeConfig=void 0,this.mneeApi=void 0,"production"!==e.environment&&"sandbox"!==e.environment)throw f('Invalid environment. Must be either "production" or "sandbox"');var r="production"===e.environment;if(""===(null==e?void 0:e.apiKey))throw f("MNEE API key cannot be an empty string");this.mneeApiKey=null!=e&&e.apiKey?e.apiKey:r?"92982ec1c0975f31979da515d46bae9f":"54f1fd1688ba66a58a67675b82feb93e",this.mneeApi=r?"https://proxy-api.mnee.net":"https://sandbox-proxy-api.mnee.net",this.getCosignerConfig().catch(function(){})}var t=r.prototype;return t.getCosignerConfig=function(){try{var e=this;return Promise.resolve(M(function(){return Promise.resolve(fetch(e.mneeApi+"/v1/config?auth_token="+e.mneeApiKey,{method:"GET"})).then(function(r){if(401===r.status||403===r.status)throw f("Invalid API key");if(!r.ok)throw f("HTTP error! status: "+r.status);return Promise.resolve(r.json()).then(function(r){return e.mneeConfig=r,r})})},function(e){throw T(e)&&S(e,"fetch config"),e}))}catch(e){return Promise.reject(e)}},t.toAtomicAmount=function(e){return Math.round(e*Math.pow(10,5))},t.fromAtomicAmount=function(e){return e/Math.pow(10,5)},t.createInscription=function(r,t,n){try{var i={p:"bsv-20",op:"transfer",id:n.tokenId,amt:t.toString()};return Promise.resolve({lockingScript:h((new d).lock(r,e.PublicKey.fromString(n.approver)),{dataB64:Buffer.from(JSON.stringify(i)).toString("base64"),contentType:"application/bsv-20"}),satoshis:1})}catch(e){return Promise.reject(e)}},t.getUtxos=function(e,r,t,n){try{var i,o=this;return Promise.resolve(M(function(){function a(a){var s;if(i)return a;function u(e){if(s)return e;throw f("Invalid input type for address")}var c=function(){if(Array.isArray(e)){var i=e.filter(function(e){return"string"==typeof e&&x(e)});if(0===i.length)throw f("No valid Bitcoin addresses provided");var a=e.filter(function(e){return"string"!=typeof e||!x(e)});return a.length>0&&console.warn("[33m"+a.length+" invalid bitcoin addresses will be ignored[0m"),Promise.resolve(fetch(o.mneeApi+"/v2/utxos?auth_token="+o.mneeApiKey+(void 0!==r?"&page="+r:"")+(void 0!==t?"&size="+t:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){var r=["transfer","deploy+mint"],t=e.filter(function(e){return r.includes(e.data.bsv21.op.toLowerCase())});return s=1,t})})}}();return c&&c.then?c.then(u):u(c)}if(!e)throw f("Address is required");if(void 0!==r&&("number"!=typeof r||r<=0||!Number.isFinite(r)))throw f("Invalid page: "+r+". Must be a positive integer");if(void 0!==t&&("number"!=typeof t||t<=0||!Number.isInteger(t)))throw f("Invalid size: "+t+". Must be a positive integer");if(void 0!==n&&"asc"!==n&&"desc"!==n)throw f("Invalid order: "+n+". Must be 'asc' or 'desc'");var s=function(){if("string"==typeof e){if(!x(e))throw f("Invalid Bitcoin address: "+e);return Promise.resolve(fetch(o.mneeApi+"/v2/utxos?auth_token="+o.mneeApiKey+(void 0!==r?"&page="+r:"")+(void 0!==t?"&size="+t:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify([e])})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){var r=["transfer","deploy+mint"],t=e.filter(function(e){return r.includes(e.data.bsv21.op.toLowerCase())});return i=1,t})})}}();return s&&s.then?s.then(a):a(s)},function(e){throw T(e)&&S(e,"fetch UTXOs"),e}))}catch(e){return Promise.reject(e)}},t.fetchRawTx=function(r,t){void 0===t&&(t=3);try{var n,i=this,o=0,a=R(function(){return!n&&o<=t},function(){return o++},function(){return M(function(){return Promise.resolve(fetch(i.mneeApi+"/v1/tx/"+r+"?auth_token="+i.mneeApiKey)).then(function(i){function a(){if(200!==i.status)throw f(i.status+" - Failed to fetch rawtx for txid: "+r);return Promise.resolve(i.json()).then(function(r){var t=e.Transaction.fromHex(Buffer.from(r.rawtx,"base64").toString("hex"));return n=1,t})}if(404===i.status)throw f("Transaction not found");if(401===i.status||403===i.status)throw f("Invalid API key");var s=function(){if(429===i.status&&o<t){var e=Math.min(500*Math.pow(2,o),2e3);return Promise.resolve(new Promise(function(r){return setTimeout(r,e)})).then(function(){})}}();return s&&s.then?s.then(a):a()})},function(e){return o===t?(T(e)&&S(e,"fetch transaction"),void(n=1)):Promise.resolve(new Promise(function(e){return setTimeout(e,200)})).then(function(){})})});return Promise.resolve(a&&a.then?a.then(function(e){return n?e:void 0}):n?a:void 0)}catch(e){return Promise.reject(e)}},t.getSignatures=function(r,t){try{try{var n;switch(r.format){case"beef":n=e.Transaction.fromHexBEEF(r.rawtx);break;case"ef":n=e.Transaction.fromHexEF(r.rawtx);break;default:n=e.Transaction.fromHex(r.rawtx)}var i=r.sigRequests.flatMap(function(r){return[t].map(function(t){var i=e.TransactionSignature.format({sourceTXID:r.prevTxid,sourceOutputIndex:r.outputIndex,sourceSatoshis:r.satoshis,transactionVersion:n.version,otherInputs:n.inputs.filter(function(e,t){return t!==r.inputIndex}),inputIndex:r.inputIndex,outputs:n.outputs,inputSequence:n.inputs[r.inputIndex].sequence||0,subscript:r.script?e.Script.fromHex(r.script):(new e.P2PKH).lock(t.toPublicKey().toAddress()),lockTime:n.lockTime,scope:r.sigHashType||65}),o=t.sign(e.Hash.sha256(i)),a=new e.TransactionSignature(o.r,o.s,r.sigHashType||65);return{sig:e.Utils.toHex(a.toChecksigFormat()),pubKey:t.toPublicKey().toString(),inputIndex:r.inputIndex,sigHashType:r.sigHashType||65,csIdx:r.csIdx}})});return Promise.resolve({sigResponses:i})}catch(e){var o;return T(e)&&S(e,"get signatures"),Promise.resolve({error:{message:null!=(o=e.message)?o:"unknown",cause:e.cause}})}}catch(e){return Promise.reject(e)}},t.getEnoughUtxos=function(e,r){try{var t=function(t){if(!t)throw f("Config not fetched");var o=t.fees.find(function(e){return r>=e.min&&r<=e.max});if(!o)throw f("Fee not found");var a=o.fee,s=r+a;return Promise.resolve(n.getBalance(e)).then(function(r){var t;function o(e){c.sort(function(e,r){return r.data.bsv21.amt-e.data.bsv21.amt});for(var r,t=[],n=0,o=i(c);!(r=o()).done;){var a=r.value;if(t.push(a),(n+=a.data.bsv21.amt)>=s)break}return t}if(r.amount<s)throw f("Insufficient MNEE balance. Max transfer amount: "+n.fromAtomicAmount(r.amount-a));var u=1,c=[],d=0,l=R(function(){return!t&&d<s},void 0,function(){return Promise.resolve(n.getUtxos(e,u,25)).then(function(e){if(0===e.length)throw f("Insufficient MNEE balance. Max transfer amount: "+n.fromAtomicAmount(d-a));c.push.apply(c,e),(d=c.reduce(function(e,r){return e+r.data.bsv21.amt},0))>=s?t=1:u++})});return l&&l.then?l.then(o):o()})},n=this,o=n.mneeConfig;return Promise.resolve(o?t(o):Promise.resolve(n.getCosignerConfig()).then(t))}catch(e){return Promise.reject(e)}},t.getAllUtxos=function(e){try{var r,t=this,n=1,i=[],o=R(function(){return!r},void 0,function(){return Promise.resolve(t.getUtxos(e,n,100)).then(function(e){0!==e.length?(i.push.apply(i,e),n++):r=1})});return Promise.resolve(o&&o.then?o.then(function(){return i}):i)}catch(e){return Promise.reject(e)}},t.transfer=function(r,t,n){try{var o=this;return Promise.resolve(M(function(){function a(a){if(!a)throw f("Config not fetched");var s=function(e,r){var t=I(r),n=t.privateKey;if(0===e.length)return{isValid:!1,error:"Empty transfer options provided. Please provide at least one recipient."};if(!t.isValid)return{isValid:!1,error:t.error||"Invalid WIF key provided"};if(!n)return{isValid:!1,error:"Private key not found"};for(var o,a=0,s=i(e);!(o=s()).done;){var u=o.value;if(!x(u.address))return{isValid:!1,error:"Invalid recipient address: "+u.address};if("number"!=typeof u.amount||isNaN(u.amount)||!isFinite(u.amount))return{isValid:!1,error:"Invalid amount for "+u.address+": amount must be a valid number"};if(u.amount<v)return{isValid:!1,error:"Invalid amount for "+u.address+": minimum transfer amount is "+v+" MNEE"};a+=u.amount}return a<=0?{isValid:!1,error:"Invalid amount: total must be greater than 0"}:{isValid:!0,totalAmount:a,privateKey:n}}(r,t),u=s.totalAmount,c=s.privateKey;if(!s.isValid)throw f(s.error||"Invalid transfer options");if(!c)throw f("Private key not found");if(!u)throw f("Invalid amount");var d=o.toAtomicAmount(u),l=c.toAddress();return Promise.resolve(o.getEnoughUtxos(l,d)).then(function(t){var i;function s(t){function i(){function r(){function r(){if(null!=n&&n.extraData){var r=Array.isArray(n.extraData)?n.extraData:[n.extraData];if(0===r.length)throw f("extraData must contain at least one item");for(var t=[],i=0;i<r.length;i++){var a=r[i];if(!a||"string"!=typeof a.data)throw f("Invalid extraData at index "+i+": data must be a string");if("utf8"===a.type){var s=Buffer.from(a.data,"utf8");if(0===s.length)throw f("extraData at index "+i+" is empty after UTF-8 encoding");t.push(s)}else{if("hex"!==a.type)throw f("Unsupported extraData type at index "+i+": "+a.type);var u=a.data.trim();if(!u)throw f("extraData at index "+i+" is empty");if(!/^[0-9a-fA-F]+$/.test(u)||u.length%2!=0)throw f("extraData at index "+i+' is not valid hex or has odd length: "'+u+'"');var d=Buffer.from(u,"hex");if(0===d.length)throw f("extraData at index "+i+" decoded to an empty buffer");t.push(d)}}var l=t.reduce(function(e,r){return e+r.length},0);if(l>512)throw f("extraData is too large: "+l+" bytes (max allowed is 512 bytes)");var v=new e.Script;v.writeOpCode(e.OP.OP_0),v.writeOpCode(e.OP.OP_RETURN);for(var m=0,p=t;m<p.length;m++)v.writeBin(Array.from(p[m]));h.addOutput({satoshis:0,lockingScript:e.LockingScript.fromBinary(v.toBinary())})}for(var g=new Map,y=0;y<h.inputs.length;y++)g.set(y,c);return Promise.resolve(o.signAllInputs(h,g)).then(function(e){if(e.error)throw f(e.error);var r=h.toHex();return!1===(null==n?void 0:n.broadcast)?{rawtx:r}:Promise.resolve(o.submitRawTx(r,n)).then(function(e){var r=e.ticketId;if(!r)throw f("Failed to broadcast transaction");return{ticketId:r}})})}var t=v-d-u,i=function(){if(t>0){var e=h.addOutput;return Promise.resolve(o.createInscription(m,t,a)).then(function(r){e.call(h,r)})}}();return i&&i.then?i.then(r):r()}var t=function(){if(u>0){var e=h.addOutput;return Promise.resolve(o.createInscription(a.feeAddress,u,a)).then(function(r){e.call(h,r)})}}();return t&&t.then?t.then(r):r()}var s=V(r,function(e){var r=h.addOutput;return Promise.resolve(o.createInscription(e.address,o.toAtomicAmount(e.amount),a)).then(function(e){r.call(h,e)})});return s&&s.then?s.then(i):i()}var u=void 0!==r.find(function(e){return e.address===a.burnAddress})?0:null==(i=a.fees.find(function(e){return d>=e.min&&d<=e.max}))?void 0:i.fee;if(void 0===u)throw f("Fee ranges inadequate");var h=new e.Transaction(1,[],[],0),v=0,m="",p=R(function(){return v<d+u},void 0,function(){function r(r){return Promise.resolve(o.fetchRawTx(n.txid)).then(function(r){if(!r)throw f("Failed to fetch source transaction: "+n.txid+"_"+n.vout);m=m||n.owners[0],h.addInput({sourceTXID:n.txid,sourceOutputIndex:n.vout,sourceTransaction:r,unlockingScript:new e.UnlockingScript}),v+=n.data.bsv21.amt})}var n=t.shift(),i=function(){if(!n)return Promise.resolve(o.getBalance(l)).then(function(e){throw f("Insufficient MNEE balance. Max transfer amount is "+o.fromAtomicAmount(e.amount-u))})}();return i&&i.then?i.then(r):r()});return p&&p.then?p.then(s):s()})}var s=o.mneeConfig;return s?a(s):Promise.resolve(o.getCosignerConfig()).then(a)},function(e){throw T(e)&&S(e,"transfer tokens"),e}))}catch(e){return Promise.reject(e)}},t.submitRawTx=function(r,t){void 0===t&&(t={broadcast:!0,callbackUrl:void 0});try{var n=this;return Promise.resolve(M(function(){var i,a,s,u;if(!1!==(null==(i=t)?void 0:i.broadcast)&&(t=o({},t,{broadcast:!0})),null!=(a=t)&&a.callbackUrl&&!1===(null==(s=t)?void 0:s.broadcast))throw f("Callback URL cannot be provided when broadcast is false");if(!r)throw f("Raw transaction is required");var c=e.Transaction.fromHex(r);if(null==(u=t)||!u.broadcast)return{rawtx:c.toHex()};var d=e.Utils.toBase64(c.toBinary()),l=JSON.stringify(t?{rawtx:d,callback_url:t.callbackUrl}:{rawtx:d});return Promise.resolve(fetch(n.mneeApi+"/v2/transfer?auth_token="+n.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:l})).then(function(e){if(!e.ok)throw f("Failed to submit transaction");return Promise.resolve(e.text()).then(function(e){return{ticketId:e}})})},function(e){throw T(e)&&S(e,"submit raw transaction"),e}))}catch(e){return Promise.reject(e)}},t.getTxStatus=function(e){try{var r=this;return Promise.resolve(M(function(){if(!e)throw f("Ticket ID is required");return Promise.resolve(fetch(r.mneeApi+"/v2/ticket?ticketID="+e+"&auth_token="+r.mneeApiKey,{method:"GET",headers:{"Content-Type":"application/json"}})).then(function(e){if(!e.ok)throw f("Failed to get transaction status");return Promise.resolve(e.json())})},function(e){throw T(e)&&S(e,"get transaction status"),e}))}catch(e){return Promise.reject(e)}},t.getBalance=function(e){try{var r=this;if(!x(e))throw f("Invalid Bitcoin address: "+e);return Promise.resolve(M(function(){function t(t){if(!t)throw f("Config not fetched");return Promise.resolve(fetch(r.mneeApi+"/v2/balance?auth_token="+r.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify([e])})).then(function(r){if(!r.ok)throw f("Failed to get transaction status: "+r.status);return Promise.resolve(r.json()).then(function(r){return r&&0!==r.length?{address:r[0].address,amount:r[0].amt,decimalAmount:r[0].precised}:{address:e,amount:0,decimalAmount:0}})})}var n=r.mneeConfig;return n?t(n):Promise.resolve(r.getCosignerConfig()).then(t)},function(e){throw T(e)&&S(e,"fetch balance"),e}))}catch(e){return Promise.reject(e)}},t.getBalances=function(e){try{var r=this;if(!Array.isArray(e))throw f("Addresses must be an array");var t=e.filter(function(e){return x(e)});if(0===t.length)throw f("You must pass at least 1 valid address");var n=e.length-t.length;return n>0&&console.warn("[33m"+n+" invalid bitcoin addresses will be ignored[0m"),Promise.resolve(M(function(){function e(e){if(!e)throw f("Config not fetched");return Promise.resolve(fetch(r.mneeApi+"/v2/balance?auth_token="+r.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).then(function(e){if(!e.ok)throw f("Failed to get transaction status: "+e.status);return Promise.resolve(e.json()).then(function(e){if(!e||0===e.length)return t.map(function(e){return{address:e,amount:0,decimalAmount:0}});var r=new Map;return e.forEach(function(e){r.set(e.address,{address:e.address,amount:e.amt,decimalAmount:e.precised})}),t.map(function(e){return r.has(e)?r.get(e):{address:e,amount:0,decimalAmount:0}})})})}var n=r.mneeConfig;return n?e(n):Promise.resolve(r.getCosignerConfig()).then(e)},function(e){throw T(e)&&S(e,"fetch balances"),e}))}catch(e){return Promise.reject(e)}},t.processMneeValidation=function(r,t,n){var o=this;try{var a=r.id("hex")===t.tokenId.split("_")[0],s=r.outputs.map(function(e){return e.lockingScript}),u=w(s),c=r.outputs.map(function(r,t){var n,i=u[t],o=P(r.lockingScript),a=null;if(null!=o&&null!=(n=o.file)&&n.content)try{var s=e.Utils.toUTF8(o.file.content);s&&(a=JSON.parse(s))}catch(e){}return{index:t,address:null==i?void 0:i.address,cosigner:null==i?void 0:i.cosigner,inscription:a}}),d=c.some(function(e){return e.cosigner===t.approver});if(c.find(function(e){return""!==e.cosigner&&e.cosigner!==t.approver}))throw f("Invalid cosigner detected");var l=c.filter(function(e){if(!e.inscription)return!1;var r=e.inscription;if("bsv-20"!==r.p)return!1;if(!a&&r.id!==t.tokenId)throw f("Invalid token ID: "+r.id);var n=parseInt(r.amt,10);if(isNaN(n)||n<=0)throw f("Invalid MNEE amount: "+r.amt);return!0});if(0===l.length)throw f("No valid MNEE inscriptions found in transaction");var h=new Set(l.map(function(e){return e.inscription.op})),v=h.has("burn"),m=l.some(function(e){var r;return"transfer"===e.inscription.op&&"redeem"===(null==(r=e.inscription.metadata)?void 0:r.action)}),g=m&&c.some(function(e){return e.address===y||e.address===p});if((h.has("transfer")&&!m||v||m&&!g)&&!d)throw f("Cosigner not found in transaction with transfer/burn/redeem operation");var b=l.filter(function(e){var r=e.inscription;return"transfer"===r.op||"burn"===r.op||"deploy+mint"===r.op});if(n)for(var x,I=[].concat(b),A=function(){var e=x.value,r=I.findIndex(function(r){return r.address===e.address&&r.inscription.amt===o.toAtomicAmount(e.amount).toString()});if(-1===r)throw f("No matching output found for "+e.address+" with amount "+e.amount);I.splice(r,1)},O=i(n);!(x=O()).done;)A();return!0}catch(e){return T(e)&&S(e,"validate Mnee transaction"),!1}},t.validateMneeTx=function(r,t){try{var n=this;return Promise.resolve(M(function(){function i(i){var o;if(!i)throw f("Config not fetched");var a=e.Transaction.fromHex(r);if(!n.processMneeValidation(a,i,t))return!1;var s=a.id("hex")===i.tokenId.split("_")[0],u=function(){if(!s)return Promise.resolve(n.processTransactionInputs(a,i)).then(function(e){var r=n.processTransactionOutputs(a,i);if(e.total!==r.total)return o=1,!1})}();return u&&u.then?u.then(function(e){return!o||e}):!o||u}var o=n.mneeConfig;return o?i(o):Promise.resolve(n.getCosignerConfig()).then(i)},function(e){return T(e)&&S(e,"validate Mnee transaction"),!1}))}catch(e){return Promise.reject(e)}},t.getMneeSyncs=function(e,r,t,n){try{var i=this;return Promise.resolve(M(function(){var o=Array.isArray(e)?e:[e];return Promise.resolve(fetch(i.mneeApi+"/v1/sync?auth_token="+i.mneeApiKey+(r?"&from="+r:"")+(t?"&limit="+t:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){return o.map(function(r){var t=e.filter(function(e){return e.senders.includes(r)||e.receivers.includes(r)});return{address:r,syncs:t}})})})},function(e){throw T(e)&&S(e,"fetch syncs"),e}))}catch(e){return Promise.reject(e)}},t.getRecentTxHistory=function(e,r,t,n){try{var o=this;if(!x(e))throw f("Invalid Bitcoin address: "+e);if(void 0!==r&&("number"!=typeof r||r<0||!Number.isFinite(r)))throw f("Invalid fromScore: "+r+". Must be a positive number or 0");if(void 0!==t&&("number"!=typeof t||t<=0||!Number.isInteger(t)))throw f("Invalid limit: "+t+". Must be a positive integer");if(void 0!==n&&"asc"!==n&&"desc"!==n)throw f("Invalid order: "+n+". Must be 'asc' or 'desc'");return Promise.resolve(M(function(){function a(a){if(!a)throw f("Config not fetched");return Promise.resolve(o.getMneeSyncs(e,r,t,n)).then(function(n){var o=n[0].syncs;if(!o||0===o.length)return{address:e,history:[],nextScore:r||0};for(var s,u=[],c=i(o);!(s=c()).done;){var f=b(s.value,e,a);f&&u.push(f)}var d=u.length>0?u[u.length-1].score:r||0;return t&&u.length>t?{address:e,history:u.slice(0,t),nextScore:d}:{address:e,history:u,nextScore:d}})}var s=o.mneeConfig;return s?a(s):Promise.resolve(o.getCosignerConfig()).then(a)},function(e){throw T(e)&&S(e,"fetch transaction history"),e}))}catch(e){return Promise.reject(e)}},t.getRecentTxHistories=function(e){try{var r=this;if(!Array.isArray(e))throw f("Parameters must be an array");if(0===e.length)throw f("You must pass at least 1 address parameter");var t=e.filter(function(e){return e&&e.address&&x(e.address)});if(0===t.length)throw f("You must pass at least 1 valid address");var n=e.length-t.length;n>0&&console.warn("[33m"+n+" invalid bitcoin addresses will be ignored[0m");for(var o,a=i(t);!(o=a()).done;){var s=o.value;if(void 0!==s.fromScore&&("number"!=typeof s.fromScore||s.fromScore<0||!Number.isFinite(s.fromScore)))throw f("Invalid fromScore for address "+s.address+": "+s.fromScore+". Must be a positive number or 0");if(void 0!==s.limit&&("number"!=typeof s.limit||s.limit<=0||!Number.isInteger(s.limit)))throw f("Invalid limit for address "+s.address+": "+s.limit+". Must be a positive integer");if(void 0!==s.order&&"asc"!==s.order&&"desc"!==s.order)throw f("Invalid order for address "+s.address+": "+s.order+". Must be 'asc' or 'desc'")}return Promise.resolve(M(function(){function e(e){if(!e)throw f("Config not fetched");var n={};t.forEach(function(e){var r=(e.fromScore||0)+":"+(e.limit||100)+":"+(e.order||"default");n[r]||(n[r]=[]),n[r].push(e)});var o=Object.entries(n).map(function(t){var n=t[0],o=t[1];try{var a=n.split(":"),s=a[1],u=a[2],c=Number(a[0]),f=Number(s),d="default"===u?void 0:u,l=o.map(function(e){return e.address});return Promise.resolve(r.getMneeSyncs(l,c,f,d)).then(function(r){return r.map(function(r){var t=r.address,n=r.syncs,a=o.find(function(e){return e.address===t});if(!n||0===n.length)return{address:t,history:[],nextScore:(null==a?void 0:a.fromScore)||0};for(var s,u=[],c=new Set,f=i(n);!(s=f()).done;){var d=b(s.value,t,e);d&&!c.has(d.txid)&&(c.add(d.txid),u.push(d))}var l=u.length>0?u[u.length-1].score:(null==a?void 0:a.fromScore)||0,h=null==a?void 0:a.limit;return h&&u.length>h?{address:t,history:u.slice(0,h),nextScore:l}:{address:t,history:u,nextScore:l}})})}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(o)).then(function(e){return e.flat()})}var n=r.mneeConfig;return n?e(n):Promise.resolve(r.getCosignerConfig()).then(e)},function(e){throw T(e)&&S(e,"fetch transaction histories"),e}))}catch(e){return Promise.reject(e)}},t.parseInscriptionData=function(r){try{var t,n=P(r),i=null==n||null==(t=n.file)?void 0:t.content;if(!i)return null;var o=e.Utils.toUTF8(i);return o?JSON.parse(o):null}catch(e){return T(e)&&S(e,"parse inscription data"),null}},t.determineEnvironment=function(e,r,t,n){return e===m.split("_")[0]?"production":e===g.split("_")[0]?"sandbox":r===m&&"020a177d6a5e6f3a8689acd2e313bd1cf0dcf5a243d1cc67b7218602aee9e04b2f"===t?"production":r===g&&"02bed35e894cc41cc9879b4002ad03d33533b615c1b476068c8dd6822a09f93f6c"===t?"sandbox":""===t&&n===p?"production":"sandbox"},t.determineTransactionType=function(e,r,t,n,i){return"burn"===e?"burn":"deploy+mint"===e||r===n?t===i.split("_")[0]?"deploy":"mint":r===p||r===y?"mint":"transfer"},t.processTransactionInputs=function(e,r){try{var t,n,o=this,a=e.id("hex"),s=new Array(e.inputs.length),u=BigInt(0),c=new D(3,334),f=e.inputs.map(function(e,r){try{return Promise.resolve(e.sourceTXID?M(function(){return Promise.resolve(c.execute(function(){return o.fetchRawTx(e.sourceTXID)})).then(function(e){return{index:r,sourceTx:e}})},function(e){return T(e)&&S(e,"fetch source transaction"),{index:r,sourceTx:null}}):{index:r,sourceTx:null})}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(f)).then(function(c){for(var f,d=i(c);!(f=d()).done;){var l=f.value,h=l.index,v=l.sourceTx,m=e.inputs[h];if(v&&m.sourceTXID){var p=v.outputs[m.sourceOutputIndex],g=w([p.lockingScript])[0],y=o.parseInscriptionData(p.lockingScript),P={address:null==g?void 0:g.address,amount:y?parseInt(y.amt):0,satoshis:Number(p.satoshis),inscription:y,cosigner:g};s[h]=P,y&&g&&(u+=BigInt(y.amt),t||(t=o.determineEnvironment(a,y.id,g.cosigner||"",g.address||"")),n||(n=o.determineTransactionType(y.op,g.address||"",a,r.mintAddress,r.tokenId)))}else s[h]={address:void 0,amount:0,satoshis:0,inscription:null,cosigner:void 0}}return{inputs:s,total:u,environment:t,type:n}})}catch(e){return Promise.reject(e)}},t.processTransactionOutputs=function(e,r){for(var t,n,i=e.id("hex"),o=[],a=BigInt(0),s=0;s<e.outputs.length;s++){var u=e.outputs[s],c=w([u.lockingScript])[0],f=this.parseInscriptionData(u.lockingScript),d={address:null==c?void 0:c.address,amount:f?parseInt(f.amt):0,satoshis:Number(u.satoshis),inscription:f,cosigner:c};if(o.push(d),f&&c){a+=BigInt(f.amt),t=this.determineEnvironment(i,f.id,c.cosigner||"",c.address||"");var l=this.determineTransactionType(f.op,c.address||"",i,r.mintAddress,r.tokenId);"burn"===l||"deploy"===l?n=l:n||(n=l)}}return{outputs:o,total:a,environment:t,type:n}},t.validateTransaction=function(e,r,t,n,i){return!!this.processMneeValidation(r,e)&&("deploy"===t||n===i)},t.buildParseResponse=function(e,r,t,n,i,a,s,u){var c={txid:e,environment:r,type:t,inputs:n.inputs.filter(function(e){return e.inscription&&e.address}).map(function(e){return{address:e.address,amount:e.amount}}),outputs:i.outputs.filter(function(e){return e.inscription&&e.address}).map(function(e){return{address:e.address,amount:e.amount}}),isValid:a,inputTotal:n.total.toString(),outputTotal:i.total.toString()};return null!=u&&u.includeRaw?o({},c,{raw:{txHex:s.toHex(),inputs:s.inputs.map(function(e,r){var t,i=n.inputs[r];return{txid:e.sourceTXID||"",vout:e.sourceOutputIndex,scriptSig:(null==(t=e.unlockingScript)?void 0:t.toHex())||"",sequence:e.sequence||4294967295,address:null==i?void 0:i.address,satoshis:(null==i?void 0:i.satoshis)||0,tokenData:null!=i&&i.inscription?{amount:i.amount}:void 0}}),outputs:s.outputs.map(function(e,r){var t=i.outputs[r];return{value:Number(e.satoshis),scriptPubKey:e.lockingScript.toHex(),address:null==t?void 0:t.address,tokenData:null!=t&&t.inscription?{amount:t.amount}:void 0}})}}):c},t.parseTransaction=function(e,r,t){try{var n=this,i=e.id("hex");return Promise.resolve(n.processTransactionInputs(e,r)).then(function(o){var a=n.processTransactionOutputs(e,r),s=a.environment||o.environment||"sandbox",u=a.type||o.type||"transfer";"transfer"===u&&o.inputs.some(function(e){return e.inscription&&(e.address===p||e.address===y)})&&(u="mint"),"transfer"!==u&&"mint"!==u||a.outputs.some(function(e){var r;return"redeem"===(null==(r=e.inscription)||null==(r=r.metadata)?void 0:r.action)})&&(u="redeem"),0===o.inputs.filter(function(e){return e.inscription}).length&&o.inputs.length>0&&(u="deploy");var c=n.validateTransaction(r,e,u,o.total,a.total);return n.buildParseResponse(i,s,u,o,a,c,e,t)})}catch(e){return Promise.reject(e)}},t.parseTx=function(e,r){try{var t=function(t){if(!t)throw f("Config not fetched");return Promise.resolve(n.fetchRawTx(e)).then(function(e){if(!e)throw f("Failed to fetch transaction");return Promise.resolve(n.parseTransaction(e,t,r))})},n=this;if(!e||"string"!=typeof e||""===e.trim()||!/^[a-fA-F0-9]{64}$/.test(e))throw f("A valid transaction ID is required");var i=n.mneeConfig;return Promise.resolve(i?t(i):Promise.resolve(n.getCosignerConfig()).then(t))}catch(e){return Promise.reject(e)}},t.parseTxFromRawTx=function(r,t){try{var n=function(e){if(!e)throw f("Config not fetched");return Promise.resolve(i.parseTransaction(o,e,t))},i=this;if(!r||"string"!=typeof r||""===r.trim())throw f("A valid raw transaction is required");if(!function(r){try{return!!/^[a-fA-F0-9]+$/.test(r)&&(e.Transaction.fromHex(r),!0)}catch(e){return!1}}(r))throw f("Invalid raw transaction hex");var o=e.Transaction.fromHex(r),a=i.mneeConfig;return Promise.resolve(a?n(a):Promise.resolve(i.getCosignerConfig()).then(n))}catch(e){return Promise.reject(e)}},t.parseInscription=function(e){return P(e)},t.parseCosignerScripts=function(e){return w(e)},t.validateUniqueInputs=function(e){for(var r,t=new Set,n=i(e);!(r=n()).done;){var o=r.value,a=o.txid+":"+o.vout;if(t.has(a))return{error:"Duplicate input detected: "+a+". Each UTXO can only be spent once."};t.add(a)}return{}},t.addInputsToTransaction=function(r,t,n){try{var i,o=function(e){return i?e:{tokensIn:s}},a=this,s=0,u=K(t,function(o){var u=t[o];return Promise.resolve(a.fetchRawTx(u.txid)).then(function(t){if(!t)return i=1,{tokensIn:0,error:"Failed to fetch source transaction: "+u.txid+"_"+u.vout};var c=t.outputs[u.vout];if(!c)return i=1,{tokensIn:0,error:"Output "+u.vout+" not found in transaction "+u.txid};var f=a.parseInscriptionData(c.lockingScript);if(!f)return i=1,{tokensIn:0,error:"No inscription found in output "+u.txid+":"+u.vout};var d=parseInt(f.amt);s+=d,n.set(o,e.PrivateKey.fromWif(u.wif)),r.addInput({sourceTXID:u.txid,sourceOutputIndex:u.vout,sourceTransaction:t,unlockingScript:new e.UnlockingScript})})},function(){return i});return Promise.resolve(u&&u.then?u.then(o):o(u))}catch(e){return Promise.reject(e)}},t.calculateTransferMultiFee=function(e,r,t,n,o,a){for(var s,u=this,c=0,f=-1,d=function(){var s;f=c;var d=e-r-c,l=0;if(d>0)if("string"==typeof t)n.has(t)||(l=d);else if(Array.isArray(t))for(var h,v=i(t);!(h=v()).done;){var m=h.value,p=u.toAtomicAmount(m.amount);n.has(m.address)||(l+=p)}else l=0;var g=r+l,y=void 0!==a.find(function(e){return e.address===o.burnAddress})?0:null==(s=o.fees.find(function(e){return g>=e.min&&g<=e.max}))?void 0:s.fee;if(void 0===y)return{v:{fee:0,error:"Fee ranges inadequate"}};c=y};c!==f;)if(s=d())return s.v;return{fee:c}},t.addChangeOutputs=function(e,r,t,n,o,a,s,u){try{var c,f=function(e){return c?e:{}},d=this;if(r<=0)return Promise.resolve({});var l=function(){if("string"==typeof t){var f=e.addOutput;return Promise.resolve(d.createInscription(t,r,o)).then(function(r){f.call(e,r)})}return function(){if(!Array.isArray(t)){var f=n.get(0).toAddress(),l=e.addOutput;return Promise.resolve(d.createInscription(f,r,o)).then(function(r){l.call(e,r)})}if(0===t.length)return c=1,{error:"Change address array cannot be empty. Provide at least one change output or use a single address string."};var h=t.map(function(e){return{address:e.address,amount:d.toAtomicAmount(e.amount)}}),v=h.reduce(function(e,r){return e+r.amount},0);if(v!==r){var m=d.fromAtomicAmount(r),p=d.fromAtomicAmount(v),g={error:"Change amounts must sum to "+m+" ("+r+" atomic units). Total inputs: "+d.fromAtomicAmount(a)+" - total outputs: "+d.fromAtomicAmount(s)+" - fee: "+d.fromAtomicAmount(u)+" = "+m+". Your change outputs sum to "+p+" ("+v+" atomic units)."};return c=1,g}for(var y,P=i(t);!(y=P()).done;){var w=y.value;if(w.amount<=0)return c=1,{error:"Invalid change amount: "+w.amount+". Must be positive."}}var b=V(h,function(r){var t=e.addOutput;return Promise.resolve(d.createInscription(r.address,r.amount,o)).then(function(r){t.call(e,r)})});return b&&b.then?b.then(function(){}):void 0}()}();return Promise.resolve(l&&l.then?l.then(f):f(l))}catch(e){return Promise.reject(e)}},t.signAllInputs=function(r,t){try{var n,o=function(t){if(n)return t;for(var o,a=i(c);!(o=a()).done;){var s=o.value;r.inputs[s.inputIndex].unlockingScript=(new e.Script).writeBin(e.Utils.toArray(s.sig,"hex")).writeBin(e.Utils.toArray(s.pubKey,"hex"))}return{}},a=this,s=r.inputs.map(function(r,n){var i,o;if(!r.sourceTXID)throw f("Source TXID is undefined");return{prevTxid:r.sourceTXID,outputIndex:r.sourceOutputIndex,inputIndex:n,address:t.get(n).toAddress(),script:null==(i=r.sourceTransaction)?void 0:i.outputs[r.sourceOutputIndex].lockingScript.toHex(),satoshis:(null==(o=r.sourceTransaction)?void 0:o.outputs[r.sourceOutputIndex].satoshis)||1,sigHashType:e.TransactionSignature.SIGHASH_ALL|e.TransactionSignature.SIGHASH_ANYONECANPAY|e.TransactionSignature.SIGHASH_FORKID}}),u=r.toHex(),c=[],d=V(t.entries(),function(e){var r=e[0];return Promise.resolve(a.getSignatures({rawtx:u,sigRequests:[s[r]]},e[1])).then(function(e){if(null==e||!e.sigResponses)return n=1,{error:"Failed to get signatures for input "+r};c.push.apply(c,e.sigResponses)})},function(){return n});return Promise.resolve(d&&d.then?d.then(o):o(d))}catch(e){return Promise.reject(e)}},t.validateTokenConservation=function(e,r){for(var t=0,n=0;n<e.outputs.length;n++){var i=this.parseInscriptionData(e.outputs[n].lockingScript);i&&(t+=parseInt(i.amt))}return r!==t?{error:"Token conservation violation! Inputs ("+this.fromAtomicAmount(r)+") do not equal outputs ("+this.fromAtomicAmount(t)+"). This would "+(r>t?"burn":"create")+" "+this.fromAtomicAmount(Math.abs(r-t))+" tokens."}:{}},t.transferMulti=function(r,t){try{var n=this;return Promise.resolve(M(function(){function o(o){if(!o)throw f("Config not fetched");var a=function(e){for(var r,t=i(e.recipients);!(r=t()).done;){var n=r.value;if(!n.address||!n.amount)return{isValid:!1,error:"Invalid recipient: "+JSON.stringify(n)+". Missing required fields: address, amount"};if("number"!=typeof n.amount||isNaN(n.amount)||!isFinite(n.amount))return{isValid:!1,error:"Invalid amount for "+n.address+": amount must be a valid number"};if(n.amount<v)return{isValid:!1,error:"Invalid amount for "+n.address+": minimum transfer amount is "+v+" MNEE"};if(!x(n.address))return{isValid:!1,error:"Invalid recipient address: "+n.address}}for(var o,a=i(e.inputs);!(o=a()).done;){var s=o.value;if(!s.txid||"number"!=typeof s.vout||!s.wif)return{isValid:!1,error:"Invalid input: "+JSON.stringify(s)+". Missing required fields: txid, vout, wif"};var u=I(s.wif);if(!u.isValid)return{isValid:!1,error:"Invalid WIF key: "+s.wif+" for input "+s.txid+":"+s.vout};var c=u.privateKey.toAddress();if(!x(c))return{isValid:!1,error:"Invalid input address: "+c}}if(e.changeAddress&&Array.isArray(e.changeAddress))for(var f,d=i(e.changeAddress);!(f=d()).done;){var l=f.value;if(l.amount<v)return{isValid:!1,error:"Invalid amount for "+l.address+": minimum transfer amount is "+v+" MNEE"};if(!x(l.address))return{isValid:!1,error:"Invalid change address: "+l.address}}return{isValid:!0}}(r);if(!a.isValid)throw f(a.error||"Invalid transfer options");var s=r.recipients.reduce(function(e,r){return e+r.amount},0);if(s<=0)throw f("Invalid amount");var u=n.toAtomicAmount(s),c=n.validateUniqueInputs(r.inputs);if(c.error)throw f(c.error);var d=new e.Transaction(1,[],[],0),l=new Map;return Promise.resolve(n.addInputsToTransaction(d,r.inputs,l)).then(function(i){function a(){function i(){return Promise.resolve(n.addChangeOutputs(d,s-u-p,r.changeAddress,l,o,s,u,p)).then(function(r){if(r.error)throw f(r.error);if(null!=t&&t.extraData){var i=Array.isArray(t.extraData)?t.extraData:[t.extraData];if(0===i.length)throw f("extraData must contain at least one item");for(var o=[],a=0;a<i.length;a++){var u=i[a];if(!u||"string"!=typeof u.data)throw f("Invalid extraData at index "+a+": data must be a string");if("utf8"===u.type){var c=Buffer.from(u.data,"utf8");if(0===c.length)throw f("extraData at index "+a+" is empty after UTF-8 encoding");o.push(c)}else{if("hex"!==u.type)throw f("Unsupported extraData type at index "+a+": "+u.type);var h=u.data.trim();if(!h)throw f("extraData at index "+a+" is empty");if(!/^[0-9a-fA-F]+$/.test(h)||h.length%2!=0)throw f("extraData at index "+a+' is not valid hex or has odd length: "'+h+'"');var v=Buffer.from(h,"hex");if(0===v.length)throw f("extraData at index "+a+" decoded to an empty buffer");o.push(v)}}var m=o.reduce(function(e,r){return e+r.length},0);if(m>512)throw f("extraData is too large: "+m+" bytes (max allowed is 512 bytes)");var p=new e.Script;p.writeOpCode(e.OP.OP_0),p.writeOpCode(e.OP.OP_RETURN);for(var g=0,y=o;g<y.length;g++)p.writeBin(Array.from(y[g]));d.addOutput({satoshis:0,lockingScript:e.LockingScript.fromBinary(p.toBinary())})}return Promise.resolve(n.signAllInputs(d,l)).then(function(e){if(e.error)throw f(e.error);var r=n.validateTokenConservation(d,s);if(r.error)throw f(r.error);var i=d.toHex();return null!=t&&t.broadcast?Promise.resolve(n.submitRawTx(i,t)).then(function(e){var r=e.ticketId;if(!r)throw f("Failed to broadcast transaction");return{ticketId:r}}):{rawtx:i}})})}var a=function(){if(p>0){var e=d.addOutput;return Promise.resolve(n.createInscription(o.feeAddress,p,o)).then(function(r){e.call(d,r)})}}();return a&&a.then?a.then(i):i()}if(i.error)throw f(i.error);for(var s=i.tokensIn,c=new Set,h=0;h<r.inputs.length;h++){var v=e.PrivateKey.fromWif(r.inputs[h].wif);c.add(v.toAddress())}var m=n.calculateTransferMultiFee(s,u,r.changeAddress,c,o,r.recipients);if(m.error)throw f(m.error);var p=m.fee;if(s<u+p)throw f("Insufficient tokens. Have: "+n.fromAtomicAmount(s)+", Need: "+n.fromAtomicAmount(u+p)+" (including fee: "+n.fromAtomicAmount(p)+")");var g=V(r.recipients,function(e){var r=d.addOutput;return Promise.resolve(n.createInscription(e.address,n.toAtomicAmount(e.amount),o)).then(function(e){r.call(d,e)})});return g&&g.then?g.then(a):a()})}var a=n.mneeConfig;return a?o(a):Promise.resolve(n.getCosignerConfig()).then(o)},function(e){throw T(e)&&S(e,"multi-source transfer"),e}))}catch(e){return Promise.reject(e)}},r}();function L(e,r,t){if(!e.s){if(t instanceof G){if(!t.s)return void(t.o=L.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(L.bind(null,e,r),L.bind(null,e,2));e.s=r,e.v=t;const n=e.o;n&&n(e)}}var G=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{L(n,1,o(this.v))}catch(e){L(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?L(n,1,r?r(i):i):t?L(n,1,t(i)):L(n,2,i)}catch(e){L(n,2,e)}},n},e}();function z(e){return e instanceof G&&1&e.s}function X(e,r,t){for(var n;;){var i=e();if(z(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!z(o)){n=1;break}o=o.s}if(r){var a=r();if(a&&a.then&&!z(a)){n=2;break}}}var s=new G,u=L.bind(null,s,2);return(0===n?i.then(f):1===n?o.then(c):a.then(d)).then(void 0,u),s;function c(n){o=n;do{if(r&&(a=r())&&a.then&&!z(a))return void a.then(d).then(void 0,u);if(!(i=e())||z(i)&&!i.v)return void L(s,1,o);if(i.then)return void i.then(f).then(void 0,u);z(o=t())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=t())&&o.then?o.then(c).then(void 0,u):c(o):L(s,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):L(s,1,o)}}var W=/*#__PURE__*/function(){function r(r,n){if(this.masterKey=void 0,this.derivationPath=void 0,this.cacheSize=void 0,this.cache=void 0,!t.validateMnemonic(r))throw f("Invalid mnemonic phrase");var i=e.Mnemonic.fromString(r).toSeed();this.masterKey=e.HD.fromSeed(i),this.derivationPath=n.derivationPath,this.cacheSize=n.cacheSize||1e3,this.cache=new Map}r.generateMnemonic=function(){return t.generateMnemonic()},r.isValidMnemonic=function(e){return t.validateMnemonic(e)};var n=r.prototype;return n.deriveAddress=function(e,r){void 0===r&&(r=!1);var t=this.derivationPath+"/"+(r?1:0)+"/"+e;if(this.cache.has(t))return this.cache.get(t);var n=this.masterKey.derive(t).privKey;if(!n)throw f("Failed to derive private key for path: "+t);var i={address:n.toAddress(),privateKey:n.toWif(),path:t};return this.cache.size<this.cacheSize&&this.cache.set(t,i),i},n.deriveAddresses=function(e,r,t){void 0===t&&(t=!1);try{for(var n=[],i=0;i<r;i++){var o=this.deriveAddress(e+i,t);n.push(o)}return Promise.resolve(n)}catch(e){return Promise.reject(e)}},n.getPrivateKeysForAddresses=function(e,r){for(var t,n=(null==r?void 0:r.maxScanReceive)||1e4,o=(null==r?void 0:r.maxScanChange)||1e4,a=(null==r?void 0:r.scanStrategy)||"parallel",s={},u={},c=new Set(e),d=i(this.cache);!(t=d()).done;){var l=t.value[1];c.has(l.address)&&(s[l.address]=l.privateKey,u[l.address]=l.path,c.delete(l.address))}if(0===c.size)return{privateKeys:s,paths:u};if("sequential"===a){for(var h=0;h<n&&c.size>0;h++){var v=this.deriveAddress(h,!1);c.has(v.address)&&(s[v.address]=v.privateKey,u[v.address]=v.path,c.delete(v.address))}for(var m=0;m<o&&c.size>0;m++){var p=this.deriveAddress(m,!0);c.has(p.address)&&(s[p.address]=p.privateKey,u[p.address]=p.path,c.delete(p.address))}}else for(var g=Math.max(n,o),y=0;y<g&&c.size>0;y++){if(y<n){var P=this.deriveAddress(y,!1);c.has(P.address)&&(s[P.address]=P.privateKey,u[P.address]=P.path,c.delete(P.address))}if(y<o){var w=this.deriveAddress(y,!0);c.has(w.address)&&(s[w.address]=w.privateKey,u[w.address]=w.path,c.delete(w.address))}}if(c.size>0){var b=Array.from(c);throw f("Could not find private keys for "+b.length+" address(es): "+b.join(", ")+". Scanned up to index "+n+" for receive addresses and "+o+" for change addresses.")}return{privateKeys:s,paths:u}},n.getPrivateKeys=function(e,r){return this.getPrivateKeysForAddresses(e,r).privateKeys},n.scanAddressesWithGapLimit=function(e,r){try{var t=function(){var r=function(){if(o){u=0;var r=0,t=X(function(){return r<a&&u<i},function(){return r++},function(){var t=n.deriveAddress(r,!0);return Promise.resolve(e(t.address)).then(function(e){e?(s.change.push(t),u=0):u++})});if(t&&t.then)return t.then(function(){})}}();return r&&r.then?r.then(function(){return s}):s},n=this,i=(null==r?void 0:r.gapLimit)||20,o=!1!==(null==r?void 0:r.scanChange),a=(null==r?void 0:r.maxScan)||1e4,s={receive:[],change:[]},u=0,c=0,f=X(function(){return c<a&&u<i},function(){return c++},function(){var r=n.deriveAddress(c,!1);return Promise.resolve(e(r.address)).then(function(e){e?(s.receive.push(r),u=0):u++})});return Promise.resolve(f&&f.then?f.then(t):t())}catch(e){return Promise.reject(e)}},n.clearCache=function(){this.cache.clear()},n.getCacheSize=function(){return this.cache.size},r}(),Y=/*#__PURE__*/function(){function e(e){this.service=void 0,this._batch=void 0,this.service=new q(e)}var r=e.prototype;return r.validateMneeTx=function(e,r){try{return Promise.resolve(this.service.validateMneeTx(e,r))}catch(e){return Promise.reject(e)}},r.toAtomicAmount=function(e){return this.service.toAtomicAmount(e)},r.fromAtomicAmount=function(e){return this.service.fromAtomicAmount(e)},r.config=function(){try{return Promise.resolve(this.service.getCosignerConfig())}catch(e){return Promise.reject(e)}},r.balance=function(e){try{return Promise.resolve(this.service.getBalance(e))}catch(e){return Promise.reject(e)}},r.balances=function(e){try{return Promise.resolve(this.service.getBalances(e))}catch(e){return Promise.reject(e)}},r.getUtxos=function(e,r,t,n){try{return Promise.resolve(this.service.getUtxos(e,r,t,n))}catch(e){return Promise.reject(e)}},r.getEnoughUtxos=function(e,r){try{return Promise.resolve(this.service.getEnoughUtxos(e,r))}catch(e){return Promise.reject(e)}},r.getAllUtxos=function(e){try{return Promise.resolve(this.service.getAllUtxos(e))}catch(e){return Promise.reject(e)}},r.transfer=function(e,r,t){try{return Promise.resolve(this.service.transfer(e,r,t))}catch(e){return Promise.reject(e)}},r.transferMulti=function(e,r){try{return Promise.resolve(this.service.transferMulti(e,r))}catch(e){return Promise.reject(e)}},r.submitRawTx=function(e,r){try{return Promise.resolve(this.service.submitRawTx(e,r))}catch(e){return Promise.reject(e)}},r.getTxStatus=function(e){try{return Promise.resolve(this.service.getTxStatus(e))}catch(e){return Promise.reject(e)}},r.recentTxHistory=function(e,r,t,n){try{return Promise.resolve(this.service.getRecentTxHistory(e,r,t,n))}catch(e){return Promise.reject(e)}},r.recentTxHistories=function(e){try{return Promise.resolve(this.service.getRecentTxHistories(e))}catch(e){return Promise.reject(e)}},r.parseTx=function(e,r){try{return Promise.resolve(this.service.parseTx(e,r))}catch(e){return Promise.reject(e)}},r.parseTxFromRawTx=function(e,r){try{return Promise.resolve(this.service.parseTxFromRawTx(e,r))}catch(e){return Promise.reject(e)}},r.parseInscription=function(e){return this.service.parseInscription(e)},r.parseCosignerScripts=function(e){return this.service.parseCosignerScripts(e)},r.HDWallet=function(e,r){return new W(e,r)},r.batch=function(){return this._batch||(this._batch=new j(this.service)),this._batch},e}();Y.HDWallet=W,module.exports=Y;
|
|
1
|
+
var e=require("@bsv/sdk");function t(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var r=/*#__PURE__*/t(require("bip39"));function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function i(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function o(){return o=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)({}).hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o.apply(null,arguments)}function s(e){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},s(e)}function a(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(a=function(){return!!e})()}function u(e,t){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},u(e,t)}function c(e){var t="function"==typeof Map?new Map:void 0;return c=function(e){if(null===e||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(e))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return function(e,t,r){if(a())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,t);var i=new(e.bind.apply(e,n));return r&&u(i,r.prototype),i}(e,arguments,s(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),u(r,e)},c(e)}function f(e){var t=new Error(e);return t.stack=void 0,t}var d=/*#__PURE__*/function(){function t(){}var r=t.prototype;return r.lock=function(t,r){var n=[];if("string"==typeof t){var i=e.Utils.fromBase58Check(t);if(0!==i.prefix[0]&&111!==i.prefix[0])throw f("only P2PKH is supported");n=i.data}else n=t;var o=new e.LockingScript;return o.writeOpCode(e.OP.OP_DUP).writeOpCode(e.OP.OP_HASH160).writeBin(n).writeOpCode(e.OP.OP_EQUALVERIFY).writeOpCode(e.OP.OP_CHECKSIGVERIFY).writeBin(r.encode(!0)).writeOpCode(e.OP.OP_CHECKSIG),o},r.userUnlock=function(t,r,n,i,o){return void 0===r&&(r="all"),void 0===n&&(n=!1),{sign:function(s,a){try{var u,c,d,h=e.TransactionSignature.SIGHASH_FORKID;"all"===r&&(h|=e.TransactionSignature.SIGHASH_ALL),"none"===r&&(h|=e.TransactionSignature.SIGHASH_NONE),"single"===r&&(h|=e.TransactionSignature.SIGHASH_SINGLE),n&&(h|=e.TransactionSignature.SIGHASH_ANYONECANPAY);var l=s.inputs[a],v=s.inputs.filter(function(e,t){return t!==a}),m=l.sourceTXID?l.sourceTXID:null==(u=l.sourceTransaction)?void 0:u.id("hex");if(!m)throw f("The input sourceTXID or sourceTransaction is required for transaction signing.");if(i||(i=null==(c=l.sourceTransaction)?void 0:c.outputs[l.sourceOutputIndex].satoshis),!i)throw f("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(o||(o=null==(d=l.sourceTransaction)?void 0:d.outputs[l.sourceOutputIndex].lockingScript),!o)throw f("The lockingScript or input sourceTransaction is required for transaction signing.");var p=e.TransactionSignature.format({sourceTXID:m,sourceOutputIndex:l.sourceOutputIndex,sourceSatoshis:i,transactionVersion:s.version,otherInputs:v,inputIndex:a,outputs:s.outputs,inputSequence:l.sequence||4294967295,subscript:o,lockTime:s.lockTime,scope:h}),g=t.sign(e.Hash.sha256(p)),y=new e.TransactionSignature(g.r,g.s,h),P=new e.UnlockingScript;return P.writeBin(y.toChecksigFormat()),P.writeBin(t.toPublicKey().encode(!0)),Promise.resolve(P)}catch(e){return Promise.reject(e)}},estimateLength:function(){return Promise.resolve(182)}}},r.unlock=function(t,r,n,i,o,s){return void 0===n&&(n="all"),void 0===i&&(i=!1),{sign:function(a,u){try{var c,d,h,l=e.TransactionSignature.SIGHASH_FORKID;"all"===n&&(l|=e.TransactionSignature.SIGHASH_ALL),"none"===n&&(l|=e.TransactionSignature.SIGHASH_NONE),"single"===n&&(l|=e.TransactionSignature.SIGHASH_SINGLE),i&&(l|=e.TransactionSignature.SIGHASH_ANYONECANPAY);var v=a.inputs[u],m=a.inputs.filter(function(e,t){return t!==u}),p=v.sourceTXID?v.sourceTXID:null==(c=v.sourceTransaction)?void 0:c.id("hex");if(!p)throw f("The input sourceTXID or sourceTransaction is required for transaction signing.");if(o||(o=null==(d=v.sourceTransaction)?void 0:d.outputs[v.sourceOutputIndex].satoshis),!o)throw f("The sourceSatoshis or input sourceTransaction is required for transaction signing.");if(s||(s=null==(h=v.sourceTransaction)?void 0:h.outputs[v.sourceOutputIndex].lockingScript),!s)throw f("The lockingScript or input sourceTransaction is required for transaction signing.");var g=e.TransactionSignature.format({sourceTXID:p,sourceOutputIndex:v.sourceOutputIndex,sourceSatoshis:o,transactionVersion:a.version,otherInputs:m,inputIndex:u,outputs:a.outputs,inputSequence:v.sequence||4294967295,subscript:s,lockTime:a.lockTime,scope:l}),y=t.sign(e.Hash.sha256(g)),P=new e.TransactionSignature(y.r,y.s,l),w=new e.UnlockingScript;return w.writeBin(P.toChecksigFormat()),w.writeScript(r),Promise.resolve(w)}catch(e){return Promise.reject(e)}},estimateLength:function(){return Promise.resolve(182)}}},t}(),h=function(e){return Buffer.from(e).toString("hex")},l=function(t,r,n,i){void 0===i&&(i=!1);var o="";if(void 0!==(null==r?void 0:r.dataB64)&&void 0!==(null==r?void 0:r.contentType)){var s=h("ord"),a=Buffer.from(r.dataB64,"base64").toString("hex").trim();if(!a)throw f("Invalid file data");var u=h(r.contentType);if(!u)throw f("Invalid media type");o="OP_0 OP_IF "+s+" OP_1 "+u+" OP_0 "+a+" OP_ENDIF"}var c=(o?o+" "+(i?"OP_CODESEPARATOR ":""):"")+t.toASM();if(n&&(!n.app||!n.type))throw f("MAP.app and MAP.type are required fields");if(null!=n&&n.app&&null!=n&&n.type){c=(c?c+" ":"")+"OP_RETURN "+h("1PuQa7K62MiKCtssSLKy1kh56WWU7MtUR5")+" "+h("SET");for(var d=0,l=Object.entries(n);d<l.length;d++){var v=l[d],m=v[0],p=v[1];"cmd"!==m&&(c=c+" "+h(m)+" "+h(p))}}return e.LockingScript.fromASM(c)},v=1e-5,m="ae59f3b898ec61acbdb6cc7a245fabeded0c094bf046f35206a3aec60ef88127_0",p="1inHbiwj2jrEcZPiSYnfgJ8FmS1Bmk4Dh",g="833a7720966a2a435db28d967385e8aa7284b6150ebb39482cc5228b73e1703f_0",y="1AZNdbFYBDFTAEgzZMfPzANxyNrpGJZAUY",P=function(t){for(var r,n,i,o=0;o<t.chunks.length;o++){var s,a=t.chunks[o];o>=2&&3===(null==(s=a.data)?void 0:s.length)&&"ord"==e.Utils.toUTF8(a.data)&&t.chunks[o-1].op==e.OP.OP_IF&&t.chunks[o-2].op==e.OP.OP_FALSE&&(i=o+1)}if(void 0!==i){for(var u={file:{hash:"",size:0,type:""},fields:{}},c=i;c<t.chunks.length;c+=2){var f,d,h=t.chunks[c];if(h.op==e.OP.OP_ENDIF)break;if(h.op>e.OP.OP_16)return;var l=t.chunks[c+1];if(l.op>e.OP.OP_PUSHDATA4)return;if(null==(f=h.data)||!f.length){var v=0;switch(h.op>e.OP.OP_PUSHDATA4&&h.op<=e.OP.OP_16?v=h.op-80:null!=(d=h.data)&&d.length&&(v=h.data[0]),v){case 0:if(u.file.size=(null==(r=l.data)?void 0:r.length)||0,null==(n=l.data)||!n.length)break;u.file.hash=e.Utils.toBase64(e.Hash.sha256(l.data)),u.file.content=l.data;break;case 1:u.file.type=Buffer.from(l.data||[]).toString()}}}return u}},w=function(t){return t.map(function(t){for(var r=t.chunks,n=0;n<=r.length-4;n++){var i,o,s;if(r.length>n+6&&r[0+n].op===e.OP.OP_DUP&&r[1+n].op===e.OP.OP_HASH160&&20===(null==(i=r[2+n].data)?void 0:i.length)&&r[3+n].op===e.OP.OP_EQUALVERIFY&&r[4+n].op===e.OP.OP_CHECKSIGVERIFY&&33===(null==(o=r[5+n].data)?void 0:o.length)&&r[6+n].op===e.OP.OP_CHECKSIG)return{cosigner:e.Utils.toHex(r[5+n].data||[]),address:e.Utils.toBase58Check(r[2+n].data||[],[0])};if(r[0+n].op===e.OP.OP_DUP&&r[1+n].op===e.OP.OP_HASH160&&20===(null==(s=r[2+n].data)?void 0:s.length)&&r[3+n].op===e.OP.OP_EQUALVERIFY&&r[4+n].op===e.OP.OP_CHECKSIG)return{cosigner:"",address:e.Utils.toBase58Check(r[2+n].data||[],[0])}}}).filter(function(e){return void 0!==e})},b=function(t,r,n){var i=t.senders.includes(r)?"send":"receive",o=t.height>0?"confirmed":"unconfirmed";if(!t.rawtx)return null;var s=e.Utils.toArray(t.rawtx,"base64"),a=e.Utils.toHex(s),u=e.Transaction.fromHex(a).outputs.map(function(e){return e.lockingScript}),c=w(u),f=u.map(P),d=c.map(function(e){return e.address}),h=d.indexOf(n.feeAddress),l=t.senders[0],v=0,m=new Map;f.forEach(function(t,i){var o,s=null==t||null==(o=t.file)?void 0:o.content;if(s){var a=e.Utils.toUTF8(s);if(a){var u;try{u=JSON.parse(a)}catch(e){return void console.error("Failed to parse inscription JSON:",e)}if("bsv-20"===u.p&&u.id===n.tokenId){var c=parseInt(u.amt,10);if(!Number.isNaN(c))if(h!==i||l!==r){var f=d[i],p=m.get(f)||0;m.set(f,p+c)}else v+=c}}}});var p=m.get(r)||0;if("send"===i){var g=m.get(l)||0;m.set(l,g-p)}var y=[],b=(y="receive"===i?[{address:l,amount:p}]:Array.from(m.entries()).map(function(e){return{address:e[0],amount:e[1]}}).filter(function(e){return e.address!==r&&e.address!==n.feeAddress&&e.amount>0})).reduce(function(e,t){return e+t.amount},0);return{txid:t.txid,height:t.height,type:i,status:o,amount:b,fee:v,score:t.score,counterparties:y}},x=function(t){try{var r=e.Utils.fromBase58Check(t),n=r.prefix[0];if("number"!=typeof n||![0].includes(n))throw f("Invalid address prefix: "+n);if(20!==r.data.length)throw f("Invalid address payload length: "+r.data.length);return!0}catch(e){return!1}},A=function(t){try{return{isValid:!0,privateKey:e.PrivateKey.fromWif(t)}}catch(e){if(e instanceof Error){var r=e.message.toLowerCase();if(r.includes("invalid base58 character"))return{isValid:!1,error:"Invalid WIF key: contains invalid characters"};if(r.includes("invalid checksum"))return{isValid:!1,error:"Invalid WIF key: checksum verification failed"};if(r.includes("expected base58 string"))return{isValid:!1,error:"Invalid WIF key: must be a valid base58 encoded string"}}return{isValid:!1,error:"Invalid WIF key provided"}}},I=/*#__PURE__*/function(e){function t(t){var r;return(r=e.call(this,t.message)||this).code=void 0,r.hostname=void 0,r.originalError=void 0,r.name="NetworkError",r.code=t.code,r.hostname=t.hostname,r.originalError=t.originalError,r}var r,n;return n=e,(r=t).prototype=Object.create(n.prototype),r.prototype.constructor=r,u(r,n),t}(/*#__PURE__*/c(Error));function T(e){var t,r,n;if(!e)return!1;var i=["ENOTFOUND","ECONNREFUSED","ETIMEDOUT","ECONNRESET","ENETUNREACH","EHOSTUNREACH","EPIPE","ECONNABORTED"];return!!i.includes(e.code)||!(!e.cause||!i.includes(null==(t=e.cause)?void 0:t.code))||!!(null!=(r=e.message)&&r.includes("fetch failed")||null!=(n=e.message)&&n.includes("getaddrinfo"))}function S(e,t){if(T(e)){var r=function(e){if(e.cause&&e.cause.code){var t=e.cause,r="Network connection failed";switch(t.code){case"ENOTFOUND":r="Unable to connect to MNEE network. Please check your internet connection.";break;case"ECONNREFUSED":r="Connection refused by MNEE server. The service may be temporarily unavailable.";break;case"ETIMEDOUT":r="Request timed out. Please check your internet connection and try again.";break;case"ECONNRESET":r="Connection was reset. Please try again.";break;case"ENETUNREACH":case"EHOSTUNREACH":r="Network unreachable. Please check your internet connection.";break;default:r="Network error: "+t.code+". Please check your connection and try again."}return new I({code:t.code,message:r,hostname:t.hostname,originalError:e})}if(e.code){var n="Network error occurred";switch(e.code){case"ENOTFOUND":n="Unable to connect to MNEE network. Please check your internet connection.";break;case"ECONNREFUSED":n="Connection refused by MNEE server. The service may be temporarily unavailable.";break;case"ETIMEDOUT":n="Request timed out. Please check your internet connection and try again.";break;default:n="Network error: "+e.code+". Please check your connection and try again."}return new I({code:e.code,message:n,hostname:e.hostname,originalError:e})}return new I({code:"NETWORK_ERROR",message:"Network error occurred. Please check your internet connection and try again.",originalError:e})}(e);return console.error("Network error during "+t+": "+r.message),r.message}console.error("Failed to "+t+":",e)}var O=["parseOptions"];function k(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}const E="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function C(e,t,r){if(!e.s){if(r instanceof N){if(!r.s)return void(r.o=C.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(C.bind(null,e,t),C.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var N=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var o=1&i?t:r;if(o){try{C(n,1,o(this.v))}catch(e){C(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?C(n,1,t?t(i):i):r?C(n,1,r(i)):C(n,2,i)}catch(e){C(n,2,e)}},n},e}();function j(e){return e instanceof N&&1&e.s}var H=/*#__PURE__*/function(){function e(e){this.service=void 0,this.service=e}var t=e.prototype;return t.getUtxos=function(e,t){void 0===t&&(t={});try{var r=this;if(!Array.isArray(e))throw f("Input must be an array of addresses");var n=[];return Promise.resolve(r.processBatch(e,function(e){try{for(var o,s=[],a=[],u=i(e);!(o=u()).done;){var c=o.value;c&&"string"==typeof c&&""!==c.trim()?/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$|^bc1[a-z0-9]{39,59}$/.test(c)?s.push(c):(a.push(c),n.push({items:[c],error:{message:"Invalid address format: "+c},retryCount:0})):(a.push(c),n.push({items:[c],error:{message:"Invalid address: empty or not a string"},retryCount:0}))}if(!t.continueOnError&&a.length>0)throw new Error(n[0].error.message);return 0===s.length?Promise.resolve([]):Promise.resolve(r.service.getUtxos(s)).then(function(t){return e.map(function(e){return{address:e,utxos:s.includes(e)?t.filter(function(t){return t.owners.includes(e)}):[]}})})}catch(e){return Promise.reject(e)}},t)).then(function(e){return o({},e,{errors:[].concat(e.errors,n),totalErrors:e.errors.length+n.length})})}catch(e){return Promise.reject(e)}},t.getBalances=function(e,t){void 0===t&&(t={});try{var r=this;if(!Array.isArray(e))throw f("Input must be an array of addresses");return Promise.resolve(r.processBatch(e,function(e){try{return Promise.resolve(r.service.getBalances(e))}catch(e){return Promise.reject(e)}},t))}catch(e){return Promise.reject(e)}},t.getTxHistories=function(e,t){void 0===t&&(t={});try{var r=this;if(!Array.isArray(e))throw f("Input must be an array of address history parameters");return Promise.resolve(r.processBatch(e,function(e){try{return Promise.resolve(r.service.getRecentTxHistories(e))}catch(e){return Promise.reject(e)}},t,function(e){return e.address}))}catch(e){return Promise.reject(e)}},t.parseTx=function(e,t){void 0===t&&(t={});try{var r=this;if(!Array.isArray(e))throw f("Input must be an array of transaction IDs");var n=t.parseOptions,i=function(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==t.indexOf(n))continue;r[n]=e[n]}return r}(t,O),s=[];return Promise.resolve(r.processBatch(e,function(e){try{return Promise.resolve(Promise.allSettled(e.map(function(e){try{if(!e||"string"!=typeof e||""===e.trim())throw new Error("Invalid transaction ID: empty or not a string");if(!/^[a-fA-F0-9]{64}$/.test(e))throw new Error("Invalid transaction ID format: "+e);return Promise.resolve(r.service.parseTx(e,n)).then(function(t){return{txid:e,parsed:t}})}catch(e){return Promise.reject(e)}}))).then(function(t){var r=[];if(t.forEach(function(t,n){var o=e[n];if("fulfilled"===t.status)r.push(t.value);else{var a=t.reason instanceof Error?t.reason.message:String(t.reason);s.push({items:[o],error:{message:a},retryCount:i.maxRetries||3})}}),!i.continueOnError&&s.length>0)throw new Error(s[0].error.message);return r})}catch(e){return Promise.reject(e)}},i)).then(function(e){return o({},e,{errors:[].concat(e.errors,s),totalErrors:e.errors.length+s.length})})}catch(e){return Promise.reject(e)}},t.processBatch=function(e,t,r,n){try{var i=this,o=r.chunkSize,s=void 0===o?20:o,a=r.continueOnError,u=void 0!==a&&a,c=r.onProgress,f=r.maxRetries,d=void 0===f?3:f,h=r.retryDelay,l=void 0===h?1e3:h,v=r.requestsPerSecond,m=void 0===v?3:v,p=s>0?s:20,g=m>0?m:3,y=Math.ceil(1e3/g),P=Math.max(1,Math.floor(g)),w=new D(P,y);if(0===e.length)return Promise.resolve({results:[],errors:[],totalProcessed:0,totalErrors:0});var b=[],x=[],A=0,I=i.chunkArray(e,p),T=I.length,S=I.map(function(e){try{return Promise.resolve(k(function(){return Promise.resolve(i.processWithRetry(function(){return t(e)},d,l,w)).then(function(e){return b.push.apply(b,e),A++,c&&c(A,T,x.length),e})},function(r){function o(){if(b.push.apply(b,s),a.length>0){var e=a.map(function(e){var t=e.item;return n?n(t):t}),t=a[0].error;x.push({items:e,error:{message:t.message,code:t.code},retryCount:d})}return A++,c&&c(A,T,x.length),s}if(!u)throw r;var s=[],a=[],f=function(e,t){if("function"==typeof e[E]){var r,n,i,o=function(e){try{for(;!(r=s.next()).done;)if((e=t(r.value))&&e.then){if(!j(e))return void e.then(o,i||(i=C.bind(null,n=new N,2)));e=e.v}n?C(n,1,e):n=e}catch(e){C(n||(n=new N),2,e)}},s=e[E]();if(o(),s.return){var a=function(e){try{r.done||s.return()}catch(e){}return e};if(n&&n.then)return n.then(a,function(e){throw a(e)});a()}return n}if(!("length"in e))throw new TypeError("Object is not iterable");for(var u=[],c=0;c<e.length;c++)u.push(e[c]);return function(e,t){var r,n,i=-1;return function o(s){try{for(;++i<e.length;)if((s=t(i))&&s.then){if(!j(s))return void s.then(o,n||(n=C.bind(null,r=new N,2)));s=s.v}r?C(r,1,s):r=s}catch(e){C(r||(r=new N),2,e)}}(),r}(u,function(e){return t(u[e])})}(e,function(e){var r=k(function(){return Promise.resolve(i.processWithRetry(function(){return t([e])},d,l,w)).then(function(e){e.length>0&&s.push.apply(s,e)})},function(t){var r=t instanceof Error?t:new Error(String(t));a.push({item:e,error:r})});if(r&&r.then)return r.then(function(){})});return f&&f.then?f.then(o):o()}))}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(S)).then(function(){return{results:b,errors:x,totalProcessed:A,totalErrors:x.length}})}catch(e){return Promise.reject(e)}},t.processWithRetry=function(e,t,r,n){try{var i,o,s=function(e){if(i)return e;throw o||new Error("Max retries exceeded")},a=this,u=0,c=function(e,t,r){for(var n;;){var i=e();if(j(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!j(o)){n=1;break}o=o.s}if(t){var s=t();if(s&&s.then&&!j(s)){n=2;break}}}var a=new N,u=C.bind(null,a,2);return(0===n?i.then(f):1===n?o.then(c):s.then(d)).then(void 0,u),a;function c(n){o=n;do{if(t&&(s=t())&&s.then&&!j(s))return void s.then(d).then(void 0,u);if(!(i=e())||j(i)&&!i.v)return void C(a,1,o);if(i.then)return void i.then(f).then(void 0,u);j(o=r())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=r())&&o.then?o.then(c).then(void 0,u):c(o):C(a,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):C(a,1,o)}}(function(){return!i&&u<t},function(){return u++},function(){return k(function(){return Promise.resolve(n.execute(e)).then(function(e){return i=1,e})},function(e){o=e;var n=function(){if(u<t-1)return Promise.resolve(a.delay(r*(u+1))).then(function(){})}();return n&&n.then?n.then(function(){}):void 0})});return Promise.resolve(c&&c.then?c.then(s):s(c))}catch(e){return Promise.reject(e)}},t.chunkArray=function(e,t){for(var r=[],n=Math.max(1,t),i=0;i<e.length;i+=n)r.push(e.slice(i,i+n));return r},t.delay=function(e){return new Promise(function(t){return setTimeout(t,e)})},e}(),D=/*#__PURE__*/function(){function e(e,t){this.maxConcurrent=void 0,this.minDelay=void 0,this.queue=[],this.running=0,this.maxConcurrent=e,this.minDelay=t}var t=e.prototype;return t.execute=function(e){try{var t=this;return Promise.resolve(t.waitForSlot()).then(function(){return function(r,n){try{var i=function(){t.running++;var r=Date.now();return Promise.resolve(e()).then(function(e){var n=Date.now()-r,i=function(){if(n<t.minDelay)return Promise.resolve(t.delay(t.minDelay-n)).then(function(){})}();return i&&i.then?i.then(function(){return e}):e})}()}catch(e){return n(!0,e)}return i&&i.then?i.then(n.bind(null,!1),n.bind(null,!0)):n(!1,i)}(0,function(e,r){if(t.running--,t.processQueue(),e)throw r;return r})})}catch(e){return Promise.reject(e)}},t.waitForSlot=function(){var e=this;return this.running<this.maxConcurrent?Promise.resolve():new Promise(function(t){e.queue.push(t)})},t.processQueue=function(){if(this.queue.length>0&&this.running<this.maxConcurrent){var e=this.queue.shift();e&&e()}},t.delay=function(e){return new Promise(function(t){return setTimeout(t,e)})},e}();function M(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}function U(e,t,r){if(!e.s){if(r instanceof _){if(!r.s)return void(r.o=U.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(U.bind(null,e,t),U.bind(null,e,2));e.s=t,e.v=r;var n=e.o;n&&n(e)}}var _=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var o=1&i?t:r;if(o){try{U(n,1,o(this.v))}catch(e){U(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?U(n,1,t?t(i):i):r?U(n,1,r(i)):U(n,2,i)}catch(e){U(n,2,e)}},n},e}();function F(e){return e instanceof _&&1&e.s}function R(e,t,r){for(var n;;){var i=e();if(F(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!F(o)){n=1;break}o=o.s}if(t){var s=t();if(s&&s.then&&!F(s)){n=2;break}}}var a=new _,u=U.bind(null,a,2);return(0===n?i.then(f):1===n?o.then(c):s.then(d)).then(void 0,u),a;function c(n){o=n;do{if(t&&(s=t())&&s.then&&!F(s))return void s.then(d).then(void 0,u);if(!(i=e())||F(i)&&!i.v)return void U(a,1,o);if(i.then)return void i.then(f).then(void 0,u);F(o=r())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=r())&&o.then?o.then(c).then(void 0,u):c(o):U(a,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):U(a,1,o)}}var B="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function K(e,t,r){var n,i,o=-1;return function s(a){try{for(;++o<e.length&&(!r||!r());)if((a=t(o))&&a.then){if(!F(a))return void a.then(s,i||(i=U.bind(null,n=new _,2)));a=a.v}n?U(n,1,a):n=a}catch(e){U(n||(n=new _),2,e)}}(),n}function q(e,t,r){if("function"==typeof e[B]){var n,i,o,s=function(e){try{for(;!((n=a.next()).done||r&&r());)if((e=t(n.value))&&e.then){if(!F(e))return void e.then(s,o||(o=U.bind(null,i=new _,2)));e=e.v}i?U(i,1,e):i=e}catch(e){U(i||(i=new _),2,e)}},a=e[B]();if(s(),a.return){var u=function(e){try{n.done||a.return()}catch(e){}return e};if(i&&i.then)return i.then(u,function(e){throw u(e)});u()}return i}if(!("length"in e))throw new TypeError("Object is not iterable");for(var c=[],f=0;f<e.length;f++)c.push(e[f]);return K(c,function(e){return t(c[e])},r)}var V=/*#__PURE__*/function(){function t(e){if(this.mneeApiKey=void 0,this.mneeConfig=void 0,this.mneeApi=void 0,"production"!==e.environment&&"sandbox"!==e.environment)throw f('Invalid environment. Must be either "production" or "sandbox"');var t="production"===e.environment;if(""===(null==e?void 0:e.apiKey))throw f("MNEE API key cannot be an empty string");this.mneeApiKey=null!=e&&e.apiKey?e.apiKey:t?"92982ec1c0975f31979da515d46bae9f":"54f1fd1688ba66a58a67675b82feb93e",this.mneeApi=t?"https://proxy-api.mnee.net":"https://sandbox-proxy-api.mnee.net",this.getCosignerConfig().catch(function(){})}var r=t.prototype;return r.getCosignerConfig=function(){try{var e=this;return Promise.resolve(M(function(){return Promise.resolve(fetch(e.mneeApi+"/v1/config?auth_token="+e.mneeApiKey,{method:"GET"})).then(function(t){if(401===t.status||403===t.status)throw f("Invalid API key");if(!t.ok)throw f("HTTP error! status: "+t.status);return Promise.resolve(t.json()).then(function(t){return e.mneeConfig=t,t})})},function(e){throw T(e)&&S(e,"fetch config"),e}))}catch(e){return Promise.reject(e)}},r.toAtomicAmount=function(e){return Math.round(e*Math.pow(10,5))},r.fromAtomicAmount=function(e){return e/Math.pow(10,5)},r.createInscription=function(t,r,n){try{var i={p:"bsv-20",op:"transfer",id:n.tokenId,amt:r.toString()};return Promise.resolve({lockingScript:l((new d).lock(t,e.PublicKey.fromString(n.approver)),{dataB64:Buffer.from(JSON.stringify(i)).toString("base64"),contentType:"application/bsv-20"}),satoshis:1})}catch(e){return Promise.reject(e)}},r.getUtxos=function(e,t,r,n){try{var i,o=this;return Promise.resolve(M(function(){function s(s){var a;if(i)return s;function u(e){if(a)return e;throw f("Invalid input type for address")}var c=function(){if(Array.isArray(e)){var i=e.filter(function(e){return"string"==typeof e&&x(e)});if(0===i.length)throw f("No valid Bitcoin addresses provided");var s=e.filter(function(e){return"string"!=typeof e||!x(e)});return s.length>0&&console.warn("[33m"+s.length+" invalid bitcoin addresses will be ignored[0m"),Promise.resolve(fetch(o.mneeApi+"/v2/utxos?auth_token="+o.mneeApiKey+(void 0!==t?"&page="+t:"")+(void 0!==r?"&size="+r:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){var t=["transfer","deploy+mint"],r=e.filter(function(e){return t.includes(e.data.bsv21.op.toLowerCase())});return a=1,r})})}}();return c&&c.then?c.then(u):u(c)}if(!e)throw f("Address is required");if(void 0!==t&&("number"!=typeof t||t<=0||!Number.isFinite(t)))throw f("Invalid page: "+t+". Must be a positive integer");if(void 0!==r&&("number"!=typeof r||r<=0||!Number.isInteger(r)))throw f("Invalid size: "+r+". Must be a positive integer");if(void 0!==n&&"asc"!==n&&"desc"!==n)throw f("Invalid order: "+n+". Must be 'asc' or 'desc'");var a=function(){if("string"==typeof e){if(!x(e))throw f("Invalid Bitcoin address: "+e);return Promise.resolve(fetch(o.mneeApi+"/v2/utxos?auth_token="+o.mneeApiKey+(void 0!==t?"&page="+t:"")+(void 0!==r?"&size="+r:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify([e])})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){var t=["transfer","deploy+mint"],r=e.filter(function(e){return t.includes(e.data.bsv21.op.toLowerCase())});return i=1,r})})}}();return a&&a.then?a.then(s):s(a)},function(e){throw T(e)&&S(e,"fetch UTXOs"),e}))}catch(e){return Promise.reject(e)}},r.fetchRawTx=function(t,r){void 0===r&&(r=3);try{var n,i=this,o=0,s=R(function(){return!n&&o<=r},function(){return o++},function(){return M(function(){return Promise.resolve(fetch(i.mneeApi+"/v1/tx/"+t+"?auth_token="+i.mneeApiKey)).then(function(i){function s(){if(200!==i.status)throw f(i.status+" - Failed to fetch rawtx for txid: "+t);return Promise.resolve(i.json()).then(function(t){var r=e.Transaction.fromHex(Buffer.from(t.rawtx,"base64").toString("hex"));return n=1,r})}if(404===i.status)throw f("Transaction not found");if(401===i.status||403===i.status)throw f("Invalid API key");var a=function(){if(429===i.status&&o<r){var e=Math.min(500*Math.pow(2,o),2e3);return Promise.resolve(new Promise(function(t){return setTimeout(t,e)})).then(function(){})}}();return a&&a.then?a.then(s):s()})},function(e){return o===r?(T(e)&&S(e,"fetch transaction"),void(n=1)):Promise.resolve(new Promise(function(e){return setTimeout(e,200)})).then(function(){})})});return Promise.resolve(s&&s.then?s.then(function(e){return n?e:void 0}):n?s:void 0)}catch(e){return Promise.reject(e)}},r.getSignatures=function(t,r){try{try{var n;switch(t.format){case"beef":n=e.Transaction.fromHexBEEF(t.rawtx);break;case"ef":n=e.Transaction.fromHexEF(t.rawtx);break;default:n=e.Transaction.fromHex(t.rawtx)}var i=t.sigRequests.flatMap(function(t){return[r].map(function(r){var i=e.TransactionSignature.format({sourceTXID:t.prevTxid,sourceOutputIndex:t.outputIndex,sourceSatoshis:t.satoshis,transactionVersion:n.version,otherInputs:n.inputs.filter(function(e,r){return r!==t.inputIndex}),inputIndex:t.inputIndex,outputs:n.outputs,inputSequence:n.inputs[t.inputIndex].sequence||0,subscript:t.script?e.Script.fromHex(t.script):(new e.P2PKH).lock(r.toPublicKey().toAddress()),lockTime:n.lockTime,scope:t.sigHashType||65}),o=r.sign(e.Hash.sha256(i)),s=new e.TransactionSignature(o.r,o.s,t.sigHashType||65);return{sig:e.Utils.toHex(s.toChecksigFormat()),pubKey:r.toPublicKey().toString(),inputIndex:t.inputIndex,sigHashType:t.sigHashType||65,csIdx:t.csIdx}})});return Promise.resolve({sigResponses:i})}catch(e){var o;return T(e)&&S(e,"get signatures"),Promise.resolve({error:{message:null!=(o=e.message)?o:"unknown",cause:e.cause}})}}catch(e){return Promise.reject(e)}},r.getEnoughUtxos=function(e,t){try{var r=function(r){if(!r)throw f("Config not fetched");var o=r.fees.find(function(e){return t>=e.min&&t<=e.max});if(!o)throw f("Fee not found");var s=o.fee,a=t+s;return Promise.resolve(n.getBalance(e)).then(function(t){var r;function o(e){c.sort(function(e,t){return t.data.bsv21.amt-e.data.bsv21.amt});for(var t,r=[],n=0,o=i(c);!(t=o()).done;){var s=t.value;if(r.push(s),(n+=s.data.bsv21.amt)>=a)break}return r}if(t.amount<a)throw f("Insufficient MNEE balance. Max transfer amount: "+n.fromAtomicAmount(t.amount-s));var u=1,c=[],d=0,h=R(function(){return!r&&d<a},void 0,function(){return Promise.resolve(n.getUtxos(e,u,25)).then(function(e){if(0===e.length)throw f("Insufficient MNEE balance. Max transfer amount: "+n.fromAtomicAmount(d-s));c.push.apply(c,e),(d=c.reduce(function(e,t){return e+t.data.bsv21.amt},0))>=a?r=1:u++})});return h&&h.then?h.then(o):o()})},n=this,o=n.mneeConfig;return Promise.resolve(o?r(o):Promise.resolve(n.getCosignerConfig()).then(r))}catch(e){return Promise.reject(e)}},r.getAllUtxos=function(e){try{var t,r=this,n=1,i=[],o=R(function(){return!t},void 0,function(){return Promise.resolve(r.getUtxos(e,n,100)).then(function(e){0!==e.length?(i.push.apply(i,e),n++):t=1})});return Promise.resolve(o&&o.then?o.then(function(){return i}):i)}catch(e){return Promise.reject(e)}},r.transfer=function(t,r,n){try{var o=this;return Promise.resolve(M(function(){function s(s){if(!s)throw f("Config not fetched");var a=function(e,t){var r=A(t),n=r.privateKey;if(0===e.length)return{isValid:!1,error:"Empty transfer options provided. Please provide at least one recipient."};if(!r.isValid)return{isValid:!1,error:r.error||"Invalid WIF key provided"};if(!n)return{isValid:!1,error:"Private key not found"};for(var o,s=0,a=i(e);!(o=a()).done;){var u=o.value;if(!x(u.address))return{isValid:!1,error:"Invalid recipient address: "+u.address};if("number"!=typeof u.amount||isNaN(u.amount)||!isFinite(u.amount))return{isValid:!1,error:"Invalid amount for "+u.address+": amount must be a valid number"};if(u.amount<v)return{isValid:!1,error:"Invalid amount for "+u.address+": minimum transfer amount is "+v+" MNEE"};s+=u.amount}return s<=0?{isValid:!1,error:"Invalid amount: total must be greater than 0"}:{isValid:!0,totalAmount:s,privateKey:n}}(t,r),u=a.totalAmount,c=a.privateKey;if(!a.isValid)throw f(a.error||"Invalid transfer options");if(!c)throw f("Private key not found");if(!u)throw f("Invalid amount");var d=o.toAtomicAmount(u),h=c.toAddress();return Promise.resolve(o.getEnoughUtxos(h,d)).then(function(r){var i;function a(r){function i(){function t(){function t(){if(null!=n&&n.extraData){var t=Array.isArray(n.extraData)?n.extraData:[n.extraData];if(0===t.length)throw f("extraData must contain at least one item");for(var r=[],i=0;i<t.length;i++){var s=t[i];if(!s||"string"!=typeof s.data)throw f("Invalid extraData at index "+i+": data must be a string");if("utf8"===s.type){var a=Buffer.from(s.data,"utf8");if(0===a.length)throw f("extraData at index "+i+" is empty after UTF-8 encoding");r.push(a)}else{if("hex"!==s.type)throw f("Unsupported extraData type at index "+i+": "+s.type);var u=s.data.trim();if(!u)throw f("extraData at index "+i+" is empty");if(!/^[0-9a-fA-F]+$/.test(u)||u.length%2!=0)throw f("extraData at index "+i+' is not valid hex or has odd length: "'+u+'"');var d=Buffer.from(u,"hex");if(0===d.length)throw f("extraData at index "+i+" decoded to an empty buffer");r.push(d)}}var h=r.reduce(function(e,t){return e+t.length},0);if(h>512)throw f("extraData is too large: "+h+" bytes (max allowed is 512 bytes)");var v=new e.Script;v.writeOpCode(e.OP.OP_0),v.writeOpCode(e.OP.OP_RETURN);for(var m=0,p=r;m<p.length;m++)v.writeBin(Array.from(p[m]));l.addOutput({satoshis:0,lockingScript:e.LockingScript.fromBinary(v.toBinary())})}for(var g=new Map,y=0;y<l.inputs.length;y++)g.set(y,c);return Promise.resolve(o.signAllInputs(l,g)).then(function(e){if(e.error)throw f(e.error);var t=l.toHex();return!1===(null==n?void 0:n.broadcast)?{rawtx:t}:Promise.resolve(o.submitRawTx(t,n)).then(function(e){var t=e.ticketId;if(!t)throw f("Failed to broadcast transaction");return{ticketId:t}})})}var r=v-d-u,i=function(){if(r>0){var e=l.addOutput;return Promise.resolve(o.createInscription(m,r,s)).then(function(t){e.call(l,t)})}}();return i&&i.then?i.then(t):t()}var r=function(){if(u>0){var e=l.addOutput;return Promise.resolve(o.createInscription(s.feeAddress,u,s)).then(function(t){e.call(l,t)})}}();return r&&r.then?r.then(t):t()}var a=q(t,function(e){var t=l.addOutput;return Promise.resolve(o.createInscription(e.address,o.toAtomicAmount(e.amount),s)).then(function(e){t.call(l,e)})});return a&&a.then?a.then(i):i()}var u=null==(i=s.fees.find(function(e){return d>=e.min&&d<=e.max}))?void 0:i.fee;if(void 0===u)throw f("Fee ranges inadequate");var l=new e.Transaction(1,[],[],0),v=0,m="",p=R(function(){return v<d+u},void 0,function(){function t(t){return Promise.resolve(o.fetchRawTx(n.txid)).then(function(t){if(!t)throw f("Failed to fetch source transaction: "+n.txid+"_"+n.vout);m=m||n.owners[0],l.addInput({sourceTXID:n.txid,sourceOutputIndex:n.vout,sourceTransaction:t,unlockingScript:new e.UnlockingScript}),v+=n.data.bsv21.amt})}var n=r.shift(),i=function(){if(!n)return Promise.resolve(o.getBalance(h)).then(function(e){throw f("Insufficient MNEE balance. Max transfer amount is "+o.fromAtomicAmount(e.amount-u))})}();return i&&i.then?i.then(t):t()});return p&&p.then?p.then(a):a()})}var a=o.mneeConfig;return a?s(a):Promise.resolve(o.getCosignerConfig()).then(s)},function(e){throw T(e)&&S(e,"transfer tokens"),e}))}catch(e){return Promise.reject(e)}},r.submitRawTx=function(t,r){void 0===r&&(r={broadcast:!0,callbackUrl:void 0});try{var n=this;return Promise.resolve(M(function(){var i,s,a,u;if(!1!==(null==(i=r)?void 0:i.broadcast)&&(r=o({},r,{broadcast:!0})),null!=(s=r)&&s.callbackUrl&&!1===(null==(a=r)?void 0:a.broadcast))throw f("Callback URL cannot be provided when broadcast is false");if(!t)throw f("Raw transaction is required");var c=e.Transaction.fromHex(t);if(null==(u=r)||!u.broadcast)return{rawtx:c.toHex()};var d=e.Utils.toBase64(c.toBinary()),h=JSON.stringify(r?{rawtx:d,callback_url:r.callbackUrl}:{rawtx:d});return Promise.resolve(fetch(n.mneeApi+"/v2/transfer?auth_token="+n.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:h})).then(function(e){if(!e.ok)throw f("Failed to submit transaction");return Promise.resolve(e.text()).then(function(e){return{ticketId:e}})})},function(e){throw T(e)&&S(e,"submit raw transaction"),e}))}catch(e){return Promise.reject(e)}},r.getTxStatus=function(e){try{var t=this;return Promise.resolve(M(function(){if(!e)throw f("Ticket ID is required");return Promise.resolve(fetch(t.mneeApi+"/v2/ticket?ticketID="+e+"&auth_token="+t.mneeApiKey,{method:"GET",headers:{"Content-Type":"application/json"}})).then(function(e){if(!e.ok)throw f("Failed to get transaction status");return Promise.resolve(e.json())})},function(e){throw T(e)&&S(e,"get transaction status"),e}))}catch(e){return Promise.reject(e)}},r.getBalance=function(e){try{var t=this;if(!x(e))throw f("Invalid Bitcoin address: "+e);return Promise.resolve(M(function(){function r(r){if(!r)throw f("Config not fetched");return Promise.resolve(fetch(t.mneeApi+"/v2/balance?auth_token="+t.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify([e])})).then(function(t){if(!t.ok)throw f("Failed to get transaction status: "+t.status);return Promise.resolve(t.json()).then(function(t){return t&&0!==t.length?{address:t[0].address,amount:t[0].amt,decimalAmount:t[0].precised}:{address:e,amount:0,decimalAmount:0}})})}var n=t.mneeConfig;return n?r(n):Promise.resolve(t.getCosignerConfig()).then(r)},function(e){throw T(e)&&S(e,"fetch balance"),e}))}catch(e){return Promise.reject(e)}},r.getBalances=function(e){try{var t=this;if(!Array.isArray(e))throw f("Addresses must be an array");var r=e.filter(function(e){return x(e)});if(0===r.length)throw f("You must pass at least 1 valid address");var n=e.length-r.length;return n>0&&console.warn("[33m"+n+" invalid bitcoin addresses will be ignored[0m"),Promise.resolve(M(function(){function e(e){if(!e)throw f("Config not fetched");return Promise.resolve(fetch(t.mneeApi+"/v2/balance?auth_token="+t.mneeApiKey,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})).then(function(e){if(!e.ok)throw f("Failed to get transaction status: "+e.status);return Promise.resolve(e.json()).then(function(e){if(!e||0===e.length)return r.map(function(e){return{address:e,amount:0,decimalAmount:0}});var t=new Map;return e.forEach(function(e){t.set(e.address,{address:e.address,amount:e.amt,decimalAmount:e.precised})}),r.map(function(e){return t.has(e)?t.get(e):{address:e,amount:0,decimalAmount:0}})})})}var n=t.mneeConfig;return n?e(n):Promise.resolve(t.getCosignerConfig()).then(e)},function(e){throw T(e)&&S(e,"fetch balances"),e}))}catch(e){return Promise.reject(e)}},r.processMneeValidation=function(t,r,n){var o=this;try{var s=t.id("hex")===r.tokenId.split("_")[0],a=t.outputs.map(function(e){return e.lockingScript}),u=w(a),c=t.outputs.map(function(t,r){var n,i=u[r],o=P(t.lockingScript),s=null;if(null!=o&&null!=(n=o.file)&&n.content)try{var a=e.Utils.toUTF8(o.file.content);a&&(s=JSON.parse(a))}catch(e){}return{index:r,address:null==i?void 0:i.address,cosigner:null==i?void 0:i.cosigner,inscription:s}}),d=c.some(function(e){return e.cosigner===r.approver});if(c.find(function(e){return""!==e.cosigner&&e.cosigner!==r.approver}))throw f("Invalid cosigner detected");var h=c.filter(function(e){if(!e.inscription)return!1;var t=e.inscription;if("bsv-20"!==t.p)return!1;if(!s&&t.id!==r.tokenId)throw f("Invalid token ID: "+t.id);var n=parseInt(t.amt,10);if(isNaN(n)||n<=0)throw f("Invalid MNEE amount: "+t.amt);return!0});if(0===h.length)throw f("No valid MNEE inscriptions found in transaction");var l=new Set(h.map(function(e){return e.inscription.op})),v=l.has("burn"),m=h.some(function(e){var t;return"transfer"===e.inscription.op&&"redeem"===(null==(t=e.inscription.metadata)?void 0:t.action)}),g=m&&c.some(function(e){return e.address===y||e.address===p});if((l.has("transfer")&&!m||v||m&&!g)&&!d)throw f("Cosigner not found in transaction with transfer/burn/redeem operation");var b=h.filter(function(e){var t=e.inscription;return"transfer"===t.op||"burn"===t.op||"deploy+mint"===t.op});if(n)for(var x,A=[].concat(b),I=function(){var e=x.value,t=A.findIndex(function(t){return t.address===e.address&&t.inscription.amt===o.toAtomicAmount(e.amount).toString()});if(-1===t)throw f("No matching output found for "+e.address+" with amount "+e.amount);A.splice(t,1)},O=i(n);!(x=O()).done;)I();return!0}catch(e){return T(e)&&S(e,"validate Mnee transaction"),!1}},r.validateMneeTx=function(t,r){try{var n=this;return Promise.resolve(M(function(){function i(i){var o;if(!i)throw f("Config not fetched");var s=e.Transaction.fromHex(t);if(!n.processMneeValidation(s,i,r))return!1;var a=s.id("hex")===i.tokenId.split("_")[0],u=function(){if(!a)return Promise.resolve(n.processTransactionInputs(s,i)).then(function(e){var t=n.processTransactionOutputs(s,i);if(e.total!==t.total)return o=1,!1})}();return u&&u.then?u.then(function(e){return!o||e}):!o||u}var o=n.mneeConfig;return o?i(o):Promise.resolve(n.getCosignerConfig()).then(i)},function(e){return T(e)&&S(e,"validate Mnee transaction"),!1}))}catch(e){return Promise.reject(e)}},r.getMneeSyncs=function(e,t,r,n){try{var i=this;return Promise.resolve(M(function(){var o=Array.isArray(e)?e:[e];return Promise.resolve(fetch(i.mneeApi+"/v1/sync?auth_token="+i.mneeApiKey+(t?"&from="+t:"")+(r?"&limit="+r:"")+(n?"&order="+n:""),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).then(function(e){if(401===e.status||403===e.status)throw f("Invalid API key");if(!e.ok)throw f("HTTP error! status: "+e.status);return Promise.resolve(e.json()).then(function(e){return o.map(function(t){var r=e.filter(function(e){return e.senders.includes(t)||e.receivers.includes(t)});return{address:t,syncs:r}})})})},function(e){throw T(e)&&S(e,"fetch syncs"),e}))}catch(e){return Promise.reject(e)}},r.getRecentTxHistory=function(e,t,r,n){try{var o=this;if(!x(e))throw f("Invalid Bitcoin address: "+e);if(void 0!==t&&("number"!=typeof t||t<0||!Number.isFinite(t)))throw f("Invalid fromScore: "+t+". Must be a positive number or 0");if(void 0!==r&&("number"!=typeof r||r<=0||!Number.isInteger(r)))throw f("Invalid limit: "+r+". Must be a positive integer");if(void 0!==n&&"asc"!==n&&"desc"!==n)throw f("Invalid order: "+n+". Must be 'asc' or 'desc'");return Promise.resolve(M(function(){function s(s){if(!s)throw f("Config not fetched");return Promise.resolve(o.getMneeSyncs(e,t,r,n)).then(function(n){var o=n[0].syncs;if(!o||0===o.length)return{address:e,history:[],nextScore:t||0};for(var a,u=[],c=i(o);!(a=c()).done;){var f=b(a.value,e,s);f&&u.push(f)}var d=u.length>0?u[u.length-1].score:t||0;return r&&u.length>r?{address:e,history:u.slice(0,r),nextScore:d}:{address:e,history:u,nextScore:d}})}var a=o.mneeConfig;return a?s(a):Promise.resolve(o.getCosignerConfig()).then(s)},function(e){throw T(e)&&S(e,"fetch transaction history"),e}))}catch(e){return Promise.reject(e)}},r.getRecentTxHistories=function(e){try{var t=this;if(!Array.isArray(e))throw f("Parameters must be an array");if(0===e.length)throw f("You must pass at least 1 address parameter");var r=e.filter(function(e){return e&&e.address&&x(e.address)});if(0===r.length)throw f("You must pass at least 1 valid address");var n=e.length-r.length;n>0&&console.warn("[33m"+n+" invalid bitcoin addresses will be ignored[0m");for(var o,s=i(r);!(o=s()).done;){var a=o.value;if(void 0!==a.fromScore&&("number"!=typeof a.fromScore||a.fromScore<0||!Number.isFinite(a.fromScore)))throw f("Invalid fromScore for address "+a.address+": "+a.fromScore+". Must be a positive number or 0");if(void 0!==a.limit&&("number"!=typeof a.limit||a.limit<=0||!Number.isInteger(a.limit)))throw f("Invalid limit for address "+a.address+": "+a.limit+". Must be a positive integer");if(void 0!==a.order&&"asc"!==a.order&&"desc"!==a.order)throw f("Invalid order for address "+a.address+": "+a.order+". Must be 'asc' or 'desc'")}return Promise.resolve(M(function(){function e(e){if(!e)throw f("Config not fetched");var n={};r.forEach(function(e){var t=(e.fromScore||0)+":"+(e.limit||100)+":"+(e.order||"default");n[t]||(n[t]=[]),n[t].push(e)});var o=Object.entries(n).map(function(r){var n=r[0],o=r[1];try{var s=n.split(":"),a=s[1],u=s[2],c=Number(s[0]),f=Number(a),d="default"===u?void 0:u,h=o.map(function(e){return e.address});return Promise.resolve(t.getMneeSyncs(h,c,f,d)).then(function(t){return t.map(function(t){var r=t.address,n=t.syncs,s=o.find(function(e){return e.address===r});if(!n||0===n.length)return{address:r,history:[],nextScore:(null==s?void 0:s.fromScore)||0};for(var a,u=[],c=new Set,f=i(n);!(a=f()).done;){var d=b(a.value,r,e);d&&!c.has(d.txid)&&(c.add(d.txid),u.push(d))}var h=u.length>0?u[u.length-1].score:(null==s?void 0:s.fromScore)||0,l=null==s?void 0:s.limit;return l&&u.length>l?{address:r,history:u.slice(0,l),nextScore:h}:{address:r,history:u,nextScore:h}})})}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(o)).then(function(e){return e.flat()})}var n=t.mneeConfig;return n?e(n):Promise.resolve(t.getCosignerConfig()).then(e)},function(e){throw T(e)&&S(e,"fetch transaction histories"),e}))}catch(e){return Promise.reject(e)}},r.parseInscriptionData=function(t){try{var r,n=P(t),i=null==n||null==(r=n.file)?void 0:r.content;if(!i)return null;var o=e.Utils.toUTF8(i);return o?JSON.parse(o):null}catch(e){return T(e)&&S(e,"parse inscription data"),null}},r.determineEnvironment=function(e,t,r,n){return e===m.split("_")[0]?"production":e===g.split("_")[0]?"sandbox":t===m&&"020a177d6a5e6f3a8689acd2e313bd1cf0dcf5a243d1cc67b7218602aee9e04b2f"===r?"production":t===g&&"02bed35e894cc41cc9879b4002ad03d33533b615c1b476068c8dd6822a09f93f6c"===r?"sandbox":""===r&&n===p?"production":"sandbox"},r.determineTransactionType=function(e,t,r,n,i){return"burn"===e?"burn":"deploy+mint"===e||t===n?r===i.split("_")[0]?"deploy":"mint":t===p||t===y?"mint":"transfer"},r.processTransactionInputs=function(e,t){try{var r,n,o=this,s=e.id("hex"),a=new Array(e.inputs.length),u=BigInt(0),c=new D(3,334),f=e.inputs.map(function(e,t){try{return Promise.resolve(e.sourceTXID?M(function(){return Promise.resolve(c.execute(function(){return o.fetchRawTx(e.sourceTXID)})).then(function(e){return{index:t,sourceTx:e}})},function(e){return T(e)&&S(e,"fetch source transaction"),{index:t,sourceTx:null}}):{index:t,sourceTx:null})}catch(e){return Promise.reject(e)}});return Promise.resolve(Promise.all(f)).then(function(c){for(var f,d=i(c);!(f=d()).done;){var h=f.value,l=h.index,v=h.sourceTx,m=e.inputs[l];if(v&&m.sourceTXID){var p=v.outputs[m.sourceOutputIndex],g=w([p.lockingScript])[0],y=o.parseInscriptionData(p.lockingScript),P={address:null==g?void 0:g.address,amount:y?parseInt(y.amt):0,satoshis:Number(p.satoshis),inscription:y,cosigner:g};a[l]=P,y&&g&&(u+=BigInt(y.amt),r||(r=o.determineEnvironment(s,y.id,g.cosigner||"",g.address||"")),n||(n=o.determineTransactionType(y.op,g.address||"",s,t.mintAddress,t.tokenId)))}else a[l]={address:void 0,amount:0,satoshis:0,inscription:null,cosigner:void 0}}return{inputs:a,total:u,environment:r,type:n}})}catch(e){return Promise.reject(e)}},r.processTransactionOutputs=function(e,t){for(var r,n,i=e.id("hex"),o=[],s=BigInt(0),a=0;a<e.outputs.length;a++){var u=e.outputs[a],c=w([u.lockingScript])[0],f=this.parseInscriptionData(u.lockingScript),d={address:null==c?void 0:c.address,amount:f?parseInt(f.amt):0,satoshis:Number(u.satoshis),inscription:f,cosigner:c};if(o.push(d),f&&c){s+=BigInt(f.amt),r=this.determineEnvironment(i,f.id,c.cosigner||"",c.address||"");var h=this.determineTransactionType(f.op,c.address||"",i,t.mintAddress,t.tokenId);"burn"===h||"deploy"===h?n=h:n||(n=h)}}return{outputs:o,total:s,environment:r,type:n}},r.validateTransaction=function(e,t,r,n,i){return!!this.processMneeValidation(t,e)&&("deploy"===r||n===i)},r.buildParseResponse=function(e,t,r,n,i,s,a,u){var c={txid:e,environment:t,type:r,inputs:n.inputs.filter(function(e){return e.inscription&&e.address}).map(function(e){return{address:e.address,amount:e.amount}}),outputs:i.outputs.filter(function(e){return e.inscription&&e.address}).map(function(e){return{address:e.address,amount:e.amount}}),isValid:s,inputTotal:n.total.toString(),outputTotal:i.total.toString()};return null!=u&&u.includeRaw?o({},c,{raw:{txHex:a.toHex(),inputs:a.inputs.map(function(e,t){var r,i=n.inputs[t];return{txid:e.sourceTXID||"",vout:e.sourceOutputIndex,scriptSig:(null==(r=e.unlockingScript)?void 0:r.toHex())||"",sequence:e.sequence||4294967295,address:null==i?void 0:i.address,satoshis:(null==i?void 0:i.satoshis)||0,tokenData:null!=i&&i.inscription?{amount:i.amount}:void 0}}),outputs:a.outputs.map(function(e,t){var r=i.outputs[t];return{value:Number(e.satoshis),scriptPubKey:e.lockingScript.toHex(),address:null==r?void 0:r.address,tokenData:null!=r&&r.inscription?{amount:r.amount}:void 0}})}}):c},r.parseTransaction=function(e,t,r){try{var n=this,i=e.id("hex");return Promise.resolve(n.processTransactionInputs(e,t)).then(function(o){var s=n.processTransactionOutputs(e,t),a=s.environment||o.environment||"sandbox",u=s.type||o.type||"transfer";"transfer"===u&&o.inputs.some(function(e){return e.inscription&&(e.address===p||e.address===y)})&&(u="mint"),"transfer"!==u&&"mint"!==u||s.outputs.some(function(e){var t;return"redeem"===(null==(t=e.inscription)||null==(t=t.metadata)?void 0:t.action)})&&(u="redeem"),0===o.inputs.filter(function(e){return e.inscription}).length&&o.inputs.length>0&&(u="deploy");var c=n.validateTransaction(t,e,u,o.total,s.total);return n.buildParseResponse(i,a,u,o,s,c,e,r)})}catch(e){return Promise.reject(e)}},r.parseTx=function(e,t){try{var r=function(r){if(!r)throw f("Config not fetched");return Promise.resolve(n.fetchRawTx(e)).then(function(e){if(!e)throw f("Failed to fetch transaction");return Promise.resolve(n.parseTransaction(e,r,t))})},n=this;if(!e||"string"!=typeof e||""===e.trim()||!/^[a-fA-F0-9]{64}$/.test(e))throw f("A valid transaction ID is required");var i=n.mneeConfig;return Promise.resolve(i?r(i):Promise.resolve(n.getCosignerConfig()).then(r))}catch(e){return Promise.reject(e)}},r.parseTxFromRawTx=function(t,r){try{var n=function(e){if(!e)throw f("Config not fetched");return Promise.resolve(i.parseTransaction(o,e,r))},i=this;if(!t||"string"!=typeof t||""===t.trim())throw f("A valid raw transaction is required");if(!function(t){try{return!!/^[a-fA-F0-9]+$/.test(t)&&(e.Transaction.fromHex(t),!0)}catch(e){return!1}}(t))throw f("Invalid raw transaction hex");var o=e.Transaction.fromHex(t),s=i.mneeConfig;return Promise.resolve(s?n(s):Promise.resolve(i.getCosignerConfig()).then(n))}catch(e){return Promise.reject(e)}},r.parseInscription=function(e){return P(e)},r.parseCosignerScripts=function(e){return w(e)},r.validateUniqueInputs=function(e){for(var t,r=new Set,n=i(e);!(t=n()).done;){var o=t.value,s=o.txid+":"+o.vout;if(r.has(s))return{error:"Duplicate input detected: "+s+". Each UTXO can only be spent once."};r.add(s)}return{}},r.addInputsToTransaction=function(t,r,n){try{var i,o=function(e){return i?e:{tokensIn:a}},s=this,a=0,u=K(r,function(o){var u=r[o];return Promise.resolve(s.fetchRawTx(u.txid)).then(function(r){if(!r)return i=1,{tokensIn:0,error:"Failed to fetch source transaction: "+u.txid+"_"+u.vout};var c=r.outputs[u.vout];if(!c)return i=1,{tokensIn:0,error:"Output "+u.vout+" not found in transaction "+u.txid};var f=s.parseInscriptionData(c.lockingScript);if(!f)return i=1,{tokensIn:0,error:"No inscription found in output "+u.txid+":"+u.vout};var d=parseInt(f.amt);a+=d,n.set(o,e.PrivateKey.fromWif(u.wif)),t.addInput({sourceTXID:u.txid,sourceOutputIndex:u.vout,sourceTransaction:r,unlockingScript:new e.UnlockingScript})})},function(){return i});return Promise.resolve(u&&u.then?u.then(o):o(u))}catch(e){return Promise.reject(e)}},r.calculateTransferMultiFee=function(e,t,r,n,o,s){for(var a,u=this,c=0,f=-1,d=function(){var s;f=c;var a=e-t-c,d=0;if(a>0)if("string"==typeof r)n.has(r)||(d=a);else if(Array.isArray(r))for(var h,l=i(r);!(h=l()).done;){var v=h.value,m=u.toAtomicAmount(v.amount);n.has(v.address)||(d+=m)}else d=0;var p=t+d,g=null==(s=o.fees.find(function(e){return p>=e.min&&p<=e.max}))?void 0:s.fee;if(void 0===g)return{v:{fee:0,error:"Fee ranges inadequate"}};c=g};c!==f;)if(a=d())return a.v;return{fee:c}},r.addChangeOutputs=function(e,t,r,n,o,s,a,u){try{var c,f=function(e){return c?e:{}},d=this;if(t<=0)return Promise.resolve({});var h=function(){if("string"==typeof r){var f=e.addOutput;return Promise.resolve(d.createInscription(r,t,o)).then(function(t){f.call(e,t)})}return function(){if(!Array.isArray(r)){var f=n.get(0).toAddress(),h=e.addOutput;return Promise.resolve(d.createInscription(f,t,o)).then(function(t){h.call(e,t)})}if(0===r.length)return c=1,{error:"Change address array cannot be empty. Provide at least one change output or use a single address string."};var l=r.map(function(e){return{address:e.address,amount:d.toAtomicAmount(e.amount)}}),v=l.reduce(function(e,t){return e+t.amount},0);if(v!==t){var m=d.fromAtomicAmount(t),p=d.fromAtomicAmount(v),g={error:"Change amounts must sum to "+m+" ("+t+" atomic units). Total inputs: "+d.fromAtomicAmount(s)+" - total outputs: "+d.fromAtomicAmount(a)+" - fee: "+d.fromAtomicAmount(u)+" = "+m+". Your change outputs sum to "+p+" ("+v+" atomic units)."};return c=1,g}for(var y,P=i(r);!(y=P()).done;){var w=y.value;if(w.amount<=0)return c=1,{error:"Invalid change amount: "+w.amount+". Must be positive."}}var b=q(l,function(t){var r=e.addOutput;return Promise.resolve(d.createInscription(t.address,t.amount,o)).then(function(t){r.call(e,t)})});return b&&b.then?b.then(function(){}):void 0}()}();return Promise.resolve(h&&h.then?h.then(f):f(h))}catch(e){return Promise.reject(e)}},r.createSignatureRequests=function(t){return t.inputs.map(function(t,r){var n,i,o=null!=(n=t.sourceTXID)?n:null==(i=t.sourceTransaction)?void 0:i.id("hex");if(!o)throw f("Input "+r+" is missing sourceTXID or sourceTransaction");var s=t.sourceTransaction;if(!s)throw f("Input "+r+" is missing sourceTransaction required for signing");var a=s.outputs[t.sourceOutputIndex];if(!a)throw f("Output "+t.sourceOutputIndex+" not found in transaction "+o);if(void 0===a.satoshis)throw f("Output "+t.sourceOutputIndex+" in transaction "+o+" is missing satoshis");return{prevTxid:o,outputIndex:t.sourceOutputIndex,inputIndex:r,address:"",script:a.lockingScript.toHex(),satoshis:a.satoshis,sigHashType:e.TransactionSignature.SIGHASH_ALL|e.TransactionSignature.SIGHASH_ANYONECANPAY|e.TransactionSignature.SIGHASH_FORKID}})},r.signAllInputs=function(e,t){try{var r,n=function(t){return r?t:(i.applySignatures(e,a),{})},i=this,o=i.createSignatureRequests(e);o.forEach(function(e,r){e.address=t.get(r).toAddress()});var s=e.toHex(),a=[],u=q(t.entries(),function(e){var t=e[0];return Promise.resolve(i.getSignatures({rawtx:s,sigRequests:[o[t]]},e[1])).then(function(e){if(null==e||!e.sigResponses)return r=1,{error:"Failed to get signatures for input "+t};a.push.apply(a,e.sigResponses)})},function(){return r});return Promise.resolve(u&&u.then?u.then(n):n(u))}catch(e){return Promise.reject(e)}},r.applySignatures=function(t,r){for(var n,o=i(r);!(n=o()).done;){var s=n.value;t.inputs[s.inputIndex].unlockingScript=(new e.Script).writeBin(e.Utils.toArray(s.sig,"hex")).writeBin(e.Utils.toArray(s.pubKey,"hex"))}return t},r.buildUnsignedMneeTransaction=function(t){try{var r=function(r){function i(e){var i;function c(){function e(){function e(e){var t=n.createSignatureRequests(s);return{transaction:s,sigRequests:t,sourceTransactions:a}}var i=u-o-d,c=function(){if(i>0)return function(){if(t.changeAddress)return function(){if("string"==typeof t.changeAddress){var e=s.addOutput;return Promise.resolve(n.createInscription(t.changeAddress,i,r)).then(function(t){e.call(s,t)})}return function(){if(Array.isArray(t.changeAddress)){var e=t.changeAddress.map(function(e){return{address:e.address,amount:n.toAtomicAmount(e.amount)}}),o=e.reduce(function(e,t){return e+t.amount},0);if(o!==i)throw f("Change amounts must sum to "+n.fromAtomicAmount(i)+". Your change outputs sum to "+n.fromAtomicAmount(o));var a=q(e,function(e){var t=s.addOutput;return Promise.resolve(n.createInscription(e.address,e.amount,r)).then(function(e){t.call(s,e)})});if(a&&a.then)return a.then(function(){})}}()}();var e=t.inputs[0],o=a.get(0),u=w([o.outputs[e.vout].lockingScript])[0];if(null==u||!u.address)throw f("Could not determine change address from input");var c=s.addOutput;return Promise.resolve(n.createInscription(u.address,i,r)).then(function(e){c.call(s,e)})}()}();return c&&c.then?c.then(e):e()}var i=function(){if(d>0){var e=s.addOutput;return Promise.resolve(n.createInscription(r.feeAddress,d,r)).then(function(t){e.call(s,t)})}}();return i&&i.then?i.then(e):e()}var d=void 0!==t.recipients.find(function(e){return e.address===r.burnAddress})?0:null==(i=r.fees.find(function(e){return o>=e.min&&o<=e.max}))?void 0:i.fee;if(void 0===d)throw f("Fee ranges inadequate");if(u<o+d)throw f("Insufficient tokens. Have: "+n.fromAtomicAmount(u)+", Need: "+n.fromAtomicAmount(o+d)+" (including fee: "+n.fromAtomicAmount(d)+")");var h=q(t.recipients,function(e){var t=s.addOutput;return Promise.resolve(n.createInscription(e.address,n.toAtomicAmount(e.amount),r)).then(function(e){t.call(s,e)})});return h&&h.then?h.then(c):c()}if(!r)throw f("Config not fetched");var o=t.recipients.reduce(function(e,t){return e+n.toAtomicAmount(t.amount)},0);if(o<=0)throw f("Invalid amount");var s=new e.Transaction(1,[],[],0),a=new Map,u=0,c=0,d=R(function(){return c<t.inputs.length},function(){return c++},function(){var r=t.inputs[c];return Promise.resolve(n.fetchRawTx(r.txid)).then(function(t){if(!t)throw f("Failed to fetch source transaction: "+r.txid+"_"+r.vout);var i=t.outputs[r.vout];if(!i)throw f("Output "+r.vout+" not found in transaction "+r.txid);var o=n.parseInscriptionData(i.lockingScript);if(!o)throw f("No inscription found in output "+r.txid+":"+r.vout);u+=parseInt(o.amt),a.set(c,t),s.addInput({sourceTXID:r.txid,sourceOutputIndex:r.vout,sourceTransaction:t,unlockingScript:new e.UnlockingScript})})});return d&&d.then?d.then(i):i()},n=this,i=n.mneeConfig;return Promise.resolve(i?r(i):Promise.resolve(n.getCosignerConfig()).then(r))}catch(e){return Promise.reject(e)}},r.validateTokenConservation=function(e,t){for(var r=0,n=0;n<e.outputs.length;n++){var i=this.parseInscriptionData(e.outputs[n].lockingScript);i&&(r+=parseInt(i.amt))}return t!==r?{error:"Token conservation violation! Inputs ("+this.fromAtomicAmount(t)+") do not equal outputs ("+this.fromAtomicAmount(r)+"). This would "+(t>r?"burn":"create")+" "+this.fromAtomicAmount(Math.abs(t-r))+" tokens."}:{}},r.transferMulti=function(t,r){try{var n=this;return Promise.resolve(M(function(){function o(o){if(!o)throw f("Config not fetched");var s=function(e){for(var t,r=i(e.recipients);!(t=r()).done;){var n=t.value;if(!n.address||!n.amount)return{isValid:!1,error:"Invalid recipient: "+JSON.stringify(n)+". Missing required fields: address, amount"};if("number"!=typeof n.amount||isNaN(n.amount)||!isFinite(n.amount))return{isValid:!1,error:"Invalid amount for "+n.address+": amount must be a valid number"};if(n.amount<v)return{isValid:!1,error:"Invalid amount for "+n.address+": minimum transfer amount is "+v+" MNEE"};if(!x(n.address))return{isValid:!1,error:"Invalid recipient address: "+n.address}}for(var o,s=i(e.inputs);!(o=s()).done;){var a=o.value;if(!a.txid||"number"!=typeof a.vout||!a.wif)return{isValid:!1,error:"Invalid input: "+JSON.stringify(a)+". Missing required fields: txid, vout, wif"};var u=A(a.wif);if(!u.isValid)return{isValid:!1,error:"Invalid WIF key: "+a.wif+" for input "+a.txid+":"+a.vout};var c=u.privateKey.toAddress();if(!x(c))return{isValid:!1,error:"Invalid input address: "+c}}if(e.changeAddress&&Array.isArray(e.changeAddress))for(var f,d=i(e.changeAddress);!(f=d()).done;){var h=f.value;if(h.amount<v)return{isValid:!1,error:"Invalid amount for "+h.address+": minimum transfer amount is "+v+" MNEE"};if(!x(h.address))return{isValid:!1,error:"Invalid change address: "+h.address}}return{isValid:!0}}(t);if(!s.isValid)throw f(s.error||"Invalid transfer options");var a=t.recipients.reduce(function(e,t){return e+t.amount},0);if(a<=0)throw f("Invalid amount");var u=n.toAtomicAmount(a),c=n.validateUniqueInputs(t.inputs);if(c.error)throw f(c.error);var d=new e.Transaction(1,[],[],0),h=new Map;return Promise.resolve(n.addInputsToTransaction(d,t.inputs,h)).then(function(i){function s(){function i(){return Promise.resolve(n.addChangeOutputs(d,a-u-p,t.changeAddress,h,o,a,u,p)).then(function(t){if(t.error)throw f(t.error);if(null!=r&&r.extraData){var i=Array.isArray(r.extraData)?r.extraData:[r.extraData];if(0===i.length)throw f("extraData must contain at least one item");for(var o=[],s=0;s<i.length;s++){var u=i[s];if(!u||"string"!=typeof u.data)throw f("Invalid extraData at index "+s+": data must be a string");if("utf8"===u.type){var c=Buffer.from(u.data,"utf8");if(0===c.length)throw f("extraData at index "+s+" is empty after UTF-8 encoding");o.push(c)}else{if("hex"!==u.type)throw f("Unsupported extraData type at index "+s+": "+u.type);var l=u.data.trim();if(!l)throw f("extraData at index "+s+" is empty");if(!/^[0-9a-fA-F]+$/.test(l)||l.length%2!=0)throw f("extraData at index "+s+' is not valid hex or has odd length: "'+l+'"');var v=Buffer.from(l,"hex");if(0===v.length)throw f("extraData at index "+s+" decoded to an empty buffer");o.push(v)}}var m=o.reduce(function(e,t){return e+t.length},0);if(m>512)throw f("extraData is too large: "+m+" bytes (max allowed is 512 bytes)");var p=new e.Script;p.writeOpCode(e.OP.OP_0),p.writeOpCode(e.OP.OP_RETURN);for(var g=0,y=o;g<y.length;g++)p.writeBin(Array.from(y[g]));d.addOutput({satoshis:0,lockingScript:e.LockingScript.fromBinary(p.toBinary())})}return Promise.resolve(n.signAllInputs(d,h)).then(function(e){if(e.error)throw f(e.error);var t=n.validateTokenConservation(d,a);if(t.error)throw f(t.error);var i=d.toHex();return null!=r&&r.broadcast?Promise.resolve(n.submitRawTx(i,r)).then(function(e){var t=e.ticketId;if(!t)throw f("Failed to broadcast transaction");return{ticketId:t}}):{rawtx:i}})})}var s=function(){if(p>0){var e=d.addOutput;return Promise.resolve(n.createInscription(o.feeAddress,p,o)).then(function(t){e.call(d,t)})}}();return s&&s.then?s.then(i):i()}if(i.error)throw f(i.error);for(var a=i.tokensIn,c=new Set,l=0;l<t.inputs.length;l++){var v=e.PrivateKey.fromWif(t.inputs[l].wif);c.add(v.toAddress())}var m=n.calculateTransferMultiFee(a,u,t.changeAddress,c,o,t.recipients);if(m.error)throw f(m.error);var p=m.fee;if(a<u+p)throw f("Insufficient tokens. Have: "+n.fromAtomicAmount(a)+", Need: "+n.fromAtomicAmount(u+p)+" (including fee: "+n.fromAtomicAmount(p)+")");var g=q(t.recipients,function(e){var t=d.addOutput;return Promise.resolve(n.createInscription(e.address,n.toAtomicAmount(e.amount),o)).then(function(e){t.call(d,e)})});return g&&g.then?g.then(s):s()})}var s=n.mneeConfig;return s?o(s):Promise.resolve(n.getCosignerConfig()).then(o)},function(e){throw T(e)&&S(e,"multi-source transfer"),e}))}catch(e){return Promise.reject(e)}},t}();function L(e,t,r){if(!e.s){if(r instanceof G){if(!r.s)return void(r.o=L.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(L.bind(null,e,t),L.bind(null,e,2));e.s=t,e.v=r;const n=e.o;n&&n(e)}}var G=/*#__PURE__*/function(){function e(){}return e.prototype.then=function(t,r){var n=new e,i=this.s;if(i){var o=1&i?t:r;if(o){try{L(n,1,o(this.v))}catch(e){L(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?L(n,1,t?t(i):i):r?L(n,1,r(i)):L(n,2,i)}catch(e){L(n,2,e)}},n},e}();function z(e){return e instanceof G&&1&e.s}function X(e,t,r){for(var n;;){var i=e();if(z(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=r();if(o&&o.then){if(!z(o)){n=1;break}o=o.s}if(t){var s=t();if(s&&s.then&&!z(s)){n=2;break}}}var a=new G,u=L.bind(null,a,2);return(0===n?i.then(f):1===n?o.then(c):s.then(d)).then(void 0,u),a;function c(n){o=n;do{if(t&&(s=t())&&s.then&&!z(s))return void s.then(d).then(void 0,u);if(!(i=e())||z(i)&&!i.v)return void L(a,1,o);if(i.then)return void i.then(f).then(void 0,u);z(o=r())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function f(e){e?(o=r())&&o.then?o.then(c).then(void 0,u):c(o):L(a,1,o)}function d(){(i=e())?i.then?i.then(f).then(void 0,u):f(i):L(a,1,o)}}var W=/*#__PURE__*/function(){function t(t,n){if(this.masterKey=void 0,this.derivationPath=void 0,this.cacheSize=void 0,this.cache=void 0,!r.validateMnemonic(t))throw f("Invalid mnemonic phrase");var i=e.Mnemonic.fromString(t).toSeed();this.masterKey=e.HD.fromSeed(i),this.derivationPath=n.derivationPath,this.cacheSize=n.cacheSize||1e3,this.cache=new Map}t.generateMnemonic=function(){return r.generateMnemonic()},t.isValidMnemonic=function(e){return r.validateMnemonic(e)};var n=t.prototype;return n.deriveAddress=function(e,t){void 0===t&&(t=!1);var r=this.derivationPath+"/"+(t?1:0)+"/"+e;if(this.cache.has(r))return this.cache.get(r);var n=this.masterKey.derive(r).privKey;if(!n)throw f("Failed to derive private key for path: "+r);var i={address:n.toAddress(),privateKey:n.toWif(),path:r};return this.cache.size<this.cacheSize&&this.cache.set(r,i),i},n.deriveAddresses=function(e,t,r){void 0===r&&(r=!1);try{for(var n=[],i=0;i<t;i++){var o=this.deriveAddress(e+i,r);n.push(o)}return Promise.resolve(n)}catch(e){return Promise.reject(e)}},n.getPrivateKeysForAddresses=function(e,t){for(var r,n=(null==t?void 0:t.maxScanReceive)||1e4,o=(null==t?void 0:t.maxScanChange)||1e4,s=(null==t?void 0:t.scanStrategy)||"parallel",a={},u={},c=new Set(e),d=i(this.cache);!(r=d()).done;){var h=r.value[1];c.has(h.address)&&(a[h.address]=h.privateKey,u[h.address]=h.path,c.delete(h.address))}if(0===c.size)return{privateKeys:a,paths:u};if("sequential"===s){for(var l=0;l<n&&c.size>0;l++){var v=this.deriveAddress(l,!1);c.has(v.address)&&(a[v.address]=v.privateKey,u[v.address]=v.path,c.delete(v.address))}for(var m=0;m<o&&c.size>0;m++){var p=this.deriveAddress(m,!0);c.has(p.address)&&(a[p.address]=p.privateKey,u[p.address]=p.path,c.delete(p.address))}}else for(var g=Math.max(n,o),y=0;y<g&&c.size>0;y++){if(y<n){var P=this.deriveAddress(y,!1);c.has(P.address)&&(a[P.address]=P.privateKey,u[P.address]=P.path,c.delete(P.address))}if(y<o){var w=this.deriveAddress(y,!0);c.has(w.address)&&(a[w.address]=w.privateKey,u[w.address]=w.path,c.delete(w.address))}}if(c.size>0){var b=Array.from(c);throw f("Could not find private keys for "+b.length+" address(es): "+b.join(", ")+". Scanned up to index "+n+" for receive addresses and "+o+" for change addresses.")}return{privateKeys:a,paths:u}},n.getPrivateKeys=function(e,t){return this.getPrivateKeysForAddresses(e,t).privateKeys},n.scanAddressesWithGapLimit=function(e,t){try{var r=function(){var t=function(){if(o){u=0;var t=0,r=X(function(){return t<s&&u<i},function(){return t++},function(){var r=n.deriveAddress(t,!0);return Promise.resolve(e(r.address)).then(function(e){e?(a.change.push(r),u=0):u++})});if(r&&r.then)return r.then(function(){})}}();return t&&t.then?t.then(function(){return a}):a},n=this,i=(null==t?void 0:t.gapLimit)||20,o=!1!==(null==t?void 0:t.scanChange),s=(null==t?void 0:t.maxScan)||1e4,a={receive:[],change:[]},u=0,c=0,f=X(function(){return c<s&&u<i},function(){return c++},function(){var t=n.deriveAddress(c,!1);return Promise.resolve(e(t.address)).then(function(e){e?(a.receive.push(t),u=0):u++})});return Promise.resolve(f&&f.then?f.then(r):r())}catch(e){return Promise.reject(e)}},n.clearCache=function(){this.cache.clear()},n.getCacheSize=function(){return this.cache.size},t}(),Y=/*#__PURE__*/function(){function e(e){this.service=void 0,this._batch=void 0,this.service=new V(e)}var t=e.prototype;return t.validateMneeTx=function(e,t){try{return Promise.resolve(this.service.validateMneeTx(e,t))}catch(e){return Promise.reject(e)}},t.toAtomicAmount=function(e){return this.service.toAtomicAmount(e)},t.fromAtomicAmount=function(e){return this.service.fromAtomicAmount(e)},t.config=function(){try{return Promise.resolve(this.service.getCosignerConfig())}catch(e){return Promise.reject(e)}},t.balance=function(e){try{return Promise.resolve(this.service.getBalance(e))}catch(e){return Promise.reject(e)}},t.balances=function(e){try{return Promise.resolve(this.service.getBalances(e))}catch(e){return Promise.reject(e)}},t.getUtxos=function(e,t,r,n){try{return Promise.resolve(this.service.getUtxos(e,t,r,n))}catch(e){return Promise.reject(e)}},t.getEnoughUtxos=function(e,t){try{return Promise.resolve(this.service.getEnoughUtxos(e,t))}catch(e){return Promise.reject(e)}},t.getAllUtxos=function(e){try{return Promise.resolve(this.service.getAllUtxos(e))}catch(e){return Promise.reject(e)}},t.transfer=function(e,t,r){try{return Promise.resolve(this.service.transfer(e,t,r))}catch(e){return Promise.reject(e)}},t.transferMulti=function(e,t){try{return Promise.resolve(this.service.transferMulti(e,t))}catch(e){return Promise.reject(e)}},t.submitRawTx=function(e,t){try{return Promise.resolve(this.service.submitRawTx(e,t))}catch(e){return Promise.reject(e)}},t.getTxStatus=function(e){try{return Promise.resolve(this.service.getTxStatus(e))}catch(e){return Promise.reject(e)}},t.recentTxHistory=function(e,t,r,n){try{return Promise.resolve(this.service.getRecentTxHistory(e,t,r,n))}catch(e){return Promise.reject(e)}},t.recentTxHistories=function(e){try{return Promise.resolve(this.service.getRecentTxHistories(e))}catch(e){return Promise.reject(e)}},t.parseTx=function(e,t){try{return Promise.resolve(this.service.parseTx(e,t))}catch(e){return Promise.reject(e)}},t.parseTxFromRawTx=function(e,t){try{return Promise.resolve(this.service.parseTxFromRawTx(e,t))}catch(e){return Promise.reject(e)}},t.parseInscription=function(e){return this.service.parseInscription(e)},t.parseCosignerScripts=function(e){return this.service.parseCosignerScripts(e)},t.HDWallet=function(e,t){return new W(e,t)},t.batch=function(){return this._batch||(this._batch=new H(this.service)),this._batch},t.createInscriptionOutput=function(e,t,r){try{return Promise.resolve(this.service.createInscription(e,t,r))}catch(e){return Promise.reject(e)}},t.fetchSourceTransaction=function(e,t){try{return Promise.resolve(this.service.fetchRawTx(e,t))}catch(e){return Promise.reject(e)}},t.generateSignatures=function(e,t){try{return Promise.resolve(this.service.getSignatures(e,t))}catch(e){return Promise.reject(e)}},t.createSignatureRequests=function(e){return this.service.createSignatureRequests(e)},t.applySignatures=function(e,t){return this.service.applySignatures(e,t)},t.buildUnsignedMneeTransaction=function(e){try{return Promise.resolve(this.service.buildUnsignedMneeTransaction(e))}catch(e){return Promise.reject(e)}},e}();Y.HDWallet=W,module.exports=Y;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|