diff --git a/src/js/components/searchbox/SearchBox.tsx b/src/js/components/searchbox/SearchBox.tsx index 39667aa02..534641696 100644 --- a/src/js/components/searchbox/SearchBox.tsx +++ b/src/js/components/searchbox/SearchBox.tsx @@ -183,7 +183,7 @@ class SearchBox extends Component { Key.getPubKeyByNip05Address(query).then((pubKey) => { // if query hasn't changed since we started the request if (pubKey && query === String(this.props.query || this.inputRef.current.value)) { - this.props.onSelect?.({ key: pubKey.toHex() }); + this.props.onSelect?.({ key: pubKey.hex }); } }); } diff --git a/src/js/utils/Hex/Hex.test.ts b/src/js/utils/Hex/Hex.test.ts index cd1ca513a..3d4c98253 100644 --- a/src/js/utils/Hex/Hex.test.ts +++ b/src/js/utils/Hex/Hex.test.ts @@ -7,15 +7,15 @@ describe('PublicKey', () => { const bech32 = 'npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk'; const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; const publicKey = new PublicKey(bech32); - expect(publicKey.toHex()).toEqual(hex); - expect(publicKey.toBech32()).toEqual(bech32); + expect(publicKey.hex).toEqual(hex); + expect(publicKey.npub).toEqual(bech32); }); it('should init from hex', () => { const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; const publicKey = new PublicKey(hex); - expect(publicKey.toHex()).toEqual(hex); - expect(publicKey.toBech32()).toEqual( + expect(publicKey.hex).toEqual(hex); + expect(publicKey.npub).toEqual( 'npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk', ); }); @@ -51,17 +51,15 @@ describe('EventID', () => { const noteBech32 = 'note1wdyajan9c9d72wanqe2l34lxgdu3q5esglhquusfkg34fqq6462qh4cjd5'; const noteHex = '7349d97665c15be53bb30655f8d7e6437910533047ee0e7209b22354801aae94'; const eventId = new EventID(noteBech32); - expect(eventId.toHex()).toEqual(noteHex); - expect(eventId.toBech32()).toEqual(noteBech32); + expect(eventId.hex).toEqual(noteHex); + expect(eventId.note).toEqual(noteBech32); }); it('should init from hex', () => { const hex = '7349d97665c15be53bb30655f8d7e6437910533047ee0e7209b22354801aae94'; const eventId = new EventID(hex); - expect(eventId.toHex()).toEqual(hex); - expect(eventId.toBech32()).toEqual( - 'note1wdyajan9c9d72wanqe2l34lxgdu3q5esglhquusfkg34fqq6462qh4cjd5', - ); + expect(eventId.hex).toEqual(hex); + expect(eventId.note).toEqual('note1wdyajan9c9d72wanqe2l34lxgdu3q5esglhquusfkg34fqq6462qh4cjd5'); }); it('should fail with too long hex', () => { diff --git a/src/js/utils/Hex/Hex.ts b/src/js/utils/Hex/Hex.ts index 569594e17..878192b7d 100644 --- a/src/js/utils/Hex/Hex.ts +++ b/src/js/utils/Hex/Hex.ts @@ -43,7 +43,7 @@ export class Hex { return bech32.encode(prefix, bytes); } - toHex(): string { + get hex(): string { return this.value; } @@ -53,15 +53,25 @@ export class Hex { } export class EventID extends Hex { + noteValue: string | undefined; + constructor(str: string) { - if (str.startsWith('note')) { - str = bech32ToHex(str); + const isNote = str.startsWith('note'); + let hexValue = str; + if (isNote) { + hexValue = bech32ToHex(str); + } + super(hexValue, 64); + if (isNote) { + this.noteValue = str; // preserve the original Bech32 value } - super(str, 64); } - toBech32(): string { - return super.toBech32('note'); + get note(): string { + if (!this.noteValue) { + this.noteValue = super.toBech32('note'); + } + return this.noteValue; } equals(other: EventID | string): boolean { @@ -76,15 +86,25 @@ export class EventID extends Hex { } export class PublicKey extends Hex { + npubValue: string | undefined; + constructor(str: string) { - if (str.startsWith('npub')) { - str = bech32ToHex(str); + const isNpub = str.startsWith('npub'); + let hexValue = str; + if (isNpub) { + hexValue = bech32ToHex(str); + } + super(hexValue, 64); + if (isNpub) { + this.npubValue = str; // preserve the original Bech32 value } - super(str, 64); } - toBech32(): string { - return super.toBech32('npub'); + get npub(): string { + if (!this.npubValue) { + this.npubValue = super.toBech32('npub'); + } + return this.npubValue; } equals(other: PublicKey | string): boolean { diff --git a/src/js/views/Note.tsx b/src/js/views/Note.tsx index d8d52a3f8..966f8bd3d 100644 --- a/src/js/views/Note.tsx +++ b/src/js/views/Note.tsx @@ -10,9 +10,9 @@ import { translate as t } from '../translations/Translation.mjs'; const Note = (props) => { useEffect(() => { - const nostrBech32Id = new EventID(props.id).toBech32(); - if (nostrBech32Id && props.id !== nostrBech32Id) { - route(`/${nostrBech32Id}`, true); + const noteId = new EventID(props.id).note; + if (noteId && props.id !== noteId) { + route(`/${noteId}`, true); return; } }, [props.id]); diff --git a/src/js/views/profile/Profile.tsx b/src/js/views/profile/Profile.tsx index 5e439edbc..ce8ae1271 100644 --- a/src/js/views/profile/Profile.tsx +++ b/src/js/views/profile/Profile.tsx @@ -63,15 +63,14 @@ function Profile(props) { useEffect(() => { try { const pub = new PublicKey(props.id); - const npubComputed = pub.toBech32(); - if (npubComputed !== props.id) { - route(`/${npubComputed}`, true); + if (pub.npub !== props.id) { + route(`/${pub.npub}`, true); return; } - setHexPub(pub.toHex()); - setNpub(npubComputed); + setHexPub(pub.hex); + setNpub(pub.npub); } catch (e) { let nostrAddress = props.id; @@ -85,8 +84,8 @@ function Profile(props) { Key.getPubKeyByNip05Address(nostrAddress).then((pubKey) => { if (pubKey) { - setNpub(pubKey.toBech32()); - setHexPub(pubKey.toHex()); + setNpub(pubKey.npub); + setHexPub(pubKey.hex); } else { setNpub(''); // To indicate not found }