Skip to content

Commit c00a7a9

Browse files
committed
WIP. Removed all instances of keypair in api/index.ts, replaced with Wallet. Not finished in text-serde, or tests and examples.
1 parent 8f5352e commit c00a7a9

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

src/api/index.ts

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as anchor from '@project-serum/anchor';
22
import { EventParser } from '@project-serum/anchor';
33
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';
55

66
import { sleep, waitForFinality, Wallet_ } from '../utils';
77
import { ENCRYPTION_OVERHEAD_BYTES } from '../utils/ecdh-encryption';
@@ -118,37 +118,37 @@ export async function getMetadataProgramAddress(
118118
// TODO: Simplify this function further now that we're no longer decrypting the device token.
119119
export async function getMetadata(
120120
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,
123123
): Promise<Metadata> {
124124
let shouldDecrypt = false;
125-
let userIsKeypair = false;
126-
let otherPartyIsKeypair = false;
125+
let userIsWallet = false;
126+
let otherPartyIsWallet = false;
127127

128128
try {
129129
// assume user is pubkey
130130
new anchor.web3.PublicKey(user.toString());
131131
} catch {
132-
// user is keypair
133-
userIsKeypair = true;
132+
// user is wallet
133+
userIsWallet = true;
134134
}
135135

136136
try {
137137
// assume otherParty is pubkey
138138
new anchor.web3.PublicKey(otherParty?.toString() || '');
139139
} catch {
140-
// otherParty is keypair or null
141-
otherPartyIsKeypair = (otherParty && true) || false;
140+
// otherParty is wallet or null
141+
otherPartyIsWallet = (otherParty && true) || false;
142142
}
143143

144-
if (otherParty && (userIsKeypair || otherPartyIsKeypair)) {
144+
if (otherParty && (userIsWallet || otherPartyIsWallet)) {
145145
// cases 3 - 5
146146
shouldDecrypt = true;
147147
}
148148

149149
const [metadataAddress] = await getMetadataProgramAddress(
150150
program,
151-
userIsKeypair ? (user as Keypair).publicKey : (user as PublicKey),
151+
userIsWallet ? (user as Wallet).publicKey : (user as PublicKey),
152152
);
153153
const metadata = await program.account.metadataAccount.fetch(metadataAddress);
154154

@@ -162,51 +162,52 @@ export async function getMetadata(
162162

163163
export async function createMetadata(
164164
program: anchor.Program,
165-
user: anchor.web3.Keypair | Wallet,
166165
): Promise<Metadata> {
166+
const wallet = program.provider.wallet;
167+
const publicKey = wallet.publicKey;
167168
const [metadataAddress, metadataNonce] = await getMetadataProgramAddress(
168169
program,
169-
user.publicKey,
170+
publicKey,
170171
);
171172
const tx = await program.rpc.createMetadata(new anchor.BN(metadataNonce), {
172173
accounts: {
173-
user: user.publicKey,
174+
user: publicKey,
174175
metadata: metadataAddress,
175176
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
176177
systemProgram: anchor.web3.SystemProgram.programId,
177178
},
178-
signers: 'secretKey' in user ? [user] : [],
179179
});
180180
await waitForFinality(program, tx);
181-
return await getMetadata(program, user.publicKey);
181+
return await getMetadata(program, publicKey);
182182
}
183183

184184
export async function deleteMetadata(
185185
program: anchor.Program,
186-
user: anchor.web3.Keypair | Wallet,
187186
): Promise<void> {
187+
const wallet = program.provider.wallet;
188+
const publicKey = wallet.publicKey;
188189
const [metadataAddress, metadataNonce] = await getMetadataProgramAddress(
189190
program,
190-
user.publicKey,
191+
publicKey,
191192
);
192193
await program.rpc.closeMetadata(new anchor.BN(metadataNonce), {
193194
accounts: {
194-
user: user.publicKey,
195+
user: publicKey,
195196
metadata: metadataAddress,
196197
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
197198
systemProgram: anchor.web3.SystemProgram.programId,
198199
},
199-
signers: 'secretKey' in user ? [user] : [],
200200
});
201201
}
202202

203203
export async function subscribeUser(
204204
program: anchor.Program,
205205
dialect: DialectAccount,
206206
user: PublicKey,
207-
signer: Keypair,
208207
): 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(
210211
program,
211212
dialect.dialect.members,
212213
);
@@ -219,14 +220,13 @@ export async function subscribeUser(
219220
new anchor.BN(metadataNonce),
220221
{
221222
accounts: {
222-
dialect: publicKey,
223-
signer: signer.publicKey,
223+
dialect: dialectPublicKey,
224+
signer: publicKey,
224225
user: user,
225226
metadata,
226227
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
227228
systemProgram: anchor.web3.SystemProgram.programId,
228229
},
229-
signers: [signer],
230230
},
231231
);
232232
await waitForFinality(program, tx);
@@ -254,9 +254,10 @@ export async function getDialectProgramAddress(
254254

255255
function parseMessages(
256256
{ messages: rawMessagesBuffer, members, encrypted }: RawDialect,
257-
user?: anchor.web3.Keypair,
257+
user?: Wallet,
258258
) {
259259
if (encrypted && !user) {
260+
// TODO: Raise encyprtion not supported
260261
return [];
261262
}
262263
const messagesBuffer = new CyclicByteBuffer(
@@ -288,7 +289,7 @@ function parseMessages(
288289
return allMessages.reverse();
289290
}
290291

291-
function parseRawDialect(rawDialect: RawDialect, user?: anchor.web3.Keypair) {
292+
function parseRawDialect(rawDialect: RawDialect, user?: Wallet) {
292293
return {
293294
encrypted: rawDialect.encrypted,
294295
members: rawDialect.members,
@@ -301,15 +302,15 @@ function parseRawDialect(rawDialect: RawDialect, user?: anchor.web3.Keypair) {
301302
export async function getDialect(
302303
program: anchor.Program,
303304
publicKey: PublicKey,
304-
user?: anchor.web3.Keypair | Wallet,
305+
user?: Wallet,
305306
): Promise<DialectAccount> {
306307
const rawDialect = (await program.account.dialectAccount.fetch(
307308
publicKey,
308309
)) as RawDialect;
309310
const account = await program.provider.connection.getAccountInfo(publicKey);
310311
const dialect = parseRawDialect(
311312
rawDialect,
312-
user && 'secretKey' in user ? user : undefined,
313+
user && 'diffieHellman' in user ? user : undefined,
313314
);
314315
return {
315316
...account,
@@ -320,7 +321,7 @@ export async function getDialect(
320321

321322
export async function getDialects(
322323
program: anchor.Program,
323-
user: anchor.web3.Keypair | Wallet,
324+
user: Wallet,
324325
): Promise<DialectAccount[]> {
325326
const metadata = await getMetadata(program, user.publicKey);
326327
const enabledSubscriptions = metadata.subscriptions.filter(
@@ -341,7 +342,7 @@ export async function getDialects(
341342
export async function getDialectForMembers(
342343
program: anchor.Program,
343344
members: Member[],
344-
user?: anchor.web3.Keypair,
345+
user?: Wallet,
345346
): Promise<DialectAccount> {
346347
const sortedMembers = members.sort((a, b) =>
347348
a.publicKey.toBuffer().compare(b.publicKey.toBuffer()),
@@ -393,10 +394,10 @@ export async function findDialects(
393394

394395
export async function createDialect(
395396
program: anchor.Program,
396-
owner: anchor.web3.Keypair | Wallet,
397397
members: Member[],
398398
encrypted = true,
399399
): Promise<DialectAccount> {
400+
const owner = program.provider.wallet;
400401
const sortedMembers = members.sort((a, b) =>
401402
a.publicKey.toBuffer().compare(b.publicKey.toBuffer()),
402403
);
@@ -421,7 +422,6 @@ export async function createDialect(
421422
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
422423
systemProgram: anchor.web3.SystemProgram.programId,
423424
},
424-
signers: 'secretKey' in owner ? [owner] : [],
425425
},
426426
);
427427
await waitForFinality(program, tx);
@@ -435,20 +435,19 @@ export async function createDialect(
435435
export async function deleteDialect(
436436
program: anchor.Program,
437437
{ dialect }: DialectAccount,
438-
owner: anchor.web3.Keypair | Wallet,
439438
): Promise<void> {
439+
const wallet = program.provider.wallet;
440440
const [dialectPublicKey, nonce] = await getDialectProgramAddress(
441441
program,
442442
dialect.members,
443443
);
444444
await program.rpc.closeDialect(new anchor.BN(nonce), {
445445
accounts: {
446446
dialect: dialectPublicKey,
447-
owner: owner.publicKey,
447+
owner: wallet.publicKey,
448448
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
449449
systemProgram: anchor.web3.SystemProgram.programId,
450450
},
451-
signers: 'secretKey' in owner ? [owner] : [],
452451
});
453452
}
454453

@@ -468,9 +467,9 @@ Messages
468467
export async function sendMessage(
469468
program: anchor.Program,
470469
{ dialect, publicKey }: DialectAccount,
471-
sender: anchor.web3.Keypair | Wallet,
472470
text: string,
473471
): Promise<Message> {
472+
const wallet = program.provider.wallet;
474473
const [dialectPublicKey, nonce] = await getDialectProgramAddress(
475474
program,
476475
dialect.members,
@@ -480,7 +479,7 @@ export async function sendMessage(
480479
encrypted: dialect.encrypted,
481480
members: dialect.members,
482481
},
483-
sender && 'secretKey' in sender ? sender : undefined,
482+
wallet && 'diffieHellman' in wallet ? wallet : undefined,
484483
);
485484
const serializedText = textSerde.serialize(text);
486485
await program.rpc.sendMessage(
@@ -489,16 +488,15 @@ export async function sendMessage(
489488
{
490489
accounts: {
491490
dialect: dialectPublicKey,
492-
sender: sender ? sender.publicKey : program.provider.wallet.publicKey,
491+
sender: wallet.publicKey,
493492
member0: dialect.members[0].publicKey,
494493
member1: dialect.members[1].publicKey,
495494
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
496495
systemProgram: anchor.web3.SystemProgram.programId,
497496
},
498-
signers: sender && 'secretKey' in sender ? [sender] : [],
499497
},
500498
);
501-
const d = await getDialect(program, publicKey, sender);
499+
const d = await getDialect(program, publicKey, wallet);
502500
return d.dialect.messages[d.dialect.nextMessageIdx - 1]; // TODO: Support ring
503501
}
504502

src/api/text-serde.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '../utils/nonce-generator';
66
import { ecdhDecrypt, ecdhEncrypt } from '../utils/ecdh-encryption';
77
import * as anchor from '@project-serum/anchor';
8+
import { Wallet } from '@project-serum/anchor/src/provider';
89

910
export interface TextSerde {
1011
serialize(text: string): Uint8Array;
@@ -91,7 +92,7 @@ export type DialectAttributes = {
9192
export class TextSerdeFactory {
9293
static create(
9394
{ encrypted, members }: DialectAttributes,
94-
user?: anchor.web3.Keypair,
95+
user?: Wallet,
9596
): TextSerde {
9697
if (!encrypted) {
9798
return new UnencryptedTextSerde();

0 commit comments

Comments
 (0)