|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { bytesToHex } from "@noble/hashes/utils"; |
| 3 | +import { normalizeToPubkey, normalizeToSecretKey } from "../nip-19.js"; |
| 4 | + |
| 5 | +describe("normalizeToPubkey", () => { |
| 6 | + it("should get pubkey from npub", () => { |
| 7 | + expect(normalizeToPubkey("npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr")).toEqual( |
| 8 | + "266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5", |
| 9 | + ); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should get pubkey from nprofile", () => { |
| 13 | + expect( |
| 14 | + normalizeToPubkey( |
| 15 | + "nprofile1qyw8wumn8ghj7umpw3jkcmrfw3jju6r6wfjrzdpe9e3k7mf0qyf8wumn8ghj7mn0wd68yat99e3k7mf0qqszv6q4uryjzr06xfxxew34wwc5hmjfmfpqn229d72gfegsdn2q3fg5g7lja", |
| 16 | + ), |
| 17 | + ).toEqual("266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should return hex pubkey", () => { |
| 21 | + expect(normalizeToPubkey("266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5")).toEqual( |
| 22 | + "266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5", |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should throw on invalid hex pubkey", () => { |
| 27 | + expect(() => { |
| 28 | + normalizeToPubkey("5028372"); |
| 29 | + }).toThrow(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should throw on invalid string", () => { |
| 33 | + expect(() => { |
| 34 | + normalizeToPubkey("testing"); |
| 35 | + }).toThrow(); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("normalizeToSecretKey", () => { |
| 40 | + it("should get secret key from nsec", () => { |
| 41 | + expect(bytesToHex(normalizeToSecretKey("nsec1xe7znq745x5n68566l32ru72aajz3pk2cys9lnf3tuexvkw0dldsj8v2lm"))).toEqual( |
| 42 | + "367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should get secret key from raw hex", () => { |
| 47 | + expect( |
| 48 | + bytesToHex(normalizeToSecretKey("367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb")), |
| 49 | + ).toEqual("367c2983d5a1a93d1e9ad7e2a1f3caef642886cac1205fcd315f326659cf6fdb"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should throw on invalid hex key", () => { |
| 53 | + expect(() => { |
| 54 | + normalizeToSecretKey("209573290"); |
| 55 | + }).toThrow(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should throw on npub", () => { |
| 59 | + expect(() => { |
| 60 | + normalizeToSecretKey("npub1ye5ptcxfyyxl5vjvdjar2ua3f0hynkjzpx552mu5snj3qmx5pzjscpknpr"); |
| 61 | + }).toThrow(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments