Use only nos2x.
This removes all code that depends on our own implementation of key signing. Thus you must now use a browser extension to use any functionality.
This commit is contained in:
parent
c748802c62
commit
20907bede6
7 changed files with 24 additions and 1470 deletions
17
TODO
17
TODO
|
@ -1,17 +0,0 @@
|
|||
Here lie goals:
|
||||
[ ] Autocomplete for usernames in composition
|
||||
[ ] Display followers
|
||||
[ ] Display following list
|
||||
[ ] Export/import contacts list
|
||||
[ ] Redesign embeds
|
||||
[ ] Render lighting invoices (BOLT11) Do this without a library
|
||||
[ ] Replace local storage usage
|
||||
[ ] Show "Show More" button at end of timeline to display next 200
|
||||
[ ] Show likes on profile page under a tab
|
||||
[ ] Store profiles of friends, friends of friends, & the user
|
||||
[x] Direct messaging
|
||||
[x] Fix UI/X issues on mobile (modal, reply, etc.)
|
||||
[x] Mark all mail as read button.
|
||||
[x] Render tagged users e.g. #[0]
|
||||
[x] Toggle replys only in home view
|
||||
[x] Update profile fields: name, banner, website, lightning address
|
|
@ -14,8 +14,8 @@
|
|||
<link rel="stylesheet" href="/css/utils.css?v=1">
|
||||
<link rel="stylesheet" href="/css/styles.css?v=13">
|
||||
<link rel="stylesheet" href="/css/responsive.css?v=10">
|
||||
<script defer src="/js/ui/safe-html.js?v=1"></script>
|
||||
<script defer src="/js/util.js?v=5"></script>
|
||||
<script defer src="/js/ui/safe-html.js?v=1"></script>
|
||||
<script defer src="/js/ui/util.js?v=8"></script>
|
||||
<script defer src="/js/ui/render.js?v=15"></script>
|
||||
<script defer src="/js/ui/state.js?v=1"></script>
|
||||
|
@ -23,8 +23,6 @@
|
|||
<script defer src="/js/ui/profile.js?v=1"></script>
|
||||
<script defer src="/js/ui/settings.js?v=1"></script>
|
||||
<script defer src="/js/ui/dm.js?v=1"></script>
|
||||
<script defer src="/js/noble-secp256k1.js?v=1"></script>
|
||||
<script defer src="/js/bech32.js?v=1"></script>
|
||||
<script defer src="/js/nostr.js?v=7"></script>
|
||||
<script defer src="/js/core.js?v=1"></script>
|
||||
<script defer src="/js/model.js?v=1"></script>
|
||||
|
|
169
js/bech32.js
169
js/bech32.js
|
@ -1,169 +0,0 @@
|
|||
var ALPHABET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
||||
var ALPHABET_MAP = {};
|
||||
for (var z = 0; z < ALPHABET.length; z++) {
|
||||
var x = ALPHABET.charAt(z);
|
||||
ALPHABET_MAP[x] = z;
|
||||
}
|
||||
function polymodStep(pre) {
|
||||
var b = pre >> 25;
|
||||
return (((pre & 0x1ffffff) << 5) ^
|
||||
(-((b >> 0) & 1) & 0x3b6a57b2) ^
|
||||
(-((b >> 1) & 1) & 0x26508e6d) ^
|
||||
(-((b >> 2) & 1) & 0x1ea119fa) ^
|
||||
(-((b >> 3) & 1) & 0x3d4233dd) ^
|
||||
(-((b >> 4) & 1) & 0x2a1462b3));
|
||||
}
|
||||
function prefixChk(prefix) {
|
||||
var chk = 1;
|
||||
for (var i = 0; i < prefix.length; ++i) {
|
||||
var c = prefix.charCodeAt(i);
|
||||
if (c < 33 || c > 126)
|
||||
return 'Invalid prefix (' + prefix + ')';
|
||||
chk = polymodStep(chk) ^ (c >> 5);
|
||||
}
|
||||
chk = polymodStep(chk);
|
||||
for (var i = 0; i < prefix.length; ++i) {
|
||||
var v = prefix.charCodeAt(i);
|
||||
chk = polymodStep(chk) ^ (v & 0x1f);
|
||||
}
|
||||
return chk;
|
||||
}
|
||||
function convertbits(data, inBits, outBits, pad) {
|
||||
var value = 0;
|
||||
var bits = 0;
|
||||
var maxV = (1 << outBits) - 1;
|
||||
var result = [];
|
||||
for (var i = 0; i < data.length; ++i) {
|
||||
value = (value << inBits) | data[i];
|
||||
bits += inBits;
|
||||
while (bits >= outBits) {
|
||||
bits -= outBits;
|
||||
result.push((value >> bits) & maxV);
|
||||
}
|
||||
}
|
||||
if (pad) {
|
||||
if (bits > 0) {
|
||||
result.push((value << (outBits - bits)) & maxV);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (bits >= inBits)
|
||||
return 'Excess padding';
|
||||
if ((value << (outBits - bits)) & maxV)
|
||||
return 'Non-zero padding';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function toWords(bytes) {
|
||||
return convertbits(bytes, 8, 5, true);
|
||||
}
|
||||
function fromWordsUnsafe(words) {
|
||||
var res = convertbits(words, 5, 8, false);
|
||||
if (Array.isArray(res))
|
||||
return res;
|
||||
}
|
||||
function fromWords(words) {
|
||||
var res = convertbits(words, 5, 8, false);
|
||||
if (Array.isArray(res))
|
||||
return res;
|
||||
throw new Error(res);
|
||||
}
|
||||
function getLibraryFromEncoding(encoding) {
|
||||
var ENCODING_CONST;
|
||||
if (encoding === 'bech32') {
|
||||
ENCODING_CONST = 1;
|
||||
}
|
||||
else {
|
||||
ENCODING_CONST = 0x2bc830a3;
|
||||
}
|
||||
function encode(prefix, words, LIMIT) {
|
||||
LIMIT = LIMIT || 90;
|
||||
if (prefix.length + 7 + words.length > LIMIT)
|
||||
throw new TypeError('Exceeds length limit');
|
||||
prefix = prefix.toLowerCase();
|
||||
// determine chk mod
|
||||
var chk = prefixChk(prefix);
|
||||
if (typeof chk === 'string')
|
||||
throw new Error(chk);
|
||||
var result = prefix + '1';
|
||||
for (var i = 0; i < words.length; ++i) {
|
||||
var x = words[i];
|
||||
if (x >> 5 !== 0)
|
||||
throw new Error('Non 5-bit word');
|
||||
chk = polymodStep(chk) ^ x;
|
||||
result += ALPHABET.charAt(x);
|
||||
}
|
||||
for (var i = 0; i < 6; ++i) {
|
||||
chk = polymodStep(chk);
|
||||
}
|
||||
chk ^= ENCODING_CONST;
|
||||
for (var i = 0; i < 6; ++i) {
|
||||
var v = (chk >> ((5 - i) * 5)) & 0x1f;
|
||||
result += ALPHABET.charAt(v);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function __decode(str, LIMIT) {
|
||||
LIMIT = LIMIT || 90;
|
||||
if (str.length < 8)
|
||||
return str + ' too short';
|
||||
if (str.length > LIMIT)
|
||||
return 'Exceeds length limit';
|
||||
// don't allow mixed case
|
||||
var lowered = str.toLowerCase();
|
||||
var uppered = str.toUpperCase();
|
||||
if (str !== lowered && str !== uppered)
|
||||
return 'Mixed-case string ' + str;
|
||||
str = lowered;
|
||||
var split = str.lastIndexOf('1');
|
||||
if (split === -1)
|
||||
return 'No separator character for ' + str;
|
||||
if (split === 0)
|
||||
return 'Missing prefix for ' + str;
|
||||
var prefix = str.slice(0, split);
|
||||
var wordChars = str.slice(split + 1);
|
||||
if (wordChars.length < 6)
|
||||
return 'Data too short';
|
||||
var chk = prefixChk(prefix);
|
||||
if (typeof chk === 'string')
|
||||
return chk;
|
||||
var words = [];
|
||||
for (var i = 0; i < wordChars.length; ++i) {
|
||||
var c = wordChars.charAt(i);
|
||||
var v = ALPHABET_MAP[c];
|
||||
if (v === undefined)
|
||||
return 'Unknown character ' + c;
|
||||
chk = polymodStep(chk) ^ v;
|
||||
// not in the checksum?
|
||||
if (i + 6 >= wordChars.length)
|
||||
continue;
|
||||
words.push(v);
|
||||
}
|
||||
if (chk !== ENCODING_CONST)
|
||||
return 'Invalid checksum for ' + str;
|
||||
return { prefix: prefix, words: words };
|
||||
}
|
||||
function decodeUnsafe(str, LIMIT) {
|
||||
var res = __decode(str, LIMIT);
|
||||
if (typeof res === 'object')
|
||||
return res;
|
||||
}
|
||||
function decode(str, LIMIT) {
|
||||
var res = __decode(str, LIMIT);
|
||||
if (typeof res === 'object')
|
||||
return res;
|
||||
throw new Error(res);
|
||||
}
|
||||
return {
|
||||
decodeUnsafe: decodeUnsafe,
|
||||
decode: decode,
|
||||
encode: encode,
|
||||
toWords: toWords,
|
||||
fromWordsUnsafe: fromWordsUnsafe,
|
||||
fromWords: fromWords
|
||||
};
|
||||
}
|
||||
|
||||
const bech32 = getLibraryFromEncoding('bech32');
|
||||
const bech32m = getLibraryFromEncoding('bech32m');
|
||||
|
49
js/core.js
49
js/core.js
|
@ -28,23 +28,6 @@ const PUBLIC_KINDS = [
|
|||
KIND_SHARE,
|
||||
];
|
||||
|
||||
function get_local_state(key) {
|
||||
if (DAMUS[key] != null)
|
||||
return DAMUS[key]
|
||||
return localStorage.getItem(key)
|
||||
}
|
||||
|
||||
function set_local_state(key, val) {
|
||||
DAMUS[key] = val
|
||||
localStorage.setItem(key, val)
|
||||
}
|
||||
|
||||
async function sign_id(privkey, id) {
|
||||
//const digest = nostrjs.hex_decode(id)
|
||||
const sig = await nobleSecp256k1.schnorr.sign(id, privkey)
|
||||
return nostrjs.hex_encode(sig)
|
||||
}
|
||||
|
||||
async function broadcast_related_events(ev) {
|
||||
ev.tags.reduce((evs, tag) => {
|
||||
// cap it at something sane
|
||||
|
@ -120,18 +103,16 @@ async function update_contacts() {
|
|||
}
|
||||
|
||||
async function sign_event(ev) {
|
||||
if (window.nostr && window.nostr.signEvent) {
|
||||
const signed = await window.nostr.signEvent(ev)
|
||||
if (typeof signed === 'string') {
|
||||
ev.sig = signed
|
||||
return ev
|
||||
}
|
||||
return signed
|
||||
if (!(window.nostr && window.nostr.signEvent)) {
|
||||
console.error("window.nostr.signEvent is unsupported");
|
||||
return;
|
||||
}
|
||||
|
||||
const privkey = get_privkey()
|
||||
ev.sig = await sign_id(privkey, ev.id)
|
||||
return ev
|
||||
const signed = await window.nostr.signEvent(ev)
|
||||
if (typeof signed === 'string') {
|
||||
ev.sig = signed
|
||||
return ev
|
||||
}
|
||||
return signed
|
||||
}
|
||||
|
||||
function new_reply_tags(ev) {
|
||||
|
@ -301,7 +282,6 @@ function get_content_warning(tags) {
|
|||
async function get_nip05_pubkey(email) {
|
||||
const [user, host] = email.split("@")
|
||||
const url = `https://${host}/.well-known/nostr.json?name=${user}`
|
||||
|
||||
try {
|
||||
const res = await fetch(url)
|
||||
const json = await res.json()
|
||||
|
@ -312,14 +292,3 @@ async function get_nip05_pubkey(email) {
|
|||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
// TODO rename handle_pubkey to fetch_pubkey
|
||||
async function handle_pubkey(pubkey) {
|
||||
if (pubkey[0] === "n")
|
||||
pubkey = bech32_decode(pubkey)
|
||||
if (pubkey.includes("@"))
|
||||
pubkey = await get_nip05_pubkey(pubkey)
|
||||
set_local_state('pubkey', pubkey)
|
||||
return pubkey
|
||||
}
|
||||
|
||||
|
|
12
js/main.js
12
js/main.js
|
@ -23,18 +23,18 @@ addEventListener('DOMContentLoaded', (ev) => {
|
|||
|
||||
async function damus_web_init() {
|
||||
let tries = 0;
|
||||
const max_wait = 500;
|
||||
const interval = 20;
|
||||
async function init() {
|
||||
if (window.nostr || tries >= (max_wait/interval)) {
|
||||
function init() {
|
||||
if (window.nostr) {
|
||||
log_info("init after", tries);
|
||||
await damus_web_init_ready();
|
||||
damus_web_init_ready();
|
||||
return;
|
||||
}
|
||||
// TODO if tries is too many say window.nostr not found.
|
||||
tries++;
|
||||
await init();
|
||||
setTimeout(init, interval);
|
||||
}
|
||||
setTimeout(init, interval);
|
||||
init();
|
||||
}
|
||||
|
||||
async function damus_web_init_ready() {
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -168,40 +168,16 @@ function update_notifications(model) {
|
|||
}
|
||||
|
||||
async function get_pubkey(use_prompt=true) {
|
||||
let pubkey = get_local_state('pubkey')
|
||||
if (pubkey)
|
||||
return pubkey
|
||||
if (window.nostr && window.nostr.getPublicKey) {
|
||||
log_debug("calling window.nostr.getPublicKey()...")
|
||||
try {
|
||||
pubkey = await window.nostr.getPublicKey()
|
||||
return await handle_pubkey(pubkey)
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
log_debug("got %s pubkey from nos2x", pubkey)
|
||||
}
|
||||
if (!use_prompt)
|
||||
if (!(window.nostr && window.nostr.getPublicKey)) {
|
||||
console.error("window.nostr.getPublicKey is unsupported");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
return await window.nostr.getPublicKey()
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
pubkey = prompt("Enter Nostr ID (eg: jb55@jb55.com) or public key (hex or npub).")
|
||||
if (!pubkey.trim())
|
||||
return;
|
||||
return await handle_pubkey(pubkey)
|
||||
}
|
||||
|
||||
function get_privkey() {
|
||||
let privkey = get_local_state('privkey')
|
||||
if (privkey)
|
||||
return privkey
|
||||
if (!privkey)
|
||||
privkey = prompt("Enter private key")
|
||||
if (!privkey)
|
||||
throw new Error("can't get privkey")
|
||||
if (privkey[0] === "n") {
|
||||
privkey = bech32_decode(privkey)
|
||||
}
|
||||
set_local_state('privkey', privkey)
|
||||
return privkey
|
||||
}
|
||||
|
||||
function open_thread(thread_id) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue