@gardenfi/core 2.1.13 → 2.1.14-beta.1

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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("./index-ByFw33UE.cjs");/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Bt=BigInt(0),pt=BigInt(1);function ft(e,n){if(typeof n!="boolean")throw new Error(e+" boolean expected, got "+n)}function st(e){const n=e.toString(16);return n.length&1?"0"+n:n}function Tt(e){if(typeof e!="string")throw new Error("hex string expected, got "+typeof e);return e===""?Bt:BigInt("0x"+e)}function ut(e){return Tt(N.bytesToHex(e))}function Zt(e){return N.abytes(e),Tt(N.bytesToHex(Uint8Array.from(e).reverse()))}function vt(e,n){return N.hexToBytes(e.toString(16).padStart(n*2,"0"))}function Rt(e,n){return vt(e,n).reverse()}function C(e,n,t){let r;if(typeof n=="string")try{r=N.hexToBytes(n)}catch(i){throw new Error(e+" must be hex string or Uint8Array, cause: "+i)}else if(N.isBytes(n))r=Uint8Array.from(n);else throw new Error(e+" must be hex string or Uint8Array");const s=r.length;if(typeof t=="number"&&s!==t)throw new Error(e+" of length "+t+" expected, got "+s);return r}const wt=e=>typeof e=="bigint"&&Bt<=e;function Pt(e,n,t){return wt(e)&&wt(n)&&wt(t)&&n<=e&&e<t}function Ft(e,n,t,r){if(!Pt(n,t,r))throw new Error("expected valid "+e+": "+t+" <= n < "+r+", got "+n)}function Xt(e){let n;for(n=0;e>Bt;e>>=pt,n+=1);return n}const lt=e=>(pt<<BigInt(e))-pt;function Wt(e,n,t){if(typeof e!="number"||e<2)throw new Error("hashLen must be a number");if(typeof n!="number"||n<2)throw new Error("qByteLen must be a number");if(typeof t!="function")throw new Error("hmacFn must be a function");const r=S=>new Uint8Array(S),s=S=>Uint8Array.of(S);let i=r(e),o=r(e),a=0;const u=()=>{i.fill(1),o.fill(0),a=0},g=(...S)=>t(o,i,...S),c=(S=r(0))=>{o=g(s(0),S),i=g(),S.length!==0&&(o=g(s(1),S),i=g())},E=()=>{if(a++>=1e3)throw new Error("drbg: tried 1000 values");let S=0;const H=[];for(;S<n;){i=g();const q=i.slice();H.push(q),S+=i.length}return N.concatBytes(...H)};return(S,H)=>{u(),c(S);let q;for(;!(q=H(E()));)c();return u(),q}}function xt(e,n,t={}){if(!e||typeof e!="object")throw new Error("expected valid options object");function r(s,i,o){const a=e[s];if(o&&a===void 0)return;const u=typeof a;if(u!==i||a===null)throw new Error(`param "${s}" is invalid: expected ${i}, got ${u}`)}Object.entries(n).forEach(([s,i])=>r(s,i,!1)),Object.entries(t).forEach(([s,i])=>r(s,i,!0))}function St(e){const n=new WeakMap;return(t,...r)=>{const s=n.get(t);if(s!==void 0)return s;const i=e(t,...r);return n.set(t,i),i}}class Lt extends N.Hash{constructor(n,t){super(),this.finished=!1,this.destroyed=!1,N.ahash(n);const r=N.toBytes(t);if(this.iHash=n.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const s=this.blockLen,i=new Uint8Array(s);i.set(r.length>s?n.create().update(r).digest():r);for(let o=0;o<i.length;o++)i[o]^=54;this.iHash.update(i),this.oHash=n.create();for(let o=0;o<i.length;o++)i[o]^=106;this.oHash.update(i),N.clean(i)}update(n){return N.aexists(this),this.iHash.update(n),this}digestInto(n){N.aexists(this),N.abytes(n,this.outputLen),this.finished=!0,this.iHash.digestInto(n),this.oHash.update(n),this.oHash.digestInto(n),this.destroy()}digest(){const n=new Uint8Array(this.oHash.outputLen);return this.digestInto(n),n}_cloneInto(n){n||(n=Object.create(Object.getPrototypeOf(this),{}));const{oHash:t,iHash:r,finished:s,destroyed:i,blockLen:o,outputLen:a}=this;return n=n,n.finished=s,n.destroyed=i,n.blockLen=o,n.outputLen=a,n.oHash=t._cloneInto(n.oHash),n.iHash=r._cloneInto(n.iHash),n}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const zt=(e,n,t)=>new Lt(e,n).update(t).digest();zt.create=(e,n)=>new Lt(e,n);/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const U=BigInt(0),j=BigInt(1),F=BigInt(2),Qt=BigInt(3),kt=BigInt(4),jt=BigInt(5),Ut=BigInt(8);function V(e,n){const t=e%n;return t>=U?t:n+t}function $(e,n,t){let r=e;for(;n-- >U;)r*=r,r%=t;return r}function It(e,n){if(e===U)throw new Error("invert: expected non-zero number");if(n<=U)throw new Error("invert: expected positive modulus, got "+n);let t=V(e,n),r=n,s=U,i=j;for(;t!==U;){const a=r/t,u=r%t,g=s-i*a;r=t,t=u,s=i,i=g}if(r!==j)throw new Error("invert: does not exist");return V(s,n)}function Mt(e,n){const t=(e.ORDER+j)/kt,r=e.pow(n,t);if(!e.eql(e.sqr(r),n))throw new Error("Cannot find square root");return r}function Jt(e,n){const t=(e.ORDER-jt)/Ut,r=e.mul(n,F),s=e.pow(r,t),i=e.mul(n,s),o=e.mul(e.mul(i,F),s),a=e.mul(i,e.sub(o,e.ONE));if(!e.eql(e.sqr(a),n))throw new Error("Cannot find square root");return a}function te(e){if(e<BigInt(3))throw new Error("sqrt is not defined for small field");let n=e-j,t=0;for(;n%F===U;)n/=F,t++;let r=F;const s=dt(e);for(;Nt(s,r)===1;)if(r++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(t===1)return Mt;let i=s.pow(r,n);const o=(n+j)/F;return function(u,g){if(u.is0(g))return g;if(Nt(u,g)!==1)throw new Error("Cannot find square root");let c=t,E=u.mul(u.ONE,i),x=u.pow(g,n),S=u.pow(g,o);for(;!u.eql(x,u.ONE);){if(u.is0(x))return u.ZERO;let H=1,q=u.sqr(x);for(;!u.eql(q,u.ONE);)if(H++,q=u.sqr(q),H===c)throw new Error("Cannot find square root");const L=j<<BigInt(c-H-1),M=u.pow(E,L);c=H,E=u.sqr(M),x=u.mul(x,E),S=u.mul(S,M)}return S}}function ee(e){return e%kt===Qt?Mt:e%Ut===jt?Jt:te(e)}const ne=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function re(e){const n={ORDER:"bigint",MASK:"bigint",BYTES:"number",BITS:"number"},t=ne.reduce((r,s)=>(r[s]="function",r),n);return xt(e,t),e}function oe(e,n,t){if(t<U)throw new Error("invalid exponent, negatives unsupported");if(t===U)return e.ONE;if(t===j)return n;let r=e.ONE,s=n;for(;t>U;)t&j&&(r=e.mul(r,s)),s=e.sqr(s),t>>=j;return r}function $t(e,n,t=!1){const r=new Array(n.length).fill(t?e.ZERO:void 0),s=n.reduce((o,a,u)=>e.is0(a)?o:(r[u]=o,e.mul(o,a)),e.ONE),i=e.inv(s);return n.reduceRight((o,a,u)=>e.is0(a)?o:(r[u]=e.mul(o,r[u]),e.mul(o,a)),i),r}function Nt(e,n){const t=(e.ORDER-j)/F,r=e.pow(n,t),s=e.eql(r,e.ONE),i=e.eql(r,e.ZERO),o=e.eql(r,e.neg(e.ONE));if(!s&&!i&&!o)throw new Error("invalid Legendre symbol result");return s?1:i?0:-1}function ie(e,n){n!==void 0&&N.anumber(n);const t=n!==void 0?n:e.toString(2).length,r=Math.ceil(t/8);return{nBitLength:t,nByteLength:r}}function dt(e,n,t=!1,r={}){if(e<=U)throw new Error("invalid field: expected ORDER > 0, got "+e);let s,i;if(typeof n=="object"&&n!=null){if(r.sqrt||t)throw new Error("cannot specify opts in two arguments");const c=n;c.BITS&&(s=c.BITS),c.sqrt&&(i=c.sqrt),typeof c.isLE=="boolean"&&(t=c.isLE)}else typeof n=="number"&&(s=n),r.sqrt&&(i=r.sqrt);const{nBitLength:o,nByteLength:a}=ie(e,s);if(a>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let u;const g=Object.freeze({ORDER:e,isLE:t,BITS:o,BYTES:a,MASK:lt(o),ZERO:U,ONE:j,create:c=>V(c,e),isValid:c=>{if(typeof c!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof c);return U<=c&&c<e},is0:c=>c===U,isValidNot0:c=>!g.is0(c)&&g.isValid(c),isOdd:c=>(c&j)===j,neg:c=>V(-c,e),eql:(c,E)=>c===E,sqr:c=>V(c*c,e),add:(c,E)=>V(c+E,e),sub:(c,E)=>V(c-E,e),mul:(c,E)=>V(c*E,e),pow:(c,E)=>oe(g,c,E),div:(c,E)=>V(c*It(E,e),e),sqrN:c=>c*c,addN:(c,E)=>c+E,subN:(c,E)=>c-E,mulN:(c,E)=>c*E,inv:c=>It(c,e),sqrt:i||(c=>(u||(u=ee(e)),u(g,c))),toBytes:c=>t?Rt(c,a):vt(c,a),fromBytes:c=>{if(c.length!==a)throw new Error("Field.fromBytes: expected "+a+" bytes, got "+c.length);return t?Zt(c):ut(c)},invertBatch:c=>$t(g,c),cmov:(c,E,x)=>x?E:c});return Object.freeze(g)}function Ct(e){if(typeof e!="bigint")throw new Error("field order must be bigint");const n=e.toString(2).length;return Math.ceil(n/8)}function Vt(e){const n=Ct(e);return n+Math.ceil(n/2)}function se(e,n,t=!1){const r=e.length,s=Ct(n),i=Vt(n);if(r<16||r<i||r>1024)throw new Error("expected "+i+"-1024 bytes of input, got "+r);const o=t?Zt(e):ut(e),a=V(o,n-j)+j;return t?Rt(a,s):vt(a,s)}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const et=BigInt(0),X=BigInt(1);function rt(e,n){const t=n.negate();return e?t:n}function ce(e,n,t){const r=o=>o.pz,s=$t(e.Fp,t.map(r));return t.map((o,a)=>o.toAffine(s[a])).map(e.fromAffine)}function Kt(e,n){if(!Number.isSafeInteger(e)||e<=0||e>n)throw new Error("invalid window size, expected [1.."+n+"], got W="+e)}function gt(e,n){Kt(e,n);const t=Math.ceil(n/e)+1,r=2**(e-1),s=2**e,i=lt(e),o=BigInt(e);return{windows:t,windowSize:r,mask:i,maxNumber:s,shiftBy:o}}function qt(e,n,t){const{windowSize:r,mask:s,maxNumber:i,shiftBy:o}=t;let a=Number(e&s),u=e>>o;a>r&&(a-=i,u+=X);const g=n*r,c=g+Math.abs(a)-1,E=a===0,x=a<0,S=n%2!==0;return{nextN:u,offset:c,isZero:E,isNeg:x,isNegF:S,offsetF:g}}function fe(e,n){if(!Array.isArray(e))throw new Error("array expected");e.forEach((t,r)=>{if(!(t instanceof n))throw new Error("invalid point at index "+r)})}function ae(e,n){if(!Array.isArray(e))throw new Error("array of scalars expected");e.forEach((t,r)=>{if(!n.isValid(t))throw new Error("invalid scalar at index "+r)})}const yt=new WeakMap,Yt=new WeakMap;function mt(e){return Yt.get(e)||1}function At(e){if(e!==et)throw new Error("invalid wNAF")}function ue(e,n){return{constTimeNegate:rt,hasPrecomputes(t){return mt(t)!==1},unsafeLadder(t,r,s=e.ZERO){let i=t;for(;r>et;)r&X&&(s=s.add(i)),i=i.double(),r>>=X;return s},precomputeWindow(t,r){const{windows:s,windowSize:i}=gt(r,n),o=[];let a=t,u=a;for(let g=0;g<s;g++){u=a,o.push(u);for(let c=1;c<i;c++)u=u.add(a),o.push(u);a=u.double()}return o},wNAF(t,r,s){let i=e.ZERO,o=e.BASE;const a=gt(t,n);for(let u=0;u<a.windows;u++){const{nextN:g,offset:c,isZero:E,isNeg:x,isNegF:S,offsetF:H}=qt(s,u,a);s=g,E?o=o.add(rt(S,r[H])):i=i.add(rt(x,r[c]))}return At(s),{p:i,f:o}},wNAFUnsafe(t,r,s,i=e.ZERO){const o=gt(t,n);for(let a=0;a<o.windows&&s!==et;a++){const{nextN:u,offset:g,isZero:c,isNeg:E}=qt(s,a,o);if(s=u,!c){const x=r[g];i=i.add(E?x.negate():x)}}return At(s),i},getPrecomputes(t,r,s){let i=yt.get(r);return i||(i=this.precomputeWindow(r,t),t!==1&&(typeof s=="function"&&(i=s(i)),yt.set(r,i))),i},wNAFCached(t,r,s){const i=mt(t);return this.wNAF(i,this.getPrecomputes(i,t,s),r)},wNAFCachedUnsafe(t,r,s,i){const o=mt(t);return o===1?this.unsafeLadder(t,r,i):this.wNAFUnsafe(o,this.getPrecomputes(o,t,s),r,i)},setWindowSize(t,r){Kt(r,n),Yt.set(t,r),yt.delete(t)}}}function le(e,n,t,r){let s=n,i=e.ZERO,o=e.ZERO;for(;t>et||r>et;)t&X&&(i=i.add(s)),r&X&&(o=o.add(s)),s=s.double(),t>>=X,r>>=X;return{p1:i,p2:o}}function de(e,n,t,r){fe(t,e),ae(r,n);const s=t.length,i=r.length;if(s!==i)throw new Error("arrays of points and scalars must have equal length");const o=e.ZERO,a=Xt(BigInt(s));let u=1;a>12?u=a-3:a>4?u=a-2:a>0&&(u=2);const g=lt(u),c=new Array(Number(g)+1).fill(o),E=Math.floor((n.BITS-1)/u)*u;let x=o;for(let S=E;S>=0;S-=u){c.fill(o);for(let q=0;q<i;q++){const L=r[q],M=Number(L>>BigInt(S)&g);c[M]=c[M].add(t[q])}let H=o;for(let q=c.length-1,L=o;q>0;q--)L=L.add(c[q]),H=H.add(L);if(x=x.add(H),S!==0)for(let q=0;q<u;q++)x=x.double()}return x}function _t(e,n){if(n){if(n.ORDER!==e)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return re(n),n}else return dt(e)}function he(e,n,t={}){if(!n||typeof n!="object")throw new Error(`expected valid ${e} CURVE object`);for(const a of["p","n","h"]){const u=n[a];if(!(typeof u=="bigint"&&u>et))throw new Error(`CURVE.${a} must be positive bigint`)}const r=_t(n.p,t.Fp),s=_t(n.n,t.Fn),o=["Gx","Gy","a","b"];for(const a of o)if(!r.isValid(n[a]))throw new Error(`CURVE.${a} must be valid field element of CURVE.Fp`);return{Fp:r,Fn:s}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */function Ht(e){e.lowS!==void 0&&ft("lowS",e.lowS),e.prehash!==void 0&&ft("prehash",e.prehash)}class we extends Error{constructor(n=""){super(n)}}const K={Err:we,_tlv:{encode:(e,n)=>{const{Err:t}=K;if(e<0||e>256)throw new t("tlv.encode: wrong tag");if(n.length&1)throw new t("tlv.encode: unpadded data");const r=n.length/2,s=st(r);if(s.length/2&128)throw new t("tlv.encode: long form length too big");const i=r>127?st(s.length/2|128):"";return st(e)+i+s+n},decode(e,n){const{Err:t}=K;let r=0;if(e<0||e>256)throw new t("tlv.encode: wrong tag");if(n.length<2||n[r++]!==e)throw new t("tlv.decode: wrong tlv");const s=n[r++],i=!!(s&128);let o=0;if(!i)o=s;else{const u=s&127;if(!u)throw new t("tlv.decode(long): indefinite length not supported");if(u>4)throw new t("tlv.decode(long): byte length is too big");const g=n.subarray(r,r+u);if(g.length!==u)throw new t("tlv.decode: length bytes not complete");if(g[0]===0)throw new t("tlv.decode(long): zero leftmost byte");for(const c of g)o=o<<8|c;if(r+=u,o<128)throw new t("tlv.decode(long): not minimal encoding")}const a=n.subarray(r,r+o);if(a.length!==o)throw new t("tlv.decode: wrong value length");return{v:a,l:n.subarray(r+o)}}},_int:{encode(e){const{Err:n}=K;if(e<ot)throw new n("integer: negative integers are not allowed");let t=st(e);if(Number.parseInt(t[0],16)&8&&(t="00"+t),t.length&1)throw new n("unexpected DER parsing assertion: unpadded hex");return t},decode(e){const{Err:n}=K;if(e[0]&128)throw new n("invalid signature integer: negative");if(e[0]===0&&!(e[1]&128))throw new n("invalid signature integer: unnecessary leading zero");return ut(e)}},toSig(e){const{Err:n,_int:t,_tlv:r}=K,s=C("signature",e),{v:i,l:o}=r.decode(48,s);if(o.length)throw new n("invalid signature: left bytes after parsing");const{v:a,l:u}=r.decode(2,i),{v:g,l:c}=r.decode(2,u);if(c.length)throw new n("invalid signature: left bytes after parsing");return{r:t.decode(a),s:t.decode(g)}},hexFromSig(e){const{_tlv:n,_int:t}=K,r=n.encode(2,t.encode(e.r)),s=n.encode(2,t.encode(e.s)),i=r+s;return n.encode(48,i)}},ot=BigInt(0),it=BigInt(1),ge=BigInt(2),ct=BigInt(3),ye=BigInt(4);function me(e,n,t){function r(s){const i=e.sqr(s),o=e.mul(i,s);return e.add(e.add(o,e.mul(s,n)),t)}return r}function Dt(e,n,t){const{BYTES:r}=e;function s(i){let o;if(typeof i=="bigint")o=i;else{let a=C("private key",i);if(n){if(!n.includes(a.length*2))throw new Error("invalid private key");const u=new Uint8Array(r);u.set(a,u.length-a.length),a=u}try{o=e.fromBytes(a)}catch{throw new Error(`invalid private key: expected ui8a of size ${r}, got ${typeof i}`)}}if(t&&(o=e.create(o)),!e.isValidNot0(o))throw new Error("invalid private key: out of range [1..N-1]");return o}return s}function pe(e,n={}){const{Fp:t,Fn:r}=he("weierstrass",e,n),{h:s,n:i}=e;xt(n,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object",wrapPrivateKey:"boolean"});const{endo:o}=n;if(o&&(!t.is0(e.a)||typeof o.beta!="bigint"||typeof o.splitScalar!="function"))throw new Error('invalid endo: expected "beta": bigint and "splitScalar": function');function a(){if(!t.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function u(O,f,h){const{x:l,y:d}=f.toAffine(),w=t.toBytes(l);if(ft("isCompressed",h),h){a();const p=!t.isOdd(d);return N.concatBytes(Gt(p),w)}else return N.concatBytes(Uint8Array.of(4),w,t.toBytes(d))}function g(O){N.abytes(O);const f=t.BYTES,h=f+1,l=2*f+1,d=O.length,w=O[0],p=O.subarray(1);if(d===h&&(w===2||w===3)){const y=t.fromBytes(p);if(!t.isValid(y))throw new Error("bad point: is not on curve, wrong x");const m=x(y);let B;try{B=t.sqrt(m)}catch(A){const v=A instanceof Error?": "+A.message:"";throw new Error("bad point: is not on curve, sqrt error"+v)}a();const b=t.isOdd(B);return(w&1)===1!==b&&(B=t.neg(B)),{x:y,y:B}}else if(d===l&&w===4){const y=t.fromBytes(p.subarray(f*0,f*1)),m=t.fromBytes(p.subarray(f*1,f*2));if(!S(y,m))throw new Error("bad point: is not on curve");return{x:y,y:m}}else throw new Error(`bad point: got length ${d}, expected compressed=${h} or uncompressed=${l}`)}const c=n.toBytes||u,E=n.fromBytes||g,x=me(t,e.a,e.b);function S(O,f){const h=t.sqr(f),l=x(O);return t.eql(h,l)}if(!S(e.Gx,e.Gy))throw new Error("bad curve params: generator point");const H=t.mul(t.pow(e.a,ct),ye),q=t.mul(t.sqr(e.b),BigInt(27));if(t.is0(t.add(H,q)))throw new Error("bad curve params: a or b");function L(O,f,h=!1){if(!t.isValid(f)||h&&t.is0(f))throw new Error(`bad point coordinate ${O}`);return f}function M(O){if(!(O instanceof I))throw new Error("ProjectivePoint expected")}const W=St((O,f)=>{const{px:h,py:l,pz:d}=O;if(t.eql(d,t.ONE))return{x:h,y:l};const w=O.is0();f==null&&(f=w?t.ONE:t.inv(d));const p=t.mul(h,f),y=t.mul(l,f),m=t.mul(d,f);if(w)return{x:t.ZERO,y:t.ZERO};if(!t.eql(m,t.ONE))throw new Error("invZ was invalid");return{x:p,y}}),D=St(O=>{if(O.is0()){if(n.allowInfinityPoint&&!t.is0(O.py))return;throw new Error("bad point: ZERO")}const{x:f,y:h}=O.toAffine();if(!t.isValid(f)||!t.isValid(h))throw new Error("bad point: x or y not field elements");if(!S(f,h))throw new Error("bad point: equation left != right");if(!O.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function Q(O,f,h,l,d){return h=new I(t.mul(h.px,O),h.py,h.pz),f=rt(l,f),h=rt(d,h),f.add(h)}class I{constructor(f,h,l){this.px=L("x",f),this.py=L("y",h,!0),this.pz=L("z",l),Object.freeze(this)}static fromAffine(f){const{x:h,y:l}=f||{};if(!f||!t.isValid(h)||!t.isValid(l))throw new Error("invalid affine point");if(f instanceof I)throw new Error("projective point not allowed");return t.is0(h)&&t.is0(l)?I.ZERO:new I(h,l,t.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static normalizeZ(f){return ce(I,"pz",f)}static fromBytes(f){return N.abytes(f),I.fromHex(f)}static fromHex(f){const h=I.fromAffine(E(C("pointHex",f)));return h.assertValidity(),h}static fromPrivateKey(f){const h=Dt(r,n.allowedPrivateKeyLengths,n.wrapPrivateKey);return I.BASE.multiply(h(f))}static msm(f,h){return de(I,r,f,h)}precompute(f=8,h=!0){return Y.setWindowSize(this,f),h||this.multiply(ct),this}_setWindowSize(f){this.precompute(f)}assertValidity(){D(this)}hasEvenY(){const{y:f}=this.toAffine();if(!t.isOdd)throw new Error("Field doesn't support isOdd");return!t.isOdd(f)}equals(f){M(f);const{px:h,py:l,pz:d}=this,{px:w,py:p,pz:y}=f,m=t.eql(t.mul(h,y),t.mul(w,d)),B=t.eql(t.mul(l,y),t.mul(p,d));return m&&B}negate(){return new I(this.px,t.neg(this.py),this.pz)}double(){const{a:f,b:h}=e,l=t.mul(h,ct),{px:d,py:w,pz:p}=this;let y=t.ZERO,m=t.ZERO,B=t.ZERO,b=t.mul(d,d),T=t.mul(w,w),A=t.mul(p,p),v=t.mul(d,w);return v=t.add(v,v),B=t.mul(d,p),B=t.add(B,B),y=t.mul(f,B),m=t.mul(l,A),m=t.add(y,m),y=t.sub(T,m),m=t.add(T,m),m=t.mul(y,m),y=t.mul(v,y),B=t.mul(l,B),A=t.mul(f,A),v=t.sub(b,A),v=t.mul(f,v),v=t.add(v,B),B=t.add(b,b),b=t.add(B,b),b=t.add(b,A),b=t.mul(b,v),m=t.add(m,b),A=t.mul(w,p),A=t.add(A,A),b=t.mul(A,v),y=t.sub(y,b),B=t.mul(A,T),B=t.add(B,B),B=t.add(B,B),new I(y,m,B)}add(f){M(f);const{px:h,py:l,pz:d}=this,{px:w,py:p,pz:y}=f;let m=t.ZERO,B=t.ZERO,b=t.ZERO;const T=e.a,A=t.mul(e.b,ct);let v=t.mul(h,w),Z=t.mul(l,p),R=t.mul(d,y),z=t.add(h,l),_=t.add(w,p);z=t.mul(z,_),_=t.add(v,Z),z=t.sub(z,_),_=t.add(h,d);let k=t.add(w,y);return _=t.mul(_,k),k=t.add(v,R),_=t.sub(_,k),k=t.add(l,d),m=t.add(p,y),k=t.mul(k,m),m=t.add(Z,R),k=t.sub(k,m),b=t.mul(T,_),m=t.mul(A,R),b=t.add(m,b),m=t.sub(Z,b),b=t.add(Z,b),B=t.mul(m,b),Z=t.add(v,v),Z=t.add(Z,v),R=t.mul(T,R),_=t.mul(A,_),Z=t.add(Z,R),R=t.sub(v,R),R=t.mul(T,R),_=t.add(_,R),v=t.mul(Z,_),B=t.add(B,v),v=t.mul(k,_),m=t.mul(z,m),m=t.sub(m,v),v=t.mul(z,Z),b=t.mul(k,b),b=t.add(b,v),new I(m,B,b)}subtract(f){return this.add(f.negate())}is0(){return this.equals(I.ZERO)}multiply(f){const{endo:h}=n;if(!r.isValidNot0(f))throw new Error("invalid scalar: out of range");let l,d;const w=p=>Y.wNAFCached(this,p,I.normalizeZ);if(h){const{k1neg:p,k1:y,k2neg:m,k2:B}=h.splitScalar(f),{p:b,f:T}=w(y),{p:A,f:v}=w(B);d=T.add(v),l=Q(h.beta,b,A,p,m)}else{const{p,f:y}=w(f);l=p,d=y}return I.normalizeZ([l,d])[0]}multiplyUnsafe(f){const{endo:h}=n,l=this;if(!r.isValid(f))throw new Error("invalid scalar: out of range");if(f===ot||l.is0())return I.ZERO;if(f===it)return l;if(Y.hasPrecomputes(this))return this.multiply(f);if(h){const{k1neg:d,k1:w,k2neg:p,k2:y}=h.splitScalar(f),{p1:m,p2:B}=le(I,l,w,y);return Q(h.beta,m,B,d,p)}else return Y.wNAFCachedUnsafe(l,f)}multiplyAndAddUnsafe(f,h,l){const d=this.multiplyUnsafe(h).add(f.multiplyUnsafe(l));return d.is0()?void 0:d}toAffine(f){return W(this,f)}isTorsionFree(){const{isTorsionFree:f}=n;return s===it?!0:f?f(I,this):Y.wNAFCachedUnsafe(this,i).is0()}clearCofactor(){const{clearCofactor:f}=n;return s===it?this:f?f(I,this):this.multiplyUnsafe(s)}toBytes(f=!0){return ft("isCompressed",f),this.assertValidity(),c(I,this,f)}toRawBytes(f=!0){return this.toBytes(f)}toHex(f=!0){return N.bytesToHex(this.toBytes(f))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}}I.BASE=new I(e.Gx,e.Gy,t.ONE),I.ZERO=new I(t.ZERO,t.ONE,t.ZERO),I.Fp=t,I.Fn=r;const G=r.BITS,Y=ue(I,n.endo?Math.ceil(G/2):G);return I}function Gt(e){return Uint8Array.of(e?2:3)}function be(e,n,t={}){xt(n,{hash:"function"},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"});const r=n.randomBytes||N.randomBytes,s=n.hmac||((l,...d)=>zt(n.hash,l,N.concatBytes(...d))),{Fp:i,Fn:o}=e,{ORDER:a,BITS:u}=o;function g(l){const d=a>>it;return l>d}function c(l){return g(l)?o.neg(l):l}function E(l,d){if(!o.isValidNot0(d))throw new Error(`invalid signature ${l}: out of range 1..CURVE.n`)}class x{constructor(d,w,p){E("r",d),E("s",w),this.r=d,this.s=w,p!=null&&(this.recovery=p),Object.freeze(this)}static fromCompact(d){const w=o.BYTES,p=C("compactSignature",d,w*2);return new x(o.fromBytes(p.subarray(0,w)),o.fromBytes(p.subarray(w,w*2)))}static fromDER(d){const{r:w,s:p}=K.toSig(C("DER",d));return new x(w,p)}assertValidity(){}addRecoveryBit(d){return new x(this.r,this.s,d)}recoverPublicKey(d){const w=i.ORDER,{r:p,s:y,recovery:m}=this;if(m==null||![0,1,2,3].includes(m))throw new Error("recovery id invalid");if(a*ge<w&&m>1)throw new Error("recovery id is ambiguous for h>1 curve");const b=m===2||m===3?p+a:p;if(!i.isValid(b))throw new Error("recovery id 2 or 3 invalid");const T=i.toBytes(b),A=e.fromHex(N.concatBytes(Gt((m&1)===0),T)),v=o.inv(b),Z=D(C("msgHash",d)),R=o.create(-Z*v),z=o.create(y*v),_=e.BASE.multiplyUnsafe(R).add(A.multiplyUnsafe(z));if(_.is0())throw new Error("point at infinify");return _.assertValidity(),_}hasHighS(){return g(this.s)}normalizeS(){return this.hasHighS()?new x(this.r,o.neg(this.s),this.recovery):this}toBytes(d){if(d==="compact")return N.concatBytes(o.toBytes(this.r),o.toBytes(this.s));if(d==="der")return N.hexToBytes(K.hexFromSig(this));throw new Error("invalid format")}toDERRawBytes(){return this.toBytes("der")}toDERHex(){return N.bytesToHex(this.toBytes("der"))}toCompactRawBytes(){return this.toBytes("compact")}toCompactHex(){return N.bytesToHex(this.toBytes("compact"))}}const S=Dt(o,t.allowedPrivateKeyLengths,t.wrapPrivateKey),H={isValidPrivateKey(l){try{return S(l),!0}catch{return!1}},normPrivateKeyToScalar:S,randomPrivateKey:()=>{const l=a;return se(r(Vt(l)),l)},precompute(l=8,d=e.BASE){return d.precompute(l,!1)}};function q(l,d=!0){return e.fromPrivateKey(l).toBytes(d)}function L(l){if(typeof l=="bigint")return!1;if(l instanceof e)return!0;const w=C("key",l).length,p=i.BYTES,y=p+1,m=2*p+1;if(!(t.allowedPrivateKeyLengths||o.BYTES===y))return w===y||w===m}function M(l,d,w=!0){if(L(l)===!0)throw new Error("first arg must be private key");if(L(d)===!1)throw new Error("second arg must be public key");return e.fromHex(d).multiply(S(l)).toBytes(w)}const W=n.bits2int||function(l){if(l.length>8192)throw new Error("input is too large");const d=ut(l),w=l.length*8-u;return w>0?d>>BigInt(w):d},D=n.bits2int_modN||function(l){return o.create(W(l))},Q=lt(u);function I(l){return Ft("num < 2^"+u,l,ot,Q),o.toBytes(l)}function G(l,d,w=Y){if(["recovered","canonical"].some(z=>z in w))throw new Error("sign() legacy options not supported");const{hash:p}=n;let{lowS:y,prehash:m,extraEntropy:B}=w;y==null&&(y=!0),l=C("msgHash",l),Ht(w),m&&(l=C("prehashed msgHash",p(l)));const b=D(l),T=S(d),A=[I(T),I(b)];if(B!=null&&B!==!1){const z=B===!0?r(i.BYTES):B;A.push(C("extraEntropy",z))}const v=N.concatBytes(...A),Z=b;function R(z){const _=W(z);if(!o.isValidNot0(_))return;const k=o.inv(_),nt=e.BASE.multiply(_).toAffine(),J=o.create(nt.x);if(J===ot)return;const P=o.create(k*o.create(Z+J*T));if(P===ot)return;let ht=(nt.x===J?0:2)|Number(nt.y&it),tt=P;return y&&g(P)&&(tt=c(P),ht^=1),new x(J,tt,ht)}return{seed:v,k2sig:R}}const Y={lowS:n.lowS,prehash:!1},O={lowS:n.lowS,prehash:!1};function f(l,d,w=Y){const{seed:p,k2sig:y}=G(l,d,w);return Wt(n.hash.outputLen,o.BYTES,s)(p,y)}e.BASE.precompute(8);function h(l,d,w,p=O){const y=l;d=C("msgHash",d),w=C("publicKey",w),Ht(p);const{lowS:m,prehash:B,format:b}=p;if("strict"in p)throw new Error("options.strict was renamed to lowS");if(b!==void 0&&!["compact","der","js"].includes(b))throw new Error('format must be "compact", "der" or "js"');const T=typeof y=="string"||N.isBytes(y),A=!T&&!b&&typeof y=="object"&&y!==null&&typeof y.r=="bigint"&&typeof y.s=="bigint";if(!T&&!A)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");let v,Z;try{if(A)if(b===void 0||b==="js")v=new x(y.r,y.s);else throw new Error("invalid format");if(T){try{b!=="compact"&&(v=x.fromDER(y))}catch(tt){if(!(tt instanceof K.Err))throw tt}!v&&b!=="der"&&(v=x.fromCompact(y))}Z=e.fromHex(w)}catch{return!1}if(!v||m&&v.hasHighS())return!1;B&&(d=n.hash(d));const{r:R,s:z}=v,_=D(d),k=o.inv(z),nt=o.create(_*k),J=o.create(R*k),P=e.BASE.multiplyUnsafe(nt).add(Z.multiplyUnsafe(J));return P.is0()?!1:o.create(P.x)===R}return Object.freeze({getPublicKey:q,getSharedSecret:M,sign:f,verify:h,utils:H,Point:e,Signature:x})}function Ee(e){const n={a:e.a,b:e.b,p:e.Fp.ORDER,n:e.n,h:e.h,Gx:e.Gx,Gy:e.Gy},t=e.Fp,r=dt(n.n,e.nBitLength),s={Fp:t,Fn:r,allowedPrivateKeyLengths:e.allowedPrivateKeyLengths,allowInfinityPoint:e.allowInfinityPoint,endo:e.endo,wrapPrivateKey:e.wrapPrivateKey,isTorsionFree:e.isTorsionFree,clearCofactor:e.clearCofactor,fromBytes:e.fromBytes,toBytes:e.toBytes};return{CURVE:n,curveOpts:s}}function Be(e){const{CURVE:n,curveOpts:t}=Ee(e),r={hash:e.hash,hmac:e.hmac,randomBytes:e.randomBytes,lowS:e.lowS,bits2int:e.bits2int,bits2int_modN:e.bits2int_modN};return{CURVE:n,curveOpts:t,ecdsaOpts:r}}function ve(e,n){return Object.assign({},n,{ProjectivePoint:n.Point,CURVE:e})}function xe(e){const{CURVE:n,curveOpts:t,ecdsaOpts:r}=Be(e),s=pe(n,t),i=be(s,r,t);return ve(e,i)}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */function Se(e,n){const t=r=>xe({...e,hash:r});return{...t(n),create:t}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const at={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")};BigInt(0);const Ie=BigInt(1),bt=BigInt(2),Ot=(e,n)=>(e+n/bt)/n;function Ne(e){const n=at.p,t=BigInt(3),r=BigInt(6),s=BigInt(11),i=BigInt(22),o=BigInt(23),a=BigInt(44),u=BigInt(88),g=e*e*e%n,c=g*g*e%n,E=$(c,t,n)*c%n,x=$(E,t,n)*c%n,S=$(x,bt,n)*g%n,H=$(S,s,n)*S%n,q=$(H,i,n)*H%n,L=$(q,a,n)*q%n,M=$(L,u,n)*L%n,W=$(M,a,n)*q%n,D=$(W,t,n)*c%n,Q=$(D,o,n)*H%n,I=$(Q,r,n)*g%n,G=$(I,bt,n);if(!Et.eql(Et.sqr(G),e))throw new Error("Cannot find square root");return G}const Et=dt(at.p,void 0,void 0,{sqrt:Ne}),qe=Se({...at,Fp:Et,lowS:!0,endo:{beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar:e=>{const n=at.n,t=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),r=-Ie*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),s=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),i=t,o=BigInt("0x100000000000000000000000000000000"),a=Ot(i*e,n),u=Ot(-r*e,n);let g=V(e-a*t-u*s,n),c=V(-a*r-u*i,n);const E=g>o,x=c>o;if(E&&(g=n-g),x&&(c=n-c),g>o||c>o)throw new Error("splitScalar: Endomorphism failed, k="+e);return{k1neg:E,k1:g,k2neg:x,k2:c}}}},N.sha256);exports.secp256k1=qe;
@@ -7,6 +7,9 @@ export type { IEVMHTLC } from './lib/evm/htlc.types';
7
7
  export { StarknetRelay } from './lib/starknet/relay/starknetRelay';
8
8
  export type { IStarknetHTLC } from './lib/starknet/starknetHTLC.types';
9
9
  export { StarknetHTLC } from './lib/starknet/htlc/starknetHTLC';
10
+ export { SolanaRelay } from './lib/solana/relayer/solanaRelay';
11
+ export type { ISolanaHTLC } from './lib/solana/htlc/ISolanaHTLC';
12
+ export { SolanaHTLC } from './lib/solana/htlc/solanaHTLC';
10
13
  export { BlockNumberFetcher } from './lib/blockNumberFetcher/blockNumber';
11
14
  export type { IBlockNumberFetcher } from './lib/blockNumberFetcher/blockNumber';
12
15
  export { OrderStatus, SwapStatus } from './lib/orderStatus/status';
@@ -17,4 +20,4 @@ export { Quote } from './lib/quote/quote';
17
20
  export type { IQuote, QuoteResponse, Strategies, } from './lib/quote/quote.types';
18
21
  export { constructOrderPair, validateBTCAddress, toXOnly, resolveApiConfig, } from './lib/utils';
19
22
  export { evmToViemChainMap, switchOrAddNetwork, hyperliquidTestnet, hyperliquid, botanixMainnet as botanix, } from './lib/switchOrAddNetwork';
20
- export { API, STARKNET_CONFIG } from './lib/constants';
23
+ export { API, STARKNET_CONFIG, SolanaRelayerAddress, solanaProgramAddress, } from './lib/constants';
@@ -7,12 +7,18 @@ export type Api = {
7
7
  info: string;
8
8
  evmRelay: string;
9
9
  starknetRelay: string;
10
+ solanaRelay: string;
10
11
  };
11
12
  export declare const API: Record<Environment, Api>;
12
13
  export declare const STARKNET_CONFIG: Record<Network, {
13
14
  chainId: string;
14
15
  nodeUrl: string;
15
16
  }>;
17
+ export declare const SolanaRelayerAddress: Record<Network, string>;
18
+ export declare const solanaProgramAddress: {
19
+ mainnet: string;
20
+ staging: string;
21
+ };
16
22
  export declare const DEFAULT_AFFILIATE_ASSET: {
17
23
  chain: "base";
18
24
  asset: "0xeae7721d779276eb0f5837e2fe260118724a2ba4";
@@ -8,6 +8,7 @@ import { IBitcoinWallet } from '@catalogfi/wallets';
8
8
  import { IBlockNumberFetcher } from '../blockNumberFetcher/blockNumber';
9
9
  import { IEVMHTLC } from '../evm/htlc.types';
10
10
  import { IStarknetHTLC } from '../starknet/starknetHTLC.types';
11
+ import { ISolanaHTLC } from '../solana/htlc/ISolanaHTLC';
11
12
 
12
13
  export declare class Garden extends EventBroker<GardenEvents> implements IGardenJS {
13
14
  private environment;
@@ -21,6 +22,7 @@ export declare class Garden extends EventBroker<GardenEvents> implements IGarden
21
22
  private refundSacpCache;
22
23
  private _evmHTLC;
23
24
  private _starknetHTLC;
25
+ private _solanaHTLC;
24
26
  private _btcWallet;
25
27
  private bitcoinRedeemCache;
26
28
  private _digestKey;
@@ -29,13 +31,20 @@ export declare class Garden extends EventBroker<GardenEvents> implements IGarden
29
31
  static fromWallets(config: GardenConfigWithWallets): Garden;
30
32
  get evmHTLC(): IEVMHTLC | undefined;
31
33
  get starknetHTLC(): IStarknetHTLC | undefined;
34
+ get solanaHTLC(): ISolanaHTLC | undefined;
32
35
  get quote(): IQuote;
33
- get btcWallet(): IBitcoinWallet;
36
+ get btcWallet(): IBitcoinWallet | undefined;
34
37
  get orderbook(): IOrderbook;
35
38
  get blockNumberFetcher(): IBlockNumberFetcher;
36
39
  get secretManager(): ISecretManager;
37
40
  get auth(): IAuth;
38
41
  get digestKey(): DigestKey;
42
+ /**
43
+ * This method takes in the `SwapParams` and returns a placed order
44
+ * It internally calls `getAttestedQuote`, `createOrder` and `pollOrder`
45
+ * @param swapParams
46
+ * @returns MatchedOrder
47
+ */
39
48
  swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
40
49
  private withDefaultAffiliateFees;
41
50
  private validateAndFillParams;
@@ -43,6 +52,7 @@ export declare class Garden extends EventBroker<GardenEvents> implements IGarden
43
52
  private validateAmount;
44
53
  private pollOrder;
45
54
  execute(interval?: number): Promise<() => void>;
55
+ private solRedeem;
46
56
  private evmRedeem;
47
57
  private starknetRedeem;
48
58
  private btcRedeem;
@@ -5,12 +5,14 @@ import { Environment, EventBroker, IAuth, DigestKey } from '@gardenfi/utils';
5
5
  import { ISecretManager } from '../secretManager/secretManager.types';
6
6
  import { IQuote } from '../quote/quote.types';
7
7
  import { IBlockNumberFetcher } from '../blockNumberFetcher/blockNumber';
8
- import { IBitcoinWallet } from '@catalogfi/wallets';
8
+ import { BitcoinWallet, IBitcoinWallet } from '@catalogfi/wallets';
9
9
  import { IEVMHTLC } from '../evm/htlc.types';
10
10
  import { IStarknetHTLC } from '../starknet/starknetHTLC.types';
11
11
  import { AccountInterface } from 'starknet';
12
12
  import { WalletClient } from 'viem';
13
13
  import { Api } from '../constants';
14
+ import { ISolanaHTLC } from '../solana/htlc/ISolanaHTLC';
15
+ import { AnchorProvider } from '@coral-xyz/anchor';
14
16
 
15
17
  export type SwapParams = {
16
18
  /**
@@ -95,6 +97,11 @@ export interface IGardenJS extends EventBroker<GardenEvents> {
95
97
  * @readonly
96
98
  */
97
99
  get starknetHTLC(): IStarknetHTLC | undefined;
100
+ /**
101
+ * The Solana relay.
102
+ * @readonly
103
+ */
104
+ get solanaHTLC(): ISolanaHTLC | undefined;
98
105
  /**
99
106
  * The current quote.
100
107
  * @readonly
@@ -152,17 +159,21 @@ export type GardenCoreConfig = {
152
159
  orderbook?: IOrderbook;
153
160
  quote?: IQuote;
154
161
  blockNumberFetcher?: IBlockNumberFetcher;
162
+ btcWallet?: BitcoinWallet;
163
+ solanaProgramAddress?: string;
155
164
  };
156
165
  export type GardenHTLCModules = {
157
166
  htlc: {
158
167
  evm?: IEVMHTLC;
159
168
  starknet?: IStarknetHTLC;
169
+ solana?: ISolanaHTLC;
160
170
  };
161
171
  };
162
172
  export type GardenWalletModules = {
163
173
  wallets: {
164
174
  evm?: WalletClient;
165
175
  starknet?: AccountInterface;
176
+ solana?: AnchorProvider;
166
177
  };
167
178
  };
168
179
  export type GardenConfigWithWallets = GardenCoreConfig & GardenWalletModules;
@@ -0,0 +1,28 @@
1
+ import { MatchedOrder } from '@gardenfi/orderbook';
2
+ import { AsyncResult } from '@catalogfi/utils';
3
+
4
+ export interface ISolanaHTLC {
5
+ /**
6
+ * The address of the HTLC actor.
7
+ */
8
+ get htlcActorAddress(): string;
9
+ /**
10
+ * Initiates the HTLC by sending funds to the HTLC contract.
11
+ * @param order - The matched order.
12
+ * @returns A promise resolving to the transaction hash of the initiation.
13
+ */
14
+ initiate(order: MatchedOrder): AsyncResult<string, string>;
15
+ /**
16
+ * Redeems funds from the HTLC contract to the actor's address.
17
+ * @param order - The matched order.
18
+ * @param secret - The secret required to unlock the htlc.
19
+ * @returns A promise resolving to the transaction hash of the redemption.
20
+ */
21
+ redeem(order: MatchedOrder, secret: string): AsyncResult<string, string>;
22
+ /**
23
+ * Refunds funds from the HTLC contract back to the actor's address upon expiration.
24
+ * @param order - The matched order.
25
+ * @returns A promise resolving to the transaction hash of the refund.
26
+ */
27
+ refund(order: MatchedOrder): AsyncResult<string, string>;
28
+ }
@@ -0,0 +1,56 @@
1
+ import { AnchorProvider } from '@coral-xyz/anchor';
2
+ import { ISolanaHTLC } from './ISolanaHTLC';
3
+ import { MatchedOrder } from '@gardenfi/orderbook';
4
+ import { AsyncResult } from '@catalogfi/utils';
5
+
6
+ /**
7
+ * SolanaHTLC is an implementation of ISolanaHTLC that performs atomic swaps directly on-chain.
8
+ * As such, fees will be deducted from the initiator.
9
+ * To use a relayer that pays fees on behalf, see SolanaRelay
10
+ */
11
+ export declare class SolanaHTLC implements ISolanaHTLC {
12
+ private provider;
13
+ /**
14
+ * The on-chain Program Derived Address (PDA) that facilitates this swap.
15
+ * Stores the swap state (initiator, redeemer, secrethash etc) and escrows the tokens.
16
+ */
17
+ private swapAccount?;
18
+ private program;
19
+ /**
20
+ * Creates a new instance of SolanaHTLC.
21
+ * @param {AnchorProvider} provider - Solana Provider (abstraction of RPC connection and a Wallet)
22
+ * @throws {Error} If provider is not provided
23
+ */
24
+ constructor(provider: AnchorProvider);
25
+ /**
26
+ * Gets the on-chain address of the atomic swap program.
27
+ * @returns {string} The program's on-chain address in base58 format
28
+ * @throws {Error} If no program ID is found
29
+ */
30
+ get htlcActorAddress(): string;
31
+ /**
32
+ * Initiates a swap by creating a new swap account and locking funds.
33
+ * @param {MatchedOrder} order - The matched order containing swap details
34
+ * @returns {Promise<AsyncResult<string, string>>} A promise that resolves to either:
35
+ * - Ok with the transaction ID on success
36
+ * - Err with an error message on failure
37
+ */
38
+ initiate(order: MatchedOrder): AsyncResult<string, string>;
39
+ /**
40
+ * Redeems a swap by providing the secret.
41
+ * @param {MatchedOrder} order - Matched order object containing swap details
42
+ * @param {string} secret - Secret key in hex format
43
+ * @returns {AsyncResult<string, string>} A promise that resolves to either:
44
+ * - Ok with the transaction ID on success
45
+ * - Err with an error message on failure
46
+ */
47
+ redeem(order: MatchedOrder, secret: string): AsyncResult<string, string>;
48
+ /**
49
+ * Refunds the swap back to the initiator.
50
+ * @param {MatchedOrder} order - Matched order object
51
+ * @returns {AsyncResult<string, string>} A promise that resolves to either:
52
+ * - Ok with the transaction ID on success
53
+ * - Err with an error message on failure
54
+ */
55
+ refund(order: MatchedOrder): AsyncResult<string, string>;
56
+ }
@@ -0,0 +1,299 @@
1
+ /**
2
+ * Program IDL in camelCase format in order to be used in JS/TS.
3
+ *
4
+ * Note that this is only a type helper and is not the actual IDL. The original
5
+ * IDL can be found at `target/idl/solana_native_swaps.json`.
6
+ */
7
+ export type SolanaNativeSwaps = {
8
+ address: '2bag6xpshpvPe7SJ9nSDLHpxqhEAoHPGpEkjNSv7gxoF';
9
+ metadata: {
10
+ name: 'solanaNativeSwaps';
11
+ version: '0.3.0';
12
+ spec: '0.1.0';
13
+ description: 'Created with Anchor';
14
+ };
15
+ instructions: [
16
+ {
17
+ name: 'initiate';
18
+ discriminator: [5, 63, 123, 113, 153, 75, 148, 14];
19
+ accounts: [
20
+ {
21
+ name: 'swapAccount';
22
+ writable: true;
23
+ pda: {
24
+ seeds: [
25
+ {
26
+ kind: 'const';
27
+ value: [115, 119, 97, 112, 95, 97, 99, 99, 111, 117, 110, 116];
28
+ },
29
+ {
30
+ kind: 'account';
31
+ path: 'initiator';
32
+ },
33
+ {
34
+ kind: 'arg';
35
+ path: 'secretHash';
36
+ }
37
+ ];
38
+ };
39
+ },
40
+ {
41
+ name: 'initiator';
42
+ docs: ['Initiator must sign this transaction'];
43
+ writable: true;
44
+ signer: true;
45
+ },
46
+ {
47
+ name: 'systemProgram';
48
+ address: '11111111111111111111111111111111';
49
+ }
50
+ ];
51
+ args: [
52
+ {
53
+ name: 'amountLamports';
54
+ type: 'u64';
55
+ },
56
+ {
57
+ name: 'expiresInSlots';
58
+ type: 'u64';
59
+ },
60
+ {
61
+ name: 'redeemer';
62
+ type: 'pubkey';
63
+ },
64
+ {
65
+ name: 'secretHash';
66
+ type: {
67
+ array: ['u8', 32];
68
+ };
69
+ }
70
+ ];
71
+ },
72
+ {
73
+ name: 'instantRefund';
74
+ discriminator: [211, 202, 103, 41, 183, 147, 59, 251];
75
+ accounts: [
76
+ {
77
+ name: 'swapAccount';
78
+ writable: true;
79
+ },
80
+ {
81
+ name: 'initiator';
82
+ writable: true;
83
+ },
84
+ {
85
+ name: 'redeemer';
86
+ docs: ['Redeemer must sign this transaction'];
87
+ signer: true;
88
+ }
89
+ ];
90
+ args: [];
91
+ },
92
+ {
93
+ name: 'redeem';
94
+ discriminator: [184, 12, 86, 149, 70, 196, 97, 225];
95
+ accounts: [
96
+ {
97
+ name: 'swapAccount';
98
+ writable: true;
99
+ },
100
+ {
101
+ name: 'initiator';
102
+ docs: [
103
+ 'This is included here for the PDA rent refund using the `close` attribute above'
104
+ ];
105
+ writable: true;
106
+ },
107
+ {
108
+ name: 'redeemer';
109
+ writable: true;
110
+ }
111
+ ];
112
+ args: [
113
+ {
114
+ name: 'secret';
115
+ type: {
116
+ array: ['u8', 32];
117
+ };
118
+ }
119
+ ];
120
+ },
121
+ {
122
+ name: 'refund';
123
+ discriminator: [2, 96, 183, 251, 63, 208, 46, 46];
124
+ accounts: [
125
+ {
126
+ name: 'swapAccount';
127
+ writable: true;
128
+ },
129
+ {
130
+ name: 'initiator';
131
+ writable: true;
132
+ }
133
+ ];
134
+ args: [];
135
+ }
136
+ ];
137
+ accounts: [
138
+ {
139
+ name: 'swapAccount';
140
+ discriminator: [53, 126, 9, 14, 14, 197, 105, 182];
141
+ }
142
+ ];
143
+ events: [
144
+ {
145
+ name: 'initiated';
146
+ discriminator: [6, 108, 212, 91, 67, 60, 207, 221];
147
+ },
148
+ {
149
+ name: 'instantRefunded';
150
+ discriminator: [220, 50, 18, 207, 183, 232, 218, 25];
151
+ },
152
+ {
153
+ name: 'redeemed';
154
+ discriminator: [14, 29, 183, 71, 31, 165, 107, 38];
155
+ },
156
+ {
157
+ name: 'refunded';
158
+ discriminator: [35, 103, 149, 246, 196, 123, 221, 99];
159
+ }
160
+ ];
161
+ errors: [
162
+ {
163
+ code: 6000;
164
+ name: 'invalidInitiator';
165
+ msg: 'The provided initiator is not the original initiator of this swap account';
166
+ },
167
+ {
168
+ code: 6001;
169
+ name: 'invalidRedeemer';
170
+ msg: 'The provided redeemer is not the original redeemer of this swap amount';
171
+ },
172
+ {
173
+ code: 6002;
174
+ name: 'invalidSecret';
175
+ msg: 'The provided secret does not correspond to the secret hash in the swap account';
176
+ },
177
+ {
178
+ code: 6003;
179
+ name: 'refundBeforeExpiry';
180
+ msg: 'Attempt to perform a refund before expiry time';
181
+ }
182
+ ];
183
+ types: [
184
+ {
185
+ name: 'initiated';
186
+ type: {
187
+ kind: 'struct';
188
+ fields: [
189
+ {
190
+ name: 'amountLamports';
191
+ type: 'u64';
192
+ },
193
+ {
194
+ name: 'expiresInSlots';
195
+ type: 'u64';
196
+ },
197
+ {
198
+ name: 'initiator';
199
+ type: 'pubkey';
200
+ },
201
+ {
202
+ name: 'redeemer';
203
+ type: 'pubkey';
204
+ },
205
+ {
206
+ name: 'secretHash';
207
+ type: {
208
+ array: ['u8', 32];
209
+ };
210
+ }
211
+ ];
212
+ };
213
+ },
214
+ {
215
+ name: 'instantRefunded';
216
+ type: {
217
+ kind: 'struct';
218
+ fields: [
219
+ {
220
+ name: 'initiator';
221
+ type: 'pubkey';
222
+ },
223
+ {
224
+ name: 'secretHash';
225
+ type: {
226
+ array: ['u8', 32];
227
+ };
228
+ }
229
+ ];
230
+ };
231
+ },
232
+ {
233
+ name: 'redeemed';
234
+ type: {
235
+ kind: 'struct';
236
+ fields: [
237
+ {
238
+ name: 'initiator';
239
+ type: 'pubkey';
240
+ },
241
+ {
242
+ name: 'secret';
243
+ type: {
244
+ array: ['u8', 32];
245
+ };
246
+ }
247
+ ];
248
+ };
249
+ },
250
+ {
251
+ name: 'refunded';
252
+ type: {
253
+ kind: 'struct';
254
+ fields: [
255
+ {
256
+ name: 'initiator';
257
+ type: 'pubkey';
258
+ },
259
+ {
260
+ name: 'secretHash';
261
+ type: {
262
+ array: ['u8', 32];
263
+ };
264
+ }
265
+ ];
266
+ };
267
+ },
268
+ {
269
+ name: 'swapAccount';
270
+ type: {
271
+ kind: 'struct';
272
+ fields: [
273
+ {
274
+ name: 'amountLamports';
275
+ type: 'u64';
276
+ },
277
+ {
278
+ name: 'expirySlot';
279
+ type: 'u64';
280
+ },
281
+ {
282
+ name: 'initiator';
283
+ type: 'pubkey';
284
+ },
285
+ {
286
+ name: 'redeemer';
287
+ type: 'pubkey';
288
+ },
289
+ {
290
+ name: 'secretHash';
291
+ type: {
292
+ array: ['u8', 32];
293
+ };
294
+ }
295
+ ];
296
+ };
297
+ }
298
+ ];
299
+ };