prettify and lint.

This commit is contained in:
fiatjaf 2023-12-16 12:39:24 -03:00
parent 0108e3b605
commit d16f3f77c3
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
6 changed files with 13 additions and 24 deletions

View File

@ -9,9 +9,7 @@ test('generate private key from a mnemonic', async () => {
test('generate private key for account 1 from a mnemonic', async () => { test('generate private key for account 1 from a mnemonic', async () => {
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong' const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
const privateKey = privateKeyFromSeedWords(mnemonic, undefined, 1) const privateKey = privateKeyFromSeedWords(mnemonic, undefined, 1)
expect(privateKey).toEqual( expect(privateKey).toEqual('b5fc7f229de3fb5c189063e3b3fc6c921d8f4366cff5bd31c6f063493665eb2b')
'b5fc7f229de3fb5c189063e3b3fc6c921d8f4366cff5bd31c6f063493665eb2b'
)
}) })
test('generate private key from a mnemonic and passphrase', async () => { test('generate private key from a mnemonic and passphrase', async () => {
@ -25,7 +23,5 @@ test('generate private key for account 1 from a mnemonic and passphrase', async
const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong' const mnemonic = 'zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong'
const passphrase = '123' const passphrase = '123'
const privateKey = privateKeyFromSeedWords(mnemonic, passphrase, 1) const privateKey = privateKeyFromSeedWords(mnemonic, passphrase, 1)
expect(privateKey).toEqual( expect(privateKey).toEqual('2e0f7bd9e3c3ebcdff1a90fb49c913477e7c055eba1a415d571b6a8c714c7135')
'2e0f7bd9e3c3ebcdff1a90fb49c913477e7c055eba1a415d571b6a8c714c7135'
)
}) })

View File

@ -3,11 +3,7 @@ import { wordlist } from '@scure/bip39/wordlists/english'
import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from '@scure/bip39' import { generateMnemonic, mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'
import { HDKey } from '@scure/bip32' import { HDKey } from '@scure/bip32'
export function privateKeyFromSeedWords( export function privateKeyFromSeedWords(mnemonic: string, passphrase?: string, accountIndex = 0): string {
mnemonic: string,
passphrase?: string,
accountIndex = 0
): string {
let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase)) let root = HDKey.fromMasterSeed(mnemonicToSeedSync(mnemonic, passphrase))
let privateKey = root.derive(`m/44'/1237'/${accountIndex}'/0/0`).privateKey let privateKey = root.derive(`m/44'/1237'/${accountIndex}'/0/0`).privateKey
if (!privateKey) throw new Error('could not derive private key') if (!privateKey) throw new Error('could not derive private key')

View File

@ -189,7 +189,7 @@ export function nprofileEncode(profile: ProfilePointer): `nprofile1${string}` {
export function neventEncode(event: EventPointer): `nevent1${string}` { export function neventEncode(event: EventPointer): `nevent1${string}` {
let kindArray let kindArray
if (event.kind != undefined) { if (event.kind !== undefined) {
kindArray = integerToUint8Array(event.kind) kindArray = integerToUint8Array(event.kind)
} }

View File

@ -13,7 +13,7 @@ export interface NostrURIMatch extends NostrURI {
} }
/** Find and decode all NIP-21 URIs. */ /** Find and decode all NIP-21 URIs. */
export function * matchAll(content: string): Iterable<NostrURIMatch> { export function* matchAll(content: string): Iterable<NostrURIMatch> {
const matches = content.matchAll(regex()) const matches = content.matchAll(regex())
for (const match of matches) { for (const match of matches) {

View File

@ -1,4 +1,4 @@
import {matchAll, replaceAll} from './nip30.ts' import { matchAll, replaceAll } from './nip30.ts'
test('matchAll', () => { test('matchAll', () => {
const result = matchAll('Hello :blobcat: :disputed: ::joy:joy:') const result = matchAll('Hello :blobcat: :disputed: ::joy:joy:')
@ -8,21 +8,21 @@ test('matchAll', () => {
name: 'blobcat', name: 'blobcat',
shortcode: ':blobcat:', shortcode: ':blobcat:',
start: 6, start: 6,
end: 15 end: 15,
}, },
{ {
name: 'disputed', name: 'disputed',
shortcode: ':disputed:', shortcode: ':disputed:',
start: 16, start: 16,
end: 26 end: 26,
} },
]) ])
}) })
test('replaceAll', () => { test('replaceAll', () => {
const content = 'Hello :blobcat: :disputed: ::joy:joy:' const content = 'Hello :blobcat: :disputed: ::joy:joy:'
const result = replaceAll(content, ({name}) => { const result = replaceAll(content, ({ name }) => {
return `<img src="https://ditto.pub/emoji/${name}.png" />` return `<img src="https://ditto.pub/emoji/${name}.png" />`
}) })

View File

@ -21,7 +21,7 @@ export interface CustomEmojiMatch extends CustomEmoji {
} }
/** Find all custom emoji shortcodes. */ /** Find all custom emoji shortcodes. */
export function * matchAll(content: string): Iterable<CustomEmojiMatch> { export function* matchAll(content: string): Iterable<CustomEmojiMatch> {
const matches = content.matchAll(regex()) const matches = content.matchAll(regex())
for (const match of matches) { for (const match of matches) {
@ -32,7 +32,7 @@ export function * matchAll(content: string): Iterable<CustomEmojiMatch> {
shortcode: shortcode as `:${string}:`, shortcode: shortcode as `:${string}:`,
name, name,
start: match.index!, start: match.index!,
end: match.index! + shortcode.length end: match.index! + shortcode.length,
} }
} catch (_e) { } catch (_e) {
// do nothing // do nothing
@ -41,10 +41,7 @@ export function * matchAll(content: string): Iterable<CustomEmojiMatch> {
} }
/** Replace all emoji shortcodes in the content. */ /** Replace all emoji shortcodes in the content. */
export function replaceAll( export function replaceAll(content: string, replacer: (match: CustomEmoji) => string): string {
content: string,
replacer: (match: CustomEmoji) => string
): string {
return content.replaceAll(regex(), (shortcode, name) => { return content.replaceAll(regex(), (shortcode, name) => {
return replacer({ return replacer({
shortcode: shortcode as `:${string}:`, shortcode: shortcode as `:${string}:`,