import { Api } from 'eosjs' ; import { convertLegacyPublicKey } from 'eosjs/dist/eosjs-numeric' ; import * as ecc from 'eosjs-ecc' ; import { Account } from './account' ; import { accountNameFromPublicKey } from './utils' ; import { EOSManager } from '../eosManager' ; import { ConfigManager , LamingtonDebugLevel } from '../configManager' ; import * as chalk from 'chalk' ; interface AccountCreationOptions { creator ? : Account ; eos ? : Api ; privateKey ? : string ; bytesToBuy ? : Number ; } export class AccountManager { note author Kevin Brown <github.com/thekevinbrown>
     *  param accountName param options returns static createAccount = async ( accountName ? , options ? ) => { let accountNameArray = accountName ? [ accountName ] : undefined ; const [ account ] = await AccountManager . createAccounts ( 1 , accountNameArray , options ) ; return account ; } ; author Kevin Brown <github.com/thekevinbrown>
     *  param numberOfAccounts param accountNames returns static createAccounts = async ( numberOfAccounts = 1 , accountNames ? , options ? ) => { const accounts = [ ] ; if ( accountNames ) { for ( let accountName of accountNames ) { const resolvedPrivateKey = options ?. privateKey ?? ( await ecc . unsafeRandomKey ( ) ) ; const publicKey = await ecc . privateToPublic ( resolvedPrivateKey ) ; const account = new Account ( accountName , resolvedPrivateKey ) ; await AccountManager . setupAccount ( account , options ) ; accounts . push ( account ) ; } } else { for ( let i = 0 ; i < numberOfAccounts ; i ++ ) { const resolvedPrivateKey = options ?. privateKey ?? ( await ecc . unsafeRandomKey ( ) ) ; const seedKey = await ecc . unsafeRandomKey ( ) ; const publicKey = await ecc . privateToPublic ( seedKey ) ; const accountName = accountNameFromPublicKey ( publicKey ) ; const account = new Account ( accountName , resolvedPrivateKey ) ; await AccountManager . setupAccount ( account , options ) ; accounts . push ( account ) ; } } return accounts ; } ; author Kevin Brown <github.com/thekevinbrown>
     *  param account param options returns static setupAccount = async ( account , options ? ) => { let logMessage = `Create account: ${ account . name }` ; if ( options ?. privateKey ) { logMessage += ` private key: ${ options . privateKey } ` ; } const { creator , eos } = AccountManager . flattenOptions ( options ) ; if ( ! account . name ) throw new Error ( 'Missing account name.' ) ; if ( ! account . publicKey ) throw new Error ( 'Missing public key.' ) ; if ( ! account . privateKey ) throw new Error ( 'Missing private key.' ) ; EOSManager . addSigningAccountIfMissing ( account ) ; const systemContract = await eos . getContract ( 'eosio' ) ; const actions = [ { account : 'eosio' , name : 'newaccount' , authorization : creator . active , data : { creator : creator . name , name : account . name , owner : { threshold : 1 , keys : [ { key : account . publicKey , weight : 1 , } , ] , accounts : [ ] , waits : [ ] , } , active : { threshold : 1 , keys : [ { key : account . publicKey , weight : 1 , } , ] , accounts : [ ] , waits : [ ] , } , } , } , ] ; if ( systemContract . actions . has ( 'buyrambytes' ) ) { const bytesToBuy = options ?. bytesToBuy ?? 4000000 ; logMessage += ` with ram: ${ bytesToBuy }` ; actions . push ( { account : 'eosio' , name : 'buyrambytes' , authorization : creator . active , data : { payer : creator . name , receiver : account . name , bytes : bytesToBuy , } , } ) ; } if ( systemContract . actions . has ( 'delegatebw' ) ) { const delegateAmount = '10.0000 EOS' ; logMessage += ` bandwidth: ${ delegateAmount } for net and cpu` ; actions . push ( { account : 'eosio' , name : 'delegatebw' , authorization : creator . active , data : { from : creator . name , receiver : account . name , stake_net_quantity : delegateAmount , stake_cpu_quantity : delegateAmount , transfer : false , } , } ) ; } if ( ConfigManager . debugLevelMin || ConfigManager . debugLevelVerbose ) { console . log ( chalk . cyan ( logMessage ) ) ; } return await EOSManager . transact ( { actions } , eos , { logMessage : logMessage , } ) ; } ; note author Kevin Brown <github.com/thekevinbrown>
     *  author Mitch Pierias <github.com/MitchPierias>
     *  param account public static addCodePermission = async ( account ) => { const { permissions } = await EOSManager . rpc . get_account ( account . name ) ; const { required_auth } = permissions . find ( ( permission ) => permission . perm_name == 'active' ) ; const existingPermission = required_auth . accounts . find ( ( account ) => account . permission . actor === account . name && account . permission . permission === 'eosio.code' ) ; if ( existingPermission ) { throw new Error ( `Code permission is already present on account ${ account . name } for contract ${ account . name }` ) ; } EOSManager . addSigningAccountIfMissing ( account ) ; required_auth . accounts . push ( { permission : { actor : account . name , permission : 'eosio.code' } , weight : 1 , } ) ; const actions = [ { account : 'eosio' , name : 'updateauth' , authorization : account . owner , data : { account : account . name , permission : 'active' , parent : 'owner' , auth : required_auth , } , } , ] ; await EOSManager . transact ( { actions } ) ; } ; author Kevin Brown <github.com/thekevinbrown>
     *  returns private static flattenOptions ( options ? ) { const creator = ( options && options . creator ) || EOSManager . adminAccount ; const eos = ( options && options . eos ) || EOSManager . api ; if ( ! creator ) throw new Error ( 'Creator not provided.' ) ; if ( ! eos ) throw new Error ( 'EOS instance not provided.' ) ; return { creator , eos } ; } }