UNPKG

micro-stacks

Version:

Tiny libraries for building Stacks apps.

4 lines (2 loc) 15 kB
function I(){if(typeof self<"u")return self;if(typeof window<"u")return window;if(typeof global<"u")return global;throw new Error("Unexpected runtime environment - no supported global scope (`window`, `self`, `global`) available")}function hr(r){if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Non array buffer passed to arrayBufferToUint8")}function Q(r,t,e){return e?`Use of '${e}' requires \`${t}\` which is unavailable on the '${r}' object within the currently executing environment.`:`\`${t}\` is unavailable on the '${r}' object within the currently executing environment.`}function lr(r,{throwIfUnavailable:t,usageDesc:e,returnEmptyObject:n}={}){let i;try{if(i=I(),i){let o=i[r];if(o)return o}}catch(o){console.error(`Error getting object '${r}' from global scope '${i}': ${o}`);}if(t){let o=Q(i,r.toString(),e);throw console.error(o),new Error(o)}if(n)return {}}function U(r){let t=r.reduce((i,o)=>i+o.length,0),e=new Uint8Array(t),n=0;for(let i=0;i<r.length;i++)e.set(r[i],n),n+=r[i].length;return e}function gr(r){return typeof r=="object"&&(r=Uint8Array.from(r)),r}function B(r){var t;return r!==null&&typeof r=="object"&&((t=r==null?void 0:r.constructor)==null?void 0:t.wordSize)===26&&Array.isArray(r==null?void 0:r.words)}function yr(r){return new TextEncoder().encode(r)}function _(r){return new TextDecoder().decode(r)}var O=new Array(255);for(let r=0;r<=255;++r)O[r]=r.toString(16).padStart(2,"0");function l(r){if(typeof r!="string")throw new TypeError("hexToBytes: expected string, got "+typeof r);if(r.startsWith("0x")&&(r=X(r)),r.length%2)throw new Error(`hexToBytes: received invalid unpadded hex, got: ${r.length}`);let t=new Uint8Array(r.length/2);for(let e=0;e<t.length;e++){let n=e*2;t[e]=Number.parseInt(r.slice(n,n+2),16);}return t}function p(r){let t=new Array(r.length);for(let e=0;e<r.length;++e)t[e]=O[r[e]];return t.join("")}function Tr(r){let t=r.toString(16);return t.length&1?`0${t}`:t}function d(r){if(typeof r!="string")throw new TypeError("hexToNumber: expected string, got "+typeof r);return BigInt(`0x${r}`)}var x=(r,t=8)=>(typeof r=="bigint"?r:y(r,!1)).toString(16).padStart(t*2,"0"),Er=r=>parseInt(r,16),Ir=r=>JSON.parse(_(l(r))),Ur=r=>typeof r=="string"?l(r):r,X=r=>r.startsWith("0x")?r.replace("0x",""):r;var N=BigInt(0),f=BigInt(1);function _r(r,t=128){if(r<-(f<<BigInt(t)-f)||r>(f<<BigInt(t)-f)-f)throw `Integer out of range given ${t} bits to represent.`;return r>=N?r:~(-r-f|~((f<<BigInt(t))-f))}function L(r,t=128){return (r&f<<BigInt(t)-f)>N&&(r=r-(f<<BigInt(t))),r}function Rr(r,t=!1,e=8){return l(x(y(r,t),e))}function Vr(r,t=!1,e=8){return x(y(r,t),e)}function y(r,t=!1){if(typeof r=="number"){if(!Number.isInteger(r))throw new RangeError("Invalid value. Values of type 'number' must be an integer.");return BigInt(r)}if(typeof r=="string")if(r.toLowerCase().startsWith("0x")){let e=r.slice(2);e=e.padStart(e.length+e.length%2,"0"),r=l(e);}else try{return BigInt(r)}catch(e){if(e instanceof SyntaxError)throw new RangeError(`Invalid value. String integer '${r}' is not finite.`)}if(typeof r=="bigint")return r;if(r instanceof Uint8Array)return t?L(d(p(r))):d(p(r));if(B(r))return BigInt(r.toString());throw new TypeError(`Invalid value type. Must be a number, bigint, integer-string, hex-string, BN.js instance, or Buffer, got: ${typeof r}.`)}var kr=r=>d(p(r));function h(r){return `[micro-stacks] ${r}`}var S=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},R=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},V=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},k=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},D=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},v=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor);}},C=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name;}},F=class extends Error{constructor(t){super(t),this.message=h(t),this.name=this.constructor.name;}};function b(r){switch(typeof r){case"string":case"number":case"bigint":case"boolean":case"undefined":case"symbol":case"function":return r}if(r===null)return r;if(ArrayBuffer.isView(r))return r.slice();if(r instanceof Date)return new Date(r);if(r instanceof Array)return r.reduce((t,e,n)=>(t[n]=b(e),t),[]);if(r instanceof Object)return Object.keys(r).reduce((t,e)=>(t[e]=b(r[e]),t),Object.create(Object.getPrototypeOf(r),Object.getOwnPropertyDescriptors(r)))}function z(r){return b(r)}function Cr(r,t){let e=z(r);return delete e[t],e}var rr=typeof window<"u",tr=rr?{referrer:"no-referrer",referrerPolicy:"no-referrer"}:{};async function Hr(r,t={}){return fetch(r,{...tr,...t})}var H="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",M="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";function $(r){let t=r.length;if(t%4>0)throw new Error("Invalid string. Length must be a multiple of 4");let e=r.indexOf("=");e===-1&&(e=t);let n=e===t?0:4-e%4;return [e,n]}function $r(r){let t=$(r),e=t[0],n=t[1];return (e+n)*3/4-n}function er(r,t,e){return (t+e)*3/4-e}function G(r,t){let{revLookup:e}=w(t),n,i=$(r),o=i[0],s=i[1],c=new Uint8Array(er(r,o,s)),u=0,m=s>0?o-4:o,a;for(a=0;a<m;a+=4)n=e[r.charCodeAt(a)]<<18|e[r.charCodeAt(a+1)]<<12|e[r.charCodeAt(a+2)]<<6|e[r.charCodeAt(a+3)],c[u++]=n>>16&255,c[u++]=n>>8&255,c[u++]=n&255;return s===2&&(n=e[r.charCodeAt(a)]<<2|e[r.charCodeAt(a+1)]>>4,c[u++]=n&255),s===1&&(n=e[r.charCodeAt(a)]<<10|e[r.charCodeAt(a+1)]<<4|e[r.charCodeAt(a+2)]>>2,c[u++]=n>>8&255,c[u++]=n&255),c}function W(r){let e=r.length,n=e%4;if(!n)return r;let i=4-n,o=e+i;return r.padEnd(o,"=")}function Gr(r){return G(W(r),H)}function Wr(r){return G(W(r),M)}function nr(r,t){let{lookup:e}=w(t);return e[r>>18&63]+e[r>>12&63]+e[r>>6&63]+e[r&63]}function ir(r,t,e,n){let i,o=[];for(let s=t;s<e;s+=3)i=(r[s]<<16&16711680)+(r[s+1]<<8&65280)+(r[s+2]&255),o.push(nr(i,n));return o.join("")}var A=new Map;function w(r){if(A.has(r))return A.get(r);let t=[],e=[];for(let n=0,i=r.length;n<i;++n)t[n]=r[n],e[r.charCodeAt(n)]=n;return e["-".charCodeAt(0)]=62,e["_".charCodeAt(0)]=63,A.set(r,{lookup:t,revLookup:e}),{lookup:t,revLookup:e}}function P(r,t){let{lookup:e}=w(t),n,i=r.length,o=i%3,s=[],c=16383;for(let u=0,m=i-o;u<m;u+=c)s.push(ir(r,u,u+c>m?m:u+c,t));return o===1?(n=r[i-1],s.push(e[n>>2]+e[n<<4&63]+"==")):o===2&&(n=(r[i-2]<<8)+r[i-1],s.push(e[n>>10]+e[n>>4&63]+e[n<<2&63]+"=")),s.join("")}function Pr(r){return P(r,H)}function jr(r){return P(r,M)}var T="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",j=BigInt(58);function Yr(r){if(r.length===0)return "";typeof r=="string"&&(typeof TextEncoder<"u"?r=new TextEncoder().encode(r):r=new Uint8Array(r.split("").map(n=>n.charCodeAt(0))));let t=BigInt("0x"+p(r)),e=[];for(;t>0;){let n=Number(t%j);t=t/j,e.push(T[n]);}for(let n=0;r[n]===0;n++)e.push(T[0]);return e.reverse().join("")}function Zr(r){if(r.length===0)return new Uint8Array([]);let t=[0];for(let e=0;e<r.length;e++){let n=r[e],i=T.indexOf(n);if(i===void 0)throw new Error(`base58.decode received invalid input. Character '${n}' is not in the base58 alphabet.`);for(let s=0;s<t.length;s++)t[s]*=58;t[0]+=i;let o=0;for(let s=0;s<t.length;s++)t[s]+=o,o=t[s]>>8,t[s]&=255;for(;o>0;)t.push(o&255),o>>=8;}for(let e=0;e<r.length&&r[e]==="1";e++)t.push(0);return new Uint8Array(t.reverse())}function Qr(r){let t=[];for(let e=0;e<r.length;++e)t.push(r.charCodeAt(e)&255);return new Uint8Array(t)}function Xr(r){let t="",e=r.length;for(let n=0;n<e;++n)t+=String.fromCharCode(r[n]&127);return t}function rt(r,t,e,n,i){if(n||(n=0),!i&&i!==0&&(i=r.length),e>=t.length&&(e=t.length),e||(e=0),i>0&&i<n&&(i=n),i===n||t.length===0||r.length===0)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(n<0||n>=r.length)throw new RangeError("Index out of range");if(i<0)throw new RangeError("sourceEnd out of bounds");i>r.length&&(i=r.length),t.length-e<i-n&&(i=t.length-e+n);let o=i-n;return r===t&&typeof Uint8Array.prototype.copyWithin=="function"?r.copyWithin(e,n,i):Uint8Array.prototype.set.call(t,r.subarray(n,i),e),o}var q=class{_value=[];get value(){return this._value}appendHexString(t){this.value.push(l(t));}push(t){return this._value.push(t)}appendByte(t){if(!Number.isInteger(t)||t<0||t>255)throw new Error(`Value ${t} is not a valid byte`);this.value.push(Uint8Array.from([t]));}concatBuffer(){return U(this.value)}};var g={INVALID_ENCODING:"Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.",INVALID_SMARTBUFFER_SIZE:"Invalid size provided. Size must be a valid integer greater than zero.",INVALID_SMARTBUFFER_BUFFER:"Invalid Buffer provided in SmartBufferOptions.",INVALID_SMARTBUFFER_OBJECT:"Invalid SmartBufferOptions object supplied to SmartBuffer constructor or factory methods.",INVALID_OFFSET:"An invalid offset value was provided.",INVALID_OFFSET_NON_NUMBER:"An invalid offset value was provided. A numeric value is required.",INVALID_LENGTH:"An invalid length value was provided.",INVALID_LENGTH_NON_NUMBER:"An invalid length value was provived. A numeric value is required.",INVALID_TARGET_OFFSET:"Target offset is beyond the bounds of the internal SmartBuffer data.",INVALID_TARGET_LENGTH:"Specified length value moves cursor beyong the bounds of the internal SmartBuffer data.",INVALID_READ_BEYOND_BOUNDS:"Attempted to read beyond the bounds of the managed data.",INVALID_WRITE_BEYOND_BOUNDS:"Attempted to write beyond the bounds of the managed data."};function ut(r,t,e){return t=+t,e=e>>>0,r[e]=t&255,e+1}function at(r,t,e){return t=+t,e=e>>>0,r[e]=t&255,r[e+1]=t>>>8,e+2}function ct(r,t,e){return t=+t,e=e>>>0,r[e]=t>>>8,r[e+1]=t&255,e+2}function ft(r,t){return t=t>>>0,r[t]}function pt(r,t){return t=t>>>0,r[t]|r[t+1]<<8}function ht(r,t){return t>>>=0,r[t]|r[t+1]<<8|r[t+2]<<16|r[t+3]<<24}function lt(r,t){return t=t>>>0,(r[t]|r[t+1]<<8|r[t+2]<<16)+r[t+3]*16777216}function gt(r,t){return t=t>>>0,r[t]*16777216+(r[t+1]<<16|r[t+2]<<8|r[t+3])}function mt(r,t,e){return t=+t,e>>>=0,r[e+3]=t>>>24,r[e+2]=t>>>16,r[e+1]=t>>>8,r[e]=t&255,e+4}function dt(r,t,e){return t=+t,e>>>=0,r[e]=t&255,r[e+1]=t>>>8,r[e+2]=t>>>16,r[e+3]=t>>>24,e+4}function yt(r,t,e){return t=+t,e>>>=0,r[e]=t>>>24,r[e+1]=t>>>16,r[e+2]=t>>>8,r[e+3]=t&255,e+4}function K(r,t){return r instanceof t||r!=null&&r.constructor!=null&&r.constructor.name!=null&&r.constructor.name===t.name}function xt(r,t){if(!K(r,Uint8Array)||!K(t,Uint8Array))throw new TypeError('The "buf1", "buf2" arguments must be one of type Uint8Array');if(r===t)return 0;let e=r.length,n=t.length;for(let i=0,o=Math.min(e,n);i<o;++i)if(r[i]!==t[i]){e=r[i],n=t[i];break}return e<n?-1:n<e?1:0}function bt(r,t){let e=typeof t=="number"?t:r.length,n=Math.min(r.length,r.byteOffset+e);return r.slice(r.byteOffset,n)}function or(r){let t=Object.values(r).filter(n=>typeof n=="number"),e=new Set(t);return n=>e.has(n)}var Y=new Map;function E(r,t){let e=Y.get(r);if(e!==void 0)return e(t);let n=or(r);return Y.set(r,n),E(r,t)}function sr(r){return typeof r=="number"&&isFinite(r)&&Math.floor(r)===r}function ur(r){return typeof r=="number"&&isFinite(r)&&sr(r)}function Z(r,t){if(typeof r=="number"){if(!ur(r)||r<0)throw new Error(t?g.INVALID_OFFSET:g.INVALID_LENGTH)}else throw new Error(t?g.INVALID_OFFSET_NON_NUMBER:g.INVALID_LENGTH_NON_NUMBER)}var J=class{buffer;view;_readOffset=0;constructor(t){if(t)this.buffer=t,this.view=new DataView(t.buffer,t.byteOffset,t.byteLength);else {let e=new Uint8Array;this.buffer=e,this.view=new DataView(e.buffer,e.byteOffset,e.byteLength);}}ensureReadable(t,e){let n=this._readOffset;if(typeof e<"u"&&(Z(e,!0),n=e),n<0||n+t>this.buffer.length)throw new Error(g.INVALID_READ_BEYOND_BOUNDS)}_readNumberValue(t,e,n){this.ensureReadable(e,n);let i=t.call(this.view,typeof n=="number"?n:this._readOffset);return typeof n>"u"&&(this._readOffset+=e),i}readBuffer(t){let e=typeof t=="number"?t:this.buffer.length,n=Math.min(this.buffer.length,this._readOffset+e),i=this.buffer.slice(this.buffer.byteOffset+this._readOffset,n);return this._readOffset=n,i}readUInt32BE(t){return this._readNumberValue(this.view.getUint32,4,t)}readUInt8(t){return this._readNumberValue(this.view.getUint8,1,t)}readUInt16BE(t){return this._readNumberValue(this.view.getUint16,2,t)}readBigUIntLE(t){let e=Uint8Array.from(this.readBuffer(t)).reverse(),n=p(e);return BigInt(`0x${n}`)}readBigUIntBE(t){let e=this.readBuffer(t),n=p(e);return BigInt(`0x${n}`)}readBigUInt64BE(t){if(typeof BigInt>"u")throw new Error("Platform does not support JS BigInt type.");return this._readNumberValue(this.view.getBigUint64,8,t)}get readOffset(){return this._readOffset}set readOffset(t){this._readOffset=t;}get internalBuffer(){return this.buffer.buffer}readUInt8Enum(t,e){let n=this.readUInt8();if(E(t,n))return n;throw e(n)}};var ar=(e=>(e[e.Testnet=2147483648]="Testnet",e[e.Mainnet=1]="Mainnet",e))(ar||{}),Ut=1,cr=(e=>(e[e.Mainnet=0]="Mainnet",e[e.Testnet=128]="Testnet",e))(cr||{});/*! micro-base58 - MIT License (c) 2021, Paul Miller (https://paulmillr.com) */ export { q as BufferArray, J as BufferReader, ar as ChainID, Ut as DEFAULT_CHAIN_ID, V as DeserializationError, g as ERRORS, F as InvalidTokenError, C as MissingParametersError, k as NotImplementedError, R as SerializationError, D as SigningError, S as StacksTransactionError, cr as TransactionVersion, v as VerificationError, hr as arrayBufferToUint8, Qr as asciiToBytes, Gr as base64ToBytes, G as base64ToBytesBase, Wr as base64UrlToBytes, $r as byteLength, Xr as bytesToAscii, Pr as bytesToBase64, P as bytesToBase64Base, jr as bytesToBase64Url, kr as bytesToBigInt, p as bytesToHex, _ as bytesToUtf8, Z as checkOffsetOrLengthValue, X as cleanHex, z as cloneDeep, xt as compare, U as concatByteArrays, rt as copy, Zr as decodeB58, Yr as encodeB58, Ur as ensureHexBytes, gr as ensureUint8Array, Hr as fetchPrivate, L as fromTwos, lr as getGlobalObject, Er as hexStringToInt, d as hexToBigInt, l as hexToBytes, Ir as hexToJSON, y as intToBigInt, Rr as intToBytes, Vr as intToHex, x as intToHexString, E as isEnum, ur as isFiniteInteger, sr as isInteger, Tr as numberToHex, Cr as omit, bt as readBuffer, ht as readInt32LE, pt as readUInt16LE, gt as readUInt32BE, lt as readUInt32LE, ft as readUInt8, _r as toTwos, yr as utf8ToBytes, dt as writeInt32LE, ct as writeUInt16BE, at as writeUInt16LE, yt as writeUInt32BE, mt as writeUInt32LE, ut as writeUInt8 };