diff --git a/packages/sdk/src/modules/provider/BakoProvider.ts b/packages/sdk/src/modules/provider/BakoProvider.ts index 2043f4f..5b59a4a 100644 --- a/packages/sdk/src/modules/provider/BakoProvider.ts +++ b/packages/sdk/src/modules/provider/BakoProvider.ts @@ -12,6 +12,7 @@ import { IPredicatePayload, ISignTransaction, TransactionStatus, + CLIAuth, } from '../service'; import { @@ -19,6 +20,7 @@ import { BakoProviderSetup, BakoProviderOptions, BakoProviderAuthOptions, + BakoCustomProviderOptions, } from './types'; import { Vault } from '../vault'; @@ -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. @@ -76,9 +79,28 @@ export class BakoProvider extends Provider { */ static async create( url: string, - options: BakoProviderOptions, + options: BakoProviderOptions | BakoCustomProviderOptions, ): Promise { - 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; } diff --git a/packages/sdk/src/modules/provider/types.ts b/packages/sdk/src/modules/provider/types.ts index 103198c..4fa679e 100644 --- a/packages/sdk/src/modules/provider/types.ts +++ b/packages/sdk/src/modules/provider/types.ts @@ -7,6 +7,10 @@ export type BakoProviderOptions = ProviderOptions & { serverApi?: string; }; +export type BakoCustomProviderOptions = ProviderOptions & { + apiToken: string; +}; + export type BakoProviderAuthOptions = BakoProviderOptions & { challenge: string; encoder?: TypeUser; diff --git a/packages/sdk/src/modules/service/service.ts b/packages/sdk/src/modules/service/service.ts index 7b15f2d..c7fc430 100644 --- a/packages/sdk/src/modules/service/service.ts +++ b/packages/sdk/src/modules/service/service.ts @@ -14,6 +14,8 @@ import { CreateSessionResponse, TransactionBakoResponse, UserAuthResponse, + CLIAuthPayload, + CLIAuth, } from './types'; // keep here to sync with the other files @@ -186,4 +188,10 @@ export class Service { return !!data; } + + static async cliAuth(params: CLIAuthPayload): Promise { + const { data } = await api.post('/cli/auth', params); + + return data; + } } diff --git a/packages/sdk/src/modules/service/types.ts b/packages/sdk/src/modules/service/types.ts index 7f67131..3a27353 100644 --- a/packages/sdk/src/modules/service/types.ts +++ b/packages/sdk/src/modules/service/types.ts @@ -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; + }; +}; diff --git a/packages/sdk/src/modules/vault/Vault.ts b/packages/sdk/src/modules/vault/Vault.ts index 828c784..9572ed2 100644 --- a/packages/sdk/src/modules/vault/Vault.ts +++ b/packages/sdk/src/modules/vault/Vault.ts @@ -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; @@ -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. *