UNPKG

@privy-io/server-auth

Version:

Server-side client for the Privy API

2 lines (1 loc) 2.19 kB
import{PrivyApiError as a}from"../../errors.mjs";import{getRpcPathForRequest as i,extractIdempotencyKeyHeader as t,createAuthorizationSignatureHeader as e}from"../utils.mjs";import"@hpke/chacha20poly1305";import"@hpke/core";import"@noble/curves/p256";import"@noble/hashes/sha256";import"canonicalize";import"../../constants.mjs";import"../../paths.mjs";class r{async signMessage(a){let r=await i(a,this.api),s={method:"signMessage",params:{message:Buffer.from(a.message).toString("base64"),encoding:"base64"}},o=t(a),n=e({method:"POST",url:this.api.baseURL+r,body:s,appId:this.appId,idempotencyKeyHeader:o,authorizationPrivateKey:this.authorizationPrivateKey}),{data:p}=await this.api.post(r,s,{headers:{...n,...o}});return{signature:Buffer.from(p.data.signature,"base64")}}async signTransaction(a){let r=await i(a,this.api),s="version"in a.transaction,o={method:"signTransaction",params:{transaction:Buffer.from(a.transaction.serialize({verifySignatures:!1})).toString("base64"),encoding:"base64"}},n=t(a),p=e({method:"POST",url:this.api.baseURL+r,body:o,appId:this.appId,idempotencyKeyHeader:n,authorizationPrivateKey:this.authorizationPrivateKey}),{data:h}=await this.api.post(r,o,{headers:{...p,...n}}),d=Buffer.from(h.data.signed_transaction,"base64"),{Transaction:c,VersionedTransaction:m}=await import("@solana/web3.js");return{signedTransaction:s?m.deserialize(d):c.from(d)}}async signAndSendTransaction(r){let s=await i(r,this.api),o={method:"signAndSendTransaction",caip2:r.caip2,params:{transaction:Buffer.from(r.transaction.serialize({verifySignatures:!1})).toString("base64"),encoding:"base64"},sponsor:r.sponsor},n=t(r),p=e({method:"POST",url:this.api.baseURL+s,body:o,appId:this.appId,idempotencyKeyHeader:n,authorizationPrivateKey:this.authorizationPrivateKey}),{data:h}=await this.api.post(s,o,{headers:{...p,...n}});if(h.data)return{hash:h.data.hash,caip2:h.data.caip2};if(h.error)throw new a(h.error.code,null,h.error.message);throw Error("Unexpected API response to signAndSendTransaction")}constructor({appId:a,api:i,authorizationPrivateKey:t}){this.authorizationPrivateKey=null,this.api=i,this.appId=a,t&&(this.authorizationPrivateKey=t)}}export{r as SolanaRpcApi};