Compare commits

...
4 Commits
Author SHA1 Message Date
Chris Troutner fda2de5362 Merge pull request #13 from Permissionless-Software-Foundation/dh-kind1-posts
feat(nostr): Created UI for Kind 1 Nostr posts
2025-05-27 18:25:14 -07:00
Daniel Gonzalez 131720ed58 feat(nostr): Created UI for Kind 1 Nostr posts 2025-05-27 19:25:38 -04:00
Chris Troutner 64b1aec4cf Merge pull request #12 from Permissionless-Software-Foundation/dh-nostr-keys
fix(nostr): Display Nostr keys as npub and nsec
2025-05-27 08:25:18 -07:00
Daniel Gonzalez 70ce941eba fix(nostr): Display Nostr keys as npub and nsec 2025-05-26 18:47:31 -04:00
9 changed files with 315 additions and 24 deletions
@@ -165,28 +165,28 @@ function WalletSummary (props) {
</Row>
<Row style={{ padding: '25px', backgroundColor: '#eee' }}>
<Col xs={10} sm={10} lg={8} style={{ padding: '10px' }}>
<b>Nostr Priv Key:</b> <span className={blurredNostrPrivKey ? 'blurred' : null}>{nostrKeyPair.privHex}</span>
<b>Nostr Priv Key:</b> <span className={blurredNostrPrivKey ? 'blurred' : null}>{nostrKeyPair.nsec}</span>
</Col>
<Col xs={6} sm={1} lg={2} style={{ textAlign: 'center' }}>
<FontAwesomeIcon
style={{ cursor: 'pointer' }}
icon={eyeIcon.privateKey}
size='lg'
onClick={() => toggleNostrPrivateKeyBlur(nostrKeyPair.privHex)}
onClick={() => toggleNostrPrivateKeyBlur(nostrKeyPair.nsec)}
/>
</Col>
<Col xs={6} sm={1} lg={2} style={{ textAlign: 'center' }}>
<CopyOnClick appData={appData} value={nostrKeyPair.privHex} />
<CopyOnClick appData={appData} value={nostrKeyPair.nsec} />
</Col>
</Row>
<Row style={{ padding: '25px', backgroundColor: '#eee' }}>
<Col xs={10} sm={10} lg={8} style={{ padding: '10px' }}>
<b>Nostr Pub Key:</b> {nostrKeyPair.pubHex}
<b>Nostr Pub Key:</b> {nostrKeyPair.npub}
</Col>
<Col xs={6} sm={1} lg={2} style={{ textAlign: 'center' }} />
<Col xs={6} sm={1} lg={2} style={{ textAlign: 'center' }}>
<CopyOnClick appData={appData} value={nostrKeyPair.pubHex} />
<CopyOnClick appData={appData} value={nostrKeyPair.npub} />
</Col>
</Row>
</Container>
+2 -2
View File
@@ -23,8 +23,8 @@ import SlpTokens from './slp-tokens'
import SweepWif from './sweep/index.js'
import SignMessage from './sign/index.js'
import ServerSelectView from './configuration/select-server-view'
import NostrPost from './nostr/nostr-post.js'
import NostrRead from './nostr/nostr-read.js'
import NostrPost from './nostr/nostr-post/index.js'
import NostrRead from './nostr/nostr-read/index.js'
function AppBody (props) {
// Dependency injection through props
@@ -0,0 +1,31 @@
/*
Component for posting nostr information.
*/
// Global npm libraries
import React from 'react'
import { Container } from 'react-bootstrap'
import ProfilePost from './profile-post'
import PublicPost from './public-post'
function NostrPost (props) {
return (
<>
<Container>
<h2 style={{
textAlign: 'center',
margin: '20px 0',
padding: '10px',
borderBottom: '2px solid #ccc'
}}
>
Nostr Post
</h2>
<ProfilePost {...props} />
<PublicPost {...props} />
</Container>
</>
)
}
export default NostrPost
@@ -1,5 +1,5 @@
/*
Component for posting nostr information.
Component for posting nostr information on kind 0.
*/
// Global npm libraries
@@ -10,8 +10,8 @@ import { finalizeEvent } from 'nostr-tools/pure'
import { Relay } from 'nostr-tools/relay'
import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency
function NostrPost (props) {
const [accordionKey, setAccordionKey] = useState('1')
function ProfilePost (props) {
const [accordionKey, setAccordionKey] = useState(null)
const [onFetch, setOnFetch] = useState(false)
const { bchWalletState } = props.appData
const [formData, setFormData] = useState({
@@ -96,10 +96,10 @@ function NostrPost (props) {
return (
<>
<Container>
<Container className='mt-4'>
<Accordion activeKey={accordionKey} onSelect={handleAccordionChange}>
<Accordion.Item eventKey='1'>
<Accordion.Header>Post</Accordion.Header>
<Accordion.Item eventKey='0'>
<Accordion.Header>Profile Post</Accordion.Header>
<Accordion.Body>
{errorMsg && (
<div className='alert alert-danger' role='alert'>
@@ -155,4 +155,4 @@ function NostrPost (props) {
)
}
export default NostrPost
export default ProfilePost
@@ -0,0 +1,142 @@
/*
Component for posting nostr information on kind 1.
*/
// Global npm libraries
import React, { useState } from 'react'
import { Container, Form, Button, Spinner } from 'react-bootstrap'
import Accordion from 'react-bootstrap/Accordion'
import { finalizeEvent } from 'nostr-tools/pure'
import { Relay } from 'nostr-tools/relay'
import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency
function PublicPost (props) {
const [accordionKey, setAccordionKey] = useState(null)
const [onFetch, setOnFetch] = useState(false)
const { bchWalletState } = props.appData
const [formData, setFormData] = useState({
content: ''
})
const [errorMsg, setErrorMsg] = useState('')
const [successMsg, setSuccessMsg] = useState('')
const handleInputChange = (e) => {
const { name, value } = e.target
setFormData({
...formData,
[name]: value
})
setSuccessMsg('')
setErrorMsg('')
}
// Post on nostr network
const handleSubmit = async (e) => {
e.preventDefault()
try {
setErrorMsg('')
setSuccessMsg('')
setOnFetch(true)
const { nostrKeyPair } = bchWalletState
// Convert private key to binary
const privateKeyBin = hexToBytes(nostrKeyPair.privHex)
// Relay list
const psf = 'wss://nostr-relay.psfoundation.info'
// Generate a post.
const eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: formData.content
}
console.log(`eventTemplate: ${JSON.stringify(eventTemplate, null, 2)}`)
// Sign the post
const signedEvent = finalizeEvent(eventTemplate, privateKeyBin)
console.log('signedEvent: ', signedEvent)
// Connect to a relay.
const relay = await Relay.connect(psf)
console.log(`connected to ${relay.url}`)
// Publish the message to the relay.
const result = await relay.publish(signedEvent)
console.log('result: ', result)
// Close the connection to the relay.
relay.close()
resetForm()
setSuccessMsg('Post successfully published!')
setOnFetch(false)
} catch (error) {
console.warn(error)
setErrorMsg(error.message || 'An error occurred while posting')
setOnFetch(false)
}
}
const handleAccordionChange = (key) => {
setAccordionKey(key)
}
const resetForm = () => {
setFormData({
content: ''
})
}
return (
<>
<Container className='mt-4'>
<Accordion activeKey={accordionKey} onSelect={handleAccordionChange}>
<Accordion.Item eventKey='0'>
<Accordion.Header>Public Post</Accordion.Header>
<Accordion.Body>
{errorMsg && (
<div className='alert alert-danger' role='alert'>
{errorMsg}
</div>
)}
{successMsg && (
<div className='alert alert-success' role='alert'>
{successMsg}
</div>
)}
<Form onSubmit={handleSubmit}>
<Form.Group className='mb-3'>
<Form.Label>Message</Form.Label>
<Form.Control
as='textarea'
rows={7}
placeholder="What's happening?"
name='content'
value={formData.content}
onChange={handleInputChange}
required
/>
</Form.Group>
<div className='d-flex justify-content-center'>
{!onFetch && (
<Button variant='primary' type='submit' disabled={onFetch}>
Post
</Button>
)}
{onFetch && <Spinner animation='border' variant='primary' />}
</div>
</Form>
</Accordion.Body>
</Accordion.Item>
</Accordion>
</Container>
</>
)
}
export default PublicPost
@@ -0,0 +1,31 @@
/*
Component for read nostr information.
*/
// Global npm libraries
import React from 'react'
import { Container } from 'react-bootstrap'
import ProfileRead from './profile-read.js'
import PublicRead from './public-read.js'
function NostrRead (props) {
return (
<>
<Container>
<h2 style={{
textAlign: 'center',
margin: '20px 0',
padding: '10px',
borderBottom: '2px solid #ccc'
}}
>
Nostr Read
</h2>
<ProfileRead {...props} />
<PublicRead {...props} />
</Container>
</>
)
}
export default NostrRead
@@ -1,3 +1,6 @@
/**
* Component for read nostr information kind 0
*/
// Global npm libraries
import React, { useEffect, useState } from 'react'
import { Container } from 'react-bootstrap'
@@ -6,10 +9,10 @@ import Accordion from 'react-bootstrap/Accordion'
import { RelayPool } from 'nostr'
function NostrRead (props) {
function ProfileRead (props) {
const { bchWalletState } = props.appData
const [posts, setPosts] = useState([])
const [accordionKey, setAccordionKey] = useState('0')
const [accordionKey, setAccordionKey] = useState(null)
const [loaded, setLoaded] = useState(false)
useEffect(() => {
@@ -47,16 +50,19 @@ function NostrRead (props) {
}
return (
<Container>
<Container className='mt-4'>
<Accordion activeKey={accordionKey} onSelect={handleAccordionChange}>
<Accordion.Item eventKey='0'>
<Accordion.Header>Read ( {posts.length} post found )</Accordion.Header>
<Accordion.Body>
<Accordion.Header>Profile </Accordion.Header>
<Accordion.Body style={{ wordBreak: 'break-word' }}>
{posts.map((post, index) => (
<div key={index}>
<span>{post.content}</span>
</div>
))}
{!posts.length && loaded && (
<span>Posts not found</span>
)}
</Accordion.Body>
</Accordion.Item>
</Accordion>
@@ -64,4 +70,4 @@ function NostrRead (props) {
)
}
export default NostrRead
export default ProfileRead
@@ -0,0 +1,74 @@
/**
* Component for read nostr information kind 1
*/
// Global npm libraries
import React, { useEffect, useState } from 'react'
import { Container } from 'react-bootstrap'
import Accordion from 'react-bootstrap/Accordion'
import { RelayPool } from 'nostr'
function PublicRead (props) {
const { bchWalletState } = props.appData
const [posts, setPosts] = useState([])
const [accordionKey, setAccordionKey] = useState(null)
const [loaded, setLoaded] = useState(false)
useEffect(() => {
// Get Last post from a author
const start = () => {
const { nostrKeyPair } = bchWalletState
const psf = 'wss://nostr-relay.psfoundation.info'
const pool = RelayPool([psf])
pool.on('open', relay => {
relay.subscribe('subid', { limit: 5, kinds: [1], authors: [nostrKeyPair.pubHex] })
})
pool.on('eose', relay => {
console.log('Closing Relay')
relay.close()
})
pool.on('event', (relay, subId, ev) => {
console.log('Received event:', ev)
setPosts(currentPosts => [...currentPosts, ev])
})
setLoaded(true)
}
if (!loaded) {
start()
}
}, [bchWalletState, loaded])
const handleAccordionChange = (key) => {
setAccordionKey(key)
}
return (
<Container className='mt-4'>
<Accordion activeKey={accordionKey} onSelect={handleAccordionChange}>
<Accordion.Item eventKey='0'>
<Accordion.Header>Public Post ( {posts.length} post found )</Accordion.Header>
<Accordion.Body style={{ wordBreak: 'break-word' }}>
{posts.map((post, index) => (
<div key={index}>
<span>{post.content}</span>
<hr />
</div>
))}
{!posts.length && loaded && (
<span>Not Post Found!</span>
)}
</Accordion.Body>
</Accordion.Item>
</Accordion>
</Container>
)
}
export default PublicRead
+11 -4
View File
@@ -13,6 +13,7 @@ import P2WDB from 'p2wdb'
import { base58_to_binary as base58ToBinary } from 'base58-js'
import { bytesToHex } from '@noble/hashes/utils' // already an installed dependency
import { getPublicKey } from 'nostr-tools/pure'
import * as nip19 from 'nostr-tools/nip19'
class AsyncLoad {
constructor () {
@@ -65,7 +66,7 @@ class AsyncLoad {
// Get Nostr key pair from WIF
const nostrKeyPair = this.nostrKeyPairFromWIF(wallet.walletInfo.privateKey)
console.log('nostrKeyPair: ', nostrKeyPair)
const walletInfo = wallet.walletInfo
walletInfo.nostrKeyPair = nostrKeyPair
// Update the state of the wallet.
@@ -265,15 +266,21 @@ class AsyncLoad {
const wifBuf = base58ToBinary(WIF)
const privBuf = wifBuf.slice(1, 33)
// console.log('privBuf: ', privBuf)
// Convert the private key to a hex string
const privHex = bytesToHex(privBuf)
console.log('BCH & Nostr private key (HEX format): ', privHex)
// Convert the private key to a Nostr NSEC key
const nsec = nip19.nsecEncode(privBuf)
// Get the public key hex string from the private key
const pubHex = getPublicKey(privBuf)
// Convert the public key to a Nostr NPUB key
const npub = nip19.npubEncode(pubHex)
return {
privHex,
pubHex
pubHex,
nsec,
npub
}
}
}