-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathClient.test.ts
480 lines (431 loc) · 15 KB
/
Client.test.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import assert from 'assert'
import {
newWallet,
newLocalHostClient,
newDevClient,
waitForUserContact,
newLocalHostClientWithCustomWallet,
} from './helpers'
import { buildUserContactTopic } from '../src/utils'
import Client, { ClientOptions } from '../src/Client'
import {
ApiUrls,
CompositeCodec,
Compression,
ContentTypeText,
HttpApiClient,
InMemoryPersistence,
PublishParams,
TextCodec,
} from '../src'
import NetworkKeyManager from '../src/keystore/providers/NetworkKeyManager'
import TopicPersistence from '../src/keystore/persistence/TopicPersistence'
import { PrivateKeyBundleV1 } from '../src/crypto'
import { Wallet } from 'ethers'
import { NetworkKeystoreProvider } from '../src/keystore/providers'
import { PublishResponse } from '@xmtp/proto/ts/dist/types/message_api/v1/message_api.pb'
import LocalStoragePonyfill from '../src/keystore/persistence/LocalStoragePonyfill'
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { generatePrivateKey } from 'viem/accounts'
type TestCase = {
name: string
newClient: (opts?: Partial<ClientOptions>) => Promise<Client<any>>
}
const mockEthRequest = jest.fn()
jest.mock('../src/utils/ethereum', () => {
return {
__esModule: true,
getEthereum: jest.fn(() => {
const ethereum: any = {
request: mockEthRequest,
}
ethereum.providers = [ethereum]
ethereum.detected = [ethereum]
ethereum.isMetaMask = true
return ethereum
}),
}
})
describe('Client', () => {
const tests: TestCase[] = [
{
name: 'local host node',
newClient: newLocalHostClient,
},
{
name: 'local host node with non-ethers wallet',
newClient: newLocalHostClientWithCustomWallet,
},
]
if (process.env.CI || process.env.TESTNET) {
tests.push({
name: 'dev',
newClient: newDevClient,
})
}
tests.forEach((testCase) => {
describe(testCase.name, () => {
let alice: Client, bob: Client
beforeEach(async () => {
alice = await testCase.newClient({ publishLegacyContact: true })
bob = await testCase.newClient({ publishLegacyContact: true })
await waitForUserContact(alice, alice)
await waitForUserContact(bob, bob)
})
it('user contacts published', async () => {
const alicePublic = await alice.getUserContact(alice.address)
assert.deepEqual(alice.publicKeyBundle, alicePublic)
const bobPublic = await bob.getUserContact(bob.address)
assert.deepEqual(bob.publicKeyBundle, bobPublic)
})
it('user contacts are filtered to valid contacts', async () => {
// publish bob's keys to alice's contact topic
const bobPublic = bob.publicKeyBundle
await alice.publishEnvelopes([
{
message: bobPublic.toBytes(),
contentTopic: buildUserContactTopic(alice.address),
},
])
const alicePublic = await alice.getUserContact(alice.address)
assert.deepEqual(alice.publicKeyBundle, alicePublic)
})
it('Check address can be sent to', async () => {
const can_mesg_a = await alice.canMessage('NOT AN ADDRESS')
assert.equal(can_mesg_a, false)
const can_mesg_b = await alice.canMessage(bob.address)
assert.equal(can_mesg_b, true)
const lower = await alice.canMessage(bob.address.toLowerCase())
assert.equal(lower, true)
})
})
})
})
describe('bootstrapping', () => {
let alice: Wallet
beforeEach(async () => {
alice = newWallet()
})
it('can bootstrap with a new wallet and persist the private key bundle', async () => {
const client = await Client.create(alice, { env: 'local' })
const manager = new NetworkKeyManager(
alice,
new TopicPersistence(client.apiClient)
)
const loadedBundle = await manager.loadPrivateKeyBundle()
expect(loadedBundle).toBeInstanceOf(PrivateKeyBundleV1)
expect(
loadedBundle?.identityKey.publicKey.walletSignatureAddress()
).toEqual(alice.address)
})
it('fails to load if no valid keystore provider is available', async () => {
expect(
Client.create(alice, { env: 'local', keystoreProviders: [] })
).rejects.toThrow('No keystore providers available')
})
it('is able to bootstrap from the network', async () => {
const opts: Partial<ClientOptions> = { env: 'local' }
// Create with the default keystore providers to ensure bootstrapping
const firstClient = await Client.create(alice, opts)
const secondClient = await Client.create(alice, {
...opts,
keystoreProviders: [new NetworkKeystoreProvider()],
})
expect(secondClient).toBeInstanceOf(Client)
expect(secondClient.address).toEqual(firstClient.address)
})
it('is able to bootstrap from a predefined private key', async () => {
const opts: Partial<ClientOptions> = { env: 'local' }
const keys = await Client.getKeys(alice, opts)
const client = await Client.create(null, {
...opts,
privateKeyOverride: keys,
})
expect(client.address).toEqual(alice.address)
})
})
describe('skipContactPublishing', () => {
it('skips publishing when flag is set to true', async () => {
const alice = newWallet()
await Client.create(alice, { skipContactPublishing: true, env: 'local' })
expect(await Client.canMessage(alice.address, { env: 'local' })).toBeFalsy()
})
it('publishes contact when flag is false', async () => {
const alice = newWallet()
await Client.create(alice, { skipContactPublishing: false, env: 'local' })
expect(
await Client.canMessage(alice.address, { env: 'local' })
).toBeTruthy()
})
})
describe('encodeContent', () => {
it('passes deflate compression option through properly', async function () {
const c = await newLocalHostClient()
const utf8Encode = new TextEncoder()
const uncompressed = utf8Encode.encode('hello world '.repeat(20))
const compressed = Uint8Array.from([
10, 18, 10, 8, 120, 109, 116, 112, 46, 111, 114, 103, 18, 4, 116, 101,
120, 116, 24, 1, 18, 17, 10, 8, 101, 110, 99, 111, 100, 105, 110, 103, 18,
5, 85, 84, 70, 45, 56, 40, 0, 34, 48, 120, 156, 51, 52, 48, 209, 49, 52,
48, 212, 49, 52, 176, 128, 96, 67, 67, 29, 99, 35, 29, 67, 67, 75, 48,
211, 208, 208, 4, 42, 101, 0, 22, 30, 85, 61, 170, 122, 84, 53, 237, 85,
3, 0, 139, 43, 173, 229,
])
const payload = await c.encodeContent(uncompressed, {
compression: Compression.COMPRESSION_DEFLATE,
})
assert.deepEqual(Uint8Array.from(payload), compressed)
})
})
describe('canMessage', () => {
it('can confirm a user is on the network statically', async () => {
const registeredClient = await newLocalHostClient({
codecs: [new TextCodec()],
})
await waitForUserContact(registeredClient, registeredClient)
const canMessageRegisteredClient = await Client.canMessage(
registeredClient.address,
{
env: 'local',
}
)
expect(canMessageRegisteredClient).toBeTruthy()
const canMessageUnregisteredClient = await Client.canMessage(
newWallet().address,
{ env: 'local' }
)
expect(canMessageUnregisteredClient).toBeFalsy()
})
})
describe('canMessageBatch', () => {
it('can confirm multiple users are on the network statically', async () => {
// Create 10 registered clients
const registeredClients = await Promise.all(
Array.from({ length: 10 }, () => newLocalHostClient())
)
// Wait for all clients to be registered
await Promise.all(
registeredClients.map((client) => waitForUserContact(client, client))
)
// Now call canMessage with all of the peerAddresses
const canMessageRegisteredClients = await Client.canMessage(
registeredClients.map((client) => client.address),
{
env: 'local',
}
)
// Expect all of the clients to be registered, so response should be all True
expect(canMessageRegisteredClients).toEqual(
registeredClients.map(() => true)
)
const canMessageUnregisteredClient = await Client.canMessage(
[newWallet().address],
{ env: 'local' }
)
expect(canMessageUnregisteredClient).toEqual([false])
})
})
describe('canMessageMultipleBatches', () => {
it('can confirm many multiple users are on the network statically', async () => {
const registeredClients = await Promise.all(
Array.from({ length: 10 }, () => newLocalHostClient())
)
// Wait for all clients to be registered
await Promise.all(
registeredClients.map((client) => waitForUserContact(client, client))
)
// Repeat registeredClients 8 times to arrive at 80 addresses
const initialPeerAddresses = registeredClients.map(
(client) => client.address
)
const repeatedPeerAddresses: string[] = []
for (let i = 0; i < 8; i++) {
repeatedPeerAddresses.push(...initialPeerAddresses)
}
// Add 5 fake addresses
repeatedPeerAddresses.push(
...Array.from(
{ length: 5 },
() => '0x0000000000000000000000000000000000000000'
)
)
// Now call canMessage with all of the peerAddresses
const canMessageRegisteredClients = await Client.canMessage(
repeatedPeerAddresses,
{
env: 'local',
}
)
// Expect 80 True and 5 False
expect(canMessageRegisteredClients).toEqual(
Array.from({ length: 80 }, () => true).concat(
Array.from({ length: 5 }, () => false)
)
)
})
})
describe('publishEnvelopes', () => {
it('can send a valid envelope', async () => {
const c = await newLocalHostClient()
const envelope = {
contentTopic: '/xmtp/0/foo/proto',
message: new TextEncoder().encode('hello world'),
timestamp: new Date(),
}
await c.publishEnvelopes([envelope])
})
it('rejects with invalid envelopes', async () => {
const c = await newLocalHostClient()
// Set a bogus authenticator so we can have failing publishes
c.apiClient.setAuthenticator({
// @ts-ignore-next-line
createToken: async () => ({
toBase64: () => 'derp!',
}),
})
const envelope = {
contentTopic: buildUserContactTopic(c.address),
message: new TextEncoder().encode('hello world'),
timestamp: new Date(),
}
expect(c.publishEnvelopes([envelope])).rejects.toThrow()
})
})
describe('ClientOptions', () => {
const tests = [
{
name: 'local docker node',
newClient: newLocalHostClient,
},
]
if (process.env.CI || process.env.TESTNET) {
tests.push({
name: 'dev',
newClient: newDevClient,
})
}
tests.forEach((testCase) => {
it('Default/empty options', async () => {
const c = await testCase.newClient()
})
it('Partial specification', async () => {
const c = await testCase.newClient({
persistConversations: true,
})
})
})
describe('custom codecs', () => {
it('gives type errors when you use the wrong types', async () => {
const client = await Client.create(newWallet(), { env: 'local' })
const other = await Client.create(newWallet(), { env: 'local' })
const convo = await client.conversations.newConversation(other.address)
expect(convo).toBeTruthy()
try {
// Add ts-expect-error so that if we break the type casting someone will notice
// @ts-expect-error
await convo.send(123)
const messages = await convo.messages()
for (const message of messages) {
// Strings don't have this kind of method
// @ts-expect-error
message.toFixed()
}
} catch (e) {
return
}
fail()
})
it('allows you to use custom content types', async () => {
const client = await Client.create(newWallet(), {
codecs: [new CompositeCodec()],
})
const other = await Client.create(newWallet())
const convo = await client.conversations.newConversation(other.address)
expect(convo).toBeTruthy()
// This will have a type error if the codecs field isn't being respected
await convo.send({ parts: [{ type: ContentTypeText, content: 'foo' }] })
})
})
describe('Pluggable API client', () => {
it('allows you to specify a custom API client factory', async () => {
const expectedError = new Error('CustomApiClient')
class CustomApiClient extends HttpApiClient {
publish(messages: PublishParams[]): Promise<PublishResponse> {
return Promise.reject(expectedError)
}
}
const c = newLocalHostClient({
apiClientFactory: (opts) => {
return new CustomApiClient(ApiUrls.local)
},
})
await expect(c).rejects.toThrow(expectedError)
})
})
describe('pluggable persistence', () => {
it('allows for an override of the persistence engine', async () => {
class MyNewPersistence extends InMemoryPersistence {
getItem(key: string): Promise<Uint8Array | null> {
return Promise.reject(new Error('MyNewPersistence'))
}
}
const c = newLocalHostClient({
basePersistence: new MyNewPersistence(new LocalStoragePonyfill()),
})
await expect(c).rejects.toThrow('MyNewPersistence')
})
})
describe('canGetKeys', () => {
it('returns true if the useSnaps flag is false', async () => {
mockEthRequest.mockRejectedValue(new Error('foo'))
const isSnapsReady = await Client.isSnapsReady()
expect(isSnapsReady).toBe(false)
})
it('returns false if the user has a Snaps capable browser and snaps are enabled', async () => {
mockEthRequest.mockResolvedValue([])
const isSnapsReady = await Client.isSnapsReady()
expect(isSnapsReady).toBe(true)
})
})
describe('viem', () => {
it('allows you to use a viem WalletClient', async () => {
const privateKey = generatePrivateKey()
const account = privateKeyToAccount(privateKey)
const walletClient = createWalletClient({
account,
chain: mainnet,
transport: http(),
})
const c = await Client.create(walletClient)
expect(c).toBeDefined()
expect(c.address).toEqual(account.address)
})
it('creates an identical client between viem and ethers', async () => {
const randomWallet = Wallet.createRandom()
const privateKey = randomWallet.privateKey
const account = privateKeyToAccount(privateKey as `0x${string}`)
const walletClient = createWalletClient({
account,
chain: mainnet,
transport: http(),
})
const viemClient = await Client.create(walletClient)
const ethersClient = await Client.create(randomWallet)
expect(viemClient.address).toEqual(ethersClient.address)
expect(
viemClient.publicKeyBundle.equals(ethersClient.publicKeyBundle)
).toBe(true)
})
it('fails if you use a viem WalletClient without an account', async () => {
const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
})
await expect(Client.create(walletClient)).rejects.toThrow(
'WalletClient is not configured'
)
})
})
})