Skip to content

Commit

Permalink
feat(auth): enable to authenticate by api token
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburigo committed Dec 18, 2024
1 parent df01d28 commit 9d1633b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
26 changes: 24 additions & 2 deletions packages/sdk/src/modules/provider/BakoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import {
IPredicatePayload,
ISignTransaction,
TransactionStatus,
CLIAuth,
} from '../service';

import {
TypeUser,
BakoProviderSetup,
BakoProviderOptions,
BakoProviderAuthOptions,
BakoCustomProviderOptions,
} from './types';

import { Vault } from '../vault';
Expand All @@ -31,6 +33,7 @@ import { Vault } from '../vault';
export class BakoProvider extends Provider {
public options: BakoProviderOptions;
public service: Service;
public cliAuth?: CLIAuth;

/**
* Protected constructor to initialize BakoProvider.
Expand Down Expand Up @@ -76,9 +79,28 @@ export class BakoProvider extends Provider {
*/
static async create(
url: string,
options: BakoProviderOptions,
options: BakoProviderOptions | BakoCustomProviderOptions,
): Promise<BakoProvider> {
const provider = new BakoProvider(url, options);
if ('apiToken' in options && options.apiToken) {
const { apiToken, ...rest } = options;
const providerFuel = await Provider.create(url);
const cliAuth = await Service.cliAuth({
token: options.apiToken,
network: {
url: providerFuel.url,
chainId: providerFuel.getChainId(),
},
});
const provider = new BakoProvider(url, {
...rest,
token: cliAuth.code,
address: cliAuth.address,
});
provider.cliAuth = cliAuth;
return provider;
}

const provider = new BakoProvider(url, options as BakoProviderOptions);
await provider.fetchChainAndNodeInfo();
return provider;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/src/modules/provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export type BakoProviderOptions = ProviderOptions & {
serverApi?: string;
};

export type BakoCustomProviderOptions = ProviderOptions & {
apiToken: string;
};

export type BakoProviderAuthOptions = BakoProviderOptions & {
challenge: string;
encoder?: TypeUser;
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/src/modules/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
CreateSessionResponse,
TransactionBakoResponse,
UserAuthResponse,
CLIAuthPayload,
CLIAuth,
} from './types';

// keep here to sync with the other files
Expand Down Expand Up @@ -186,4 +188,10 @@ export class Service {

return !!data;
}

static async cliAuth(params: CLIAuthPayload): Promise<CLIAuth> {
const { data } = await api.post<CLIAuth>('/cli/auth', params);

return data;
}
}
21 changes: 21 additions & 0 deletions packages/sdk/src/modules/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,24 @@ export interface Workspace {
name: string;
avatar: string;
}

export type CLIAuthPayload = {
token: string;
network: {
url: string;
chainId: number;
};
};

export type CLIAuth = {
code: string;
address: string;
configurable: {
SIGNATURES_COUNT: number;
SIGNERS: string[];
HASH_PREDICATE: string;
};
tokenConfig: {
transactionTitle: string;
};
};
28 changes: 26 additions & 2 deletions packages/sdk/src/modules/vault/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,33 @@ export class Vault extends Predicate<[]> {
* @param {Provider | BakoProvider} provider - The provider instance
* @param {VaultConfigurable} configurable - The configuration for the vault, including signature requirements.
*/
constructor(provider: BakoProvider);
constructor(
provider: Provider | BakoProvider,
configurable: VaultConfigurable,
version?: string,
);
constructor(
provider: Provider | BakoProvider,
configurable?: VaultConfigurable,
version?: string,
) {
const conf = Vault.makePredicate(configurable);
let conf = configurable;

if ('cliAuth' in provider && provider.cliAuth) {
conf = provider.cliAuth.configurable;
}

if (!conf) {
throw new Error('Vault configurable is required');
}

const BakoPredicateLoader = loadPredicate(provider.url, version);
super({
abi: BakoPredicateLoader.abi,
bytecode: arrayify(BakoPredicateLoader.bytecode),
provider: provider,
configurableConstants: conf,
configurableConstants: Vault.makePredicate(conf),
});

this.predicateVersion = BakoPredicateLoader.version;
Expand Down Expand Up @@ -116,6 +131,15 @@ export class Vault extends Predicate<[]> {
};
}

async sendTransaction(transactionRequestLike: TransactionRequestLike) {
if ('address' in this.provider.options && this.provider.options.address) {
const { hashTxId } = await this.BakoTransfer(transactionRequestLike);
return new TransactionResponse(hashTxId, this.provider);
}

return super.sendTransaction(transactionRequestLike);
}

/**
* Sends a transaction to the blockchain using the vault resources.
*
Expand Down

0 comments on commit 9d1633b

Please sign in to comment.