Skip to content

Commit 471b117

Browse files
committed
WIP
- console debugs shall be removed again
1 parent 3538065 commit 471b117

File tree

10 files changed

+170
-106
lines changed

10 files changed

+170
-106
lines changed

packages/js-sdk/src/Dm3Sdk.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getIdForAddress(address: string, addrEnsSubdomain: string) {
3030

3131
export interface Dm3SdkConfig {
3232
mainnetProvider: ethers.providers.JsonRpcProvider;
33-
storageApi: StorageAPI;
33+
storageApi?: StorageAPI;
3434
nonce: string;
3535
defaultDeliveryService: string;
3636
addressEnsSubdomain: string;
@@ -63,7 +63,7 @@ export class Dm3Sdk {
6363
/**
6464
* DM3 STORAGE
6565
*/
66-
private storageApi: StorageAPI;
66+
private storageApi?: StorageAPI;
6767

6868
/**
6969
* DM3 CONVERSATIONS
@@ -85,8 +85,8 @@ export class Dm3Sdk {
8585
this.userEnsSubdomain = config.userEnsSubdomain;
8686
this.resolverBackendUrl = config.resolverBackendUrl;
8787
this.backendUrl = config.backendUrl;
88-
this.storageApi = config.storageApi;
8988
this._tld = config._tld;
89+
this.storageApi = config.storageApi;
9090
}
9191
/**
9292
* login can be used to login with a profile regardles the connector. Its also great for testing
@@ -132,6 +132,14 @@ export class Dm3Sdk {
132132

133133
await beConnector.login(profile);
134134

135+
this.storageApi = this.storageApi ?? new EncryptedCloudStorage(
136+
beConnector,
137+
account,
138+
this.profileKeys,
139+
).getCloudStorage();
140+
141+
console.log('this.storageApi', this.storageApi);
142+
135143
const conversations = new Conversations(
136144
this.storageApi,
137145
tld,
@@ -141,12 +149,6 @@ export class Dm3Sdk {
141149
this.addressEnsSubdomain,
142150
);
143151

144-
this.storageApi = new EncryptedCloudStorage(
145-
beConnector,
146-
account,
147-
this.profileKeys,
148-
).getCloudStorage();
149-
150152
return new Dm3(conversations, tld);
151153
}
152154

@@ -160,8 +162,12 @@ export class Dm3Sdk {
160162
this.nonce,
161163
this.defaultDeliveryService,
162164
);
165+
166+
console.log('lc', lc, typeof lc.login);
163167
const loginResult = await lc.login();
164168

169+
console.log('loginResult', loginResult);
170+
165171
const { profileKeys, profile, accountAddress } = loginResult as Success;
166172
return await this.login({ profileKeys, profile, accountAddress });
167173
}

packages/js-sdk/src/connectors/LuksoConnector.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,49 @@ import { ethers } from 'ethers';
33
import ERC725Abi from './ERC725Abi.json';
44
import { SmartAccountConnector } from './SmartAccountConnector';
55

6-
declare global {
7-
interface Window {
8-
lukso?: any;
9-
}
10-
}
116
export class LuksoConnector {
127
//TODO move to class tailored to lukso
138
public static async _instance(
149
lukso: ethers.providers.ExternalProvider,
1510
nonce: string,
1611
defaultDeliveryService: string,
1712
): Promise<SmartAccountConnector> {
18-
//The universal profile extension can be accessed via the window.lukso object
19-
// if (!window.lukso) {
20-
// throw 'Universal Profile extension not found';
21-
// }
2213
const provider = new ethers.providers.Web3Provider(lukso);
2314
//Connect with the UP extension
2415
console.log('done0');
2516
await provider.send('eth_requestAccounts', []);
2617
console.log('done1');
2718

2819
//The signer that will be used to sign transactions
29-
const upController = await provider.getSigner();
20+
const upController = provider.getSigner();
3021
console.log('done2');
3122
//When used with UP the signer.getAddress() will return the UP address. Even though the signer uses the controller address to sign transactions
3223
//TODO clearify with Lukso-Team if that is always the case
33-
const upAddress = upController._address;
24+
const upAddress = await upController.getAddress();
3425
console.log(upController);
35-
console.log('done3 ', upAddress);
26+
console.log('done3 .', upAddress);
3627

37-
//Instance of the UP contract
28+
29+
//Instance of the UP contract
3830
const upContract = new ethers.Contract(
3931
upAddress,
4032
ERC725Abi,
4133
upController,
4234
);
35+
console.log('done4', upContract);
4336
const keyStore = new Lukso.LuksoKeyStore(upContract);
37+
console.log('done5', keyStore);
38+
4439

45-
return new SmartAccountConnector(
40+
const sc = new SmartAccountConnector(
4641
keyStore,
4742
upController,
4843
nonce,
4944
defaultDeliveryService,
5045
);
46+
47+
console.log('done6', sc);
48+
49+
return sc;
5150
}
5251
}

packages/js-sdk/src/connectors/SmartAccountConnector.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,30 +142,36 @@ export class SmartAccountConnector {
142142

143143
public async login(): Promise<LoginResult> {
144144
const keyStore = await this.keyStoreService.readDm3KeyStore();
145+
console.log('keyStore', keyStore);
145146
//Smart account has never used dm3 before
146147
if (Object.keys(keyStore).length === 0) {
148+
console.log('signUp');
147149
return await this.signUp();
148150
}
149151
//Smart account has used Dm before. We have to check if the controller has published its public key yet
150-
const { encryptionKeyPair, signature } =
151-
await this.createEncryptionKeys();
152+
console.debug('Starting login process for existing smart account.');
153+
const { encryptionKeyPair, signature } = await this.createEncryptionKeys();
154+
console.debug('Encryption keys created:', encryptionKeyPair, 'Signature:', signature);
152155

153156
//Recover the address of the controller from the signature
154-
const upControllerAddress =
155-
await this.recoverAddressFromEncryptionKeySignature(signature);
157+
const upControllerAddress = await this.recoverAddressFromEncryptionKeySignature(signature);
158+
console.debug('Recovered UP controller address from signature:', upControllerAddress);
156159

157-
//Check if the controller has already a ketStore entry
160+
//Check if the controller has already a keyStore entry
158161
const encryptedControllerKeyStore = keyStore[upControllerAddress];
162+
console.debug('Retrieved encrypted controller key store:', encryptedControllerKeyStore);
159163

160164
//If the controller already has a keyStore, we can decrypt the profileKeys using its encryptionKeys
161165
//If not we've to start the keyExchange process
162166
if (
163167
!encryptedControllerKeyStore ||
164168
!encryptedControllerKeyStore.encryptedProfileKeys
165169
) {
166-
//The signer connected to the UP has not used dm3 before, it has to publish its public key so another device can share the profile keys
170+
console.debug('No existing key store or encrypted profile keys found. Initiating key exchange process.');
171+
//The signer connected to the UP has not used dm3 before, it has to publish its public key so another device can share the profile keys
167172
return await this.addNewSigner(keyStore);
168173
}
174+
console.debug('Existing key store and encrypted profile keys found. Proceeding with sign-in for existing signer.');
169175
//The signer connected to the UP has already used dm3 before, hence it knows the profile
170176
return await this.signInExistingSigner(
171177
encryptionKeyPair,
@@ -222,15 +228,22 @@ export class SmartAccountConnector {
222228

223229
//Returns Keys to encrypt the actual profile at UP
224230
private async createEncryptionKeys() {
225-
//If the user has created its encryption keys before, we can reuse them. That way we don't have to ask the user to sign again
231+
// If the user has created its encryption keys before, we can reuse them. That way we don't have to ask the user to sign again
232+
console.debug('Checking for cached encryption keys.');
226233
if (this.cachedEncryptionKeys) {
234+
console.debug('Cached encryption keys found, returning them.');
227235
return this.cachedEncryptionKeys;
228236
}
237+
238+
console.debug('No cached encryption keys found, proceeding to create new ones.');
229239
const upAddress = await this.keyStoreService.getAccountAddress();
240+
console.debug('Retrieved account address:', upAddress);
241+
230242
const statement =
231243
`Connect the DM3 MESSENGER with your wallet. ` +
232244
`Keys for secure communication are derived from this signature.` +
233245
`(There is no paid transaction initiated. The signature is used off-chain only.)`;
246+
console.debug('Prepared statement for SiweMessage:', statement);
234247

235248
const message = new SiweMessage({
236249
domain: 'dm3.chat',
@@ -240,23 +253,31 @@ export class SmartAccountConnector {
240253
version: '1',
241254
chainId: 42,
242255
nonce: this.nonce,
243-
//Date is a mandatory property otherwise it'll be DAte.now(). We need it to be constant to create teh encryption keys deterministically
256+
// Date is a mandatory property otherwise it'll be Date.now(). We need it to be constant to create the encryption keys deterministically
244257
issuedAt: new Date(978307200000).toISOString(),
245258
resources: ['https://dm3.network'],
246259
});
260+
console.debug('Created SiweMessage:', message);
261+
console.debug('this.controller:', this.controller);
262+
console.debug('message.prepareMessage():', message.prepareMessage());
247263

248264
const signature = await this.controller.signMessage(
249265
message.prepareMessage(),
250266
);
267+
console.debug('Generated signature:', signature);
268+
251269
const storageKey = await createStorageKey(signature);
270+
console.debug('Created storage key:', storageKey);
252271

253272
const keys = await _createProfileKeys(storageKey, this.nonce);
273+
console.debug('Generated profile keys:', keys);
254274

255-
//Keep the encryptionKeyPair for later use
275+
// Keep the encryptionKeyPair for later use
256276
this.cachedEncryptionKeys = {
257277
encryptionKeyPair: keys.encryptionKeyPair,
258278
signature: signature,
259279
};
280+
console.debug('Cached encryption keys for future use.');
260281

261282
return {
262283
encryptionKeyPair: keys.encryptionKeyPair,

packages/js-sdk/src/message/Messages.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export class Messages {
4444
this._messages = [];
4545
}
4646

47+
get meta() {
48+
return {
49+
sender: this.senderAccount,
50+
receiver: this.receiver,
51+
messages: this._messages,
52+
}
53+
}
54+
4755
public get list() {
4856
return renderMessage(this._messages);
4957
}

packages/js-sdk/src/storage/EncryptedCloudStorage.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ export class EncryptedCloudStorage {
1818
account: Account,
1919
profileKeys: ProfileKeys,
2020
) {
21+
if(!account.ensName) {
22+
throw new Error('Account must have an ENS name');
23+
}
24+
2125
this.backendConnector = backendConnector;
2226
this.account = account;
2327
this.profileKeys = profileKeys;
2428
}
2529

2630
private async encryptSync(data: string) {
27-
const accountNonce = sha256(this.account!.ensName).slice(0, 26);
31+
console.log('this.account', this.account);
32+
console.log('this.account.ensName', this.account.ensName);
33+
34+
const accountNonce = sha256(this.account.ensName).slice(0, 26);
2835
const encryptedPayload: EncryptedPayload = await _encrypt(
2936
this.profileKeys?.encryptionKeyPair?.privateKey!,
3037
data,
@@ -63,7 +70,7 @@ export class EncryptedCloudStorage {
6370
}
6471

6572
public getCloudStorage() {
66-
return getCloudStorage(this.backendConnector, this.account!.ensName, {
73+
return getCloudStorage(this.backendConnector, this.account.ensName, {
6774
encryptAsync: this.encryptAsync,
6875
decryptAsync: this.decryptAsync,
6976
encryptSync: this.encryptSync,

packages/messenger-vue-demo/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import Chat from './components/Chat.vue'
2+
import Chat from './components/Dm3Chat.vue'
33
</script>
44

55
<template>

packages/messenger-vue-demo/src/components/Chat.vue renamed to packages/messenger-vue-demo/src/components/Dm3Chat.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<template>
2-
<vue-advanced-chat
2+
{{ rooms }}
3+
{{ messages }}
4+
<!-- <vue-advanced-chat
35
:current-user-id="currentUserId"
46
:rooms="JSON.stringify(rooms)"
57
:messages="JSON.stringify(messages)"
68
:room-actions="JSON.stringify(roomActions)"
79
:messages-loaded="messagesLoaded"
810
:rooms-loaded="roomsLoaded"
9-
/>
11+
/> -->
12+
<button @click="startTestConversation">Start test conversation</button>
13+
isReady: {{ isReady }}
1014
</template>
1115

1216
<script setup>
@@ -18,9 +22,11 @@ register()
1822
const roomsLoaded = ref(false);
1923
const messagesLoaded = ref(false);
2024
21-
const { rooms, messages, init } = useDm3Chat();
25+
const { rooms, messages, init, startTestConversation, isReady } = useDm3Chat();
2226
23-
init();
27+
onMounted(() => {
28+
init();
29+
})
2430
2531
const currentUserId = '1234'
2632
// const rooms = [

0 commit comments

Comments
 (0)