forked from nbd-wtf/nostr-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnip111.test.js
111 lines (99 loc) · 4.95 KB
/
nip111.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* eslint-env jest */
const ethers = require('ethers')
const fetch = require('node-fetch')
const { nip111, nip05 } = require('./lib/nostr.cjs')
globalThis.crypto = require('crypto')
require('dotenv').config({ path: '.env.nip111' })
privateKey = process.env.ETHER_PRIVATE_KEY;
let wallet = new ethers.Wallet(privateKey);
nip111.useFetchImplementation(fetch)
nip05.useFetchImplementation(fetch)
// @dev: ↓ YOU MUST ENTER THE CORRESPONDING PRIVATE KEY IN THE .env.nip111 FILE
let address = '0x0d2C290bb3fE24D8D566268d5c41527c519715Db' // ← signing checksummed ethereum pubkey/address
let caip10 = `eip155:1:${address}`
// @dev : uses arbitrary signature not linked to any ethereum account to test key generation
// SHOULD result in successful key generation
test('Private Key from Deterministic Signature and Identifiers', async () => {
let username = '[email protected]'
let signature = 'f'.padEnd(130, 'f')
let password = 'hello dear fucker'
expect(
await nip111.privateKeyFromX(username, caip10, signature, password)
).toEqual('fd6a6c03eadf0db178f79de3a2dd3f0464fb5fac96608842d68ce64da2e40954')
// without password
expect(
await nip111.privateKeyFromX(username, caip10, signature)
).toEqual('897c5140a6e0e09b512c755cfd60829998fb0d046c52fae0846cca045185a52b')
// 0x+hex signature; SHOULD BE agnostic to '0x' prefix
expect(
await nip111.privateKeyFromX(username, caip10, '0x' + signature, password)
).toEqual('fd6a6c03eadf0db178f79de3a2dd3f0464fb5fac96608842d68ce64da2e40954')
})
// @dev : uses arbitrary signature plus NIP-02 identifier to Sign-In-With-X (SIWX)
// SHOULD result in successful key generation and login
test('Login with Deterministic Signature and NIP-02 Identifiers', async () => {
let username = 'nipxxtest1'
let signature = 'f'.padEnd(130, 'f')
let password = 'hello dear fucker'
expect(
await nip111.signInWithX(username, caip10, signature, password)
).toEqual({
'petname': username,
'privkey': '3f0b9c0ddb02d7056c4908d362c8d03072ef3f2dc1eb10a6ef9706dc3f017198',
// pubkey : 6cb22c9037a08313f0f1f2cfafebcb14cc57acef43b11c6343e6a0d5b46a4abe
'profile': null
})
})
// @dev : uses arbitrary signature on an arbitrary non-existent NIP-05 identifier to Sign-In-With-X (SIWX) for registration
// SHOULD result in private key generation for registration
test('Generate Public Key for NIP-05 Identifier without verifying record', async () => {
let username = '[email protected]'
let message = `Log into Nostr client as '${username}'\n\nIMPORTANT: Please verify the integrity and authenticity of connected Nostr client before signing this message\n\nSIGNED BY: ${caip10}`
let promise = wallet.signMessage(message)
let signature = await promise;
let password = ''
expect(
await nip111.signInWithXStandalone('[email protected]', caip10, signature, password)
).toEqual({
'petname': username.split('@')[0],
'pubkey': '84ef21c0150c3a6abaf9da9c6e078281c2c1af09abb72a6518dce2ff82c9cb45',
})
})
// @dev : uses arbitrary signature on a NIP-05 identifier to Sign-In-With-X (SIWX)
// SHOULD result in 'Invalid Signature/Password' since NIP-05 identifiers must sign in with an X wallet
test('Invalid Signature/Password in NIP-05 Identifiers', async () => {
let username = '[email protected]'
let signature = 'f'.padEnd(130, 'f')
let password = ''
await expect(async () => {
await nip111.signInWithX(username, caip10, signature, password)
}).rejects.toThrow('Invalid Signature/Password');
})
// @dev : uses arbitrary signature on an arbitrary non-existent NIP-05 identifier to Sign-In-With-X (SIWX)
// SHOULD result in 'Nostr Profile Not Found' since NIP-05 doesn't exist
test('NIP-05 Identifiers Not Set', async () => {
let signature = 'f'.padEnd(130, 'f')
let password = 'hello dear fucker'
await expect(async () => {
await nip111.signInWithX('[email protected]', caip10, signature, password)
}).rejects.toThrow('Nostr Profile Not Found');
})
// @dev : uses ethereum signature from a valid NIP-05 identifier with an associated ethereum account to Sign-In-With-Ethereum (SIWE)
// SHOULD result in successful key generation and login from a valid signature verified to originate from the correct ethereum account
test('Login with Deterministic Signature and NIP-05 Identifiers', async () => {
let username = '[email protected]'
let message = `Log into Nostr client as '${username}'\n\nIMPORTANT: Please verify the integrity and authenticity of connected Nostr client before signing this message\n\nSIGNED BY: ${caip10}`
let promise = wallet.signMessage(message) // ↑ signed by address's private key
let signature = await promise;
let password = 'hello dear fucker'
expect(
await nip111.signInWithX(username, caip10, signature, password)
).toEqual({
'petname': username.split('@')[0],
'profile': {
'pubkey': '7b9debae4b1767d46797c8289172d592e6e713220dcd2647d712c9a229d5ffe0',
'relays': []
},
'privkey': 'ddd49f5dbf378d993a3322c1fc9f2b3357c1c9568655ccba08403ef30f05c416'
})
})