Skip to content

Commit

Permalink
Adds ability to provide account names for creating new accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
dallasjohnson committed Dec 14, 2019
1 parent 6193848 commit 3bc3797
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/accounts/accountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,49 @@ export class AccountManager {
* Generates a new random account
* @note Shorthand method for [[AccountManager.createAccounts]]
* @author Kevin Brown <github.com/thekevinbrown>
* @param accountName Optional name for the account to create
* @param options Optional account creation settings
* @returns Result returned from [[AccountManager.createAccounts]]
*/
static createAccount = async (options?: AccountCreationOptions) => {
const [account] = await AccountManager.createAccounts(1, options);
static createAccount = async (accountName?: string, options?: AccountCreationOptions) => {
let accountNameArray = accountName ? [accountName] : undefined;
const [account] = await AccountManager.createAccounts(1, accountNameArray, options);
return account;
};

/**
* Generates a specified number of random accounts
* @author Kevin Brown <github.com/thekevinbrown>
* @param numberOfAccounts Number of accounts to generate
* @param accountNames Array of account names. If array is provided then the numberOfAccounts is ignored.
* @returns Array of created account transaction promises
*/
static createAccounts = async (numberOfAccounts = 1, options?: AccountCreationOptions) => {
static createAccounts = async (
numberOfAccounts = 1,
accountNames?: string[],
options?: AccountCreationOptions
) => {
const accounts = [];
// Repeat account creation for specified
for (let i = 0; i < numberOfAccounts; i++) {
const privateKey = await ecc.unsafeRandomKey();
const publicKey = await ecc.privateToPublic(privateKey);
const accountName = accountNameFromPublicKey(publicKey);
const account = new Account(accountName, privateKey);
// Publish the new account and store result
await AccountManager.setupAccount(account, options);
accounts.push(account);
if (accountNames) {
for (let accountName of accountNames) {
const privateKey = await ecc.unsafeRandomKey();
const publicKey = await ecc.privateToPublic(privateKey);
const account = new Account(accountName, privateKey);
// Publish the new account and store result
await AccountManager.setupAccount(account, options);
accounts.push(account);
}
} else {
// Repeat account creation for specified
for (let i = 0; i < numberOfAccounts; i++) {
const privateKey = await ecc.unsafeRandomKey();
const publicKey = await ecc.privateToPublic(privateKey);
const accountName = accountNameFromPublicKey(publicKey);
const account = new Account(accountName, privateKey);
// Publish the new account and store result
await AccountManager.setupAccount(account, options);
accounts.push(account);
}
}
// Return created acounts
return accounts;
Expand Down

0 comments on commit 3bc3797

Please sign in to comment.