mirror of
https://github.com/nostr-connect/connect.git
synced 2026-02-23 21:24:21 +01:00
add poc
This commit is contained in:
3
example/.npmignore
Normal file
3
example/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
.cache
|
||||
dist
|
||||
14
example/index.html
Normal file
14
example/index.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||
<title>Playground</title>
|
||||
</head>
|
||||
|
||||
<body style="background-color:lightgray;"}}>
|
||||
<div id="root"></div>
|
||||
<script src="./index.tsx" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
97
example/index.tsx
Normal file
97
example/index.tsx
Normal file
@@ -0,0 +1,97 @@
|
||||
import 'react-app-polyfill/ie11';
|
||||
import * as React from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import * as ReactDOM from 'react-dom';
|
||||
import { Session } from '../src/index';
|
||||
import { generatePrivateKey, getPublicKey, nip04, relayInit } from 'nostr-tools'
|
||||
|
||||
const App = () => {
|
||||
/* useEffect(() => {
|
||||
(async () => {
|
||||
})();
|
||||
}, []); */
|
||||
|
||||
const [walletKey, setWalletKey] = React.useState<{ pk: string; sk: string }>();
|
||||
const [sessionHolder, setSession] = React.useState<Session>();
|
||||
const [request, setRequest] = React.useState<any>();
|
||||
|
||||
|
||||
const newWallet = () => {
|
||||
//this is the wallet public key
|
||||
let sk = generatePrivateKey()
|
||||
let pk = getPublicKey(sk)
|
||||
setWalletKey({ pk, sk });
|
||||
}
|
||||
|
||||
const newSession = async () => {
|
||||
|
||||
const session = new Session({
|
||||
name: 'Auth',
|
||||
description: 'lorem ipsum dolor sit amet',
|
||||
url: 'https://vulpem.com',
|
||||
icons: ['https://vulpem.com/1000x860-p-500.422be1bc.png'],
|
||||
});
|
||||
|
||||
await session.startPairing(walletKey?.pk);
|
||||
setSession(session);
|
||||
};
|
||||
|
||||
const listen = async () => {
|
||||
if (!sessionHolder) return;
|
||||
|
||||
// let's query for an event to this wallet pub key
|
||||
const relay = relayInit('wss://nostr.vulpem.com');
|
||||
await relay.connect()
|
||||
|
||||
relay.on('connect', () => {
|
||||
console.log(`wallet: connected to ${relay.url}`)
|
||||
})
|
||||
relay.on('error', () => {
|
||||
console.log(`wallet: failed to connect to ${relay.url}`)
|
||||
})
|
||||
|
||||
let sub = relay.sub([{ kinds: [4] }])
|
||||
// on the receiver side
|
||||
sub.on('event', async (event) => {
|
||||
if (!walletKey) return;
|
||||
|
||||
const mention = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
||||
if (mention !== walletKey.pk) return;
|
||||
|
||||
const plaintext = await nip04.decrypt(walletKey?.sk, sessionHolder.pubKey, event.content);
|
||||
console.log('wallet', event.id, event.pubkey, JSON.parse(plaintext));
|
||||
setRequest(JSON.parse(plaintext));
|
||||
})
|
||||
|
||||
sub.on('eose', () => {
|
||||
sub.unsub()
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>💸 Wallet</h1>
|
||||
{walletKey && <p> 🔎 Wallet Pub: {walletKey?.pk} </p>}
|
||||
<button onClick={newWallet}>Create Wallet</button>
|
||||
<button disabled={!sessionHolder} onClick={listen}>Listen</button>
|
||||
{request && <div>
|
||||
<h1>📨 Incoming Request</h1>
|
||||
<img height={80} src={request?.icons[0]} />
|
||||
<p>
|
||||
Name <b>{request?.name}</b>
|
||||
</p>
|
||||
<p>
|
||||
Description <b>{request?.description}</b>
|
||||
</p>
|
||||
<p>
|
||||
URL: <b>{request?.url}</b>
|
||||
</p>
|
||||
</div>}
|
||||
<hr />
|
||||
<h1> App </h1>
|
||||
<button disabled={!walletKey} onClick={newSession}>New Session</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'));
|
||||
24
example/package.json
Normal file
24
example/package.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "example",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"start": "parcel --no-cache index.html",
|
||||
"watch": "parcel watch index.html",
|
||||
"build": "parcel build index.html"
|
||||
},
|
||||
"dependencies": {
|
||||
"react-app-polyfill": "^1.0.0"
|
||||
},
|
||||
"alias": {
|
||||
"react": "../node_modules/react",
|
||||
"react-dom": "../node_modules/react-dom/profiling",
|
||||
"scheduler/tracing": "../node_modules/scheduler/tracing-profiling"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^16.9.11",
|
||||
"@types/react-dom": "^16.8.4",
|
||||
"parcel": "^2.8.2",
|
||||
"typescript": "^3.4.5"
|
||||
}
|
||||
}
|
||||
18
example/tsconfig.json
Normal file
18
example/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowSyntheticDefaultImports": false,
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"jsx": "react",
|
||||
"moduleResolution": "node",
|
||||
"noImplicitAny": false,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"removeComments": true,
|
||||
"strictNullChecks": true,
|
||||
"preserveConstEnums": true,
|
||||
"sourceMap": true,
|
||||
"lib": ["es2015", "es2016", "dom"],
|
||||
"types": ["node"]
|
||||
}
|
||||
}
|
||||
1489
example/yarn.lock
Normal file
1489
example/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user