diff --git a/packages/tree-builder/src/applications/example/create_accounts.ts b/packages/tree-builder/src/applications/example/create_accounts.ts index cfdad85..49b39a4 100644 --- a/packages/tree-builder/src/applications/example/create_accounts.ts +++ b/packages/tree-builder/src/applications/example/create_accounts.ts @@ -1,12 +1,10 @@ import * as fs from 'fs'; import { address } from '@liskhq/lisk-cryptography'; import { Account, ExampleKey } from '../../interface'; +import { randomBalanceBeddows } from '../../utils'; -// 1 LSK = 10^8 Beddows -const LSK_MULTIPLIER = 10 ** 8; - -// Balances are random between 0 - -const RANDOM_RANGE = 10000; +// Random Balance in Beddows between 0 - 2 ** 8 * (RANDOM_BYTES_RANGE) +const RANDOM_BYTES_RANGE = 5; // Multisig Accounts // For each account it will use the address of the index as account holder, @@ -41,8 +39,6 @@ const multiSigs = [ }, ]; -const randomBalance = (range: number): number => Number((range * Math.random()).toFixed(8)); - export function createAccounts(numberOfAccounts = 54) { const keyPairs = JSON.parse( fs.readFileSync('../../data/example/key-pairs.json', 'utf-8'), @@ -60,8 +56,7 @@ export function createAccounts(numberOfAccounts = 54) { // Regular Accounts for (let index = 0; index < numberOfAccounts - multiSigs.length; index++) { const account = sortedKeyPairs[index]; - const balance = randomBalance(RANDOM_RANGE); - const balanceBeddows = Math.round(balance * LSK_MULTIPLIER); + const balanceBeddows = randomBalanceBeddows(RANDOM_BYTES_RANGE); results.push({ lskAddress: account.address, @@ -71,8 +66,7 @@ export function createAccounts(numberOfAccounts = 54) { for (const multiSig of multiSigs) { const account = sortedKeyPairs[results.length]; - const balance = randomBalance(RANDOM_RANGE); - const balanceBeddows = Math.round(balance * LSK_MULTIPLIER).toString(); + const balanceBeddows = randomBalanceBeddows(RANDOM_BYTES_RANGE); results.push({ lskAddress: account.address, diff --git a/packages/tree-builder/src/applications/example/create_key_pairs.ts b/packages/tree-builder/src/applications/example/create_key_pairs.ts index 8fefcbf..5361187 100644 --- a/packages/tree-builder/src/applications/example/create_key_pairs.ts +++ b/packages/tree-builder/src/applications/example/create_key_pairs.ts @@ -5,10 +5,12 @@ import { ExampleKey } from '../../interface'; const initialPath = "m/44'/134'"; export async function createKeyPairs(amount = 100) { + const mnemonic = + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; const keys: ExampleKey[] = []; for (let i = 0; i < amount; i++) { const keyPath = `${initialPath}/${i}'`; - const privateKey = await ed.getPrivateKeyFromPhraseAndPath('lisk', keyPath); + const privateKey = await ed.getPrivateKeyFromPhraseAndPath(mnemonic, keyPath); keys.push({ address: address.getLisk32AddressFromAddress(address.getAddressFromPrivateKey(privateKey)), keyPath, diff --git a/packages/tree-builder/src/constants.ts b/packages/tree-builder/src/constants.ts index da9c525..35b5e38 100644 --- a/packages/tree-builder/src/constants.ts +++ b/packages/tree-builder/src/constants.ts @@ -1,8 +1,5 @@ import { computeStorePrefix } from './utils'; -// 1 LSK = 10^8 Beddows -export const LSK_MULTIPLIER = 10 ** 8; - // Each leaf will be encoded in the following order: // LSK_ADDRESS_IN_HEX: bytes20 // BALANCE_IN_BEDDOWS: uint64 diff --git a/packages/tree-builder/src/utils.ts b/packages/tree-builder/src/utils.ts index ba9f6b8..4662b33 100644 --- a/packages/tree-builder/src/utils.ts +++ b/packages/tree-builder/src/utils.ts @@ -29,3 +29,8 @@ export function getTotalBalance(balance: UserBalance): bigint { balance.lockedBalances.reduce((acc, cur) => acc + cur.amount, BigInt(0)) ); } + +// When bytes = 8, max value = 2 ** (8 * 8) - 1 +export function randomBalanceBeddows(maxBytes = 8): string { + return BigInt(append0x(utils.getRandomBytes(maxBytes))).toString(); +} diff --git a/packages/tree-builder/test/applications/build_tree.test.ts b/packages/tree-builder/test/applications/build_tree.test.ts index f6dd47f..8d3a938 100644 --- a/packages/tree-builder/test/applications/build_tree.test.ts +++ b/packages/tree-builder/test/applications/build_tree.test.ts @@ -6,9 +6,9 @@ import { StandardMerkleTree } from '@openzeppelin/merkle-tree'; import { defaultAbiCoder } from '@ethersproject/abi'; import { Account, ExampleKey } from '../../src/interface'; import { createPayload, buildTree } from '../../src/applications/generate-merkle-tree/build_tree'; -import { LEAF_ENCODING, LSK_MULTIPLIER } from '../../src/constants'; +import { LEAF_ENCODING } from '../../src/constants'; import { createKeyPairs } from '../../src/applications/example/create_key_pairs'; -import { append0x } from '../../src/utils'; +import { append0x, randomBalanceBeddows } from '../../src/utils'; describe('buildTree', () => { let accounts: Account[]; @@ -24,12 +24,11 @@ describe('buildTree', () => { // Create 5 accounts on the fly, they are all Multisig such that all fields are filled accounts = keyPairsSorted.slice(0, 5).map(key => { - const balance = Number((10000 * Math.random()).toFixed(8)); const numberOfMandatoryKeys = Math.floor(5 * Math.random()) + 1; const numberOfOptionalKeys = Math.floor(5 * Math.random()); return { lskAddress: key.address, - balanceBeddows: Math.floor(balance * LSK_MULTIPLIER).toString(), + balanceBeddows: randomBalanceBeddows(), numberOfSignatures: numberOfMandatoryKeys + numberOfOptionalKeys, mandatoryKeys: keyPairsSorted.slice(0, numberOfMandatoryKeys).map(key => key.publicKey), optionalKeys: keyPairsSorted