Skip to content

Commit 95c1ebd

Browse files
committed
mobx: Updated addSessionView to support custodial accounts
1 parent f470c13 commit 95c1ebd

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

app/src/store/models/session.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export default class Session {
5959
return 'Admin';
6060
case LIT.SessionType.TYPE_MACAROON_CUSTOM:
6161
return 'Custom';
62+
case LIT.SessionType.TYPE_MACAROON_ACCOUNT:
63+
return 'Custodial';
6264
case LIT.SessionType.TYPE_UI_PASSWORD:
6365
return 'LiT UI Password';
6466
}
@@ -96,7 +98,7 @@ export default class Session {
9698

9799
/** The HEX encoded pairing secret mnemonic and mailbox server address */
98100
get encodedPairingData() {
99-
const data = `${this.pairingSecretMnemonic}||${this.mailboxServerAddr}`;
101+
const data = `${this.pairingSecretMnemonic}||${this.mailboxServerAddr}||${this.typeLabel}`;
100102
return Buffer.from(data, 'ascii').toString('base64');
101103
}
102104

app/src/store/stores/sessionStore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export default class SessionStore {
104104
copy = false,
105105
proxy?: string,
106106
customPermissions?: LIT.MacaroonPermission[],
107+
accountId?: string,
107108
) {
108109
try {
109110
this._store.log.info(`submitting session with label ${label}`, {
@@ -119,6 +120,7 @@ export default class SessionStore {
119120
proxy || this.proxyServer,
120121
!IS_PROD,
121122
customPermissions || [],
123+
accountId || '',
122124
);
123125

124126
// fetch all sessions to update the store's state

app/src/store/views/addSessionView.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class AddSessionView {
77
private _store: Store;
88

99
label = '';
10-
permissionType = 'admin'; // Expected values: admin | read-only | custom | liquidity | payments
10+
permissionType = 'admin'; // Expected values: admin | read-only | custodial | custom | liquidity | payments
1111
editing = false;
1212
permissions: { [key: string]: boolean } = {
1313
openChannel: false,
@@ -30,6 +30,7 @@ export default class AddSessionView {
3030
expirationDate = '';
3131
showAdvanced = false;
3232
proxy = '';
33+
custodialBalance = 0;
3334

3435
constructor(store: Store) {
3536
makeAutoObservable(
@@ -52,6 +53,8 @@ export default class AddSessionView {
5253
return LIT.SessionType.TYPE_MACAROON_ADMIN;
5354
} else if (this.permissionType === 'read-only') {
5455
return LIT.SessionType.TYPE_MACAROON_READONLY;
56+
} else if (this.permissionType === 'custodial') {
57+
return LIT.SessionType.TYPE_MACAROON_ACCOUNT;
5558
}
5659

5760
return LIT.SessionType.TYPE_MACAROON_CUSTOM;
@@ -125,6 +128,10 @@ export default class AddSessionView {
125128
this.proxy = proxy;
126129
}
127130

131+
setCustodialBalance(balance: number) {
132+
this.custodialBalance = balance;
133+
}
134+
128135
setPermissionType(permissionType: string) {
129136
this.permissionType = permissionType;
130137

@@ -150,6 +157,12 @@ export default class AddSessionView {
150157
this.permissions.receive = true;
151158
break;
152159

160+
case 'custodial':
161+
this.setAllPermissions(false);
162+
this.permissions.send = true;
163+
this.permissions.receive = true;
164+
break;
165+
153166
case 'custom':
154167
// We don't need to change anything, let the user customize permissions how they want
155168
break;
@@ -203,19 +216,32 @@ export default class AddSessionView {
203216

204217
async handleCustomSubmit() {
205218
let label = this.label;
219+
let accountId = '';
206220

207221
// Automatically generate human friendly labels for custom sessions
208222
if (label === '') {
209223
label = `My ${this.permissionType} session`;
210224
}
211225

226+
if (this.permissionType === 'custodial') {
227+
const custodialAccountId = await this.registerCustodialAccount();
228+
229+
// Return immediately to prevent a session being created when there is an error creating the custodial account
230+
if (!custodialAccountId) {
231+
return;
232+
}
233+
234+
accountId = custodialAccountId;
235+
}
236+
212237
const session = await this._store.sessionStore.addSession(
213238
label,
214239
this.sessionType,
215240
this.sessionDate,
216241
true,
217242
this.sessionProxy,
218243
this.getMacaroonPermissions,
244+
accountId,
219245
);
220246

221247
if (session) {
@@ -224,6 +250,21 @@ export default class AddSessionView {
224250
}
225251
}
226252

253+
async registerCustodialAccount(): Promise<string | undefined> {
254+
try {
255+
const response = await this._store.api.lit.createAccount(
256+
this.custodialBalance,
257+
this.sessionDate,
258+
);
259+
260+
if (response.account) {
261+
return response.account.id;
262+
}
263+
} catch (error) {
264+
this._store.appView.handleError(error, 'Unable to register custodial account');
265+
}
266+
}
267+
227268
//
228269
// Private helper functions
229270
//

0 commit comments

Comments
 (0)