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