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