UNPKG

@velas/account-agent

Version:

sdk

123 lines (103 loc) 5.25 kB
import BN from 'bn.js'; /* * Helpers for scopes */ function ScopesConstructor(options = {}) { if (!options.account_contract) throw new Error('account_contract option is required') this.VELAS_ACCOUNT_PROGRAM_ADDRESS = options.account_contract; this.STAKE_PROGRAM_ADDRESS = 'Stake11111111111111111111111111111111111111'; this.SYSTEM_PROGRAM_ADDRESS = '11111111111111111111111111111111'; this.SYSVAR_RENT_ADDRESS = 'SysvarRent111111111111111111111111111111111'; this.EVM_PROGRAM_ADDRESS = 'EVM1111111111111111111111111111111111111111'; this.EVM_STATE_PROGRAM_ADDRESS = 'EvmState11111111111111111111111111111111111'; this.PERMITTED_SCOPES = [ 'authorization', 'openid' ]; this.MOBILE_FLOW_REQUIRED_SCOPES = [ `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:2` ]; this.SCOPES = { [`${this.STAKE_PROGRAM_ADDRESS}:0`]: 'StakeProgram:Initialize', [`${this.STAKE_PROGRAM_ADDRESS}:1`]: 'StakeProgram:Authorize', [`${this.STAKE_PROGRAM_ADDRESS}:2`]: 'StakeProgram:Delegate', [`${this.STAKE_PROGRAM_ADDRESS}:3`]: 'StakeProgram:Split', [`${this.STAKE_PROGRAM_ADDRESS}:4`]: 'StakeProgram:Withdraw', [`${this.STAKE_PROGRAM_ADDRESS}:5`]: 'StakeProgram:Deactivate', [`${this.STAKE_PROGRAM_ADDRESS}:8`]: 'StakeProgram:AuthorizeWithSeed', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:0` ]: 'VelasAccountProgram:Initialize', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:1` ]: 'VelasAccountProgram:AddOwner', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:2` ]: 'VelasAccountProgram:AddOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:3` ]: 'VelasAccountProgram:ExtendOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:4` ]: 'VelasAccountProgram:MergeOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:5` ]: 'VelasAccountProgram:ReplaceOwner', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:6` ]: 'VelasAccountProgram:RemoveOwner', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:7` ]: 'VelasAccountProgram:RemoveOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:8` ]: 'VelasAccountProgram:LockOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:9` ]: 'VelasAccountProgram:UnlockOperational', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:10`]: 'VelasAccountProgram:Execute', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:11`]: 'VelasAccountProgram:Transfer', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:12`]: 'VelasAccountProgram:CreateAccount', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:13`]: 'VelasAccountProgram:CreateAccountWithSeed', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:19`]: 'VelasAccountProgram:RemoveProgramPermission', [`${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:20`]: 'VelasAccountProgram:SponsorAndExecute', [`${this.EVM_PROGRAM_ADDRESS}:4`]: 'EVM:Execute' }; this.NOT_EPHEMERAL_SCOPES = [ `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:0`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:1`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:2`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:3`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:4`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:5`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:6`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:7`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:8`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:9`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:12`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:13`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:14`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:15`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:16`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:17`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:18`, `${this.VELAS_ACCOUNT_PROGRAM_ADDRESS}:19`, `VRPLtk4k31bDL99mn1A5mE96CUUzQ9PnftEwf2LvMiG:0` ]; this.createRPAccountInstuctionAddress = "VRPLtk4k31bDL99mn1A5mE96CUUzQ9PnftEwf2LvMiG"; }; ScopesConstructor.prototype.swap = function() { var ret = {}; for(var key in this.SCOPES){ ret[this.SCOPES[key]] = key; } return ret; }; ScopesConstructor.prototype.encode = function(array) { return array.map(item => this.SCOPES[item] || item); }; ScopesConstructor.prototype.decode = function(array) { const inv = this.swap(); return array.map(item => inv[item] || item); }; ScopesConstructor.prototype.bitsToScopes = function(scopes, size = 32) { const bn = new BN(new Uint8Array(scopes)); const length = bn.bitLength(); const bits = Array(size - 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; }, {}); }; ScopesConstructor.prototype.chunkArray = function(myArray, chunk_size) { var index = 0; var arrayLength = myArray.length; var tempArray = []; for (index = 0; index < arrayLength; index += chunk_size) { const myChunk = myArray.slice(index, index+chunk_size).slice(0, 32); // REFACTORING tempArray.push(myChunk); } return tempArray; }; export default ScopesConstructor;