Created
June 6, 2022 21:27
-
-
Save SuperstrongBE/c3239e6c033de75cbfbefd4c7a3731fc to your computer and use it in GitHub Desktop.
EosService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
generateCreateAccountAction, //Just a function that return a prebuild action | |
generateBuyRamBytesAction, //Just a function that return a prebuild action | |
} from '../utils/proton/actions/EosActions'; | |
import { ProtonRPC } from '../utils/proton/rpc/ProtonRPC'; | |
export class EosService { | |
async isAccountExist(newAccountName: string): Promise<boolean> { | |
const rpc = new ProtonRPC(); | |
const account = await rpc.getAccount(newAccountName); | |
if (account) return true; | |
return false; | |
} | |
// Public key is generated using the AccountKeyGenerator below | |
async createAccount(newAccountName: string, publicKey: string) { | |
const rpc = new ProtonRPC(); | |
if (!process.env.EOSIO_ACTOR_NAME) | |
throw new Error('Creator account name is missing'); | |
if (!process.env.EOSIO_PUBLIC) | |
throw new Error('Creator public key is missing'); | |
const accountActions = [ | |
generateCreateAccountAction({ | |
creatorAccountName: process.env.EOSIO_ACTOR_NAME, | |
creatorPublicKey: publicKey, | |
newAccountName, | |
}), | |
generateBuyRamBytesAction({ | |
creatorAccountName: process.env.EOSIO_ACTOR_NAME, | |
creatorPublicKey: publicKey, | |
newAccountName, | |
}), | |
]; | |
return await rpc.api.transact( | |
{ actions: accountActions }, | |
{ | |
broadcast: true, | |
blocksBehind: 3, | |
expireSeconds: 30, | |
}, | |
); | |
} | |
} | |
// The class bellow is used to generate keys for the account | |
// we sent the mnemonic by email at the end of the process and never store private or mnemonic | |
import { Mnemonic } from '@proton/mnemonic'; | |
export interface AccountKeys { | |
mnemonic: string; | |
privateKey: string; | |
publicKey: string; | |
} | |
export class AccountKeyGenerator { | |
static async GenerateKeys() { | |
const mnemonic = new Mnemonic({ | |
numWords: 12, | |
}); | |
const { privateKey, publicKey } = mnemonic.keyPairAtIndex(0); | |
return <AccountKeys>{ | |
mnemonic: mnemonic.phrase, | |
privateKey: privateKey.toString(), | |
publicKey: publicKey.toString(), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment