@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 2.68 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@privy-io/public-api"),t=require("./maybeCreateWalletOnLogin.js");require("../../utils/getUserEmbeddedEthereumWallet.js"),require("../../utils/getAllUserEmbeddedEthereumWallets.js"),require("../../utils/embedded-wallets.js"),require("../../utils/getUserEmbeddedSolanaWallet.js"),require("../../utils/getAllUserEmbeddedSolanaWallets.js"),require("../../utils/shouldCreateEmbeddedEthWallet.js"),require("../../utils/shouldCreateEmbeddedSolWallet.js");exports.default=class{async unlinkWallet(t){await this._privyInternal.fetch(e.SiweUnlink,{body:{address:t}});let i=await this._privyInternal.refreshSession();return{user:i.user,identity_token:i.identity_token}}async linkWithSiwe(t,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(e.SiweLink,{body:{message:r,signature:t,chainId:s.chainId,walletClientType:s.walletClientType,connectorType:s.connectorType}});let a=await this._privyInternal.refreshSession();return{user:a.user,identity_token:a.identity_token}}async loginWithSiwe(i,n,s,r,a){let l=n||this._wallet,d=s||this._preparedMessage;if(!l)throw Error("A wallet must be provided in the init step or as an argument to loginWithSiwe");if(!d)throw Error("A message must be generated and signed before being used to login to privy with a wallet");let o=await this._privyInternal.fetch(e.SiweAuthenticate,{body:{signature:i,message:d,chainId:l.chainId,walletClientType:l.walletClientType,connectorType:l.connectorType,mode:r}});await this._privyInternal.session.updateWithTokensResponse(o);let u=await t.maybeCreateWalletOnLogin(this._embedded,o,a?.embedded);return this._privyInternal.callbacks?.setUser?.(u.user),u}async init(t,i,n){var s;this._wallet=t;let{nonce:r}=await this._privyInternal.fetch(e.SiweInit,{body:{address:t.address}}),a=`${(s={chainId:t.chainId.toString().replace("eip155:",""),address:t.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:i,nonce:r,uri:n}).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}};