diff --git a/index.ts b/index.ts index 249c968..ac782a3 100644 --- a/index.ts +++ b/index.ts @@ -9,6 +9,7 @@ export * as nip05 from './nip05' export * as nip06 from './nip06' export * as nip19 from './nip19' export * as nip26 from './nip26' +export * as nip39 from './nip39' export * as nip57 from './nip57' export * as fj from './fakejson' diff --git a/nip39.test.js b/nip39.test.js new file mode 100644 index 0000000..dbd4b8b --- /dev/null +++ b/nip39.test.js @@ -0,0 +1,15 @@ +/* eslint-env jest */ + +const fetch = require('node-fetch') +const {nip39} = require('./lib/nostr.cjs.js') + +test('validate github claim', async () => { + nip39.useFetchImplementation(fetch) + + let result = await nip39.validateGithub( + 'npub1gcxzte5zlkncx26j68ez60fzkvtkm9e0vrwdcvsjakxf9mu9qewqlfnj5z', + 'vitorpamplona', + 'cf19e2d1d7f8dac6348ad37b35ec8421' + ) + expect(result).toBe(true) +}) diff --git a/nip39.ts b/nip39.ts new file mode 100644 index 0000000..9a94af7 --- /dev/null +++ b/nip39.ts @@ -0,0 +1,27 @@ +var _fetch: any + +try { + _fetch = fetch +} catch {} + +export function useFetchImplementation(fetchImplementation: any) { + _fetch = fetchImplementation +} + +export async function validateGithub( + pubkey: string, + username: string, + proof: string +): Promise { + try { + let res = await ( + await _fetch(`https://gist.github.com/${username}/${proof}/raw`) + ).text() + return ( + res === + `Verifying that I control the following Nostr public key: ${pubkey}` + ) + } catch (_) { + return false + } +}