This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
message-feed.js
270 lines (244 loc) · 6.54 KB
/
message-feed.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
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
/* @flow */
import {
Account,
Connection,
SystemProgram,
PublicKey,
Transaction,
sendAndConfirmTransaction,
} from '@solana/web3.js';
import * as BufferLayout from 'buffer-layout';
import type {TransactionSignature} from '@solana/web3.js';
import {publicKeyToName} from '../util/publickey-to-name';
export type Message = {
publicKey: PublicKey,
from: PublicKey,
name: string,
text: string,
};
type MessageData = {
nextMessage: PublicKey,
from: PublicKey,
programId: PublicKey,
text: string,
};
const publicKeyLayout = (property: string = 'publicKey'): Object => {
return BufferLayout.blob(32, property);
};
export const userAccountSize = 1 + 32; // 32 = size of a public key
export function messageAccountSize(text: string): number {
const textBuffer = Buffer.from(text);
return 32 + 32 + 32 + textBuffer.length; // 32 = size of a public key
}
async function createUserAccount(
connection: Connection,
programId: PublicKey,
payerAccount: Account,
messageAccount: Account,
transaction: Transaction,
): Promise<Account> {
const userAccount = new Account();
// Allocate the user account
transaction.add(
SystemProgram.createAccount({
fromPubkey: payerAccount.publicKey,
newAccountPubkey: userAccount.publicKey,
lamports: await connection.getMinimumBalanceForRentExemption(
userAccountSize,
),
space: userAccountSize,
programId,
}),
);
// Initialize the user account
const keys = [
{pubkey: userAccount.publicKey, isSigner: true, isWritable: false},
{pubkey: messageAccount.publicKey, isSigner: true, isWritable: false},
];
transaction.add({
keys,
programId,
});
return userAccount;
}
export async function createUser(
connection: Connection,
programId: PublicKey,
payerAccount: Account,
messageAccount: Account,
): Promise<Account> {
const transaction = new Transaction();
const userAccount = await createUserAccount(
connection,
programId,
payerAccount,
messageAccount,
transaction,
);
await sendAndConfirmTransaction(
connection,
transaction,
payerAccount,
userAccount,
messageAccount,
);
return userAccount;
}
/**
* Checks if a user has been banned
*/
export async function userBanned(
connection: Connection,
user: PublicKey,
): Promise<boolean> {
const accountInfo = await connection.getAccountInfo(user);
const userAccountDataLayout = BufferLayout.struct([
BufferLayout.u8('banned'),
publicKeyLayout('creator'),
]);
const userAccountData = userAccountDataLayout.decode(accountInfo.data);
return userAccountData.banned !== 0;
}
/**
* Read the contents of a message
*/
async function readMessage(
connection: Connection,
message: PublicKey,
): Promise<MessageData> {
const accountInfo = await connection.getAccountInfo(message);
const messageAccountDataLayout = BufferLayout.struct([
publicKeyLayout('nextMessage'),
publicKeyLayout('from'),
publicKeyLayout('creator'),
BufferLayout.cstr('text'),
]);
const messageAccountData = messageAccountDataLayout.decode(accountInfo.data);
return {
nextMessage: new PublicKey(messageAccountData.nextMessage),
from: new PublicKey(messageAccountData.from),
programId: accountInfo.owner,
text: messageAccountData.text,
};
}
/**
* Checks a message feed for new messages and loads them into the provided
* messages array.
*/
export async function refreshMessageFeed(
connection: Connection,
messages: Array<Message>,
onNewMessage: Function | null,
message: PublicKey | null = null,
): Promise<void> {
const emptyMessage = new PublicKey(0);
for (;;) {
if (message === null) {
if (messages.length === 0) {
return;
}
const lastMessage = messages[messages.length - 1].publicKey;
const lastMessageData = await readMessage(connection, lastMessage);
message = lastMessageData.nextMessage;
}
if (message.equals(emptyMessage)) {
return;
}
console.log(`Loading message ${message}`);
const messageData = await readMessage(connection, message);
messages.push({
publicKey: message,
from: messageData.from,
name: publicKeyToName(messageData.from),
text: messageData.text,
});
onNewMessage && onNewMessage();
message = messageData.nextMessage;
}
}
/**
* Posts a new message
*/
export async function postMessage(
connection: Connection,
payerAccount: Account,
userAccount: Account,
text: string,
previousMessage: PublicKey,
userToBan: PublicKey | null = null,
): Promise<TransactionSignature> {
const messageData = await readMessage(connection, previousMessage);
const messageAccount = new Account();
return postMessageWithProgramId(
connection,
messageData.programId,
payerAccount,
userAccount,
messageAccount,
text,
previousMessage,
userToBan,
);
}
export async function postMessageWithProgramId(
connection: Connection,
programId: PublicKey,
payerAccount: Account,
userAccountArg: Account | null,
messageAccount: Account,
text: string,
previousMessagePublicKey: PublicKey | null = null,
userToBan: PublicKey | null = null,
): Promise<TransactionSignature> {
const transaction = new Transaction();
const dataSize = messageAccountSize(text);
const textBuffer = Buffer.from(text);
// Allocate the message account
transaction.add(
SystemProgram.createAccount({
fromPubkey: payerAccount.publicKey,
newAccountPubkey: messageAccount.publicKey,
lamports: await connection.getMinimumBalanceForRentExemption(dataSize),
space: dataSize,
programId,
}),
);
let userAccount = userAccountArg;
if (userAccount === null) {
userAccount = await createUserAccount(
connection,
programId,
payerAccount,
messageAccount,
transaction,
);
}
// The second instruction in the transaction posts the message, optionally
// links it to the previous message and optionally bans another user
const keys = [
{pubkey: userAccount.publicKey, isSigner: true, isWritable: false},
{pubkey: messageAccount.publicKey, isSigner: true, isWritable: false},
];
if (previousMessagePublicKey) {
keys.push({
pubkey: previousMessagePublicKey,
isSigner: false,
isWritable: true,
});
if (userToBan) {
keys.push({pubkey: userToBan, isSigner: false, isWritable: true});
}
}
transaction.add({
keys,
programId,
data: textBuffer,
});
return await sendAndConfirmTransaction(
connection,
transaction,
payerAccount,
userAccount,
messageAccount,
);
}