Skip to content

Commit 25a2ac4

Browse files
committed
small copy button for nsec
1 parent d0911fb commit 25a2ac4

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/routes/settings/+page.svelte

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
let newRelay = 'wss://';
2626
let copiedPublicKey = false;
27+
let copiedPrivateKey = false;
2728
let connectionName = 'Connect another Nostr app';
2829
let connections: Array<PomegranateProfile & { bunker: string }> = [];
2930
let connectionError = '';
@@ -78,6 +79,21 @@
7879
setTimeout(() => (copiedPublicKey = false), 1400);
7980
}
8081
82+
async function copyPrivateKey() {
83+
if ($session?.mode !== 'private-key' || !$session.secret) return;
84+
const nsec = nsecFromHex($session.secret);
85+
if (!nsec) return;
86+
await navigator.clipboard.writeText(nsec);
87+
copiedPrivateKey = true;
88+
setTimeout(() => (copiedPrivateKey = false), 1400);
89+
}
90+
91+
function nsecFromHex(hex: string) {
92+
if (!/^[0-9a-f]{64}$/i.test(hex)) return '';
93+
const bytes = new Uint8Array(hex.match(/.{1,2}/g)?.map((byte) => parseInt(byte, 16)) ?? []);
94+
return nip19.nsecEncode(bytes);
95+
}
96+
8197
async function refreshConnections() {
8298
connectionError = '';
8399
loadingConnections = true;
@@ -182,7 +198,14 @@
182198
<div class="setting-grid">
183199
<div class="account-field">
184200
<span>Signed in with</span>
185-
<strong>{sessionLabel($session.mode)}</strong>
201+
<span class="session-label-row">
202+
<strong>{sessionLabel($session.mode)}</strong>
203+
{#if $session.mode === 'private-key' && $session.secret}
204+
<button class="icon-button small local-key-copy" on:click={copyPrivateKey} aria-label="Copy private key">
205+
{#if copiedPrivateKey}<Check size={15} />{:else}<Copy size={15} />{/if}
206+
</button>
207+
{/if}
208+
</span>
186209
</div>
187210
<div class="account-field">
188211
<span>Public key</span>

src/styles.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,19 @@ label span, label strong { color: var(--text); }
27222722
color: var(--text);
27232723
overflow-wrap: anywhere;
27242724
}
2725+
.session-label-row {
2726+
min-width: 0;
2727+
display: inline-flex;
2728+
align-items: center;
2729+
justify-content: flex-start;
2730+
gap: 7px;
2731+
}
2732+
.local-key-copy {
2733+
color: var(--muted);
2734+
}
2735+
.local-key-copy:hover {
2736+
color: var(--accent);
2737+
}
27252738
.public-key-copy {
27262739
max-width: 100%;
27272740
min-height: 0;

0 commit comments

Comments
 (0)