Skip to content

Commit

Permalink
hex, npub & note getters
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Aug 21, 2023
1 parent 379662a commit b0493eb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/js/components/searchbox/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class SearchBox extends Component<Props, State> {
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 });
}
});
}
Expand Down
18 changes: 8 additions & 10 deletions src/js/utils/Hex/Hex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});
Expand Down Expand Up @@ -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', () => {
Expand Down
42 changes: 31 additions & 11 deletions src/js/utils/Hex/Hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Hex {
return bech32.encode(prefix, bytes);
}

toHex(): string {
get hex(): string {
return this.value;
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/js/views/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
13 changes: 6 additions & 7 deletions src/js/views/profile/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
Expand Down

0 comments on commit b0493eb

Please sign in to comment.