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