UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 2.64 kB
import{SiweUnlink as e,SiweLink as t,SiweAuthenticate as i,SiweInit as n}from"@privy-io/routes";import{maybeCreateWalletOnLogin as s}from"./maybeCreateWalletOnLogin.mjs";import"../../utils/getUserEmbeddedEthereumWallet.mjs";import"../../utils/getAllUserEmbeddedEthereumWallets.mjs";import"../../utils/embedded-wallets.mjs";import"../../utils/getUserEmbeddedSolanaWallet.mjs";import"../../utils/getAllUserEmbeddedSolanaWallets.mjs";import"../../utils/shouldCreateEmbeddedEthWallet.mjs";import"../../utils/shouldCreateEmbeddedSolWallet.mjs";class r{async unlinkWallet(t){await this._privyInternal.fetch(e,{body:{address:t}});let i=await this._privyInternal.refreshSession();return{user:i.user,identity_token:i.identity_token}}async linkWithSiwe(e,i,n){let s=i||this._wallet,r=n||this._preparedMessage;if(!s)throw Error("A wallet must be provided in the init step or as an argument to linkWithSiwe");if(!r)throw Error("A message must be generated and signed before being used to link a wallet to privy");await this._privyInternal.fetch(t,{body:{message:r,signature:e,chainId:s.chainId,walletClientType:s.walletClientType??null,connectorType:s.connectorType??null}});let a=await this._privyInternal.refreshSession();return{user:a.user,identity_token:a.identity_token}}async loginWithSiwe(e,t,n,r,a){let l=t||this._wallet,o=n||this._preparedMessage;if(!l)throw Error("A wallet must be provided in the init step or as an argument to loginWithSiwe");if(!o)throw Error("A message must be generated and signed before being used to login to privy with a wallet");let d=await this._privyInternal.fetch(i,{body:{signature:e,message:o,chainId:l.chainId,walletClientType:l.walletClientType??null,connectorType:l.connectorType??null,mode:r}});await this._privyInternal.session.updateWithTokensResponse(d);let h=await s(this._embedded,d,a?.embedded);return this._privyInternal.callbacks?.setUser?.(h.user),h}async init(e,t,i){var s;this._wallet=e;let{nonce:r}=await this._privyInternal.fetch(n,{body:{address:e.address}}),a=`${(s={chainId:e.chainId.toString().replace("eip155:",""),address:e.address,issuedAt:(new Date).toISOString(),statement:"By signing, you are proving you own this wallet and logging in. This does not initiate a transaction or cost any fees.",domain:t,nonce:r,uri:i}).domain} wants you to sign in with your Ethereum account:\n${s.address}\n\n${s.statement}\n\nURI: ${s.uri}\nVersion: 1\nChain ID: ${s.chainId}\nNonce: ${s.nonce}\nIssued At: ${s.issuedAt}\nResources:\n- https://privy.io`;return this._preparedMessage=a,{nonce:r,message:a}}constructor(e,t){this._wallet=void 0,this._privyInternal=e,this._embedded=t}}export{r as default};