-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenAccessManager.js
249 lines (212 loc) · 7.58 KB
/
tokenAccessManager.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
/**
* Social Token Platform - Token Access Manager
*
* Core functionality for the friend.tech-like platform that manages
* access keys, private chats, and tokenized social interactions.
*/
const { ethers } = require('ethers');
const { WebSocketServer } = require('ws');
class TokenAccessManager {
constructor(config = {}) {
this.provider = new ethers.providers.JsonRpcProvider(config.rpcUrl);
this.contractAddress = config.contractAddress;
this.abi = require('./contracts/SocialKeyABI.json');
this.contract = new ethers.Contract(this.contractAddress, this.abi, this.provider);
this.chatServer = new WebSocketServer({ port: config.wsPort || 8080 });
this.userSessions = new Map();
this.chatRooms = new Map();
// Initialize WebSocket server for private chats
this.initChatServer();
}
// Get key price based on bonding curve
async getKeyPrice(creatorAddress, action) {
try {
const keySupply = await this.contract.getKeySupply(creatorAddress);
return action === 'buy'
? await this.contract.getBuyPrice(creatorAddress, 1)
: await this.contract.getSellPrice(creatorAddress, 1);
} catch (error) {
console.error('Error getting key price:', error);
throw new Error(`Failed to get key price: ${error.message}`);
}
}
// Buy keys for a creator
async buyKeys(buyerWallet, creatorAddress, amount = 1) {
try {
const keyPrice = await this.contract.getBuyPrice(creatorAddress, amount);
const fee = keyPrice.mul(ethers.BigNumber.from(10)).div(ethers.BigNumber.from(100)); // 10% fee
const totalCost = keyPrice.add(fee);
const signer = buyerWallet.connect(this.provider);
const contractWithSigner = this.contract.connect(signer);
const tx = await contractWithSigner.buyKeys(creatorAddress, amount, {
value: totalCost
});
return {
success: true,
transaction: tx.hash,
keyPrice: ethers.utils.formatEther(keyPrice),
fee: ethers.utils.formatEther(fee),
totalCost: ethers.utils.formatEther(totalCost)
};
} catch (error) {
console.error('Error buying keys:', error);
return { success: false, error: error.message };
}
}
// Sell keys for a creator
async sellKeys(sellerWallet, creatorAddress, amount = 1) {
try {
const signer = sellerWallet.connect(this.provider);
const contractWithSigner = this.contract.connect(signer);
const tx = await contractWithSigner.sellKeys(creatorAddress, amount);
const keyPrice = await this.contract.getSellPrice(creatorAddress, amount);
return {
success: true,
transaction: tx.hash,
salePrice: ethers.utils.formatEther(keyPrice)
};
} catch (error) {
console.error('Error selling keys:', error);
return { success: false, error: error.message };
}
}
// Check if a user has access to a creator's exclusive content
async checkAccess(userAddress, creatorAddress) {
try {
const balance = await this.contract.keysBalance(userAddress, creatorAddress);
return {
hasAccess: balance.gt(0),
keyCount: balance.toNumber()
};
} catch (error) {
console.error('Error checking access:', error);
return { hasAccess: false, error: error.message };
}
}
// Initialize WebSocket server for private chats
initChatServer() {
this.chatServer.on('connection', (ws) => {
let userId = null;
let accessibleRooms = [];
ws.on('message', async (message) => {
try {
const data = JSON.parse(message);
switch (data.type) {
case 'auth':
// Authenticate user and get their accessible rooms
userId = await this.authenticateUser(data.signature, data.message);
accessibleRooms = await this.getUserAccessibleRooms(userId);
ws.send(JSON.stringify({
type: 'auth_result',
success: !!userId,
accessibleRooms
}));
break;
case 'join_room':
// Join a creator's private chat room
if (!userId) {
ws.send(JSON.stringify({ type: 'error', message: 'Not authenticated' }));
return;
}
const hasAccess = await this.checkAccess(userId, data.creatorAddress);
if (!hasAccess.hasAccess) {
ws.send(JSON.stringify({
type: 'error',
message: 'You do not have access to this room. Purchase keys first.'
}));
return;
}
this.addUserToRoom(ws, userId, data.creatorAddress);
break;
case 'send_message':
// Send a message to a room
if (!userId) {
ws.send(JSON.stringify({ type: 'error', message: 'Not authenticated' }));
return;
}
this.broadcastToRoom(data.roomId, {
type: 'new_message',
sender: userId,
content: data.content,
timestamp: Date.now()
}, ws);
break;
}
} catch (error) {
console.error('WebSocket message error:', error);
ws.send(JSON.stringify({ type: 'error', message: 'Invalid message format' }));
}
});
ws.on('close', () => {
if (userId) {
this.removeUserFromRooms(ws, userId);
}
});
});
}
// Private methods
async authenticateUser(signature, message) {
try {
const recoveredAddress = ethers.utils.verifyMessage(message, signature);
return recoveredAddress;
} catch (error) {
console.error('Authentication error:', error);
return null;
}
}
async getUserAccessibleRooms(userAddress) {
try {
// Would typically query the blockchain for all creators the user holds keys for
const filter = this.contract.filters.KeyPurchase(null, userAddress, null);
const events = await this.contract.queryFilter(filter);
// Deduplicate creator addresses
const creatorSet = new Set();
events.forEach(event => creatorSet.add(event.args.subject));
return Array.from(creatorSet);
} catch (error) {
console.error('Error getting accessible rooms:', error);
return [];
}
}
addUserToRoom(ws, userId, roomId) {
if (!this.chatRooms.has(roomId)) {
this.chatRooms.set(roomId, new Set());
}
const room = this.chatRooms.get(roomId);
room.add(ws);
this.userSessions.set(ws, {
userId,
rooms: new Set([roomId])
});
ws.send(JSON.stringify({
type: 'room_joined',
roomId
}));
}
removeUserFromRooms(ws, userId) {
const session = this.userSessions.get(ws);
if (!session) return;
for (const roomId of session.rooms) {
const room = this.chatRooms.get(roomId);
if (room) {
room.delete(ws);
// Clean up empty rooms
if (room.size === 0) {
this.chatRooms.delete(roomId);
}
}
}
this.userSessions.delete(ws);
}
broadcastToRoom(roomId, message, excludeWs = null) {
const room = this.chatRooms.get(roomId);
if (!room) return;
const messageStr = JSON.stringify(message);
for (const client of room) {
if (client !== excludeWs && client.readyState === 1) {
client.send(messageStr);
}
}
}
}
module.exports = TokenAccessManager;