Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/lib/http-server/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Account } from "@keetanetwork/keetanet-client/lib/account.js";
import type { Account, GenericAccount } from "@keetanetwork/keetanet-client/lib/account.js";
import { KeetaAnchorUserError } from "../error.js";
import { KeetaNet } from "../../client/index.js";
import { createAssertEquals } from "typia";
Expand All @@ -20,7 +20,12 @@ export interface HTTPSignedFieldURLParameters {
account: Account;
}

export function addSignatureToURL(input: URL | string, data: HTTPSignedFieldURLParameters): URL {
export interface HTTPSignedFieldURLParametersGenericAccount {
signedField: HTTPSignedField;
account: GenericAccount;
}

export function addSignatureToURL(input: URL | string, data: HTTPSignedFieldURLParametersGenericAccount): URL {
let url: URL;

if (typeof input === 'string') {
Expand All @@ -44,7 +49,9 @@ export function addSignatureToURL(input: URL | string, data: HTTPSignedFieldURLP
return(url);
}

export function parseSignatureFromURL(input: URL | string): Partial<HTTPSignedFieldURLParameters> {
export function parseSignatureFromURL(input: URL | string): Partial<HTTPSignedFieldURLParameters>;
export function parseSignatureFromURL(input: URL | string, options: { assertKeyed: false }): Partial<HTTPSignedFieldURLParametersGenericAccount>;
export function parseSignatureFromURL(input: URL | string, options?: { assertKeyed?: boolean }): Partial<HTTPSignedFieldURLParametersGenericAccount> {
let url: URL;

if (typeof input === 'string') {
Expand All @@ -53,7 +60,7 @@ export function parseSignatureFromURL(input: URL | string): Partial<HTTPSignedFi
url = new URL(input.toString());
}

const retVal: Partial<HTTPSignedFieldURLParameters> = {};
const retVal: Partial<HTTPSignedFieldURLParametersGenericAccount> = {};

const signedField = ((): HTTPSignedField | undefined => {
const nonce = url.searchParams.get('signed.nonce');
Expand All @@ -75,17 +82,14 @@ export function parseSignatureFromURL(input: URL | string): Partial<HTTPSignedFi
retVal.signedField = signedField;
}

const account = ((): Account | undefined => {
const accountParam = url.searchParams.get('account');
if (!accountParam) {
return(undefined);
const accountParam = url.searchParams.get('account');
if (accountParam) {
const parsed = KeetaNet.lib.Account.fromPublicKeyString(accountParam);
if (options?.assertKeyed === false) {
retVal.account = parsed;
} else {
retVal.account = parsed.assertAccount();
}

return(KeetaNet.lib.Account.fromPublicKeyString(accountParam).assertAccount());
})();

if (account) {
retVal.account = account;
}

return(retVal);
Expand Down
27 changes: 27 additions & 0 deletions src/lib/utils/storage-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { KeetaNet } from '../../client/index.js';

type KeetaNetClient = InstanceType<typeof KeetaNet.Client>;
type KeetaNetAccount = InstanceType<typeof KeetaNet.lib.Account>;

/**
* Resolve the owner of a storage account by querying the network
* for the ACL entry with the OWNER permission.
*
* @param client - A KeetaNet Client (or UserClient) to query the network
* @param storageAccount - The storage account to resolve the owner for
* @returns The owner (keyed) account
*/
export async function resolveStorageAccountOwner(client: KeetaNetClient, storageAccount: KeetaNetAccount): Promise<ReturnType<KeetaNetAccount['assertAccount']>> {
if (!storageAccount.isStorage()) {
throw(new Error('resolveStorageAccountOwner requires a storage account'));
}

const acls = await client.listACLsByEntity(storageAccount);
for (const acl of acls) {
if (acl.permissions.has(['OWNER'])) {
return(acl.principal.assertAccount());
}
}

throw(new Error('Storage account has no owner'));
}
Loading
Loading