-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-identity.ts
289 lines (253 loc) · 9.77 KB
/
test-identity.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { assert, expect } from 'aegir/chai'
import { base32 } from 'multiformats/bases/base32'
import { fixtPath, getTestPaths, names, tempPath } from './utils/constants.js'
import { getTestIdentities, kpi } from './utils/identities.js'
import { getTestIpfs, offlineIpfsOptions } from './utils/ipfs.js'
import { getTestLibp2p } from './utils/libp2p.js'
import type { GossipHelia } from '@/interface.js'
import type { PublicKey } from '@libp2p/interface/keys'
import type { Keychain } from '@libp2p/keychain'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import { Identity, basalIdentity } from '@/identity/basal/index.js'
const testName = 'basal identity'
describe(testName, () => {
let ipfs: GossipHelia,
blockstore: Blockstore,
identities: Datastore,
keychain: Keychain,
identity: Identity
let tempIpfs: GossipHelia, tempIdentities: Datastore, tempKeychain: Keychain
const expectedProtocol = '/hldb/identity/basal'
const name = names.name0
const password = ''
const identityModule = basalIdentity()
const dataEmpty = new Uint8Array()
const signedEmpty = base32.decode(
'bgbcqeiiayc2skoa4am3u3kmq4io6zjspbroxyfw2duj2lzxagrxfoafes4eaeid3eayxiin7neu6s4jhngwj2uoxoxxhzrdcckhtinpqtugc64tyze'
)
const authstring =
'bafyreibvk33g3t2jktm3i7q7vwugu3mqoc3oajlymb7u46qn6kqpsexl4u'
before(async () => {
const fixtTestPaths = getTestPaths(fixtPath, testName)
ipfs = await getTestIpfs(fixtTestPaths, offlineIpfsOptions)
blockstore = ipfs.blockstore
identities = await getTestIdentities(fixtTestPaths)
const libp2p = await getTestLibp2p(ipfs)
keychain = libp2p.services.keychain
identity = await identityModule.import({
name,
identities,
keychain,
kpi
}).catch(async () => identityModule.get({ name, identities, keychain }))
const tempTestPaths = getTestPaths(tempPath, testName)
tempIpfs = await getTestIpfs(tempTestPaths, offlineIpfsOptions)
tempIdentities = await getTestIdentities(tempTestPaths)
const tempLibp2p = await getTestLibp2p(ipfs)
tempKeychain = tempLibp2p.services.keychain
})
after(async () => {
await ipfs.stop()
await tempIpfs.stop()
})
describe('Class', () => {
it('exposes static properties', () => {
assert.isOk(identityModule.protocol)
assert.isOk(identityModule.get)
assert.isOk(identityModule.fetch)
assert.isOk(identityModule.asIdentity)
assert.isOk(identityModule.export)
assert.isOk(identityModule.import)
assert.isOk(identityModule.verify)
})
it(`.type is equal to '${expectedProtocol}'`, () => {
assert.strictEqual(identityModule.protocol, expectedProtocol)
})
describe('.get', () => {
it('grabs existing identity', async () => {
identity = await identityModule.get({ name, identities, keychain })
assert.strictEqual(identity.name, name)
assert.strictEqual(identity.block.cid.toString(base32), authstring)
assert.isOk(identity instanceof Identity)
})
it('creates a new identity', async () => {
const randomName = Math.random().toString()
const _identity = await identityModule.get({
name: randomName,
identities: tempIdentities,
keychain: tempKeychain
})
assert.strictEqual(_identity.name, randomName)
assert.isOk(_identity instanceof Identity)
})
// it('returns an existing instance of the identity', async () => {})
})
describe('.fetch', () => {
it('fetches valid identity', async () => {
await blockstore.put(identity.block.cid, identity.block.bytes)
const _identity = await identityModule.fetch({ blockstore, auth: identity.auth })
assert.notStrictEqual(_identity, identity)
assert.strictEqual(
_identity.block.cid.toString(base32),
identity.block.cid.toString(base32)
)
assert.strictEqual(_identity.pubkey.equals(identity.pubkey), true)
assert.strictEqual(_identity.name, undefined)
})
// it('throws fetching invalid identity', async () => {})
})
describe('.asIdentity', () => {
it('returns the same instance if possible', async () => {
const _identity = (identityModule.asIdentity(identity)) as Identity
assert.strictEqual(_identity, identity)
assert.strictEqual(
_identity.block.cid.toString(base32),
identity.block.cid.toString(base32)
)
assert.strictEqual(_identity.pubkey.equals(identity.pubkey), true)
assert.strictEqual(_identity.name, name)
})
it('returns a new instance if needed', async () => {
const _identity = (identityModule.asIdentity({
block: identity.block
})) as Identity
assert.notStrictEqual(_identity, identity)
assert.strictEqual(
_identity.block.cid.toString(base32),
identity.block.cid.toString(base32)
)
assert.strictEqual(_identity.pubkey.equals(identity.pubkey), true)
assert.strictEqual(_identity.name, undefined)
})
})
describe('.import and .export', () => {
it('imports an encoded identity/keypair', async () => {
const name = names.name1
const imported = await identityModule.import({
name,
identities: tempIdentities,
keychain: tempKeychain,
kpi
})
assert.isOk(await tempKeychain.exportKey(name, password))
assert.deepEqual(
imported.auth.toString(base32),
identity.auth.toString(base32)
)
assert.isOk(imported instanceof Identity)
})
it('rejects importing to an existing identity', async () => {
const name = names.name1
const promise = identityModule.import({
name,
identities: tempIdentities,
keychain: tempKeychain,
kpi
})
await expect(promise).to.eventually.be.rejected()
})
it('exports an encoded identity/keypair', async () => {
const name = names.name1
const exported = await identityModule.export({
name,
identities: tempIdentities,
keychain: tempKeychain
})
assert.deepEqual(new Uint8Array(exported), new Uint8Array(exported))
})
it('rejects exporting a non-existant identity', async () => {
const name = names.name2
const promise = identityModule.export({
name,
identities: tempIdentities,
keychain: tempKeychain
})
await expect(promise).to.eventually.be.rejected()
})
})
describe('.sign', () => {
it('signs an empty byte array', async () => {
const data = dataEmpty
const sig = await identityModule.sign(identity, data)
assert.isOk(sig instanceof Uint8Array)
assert.strictEqual(sig.toString(), signedEmpty.toString())
})
})
describe('.verify', () => {
it('verifies a valid signature', async () => {
const data = dataEmpty
const sig = signedEmpty
const verified = await identityModule.verify(identity, data, sig)
assert.strictEqual(verified, true)
})
it('unverifies an invalid signature', async () => {
const data = new Uint8Array([1])
const sig = signedEmpty
const verified = await identityModule.verify(identity, data, sig)
assert.strictEqual(verified, false)
})
it('rejects verifying signatures without a pubkey', async () => {
const _identity = (identityModule.asIdentity({
block: identity.block
})) as Identity
_identity.pubkey = undefined as unknown as PublicKey
const data = new Uint8Array([1])
const sig = signedEmpty
const promise = identityModule.verify(_identity, data, sig)
await expect(promise).to.eventually.be.rejected()
})
})
})
describe('Instance', () => {
it('exposes instance properties', async () => {
assert.isOk(identity.name)
assert.isOk(identity.block)
assert.isOk(identity.pubkey)
assert.isOk(identity.sign)
assert.isOk(identity.verify)
assert.isOk(identity.auth)
assert.isOk(identity.id)
assert.isOk(identity.pub)
assert.isOk(identity.sig)
assert.strictEqual(identity.auth, identity.block.cid)
assert.strictEqual(identity.id, identity.block.value.id)
assert.strictEqual(identity.pub, identity.block.value.pub)
assert.strictEqual(identity.sig, identity.block.value.sig)
assert.isOk(identity.id instanceof Uint8Array)
assert.isOk(identity.pub instanceof Uint8Array)
assert.isOk(identity.sig instanceof Uint8Array)
assert.isOk(await identity.verify(identity.pub, identity.sig))
})
describe('.sign', () => {
it('signs an empty byte array', async () => {
const data = dataEmpty
const sig = await identity.sign(data)
assert.isOk(sig instanceof Uint8Array)
assert.deepEqual(sig, signedEmpty)
})
it('rejects signing data without private key', async () => {
const _identity = (identityModule.asIdentity({
block: identity.block
})) as Identity
const data = dataEmpty
const promise = _identity.sign(data)
await expect(promise).to.eventually.be.rejected()
})
})
describe('.verify', () => {
it('verifies a valid signature', async () => {
const data = dataEmpty
const sig = signedEmpty
const verified = await identity.verify(data, sig)
assert.strictEqual(verified, true)
})
it('unverifies an invalid signature', async () => {
const data = new Uint8Array([1])
const sig = signedEmpty
const verified = await identity.verify(data, sig)
assert.strictEqual(verified, false)
})
})
})
})