UNPKG

@privy-io/server-auth

Version:

Server-side client for the Privy API

2 lines (1 loc) 1.84 kB
import{toHex as e}from"viem";import{toAccount as a}from"viem/accounts";import{PrivyClientError as t}from"./errors.mjs";import{r}from"./utils-DDjlFg8J.mjs";import"node-fetch-native";import"ts-case-convert";const i=async i=>{let{walletId:n,address:s,privy:c}=i;return a({address:s,sign:async({hash:e})=>{let{signature:a}=await c.walletApi.ethereum.secp256k1Sign({walletId:n,hash:e});return a},signMessage:async({message:e})=>{let a=o(e),{signature:t}=await c.walletApi.ethereum.signMessage({walletId:n,message:a});return t},signTypedData:async a=>{let{message:i,domain:s,types:o,primaryType:d}=r(a,e);if(!s)throw new t("typedData.domain must be defined");if(!i||!o)throw new t("typedData.message must be defined");let{signature:m}=await c.walletApi.ethereum.signTypedData({walletId:n,typedData:{domain:s,message:i,primaryType:d,types:o}});return m},signTransaction:async e=>{let a=d(e),{signedTransaction:t}=await c.walletApi.ethereum.signTransaction({walletId:n,transaction:a});return t},signAuthorization:async e=>{let a=await c.walletApi.ethereum.sign7702Authorization({walletId:n,contract:e.contractAddress??e.address,chainId:e.chainId,nonce:e.nonce});return{address:a.contract,nonce:Number(a.nonce),chainId:Number(a.chainId),yParity:a.yParity,r:a.r,s:a.s}}})};let n=e=>{if("legacy"===e)return 0;if("eip2930"===e)return 1;if("eip1559"==e||void 0===e)return 2;throw new t("EIP4844 and EIP7702 transaction types are not yet supported.")},s=e=>{if(e)return`0x${e.toString(16)}`},o=e=>"string"==typeof e?e:"string"==typeof e.raw?Uint8Array.from(Buffer.from(e.raw.slice(2),"hex")):e.raw,d=e=>({to:e.to??void 0,nonce:e.nonce,chainId:e.chainId,data:e.data,value:s(e.value),type:n(e.type),gasLimit:s(e.gas),gasPrice:s(e.gasPrice),maxFeePerGas:s(e.maxFeePerGas),maxPriorityFeePerGas:s(e.maxPriorityFeePerGas)});export{i as createViemAccount};