-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { EventID, PublicKey } from '@/utils/Hex'; | ||
|
||
describe('PublicKey', () => { | ||
it('should convert npub bech32 to hex', () => { | ||
const bech32 = 'npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk'; | ||
const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; | ||
const publicKey = new PublicKey(bech32); | ||
expect(publicKey.toHex()).toEqual(hex); | ||
expect(publicKey.toBech32()).toEqual(bech32); | ||
}); | ||
|
||
it('should init from hex', () => { | ||
const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; | ||
const publicKey = new PublicKey(hex); | ||
expect(publicKey.toHex()).toEqual(hex); | ||
expect(publicKey.toBech32()).toEqual( | ||
'npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk', | ||
); | ||
}); | ||
|
||
it('should fail with too long hex', () => { | ||
const hex = | ||
'4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd04523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; | ||
expect(() => new PublicKey(hex)).toThrow(); | ||
}); | ||
|
||
it('equals(hexStr)', () => { | ||
const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; | ||
const publicKey = new PublicKey(hex); | ||
expect(publicKey.equals(hex)).toEqual(true); | ||
}); | ||
|
||
it('equals(PublicKey)', () => { | ||
const hex = '4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0'; | ||
const publicKey = new PublicKey(hex); | ||
const publicKey2 = new PublicKey(hex); | ||
expect(publicKey.equals(publicKey2)).toEqual(true); | ||
}); | ||
|
||
it('equals(bech32)', () => { | ||
const bech32 = 'npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk'; | ||
const publicKey = new PublicKey(bech32); | ||
expect(publicKey.equals(bech32)).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('EventID', () => { | ||
it('should convert note id bech32 to hex', () => { | ||
const noteBech32 = 'note1wdyajan9c9d72wanqe2l34lxgdu3q5esglhquusfkg34fqq6462qh4cjd5'; | ||
const noteHex = '7349d97665c15be53bb30655f8d7e6437910533047ee0e7209b22354801aae94'; | ||
const eventId = new EventID(noteBech32); | ||
expect(eventId.toHex()).toEqual(noteHex); | ||
expect(eventId.toBech32()).toEqual(noteBech32); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as bech32 from 'bech32-buffer'; | ||
|
||
import Helpers from '@/utils/Helpers.tsx'; | ||
|
||
function bech32ToHex(str: string): string { | ||
try { | ||
const { data } = bech32.decode(str); | ||
const addr = Helpers.arrayToHex(data); | ||
return addr; | ||
} catch (e) { | ||
throw new Error('The provided string is not a valid bech32 address: ' + str); | ||
} | ||
} | ||
|
||
export class Hex { | ||
value: string; | ||
|
||
constructor(str: string, expectedLength?: number) { | ||
this.validateHex(str, expectedLength); | ||
this.value = str; | ||
} | ||
|
||
private validateHex(str: string, expectedLength?: number): void { | ||
if (!/^[0-9a-fA-F]+$/.test(str)) { | ||
throw new Error(`The provided string is not a valid hex value: "${str}"`); | ||
} | ||
|
||
if (expectedLength && str.length !== expectedLength) { | ||
throw new Error( | ||
`The provided hex value does not match the expected length of ${expectedLength} characters: ${str}`, | ||
); | ||
} | ||
} | ||
|
||
toBech32(prefix: string): string { | ||
if (!prefix) { | ||
throw new Error('prefix is required'); | ||
} | ||
|
||
const bytesArray = this.value.match(/.{1,2}/g); | ||
const bytes = new Uint8Array(bytesArray!.map((byte) => parseInt(byte, 16))); | ||
return bech32.encode(prefix, bytes); | ||
} | ||
|
||
toHex(): string { | ||
return this.value; | ||
} | ||
} | ||
|
||
export class EventID extends Hex { | ||
constructor(str: string) { | ||
if (str.startsWith('note')) { | ||
str = bech32ToHex(str); | ||
} | ||
super(str, 64); | ||
} | ||
|
||
toBech32(): string { | ||
return super.toBech32('note'); | ||
} | ||
|
||
equals(other: EventID | string): boolean { | ||
if (typeof other === 'string') { | ||
other = new EventID(other); | ||
} | ||
return this.value === other.value; | ||
} | ||
} | ||
|
||
export class PublicKey extends Hex { | ||
constructor(str: string) { | ||
if (str.startsWith('npub')) { | ||
str = bech32ToHex(str); | ||
} | ||
super(str, 64); | ||
} | ||
|
||
toBech32(): string { | ||
return super.toBech32('npub'); | ||
} | ||
|
||
equals(other: PublicKey | string): boolean { | ||
if (typeof other === 'string') { | ||
other = new PublicKey(other); | ||
} | ||
return this.value === other.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters