UNPKG

@privy-io/server-auth

Version:

Server-side client for the Privy API

2 lines (1 loc) 1.83 kB
import{base58 as e}from"@scure/base";import{isHex as t}from"viem";import{getWalletApiImportInitPath as r,getWalletApiImportSubmitPath as p}from"../paths.mjs";import{encryptHPKEMessage as n,translateOwnerInput as a,translateSignersInput as o}from"./utils.mjs";import"@hpke/chacha20poly1305";import"@hpke/core";import"@noble/curves/p256";import"@noble/hashes/sha256";import"canonicalize";import"../constants.mjs";import"../errors.mjs";const i=async(i,y)=>{let s=(({entropy:r,entropyType:p,chainType:n})=>{if("private-key"!==p)throw Error("Invalid entropy type: Only private key entropy is supported");switch(n){case"ethereum":let p=r.startsWith("0x")?r:`0x${r}`;if(!t(p))throw Error("Invalid entropy: Ethereum entropy must be hex encoded");return new Uint8Array(Buffer.from(p.replace(/^0x/,""),"hex"));case"solana":let a;try{a=e.decode(r)}catch(e){throw Error("Invalid entropy: Solana entropy must be base58 encoded")}return a;default:throw Error(`Unsupported chain type: ${n}`)}})({entropy:y.entropy,entropyType:y.entropyType,chainType:y.chainType}),{encryption_public_key:c}=await(async({apiClient:e,address:t,chainType:p,entropyType:n})=>{let{data:a}=await e.post(r(),{chain_type:p,address:t,entropy_type:n,encryption_type:"HPKE"});return a})({apiClient:i,...y}),{encapsulatedKey:h,ciphertext:l}=await n({encryptionPublicKey:new Uint8Array(Buffer.from(c,"base64")),plaintext:s});return await(async({apiClient:e,input:t,encapsulatedKey:r,ciphertext:n})=>{let{chainType:i,address:y,entropyType:s}=t,c={wallet:{chain_type:i,address:y,encryption_type:"HPKE",ciphertext:n,encapsulated_key:r,entropy_type:s},...a({input:t}),...o({input:t})},{data:h}=await e.post(p(),c);return h})({apiClient:i,input:y,encapsulatedKey:Buffer.from(h).toString("base64"),ciphertext:Buffer.from(l).toString("base64")})};export{i as handleWalletImport};