Skip to content

Commit

Permalink
feat: Moshe authorizations to deploy backends
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaRahemtola committed Nov 6, 2024
1 parent e2f1f06 commit 212bd34
Showing 1 changed file with 33 additions and 53 deletions.
86 changes: 33 additions & 53 deletions src/utils/aleph-persistent-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,59 +87,39 @@ export class AlephPersistentStorage {
subAccount: BaseAccount,
accountClient: AuthenticatedAlephHttpClient,
) {
try {
// TODO: Add zod parsing
const securitySettings = (await accountClient.fetchAggregate(account.address, SECURITY_AGGREGATE_KEY)) as any;

type SecurityAuthorization = {
address: string;
chain?: string;
channels?: string;
types?: string;
post_types?: string;
aggregate_keys?: string;
};

if (
!securitySettings.authorizations.find(
(authorization: SecurityAuthorization) =>
authorization.address === subAccount.address &&
authorization.types === undefined &&
authorization.channels?.includes(LIBERTAI_GENERAL_CHANNEL) &&
authorization.channels?.includes(LIBERTAI_UI_CHANNEL),
)
) {
const oldAuthorizations = securitySettings.authorizations.filter(
(a: SecurityAuthorization) => a.address !== subAccount.address,
);

await accountClient.createAggregate({
key: SECURITY_AGGREGATE_KEY,
content: {
authorizations: [
...oldAuthorizations,
{
address: subAccount.address,
channels: [LIBERTAI_GENERAL_CHANNEL, LIBERTAI_UI_CHANNEL],
},
],
},
});
}
} catch (error) {
// Security aggregate does not exist or with invalid content, creating a new one
await accountClient.createAggregate({
key: SECURITY_AGGREGATE_KEY,
content: {
authorizations: [
{
address: subAccount.address,
channels: [LIBERTAI_GENERAL_CHANNEL, LIBERTAI_UI_CHANNEL],
},
],
},
});
}
type SecurityAuthorization = {
address: string;
chain?: string;
channels?: string[];
types?: string[];
post_types?: string[];
aggregate_keys?: string[];
};

const agentsAuthorization: SecurityAuthorization = {
chain: 'ETH',
address: '0x501DD421f2B9F96E3e20945f42aeCBDd4A607a03',
channels: ['libertai', 'libertai-agents'],
types: ['POST', 'STORE', 'PROGRAM'],
post_types: ['libertai-agent'],
};

const subscriptionsAuthorization: SecurityAuthorization = {
chain: 'ETH',
address: '0x964c8A4cDabDa3c1f2E84662714563684599f76F',
channels: ['libertai', 'libertai-subscriptions'],
types: ['POST'],
post_types: ['libertai-subscription'],
};

const mosheAuthorizations: SecurityAuthorization[] = [agentsAuthorization, subscriptionsAuthorization];

await accountClient.createAggregate({
key: SECURITY_AGGREGATE_KEY,
content: {
authorizations: mosheAuthorizations,
},
});
}

private static async getAlephAccount(chain: AccountChain): Promise<BaseAccount | SOLAccount> {
Expand Down

0 comments on commit 212bd34

Please sign in to comment.