@bvdaniel/confidential-pay 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/ConfidentialUSDC.css +105 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +35 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.js +103 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 A0x
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
.cpw {
|
|
2
|
+
--c-fg: #e8f6f6;
|
|
3
|
+
--c-muted: #6b8585;
|
|
4
|
+
--c-line: #1e3333;
|
|
5
|
+
--c-accent: #00ffff;
|
|
6
|
+
|
|
7
|
+
display: flex;
|
|
8
|
+
flex-direction: column;
|
|
9
|
+
gap: 12px;
|
|
10
|
+
color: var(--c-fg);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.cpw-label {
|
|
14
|
+
font-size: 11px;
|
|
15
|
+
text-transform: uppercase;
|
|
16
|
+
letter-spacing: 0.22em;
|
|
17
|
+
color: var(--c-accent);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.cpw-field-label {
|
|
21
|
+
font-size: 12px;
|
|
22
|
+
letter-spacing: 0.08em;
|
|
23
|
+
text-transform: uppercase;
|
|
24
|
+
color: var(--c-muted);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.cpw-input {
|
|
28
|
+
width: 100%;
|
|
29
|
+
box-sizing: border-box;
|
|
30
|
+
padding: 10px 12px;
|
|
31
|
+
border-radius: 6px;
|
|
32
|
+
border: 1px solid var(--c-line);
|
|
33
|
+
background: rgba(5, 6, 7, 0.8);
|
|
34
|
+
color: var(--c-fg);
|
|
35
|
+
font-size: 16px;
|
|
36
|
+
font-family: var(--font-geist-mono, ui-monospace, monospace);
|
|
37
|
+
transition: border-color 150ms ease;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.cpw-input:focus {
|
|
41
|
+
outline: none;
|
|
42
|
+
border-color: var(--c-accent);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.cpw-input:disabled {
|
|
46
|
+
opacity: 0.6;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.cpw-recipient {
|
|
50
|
+
font-size: 12px;
|
|
51
|
+
font-family: var(--font-geist-mono, ui-monospace, monospace);
|
|
52
|
+
color: var(--c-muted);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.cpw-recipient-addr {
|
|
56
|
+
color: var(--c-accent);
|
|
57
|
+
word-break: break-all;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
.cpw-button {
|
|
61
|
+
padding: 12px;
|
|
62
|
+
border-radius: 6px;
|
|
63
|
+
background: var(--c-accent);
|
|
64
|
+
color: #050607;
|
|
65
|
+
border: none;
|
|
66
|
+
font-family: var(--font-geist-mono, ui-monospace, monospace);
|
|
67
|
+
font-size: 13px;
|
|
68
|
+
font-weight: 600;
|
|
69
|
+
letter-spacing: 0.06em;
|
|
70
|
+
cursor: pointer;
|
|
71
|
+
transition: box-shadow 150ms ease, opacity 150ms ease;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.cpw-button:hover:not(:disabled) {
|
|
75
|
+
box-shadow: 0 0 20px -4px var(--c-accent);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.cpw-button.is-sending {
|
|
79
|
+
cursor: wait;
|
|
80
|
+
opacity: 0.7;
|
|
81
|
+
animation: cpw-pulse 1.2s ease-in-out infinite;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@keyframes cpw-pulse {
|
|
85
|
+
0%, 100% { opacity: 0.7; }
|
|
86
|
+
50% { opacity: 1; }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.cpw-status {
|
|
90
|
+
font-size: 12px;
|
|
91
|
+
font-family: var(--font-geist-mono, ui-monospace, monospace);
|
|
92
|
+
word-break: break-all;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.cpw-status.is-success {
|
|
96
|
+
color: #00ff88;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.cpw-status.is-error {
|
|
100
|
+
color: #ff5c5c;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.cpw-hash {
|
|
104
|
+
color: var(--c-accent);
|
|
105
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type ConfidentialUSDCProps = {
|
|
2
|
+
/** Wallet or email address receiving the payment. */
|
|
3
|
+
recipient: string;
|
|
4
|
+
/** Default amount in USDC (decimal string). */
|
|
5
|
+
amount?: string;
|
|
6
|
+
/** Endpoint that proxies to CDP. Defaults to the conventional /api/confidential-pay. */
|
|
7
|
+
endpoint?: string;
|
|
8
|
+
/** Called with the transaction hash on success. */
|
|
9
|
+
onSuccess?: (txHash: string) => void;
|
|
10
|
+
/** Called with an error message on failure. */
|
|
11
|
+
onError?: (message: string) => void;
|
|
12
|
+
/** Disable edits to the amount. */
|
|
13
|
+
lockedAmount?: boolean;
|
|
14
|
+
/** Supply a sender wallet address if you already have one. Defaults to auto-create. */
|
|
15
|
+
sender?: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function ConfidentialUSDC({ recipient, amount: initialAmount, endpoint, onSuccess, onError, lockedAmount, sender, }: ConfidentialUSDCProps): import("react").JSX.Element;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
export function ConfidentialUSDC({ recipient, amount: initialAmount = "1", endpoint = "/api/confidential-pay", onSuccess, onError, lockedAmount = false, sender, }) {
|
|
5
|
+
const [amount, setAmount] = useState(initialAmount);
|
|
6
|
+
const [recipientInput, setRecipientInput] = useState(recipient ?? "");
|
|
7
|
+
const [status, setStatus] = useState("idle");
|
|
8
|
+
const [message, setMessage] = useState("");
|
|
9
|
+
const [effectiveSender, setEffectiveSender] = useState(sender);
|
|
10
|
+
const to = recipient || recipientInput;
|
|
11
|
+
async function handlePay() {
|
|
12
|
+
setStatus("sending");
|
|
13
|
+
setMessage("");
|
|
14
|
+
try {
|
|
15
|
+
const res = await fetch(endpoint, {
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: { "Content-Type": "application/json" },
|
|
18
|
+
body: JSON.stringify({ recipient: to, amount, sender: effectiveSender }),
|
|
19
|
+
});
|
|
20
|
+
const body = await res.json();
|
|
21
|
+
if (!res.ok)
|
|
22
|
+
throw new Error(body.error ?? `HTTP ${res.status}`);
|
|
23
|
+
setStatus("success");
|
|
24
|
+
setMessage(body.transactionHash);
|
|
25
|
+
onSuccess?.(body.transactionHash);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
setStatus("error");
|
|
29
|
+
const m = e instanceof Error ? e.message : "Unknown error";
|
|
30
|
+
setMessage(m);
|
|
31
|
+
onError?.(m);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return (_jsxs("div", { className: "cpw", children: [_jsx("div", { className: "cpw-label", children: "Confidential USDC \u00B7 Base" }), _jsx("label", { className: "cpw-field-label", htmlFor: "cpw-recipient", children: "Recipient" }), recipient ? (_jsxs("div", { className: "cpw-recipient", children: ["To: ", _jsx("span", { className: "cpw-recipient-addr", children: recipient })] })) : (_jsx("input", { id: "cpw-recipient", type: "text", inputMode: "text", placeholder: "0x\u2026", value: recipientInput, disabled: status === "sending", onChange: (e) => setRecipientInput(e.target.value), className: "cpw-input", "aria-label": "Recipient wallet address" })), _jsx("label", { className: "cpw-field-label", htmlFor: "cpw-amount", children: "Amount (USDC)" }), _jsx("input", { id: "cpw-amount", type: "number", inputMode: "decimal", min: "0.01", step: "0.01", value: amount, disabled: lockedAmount || status === "sending", onChange: (e) => setAmount(e.target.value), className: "cpw-input", "aria-label": "Amount in USDC" }), _jsx("button", { onClick: handlePay, disabled: status === "sending", className: `cpw-button${status === "sending" ? " is-sending" : ""}`, children: status === "sending" ? "Sending…" : `Pay ${amount} USDC` }), status === "success" ? (_jsxs("div", { className: "cpw-status is-success", role: "status", children: ["Sent. Tx: ", _jsx("span", { className: "cpw-hash", children: message })] })) : null, status === "error" ? (_jsx("div", { className: "cpw-status is-error", role: "alert", children: message })) : null] }));
|
|
35
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a Next.js POST route handler that proxies the ConfidentialUSDC
|
|
4
|
+
* widget's request to CDP. Mount it as:
|
|
5
|
+
*
|
|
6
|
+
* // app/api/confidential-pay/route.ts
|
|
7
|
+
* import { createConfidentialPayHandler } from "@bvdaniel/confidential-pay/server";
|
|
8
|
+
* export const POST = createConfidentialPayHandler();
|
|
9
|
+
* export const runtime = "nodejs";
|
|
10
|
+
*
|
|
11
|
+
* The CDP credentials are read from server env vars:
|
|
12
|
+
* CDP_API_KEY_ID, CDP_API_KEY_SECRET, CDP_WALLET_SECRET, CDP_NETWORK
|
|
13
|
+
*
|
|
14
|
+
* When `devFaucet` is true and CDP_NETWORK is a testnet, auto-created sender
|
|
15
|
+
* wallets are funded with testnet ETH and USDC before the first send. Never
|
|
16
|
+
* enable this in production — it exists for sandbox demos.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createConfidentialPayHandler(options?: {
|
|
19
|
+
devFaucet?: boolean;
|
|
20
|
+
}): (req: NextRequest) => Promise<NextResponse>;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ConfidentialPay } from "@bvdaniel/confidential-pay-core";
|
|
2
|
+
function applyTrustedTimeout(ms) {
|
|
3
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Next.js POST route handler that proxies the ConfidentialUSDC
|
|
7
|
+
* widget's request to CDP. Mount it as:
|
|
8
|
+
*
|
|
9
|
+
* // app/api/confidential-pay/route.ts
|
|
10
|
+
* import { createConfidentialPayHandler } from "@bvdaniel/confidential-pay/server";
|
|
11
|
+
* export const POST = createConfidentialPayHandler();
|
|
12
|
+
* export const runtime = "nodejs";
|
|
13
|
+
*
|
|
14
|
+
* The CDP credentials are read from server env vars:
|
|
15
|
+
* CDP_API_KEY_ID, CDP_API_KEY_SECRET, CDP_WALLET_SECRET, CDP_NETWORK
|
|
16
|
+
*
|
|
17
|
+
* When `devFaucet` is true and CDP_NETWORK is a testnet, auto-created sender
|
|
18
|
+
* wallets are funded with testnet ETH and USDC before the first send. Never
|
|
19
|
+
* enable this in production — it exists for sandbox demos.
|
|
20
|
+
*/
|
|
21
|
+
export function createConfidentialPayHandler(options) {
|
|
22
|
+
return async function handler(req) {
|
|
23
|
+
const { NextResponse } = await import("next/server");
|
|
24
|
+
if (req.method !== "POST") {
|
|
25
|
+
return NextResponse.json({ error: "method not allowed" }, { status: 405 });
|
|
26
|
+
}
|
|
27
|
+
let payload;
|
|
28
|
+
try {
|
|
29
|
+
payload = await req.json();
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return NextResponse.json({ error: "invalid body" }, { status: 400 });
|
|
33
|
+
}
|
|
34
|
+
const to = payload.recipient;
|
|
35
|
+
const amount = payload.amount;
|
|
36
|
+
if (typeof to !== "string" || !to.trim()) {
|
|
37
|
+
return NextResponse.json({ error: "recipient is required" }, { status: 400 });
|
|
38
|
+
}
|
|
39
|
+
if (typeof amount !== "string" || !/^\d+(\.\d{1,6})?$/.test(amount) || Number(amount) <= 0) {
|
|
40
|
+
return NextResponse.json({ error: "amount must be a positive USDC amount" }, { status: 400 });
|
|
41
|
+
}
|
|
42
|
+
const pay = new ConfidentialPay();
|
|
43
|
+
const isTestnet = pay.networkName !== "base-mainnet";
|
|
44
|
+
let sender = typeof payload.sender === "string" && payload.sender.length > 0
|
|
45
|
+
? payload.sender
|
|
46
|
+
: undefined;
|
|
47
|
+
try {
|
|
48
|
+
if (!sender) {
|
|
49
|
+
const wallet = await pay.getOrCreateWallet("widget-auto");
|
|
50
|
+
sender = wallet.address;
|
|
51
|
+
if (options?.devFaucet && isTestnet) {
|
|
52
|
+
const usdcBal = Number(await pay.getUsdcBalance(sender));
|
|
53
|
+
const ethBal = Number(await pay.getNativeBalance(sender));
|
|
54
|
+
if (usdcBal < 1) {
|
|
55
|
+
try {
|
|
56
|
+
const hash = await pay.faucetUsdc(sender);
|
|
57
|
+
await pay.waitForReceipt(hash);
|
|
58
|
+
applyTrustedTimeout(8000);
|
|
59
|
+
for (let i = 0; i < 12; i++) {
|
|
60
|
+
const b = Number(await pay.getUsdcBalance(sender));
|
|
61
|
+
if (b >= 1)
|
|
62
|
+
break;
|
|
63
|
+
applyTrustedTimeout(5000);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// faucet may be rate-limited; fall through if already funded
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (ethBal < 0.001) {
|
|
71
|
+
try {
|
|
72
|
+
const ethHash = await pay.faucetEth(sender);
|
|
73
|
+
await pay.waitForReceipt(ethHash);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// ETH faucet may be rate-limited; fall through if already funded
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const funded = Number(await pay.getUsdcBalance(sender)) >= 1;
|
|
80
|
+
if (!funded) {
|
|
81
|
+
return NextResponse.json({ error: "faucet unavailable — fund the sender wallet or retry" }, { status: 502 });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const result = await pay.sendUsdcPayment({
|
|
86
|
+
from: sender,
|
|
87
|
+
to: to,
|
|
88
|
+
amount,
|
|
89
|
+
});
|
|
90
|
+
return NextResponse.json({
|
|
91
|
+
ok: true,
|
|
92
|
+
transactionHash: result.transactionHash,
|
|
93
|
+
network: result.network,
|
|
94
|
+
sender,
|
|
95
|
+
devFaucet: true,
|
|
96
|
+
}, { status: 201 });
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
const message = e instanceof Error ? e.message : "unknown error";
|
|
100
|
+
return NextResponse.json({ error: message }, { status: 502 });
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bvdaniel/confidential-pay",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Drop-in confidential USDC payments widget for Base. Built on CDP Non-custodial Wallets.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"confidential",
|
|
7
|
+
"payments",
|
|
8
|
+
"usdc",
|
|
9
|
+
"base",
|
|
10
|
+
"cdp",
|
|
11
|
+
"react",
|
|
12
|
+
"widget"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "a0x-co <hi@a0x.co>",
|
|
16
|
+
"homepage": "https://a0x-confidential-pay-demo-2w42vltmu-bvdaniels-projects.vercel.app",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/a0x-co/a0x-confidential-pay.git",
|
|
20
|
+
"directory": "packages/react"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/a0x-co/a0x-confidential-pay/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./server": {
|
|
35
|
+
"types": "./dist/server.d.ts",
|
|
36
|
+
"import": "./dist/server.js"
|
|
37
|
+
},
|
|
38
|
+
"./ConfidentialUSDC.css": "./dist/ConfidentialUSDC.css"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist"
|
|
42
|
+
],
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": "^18 || ^19"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/react": "^18",
|
|
48
|
+
"next": "14",
|
|
49
|
+
"react": "^18",
|
|
50
|
+
"typescript": "^5"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@bvdaniel/confidential-pay-core": "^0.1.0"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsc -p tsconfig.json && cp src/ConfidentialUSDC.css dist/ConfidentialUSDC.css"
|
|
57
|
+
}
|
|
58
|
+
}
|