UNPKG

@privy-io/server-auth

Version:

Server-side client for the Privy API

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