UNPKG

@velas/account-agent

Version:

sdk

109 lines (83 loc) 3.79 kB
import bs58 from 'bs58'; import BN from 'bn.js' /* * Process the Submission */ function bitsToScopes(scopes) { const bn = new BN(new Uint8Array(scopes)); const length = bn.bitLength(); const bits = Array(256 - length).fill("0").concat(bn.toString(2).split('')); return bits.reduce((a,b,i) => { if (i === 0) a = []; if (b === '1') a.push(i); return a; }, {}); }; async function processSubmission(next) { const { client_id } = this.interaction.client; this.interaction.result = this.interaction.result ? this.result.mergeWithLastSubmission ? { ...this.interaction.result, ...this.result } : this.result : this.result; if (this.interaction.result.login) { const data = { account: this.interaction.result.login, op_key: this.interaction.op_key, } this.interaction.session = new this.provider.Session(data); }; if (this.interaction.result.select_account) { const session = await this.provider.Session.findById(this.interaction.result.select_account.op_key); this.interaction.session = session; }; if ( this.interaction.result.marge && this.interaction.session && this.interaction.stage === 'merge' ) { const { session: {account, op_key}, sessions} = this.interaction; const selected = sessions.reduce((result,i)=> { if (i.account === account && i.op_key !== op_key) result = i return result }); if (selected) { if (selected.count === 0) { const session = await this.provider.Session.findById(selected.op_key); await session.destroy(); } else { const old_session = await this.provider.Session.findById(op_key); await old_session.destroy(); const session = await this.provider.Session.findById(selected.op_key); this.interaction.session = session; this.interaction.op_key = session.op_key; }; }; }; if (this.interaction.session) { this.interaction.session.op_key_scopes = []; try { const account_data = await this.provider.client.getAccountData(this.interaction.session.account); const account_scopes = account_data.operational_keys[this.interaction.session.op_key].scopes || []; const all_programs = await this.provider.client.getAccountPrograms(account_data); const external_programs_indices = bitsToScopes(account_data.operational_keys[this.interaction.session.op_key].external_programs_indices); account_scopes.forEach((scope) => { this.interaction.session.op_key_scopes.push(`${this.provider.sc.VELAS_ACCOUNT_PROGRAM_ADDRESS}:${scope}`); }); external_programs_indices.forEach((index) => { this.interaction.session.op_key_scopes.push(all_programs[index]); }); } catch (_) {}; }; if ( this.interaction.result.consent && this.interaction.session ) { const { rejectedScopes, replace = false } = this.interaction.result.consent; if (rejectedScopes) { this.interaction.session.rejectedScopesFor(client_id, rejectedScopes, replace); } const requestParamScopes = new Set(); const scopes = this.interaction.params.scope; scopes.forEach((scope) => { requestParamScopes.add(scope); }); this.interaction.session.promptedScopesFor(client_id, requestParamScopes); }; if (this.interaction.session) { await this.interaction.session.save(); }; await next(); }; export default processSubmission;