1
1
import * as anchor from '@project-serum/anchor' ;
2
2
import { EventParser } from '@project-serum/anchor' ;
3
3
import { Wallet } from '@project-serum/anchor/src/provider' ;
4
- import { Connection , Keypair , PublicKey } from '@solana/web3.js' ;
4
+ import { Connection , PublicKey } from '@solana/web3.js' ;
5
5
6
6
import { sleep , waitForFinality , Wallet_ } from '../utils' ;
7
7
import { ENCRYPTION_OVERHEAD_BYTES } from '../utils/ecdh-encryption' ;
@@ -118,37 +118,37 @@ export async function getMetadataProgramAddress(
118
118
// TODO: Simplify this function further now that we're no longer decrypting the device token.
119
119
export async function getMetadata (
120
120
program : anchor . Program ,
121
- user : PublicKey | anchor . web3 . Keypair ,
122
- otherParty ?: PublicKey | anchor . web3 . Keypair | null ,
121
+ user : PublicKey | Wallet ,
122
+ otherParty ?: PublicKey | Wallet | null ,
123
123
) : Promise < Metadata > {
124
124
let shouldDecrypt = false ;
125
- let userIsKeypair = false ;
126
- let otherPartyIsKeypair = false ;
125
+ let userIsWallet = false ;
126
+ let otherPartyIsWallet = false ;
127
127
128
128
try {
129
129
// assume user is pubkey
130
130
new anchor . web3 . PublicKey ( user . toString ( ) ) ;
131
131
} catch {
132
- // user is keypair
133
- userIsKeypair = true ;
132
+ // user is wallet
133
+ userIsWallet = true ;
134
134
}
135
135
136
136
try {
137
137
// assume otherParty is pubkey
138
138
new anchor . web3 . PublicKey ( otherParty ?. toString ( ) || '' ) ;
139
139
} catch {
140
- // otherParty is keypair or null
141
- otherPartyIsKeypair = ( otherParty && true ) || false ;
140
+ // otherParty is wallet or null
141
+ otherPartyIsWallet = ( otherParty && true ) || false ;
142
142
}
143
143
144
- if ( otherParty && ( userIsKeypair || otherPartyIsKeypair ) ) {
144
+ if ( otherParty && ( userIsWallet || otherPartyIsWallet ) ) {
145
145
// cases 3 - 5
146
146
shouldDecrypt = true ;
147
147
}
148
148
149
149
const [ metadataAddress ] = await getMetadataProgramAddress (
150
150
program ,
151
- userIsKeypair ? ( user as Keypair ) . publicKey : ( user as PublicKey ) ,
151
+ userIsWallet ? ( user as Wallet ) . publicKey : ( user as PublicKey ) ,
152
152
) ;
153
153
const metadata = await program . account . metadataAccount . fetch ( metadataAddress ) ;
154
154
@@ -162,51 +162,52 @@ export async function getMetadata(
162
162
163
163
export async function createMetadata (
164
164
program : anchor . Program ,
165
- user : anchor . web3 . Keypair | Wallet ,
166
165
) : Promise < Metadata > {
166
+ const wallet = program . provider . wallet ;
167
+ const publicKey = wallet . publicKey ;
167
168
const [ metadataAddress , metadataNonce ] = await getMetadataProgramAddress (
168
169
program ,
169
- user . publicKey ,
170
+ publicKey ,
170
171
) ;
171
172
const tx = await program . rpc . createMetadata ( new anchor . BN ( metadataNonce ) , {
172
173
accounts : {
173
- user : user . publicKey ,
174
+ user : publicKey ,
174
175
metadata : metadataAddress ,
175
176
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
176
177
systemProgram : anchor . web3 . SystemProgram . programId ,
177
178
} ,
178
- signers : 'secretKey' in user ? [ user ] : [ ] ,
179
179
} ) ;
180
180
await waitForFinality ( program , tx ) ;
181
- return await getMetadata ( program , user . publicKey ) ;
181
+ return await getMetadata ( program , publicKey ) ;
182
182
}
183
183
184
184
export async function deleteMetadata (
185
185
program : anchor . Program ,
186
- user : anchor . web3 . Keypair | Wallet ,
187
186
) : Promise < void > {
187
+ const wallet = program . provider . wallet ;
188
+ const publicKey = wallet . publicKey ;
188
189
const [ metadataAddress , metadataNonce ] = await getMetadataProgramAddress (
189
190
program ,
190
- user . publicKey ,
191
+ publicKey ,
191
192
) ;
192
193
await program . rpc . closeMetadata ( new anchor . BN ( metadataNonce ) , {
193
194
accounts : {
194
- user : user . publicKey ,
195
+ user : publicKey ,
195
196
metadata : metadataAddress ,
196
197
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
197
198
systemProgram : anchor . web3 . SystemProgram . programId ,
198
199
} ,
199
- signers : 'secretKey' in user ? [ user ] : [ ] ,
200
200
} ) ;
201
201
}
202
202
203
203
export async function subscribeUser (
204
204
program : anchor . Program ,
205
205
dialect : DialectAccount ,
206
206
user : PublicKey ,
207
- signer : Keypair ,
208
207
) : Promise < Metadata > {
209
- const [ publicKey , nonce ] = await getDialectProgramAddress (
208
+ const wallet = program . provider . wallet ;
209
+ const publicKey = wallet . publicKey ;
210
+ const [ dialectPublicKey , nonce ] = await getDialectProgramAddress (
210
211
program ,
211
212
dialect . dialect . members ,
212
213
) ;
@@ -219,14 +220,13 @@ export async function subscribeUser(
219
220
new anchor . BN ( metadataNonce ) ,
220
221
{
221
222
accounts : {
222
- dialect : publicKey ,
223
- signer : signer . publicKey ,
223
+ dialect : dialectPublicKey ,
224
+ signer : publicKey ,
224
225
user : user ,
225
226
metadata,
226
227
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
227
228
systemProgram : anchor . web3 . SystemProgram . programId ,
228
229
} ,
229
- signers : [ signer ] ,
230
230
} ,
231
231
) ;
232
232
await waitForFinality ( program , tx ) ;
@@ -254,9 +254,10 @@ export async function getDialectProgramAddress(
254
254
255
255
function parseMessages (
256
256
{ messages : rawMessagesBuffer , members, encrypted } : RawDialect ,
257
- user ?: anchor . web3 . Keypair ,
257
+ user ?: Wallet ,
258
258
) {
259
259
if ( encrypted && ! user ) {
260
+ // TODO: Raise encyprtion not supported
260
261
return [ ] ;
261
262
}
262
263
const messagesBuffer = new CyclicByteBuffer (
@@ -288,7 +289,7 @@ function parseMessages(
288
289
return allMessages . reverse ( ) ;
289
290
}
290
291
291
- function parseRawDialect ( rawDialect : RawDialect , user ?: anchor . web3 . Keypair ) {
292
+ function parseRawDialect ( rawDialect : RawDialect , user ?: Wallet ) {
292
293
return {
293
294
encrypted : rawDialect . encrypted ,
294
295
members : rawDialect . members ,
@@ -301,15 +302,15 @@ function parseRawDialect(rawDialect: RawDialect, user?: anchor.web3.Keypair) {
301
302
export async function getDialect (
302
303
program : anchor . Program ,
303
304
publicKey : PublicKey ,
304
- user ?: anchor . web3 . Keypair | Wallet ,
305
+ user ?: Wallet ,
305
306
) : Promise < DialectAccount > {
306
307
const rawDialect = ( await program . account . dialectAccount . fetch (
307
308
publicKey ,
308
309
) ) as RawDialect ;
309
310
const account = await program . provider . connection . getAccountInfo ( publicKey ) ;
310
311
const dialect = parseRawDialect (
311
312
rawDialect ,
312
- user && 'secretKey ' in user ? user : undefined ,
313
+ user && 'diffieHellman ' in user ? user : undefined ,
313
314
) ;
314
315
return {
315
316
...account ,
@@ -320,7 +321,7 @@ export async function getDialect(
320
321
321
322
export async function getDialects (
322
323
program : anchor . Program ,
323
- user : anchor . web3 . Keypair | Wallet ,
324
+ user : Wallet ,
324
325
) : Promise < DialectAccount [ ] > {
325
326
const metadata = await getMetadata ( program , user . publicKey ) ;
326
327
const enabledSubscriptions = metadata . subscriptions . filter (
@@ -341,7 +342,7 @@ export async function getDialects(
341
342
export async function getDialectForMembers (
342
343
program : anchor . Program ,
343
344
members : Member [ ] ,
344
- user ?: anchor . web3 . Keypair ,
345
+ user ?: Wallet ,
345
346
) : Promise < DialectAccount > {
346
347
const sortedMembers = members . sort ( ( a , b ) =>
347
348
a . publicKey . toBuffer ( ) . compare ( b . publicKey . toBuffer ( ) ) ,
@@ -393,10 +394,10 @@ export async function findDialects(
393
394
394
395
export async function createDialect (
395
396
program : anchor . Program ,
396
- owner : anchor . web3 . Keypair | Wallet ,
397
397
members : Member [ ] ,
398
398
encrypted = true ,
399
399
) : Promise < DialectAccount > {
400
+ const owner = program . provider . wallet ;
400
401
const sortedMembers = members . sort ( ( a , b ) =>
401
402
a . publicKey . toBuffer ( ) . compare ( b . publicKey . toBuffer ( ) ) ,
402
403
) ;
@@ -421,7 +422,6 @@ export async function createDialect(
421
422
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
422
423
systemProgram : anchor . web3 . SystemProgram . programId ,
423
424
} ,
424
- signers : 'secretKey' in owner ? [ owner ] : [ ] ,
425
425
} ,
426
426
) ;
427
427
await waitForFinality ( program , tx ) ;
@@ -435,20 +435,19 @@ export async function createDialect(
435
435
export async function deleteDialect (
436
436
program : anchor . Program ,
437
437
{ dialect } : DialectAccount ,
438
- owner : anchor . web3 . Keypair | Wallet ,
439
438
) : Promise < void > {
439
+ const wallet = program . provider . wallet ;
440
440
const [ dialectPublicKey , nonce ] = await getDialectProgramAddress (
441
441
program ,
442
442
dialect . members ,
443
443
) ;
444
444
await program . rpc . closeDialect ( new anchor . BN ( nonce ) , {
445
445
accounts : {
446
446
dialect : dialectPublicKey ,
447
- owner : owner . publicKey ,
447
+ owner : wallet . publicKey ,
448
448
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
449
449
systemProgram : anchor . web3 . SystemProgram . programId ,
450
450
} ,
451
- signers : 'secretKey' in owner ? [ owner ] : [ ] ,
452
451
} ) ;
453
452
}
454
453
@@ -468,9 +467,9 @@ Messages
468
467
export async function sendMessage (
469
468
program : anchor . Program ,
470
469
{ dialect, publicKey } : DialectAccount ,
471
- sender : anchor . web3 . Keypair | Wallet ,
472
470
text : string ,
473
471
) : Promise < Message > {
472
+ const wallet = program . provider . wallet ;
474
473
const [ dialectPublicKey , nonce ] = await getDialectProgramAddress (
475
474
program ,
476
475
dialect . members ,
@@ -480,7 +479,7 @@ export async function sendMessage(
480
479
encrypted : dialect . encrypted ,
481
480
members : dialect . members ,
482
481
} ,
483
- sender && 'secretKey ' in sender ? sender : undefined ,
482
+ wallet && 'diffieHellman ' in wallet ? wallet : undefined ,
484
483
) ;
485
484
const serializedText = textSerde . serialize ( text ) ;
486
485
await program . rpc . sendMessage (
@@ -489,16 +488,15 @@ export async function sendMessage(
489
488
{
490
489
accounts : {
491
490
dialect : dialectPublicKey ,
492
- sender : sender ? sender . publicKey : program . provider . wallet . publicKey ,
491
+ sender : wallet . publicKey ,
493
492
member0 : dialect . members [ 0 ] . publicKey ,
494
493
member1 : dialect . members [ 1 ] . publicKey ,
495
494
rent : anchor . web3 . SYSVAR_RENT_PUBKEY ,
496
495
systemProgram : anchor . web3 . SystemProgram . programId ,
497
496
} ,
498
- signers : sender && 'secretKey' in sender ? [ sender ] : [ ] ,
499
497
} ,
500
498
) ;
501
- const d = await getDialect ( program , publicKey , sender ) ;
499
+ const d = await getDialect ( program , publicKey , wallet ) ;
502
500
return d . dialect . messages [ d . dialect . nextMessageIdx - 1 ] ; // TODO: Support ring
503
501
}
504
502
0 commit comments