@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.87 kB
JavaScript
import {c,a}from'./chunk-UGMWAWZZ.mjs';import'./chunk-3UK7MQXP.mjs';import {i}from'./chunk-VGFAFYVC.mjs';import {http,createPublicClient,hashTypedData,serializeTransaction,keccak256,parseSignature,hashMessage,createWalletClient,isAddress,getContract,erc20Abi,EstimateGasExecutionError,InsufficientFundsError,erc721Abi}from'viem';import {toAccount,publicKeyToAddress}from'viem/accounts';import*as d from'viem/chains';var E=[{type:"function",name:"safeTransferFrom",inputs:[{name:"from",type:"address",internalType:"address"},{name:"to",type:"address",internalType:"address"},{name:"id",type:"uint256",internalType:"uint256"},{name:"value",type:"uint256",internalType:"uint256"},{name:"data",type:"bytes",internalType:"bytes"}],outputs:[],stateMutability:"nonpayable"}],u=class extends a{#t;#e;constructor(a,t,s){super(a),this.#t=t,this.#e=s;}get address(){return this.#e.account.address.toLowerCase()}get publicKey(){return this.#e.account.publicKey.toLowerCase()}async signMessage(a){return this.#e.signMessage({message:a})}async transfer(a,t,s){if(!isAddress(a))throw new i(i.types.AddressInvalid,"KeybanAccount.transfer");if(t<=0n)throw new i(i.types.AmountInvalid,"KeybanAccount.transfer");return this.#e.sendTransaction({to:a,value:t,type:"eip1559",...s}).catch(n=>{throw n.cause})}async estimateTransfer(a){let[{maxFeePerGas:t,maxPriorityFeePerGas:s},n]=await Promise.all([this.#t.estimateFeesPerGas({type:"eip1559"}),this.#t.estimateGas({to:a,account:this.address})]);return {maxFees:t*n,details:{maxFeePerGas:t,maxPriorityFeePerGas:s}}}async transferERC20(a){let{contractAddress:t,to:s,value:n,fees:r}=a;if(!isAddress(s))throw new i(i.types.AddressInvalid,"KeybanAccount.transferERC20");if(!isAddress(t))throw new i(i.types.AddressInvalid,"KeybanAccount.transferERC20");if(s===this.address)throw new i(i.types.RecipientAddressEqualsSender,"KeybanAccount.transferERC20");if(n<=0n)throw new i(i.types.AmountInvalid,"KeybanAccount.transferERC20");return getContract({address:t,abi:erc20Abi,client:{public:this.#t,wallet:this.#e}}).write.transfer([s,n],r).catch(i$1=>{switch(true){case i$1.cause.cause instanceof InsufficientFundsError:case i$1.cause.cause instanceof EstimateGasExecutionError:throw new i(i.types.InsufficientFunds,"KeybanAccount.transferERC20");default:throw i$1.cause}})}async estimateERC20Transfer(a){let{contractAddress:t,to:s,value:n}=a,[{maxFeePerGas:r,maxPriorityFeePerGas:c},i]=await Promise.all([this.#t.estimateFeesPerGas({type:"eip1559"}),this.#t.estimateContractGas({address:t,abi:erc20Abi,functionName:"transfer",args:[s,n],account:this.address})]);return {maxFees:r*i,details:{maxFeePerGas:r,maxPriorityFeePerGas:c}}}async transferNft(a){let{contractAddress:t,tokenId:s,to:n,value:r,standard:c,fees:i$1}=a;if(!isAddress(n))throw new i(i.types.AddressInvalid,"KeybanAccount.transferNft");if(!isAddress(t))throw new i(i.types.AddressInvalid,"KeybanAccount.transferNft");if(n===this.address)throw new i(i.types.RecipientAddressEqualsSender,"KeybanAccount.transferNft");if(c==="ERC1155"){if(r===void 0)throw new i(i.types.AmountRequired,"KeybanAccount.transferNft");if(r<=0n)throw new i(i.types.AmountInvalid,"KeybanAccount.transferNft");return this.#a({contractAddress:t,tokenId:s,value:r,to:n,fees:i$1})}if(c==="ERC721"){if(r!==void 0&&r!==1n)throw new i(i.types.AmountIrrelevant,"KeybanAccount.transferNft");return this.#s({contractAddress:t,tokenId:s,to:n,fees:i$1})}throw new i(i.types.InvalidNftStandard,"KeybanAccount.transferNft")}async#s({contractAddress:a,tokenId:t,to:s,fees:n}){let r=getContract({address:a,abi:erc721Abi,client:{public:this.#t,wallet:this.#e}}),c=this.address;return r.write.transferFrom([c,s,t],n).catch(i$1=>{switch(true){case i$1.cause.cause instanceof InsufficientFundsError:case i$1.cause.cause instanceof EstimateGasExecutionError:throw new i(i.types.InsufficientFunds,"KeybanAccount.transferNft");default:throw i$1.cause}})}async#a({contractAddress:a,tokenId:t,value:s,to:n,fees:r}){let c=getContract({address:a,abi:E,client:{public:this.#t,wallet:this.#e}}),i$1=this.address;return c.write.safeTransferFrom([i$1,n,t,s,""],r).catch(o=>{switch(true){case o.cause.cause instanceof InsufficientFundsError:case o.cause.cause instanceof EstimateGasExecutionError:throw new i(i.types.InsufficientFunds,"KeybanAccount.transferNft");default:throw o.cause}})}async estimateNftTransfer(a){let{standard:t,contractAddress:s,tokenId:n,to:r,value:c}=a;if(t==="ERC1155")return this.#r({contractAddress:s,tokenId:n,to:r,value:c});if(t==="ERC721"){if(c!==void 0&&c!==1n)throw new i(i.types.AmountIrrelevant,"KeybanAccount.transferNft");return this.#n({contractAddress:s,tokenId:n,to:r})}throw new i(i.types.InvalidNftStandard,"KeybanAccount.estimateNftTransfer")}async#n({contractAddress:a,tokenId:t,to:s}){let n=this.address,[{maxFeePerGas:r,maxPriorityFeePerGas:c},i]=await Promise.all([this.#t.estimateFeesPerGas({type:"eip1559"}),this.#t.estimateContractGas({address:a,abi:erc721Abi,functionName:"transferFrom",args:[n,s,t],account:this.address})]);return {maxFees:r*i,details:{maxFeePerGas:r,maxPriorityFeePerGas:c}}}async#r({contractAddress:a,tokenId:t,to:s,value:n}){let r=this.address,[{maxFeePerGas:c,maxPriorityFeePerGas:i},o]=await Promise.all([this.#t.estimateFeesPerGas({type:"eip1559"}),this.#t.estimateContractGas({address:a,abi:E,functionName:"safeTransferFrom",args:[r,s,t,n,""],account:this.address})]);return {maxFees:c*o,details:{maxFeePerGas:c,maxPriorityFeePerGas:i}}}async signTransaction(a){throw new Error("Unimplemented")}};var h=class extends c{#t;constructor(a,t){super(a,t);let s=this.metadataConfig.then(c=>http(c.network.rpcUrl)),n={EthereumAnvil:d.anvil,PolygonAmoy:d.polygonAmoy,StarknetDevnet:null,StarknetSepolia:null,StarknetMainnet:null,StellarQuickstart:null,StellarTestnet:null,StellarMainnet:null}[this.network],r=s.then(c=>createPublicClient({chain:n,transport:c}));this.#t={chain:n,transport:s,publicClient:r};}async initialize(){let a=`ecdsa:${this.network}`,t=await this.clientShareProvider.get(a);t||(t=await this.rpcClient.call("ecdsa","dkg"),await this.clientShareProvider.set(a,t));let s=await this.rpcClient.call("ecdsa","publicKey",t),n=toAccount({address:publicKeyToAddress(s),signMessage:async({message:i})=>{let o=hashMessage(i,"hex");return this.rpcClient.call("ecdsa","sign",t,o)},signTransaction:async(i,o)=>{let y=o?.serializer??serializeTransaction,A=i.type==="eip4844"?{...i,sidecars:false}:i,T=await this.rpcClient.call("ecdsa","sign",t,keccak256(y(A))).then(parseSignature);return y(i,T)},signTypedData:async i=>{let o=hashTypedData(i);return this.rpcClient.call("ecdsa","sign",t,o)}});n.publicKey=s;let r=await this.#t.publicClient,c=createWalletClient({chain:r.chain,transport:await this.#t.transport,account:n});return new u(this.rpcClient,r,c)}};export{h as KeybanEvmClient};//# sourceMappingURL=evm-HH7W4XO7.mjs.map
//# sourceMappingURL=evm-HH7W4XO7.mjs.map