UNPKG

@dfinity/ckbtc

Version:

A library for interfacing with ckBTC.

8 lines (6 loc) • 10.7 kB
import{a as Et,b as Bt,c as st}from"./chunk-ABDCS67G.js";import{a as _t,b as Ct,c as Ut,d as Lt,e as kt,f as It,g as St,h as Ft,i as Dt,j as Tt,k as Pt,l as Gt,m as Vt,n as ct}from"./chunk-HAU7JR75.js";import{a as nt,b as ot}from"./chunk-FW4363Y4.js";var et=nt(H=>{"use strict";Object.defineProperty(H,"__esModule",{value:!0});H.bech32m=H.bech32=void 0;var M="qpzry9x8gf2tvdw0s3jn54khce6mua7l",Z={};for(let e=0;e<M.length;e++){let t=M.charAt(e);Z[t]=e}function A(e){let t=e>>25;return(e&33554431)<<5^-(t>>0&1)&996825010^-(t>>1&1)&642813549^-(t>>2&1)&513874426^-(t>>3&1)&1027748829^-(t>>4&1)&705979059}function Y(e){let t=1;for(let o=0;o<e.length;++o){let n=e.charCodeAt(o);if(n<33||n>126)return"Invalid prefix ("+e+")";t=A(t)^n>>5}t=A(t);for(let o=0;o<e.length;++o){let n=e.charCodeAt(o);t=A(t)^n&31}return t}function j(e,t,o,n){let s=0,c=0,r=(1<<o)-1,i=[];for(let h=0;h<e.length;++h)for(s=s<<t|e[h],c+=t;c>=o;)c-=o,i.push(s>>c&r);if(n)c>0&&i.push(s<<o-c&r);else{if(c>=t)return"Excess padding";if(s<<o-c&r)return"Non-zero padding"}return i}function ut(e){return j(e,8,5,!0)}function lt(e){let t=j(e,5,8,!1);if(Array.isArray(t))return t}function bt(e){let t=j(e,5,8,!1);if(Array.isArray(t))return t;throw new Error(t)}function tt(e){let t;e==="bech32"?t=1:t=734539939;function o(r,i,h){if(h=h||90,r.length+7+i.length>h)throw new TypeError("Exceeds length limit");r=r.toLowerCase();let x=Y(r);if(typeof x=="string")throw new Error(x);let a=r+"1";for(let f=0;f<i.length;++f){let d=i[f];if(d>>5!==0)throw new Error("Non 5-bit word");x=A(x)^d,a+=M.charAt(d)}for(let f=0;f<6;++f)x=A(x);x^=t;for(let f=0;f<6;++f){let d=x>>(5-f)*5&31;a+=M.charAt(d)}return a}function n(r,i){if(i=i||90,r.length<8)return r+" too short";if(r.length>i)return"Exceeds length limit";let h=r.toLowerCase(),x=r.toUpperCase();if(r!==h&&r!==x)return"Mixed-case string "+r;r=h;let a=r.lastIndexOf("1");if(a===-1)return"No separator character for "+r;if(a===0)return"Missing prefix for "+r;let f=r.slice(0,a),d=r.slice(a+1);if(d.length<6)return"Data too short";let u=Y(f);if(typeof u=="string")return u;let g=[];for(let l=0;l<d.length;++l){let L=d.charAt(l),E=Z[L];if(E===void 0)return"Unknown character "+L;u=A(u)^E,!(l+6>=d.length)&&g.push(E)}return u!==t?"Invalid checksum for "+r:{prefix:f,words:g}}function s(r,i){let h=n(r,i);if(typeof h=="object")return h}function c(r,i){let h=n(r,i);if(typeof h=="object")return h;throw new Error(h)}return{decodeUnsafe:s,decode:c,encode:o,toWords:ut,fromWordsUnsafe:lt,fromWords:bt}}H.bech32=tt("bech32");H.bech32m=tt("bech32m")});var z=(n=>(n[n.Mainnet=0]="Mainnet",n[n.Regtest=1]="Regtest",n[n.Testnet=2]="Testnet",n))(z||{}),X=(c=>(c[c.P2wpkhV0=0]="P2wpkhV0",c[c.P2pkh=1]="P2pkh",c[c.P2sh=2]="P2sh",c[c.P2wsh=3]="P2wsh",c[c.P2tr=4]="P2tr",c))(X||{});var p=class extends Error{},B=class extends p{},k=class extends p{},_=class extends p{},I=class extends p{},S=class extends p{},F=class extends p{},y=class extends p{},D=class extends p{};import{isNullish as rt}from"@dfinity/utils";function it(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function P(e,...t){if(!it(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function W(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function K(e,t){P(e);let o=t.outputLen;if(e.length<o)throw new Error("digestInto() expects output buffer of length at least "+o)}function C(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function G(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function b(e,t){return e<<32-t|e>>>t}function ft(e){if(typeof e!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(e))}function v(e){return typeof e=="string"&&(e=ft(e)),P(e),e}var T=class{};function $(e){let t=n=>e().update(v(n)).digest(),o=e();return t.outputLen=o.outputLen,t.blockLen=o.blockLen,t.create=()=>e(),t}function ht(e,t,o,n){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,o,n);let s=BigInt(32),c=BigInt(4294967295),r=Number(o>>s&c),i=Number(o&c),h=n?4:0,x=n?0:4;e.setUint32(t+h,r,n),e.setUint32(t+x,i,n)}function q(e,t,o){return e&t^~e&o}function J(e,t,o){return e&t^e&o^t&o}var V=class extends T{constructor(t,o,n,s){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=t,this.outputLen=o,this.padOffset=n,this.isLE=s,this.buffer=new Uint8Array(t),this.view=G(this.buffer)}update(t){W(this),t=v(t),P(t);let{view:o,buffer:n,blockLen:s}=this,c=t.length;for(let r=0;r<c;){let i=Math.min(s-this.pos,c-r);if(i===s){let h=G(t);for(;s<=c-r;r+=s)this.process(h,r);continue}n.set(t.subarray(r,r+i),this.pos),this.pos+=i,r+=i,this.pos===s&&(this.process(o,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){W(this),K(t,this),this.finished=!0;let{buffer:o,view:n,blockLen:s,isLE:c}=this,{pos:r}=this;o[r++]=128,C(this.buffer.subarray(r)),this.padOffset>s-r&&(this.process(n,0),r=0);for(let f=r;f<s;f++)o[f]=0;ht(n,s-8,BigInt(this.length*8),c),this.process(n,0);let i=G(t),h=this.outputLen;if(h%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let x=h/4,a=this.get();if(x>a.length)throw new Error("_sha2: outputLen bigger than state");for(let f=0;f<x;f++)i.setUint32(4*f,a[f],c)}digest(){let{buffer:t,outputLen:o}=this;this.digestInto(t);let n=t.slice(0,o);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:o,buffer:n,length:s,finished:c,destroyed:r,pos:i}=this;return t.destroyed=r,t.finished=c,t.length=s,t.pos=i,s%o&&t.buffer.set(n),t}clone(){return this._cloneInto()}},w=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);var at=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),m=new Uint32Array(64),N=class extends V{constructor(t=32){super(64,t,8,!1),this.A=w[0]|0,this.B=w[1]|0,this.C=w[2]|0,this.D=w[3]|0,this.E=w[4]|0,this.F=w[5]|0,this.G=w[6]|0,this.H=w[7]|0}get(){let{A:t,B:o,C:n,D:s,E:c,F:r,G:i,H:h}=this;return[t,o,n,s,c,r,i,h]}set(t,o,n,s,c,r,i,h){this.A=t|0,this.B=o|0,this.C=n|0,this.D=s|0,this.E=c|0,this.F=r|0,this.G=i|0,this.H=h|0}process(t,o){for(let f=0;f<16;f++,o+=4)m[f]=t.getUint32(o,!1);for(let f=16;f<64;f++){let d=m[f-15],u=m[f-2],g=b(d,7)^b(d,18)^d>>>3,l=b(u,17)^b(u,19)^u>>>10;m[f]=l+m[f-7]+g+m[f-16]|0}let{A:n,B:s,C:c,D:r,E:i,F:h,G:x,H:a}=this;for(let f=0;f<64;f++){let d=b(i,6)^b(i,11)^b(i,25),u=a+d+q(i,h,x)+at[f]+m[f]|0,l=(b(n,2)^b(n,13)^b(n,22))+J(n,s,c)|0;a=x,x=h,h=i,i=r+u|0,r=c,c=s,s=n,n=u+l|0}n=n+this.A|0,s=s+this.B|0,c=c+this.C|0,r=r+this.D|0,i=i+this.E|0,h=h+this.F|0,x=x+this.G|0,a=a+this.H|0,this.set(n,s,c,r,i,h,x,a)}roundClean(){C(m)}destroy(){this.set(0,0,0,0,0,0,0,0),C(this.buffer)}};var O=$(()=>new N);var xt="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",Q=xt;function dt(e){if(!e||typeof e!="string")throw new Error(`Expected base58 string but got \u201C${e}\u201D`);if(e.match(/[IOl0]/gmu))throw new Error(`Invalid base58 character \u201C${e.match(/[IOl0]/gmu)}\u201D`);let t=e.match(/^1+/gmu),o=t?t[0].length:0,n=(e.length-o)*(Math.log(58)/Math.log(256))+1>>>0;return new Uint8Array([...new Uint8Array(o),...e.match(/.{1}/gmu).map(s=>Q.indexOf(s)).reduce((s,c)=>(s=s.map(r=>{let i=r*58+c;return c=i>>8,i}),s),new Uint8Array(n)).reverse().filter((s=>c=>s=s||c)(!1))])}var R=dt;var U=ot(et());var pt=0,wt=5,gt=111,mt=196,yt={[pt]:{type:1,networks:[0]},[gt]:{type:1,networks:[2,1]},[wt]:{type:2,networks:[0]},[mt]:{type:2,networks:[2,1]}},At=({address:e,network:t})=>{let n=(a=>{try{return R(a)}catch{throw new B}})(e),{length:s}=n;if(s!==25)throw new y(`Expected the address to be 25 bytes, got ${s}.`);(a=>{let f=a.slice(s-4,s),d=a.slice(0,s-4),u=O.create();u.update(d);let g=O.create();g.update(u.digest());let l=g.digest().slice(0,4);if(f.some((L,E)=>L!==l[E]))throw new y(`Checksum mismatch expected ${f}, got ${l}.`)})(n);let[r]=n,i=yt[r];if(rt(i))throw new _;let{type:h,networks:x}=i;if(!x.includes(t))throw new D;return{address:e,network:t,type:h,parser:"base58"}},Ht=({address:e,network:t})=>{let o=a=>{try{return a.startsWith("bc1p")||a.startsWith("tb1p")||a.startsWith("bcrt1p")?U.bech32m.decode(a):U.bech32.decode(a)}catch{throw new y}},{prefix:n,words:s}=o(e),r={bc:0,tb:2,bcrt:1}[n];if(rt(r))throw new B;if(r!==t)throw new F;let[i,...h]=s;if(i>1)throw new S;switch(U.bech32.fromWords(h).length){case 20:return{address:e,network:t,type:0,parser:"bip-173"};case 32:return{address:e,network:t,type:i===0?3:4,parser:"bip-173"};default:throw new I}},ce=({address:e,network:t=0})=>{switch(e.charAt(0)){case"1":case"2":case"3":case"m":case"n":return At({address:e,network:t});case"b":case"B":case"t":case"T":return Ht({address:e,network:t});case"":throw new k;default:throw new _}};export{st as BitcoinCanister,X as BtcAddressType,z as BtcNetwork,ct as CkBTCMinterCanister,Ut as MinterAlreadyProcessingError,Ft as MinterAmountTooLowError,_t as MinterGenericError,Tt as MinterInsufficientAllowanceError,Dt as MinterInsufficientFundsError,St as MinterMalformedAddressError,kt as MinterNoNewUtxosError,It as MinterRetrieveBtcError,Ct as MinterTemporaryUnavailableError,Lt as MinterUpdateBalanceError,I as ParseBtcAddressBadWitnessLengthError,p as ParseBtcAddressError,B as ParseBtcAddressInvalidError,y as ParseBtcAddressMalformedAddressError,k as ParseBtcAddressNoDataError,F as ParseBtcAddressUnexpectedHumanReadablePartError,_ as ParseBtcAddressUnsupportedAddressTypeError,S as ParseBtcAddressUnsupportedWitnessVersionError,D as ParseBtcAddressWrongNetworkError,Gt as createRetrieveBtcError,Vt as createRetrieveBtcWithApprovalError,Pt as createUpdateBalanceError,ce as parseBtcAddress,Bt as toGetBalanceParams,Et as toGetUtxosParams}; /*! Bundled license information: @noble/hashes/esm/utils.js: (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *) */ //# sourceMappingURL=index.js.map