cdp-wallet-onramp-kit
Version:
Complete toolkit for Coinbase Developer Platform (CDP) Embedded Wallets and Onramp integration with reusable components, utilities, and documentation
150 lines (149 loc) • 5.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onrampUrlResponseSchema = exports.sessionTokenResponseSchema = exports.errorResponseSchema = exports.networkSchema = exports.assetSchema = exports.otpSchema = exports.emailSchema = exports.amountSchema = exports.onrampUrlSchema = exports.sessionTokenSchema = exports.walletSessionTokenSchema = exports.guestSessionTokenSchema = exports.addressConfigSchema = exports.walletAddressSchema = void 0;
exports.validateRequest = validateRequest;
exports.createErrorResponse = createErrorResponse;
exports.validateEnvironment = validateEnvironment;
const zod_1 = require("zod");
const server_1 = require("next/server");
/**
* Validation schema for wallet addresses
*/
exports.walletAddressSchema = zod_1.z.string().regex(/^0x[a-fA-F0-9]{40}$/, "Invalid Ethereum wallet address format");
/**
* Validation schema for address with blockchains
*/
exports.addressConfigSchema = zod_1.z.object({
address: exports.walletAddressSchema,
blockchains: zod_1.z.array(zod_1.z.string().min(1)).min(1, "At least one blockchain is required")
});
/**
* Validation schema for session token creation (guest checkout)
*/
exports.guestSessionTokenSchema = zod_1.z.object({
guestCheckout: zod_1.z.literal(true),
assets: zod_1.z.array(zod_1.z.string()).optional().default(['ETH', 'USDC'])
});
/**
* Validation schema for session token creation (with wallet)
*/
exports.walletSessionTokenSchema = zod_1.z.object({
userAddress: exports.walletAddressSchema,
assets: zod_1.z.array(zod_1.z.string()).optional().default(['ETH', 'USDC'])
});
/**
* Combined session token validation schema
*/
exports.sessionTokenSchema = zod_1.z.union([
exports.guestSessionTokenSchema,
exports.walletSessionTokenSchema
]);
/**
* Validation schema for onramp URL generation
*/
exports.onrampUrlSchema = zod_1.z.object({
addresses: zod_1.z.array(exports.addressConfigSchema).min(1, "At least one address is required"),
assets: zod_1.z.array(zod_1.z.string()).optional(),
defaultAsset: zod_1.z.string().optional(),
defaultNetwork: zod_1.z.string().optional(),
presetFiatAmount: zod_1.z.number()
.min(5, "Minimum amount is $5")
.max(500, "Maximum amount is $500 (guest checkout limit)")
.optional(),
presetCryptoAmount: zod_1.z.number().positive("Crypto amount must be positive").optional(),
redirectUrl: zod_1.z.string().url("Invalid redirect URL").optional(),
partnerUserId: zod_1.z.string().max(50, "Partner user ID must be 50 characters or less").optional()
});
/**
* Validation schema for amount input
*/
exports.amountSchema = zod_1.z.number()
.min(5, "Minimum amount is $5")
.max(500, "Maximum amount is $500");
/**
* Validation schema for email input
*/
exports.emailSchema = zod_1.z.string().email("Invalid email address");
/**
* Validation schema for OTP input
*/
exports.otpSchema = zod_1.z.string()
.length(6, "OTP must be 6 digits")
.regex(/^\d{6}$/, "OTP must contain only numbers");
/**
* Common asset symbols validation
*/
exports.assetSchema = zod_1.z.enum([
'ETH', 'USDC', 'BTC', 'USDT', 'DAI', 'WETH', 'MATIC', 'LINK', 'UNI'
]);
/**
* Network validation
*/
exports.networkSchema = zod_1.z.enum([
'base', 'ethereum', 'arbitrum', 'optimism', 'polygon'
]);
/**
* Error response schema
*/
exports.errorResponseSchema = zod_1.z.object({
error: zod_1.z.string(),
details: zod_1.z.any().optional(),
code: zod_1.z.string().optional()
});
/**
* Success response schema for session token
*/
exports.sessionTokenResponseSchema = zod_1.z.object({
sessionToken: zod_1.z.string(),
expiresIn: zod_1.z.number().optional().default(300), // 5 minutes
channelId: zod_1.z.string().optional()
});
/**
* Success response schema for onramp URL
*/
exports.onrampUrlResponseSchema = zod_1.z.object({
url: zod_1.z.string().url(),
expiresIn: zod_1.z.number().optional().default(300), // 5 minutes
sessionToken: zod_1.z.string().optional()
});
/**
* Utility function to validate and parse request data
*/
function validateRequest(schema, data) {
const result = schema.safeParse(data);
if (!result.success) {
const errorMessage = result.error.issues
.map(issue => `${issue.path.join('.')}: ${issue.message}`)
.join('; ');
throw new Error(`Validation failed: ${errorMessage}`);
}
return result.data;
}
/**
* Utility function to create error response
*/
function createErrorResponse(error, statusCode, suggestions) {
return server_1.NextResponse.json({
error,
...(statusCode && { statusCode }),
...(suggestions && suggestions.length > 0 && { suggestions }),
timestamp: new Date().toISOString(),
help: 'Run "npx cdp-wallet-onramp-kit validate" to check your setup'
}, {
status: statusCode || 500
});
}
/**
* Utility function to validate environment variables
*/
function validateEnvironment() {
const required = [
'CDP_API_KEY_PRIVATE_KEY',
'CDP_API_KEY_NAME'
];
const missing = required.filter(key => !process.env[key]);
return {
isValid: missing.length === 0,
missing
};
}