@keyban/sdk-base
Version:
Keyban Javascript SDK provides core functionalities for the MPC wallet solution, supporting web and Node.js apps with TypeScript, custom storage, and Ethereum blockchain integration.
2 lines • 6.67 kB
JavaScript
import {c,a}from'./chunk-UGMWAWZZ.mjs';import'./chunk-3UK7MQXP.mjs';import {i}from'./chunk-VGFAFYVC.mjs';import {StrKey,rpc,Horizon,xdr,TransactionBuilder,BASE_FEE,Operation,Asset,Transaction}from'@stellar/stellar-sdk';import {Buffer}from'buffer';import {BigNumber}from'bignumber.js';import {ed25519}from'@noble/curves/ed25519';var R=25,w=7;BigNumber.config({DECIMAL_PLACES:w});var d=class extends a{address;publicKey;#t;#e;#r;constructor(t,e,r,s,i){super(t),this.#e=e,this.#r=r,this.#t=s,this.address=i,this.publicKey=i;}async signMessage(t){let e=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(t));return (await this.#t.sign(Buffer.from(e))).toString("base64")}async transfer(t,e,r){if(e<=0n)throw new i(i.types.AmountInvalid,"KeybanAccount.transfer");let[s,i$1]=await Promise.all([this.#e.getAccount(this.address),this.#e.getNetwork()]),a=new TransactionBuilder(s,{fee:r??BASE_FEE,networkPassphrase:i$1.passphrase}),l=BigNumber(e).dividedBy(10**w).toString();await this.#s(t)?a.addOperation(Operation.payment({destination:t,asset:Asset.native(),amount:l})):a.addOperation(Operation.createAccount({destination:t,startingBalance:l}));let c=a.setTimeout(R).build(),h=await this.#t.signDecorated(c.hash());return c.addDecoratedSignature(h),this.#n(c,"transfer")}async estimateTransfer(t){let e=1n,r=await this.#i();return {maxFees:r*e,details:r.toString()}}async transferERC20({contractAddress:t,to:e,value:r,fees:s}){if(!await this.#s(e))throw new i(i.types.AddressInvalid,"KeybanAccount.transferERC20");if(!/^[a-zA-Z0-9]+:[a-zA-Z0-9]+$/.test(t))throw new i(i.types.AddressInvalid,"KeybanAccount.transferERC20");if(e===this.address)throw new i(i.types.RecipientAddressEqualsSender,"KeybanAccount.transferERC20");if(r<=0n)throw new i(i.types.AmountInvalid,"KeybanAccount.transferERC20");let[a,l]=t.split(":"),P=new Asset(a,l),[c,h]=await Promise.all([this.#e.getAccount(this.address),this.#e.getNetwork()]),k=BigNumber(r).dividedBy(10**w).toString(),p=new TransactionBuilder(c,{fee:s??BASE_FEE,networkPassphrase:h.passphrase}).addOperation(Operation.payment({destination:e,asset:P,amount:k})).setTimeout(R).build(),B=await this.#t.signDecorated(p.hash());return p.addDecoratedSignature(B),this.#n(p,"transferERC20")}async estimateERC20Transfer(t){let e=1n,r=await this.#i();return {maxFees:r*e,details:r.toString()}}transferNft(t){throw new Error("transferNft not implemented.")}estimateNftTransfer(t){throw new Error("estimateNftTransfer not implemented.")}async#n(t,e){return this.#e.sendTransaction(t).then(async r=>{if(r.status!=="PENDING")throw JSON.stringify(r).includes("txInsufficientBalance")?new i(i.types.InsufficientFunds,"KeybanAccount.transferERC20"):new i(i.types.UnknownTransactionError,`KeybanAccount.${e}`,{SendTransactionResponse:r.errorResult?.result()??r});return this.#e.pollTransaction(r.hash)}).then(r=>{if(JSON.stringify(r).includes("txInsufficientBalance"))throw new i(i.types.InsufficientFunds,"KeybanAccount.transferERC20");switch(r.status){case rpc.Api.GetTransactionStatus.NOT_FOUND:throw new i(i.types.UnknownTransactionError,`KeybanAccount.${e}`,{transactionResponse:JSON.stringify(r)});case rpc.Api.GetTransactionStatus.FAILED:throw new i(i.types.UnknownTransactionError,`KeybanAccount.${e}`,{transactionResponse:r.resultXdr.result()});case rpc.Api.GetTransactionStatus.SUCCESS:return r.txHash}})}async loyaltySetup(){if(await this.#s(this.address)){console.log("Stellar Account already setup for Loyalty, nothing to do.");return}console.log("Stellar Account not setup for Loyalty, setting it up now...");let e=await this.rpcClient.call("loyalty","initAccountSetup"),{passphrase:r}=await this.#e.getNetwork(),s=new Transaction(e,r),i=await this.#t.signDecorated(s.hash());s.addDecoratedSignature(i),await this.rpcClient.call("loyalty","submitAccountSetup",s.toXDR());}async loyaltyDestroy(){if(!await this.#s(this.address)){console.log("Stellar Account not setup for Loyalty, nothing to destroy.");return}console.log("Stellar Account setup for Loyalty, destroying it now...");let e=await this.rpcClient.call("loyalty","initAccountDestroy"),{passphrase:r}=await this.#e.getNetwork(),s=new Transaction(e,r),i=await this.#t.signDecorated(s.hash());s.addDecoratedSignature(i),await this.rpcClient.call("loyalty","submitAccountDestroy",s.toXDR());}async#s(t){try{return await this.#e.getAccount(t),!0}catch{return false}}async#i(){let{max_fee:t}=await this.#r.feeStats();return BigInt(t.mode)}async signTransaction(t){let e=Buffer.from(t,"hex");return (await this.#t.signDecorated(e)).toXDR("hex")}};var f=class{type="ed25519";#t;#e;#r;constructor(t,e,r){this.#t=t,this.#e=e,this.#r=r;}publicKey(){return StrKey.encodeEd25519PublicKey(this.rawPublicKey())}rawPublicKey(){let t=this.#r.replace(/^0x/,"");return Buffer.from(t,"hex")}async sign(t){let e=await this.#t.call("eddsa","sign",this.#e,t.toString("hex"));return Buffer.from(e,"hex")}async signDecorated(t){let e=await this.sign(t),r=this.signatureHint();return new xdr.DecoratedSignature({hint:r,signature:e})}async signPayloadDecorated(t){let e=await this.sign(t),r=this.signatureHint(),s=Buffer.from(t.slice(-4));return s.length<4&&(s=Buffer.concat([s,Buffer.alloc(4-t.length,0)])),new xdr.DecoratedSignature({hint:s.map((i,a)=>i^r[a]),signature:e})}signatureHint(){let t=this.xdrAccountId().toXDR();return t.slice(t.length-4)}verify(t,e){try{return ed25519.verify(e,t,this.rawPublicKey())}catch{return false}}xdrAccountId(){return xdr.PublicKey.publicKeyTypeEd25519(this.rawPublicKey())}xdrPublicKey(){return xdr.PublicKey.publicKeyTypeEd25519(this.rawPublicKey())}xdrMuxedAccount(t){if(typeof t<"u"){if(typeof t!="string")throw new TypeError(`expected string for ID, got ${typeof t}`);return xdr.MuxedAccount.keyTypeMuxedEd25519(new xdr.MuxedAccountMed25519({id:xdr.Uint64.fromString(t),ed25519:this.rawPublicKey()}))}return xdr.MuxedAccount.keyTypeEd25519(this.rawPublicKey())}};var S=class extends c{#t;constructor(t,e){super(t,e),this.#t=e??Promise.reject(new Error("metadataConfig is required"));}async initialize(){let t=`eddsa:${this.network}`,e=await this.clientShareProvider.get(t);e||(e=await this.rpcClient.call("eddsa","dkg"),await this.clientShareProvider.set(t,e));let[r,s]=await Promise.all([this.#t,this.rpcClient.call("eddsa","publicKey",e)]);if(!r.network.horizonUrl)throw new Error("Missing required horizonUrl in network config");let i=new f(this.rpcClient,e,s),a=StrKey.encodeEd25519PublicKey(Buffer.from(s,"hex"));return new d(this.rpcClient,new rpc.Server(r.network.rpcUrl),new Horizon.Server(r.network.horizonUrl),i,a)}};export{S as StellarClient};//# sourceMappingURL=stellar-REZKRHRW.mjs.map
//# sourceMappingURL=stellar-REZKRHRW.mjs.map